]> The Tcpdump Group git mirrors - tcpdump/blob - cpack.c
Add "cpack", a library for extracting 1-, 2-, 4-, and 8-octet words
[tcpdump] / cpack.c
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 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <tcpdump-stdinc.h>
36
37 #include "cpack.h"
38 #include "extract.h"
39
40 static u_int8_t *
41 cpack_next_boundary(u_int8_t *buf, u_int8_t *p, size_t alignment)
42 {
43 size_t misalignment = (size_t)(p - buf) % alignment;
44
45 if (misalignment == 0)
46 return p;
47
48 return p + (alignment - misalignment);
49 }
50
51 /* Advance to the next wordsize boundary. Return NULL if fewer than
52 * wordsize bytes remain in the buffer after the boundary. Otherwise,
53 * return a pointer to the boundary.
54 */
55 static u_int8_t *
56 cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
57 {
58 u_int8_t *next;
59
60 /* Ensure alignment. */
61 next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
62
63 /* Too little space for wordsize bytes? */
64 if (next - cs->c_buf + wordsize > cs->c_len)
65 return NULL;
66
67 return next;
68 }
69
70 int
71 cpack_init(struct cpack_state *cs, u_int8_t *buf, size_t buflen)
72 {
73 memset(cs, 0, sizeof(*cs));
74
75 cs->c_buf = buf;
76 cs->c_len = buflen;
77 cs->c_next = cs->c_buf;
78
79 return 0;
80 }
81
82 /* Unpack a 64-bit unsigned integer. */
83 int
84 cpack_uint64(struct cpack_state *cs, u_int64_t *u)
85 {
86 u_int8_t *next;
87
88 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
89 return -1;
90
91 *u = EXTRACT_LE_64BITS(next);
92
93 /* Move pointer past the u_int64_t. */
94 cs->c_next = next + sizeof(*u);
95 return 0;
96 }
97
98 /* Unpack a 32-bit unsigned integer. */
99 int
100 cpack_uint32(struct cpack_state *cs, u_int32_t *u)
101 {
102 u_int8_t *next;
103
104 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
105 return -1;
106
107 *u = EXTRACT_LE_32BITS(next);
108
109 /* Move pointer past the u_int32_t. */
110 cs->c_next = next + sizeof(*u);
111 return 0;
112 }
113
114 /* Unpack a 16-bit unsigned integer. */
115 int
116 cpack_uint16(struct cpack_state *cs, u_int16_t *u)
117 {
118 u_int8_t *next;
119
120 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
121 return -1;
122
123 *u = EXTRACT_LE_16BITS(next);
124
125 /* Move pointer past the u_int16_t. */
126 cs->c_next = next + sizeof(*u);
127 return 0;
128 }
129
130 /* Unpack an 8-bit unsigned integer. */
131 int
132 cpack_uint8(struct cpack_state *cs, u_int8_t *u)
133 {
134 /* No space left? */
135 if (cs->c_next - cs->c_buf >= cs->c_len)
136 return -1;
137
138 *u = *cs->c_next;
139
140 /* Move pointer past the u_int8_t. */
141 cs->c_next++;
142 return 0;
143 }