wpa_supplicant / hostapd  2.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
wpabuf.h
Go to the documentation of this file.
1 
5 #ifndef WPABUF_H
6 #define WPABUF_H
7 
8 /* wpabuf::buf is a pointer to external data */
9 #define WPABUF_FLAG_EXT_DATA BIT(0)
10 
11 /*
12  * Internal data structure for wpabuf. Please do not touch this directly from
13  * elsewhere. This is only defined in header file to allow inline functions
14  * from this file to access data.
15  */
16 struct wpabuf {
17  size_t size; /* total size of the allocated buffer */
18  size_t used; /* length of data in the buffer */
19  u8 *buf; /* pointer to the head of the buffer */
20  unsigned int flags;
21  /* optionally followed by the allocated buffer */
22 };
23 
24 
25 int wpabuf_resize(struct wpabuf **buf, size_t add_len);
26 struct wpabuf * wpabuf_alloc(size_t len);
27 struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len);
28 struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len);
29 struct wpabuf * wpabuf_dup(const struct wpabuf *src);
30 void wpabuf_free(struct wpabuf *buf);
31 void wpabuf_clear_free(struct wpabuf *buf);
32 void * wpabuf_put(struct wpabuf *buf, size_t len);
33 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b);
34 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len);
35 void wpabuf_printf(struct wpabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3);
36 
37 
43 static inline size_t wpabuf_size(const struct wpabuf *buf)
44 {
45  return buf->size;
46 }
47 
53 static inline size_t wpabuf_len(const struct wpabuf *buf)
54 {
55  return buf->used;
56 }
57 
63 static inline size_t wpabuf_tailroom(const struct wpabuf *buf)
64 {
65  return buf->size - buf->used;
66 }
67 
73 static inline const void * wpabuf_head(const struct wpabuf *buf)
74 {
75  return buf->buf;
76 }
77 
78 static inline const u8 * wpabuf_head_u8(const struct wpabuf *buf)
79 {
80  return wpabuf_head(buf);
81 }
82 
88 static inline void * wpabuf_mhead(struct wpabuf *buf)
89 {
90  return buf->buf;
91 }
92 
93 static inline u8 * wpabuf_mhead_u8(struct wpabuf *buf)
94 {
95  return wpabuf_mhead(buf);
96 }
97 
98 static inline void wpabuf_put_u8(struct wpabuf *buf, u8 data)
99 {
100  u8 *pos = wpabuf_put(buf, 1);
101  *pos = data;
102 }
103 
104 static inline void wpabuf_put_le16(struct wpabuf *buf, u16 data)
105 {
106  u8 *pos = wpabuf_put(buf, 2);
107  WPA_PUT_LE16(pos, data);
108 }
109 
110 static inline void wpabuf_put_le32(struct wpabuf *buf, u32 data)
111 {
112  u8 *pos = wpabuf_put(buf, 4);
113  WPA_PUT_LE32(pos, data);
114 }
115 
116 static inline void wpabuf_put_be16(struct wpabuf *buf, u16 data)
117 {
118  u8 *pos = wpabuf_put(buf, 2);
119  WPA_PUT_BE16(pos, data);
120 }
121 
122 static inline void wpabuf_put_be24(struct wpabuf *buf, u32 data)
123 {
124  u8 *pos = wpabuf_put(buf, 3);
125  WPA_PUT_BE24(pos, data);
126 }
127 
128 static inline void wpabuf_put_be32(struct wpabuf *buf, u32 data)
129 {
130  u8 *pos = wpabuf_put(buf, 4);
131  WPA_PUT_BE32(pos, data);
132 }
133 
134 static inline void wpabuf_put_data(struct wpabuf *buf, const void *data,
135  size_t len)
136 {
137  if (data)
138  os_memcpy(wpabuf_put(buf, len), data, len);
139 }
140 
141 static inline void wpabuf_put_buf(struct wpabuf *dst,
142  const struct wpabuf *src)
143 {
144  wpabuf_put_data(dst, wpabuf_head(src), wpabuf_len(src));
145 }
146 
147 static inline void wpabuf_set(struct wpabuf *buf, const void *data, size_t len)
148 {
149  buf->buf = (u8 *) data;
150  buf->flags = WPABUF_FLAG_EXT_DATA;
151  buf->size = buf->used = len;
152 }
153 
154 static inline void wpabuf_put_str(struct wpabuf *dst, const char *str)
155 {
156  wpabuf_put_data(dst, str, os_strlen(str));
157 }
158 
159 #endif /* WPABUF_H */
struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
Concatenate two buffers into a newly allocated one.
Definition: wpabuf.c:233
struct wpabuf * wpabuf_alloc(size_t len)
Allocate a wpabuf of the given size.
Definition: wpabuf.c:109
Definition: wpabuf.h:16
void wpabuf_free(struct wpabuf *buf)
Free a wpabuf.
Definition: wpabuf.c:178
struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
Pad buffer with 0x00 octets (prefix) to specified length.
Definition: wpabuf.c:273