3 * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 static void wpabuf_overflow(const struct wpabuf *buf, size_t len)
22 wpa_printf(MSG_ERROR, "wpabuf %p (size=%lu used=%lu) overflow len=%lu",
23 buf, (unsigned long) buf->size, (unsigned long) buf->used,
29 int wpabuf_resize(struct wpabuf **_buf, size_t add_len)
31 struct wpabuf *buf = *_buf;
33 *_buf = wpabuf_alloc(add_len);
34 return *_buf == NULL ? -1 : 0;
36 if (buf->used + add_len > buf->size) {
39 nbuf = os_realloc(buf->ext_data, buf->used + add_len);
42 os_memset(nbuf + buf->used, 0, add_len);
45 nbuf = os_realloc(buf, sizeof(struct wpabuf) +
49 buf = (struct wpabuf *) nbuf;
50 os_memset(nbuf + sizeof(struct wpabuf) + buf->used, 0,
54 buf->size = buf->used + add_len;
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
66 struct wpabuf * wpabuf_alloc(size_t len)
68 struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf) + len);
76 struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
78 struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf));
90 struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
92 struct wpabuf *buf = wpabuf_alloc(len);
94 wpabuf_put_data(buf, data, len);
99 struct wpabuf * wpabuf_dup(const struct wpabuf *src)
101 struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src));
103 wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src));
109 * wpabuf_free - Free a wpabuf
110 * @buf: wpabuf buffer
112 void wpabuf_free(struct wpabuf *buf)
116 os_free(buf->ext_data);
121 void * wpabuf_put(struct wpabuf *buf, size_t len)
123 void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
125 if (buf->used > buf->size) {
126 wpabuf_overflow(buf, len);
133 * wpabuf_concat - Concatenate two buffers into a newly allocated one
136 * Returns: wpabuf with concatenated a + b data or %NULL on failure
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.
141 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
143 struct wpabuf *n = NULL;
150 len += wpabuf_len(a);
152 len += wpabuf_len(b);
154 n = wpabuf_alloc(len);
157 wpabuf_put_buf(n, a);
159 wpabuf_put_buf(n, b);
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
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.
181 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
189 blen = wpabuf_len(buf);
193 ret = wpabuf_alloc(len);
195 os_memset(wpabuf_put(ret, len - blen), 0, len - blen);
196 wpabuf_put_buf(ret, buf);
204 void wpabuf_printf(struct wpabuf *buf, char *fmt, ...)
207 void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
211 res = vsnprintf(tmp, buf->size - buf->used, fmt, ap);
213 if (res < 0 || (size_t) res >= buf->size - buf->used)
214 wpabuf_overflow(buf, res);