• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
36 #include <tcpdump-stdinc.h>
37 
38 #include "cpack.h"
39 #include "extract.h"
40 
41 static u_int8_t *
cpack_next_boundary(u_int8_t * buf,u_int8_t * p,size_t alignment)42 cpack_next_boundary(u_int8_t *buf, u_int8_t *p, size_t alignment)
43 {
44 	size_t misalignment = (size_t)(p - buf) % alignment;
45 
46 	if (misalignment == 0)
47 		return p;
48 
49 	return p + (alignment - misalignment);
50 }
51 
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.
55  */
56 static u_int8_t *
cpack_align_and_reserve(struct cpack_state * cs,size_t wordsize)57 cpack_align_and_reserve(struct cpack_state *cs, size_t wordsize)
58 {
59 	u_int8_t *next;
60 
61 	/* Ensure alignment. */
62 	next = cpack_next_boundary(cs->c_buf, cs->c_next, wordsize);
63 
64 	/* Too little space for wordsize bytes? */
65 	if (next - cs->c_buf + wordsize > cs->c_len)
66 		return NULL;
67 
68 	return next;
69 }
70 
71 int
cpack_init(struct cpack_state * cs,u_int8_t * buf,size_t buflen)72 cpack_init(struct cpack_state *cs, u_int8_t *buf, size_t buflen)
73 {
74 	memset(cs, 0, sizeof(*cs));
75 
76 	cs->c_buf = buf;
77 	cs->c_len = buflen;
78 	cs->c_next = cs->c_buf;
79 
80 	return 0;
81 }
82 
83 /* Unpack a 64-bit unsigned integer. */
84 int
cpack_uint64(struct cpack_state * cs,u_int64_t * u)85 cpack_uint64(struct cpack_state *cs, u_int64_t *u)
86 {
87 	u_int8_t *next;
88 
89 	if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
90 		return -1;
91 
92 	*u = EXTRACT_LE_64BITS(next);
93 
94 	/* Move pointer past the u_int64_t. */
95 	cs->c_next = next + sizeof(*u);
96 	return 0;
97 }
98 
99 /* Unpack a 32-bit unsigned integer. */
100 int
cpack_uint32(struct cpack_state * cs,u_int32_t * u)101 cpack_uint32(struct cpack_state *cs, u_int32_t *u)
102 {
103 	u_int8_t *next;
104 
105 	if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
106 		return -1;
107 
108 	*u = EXTRACT_LE_32BITS(next);
109 
110 	/* Move pointer past the u_int32_t. */
111 	cs->c_next = next + sizeof(*u);
112 	return 0;
113 }
114 
115 /* Unpack a 16-bit unsigned integer. */
116 int
cpack_uint16(struct cpack_state * cs,u_int16_t * u)117 cpack_uint16(struct cpack_state *cs, u_int16_t *u)
118 {
119 	u_int8_t *next;
120 
121 	if ((next = cpack_align_and_reserve(cs, sizeof(*u))) == NULL)
122 		return -1;
123 
124 	*u = EXTRACT_LE_16BITS(next);
125 
126 	/* Move pointer past the u_int16_t. */
127 	cs->c_next = next + sizeof(*u);
128 	return 0;
129 }
130 
131 /* Unpack an 8-bit unsigned integer. */
132 int
cpack_uint8(struct cpack_state * cs,u_int8_t * u)133 cpack_uint8(struct cpack_state *cs, u_int8_t *u)
134 {
135 	/* No space left? */
136 	if ((size_t)(cs->c_next - cs->c_buf) >= cs->c_len)
137 		return -1;
138 
139 	*u = *cs->c_next;
140 
141 	/* Move pointer past the u_int8_t. */
142 	cs->c_next++;
143 	return 0;
144 }
145