1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2003, 2004 David Young. All rights reserved. |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * 3. The name of David Young may not be used to endorse or promote |
---|
15 | * products derived from this software without specific prior |
---|
16 | * written permission. |
---|
17 | * |
---|
18 | * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY |
---|
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
---|
20 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
---|
21 | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID |
---|
22 | * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
---|
24 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
---|
29 | * OF SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | #ifdef HAVE_CONFIG_H |
---|
33 | #include "config.h" |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <stdlib.h> |
---|
37 | #include <string.h> |
---|
38 | #include <tcpdump-stdinc.h> |
---|
39 | |
---|
40 | #include "cpack.h" |
---|
41 | #include "extract.h" |
---|
42 | |
---|
43 | u_int8_t * |
---|
44 | cpack_next_boundary(u_int8_t *buf, u_int8_t *p, size_t alignment) |
---|
45 | { |
---|
46 | size_t misalignment = (size_t)(p - buf) % alignment; |
---|
47 | |
---|
48 | if (misalignment == 0) |
---|
49 | return p; |
---|
50 | |
---|
51 | return p + (alignment - misalignment); |
---|
52 | } |
---|
53 | |
---|
54 | /* Advance to the next wordsize boundary. Return NULL if fewer than |
---|
55 | * wordsize bytes remain in the buffer after the boundary. Otherwise, |
---|
56 | * return a pointer to the boundary. |
---|
57 | */ |
---|
58 | u_int8_t * |
---|
59 | cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize) |
---|
60 | { |
---|
61 | u_int8_t *next; |
---|
62 | |
---|
63 | /* Ensure alignment. */ |
---|
64 | next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize); |
---|
65 | |
---|
66 | /* Too little space for wordsize bytes? */ |
---|
67 | if (next - cs->c_buf + wordsize > cs->c_len) |
---|
68 | return NULL; |
---|
69 | |
---|
70 | return next; |
---|
71 | } |
---|
72 | |
---|
73 | int |
---|
74 | cpack_init(struct cpack_state *cs, u_int8_t *buf, size_t buflen) |
---|
75 | { |
---|
76 | memset(cs, 0, sizeof(*cs)); |
---|
77 | |
---|
78 | cs->c_buf = buf; |
---|
79 | cs->c_len = buflen; |
---|
80 | cs->c_next = cs->c_buf; |
---|
81 | |
---|
82 | return 0; |
---|
83 | } |
---|
84 | |
---|
85 | /* Unpack a 64-bit unsigned integer. */ |
---|
86 | int |
---|
87 | cpack_uint64(struct cpack_state *cs, u_int64_t *u) |
---|
88 | { |
---|
89 | u_int8_t *next; |
---|
90 | |
---|
91 | if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL) |
---|
92 | return -1; |
---|
93 | |
---|
94 | *u = EXTRACT_LE_64BITS(next); |
---|
95 | |
---|
96 | /* Move pointer past the u_int64_t. */ |
---|
97 | cs->c_next = next + sizeof(*u); |
---|
98 | return 0; |
---|
99 | } |
---|
100 | |
---|
101 | /* Unpack a 32-bit unsigned integer. */ |
---|
102 | int |
---|
103 | cpack_uint32(struct cpack_state *cs, u_int32_t *u) |
---|
104 | { |
---|
105 | u_int8_t *next; |
---|
106 | |
---|
107 | if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL) |
---|
108 | return -1; |
---|
109 | |
---|
110 | *u = EXTRACT_LE_32BITS(next); |
---|
111 | |
---|
112 | /* Move pointer past the u_int32_t. */ |
---|
113 | cs->c_next = next + sizeof(*u); |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | /* Unpack a 16-bit unsigned integer. */ |
---|
118 | int |
---|
119 | cpack_uint16(struct cpack_state *cs, u_int16_t *u) |
---|
120 | { |
---|
121 | u_int8_t *next; |
---|
122 | |
---|
123 | if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL) |
---|
124 | return -1; |
---|
125 | |
---|
126 | *u = EXTRACT_LE_16BITS(next); |
---|
127 | |
---|
128 | /* Move pointer past the u_int16_t. */ |
---|
129 | cs->c_next = next + sizeof(*u); |
---|
130 | return 0; |
---|
131 | } |
---|
132 | |
---|
133 | /* Unpack an 8-bit unsigned integer. */ |
---|
134 | int |
---|
135 | cpack_uint8(struct cpack_state *cs, u_int8_t *u) |
---|
136 | { |
---|
137 | /* No space left? */ |
---|
138 | if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len) |
---|
139 | return -1; |
---|
140 | |
---|
141 | *u = *cs->c_next; |
---|
142 | |
---|
143 | /* Move pointer past the u_int8_t. */ |
---|
144 | cs->c_next++; |
---|
145 | return 0; |
---|
146 | } |
---|