1 /* $NetBSD: cpack.c,v 1.2 2007/07/24 11:53:37 drochner Exp $ */
4 * Copyright (c) 2003, 2004 David Young. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID
19 * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 #include <tcpdump-stdinc.h>
41 cpack_next_boundary(u_int8_t
*buf
, u_int8_t
*p
, size_t alignment
)
43 size_t misalignment
= (size_t)(p
- buf
) % alignment
;
45 if (misalignment
== 0)
48 return p
+ (alignment
- misalignment
);
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.
56 cpack_align_and_reserve(struct cpack_state
*cs
, size_t wordsize
)
60 /* Ensure alignment. */
61 next
= cpack_next_boundary(cs
->c_buf
, cs
->c_next
, wordsize
);
63 /* Too little space for wordsize bytes? */
64 if (next
- cs
->c_buf
+ wordsize
> cs
->c_len
)
71 cpack_init(struct cpack_state
*cs
, u_int8_t
*buf
, size_t buflen
)
73 memset(cs
, 0, sizeof(*cs
));
77 cs
->c_next
= cs
->c_buf
;
82 /* Unpack a 64-bit unsigned integer. */
84 cpack_uint64(struct cpack_state
*cs
, u_int64_t
*u
)
88 if ((next
= cpack_align_and_reserve(cs
, sizeof(*u
))) == NULL
)
91 *u
= EXTRACT_LE_64BITS(next
);
93 /* Move pointer past the u_int64_t. */
94 cs
->c_next
= next
+ sizeof(*u
);
98 /* Unpack a 32-bit unsigned integer. */
100 cpack_uint32(struct cpack_state
*cs
, u_int32_t
*u
)
104 if ((next
= cpack_align_and_reserve(cs
, sizeof(*u
))) == NULL
)
107 *u
= EXTRACT_LE_32BITS(next
);
109 /* Move pointer past the u_int32_t. */
110 cs
->c_next
= next
+ sizeof(*u
);
114 /* Unpack a 16-bit unsigned integer. */
116 cpack_uint16(struct cpack_state
*cs
, u_int16_t
*u
)
120 if ((next
= cpack_align_and_reserve(cs
, sizeof(*u
))) == NULL
)
123 *u
= EXTRACT_LE_16BITS(next
);
125 /* Move pointer past the u_int16_t. */
126 cs
->c_next
= next
+ sizeof(*u
);
130 /* Unpack an 8-bit unsigned integer. */
132 cpack_uint8(struct cpack_state
*cs
, u_int8_t
*u
)
135 if ((size_t)(cs
->c_next
- cs
->c_buf
) >= cs
->c_len
)
140 /* Move pointer past the u_int8_t. */