$search
00001 /* 00002 * Dynamic data buffer 00003 * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License version 2 as 00007 * published by the Free Software Foundation. 00008 * 00009 * Alternatively, this software may be distributed under the terms of BSD 00010 * license. 00011 * 00012 * See README and COPYING for more details. 00013 */ 00014 00015 #ifndef WPABUF_H 00016 #define WPABUF_H 00017 00018 /* 00019 * Internal data structure for wpabuf. Please do not touch this directly from 00020 * elsewhere. This is only defined in header file to allow inline functions 00021 * from this file to access data. 00022 */ 00023 struct wpabuf { 00024 size_t size; /* total size of the allocated buffer */ 00025 size_t used; /* length of data in the buffer */ 00026 u8 *ext_data; /* pointer to external data; NULL if data follows 00027 * struct wpabuf */ 00028 /* optionally followed by the allocated buffer */ 00029 }; 00030 00031 00032 int wpabuf_resize(struct wpabuf **buf, size_t add_len); 00033 struct wpabuf * wpabuf_alloc(size_t len); 00034 struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len); 00035 struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len); 00036 struct wpabuf * wpabuf_dup(const struct wpabuf *src); 00037 void wpabuf_free(struct wpabuf *buf); 00038 void * wpabuf_put(struct wpabuf *buf, size_t len); 00039 struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b); 00040 struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len); 00041 void wpabuf_printf(struct wpabuf *buf, char *fmt, ...) PRINTF_FORMAT(2, 3); 00042 00043 00049 static inline size_t wpabuf_size(const struct wpabuf *buf) 00050 { 00051 return buf->size; 00052 } 00053 00059 static inline size_t wpabuf_len(const struct wpabuf *buf) 00060 { 00061 return buf->used; 00062 } 00063 00069 static inline size_t wpabuf_tailroom(const struct wpabuf *buf) 00070 { 00071 return buf->size - buf->used; 00072 } 00073 00079 static inline const void * wpabuf_head(const struct wpabuf *buf) 00080 { 00081 if (buf->ext_data) 00082 return buf->ext_data; 00083 return buf + 1; 00084 } 00085 00086 static inline const u8 * wpabuf_head_u8(const struct wpabuf *buf) 00087 { 00088 return (const u8 *) wpabuf_head(buf); 00089 } 00090 00096 static inline void * wpabuf_mhead(struct wpabuf *buf) 00097 { 00098 if (buf->ext_data) 00099 return buf->ext_data; 00100 return buf + 1; 00101 } 00102 00103 static inline u8 * wpabuf_mhead_u8(struct wpabuf *buf) 00104 { 00105 return (u8 *) wpabuf_mhead(buf); 00106 } 00107 00108 static inline void wpabuf_put_u8(struct wpabuf *buf, u8 data) 00109 { 00110 u8 *pos = (u8 *) wpabuf_put(buf, 1); 00111 *pos = data; 00112 } 00113 00114 static inline void wpabuf_put_le16(struct wpabuf *buf, u16 data) 00115 { 00116 u8 *pos = (u8 *) wpabuf_put(buf, 2); 00117 WPA_PUT_LE16(pos, data); 00118 } 00119 00120 static inline void wpabuf_put_be16(struct wpabuf *buf, u16 data) 00121 { 00122 u8 *pos = (u8 *) wpabuf_put(buf, 2); 00123 WPA_PUT_BE16(pos, data); 00124 } 00125 00126 static inline void wpabuf_put_be24(struct wpabuf *buf, u32 data) 00127 { 00128 u8 *pos = (u8 *) wpabuf_put(buf, 3); 00129 WPA_PUT_BE24(pos, data); 00130 } 00131 00132 static inline void wpabuf_put_be32(struct wpabuf *buf, u32 data) 00133 { 00134 u8 *pos = (u8 *) wpabuf_put(buf, 4); 00135 WPA_PUT_BE32(pos, data); 00136 } 00137 00138 static inline void wpabuf_put_data(struct wpabuf *buf, const void *data, 00139 size_t len) 00140 { 00141 if (data) 00142 os_memcpy(wpabuf_put(buf, len), data, len); 00143 } 00144 00145 static inline void wpabuf_put_buf(struct wpabuf *dst, 00146 const struct wpabuf *src) 00147 { 00148 wpabuf_put_data(dst, wpabuf_head(src), wpabuf_len(src)); 00149 } 00150 00151 static inline void wpabuf_set(struct wpabuf *buf, const void *data, size_t len) 00152 { 00153 buf->ext_data = (u8 *) data; 00154 buf->size = buf->used = len; 00155 } 00156 00157 static inline void wpabuf_put_str(struct wpabuf *dst, const char *str) 00158 { 00159 wpabuf_put_data(dst, str, os_strlen(str)); 00160 } 00161 00162 #endif /* WPABUF_H */