blob: 16bfd151698e8c195193a1880944f74c6e3f538f [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*-
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
Elliott Hughes892a68b2015-10-19 14:43:53 -070030#define NETDISSECT_REWORKED
The Android Open Source Project2949f582009-03-03 19:30:46 -080031#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <stdlib.h>
36#include <string.h>
37#include <tcpdump-stdinc.h>
38
39#include "cpack.h"
40#include "extract.h"
41
Elliott Hughes892a68b2015-10-19 14:43:53 -070042uint8_t *
43cpack_next_boundary(uint8_t *buf, uint8_t *p, size_t alignment)
The Android Open Source Project2949f582009-03-03 19:30:46 -080044{
45 size_t misalignment = (size_t)(p - buf) % alignment;
46
47 if (misalignment == 0)
48 return p;
49
50 return p + (alignment - misalignment);
51}
52
53/* Advance to the next wordsize boundary. Return NULL if fewer than
54 * wordsize bytes remain in the buffer after the boundary. Otherwise,
55 * return a pointer to the boundary.
56 */
Elliott Hughes892a68b2015-10-19 14:43:53 -070057uint8_t *
The Android Open Source Project2949f582009-03-03 19:30:46 -080058cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
59{
Elliott Hughes892a68b2015-10-19 14:43:53 -070060 uint8_t *next;
The Android Open Source Project2949f582009-03-03 19:30:46 -080061
62 /* Ensure alignment. */
63 next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
64
65 /* Too little space for wordsize bytes? */
66 if (next - cs->c_buf + wordsize > cs->c_len)
67 return NULL;
68
69 return next;
70}
71
JP Abgrall53f17a92014-02-12 14:02:41 -080072/* Advance by N bytes without returning them. */
73int
74cpack_advance(struct cpack_state *cs, const size_t toskip)
75{
76 /* No space left? */
77 if (cs->c_next - cs->c_buf + toskip > cs->c_len)
78 return -1;
79 cs->c_next += toskip;
80 return 0;
81}
82
The Android Open Source Project2949f582009-03-03 19:30:46 -080083int
Elliott Hughes892a68b2015-10-19 14:43:53 -070084cpack_init(struct cpack_state *cs, uint8_t *buf, size_t buflen)
The Android Open Source Project2949f582009-03-03 19:30:46 -080085{
86 memset(cs, 0, sizeof(*cs));
87
88 cs->c_buf = buf;
89 cs->c_len = buflen;
90 cs->c_next = cs->c_buf;
91
92 return 0;
93}
94
95/* Unpack a 64-bit unsigned integer. */
96int
Elliott Hughes892a68b2015-10-19 14:43:53 -070097cpack_uint64(struct cpack_state *cs, uint64_t *u)
The Android Open Source Project2949f582009-03-03 19:30:46 -080098{
Elliott Hughes892a68b2015-10-19 14:43:53 -070099 uint8_t *next;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800100
101 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
102 return -1;
103
104 *u = EXTRACT_LE_64BITS(next);
105
Elliott Hughes892a68b2015-10-19 14:43:53 -0700106 /* Move pointer past the uint64_t. */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800107 cs->c_next = next + sizeof(*u);
108 return 0;
109}
110
111/* Unpack a 32-bit unsigned integer. */
112int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700113cpack_uint32(struct cpack_state *cs, uint32_t *u)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800114{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700115 uint8_t *next;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800116
117 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
118 return -1;
119
120 *u = EXTRACT_LE_32BITS(next);
121
Elliott Hughes892a68b2015-10-19 14:43:53 -0700122 /* Move pointer past the uint32_t. */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800123 cs->c_next = next + sizeof(*u);
124 return 0;
125}
126
127/* Unpack a 16-bit unsigned integer. */
128int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700129cpack_uint16(struct cpack_state *cs, uint16_t *u)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800130{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700131 uint8_t *next;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800132
133 if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
134 return -1;
135
136 *u = EXTRACT_LE_16BITS(next);
137
Elliott Hughes892a68b2015-10-19 14:43:53 -0700138 /* Move pointer past the uint16_t. */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800139 cs->c_next = next + sizeof(*u);
140 return 0;
141}
142
143/* Unpack an 8-bit unsigned integer. */
144int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700145cpack_uint8(struct cpack_state *cs, uint8_t *u)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800146{
147 /* No space left? */
148 if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
149 return -1;
150
151 *u = *cs->c_next;
152
Elliott Hughes892a68b2015-10-19 14:43:53 -0700153 /* Move pointer past the uint8_t. */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800154 cs->c_next++;
155 return 0;
156}