1*6d49e1aeSJan Lentfer /* 2*6d49e1aeSJan Lentfer * Dynamic data buffer 3*6d49e1aeSJan Lentfer * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi> 4*6d49e1aeSJan Lentfer * 5*6d49e1aeSJan Lentfer * This program is free software; you can redistribute it and/or modify 6*6d49e1aeSJan Lentfer * it under the terms of the GNU General Public License version 2 as 7*6d49e1aeSJan Lentfer * published by the Free Software Foundation. 8*6d49e1aeSJan Lentfer * 9*6d49e1aeSJan Lentfer * Alternatively, this software may be distributed under the terms of BSD 10*6d49e1aeSJan Lentfer * license. 11*6d49e1aeSJan Lentfer * 12*6d49e1aeSJan Lentfer * See README and COPYING for more details. 13*6d49e1aeSJan Lentfer */ 14*6d49e1aeSJan Lentfer 15*6d49e1aeSJan Lentfer #ifndef WPABUF_H 16*6d49e1aeSJan Lentfer #define WPABUF_H 17*6d49e1aeSJan Lentfer 18*6d49e1aeSJan Lentfer /* 19*6d49e1aeSJan Lentfer * Internal data structure for wpabuf. Please do not touch this directly from 20*6d49e1aeSJan Lentfer * elsewhere. This is only defined in header file to allow inline functions 21*6d49e1aeSJan Lentfer * from this file to access data. 22*6d49e1aeSJan Lentfer */ 23*6d49e1aeSJan Lentfer struct wpabuf { 24*6d49e1aeSJan Lentfer size_t size; /* total size of the allocated buffer */ 25*6d49e1aeSJan Lentfer size_t used; /* length of data in the buffer */ 26*6d49e1aeSJan Lentfer u8 *ext_data; /* pointer to external data; NULL if data follows 27*6d49e1aeSJan Lentfer * struct wpabuf */ 28*6d49e1aeSJan Lentfer /* optionally followed by the allocated buffer */ 29*6d49e1aeSJan Lentfer }; 30*6d49e1aeSJan Lentfer 31*6d49e1aeSJan Lentfer 32*6d49e1aeSJan Lentfer int wpabuf_resize(struct wpabuf **buf, size_t add_len); 33*6d49e1aeSJan Lentfer struct wpabuf * wpabuf_alloc(size_t len); 34*6d49e1aeSJan Lentfer struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len); 35*6d49e1aeSJan Lentfer struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len); 36*6d49e1aeSJan Lentfer struct wpabuf * wpabuf_dup(const struct wpabuf *src); 37*6d49e1aeSJan Lentfer void wpabuf_free(struct wpabuf *buf); 38*6d49e1aeSJan Lentfer void * wpabuf_put(struct wpabuf *buf, size_t len); 39*6d49e1aeSJan Lentfer struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b); 40*6d49e1aeSJan Lentfer struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len); 41*6d49e1aeSJan Lentfer void wpabuf_printf(struct wpabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3); 42*6d49e1aeSJan Lentfer 43*6d49e1aeSJan Lentfer 44*6d49e1aeSJan Lentfer /** 45*6d49e1aeSJan Lentfer * wpabuf_size - Get the currently allocated size of a wpabuf buffer 46*6d49e1aeSJan Lentfer * @buf: wpabuf buffer 47*6d49e1aeSJan Lentfer * Returns: Currently allocated size of the buffer 48*6d49e1aeSJan Lentfer */ 49*6d49e1aeSJan Lentfer static inline size_t wpabuf_size(const struct wpabuf *buf) 50*6d49e1aeSJan Lentfer { 51*6d49e1aeSJan Lentfer return buf->size; 52*6d49e1aeSJan Lentfer } 53*6d49e1aeSJan Lentfer 54*6d49e1aeSJan Lentfer /** 55*6d49e1aeSJan Lentfer * wpabuf_len - Get the current length of a wpabuf buffer data 56*6d49e1aeSJan Lentfer * @buf: wpabuf buffer 57*6d49e1aeSJan Lentfer * Returns: Currently used length of the buffer 58*6d49e1aeSJan Lentfer */ 59*6d49e1aeSJan Lentfer static inline size_t wpabuf_len(const struct wpabuf *buf) 60*6d49e1aeSJan Lentfer { 61*6d49e1aeSJan Lentfer return buf->used; 62*6d49e1aeSJan Lentfer } 63*6d49e1aeSJan Lentfer 64*6d49e1aeSJan Lentfer /** 65*6d49e1aeSJan Lentfer * wpabuf_tailroom - Get size of available tail room in the end of the buffer 66*6d49e1aeSJan Lentfer * @buf: wpabuf buffer 67*6d49e1aeSJan Lentfer * Returns: Tail room (in bytes) of available space in the end of the buffer 68*6d49e1aeSJan Lentfer */ 69*6d49e1aeSJan Lentfer static inline size_t wpabuf_tailroom(const struct wpabuf *buf) 70*6d49e1aeSJan Lentfer { 71*6d49e1aeSJan Lentfer return buf->size - buf->used; 72*6d49e1aeSJan Lentfer } 73*6d49e1aeSJan Lentfer 74*6d49e1aeSJan Lentfer /** 75*6d49e1aeSJan Lentfer * wpabuf_head - Get pointer to the head of the buffer data 76*6d49e1aeSJan Lentfer * @buf: wpabuf buffer 77*6d49e1aeSJan Lentfer * Returns: Pointer to the head of the buffer data 78*6d49e1aeSJan Lentfer */ 79*6d49e1aeSJan Lentfer static inline const void * wpabuf_head(const struct wpabuf *buf) 80*6d49e1aeSJan Lentfer { 81*6d49e1aeSJan Lentfer if (buf->ext_data) 82*6d49e1aeSJan Lentfer return buf->ext_data; 83*6d49e1aeSJan Lentfer return buf + 1; 84*6d49e1aeSJan Lentfer } 85*6d49e1aeSJan Lentfer 86*6d49e1aeSJan Lentfer static inline const u8 * wpabuf_head_u8(const struct wpabuf *buf) 87*6d49e1aeSJan Lentfer { 88*6d49e1aeSJan Lentfer return wpabuf_head(buf); 89*6d49e1aeSJan Lentfer } 90*6d49e1aeSJan Lentfer 91*6d49e1aeSJan Lentfer /** 92*6d49e1aeSJan Lentfer * wpabuf_mhead - Get modifiable pointer to the head of the buffer data 93*6d49e1aeSJan Lentfer * @buf: wpabuf buffer 94*6d49e1aeSJan Lentfer * Returns: Pointer to the head of the buffer data 95*6d49e1aeSJan Lentfer */ 96*6d49e1aeSJan Lentfer static inline void * wpabuf_mhead(struct wpabuf *buf) 97*6d49e1aeSJan Lentfer { 98*6d49e1aeSJan Lentfer if (buf->ext_data) 99*6d49e1aeSJan Lentfer return buf->ext_data; 100*6d49e1aeSJan Lentfer return buf + 1; 101*6d49e1aeSJan Lentfer } 102*6d49e1aeSJan Lentfer 103*6d49e1aeSJan Lentfer static inline u8 * wpabuf_mhead_u8(struct wpabuf *buf) 104*6d49e1aeSJan Lentfer { 105*6d49e1aeSJan Lentfer return wpabuf_mhead(buf); 106*6d49e1aeSJan Lentfer } 107*6d49e1aeSJan Lentfer 108*6d49e1aeSJan Lentfer static inline void wpabuf_put_u8(struct wpabuf *buf, u8 data) 109*6d49e1aeSJan Lentfer { 110*6d49e1aeSJan Lentfer u8 *pos = wpabuf_put(buf, 1); 111*6d49e1aeSJan Lentfer *pos = data; 112*6d49e1aeSJan Lentfer } 113*6d49e1aeSJan Lentfer 114*6d49e1aeSJan Lentfer static inline void wpabuf_put_be16(struct wpabuf *buf, u16 data) 115*6d49e1aeSJan Lentfer { 116*6d49e1aeSJan Lentfer u8 *pos = wpabuf_put(buf, 2); 117*6d49e1aeSJan Lentfer WPA_PUT_BE16(pos, data); 118*6d49e1aeSJan Lentfer } 119*6d49e1aeSJan Lentfer 120*6d49e1aeSJan Lentfer static inline void wpabuf_put_be24(struct wpabuf *buf, u32 data) 121*6d49e1aeSJan Lentfer { 122*6d49e1aeSJan Lentfer u8 *pos = wpabuf_put(buf, 3); 123*6d49e1aeSJan Lentfer WPA_PUT_BE24(pos, data); 124*6d49e1aeSJan Lentfer } 125*6d49e1aeSJan Lentfer 126*6d49e1aeSJan Lentfer static inline void wpabuf_put_be32(struct wpabuf *buf, u32 data) 127*6d49e1aeSJan Lentfer { 128*6d49e1aeSJan Lentfer u8 *pos = wpabuf_put(buf, 4); 129*6d49e1aeSJan Lentfer WPA_PUT_BE32(pos, data); 130*6d49e1aeSJan Lentfer } 131*6d49e1aeSJan Lentfer 132*6d49e1aeSJan Lentfer static inline void wpabuf_put_data(struct wpabuf *buf, const void *data, 133*6d49e1aeSJan Lentfer size_t len) 134*6d49e1aeSJan Lentfer { 135*6d49e1aeSJan Lentfer if (data) 136*6d49e1aeSJan Lentfer os_memcpy(wpabuf_put(buf, len), data, len); 137*6d49e1aeSJan Lentfer } 138*6d49e1aeSJan Lentfer 139*6d49e1aeSJan Lentfer static inline void wpabuf_put_buf(struct wpabuf *dst, 140*6d49e1aeSJan Lentfer const struct wpabuf *src) 141*6d49e1aeSJan Lentfer { 142*6d49e1aeSJan Lentfer wpabuf_put_data(dst, wpabuf_head(src), wpabuf_len(src)); 143*6d49e1aeSJan Lentfer } 144*6d49e1aeSJan Lentfer 145*6d49e1aeSJan Lentfer static inline void wpabuf_set(struct wpabuf *buf, const void *data, size_t len) 146*6d49e1aeSJan Lentfer { 147*6d49e1aeSJan Lentfer buf->ext_data = (u8 *) data; 148*6d49e1aeSJan Lentfer buf->size = buf->used = len; 149*6d49e1aeSJan Lentfer } 150*6d49e1aeSJan Lentfer 151*6d49e1aeSJan Lentfer static inline void wpabuf_put_str(struct wpabuf *dst, const char *str) 152*6d49e1aeSJan Lentfer { 153*6d49e1aeSJan Lentfer wpabuf_put_data(dst, str, os_strlen(str)); 154*6d49e1aeSJan Lentfer } 155*6d49e1aeSJan Lentfer 156*6d49e1aeSJan Lentfer #endif /* WPABUF_H */ 157