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
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.
13 * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
17 * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 #define NETDISSECT_REWORKED
34 #include <tcpdump-stdinc.h>
40 cpack_next_boundary(uint8_t *buf
, uint8_t *p
, size_t alignment
)
42 size_t misalignment
= (size_t)(p
- buf
) % alignment
;
44 if (misalignment
== 0)
47 return p
+ (alignment
- misalignment
);
50 /* Advance to the next wordsize boundary. Return NULL if fewer than
51 * wordsize bytes remain in the buffer after the boundary. Otherwise,
52 * return a pointer to the boundary.
55 cpack_align_and_reserve(struct cpack_state
*cs
, size_t wordsize
)
59 /* Ensure alignment. */
60 next
= cpack_next_boundary(cs
->c_buf
, cs
->c_next
, wordsize
);
62 /* Too little space for wordsize bytes? */
63 if (next
- cs
->c_buf
+ wordsize
> cs
->c_len
)
69 /* Advance by N bytes without returning them. */
71 cpack_advance(struct cpack_state
*cs
, const size_t toskip
)
74 if (cs
->c_next
- cs
->c_buf
+ toskip
> cs
->c_len
)
81 cpack_init(struct cpack_state
*cs
, uint8_t *buf
, size_t buflen
)
83 memset(cs
, 0, sizeof(*cs
));
87 cs
->c_next
= cs
->c_buf
;
92 /* Unpack a 64-bit unsigned integer. */
94 cpack_uint64(struct cpack_state
*cs
, uint64_t *u
)
98 if ((next
= cpack_align_and_reserve(cs
, sizeof(*u
))) == NULL
)
101 *u
= EXTRACT_LE_64BITS(next
);
103 /* Move pointer past the uint64_t. */
104 cs
->c_next
= next
+ sizeof(*u
);
108 /* Unpack a 32-bit unsigned integer. */
110 cpack_uint32(struct cpack_state
*cs
, uint32_t *u
)
114 if ((next
= cpack_align_and_reserve(cs
, sizeof(*u
))) == NULL
)
117 *u
= EXTRACT_LE_32BITS(next
);
119 /* Move pointer past the uint32_t. */
120 cs
->c_next
= next
+ sizeof(*u
);
124 /* Unpack a 16-bit unsigned integer. */
126 cpack_uint16(struct cpack_state
*cs
, uint16_t *u
)
130 if ((next
= cpack_align_and_reserve(cs
, sizeof(*u
))) == NULL
)
133 *u
= EXTRACT_LE_16BITS(next
);
135 /* Move pointer past the uint16_t. */
136 cs
->c_next
= next
+ sizeof(*u
);
140 /* Unpack an 8-bit unsigned integer. */
142 cpack_uint8(struct cpack_state
*cs
, uint8_t *u
)
145 if ((size_t)(cs
->c_next
- cs
->c_buf
) >= cs
->c_len
)
150 /* Move pointer past the uint8_t. */