1189251Ssam/*
2189251Ssam * Dynamic data buffer
3252726Srpaulo * Copyright (c) 2007-2012, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#ifndef WPABUF_H
10189251Ssam#define WPABUF_H
11189251Ssam
12252726Srpaulo/* wpabuf::buf is a pointer to external data */
13252726Srpaulo#define WPABUF_FLAG_EXT_DATA BIT(0)
14252726Srpaulo
15189251Ssam/*
16189251Ssam * Internal data structure for wpabuf. Please do not touch this directly from
17189251Ssam * elsewhere. This is only defined in header file to allow inline functions
18189251Ssam * from this file to access data.
19189251Ssam */
20189251Ssamstruct wpabuf {
21189251Ssam	size_t size; /* total size of the allocated buffer */
22189251Ssam	size_t used; /* length of data in the buffer */
23252726Srpaulo	u8 *buf; /* pointer to the head of the buffer */
24252726Srpaulo	unsigned int flags;
25189251Ssam	/* optionally followed by the allocated buffer */
26189251Ssam};
27189251Ssam
28189251Ssam
29189251Ssamint wpabuf_resize(struct wpabuf **buf, size_t add_len);
30189251Ssamstruct wpabuf * wpabuf_alloc(size_t len);
31189251Ssamstruct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len);
32189251Ssamstruct wpabuf * wpabuf_alloc_copy(const void *data, size_t len);
33189251Ssamstruct wpabuf * wpabuf_dup(const struct wpabuf *src);
34189251Ssamvoid wpabuf_free(struct wpabuf *buf);
35189251Ssamvoid * wpabuf_put(struct wpabuf *buf, size_t len);
36189251Ssamstruct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b);
37189251Ssamstruct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len);
38189251Ssamvoid wpabuf_printf(struct wpabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3);
39189251Ssam
40189251Ssam
41189251Ssam/**
42189251Ssam * wpabuf_size - Get the currently allocated size of a wpabuf buffer
43189251Ssam * @buf: wpabuf buffer
44189251Ssam * Returns: Currently allocated size of the buffer
45189251Ssam */
46189251Ssamstatic inline size_t wpabuf_size(const struct wpabuf *buf)
47189251Ssam{
48189251Ssam	return buf->size;
49189251Ssam}
50189251Ssam
51189251Ssam/**
52189251Ssam * wpabuf_len - Get the current length of a wpabuf buffer data
53189251Ssam * @buf: wpabuf buffer
54189251Ssam * Returns: Currently used length of the buffer
55189251Ssam */
56189251Ssamstatic inline size_t wpabuf_len(const struct wpabuf *buf)
57189251Ssam{
58189251Ssam	return buf->used;
59189251Ssam}
60189251Ssam
61189251Ssam/**
62189251Ssam * wpabuf_tailroom - Get size of available tail room in the end of the buffer
63189251Ssam * @buf: wpabuf buffer
64189251Ssam * Returns: Tail room (in bytes) of available space in the end of the buffer
65189251Ssam */
66189251Ssamstatic inline size_t wpabuf_tailroom(const struct wpabuf *buf)
67189251Ssam{
68189251Ssam	return buf->size - buf->used;
69189251Ssam}
70189251Ssam
71189251Ssam/**
72189251Ssam * wpabuf_head - Get pointer to the head of the buffer data
73189251Ssam * @buf: wpabuf buffer
74189251Ssam * Returns: Pointer to the head of the buffer data
75189251Ssam */
76189251Ssamstatic inline const void * wpabuf_head(const struct wpabuf *buf)
77189251Ssam{
78252726Srpaulo	return buf->buf;
79189251Ssam}
80189251Ssam
81189251Ssamstatic inline const u8 * wpabuf_head_u8(const struct wpabuf *buf)
82189251Ssam{
83189251Ssam	return wpabuf_head(buf);
84189251Ssam}
85189251Ssam
86189251Ssam/**
87189251Ssam * wpabuf_mhead - Get modifiable pointer to the head of the buffer data
88189251Ssam * @buf: wpabuf buffer
89189251Ssam * Returns: Pointer to the head of the buffer data
90189251Ssam */
91189251Ssamstatic inline void * wpabuf_mhead(struct wpabuf *buf)
92189251Ssam{
93252726Srpaulo	return buf->buf;
94189251Ssam}
95189251Ssam
96189251Ssamstatic inline u8 * wpabuf_mhead_u8(struct wpabuf *buf)
97189251Ssam{
98189251Ssam	return wpabuf_mhead(buf);
99189251Ssam}
100189251Ssam
101189251Ssamstatic inline void wpabuf_put_u8(struct wpabuf *buf, u8 data)
102189251Ssam{
103189251Ssam	u8 *pos = wpabuf_put(buf, 1);
104189251Ssam	*pos = data;
105189251Ssam}
106189251Ssam
107214734Srpaulostatic inline void wpabuf_put_le16(struct wpabuf *buf, u16 data)
108214734Srpaulo{
109214734Srpaulo	u8 *pos = wpabuf_put(buf, 2);
110214734Srpaulo	WPA_PUT_LE16(pos, data);
111214734Srpaulo}
112214734Srpaulo
113252726Srpaulostatic inline void wpabuf_put_le32(struct wpabuf *buf, u32 data)
114252726Srpaulo{
115252726Srpaulo	u8 *pos = wpabuf_put(buf, 4);
116252726Srpaulo	WPA_PUT_LE32(pos, data);
117252726Srpaulo}
118252726Srpaulo
119189251Ssamstatic inline void wpabuf_put_be16(struct wpabuf *buf, u16 data)
120189251Ssam{
121189251Ssam	u8 *pos = wpabuf_put(buf, 2);
122189251Ssam	WPA_PUT_BE16(pos, data);
123189251Ssam}
124189251Ssam
125189251Ssamstatic inline void wpabuf_put_be24(struct wpabuf *buf, u32 data)
126189251Ssam{
127189251Ssam	u8 *pos = wpabuf_put(buf, 3);
128189251Ssam	WPA_PUT_BE24(pos, data);
129189251Ssam}
130189251Ssam
131189251Ssamstatic inline void wpabuf_put_be32(struct wpabuf *buf, u32 data)
132189251Ssam{
133189251Ssam	u8 *pos = wpabuf_put(buf, 4);
134189251Ssam	WPA_PUT_BE32(pos, data);
135189251Ssam}
136189251Ssam
137189251Ssamstatic inline void wpabuf_put_data(struct wpabuf *buf, const void *data,
138189251Ssam				   size_t len)
139189251Ssam{
140189251Ssam	if (data)
141189251Ssam		os_memcpy(wpabuf_put(buf, len), data, len);
142189251Ssam}
143189251Ssam
144189251Ssamstatic inline void wpabuf_put_buf(struct wpabuf *dst,
145189251Ssam				  const struct wpabuf *src)
146189251Ssam{
147189251Ssam	wpabuf_put_data(dst, wpabuf_head(src), wpabuf_len(src));
148189251Ssam}
149189251Ssam
150189251Ssamstatic inline void wpabuf_set(struct wpabuf *buf, const void *data, size_t len)
151189251Ssam{
152252726Srpaulo	buf->buf = (u8 *) data;
153252726Srpaulo	buf->flags = WPABUF_FLAG_EXT_DATA;
154189251Ssam	buf->size = buf->used = len;
155189251Ssam}
156189251Ssam
157189251Ssamstatic inline void wpabuf_put_str(struct wpabuf *dst, const char *str)
158189251Ssam{
159189251Ssam	wpabuf_put_data(dst, str, os_strlen(str));
160189251Ssam}
161189251Ssam
162189251Ssam#endif /* WPABUF_H */
163