1 /*
2  * Dynamic data buffer
3  * Copyright (c) 2007-2012, 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 #ifndef WPABUF_H
16 #define WPABUF_H
17 
18 /* wpabuf::buf is a pointer to external data */
19 #define WPABUF_FLAG_EXT_DATA BIT(0)
20 
21 /*
22  * Internal data structure for wpabuf. Please do not touch this directly from
23  * elsewhere. This is only defined in header file to allow inline functions
24  * from this file to access data.
25  */
26 struct wpabuf {
27 	size_t size; /* total size of the allocated buffer */
28 	size_t used; /* length of data in the buffer */
29 	u8 *buf; /* pointer to the head of the buffer */
30 	unsigned int flags;
31 	/* optionally followed by the allocated buffer */
32 };
33 
34 
35 int wpabuf_resize(struct wpabuf **buf, size_t add_len);
36 struct wpabuf * wpabuf_alloc(size_t len);
37 struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len);
38 struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len);
39 struct wpabuf * wpabuf_dup(const struct wpabuf *src);
40 void wpabuf_free(struct wpabuf *buf);
41 void wpabuf_clear_free(struct wpabuf *buf);
42 void * wpabuf_put(struct wpabuf *buf, size_t len);
43 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b);
44 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len);
45 void wpabuf_printf(struct wpabuf *buf, const char *fmt, ...) PRINTF_FORMAT(2, 3);
46 
47 
48 /**
49  * wpabuf_size - Get the currently allocated size of a wpabuf buffer
50  * @buf: wpabuf buffer
51  * Returns: Currently allocated size of the buffer
52  */
wpabuf_size(const struct wpabuf * buf)53 static inline size_t wpabuf_size(const struct wpabuf *buf)
54 {
55 	return buf->size;
56 }
57 
58 /**
59  * wpabuf_len - Get the current length of a wpabuf buffer data
60  * @buf: wpabuf buffer
61  * Returns: Currently used length of the buffer
62  */
wpabuf_len(const struct wpabuf * buf)63 static inline size_t wpabuf_len(const struct wpabuf *buf)
64 {
65 	return buf->used;
66 }
67 
68 /**
69  * wpabuf_tailroom - Get size of available tail room in the end of the buffer
70  * @buf: wpabuf buffer
71  * Returns: Tail room (in bytes) of available space in the end of the buffer
72  */
wpabuf_tailroom(const struct wpabuf * buf)73 static inline size_t wpabuf_tailroom(const struct wpabuf *buf)
74 {
75 	return buf->size - buf->used;
76 }
77 
78 /**
79  * wpabuf_head - Get pointer to the head of the buffer data
80  * @buf: wpabuf buffer
81  * Returns: Pointer to the head of the buffer data
82  */
wpabuf_head(const struct wpabuf * buf)83 static inline const void * wpabuf_head(const struct wpabuf *buf)
84 {
85 	return buf->buf;
86 }
87 
wpabuf_head_u8(const struct wpabuf * buf)88 static inline const u8 * wpabuf_head_u8(const struct wpabuf *buf)
89 {
90 	return wpabuf_head(buf);
91 }
92 
93 /**
94  * wpabuf_mhead - Get modifiable pointer to the head of the buffer data
95  * @buf: wpabuf buffer
96  * Returns: Pointer to the head of the buffer data
97  */
wpabuf_mhead(struct wpabuf * buf)98 static inline void * wpabuf_mhead(struct wpabuf *buf)
99 {
100 	return buf->buf;
101 }
102 
wpabuf_mhead_u8(struct wpabuf * buf)103 static inline u8 * wpabuf_mhead_u8(struct wpabuf *buf)
104 {
105 	return wpabuf_mhead(buf);
106 }
107 
wpabuf_put_u8(struct wpabuf * buf,u8 data)108 static inline void wpabuf_put_u8(struct wpabuf *buf, u8 data)
109 {
110 	u8 *pos = wpabuf_put(buf, 1);
111 	*pos = data;
112 }
113 
wpabuf_put_le16(struct wpabuf * buf,u16 data)114 static inline void wpabuf_put_le16(struct wpabuf *buf, u16 data)
115 {
116 	u8 *pos = wpabuf_put(buf, 2);
117 	WPA_PUT_LE16(pos, data);
118 }
119 
wpabuf_put_le32(struct wpabuf * buf,u32 data)120 static inline void wpabuf_put_le32(struct wpabuf *buf, u32 data)
121 {
122 	u8 *pos = wpabuf_put(buf, 4);
123 	WPA_PUT_LE32(pos, data);
124 }
125 
wpabuf_put_be16(struct wpabuf * buf,u16 data)126 static inline void wpabuf_put_be16(struct wpabuf *buf, u16 data)
127 {
128 	u8 *pos = wpabuf_put(buf, 2);
129 	WPA_PUT_BE16(pos, data);
130 }
131 
wpabuf_put_be24(struct wpabuf * buf,u32 data)132 static inline void wpabuf_put_be24(struct wpabuf *buf, u32 data)
133 {
134 	u8 *pos = wpabuf_put(buf, 3);
135 	WPA_PUT_BE24(pos, data);
136 }
137 
wpabuf_put_be32(struct wpabuf * buf,u32 data)138 static inline void wpabuf_put_be32(struct wpabuf *buf, u32 data)
139 {
140 	u8 *pos = wpabuf_put(buf, 4);
141 	WPA_PUT_BE32(pos, data);
142 }
143 
wpabuf_put_data(struct wpabuf * buf,const void * data,size_t len)144 static inline void wpabuf_put_data(struct wpabuf *buf, const void *data,
145 				   size_t len)
146 {
147 	if (data)
148 		os_memcpy(wpabuf_put(buf, len), data, len);
149 }
150 
wpabuf_put_buf(struct wpabuf * dst,const struct wpabuf * src)151 static inline void wpabuf_put_buf(struct wpabuf *dst,
152 				  const struct wpabuf *src)
153 {
154 	wpabuf_put_data(dst, wpabuf_head(src), wpabuf_len(src));
155 }
156 
wpabuf_set(struct wpabuf * buf,const void * data,size_t len)157 static inline void wpabuf_set(struct wpabuf *buf, const void *data, size_t len)
158 {
159 	buf->buf = (u8 *) data;
160 	buf->flags = WPABUF_FLAG_EXT_DATA;
161 	buf->size = buf->used = len;
162 }
163 
wpabuf_put_str(struct wpabuf * dst,const char * str)164 static inline void wpabuf_put_str(struct wpabuf *dst, const char *str)
165 {
166 	wpabuf_put_data(dst, str, os_strlen(str));
167 }
168 
169 #endif /* WPABUF_H */
170