3 * Copyright (c) 2007-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
16 #define WPABUF_MAGIC 0x51a974e3
22 static struct wpabuf_trace * wpabuf_get_trace(const struct wpabuf *buf)
24 return (struct wpabuf_trace *)
25 ((const u8 *) buf - sizeof(struct wpabuf_trace));
27 #endif /* WPA_TRACE */
30 static void wpabuf_overflow(const struct wpabuf *buf, size_t len)
33 struct wpabuf_trace *trace = wpabuf_get_trace(buf);
34 if (trace->magic != WPABUF_MAGIC) {
35 wpa_printf(MSG_ERROR, "wpabuf: invalid magic %x",
38 #endif /* WPA_TRACE */
39 wpa_printf(MSG_ERROR, "wpabuf %p (size=%lu used=%lu) overflow len=%lu",
40 buf, (unsigned long) buf->size, (unsigned long) buf->used,
42 wpa_trace_show("wpabuf overflow");
47 int wpabuf_resize(struct wpabuf **_buf, size_t add_len)
49 struct wpabuf *buf = *_buf;
51 struct wpabuf_trace *trace;
52 #endif /* WPA_TRACE */
55 *_buf = wpabuf_alloc(add_len);
56 return *_buf == NULL ? -1 : 0;
60 trace = wpabuf_get_trace(buf);
61 if (trace->magic != WPABUF_MAGIC) {
62 wpa_printf(MSG_ERROR, "wpabuf: invalid magic %x",
64 wpa_trace_show("wpabuf_resize invalid magic");
67 #endif /* WPA_TRACE */
69 if (buf->used + add_len > buf->size) {
71 if (buf->flags & WPABUF_FLAG_EXT_DATA) {
72 nbuf = os_realloc(buf->buf, buf->used + add_len);
75 os_memset(nbuf + buf->used, 0, add_len);
79 nbuf = os_realloc(trace, sizeof(struct wpabuf_trace) +
80 sizeof(struct wpabuf) +
84 trace = (struct wpabuf_trace *) nbuf;
85 buf = (struct wpabuf *) (trace + 1);
86 os_memset(nbuf + sizeof(struct wpabuf_trace) +
87 sizeof(struct wpabuf) + buf->used, 0,
90 nbuf = os_realloc(buf, sizeof(struct wpabuf) +
94 buf = (struct wpabuf *) nbuf;
95 os_memset(nbuf + sizeof(struct wpabuf) + buf->used, 0,
97 #endif /* WPA_TRACE */
98 buf->buf = (u8 *) (buf + 1);
101 buf->size = buf->used + add_len;
109 * wpabuf_alloc - Allocate a wpabuf of the given size
110 * @len: Length for the allocated buffer
111 * Returns: Buffer to the allocated wpabuf or %NULL on failure
113 struct wpabuf * wpabuf_alloc(size_t len)
116 struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
117 sizeof(struct wpabuf) + len);
121 trace->magic = WPABUF_MAGIC;
122 buf = (struct wpabuf *) (trace + 1);
123 #else /* WPA_TRACE */
124 struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf) + len);
127 #endif /* WPA_TRACE */
130 buf->buf = (u8 *) (buf + 1);
135 struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
138 struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
139 sizeof(struct wpabuf));
143 trace->magic = WPABUF_MAGIC;
144 buf = (struct wpabuf *) (trace + 1);
145 #else /* WPA_TRACE */
146 struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf));
149 #endif /* WPA_TRACE */
154 buf->flags |= WPABUF_FLAG_EXT_DATA;
160 struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
162 struct wpabuf *buf = wpabuf_alloc(len);
164 wpabuf_put_data(buf, data, len);
169 struct wpabuf * wpabuf_dup(const struct wpabuf *src)
171 struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src));
173 wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src));
179 * wpabuf_free - Free a wpabuf
180 * @buf: wpabuf buffer
182 void wpabuf_free(struct wpabuf *buf)
185 struct wpabuf_trace *trace;
188 trace = wpabuf_get_trace(buf);
189 if (trace->magic != WPABUF_MAGIC) {
190 wpa_printf(MSG_ERROR, "wpabuf_free: invalid magic %x",
192 wpa_trace_show("wpabuf_free magic mismatch");
195 if (buf->flags & WPABUF_FLAG_EXT_DATA)
198 #else /* WPA_TRACE */
201 if (buf->flags & WPABUF_FLAG_EXT_DATA)
204 #endif /* WPA_TRACE */
208 void * wpabuf_put(struct wpabuf *buf, size_t len)
210 void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
212 if (buf->used > buf->size) {
213 wpabuf_overflow(buf, len);
220 * wpabuf_concat - Concatenate two buffers into a newly allocated one
223 * Returns: wpabuf with concatenated a + b data or %NULL on failure
225 * Both buffers a and b will be freed regardless of the return value. Input
226 * buffers can be %NULL which is interpreted as an empty buffer.
228 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
230 struct wpabuf *n = NULL;
237 len += wpabuf_len(a);
239 len += wpabuf_len(b);
241 n = wpabuf_alloc(len);
244 wpabuf_put_buf(n, a);
246 wpabuf_put_buf(n, b);
257 * wpabuf_zeropad - Pad buffer with 0x00 octets (prefix) to specified length
258 * @buf: Buffer to be padded
259 * @len: Length for the padded buffer
260 * Returns: wpabuf padded to len octets or %NULL on failure
262 * If buf is longer than len octets or of same size, it will be returned as-is.
263 * Otherwise a new buffer is allocated and prefixed with 0x00 octets followed
264 * by the source data. The source buffer will be freed on error, i.e., caller
265 * will only be responsible on freeing the returned buffer. If buf is %NULL,
266 * %NULL will be returned.
268 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
276 blen = wpabuf_len(buf);
280 ret = wpabuf_alloc(len);
282 os_memset(wpabuf_put(ret, len - blen), 0, len - blen);
283 wpabuf_put_buf(ret, buf);
291 void wpabuf_printf(struct wpabuf *buf, char *fmt, ...)
294 void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
298 res = vsnprintf(tmp, buf->size - buf->used, fmt, ap);
300 if (res < 0 || (size_t) res >= buf->size - buf->used)
301 wpabuf_overflow(buf, res);