1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
21
22
23
35
36
37
50
51
52
53
54
55
56
57
58
59
63
64
65
72
73
74
75
82
83
84
85
86
87
88
89
90
91
92
93
94
101
102
103
104
105
106
107
108
109
110
111
112
117
118
119
120
127
128
131
132
133
134
135
136
137
138
139
140
141
148
149
152
153
154
155
156
157
158
159
160
161
162
163
170
171
172
179
180
181
185
186
187
188
189
190
191
192
197
198
199
200
201
206
207
208
209
210
217
218
219
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* ... */
#include "utils/includes.h"
#include "utils/common.h"
#include "utils/wpabuf.h"
#include "stdio.h"
#include "stdarg.h"5 includes
#ifdef WPA_TRACE
#define WPABUF_MAGIC 0x51a974e3
struct wpabuf_trace {
unsigned int magic;
}{...};
static struct wpabuf_trace * wpabuf_get_trace(const struct wpabuf *buf)
{
return (struct wpabuf_trace *)
((const u8 *) buf - sizeof(struct wpabuf_trace));
}{...}
/* ... */#endif
static void wpabuf_overflow(const struct wpabuf *buf, size_t len)
{
#ifdef WPA_TRACE
struct wpabuf_trace *trace = wpabuf_get_trace(buf);
if (trace->magic != WPABUF_MAGIC) {
wpa_printf( MSG_ERROR, "wpabuf: invalid magic %x",
trace->magic);
}{...}
#endif/* ... */
wpa_printf( MSG_ERROR, "wpabuf %p (size=%lu used=%lu) overflow len=%lu",
buf, (unsigned long) buf->size, (unsigned long) buf->used,
(unsigned long) len);
}{ ... }
int wpabuf_resize(struct wpabuf **_buf, size_t add_len)
{
struct wpabuf *buf = *_buf;
#ifdef WPA_TRACE
struct wpabuf_trace *trace;
#endif
if (buf == NULL) {
*_buf = wpabuf_alloc(add_len);
return *_buf == NULL ? -1 : 0;
}{...}
#ifdef WPA_TRACE
trace = wpabuf_get_trace(buf);
if (trace->magic != WPABUF_MAGIC) {
wpa_printf( MSG_ERROR, "wpabuf: invalid magic %x",
trace->magic);
abort();
}{...}
#endif/* ... */
if (buf->used + add_len > buf->size) {
unsigned char *nbuf;
if (buf->flags & WPABUF_FLAG_EXT_DATA) {
nbuf = os_realloc(buf->buf, buf->used + add_len);
if (nbuf == NULL)
return -1;
memset(nbuf + buf->used, 0, add_len);
buf->buf = nbuf;
}{...} else {
#ifdef WPA_TRACE
nbuf = os_realloc(trace, sizeof(struct wpabuf_trace) +
sizeof(struct wpabuf) +
buf->used + add_len);
if (nbuf == NULL)
return -1;
trace = (struct wpabuf_trace *) nbuf;
buf = (struct wpabuf *) (trace + 1);
memset(nbuf + sizeof(struct wpabuf_trace) +
sizeof(struct wpabuf) + buf->used, 0,
add_len);/* ... */
#else
nbuf = (unsigned char*)os_realloc(buf, sizeof(struct wpabuf) +
buf->used + add_len);
if (nbuf == NULL)
return -1;
buf = (struct wpabuf *) nbuf;
memset(nbuf + sizeof(struct wpabuf) + buf->used, 0,
add_len);/* ... */
#endif
buf->buf = (u8 *) (buf + 1);
*_buf = buf;
}{...}
buf->size = buf->used + add_len;
}{...}
return 0;
}{ ... }
/* ... */
struct wpabuf * wpabuf_alloc(size_t len)
{
#ifdef WPA_TRACE
struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
sizeof(struct wpabuf) + len);
struct wpabuf *buf;
if (trace == NULL)
return NULL;
trace->magic = WPABUF_MAGIC;
buf = (struct wpabuf *) (trace + 1);/* ... */
#else
struct wpabuf *buf = (struct wpabuf *)os_zalloc(sizeof(struct wpabuf) + len);
if (buf == NULL)
return NULL;/* ... */
#endif
buf->size = len;
buf->buf = (u8 *) (buf + 1);
return buf;
}{ ... }
struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
{
#ifdef WPA_TRACE
struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
sizeof(struct wpabuf));
struct wpabuf *buf;
if (trace == NULL)
return NULL;
trace->magic = WPABUF_MAGIC;
buf = (struct wpabuf *) (trace + 1);/* ... */
#else
struct wpabuf *buf = (struct wpabuf *)os_zalloc(sizeof(struct wpabuf));
if (buf == NULL)
return NULL;/* ... */
#endif
buf->size = len;
buf->used = len;
buf->buf = data;
buf->flags |= WPABUF_FLAG_EXT_DATA;
return buf;
}{ ... }
struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
{
struct wpabuf *buf = wpabuf_alloc(len);
if (buf)
wpabuf_put_data(buf, data, len);
return buf;
}{ ... }
struct wpabuf * wpabuf_dup(const struct wpabuf *src)
{
struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src));
if (buf)
wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src));
return buf;
}{ ... }
/* ... */
void wpabuf_free(struct wpabuf *buf)
{
#ifdef WPA_TRACE
struct wpabuf_trace *trace;
if (buf == NULL)
return;
trace = wpabuf_get_trace(buf);
if (trace->magic != WPABUF_MAGIC) {
wpa_printf( MSG_ERROR, "wpabuf_free: invalid magic %x",
trace->magic);
abort();
}{...}
if (buf->flags & WPABUF_FLAG_EXT_DATA)
os_free(buf->buf);
os_free(trace);/* ... */
#else
if (buf == NULL)
return;
if (buf->flags & WPABUF_FLAG_EXT_DATA)
os_free(buf->buf);
os_free(buf);/* ... */
#endif
}{ ... }
void wpabuf_clear_free(struct wpabuf *buf)
{
if (buf) {
os_memset(wpabuf_mhead(buf), 0, wpabuf_len(buf));
wpabuf_free(buf);
}{...}
}{ ... }
void * wpabuf_put(struct wpabuf *buf, size_t len)
{
void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
buf->used += len;
if (buf->used > buf->size) {
wpabuf_overflow(buf, len);
}{...}
return tmp;
}{ ... }
/* ... */
struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
{
struct wpabuf *n = NULL;
size_t len = 0;
if (b == NULL)
return a;
if (a)
len += wpabuf_len(a);
if (b)
len += wpabuf_len(b);
n = wpabuf_alloc(len);
if (n) {
if (a)
wpabuf_put_buf(n, a);
if (b)
wpabuf_put_buf(n, b);
}{...}
wpabuf_free(a);
wpabuf_free(b);
return n;
}{ ... }
/* ... */
struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
{
struct wpabuf *ret;
size_t blen;
if (buf == NULL)
return NULL;
blen = wpabuf_len(buf);
if (blen >= len)
return buf;
ret = wpabuf_alloc(len);
if (ret) {
memset(wpabuf_put(ret, len - blen), 0, len - blen);
wpabuf_put_buf(ret, buf);
}{...}
wpabuf_free(buf);
return ret;
}{ ... }
void wpabuf_printf(struct wpabuf *buf, const char *fmt, ...)
{
va_list ap;
void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
int res;
va_start(ap, fmt);
res = vsnprintf(tmp, buf->size - buf->used, fmt, ap);
va_end(ap);
if (res < 0 || (size_t) res >= buf->size - buf->used)
wpabuf_overflow(buf, res);
buf->used += res;
}{ ... }