Merge commit '37e84ab74e939caf52150fc3352081786ecc0c29' into merges
[unleashed.git] / contrib / tcpdump / cpack.c
blobe37d813d39fb7d0335638bdee826dc80cbfe2166
1 /*-
2 * Copyright (c) 2003, 2004 David Young. All rights reserved.
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.
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.
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
34 #include <stdlib.h>
35 #include <string.h>
36 #include <netdissect-stdinc.h>
38 #include "cpack.h"
39 #include "extract.h"
41 const uint8_t *
42 cpack_next_boundary(const uint8_t *buf, const uint8_t *p, size_t alignment)
44 size_t misalignment = (size_t)(p - buf) % alignment;
46 if (misalignment == 0)
47 return p;
49 return p + (alignment - misalignment);
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.
56 const uint8_t *
57 cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
59 const uint8_t *next;
61 /* Ensure alignment. */
62 next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
64 /* Too little space for wordsize bytes? */
65 if (next - cs->c_buf + wordsize > cs->c_len)
66 return NULL;
68 return next;
71 /* Advance by N bytes without returning them. */
72 int
73 cpack_advance(struct cpack_state *cs, const size_t toskip)
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;
82 int
83 cpack_init(struct cpack_state *cs, const uint8_t *buf, size_t buflen)
85 memset(cs, 0, sizeof(*cs));
87 cs->c_buf = buf;
88 cs->c_len = buflen;
89 cs->c_next = cs->c_buf;
91 return 0;
94 /* Unpack a 64-bit unsigned integer. */
95 int
96 cpack_uint64(struct cpack_state *cs, uint64_t *u)
98 const uint8_t *next;
100 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
101 return -1;
103 *u = EXTRACT_LE_64BITS(next);
105 /* Move pointer past the uint64_t. */
106 cs->c_next = next + sizeof(*u);
107 return 0;
110 /* Unpack a 32-bit unsigned integer. */
112 cpack_uint32(struct cpack_state *cs, uint32_t *u)
114 const uint8_t *next;
116 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
117 return -1;
119 *u = EXTRACT_LE_32BITS(next);
121 /* Move pointer past the uint32_t. */
122 cs->c_next = next + sizeof(*u);
123 return 0;
126 /* Unpack a 16-bit unsigned integer. */
128 cpack_uint16(struct cpack_state *cs, uint16_t *u)
130 const uint8_t *next;
132 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
133 return -1;
135 *u = EXTRACT_LE_16BITS(next);
137 /* Move pointer past the uint16_t. */
138 cs->c_next = next + sizeof(*u);
139 return 0;
142 /* Unpack an 8-bit unsigned integer. */
144 cpack_uint8(struct cpack_state *cs, uint8_t *u)
146 /* No space left? */
147 if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
148 return -1;
150 *u = *cs->c_next;
152 /* Move pointer past the uint8_t. */
153 cs->c_next++;
154 return 0;