16d49e1aeSJan Lentfer /*
26d49e1aeSJan Lentfer  * Dynamic data buffer
33ff40c12SJohn Marino  * Copyright (c) 2007-2012, Jouni Malinen <j@w1.fi>
46d49e1aeSJan Lentfer  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
76d49e1aeSJan Lentfer  */
86d49e1aeSJan Lentfer 
96d49e1aeSJan Lentfer #ifndef WPABUF_H
106d49e1aeSJan Lentfer #define WPABUF_H
116d49e1aeSJan Lentfer 
123ff40c12SJohn Marino /* wpabuf::buf is a pointer to external data */
133ff40c12SJohn Marino #define WPABUF_FLAG_EXT_DATA BIT(0)
143ff40c12SJohn Marino 
156d49e1aeSJan Lentfer /*
166d49e1aeSJan Lentfer  * Internal data structure for wpabuf. Please do not touch this directly from
176d49e1aeSJan Lentfer  * elsewhere. This is only defined in header file to allow inline functions
186d49e1aeSJan Lentfer  * from this file to access data.
196d49e1aeSJan Lentfer  */
206d49e1aeSJan Lentfer struct wpabuf {
216d49e1aeSJan Lentfer 	size_t size; /* total size of the allocated buffer */
226d49e1aeSJan Lentfer 	size_t used; /* length of data in the buffer */
233ff40c12SJohn Marino 	u8 *buf; /* pointer to the head of the buffer */
243ff40c12SJohn Marino 	unsigned int flags;
256d49e1aeSJan Lentfer 	/* optionally followed by the allocated buffer */
266d49e1aeSJan Lentfer };
276d49e1aeSJan Lentfer 
286d49e1aeSJan Lentfer 
296d49e1aeSJan Lentfer int wpabuf_resize(struct wpabuf **buf, size_t add_len);
306d49e1aeSJan Lentfer struct wpabuf * wpabuf_alloc(size_t len);
316d49e1aeSJan Lentfer struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len);
326d49e1aeSJan Lentfer struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len);
336d49e1aeSJan Lentfer struct wpabuf * wpabuf_dup(const struct wpabuf *src);
346d49e1aeSJan Lentfer void wpabuf_free(struct wpabuf *buf);
35*a1157835SDaniel Fojt void wpabuf_clear_free(struct wpabuf *buf);
366d49e1aeSJan Lentfer void * wpabuf_put(struct wpabuf *buf, size_t len);
376d49e1aeSJan Lentfer struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b);
386d49e1aeSJan Lentfer struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len);
396d49e1aeSJan Lentfer void wpabuf_printf(struct wpabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3);
40*a1157835SDaniel Fojt struct wpabuf * wpabuf_parse_bin(const char *buf);
416d49e1aeSJan Lentfer 
426d49e1aeSJan Lentfer 
436d49e1aeSJan Lentfer /**
446d49e1aeSJan Lentfer  * wpabuf_size - Get the currently allocated size of a wpabuf buffer
456d49e1aeSJan Lentfer  * @buf: wpabuf buffer
466d49e1aeSJan Lentfer  * Returns: Currently allocated size of the buffer
476d49e1aeSJan Lentfer  */
wpabuf_size(const struct wpabuf * buf)486d49e1aeSJan Lentfer static inline size_t wpabuf_size(const struct wpabuf *buf)
496d49e1aeSJan Lentfer {
506d49e1aeSJan Lentfer 	return buf->size;
516d49e1aeSJan Lentfer }
526d49e1aeSJan Lentfer 
536d49e1aeSJan Lentfer /**
546d49e1aeSJan Lentfer  * wpabuf_len - Get the current length of a wpabuf buffer data
556d49e1aeSJan Lentfer  * @buf: wpabuf buffer
566d49e1aeSJan Lentfer  * Returns: Currently used length of the buffer
576d49e1aeSJan Lentfer  */
wpabuf_len(const struct wpabuf * buf)586d49e1aeSJan Lentfer static inline size_t wpabuf_len(const struct wpabuf *buf)
596d49e1aeSJan Lentfer {
606d49e1aeSJan Lentfer 	return buf->used;
616d49e1aeSJan Lentfer }
626d49e1aeSJan Lentfer 
636d49e1aeSJan Lentfer /**
646d49e1aeSJan Lentfer  * wpabuf_tailroom - Get size of available tail room in the end of the buffer
656d49e1aeSJan Lentfer  * @buf: wpabuf buffer
666d49e1aeSJan Lentfer  * Returns: Tail room (in bytes) of available space in the end of the buffer
676d49e1aeSJan Lentfer  */
wpabuf_tailroom(const struct wpabuf * buf)686d49e1aeSJan Lentfer static inline size_t wpabuf_tailroom(const struct wpabuf *buf)
696d49e1aeSJan Lentfer {
706d49e1aeSJan Lentfer 	return buf->size - buf->used;
716d49e1aeSJan Lentfer }
726d49e1aeSJan Lentfer 
736d49e1aeSJan Lentfer /**
746d49e1aeSJan Lentfer  * wpabuf_head - Get pointer to the head of the buffer data
756d49e1aeSJan Lentfer  * @buf: wpabuf buffer
766d49e1aeSJan Lentfer  * Returns: Pointer to the head of the buffer data
776d49e1aeSJan Lentfer  */
wpabuf_head(const struct wpabuf * buf)786d49e1aeSJan Lentfer static inline const void * wpabuf_head(const struct wpabuf *buf)
796d49e1aeSJan Lentfer {
803ff40c12SJohn Marino 	return buf->buf;
816d49e1aeSJan Lentfer }
826d49e1aeSJan Lentfer 
wpabuf_head_u8(const struct wpabuf * buf)836d49e1aeSJan Lentfer static inline const u8 * wpabuf_head_u8(const struct wpabuf *buf)
846d49e1aeSJan Lentfer {
85*a1157835SDaniel Fojt 	return (const u8 *) wpabuf_head(buf);
866d49e1aeSJan Lentfer }
876d49e1aeSJan Lentfer 
886d49e1aeSJan Lentfer /**
896d49e1aeSJan Lentfer  * wpabuf_mhead - Get modifiable pointer to the head of the buffer data
906d49e1aeSJan Lentfer  * @buf: wpabuf buffer
916d49e1aeSJan Lentfer  * Returns: Pointer to the head of the buffer data
926d49e1aeSJan Lentfer  */
wpabuf_mhead(struct wpabuf * buf)936d49e1aeSJan Lentfer static inline void * wpabuf_mhead(struct wpabuf *buf)
946d49e1aeSJan Lentfer {
953ff40c12SJohn Marino 	return buf->buf;
966d49e1aeSJan Lentfer }
976d49e1aeSJan Lentfer 
wpabuf_mhead_u8(struct wpabuf * buf)986d49e1aeSJan Lentfer static inline u8 * wpabuf_mhead_u8(struct wpabuf *buf)
996d49e1aeSJan Lentfer {
100*a1157835SDaniel Fojt 	return (u8 *) wpabuf_mhead(buf);
1016d49e1aeSJan Lentfer }
1026d49e1aeSJan Lentfer 
wpabuf_put_u8(struct wpabuf * buf,u8 data)1036d49e1aeSJan Lentfer static inline void wpabuf_put_u8(struct wpabuf *buf, u8 data)
1046d49e1aeSJan Lentfer {
105*a1157835SDaniel Fojt 	u8 *pos = (u8 *) wpabuf_put(buf, 1);
1066d49e1aeSJan Lentfer 	*pos = data;
1076d49e1aeSJan Lentfer }
1086d49e1aeSJan Lentfer 
wpabuf_put_le16(struct wpabuf * buf,u16 data)1093ff40c12SJohn Marino static inline void wpabuf_put_le16(struct wpabuf *buf, u16 data)
1103ff40c12SJohn Marino {
111*a1157835SDaniel Fojt 	u8 *pos = (u8 *) wpabuf_put(buf, 2);
1123ff40c12SJohn Marino 	WPA_PUT_LE16(pos, data);
1133ff40c12SJohn Marino }
1143ff40c12SJohn Marino 
wpabuf_put_le32(struct wpabuf * buf,u32 data)1153ff40c12SJohn Marino static inline void wpabuf_put_le32(struct wpabuf *buf, u32 data)
1163ff40c12SJohn Marino {
117*a1157835SDaniel Fojt 	u8 *pos = (u8 *) wpabuf_put(buf, 4);
1183ff40c12SJohn Marino 	WPA_PUT_LE32(pos, data);
1193ff40c12SJohn Marino }
1203ff40c12SJohn Marino 
wpabuf_put_be16(struct wpabuf * buf,u16 data)1216d49e1aeSJan Lentfer static inline void wpabuf_put_be16(struct wpabuf *buf, u16 data)
1226d49e1aeSJan Lentfer {
123*a1157835SDaniel Fojt 	u8 *pos = (u8 *) wpabuf_put(buf, 2);
1246d49e1aeSJan Lentfer 	WPA_PUT_BE16(pos, data);
1256d49e1aeSJan Lentfer }
1266d49e1aeSJan Lentfer 
wpabuf_put_be24(struct wpabuf * buf,u32 data)1276d49e1aeSJan Lentfer static inline void wpabuf_put_be24(struct wpabuf *buf, u32 data)
1286d49e1aeSJan Lentfer {
129*a1157835SDaniel Fojt 	u8 *pos = (u8 *) wpabuf_put(buf, 3);
1306d49e1aeSJan Lentfer 	WPA_PUT_BE24(pos, data);
1316d49e1aeSJan Lentfer }
1326d49e1aeSJan Lentfer 
wpabuf_put_be32(struct wpabuf * buf,u32 data)1336d49e1aeSJan Lentfer static inline void wpabuf_put_be32(struct wpabuf *buf, u32 data)
1346d49e1aeSJan Lentfer {
135*a1157835SDaniel Fojt 	u8 *pos = (u8 *) wpabuf_put(buf, 4);
1366d49e1aeSJan Lentfer 	WPA_PUT_BE32(pos, data);
1376d49e1aeSJan Lentfer }
1386d49e1aeSJan Lentfer 
wpabuf_put_data(struct wpabuf * buf,const void * data,size_t len)1396d49e1aeSJan Lentfer static inline void wpabuf_put_data(struct wpabuf *buf, const void *data,
1406d49e1aeSJan Lentfer 				   size_t len)
1416d49e1aeSJan Lentfer {
1426d49e1aeSJan Lentfer 	if (data)
1436d49e1aeSJan Lentfer 		os_memcpy(wpabuf_put(buf, len), data, len);
1446d49e1aeSJan Lentfer }
1456d49e1aeSJan Lentfer 
wpabuf_put_buf(struct wpabuf * dst,const struct wpabuf * src)1466d49e1aeSJan Lentfer static inline void wpabuf_put_buf(struct wpabuf *dst,
1476d49e1aeSJan Lentfer 				  const struct wpabuf *src)
1486d49e1aeSJan Lentfer {
1496d49e1aeSJan Lentfer 	wpabuf_put_data(dst, wpabuf_head(src), wpabuf_len(src));
1506d49e1aeSJan Lentfer }
1516d49e1aeSJan Lentfer 
wpabuf_set(struct wpabuf * buf,const void * data,size_t len)1526d49e1aeSJan Lentfer static inline void wpabuf_set(struct wpabuf *buf, const void *data, size_t len)
1536d49e1aeSJan Lentfer {
1543ff40c12SJohn Marino 	buf->buf = (u8 *) data;
1553ff40c12SJohn Marino 	buf->flags = WPABUF_FLAG_EXT_DATA;
1566d49e1aeSJan Lentfer 	buf->size = buf->used = len;
1576d49e1aeSJan Lentfer }
1586d49e1aeSJan Lentfer 
wpabuf_put_str(struct wpabuf * dst,const char * str)1596d49e1aeSJan Lentfer static inline void wpabuf_put_str(struct wpabuf *dst, const char *str)
1606d49e1aeSJan Lentfer {
1616d49e1aeSJan Lentfer 	wpabuf_put_data(dst, str, os_strlen(str));
1626d49e1aeSJan Lentfer }
1636d49e1aeSJan Lentfer 
1646d49e1aeSJan Lentfer #endif /* WPABUF_H */
165