source: rtems-libbsd/freebsd/contrib/wpa/src/utils/wpabuf.c @ 8f2267b

55-freebsd-126-freebsd-12
Last change on this file since 8f2267b was 9c9d11b, checked in by Sichen Zhao <1473996754@…>, on 08/01/17 at 12:43:41

Import wpa from FreeBSD

  • Property mode set to 100644
File size: 6.9 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Dynamic data buffer
5 * Copyright (c) 2007-2012, Jouni Malinen <j@w1.fi>
6 *
7 * This software may be distributed under the terms of the BSD license.
8 * See README for more details.
9 */
10
11#include "includes.h"
12
13#include "common.h"
14#include "trace.h"
15#include "wpabuf.h"
16
17#ifdef WPA_TRACE
18#define WPABUF_MAGIC 0x51a974e3
19
20struct wpabuf_trace {
21        unsigned int magic;
22} __attribute__((aligned(8)));
23
24static struct wpabuf_trace * wpabuf_get_trace(const struct wpabuf *buf)
25{
26        return (struct wpabuf_trace *)
27                ((const u8 *) buf - sizeof(struct wpabuf_trace));
28}
29#endif /* WPA_TRACE */
30
31
32static void wpabuf_overflow(const struct wpabuf *buf, size_t len)
33{
34#ifdef WPA_TRACE
35        struct wpabuf_trace *trace = wpabuf_get_trace(buf);
36        if (trace->magic != WPABUF_MAGIC) {
37                wpa_printf(MSG_ERROR, "wpabuf: invalid magic %x",
38                           trace->magic);
39        }
40#endif /* WPA_TRACE */
41        wpa_printf(MSG_ERROR, "wpabuf %p (size=%lu used=%lu) overflow len=%lu",
42                   buf, (unsigned long) buf->size, (unsigned long) buf->used,
43                   (unsigned long) len);
44        wpa_trace_show("wpabuf overflow");
45        abort();
46}
47
48
49int wpabuf_resize(struct wpabuf **_buf, size_t add_len)
50{
51        struct wpabuf *buf = *_buf;
52#ifdef WPA_TRACE
53        struct wpabuf_trace *trace;
54#endif /* WPA_TRACE */
55
56        if (buf == NULL) {
57                *_buf = wpabuf_alloc(add_len);
58                return *_buf == NULL ? -1 : 0;
59        }
60
61#ifdef WPA_TRACE
62        trace = wpabuf_get_trace(buf);
63        if (trace->magic != WPABUF_MAGIC) {
64                wpa_printf(MSG_ERROR, "wpabuf: invalid magic %x",
65                           trace->magic);
66                wpa_trace_show("wpabuf_resize invalid magic");
67                abort();
68        }
69#endif /* WPA_TRACE */
70
71        if (buf->used + add_len > buf->size) {
72                unsigned char *nbuf;
73                if (buf->flags & WPABUF_FLAG_EXT_DATA) {
74                        nbuf = os_realloc(buf->buf, buf->used + add_len);
75                        if (nbuf == NULL)
76                                return -1;
77                        os_memset(nbuf + buf->used, 0, add_len);
78                        buf->buf = nbuf;
79                } else {
80#ifdef WPA_TRACE
81                        nbuf = os_realloc(trace, sizeof(struct wpabuf_trace) +
82                                          sizeof(struct wpabuf) +
83                                          buf->used + add_len);
84                        if (nbuf == NULL)
85                                return -1;
86                        trace = (struct wpabuf_trace *) nbuf;
87                        buf = (struct wpabuf *) (trace + 1);
88                        os_memset(nbuf + sizeof(struct wpabuf_trace) +
89                                  sizeof(struct wpabuf) + buf->used, 0,
90                                  add_len);
91#else /* WPA_TRACE */
92                        nbuf = os_realloc(buf, sizeof(struct wpabuf) +
93                                          buf->used + add_len);
94                        if (nbuf == NULL)
95                                return -1;
96                        buf = (struct wpabuf *) nbuf;
97                        os_memset(nbuf + sizeof(struct wpabuf) + buf->used, 0,
98                                  add_len);
99#endif /* WPA_TRACE */
100                        buf->buf = (u8 *) (buf + 1);
101                        *_buf = buf;
102                }
103                buf->size = buf->used + add_len;
104        }
105
106        return 0;
107}
108
109
110/**
111 * wpabuf_alloc - Allocate a wpabuf of the given size
112 * @len: Length for the allocated buffer
113 * Returns: Buffer to the allocated wpabuf or %NULL on failure
114 */
115struct wpabuf * wpabuf_alloc(size_t len)
116{
117#ifdef WPA_TRACE
118        struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
119                                               sizeof(struct wpabuf) + len);
120        struct wpabuf *buf;
121        if (trace == NULL)
122                return NULL;
123        trace->magic = WPABUF_MAGIC;
124        buf = (struct wpabuf *) (trace + 1);
125#else /* WPA_TRACE */
126        struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf) + len);
127        if (buf == NULL)
128                return NULL;
129#endif /* WPA_TRACE */
130
131        buf->size = len;
132        buf->buf = (u8 *) (buf + 1);
133        return buf;
134}
135
136
137struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
138{
139#ifdef WPA_TRACE
140        struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
141                                               sizeof(struct wpabuf));
142        struct wpabuf *buf;
143        if (trace == NULL)
144                return NULL;
145        trace->magic = WPABUF_MAGIC;
146        buf = (struct wpabuf *) (trace + 1);
147#else /* WPA_TRACE */
148        struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf));
149        if (buf == NULL)
150                return NULL;
151#endif /* WPA_TRACE */
152
153        buf->size = len;
154        buf->used = len;
155        buf->buf = data;
156        buf->flags |= WPABUF_FLAG_EXT_DATA;
157
158        return buf;
159}
160
161
162struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
163{
164        struct wpabuf *buf = wpabuf_alloc(len);
165        if (buf)
166                wpabuf_put_data(buf, data, len);
167        return buf;
168}
169
170
171struct wpabuf * wpabuf_dup(const struct wpabuf *src)
172{
173        struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src));
174        if (buf)
175                wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src));
176        return buf;
177}
178
179
180/**
181 * wpabuf_free - Free a wpabuf
182 * @buf: wpabuf buffer
183 */
184void wpabuf_free(struct wpabuf *buf)
185{
186#ifdef WPA_TRACE
187        struct wpabuf_trace *trace;
188        if (buf == NULL)
189                return;
190        trace = wpabuf_get_trace(buf);
191        if (trace->magic != WPABUF_MAGIC) {
192                wpa_printf(MSG_ERROR, "wpabuf_free: invalid magic %x",
193                           trace->magic);
194                wpa_trace_show("wpabuf_free magic mismatch");
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
210void 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
219void * 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 */
239struct 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 */
279struct 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                os_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
301
302void wpabuf_printf(struct wpabuf *buf, char *fmt, ...)
303{
304        va_list ap;
305        void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
306        int res;
307
308        va_start(ap, fmt);
309        res = vsnprintf(tmp, buf->size - buf->used, fmt, ap);
310        va_end(ap);
311        if (res < 0 || (size_t) res >= buf->size - buf->used)
312                wpabuf_overflow(buf, res);
313        buf->used += res;
314}
Note: See TracBrowser for help on using the repository browser.