1 /*
2  * Dynamic data buffer
3  * Copyright (c) 2007-2009, 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 "includes.h"
16 
17 #include "common.h"
18 #include "wpabuf.h"
19 
20 static void wpabuf_overflow(const struct wpabuf *buf, size_t len)
21 {
22 	wpa_printf(MSG_ERROR, "wpabuf %p (size=%lu used=%lu) overflow len=%lu",
23 		   buf, (unsigned long) buf->size, (unsigned long) buf->used,
24 		   (unsigned long) len);
25 	abort();
26 }
27 
28 
29 int wpabuf_resize(struct wpabuf **_buf, size_t add_len)
30 {
31 	struct wpabuf *buf = *_buf;
32 	if (buf == NULL) {
33 		*_buf = wpabuf_alloc(add_len);
34 		return *_buf == NULL ? -1 : 0;
35 	}
36 	if (buf->used + add_len > buf->size) {
37 		unsigned char *nbuf;
38 		if (buf->ext_data) {
39 			nbuf = os_realloc(buf->ext_data, buf->used + add_len);
40 			if (nbuf == NULL)
41 				return -1;
42 			os_memset(nbuf + buf->used, 0, add_len);
43 			buf->ext_data = nbuf;
44 		} else {
45 			nbuf = os_realloc(buf, sizeof(struct wpabuf) +
46 					  buf->used + add_len);
47 			if (nbuf == NULL)
48 				return -1;
49 			buf = (struct wpabuf *) nbuf;
50 			os_memset(nbuf + sizeof(struct wpabuf) + buf->used, 0,
51 				  add_len);
52 			*_buf = buf;
53 		}
54 		buf->size = buf->used + add_len;
55 	}
56 
57 	return 0;
58 }
59 
60 
61 /**
62  * wpabuf_alloc - Allocate a wpabuf of the given size
63  * @len: Length for the allocated buffer
64  * Returns: Buffer to the allocated wpabuf or %NULL on failure
65  */
66 struct wpabuf * wpabuf_alloc(size_t len)
67 {
68 	struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf) + len);
69 	if (buf == NULL)
70 		return NULL;
71 	buf->size = len;
72 	return buf;
73 }
74 
75 
76 struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
77 {
78 	struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf));
79 	if (buf == NULL)
80 		return NULL;
81 
82 	buf->size = len;
83 	buf->used = len;
84 	buf->ext_data = data;
85 
86 	return buf;
87 }
88 
89 
90 struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
91 {
92 	struct wpabuf *buf = wpabuf_alloc(len);
93 	if (buf)
94 		wpabuf_put_data(buf, data, len);
95 	return buf;
96 }
97 
98 
99 struct wpabuf * wpabuf_dup(const struct wpabuf *src)
100 {
101 	struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src));
102 	if (buf)
103 		wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src));
104 	return buf;
105 }
106 
107 
108 /**
109  * wpabuf_free - Free a wpabuf
110  * @buf: wpabuf buffer
111  */
112 void wpabuf_free(struct wpabuf *buf)
113 {
114 	if (buf == NULL)
115 		return;
116 	os_free(buf->ext_data);
117 	os_free(buf);
118 }
119 
120 
121 void * wpabuf_put(struct wpabuf *buf, size_t len)
122 {
123 	void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
124 	buf->used += len;
125 	if (buf->used > buf->size) {
126 		wpabuf_overflow(buf, len);
127 	}
128 	return tmp;
129 }
130 
131 
132 /**
133  * wpabuf_concat - Concatenate two buffers into a newly allocated one
134  * @a: First buffer
135  * @b: Second buffer
136  * Returns: wpabuf with concatenated a + b data or %NULL on failure
137  *
138  * Both buffers a and b will be freed regardless of the return value. Input
139  * buffers can be %NULL which is interpreted as an empty buffer.
140  */
141 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
142 {
143 	struct wpabuf *n = NULL;
144 	size_t len = 0;
145 
146 	if (b == NULL)
147 		return a;
148 
149 	if (a)
150 		len += wpabuf_len(a);
151 	if (b)
152 		len += wpabuf_len(b);
153 
154 	n = wpabuf_alloc(len);
155 	if (n) {
156 		if (a)
157 			wpabuf_put_buf(n, a);
158 		if (b)
159 			wpabuf_put_buf(n, b);
160 	}
161 
162 	wpabuf_free(a);
163 	wpabuf_free(b);
164 
165 	return n;
166 }
167 
168 
169 /**
170  * wpabuf_zeropad - Pad buffer with 0x00 octets (prefix) to specified length
171  * @buf: Buffer to be padded
172  * @len: Length for the padded buffer
173  * Returns: wpabuf padded to len octets or %NULL on failure
174  *
175  * If buf is longer than len octets or of same size, it will be returned as-is.
176  * Otherwise a new buffer is allocated and prefixed with 0x00 octets followed
177  * by the source data. The source buffer will be freed on error, i.e., caller
178  * will only be responsible on freeing the returned buffer. If buf is %NULL,
179  * %NULL will be returned.
180  */
181 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
182 {
183 	struct wpabuf *ret;
184 	size_t blen;
185 
186 	if (buf == NULL)
187 		return NULL;
188 
189 	blen = wpabuf_len(buf);
190 	if (blen >= len)
191 		return buf;
192 
193 	ret = wpabuf_alloc(len);
194 	if (ret) {
195 		os_memset(wpabuf_put(ret, len - blen), 0, len - blen);
196 		wpabuf_put_buf(ret, buf);
197 	}
198 	wpabuf_free(buf);
199 
200 	return ret;
201 }
202 
203 
204 void wpabuf_printf(struct wpabuf *buf, char *fmt, ...)
205 {
206 	va_list ap;
207 	void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
208 	int res;
209 
210 	va_start(ap, fmt);
211 	res = vsnprintf(tmp, buf->size - buf->used, fmt, ap);
212 	va_end(ap);
213 	if (res < 0 || (size_t) res >= buf->size - buf->used)
214 		wpabuf_overflow(buf, res);
215 	buf->used += res;
216 }
217