1 /*
2  * Dynamic data buffer
3  * Copyright (c) 2007-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include "utils/includes.h"
16 
17 #include "utils/common.h"
18 #include "utils/wpabuf.h"
19 #include "stdio.h"
20 #include "stdarg.h"
21 
22 #ifdef WPA_TRACE
23 #define WPABUF_MAGIC 0x51a974e3
24 
25 struct wpabuf_trace {
26 	unsigned int magic;
27 };
28 
wpabuf_get_trace(const struct wpabuf * buf)29 static struct wpabuf_trace * wpabuf_get_trace(const struct wpabuf *buf)
30 {
31 	return (struct wpabuf_trace *)
32 		((const u8 *) buf - sizeof(struct wpabuf_trace));
33 }
34 #endif /* WPA_TRACE */
35 
36 
wpabuf_overflow(const struct wpabuf * buf,size_t len)37 static void wpabuf_overflow(const struct wpabuf *buf, size_t len)
38 {
39 #ifdef WPA_TRACE
40 	struct wpabuf_trace *trace = wpabuf_get_trace(buf);
41 	if (trace->magic != WPABUF_MAGIC) {
42 		wpa_printf( MSG_ERROR,  "wpabuf: invalid magic %x",
43 			   trace->magic);
44 	}
45 #endif /* WPA_TRACE */
46 	wpa_printf( MSG_ERROR, "wpabuf %p (size=%lu used=%lu) overflow len=%lu",
47 		   buf, (unsigned long) buf->size, (unsigned long) buf->used,
48 		   (unsigned long) len);
49 }
50 
51 
wpabuf_resize(struct wpabuf ** _buf,size_t add_len)52 int wpabuf_resize(struct wpabuf **_buf, size_t add_len)
53 {
54 	struct wpabuf *buf = *_buf;
55 #ifdef WPA_TRACE
56 	struct wpabuf_trace *trace;
57 #endif /* WPA_TRACE */
58 
59 	if (buf == NULL) {
60 		*_buf = wpabuf_alloc(add_len);
61 		return *_buf == NULL ? -1 : 0;
62 	}
63 
64 #ifdef WPA_TRACE
65 	trace = wpabuf_get_trace(buf);
66 	if (trace->magic != WPABUF_MAGIC) {
67 		wpa_printf( MSG_ERROR,  "wpabuf: invalid magic %x",
68 			   trace->magic);
69 		abort();
70 	}
71 #endif /* WPA_TRACE */
72 
73 	if (buf->used + add_len > buf->size) {
74 		unsigned char *nbuf;
75 		if (buf->flags & WPABUF_FLAG_EXT_DATA) {
76 			nbuf = os_realloc(buf->buf, buf->used + add_len);
77 			if (nbuf == NULL)
78 				return -1;
79 			memset(nbuf + buf->used, 0, add_len);
80 			buf->buf = nbuf;
81 		} else {
82 #ifdef WPA_TRACE
83 			nbuf = os_realloc(trace, sizeof(struct wpabuf_trace) +
84 					  sizeof(struct wpabuf) +
85 					  buf->used + add_len);
86 			if (nbuf == NULL)
87 				return -1;
88 			trace = (struct wpabuf_trace *) nbuf;
89 			buf = (struct wpabuf *) (trace + 1);
90 			memset(nbuf + sizeof(struct wpabuf_trace) +
91 				  sizeof(struct wpabuf) + buf->used, 0,
92 				  add_len);
93 #else /* WPA_TRACE */
94 			nbuf = (unsigned char*)os_realloc(buf, sizeof(struct wpabuf) +
95 					  buf->used + add_len);
96 			if (nbuf == NULL)
97 				return -1;
98 			buf = (struct wpabuf *) nbuf;
99 			memset(nbuf + sizeof(struct wpabuf) + buf->used, 0,
100 				  add_len);
101 #endif /* WPA_TRACE */
102 			buf->buf = (u8 *) (buf + 1);
103 			*_buf = buf;
104 		}
105 		buf->size = buf->used + add_len;
106 	}
107 
108 	return 0;
109 }
110 
111 
112 /**
113  * wpabuf_alloc - Allocate a wpabuf of the given size
114  * @len: Length for the allocated buffer
115  * Returns: Buffer to the allocated wpabuf or %NULL on failure
116  */
wpabuf_alloc(size_t len)117 struct wpabuf * wpabuf_alloc(size_t len)
118 {
119 #ifdef WPA_TRACE
120 	struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
121 					       sizeof(struct wpabuf) + len);
122 	struct wpabuf *buf;
123 	if (trace == NULL)
124 		return NULL;
125 	trace->magic = WPABUF_MAGIC;
126 	buf = (struct wpabuf *) (trace + 1);
127 #else /* WPA_TRACE */
128 	struct wpabuf *buf = (struct wpabuf *)os_zalloc(sizeof(struct wpabuf) + len);
129 	if (buf == NULL)
130 		return NULL;
131 #endif /* WPA_TRACE */
132 
133 	buf->size = len;
134 	buf->buf = (u8 *) (buf + 1);
135 	return buf;
136 }
137 
wpabuf_alloc_ext_data(u8 * data,size_t len)138 struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
139 {
140 #ifdef WPA_TRACE
141 	struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
142 					       sizeof(struct wpabuf));
143 	struct wpabuf *buf;
144 	if (trace == NULL)
145 		return NULL;
146 	trace->magic = WPABUF_MAGIC;
147 	buf = (struct wpabuf *) (trace + 1);
148 #else /* WPA_TRACE */
149 	struct wpabuf *buf = (struct wpabuf *)os_zalloc(sizeof(struct wpabuf));
150 	if (buf == NULL)
151 		return NULL;
152 #endif /* WPA_TRACE */
153 
154 	buf->size = len;
155 	buf->used = len;
156 	buf->buf = data;
157 	buf->flags |= WPABUF_FLAG_EXT_DATA;
158 
159 	return buf;
160 }
161 
162 
wpabuf_alloc_copy(const void * data,size_t len)163 struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
164 {
165 	struct wpabuf *buf = wpabuf_alloc(len);
166 	if (buf)
167 		wpabuf_put_data(buf, data, len);
168 	return buf;
169 }
170 
171 
wpabuf_dup(const struct wpabuf * src)172 struct wpabuf * wpabuf_dup(const struct wpabuf *src)
173 {
174 	struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src));
175 	if (buf)
176 		wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src));
177 	return buf;
178 }
179 
180 
181 /**
182  * wpabuf_free - Free a wpabuf
183  * @buf: wpabuf buffer
184  */
wpabuf_free(struct wpabuf * buf)185 void wpabuf_free(struct wpabuf *buf)
186 {
187 #ifdef WPA_TRACE
188 	struct wpabuf_trace *trace;
189 	if (buf == NULL)
190 		return;
191 	trace = wpabuf_get_trace(buf);
192 	if (trace->magic != WPABUF_MAGIC) {
193 		wpa_printf( MSG_ERROR,  "wpabuf_free: invalid magic %x",
194 			   trace->magic);
195 		abort();
196 	}
197 	if (buf->flags & WPABUF_FLAG_EXT_DATA)
198 		os_free(buf->buf);
199 	os_free(trace);
200 #else /* WPA_TRACE */
201 	if (buf == NULL)
202 		return;
203 	if (buf->flags & WPABUF_FLAG_EXT_DATA)
204 		os_free(buf->buf);
205 	os_free(buf);
206 #endif /* WPA_TRACE */
207 }
208 
209 
wpabuf_clear_free(struct wpabuf * buf)210 void wpabuf_clear_free(struct wpabuf *buf)
211 {
212 	if (buf) {
213 		os_memset(wpabuf_mhead(buf), 0, wpabuf_len(buf));
214 		wpabuf_free(buf);
215 	}
216 }
217 
218 
wpabuf_put(struct wpabuf * buf,size_t len)219 void * wpabuf_put(struct wpabuf *buf, size_t len)
220 {
221 	void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
222 	buf->used += len;
223 	if (buf->used > buf->size) {
224 		wpabuf_overflow(buf, len);
225 	}
226 	return tmp;
227 }
228 
229 
230 /**
231  * wpabuf_concat - Concatenate two buffers into a newly allocated one
232  * @a: First buffer
233  * @b: Second buffer
234  * Returns: wpabuf with concatenated a + b data or %NULL on failure
235  *
236  * Both buffers a and b will be freed regardless of the return value. Input
237  * buffers can be %NULL which is interpreted as an empty buffer.
238  */
wpabuf_concat(struct wpabuf * a,struct wpabuf * b)239 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
240 {
241 	struct wpabuf *n = NULL;
242 	size_t len = 0;
243 
244 	if (b == NULL)
245 		return a;
246 
247 	if (a)
248 		len += wpabuf_len(a);
249 	if (b)
250 		len += wpabuf_len(b);
251 
252 	n = wpabuf_alloc(len);
253 	if (n) {
254 		if (a)
255 			wpabuf_put_buf(n, a);
256 		if (b)
257 			wpabuf_put_buf(n, b);
258 	}
259 
260 	wpabuf_free(a);
261 	wpabuf_free(b);
262 
263 	return n;
264 }
265 
266 
267 /**
268  * wpabuf_zeropad - Pad buffer with 0x00 octets (prefix) to specified length
269  * @buf: Buffer to be padded
270  * @len: Length for the padded buffer
271  * Returns: wpabuf padded to len octets or %NULL on failure
272  *
273  * If buf is longer than len octets or of same size, it will be returned as-is.
274  * Otherwise a new buffer is allocated and prefixed with 0x00 octets followed
275  * by the source data. The source buffer will be freed on error, i.e., caller
276  * will only be responsible on freeing the returned buffer. If buf is %NULL,
277  * %NULL will be returned.
278  */
wpabuf_zeropad(struct wpabuf * buf,size_t len)279 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
280 {
281 	struct wpabuf *ret;
282 	size_t blen;
283 
284 	if (buf == NULL)
285 		return NULL;
286 
287 	blen = wpabuf_len(buf);
288 	if (blen >= len)
289 		return buf;
290 
291 	ret = wpabuf_alloc(len);
292 	if (ret) {
293 		memset(wpabuf_put(ret, len - blen), 0, len - blen);
294 		wpabuf_put_buf(ret, buf);
295 	}
296 	wpabuf_free(buf);
297 
298 	return ret;
299 }
300 
wpabuf_printf(struct wpabuf * buf,const char * fmt,...)301 void wpabuf_printf(struct wpabuf *buf, const char *fmt, ...)
302 {
303 	va_list ap;
304 	void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
305 	int res;
306 
307 	va_start(ap, fmt);
308         res = vsnprintf(tmp, buf->size - buf->used, fmt, ap);
309 	va_end(ap);
310 	if (res < 0 || (size_t) res >= buf->size - buf->used)
311 		wpabuf_overflow(buf, res);
312 	buf->used += res;
313 }
314