xref: /freebsd/contrib/tcpdump/cpack.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*-
2  * Copyright (c) 2003, 2004 David Young.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of David Young may not be used to endorse or promote
13  *    products derived from this software without specific prior
14  *    written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DAVID
20  * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27  * OF SUCH DAMAGE.
28  */
29 
30 #include <config.h>
31 
32 #include <stdlib.h>
33 #include <string.h>
34 #include "netdissect-stdinc.h"
35 
36 #include "netdissect.h"
37 #include "extract.h"
38 
39 #include "cpack.h"
40 
41 const uint8_t *
42 nd_cpack_next_boundary(const uint8_t *buf, const uint8_t *p, size_t alignment)
43 {
44 	size_t misalignment = (size_t)(p - buf) % alignment;
45 
46 	if (misalignment == 0)
47 		return p;
48 
49 	return p + (alignment - misalignment);
50 }
51 
52 /* Advance to the next wordsize boundary. Return NULL if fewer than
53  * wordsize bytes remain in the buffer after the boundary.  Otherwise,
54  * return a pointer to the boundary.
55  */
56 const uint8_t *
57 nd_cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
58 {
59 	const uint8_t *next;
60 
61 	/* Ensure alignment. */
62 	next = nd_cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
63 
64 	/* Too little space for wordsize bytes? */
65 	if (next - cs->c_buf + wordsize > cs->c_len)
66 		return NULL;
67 
68 	return next;
69 }
70 
71 /* Advance by N bytes without returning them. */
72 int
73 nd_cpack_advance(struct cpack_state *cs, const size_t toskip)
74 {
75 	/* No space left? */
76 	if (cs->c_next - cs->c_buf + toskip > cs->c_len)
77 		return -1;
78 	cs->c_next += toskip;
79 	return 0;
80 }
81 
82 int
83 nd_cpack_init(struct cpack_state *cs, const uint8_t *buf, size_t buflen)
84 {
85 	memset(cs, 0, sizeof(*cs));
86 
87 	cs->c_buf = buf;
88 	cs->c_len = buflen;
89 	cs->c_next = cs->c_buf;
90 
91 	return 0;
92 }
93 
94 /* Unpack a 64-bit unsigned integer. */
95 int
96 nd_cpack_uint64(netdissect_options *ndo, struct cpack_state *cs, uint64_t *u)
97 {
98 	const uint8_t *next;
99 
100 	if ((next = nd_cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
101 		return -1;
102 
103 	*u = GET_LE_U_8(next);
104 
105 	/* Move pointer past the uint64_t. */
106 	cs->c_next = next + sizeof(*u);
107 	return 0;
108 }
109 
110 /* Unpack a 64-bit signed integer. */
111 int
112 nd_cpack_int64(netdissect_options *ndo, struct cpack_state *cs, int64_t *u)
113 {
114 	const uint8_t *next;
115 
116 	if ((next = nd_cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
117 		return -1;
118 
119 	*u = GET_LE_S_8(next);
120 
121 	/* Move pointer past the int64_t. */
122 	cs->c_next = next + sizeof(*u);
123 	return 0;
124 }
125 
126 /* Unpack a 32-bit unsigned integer. */
127 int
128 nd_cpack_uint32(netdissect_options *ndo, struct cpack_state *cs, uint32_t *u)
129 {
130 	const uint8_t *next;
131 
132 	if ((next = nd_cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
133 		return -1;
134 
135 	*u = GET_LE_U_4(next);
136 
137 	/* Move pointer past the uint32_t. */
138 	cs->c_next = next + sizeof(*u);
139 	return 0;
140 }
141 
142 /* Unpack a 32-bit signed integer. */
143 int
144 nd_cpack_int32(netdissect_options *ndo, struct cpack_state *cs, int32_t *u)
145 {
146 	const uint8_t *next;
147 
148 	if ((next = nd_cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
149 		return -1;
150 
151 	*u = GET_LE_S_4(next);
152 
153 	/* Move pointer past the int32_t. */
154 	cs->c_next = next + sizeof(*u);
155 	return 0;
156 }
157 
158 /* Unpack a 16-bit unsigned integer. */
159 int
160 nd_cpack_uint16(netdissect_options *ndo, struct cpack_state *cs, uint16_t *u)
161 {
162 	const uint8_t *next;
163 
164 	if ((next = nd_cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
165 		return -1;
166 
167 	*u = GET_LE_U_2(next);
168 
169 	/* Move pointer past the uint16_t. */
170 	cs->c_next = next + sizeof(*u);
171 	return 0;
172 }
173 
174 /* Unpack a 16-bit signed integer. */
175 int
176 nd_cpack_int16(netdissect_options *ndo, struct cpack_state *cs, int16_t *u)
177 {
178 	const uint8_t *next;
179 
180 	if ((next = nd_cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
181 		return -1;
182 
183 	*u = GET_LE_S_2(next);
184 
185 	/* Move pointer past the int16_t. */
186 	cs->c_next = next + sizeof(*u);
187 	return 0;
188 }
189 
190 /* Unpack an 8-bit unsigned integer. */
191 int
192 nd_cpack_uint8(netdissect_options *ndo, struct cpack_state *cs, uint8_t *u)
193 {
194 	/* No space left? */
195 	if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
196 		return -1;
197 
198 	*u = GET_U_1(cs->c_next);
199 
200 	/* Move pointer past the uint8_t. */
201 	cs->c_next++;
202 	return 0;
203 }
204 
205 /* Unpack an 8-bit signed integer. */
206 int
207 nd_cpack_int8(netdissect_options *ndo, struct cpack_state *cs, int8_t *u)
208 {
209 	/* No space left? */
210 	if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
211 		return -1;
212 
213 	*u = GET_S_1(cs->c_next);
214 
215 	/* Move pointer past the int8_t. */
216 	cs->c_next++;
217 	return 0;
218 }
219