wpabuf.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Dynamic data buffer
  3. * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "trace.h"
  17. #include "wpabuf.h"
  18. #ifdef WPA_TRACE
  19. #define WPABUF_MAGIC 0x51a974e3
  20. struct wpabuf_trace {
  21. unsigned int magic;
  22. };
  23. static struct wpabuf_trace * wpabuf_get_trace(const struct wpabuf *buf)
  24. {
  25. return (struct wpabuf_trace *)
  26. ((const u8 *) buf - sizeof(struct wpabuf_trace));
  27. }
  28. #endif /* WPA_TRACE */
  29. static void wpabuf_overflow(const struct wpabuf *buf, size_t len)
  30. {
  31. #ifdef WPA_TRACE
  32. struct wpabuf_trace *trace = wpabuf_get_trace(buf);
  33. if (trace->magic != WPABUF_MAGIC) {
  34. wpa_printf(MSG_ERROR, "wpabuf: invalid magic %x",
  35. trace->magic);
  36. }
  37. #endif /* WPA_TRACE */
  38. wpa_printf(MSG_ERROR, "wpabuf %p (size=%lu used=%lu) overflow len=%lu",
  39. buf, (unsigned long) buf->size, (unsigned long) buf->used,
  40. (unsigned long) len);
  41. wpa_trace_show("wpabuf overflow");
  42. abort();
  43. }
  44. int wpabuf_resize(struct wpabuf **_buf, size_t add_len)
  45. {
  46. struct wpabuf *buf = *_buf;
  47. #ifdef WPA_TRACE
  48. struct wpabuf_trace *trace;
  49. #endif /* WPA_TRACE */
  50. if (buf == NULL) {
  51. *_buf = wpabuf_alloc(add_len);
  52. return *_buf == NULL ? -1 : 0;
  53. }
  54. #ifdef WPA_TRACE
  55. trace = wpabuf_get_trace(buf);
  56. if (trace->magic != WPABUF_MAGIC) {
  57. wpa_printf(MSG_ERROR, "wpabuf: invalid magic %x",
  58. trace->magic);
  59. wpa_trace_show("wpabuf_resize invalid magic");
  60. abort();
  61. }
  62. #endif /* WPA_TRACE */
  63. if (buf->used + add_len > buf->size) {
  64. unsigned char *nbuf;
  65. if (buf->ext_data) {
  66. nbuf = os_realloc(buf->ext_data, buf->used + add_len);
  67. if (nbuf == NULL)
  68. return -1;
  69. os_memset(nbuf + buf->used, 0, add_len);
  70. buf->ext_data = nbuf;
  71. } else {
  72. #ifdef WPA_TRACE
  73. nbuf = os_realloc(trace, sizeof(struct wpabuf_trace) +
  74. sizeof(struct wpabuf) +
  75. buf->used + add_len);
  76. if (nbuf == NULL)
  77. return -1;
  78. trace = (struct wpabuf_trace *) nbuf;
  79. buf = (struct wpabuf *) (trace + 1);
  80. os_memset(nbuf + sizeof(struct wpabuf_trace) +
  81. sizeof(struct wpabuf) + buf->used, 0,
  82. add_len);
  83. #else /* WPA_TRACE */
  84. nbuf = os_realloc(buf, sizeof(struct wpabuf) +
  85. buf->used + add_len);
  86. if (nbuf == NULL)
  87. return -1;
  88. buf = (struct wpabuf *) nbuf;
  89. os_memset(nbuf + sizeof(struct wpabuf) + buf->used, 0,
  90. add_len);
  91. #endif /* WPA_TRACE */
  92. *_buf = buf;
  93. }
  94. buf->size = buf->used + add_len;
  95. }
  96. return 0;
  97. }
  98. /**
  99. * wpabuf_alloc - Allocate a wpabuf of the given size
  100. * @len: Length for the allocated buffer
  101. * Returns: Buffer to the allocated wpabuf or %NULL on failure
  102. */
  103. struct wpabuf * wpabuf_alloc(size_t len)
  104. {
  105. #ifdef WPA_TRACE
  106. struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
  107. sizeof(struct wpabuf) + len);
  108. struct wpabuf *buf;
  109. if (trace == NULL)
  110. return NULL;
  111. trace->magic = WPABUF_MAGIC;
  112. buf = (struct wpabuf *) (trace + 1);
  113. #else /* WPA_TRACE */
  114. struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf) + len);
  115. if (buf == NULL)
  116. return NULL;
  117. #endif /* WPA_TRACE */
  118. buf->size = len;
  119. return buf;
  120. }
  121. struct wpabuf * wpabuf_alloc_ext_data(u8 *data, size_t len)
  122. {
  123. #ifdef WPA_TRACE
  124. struct wpabuf_trace *trace = os_zalloc(sizeof(struct wpabuf_trace) +
  125. sizeof(struct wpabuf));
  126. struct wpabuf *buf;
  127. if (trace == NULL)
  128. return NULL;
  129. trace->magic = WPABUF_MAGIC;
  130. buf = (struct wpabuf *) (trace + 1);
  131. #else /* WPA_TRACE */
  132. struct wpabuf *buf = os_zalloc(sizeof(struct wpabuf));
  133. if (buf == NULL)
  134. return NULL;
  135. #endif /* WPA_TRACE */
  136. buf->size = len;
  137. buf->used = len;
  138. buf->ext_data = data;
  139. return buf;
  140. }
  141. struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len)
  142. {
  143. struct wpabuf *buf = wpabuf_alloc(len);
  144. if (buf)
  145. wpabuf_put_data(buf, data, len);
  146. return buf;
  147. }
  148. struct wpabuf * wpabuf_dup(const struct wpabuf *src)
  149. {
  150. struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src));
  151. if (buf)
  152. wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src));
  153. return buf;
  154. }
  155. /**
  156. * wpabuf_free - Free a wpabuf
  157. * @buf: wpabuf buffer
  158. */
  159. void wpabuf_free(struct wpabuf *buf)
  160. {
  161. #ifdef WPA_TRACE
  162. struct wpabuf_trace *trace;
  163. if (buf == NULL)
  164. return;
  165. trace = wpabuf_get_trace(buf);
  166. if (trace->magic != WPABUF_MAGIC) {
  167. wpa_printf(MSG_ERROR, "wpabuf_free: invalid magic %x",
  168. trace->magic);
  169. wpa_trace_show("wpabuf_free magic mismatch");
  170. abort();
  171. }
  172. os_free(buf->ext_data);
  173. os_free(trace);
  174. #else /* WPA_TRACE */
  175. if (buf == NULL)
  176. return;
  177. os_free(buf->ext_data);
  178. os_free(buf);
  179. #endif /* WPA_TRACE */
  180. }
  181. void * wpabuf_put(struct wpabuf *buf, size_t len)
  182. {
  183. void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
  184. buf->used += len;
  185. if (buf->used > buf->size) {
  186. wpabuf_overflow(buf, len);
  187. }
  188. return tmp;
  189. }
  190. /**
  191. * wpabuf_concat - Concatenate two buffers into a newly allocated one
  192. * @a: First buffer
  193. * @b: Second buffer
  194. * Returns: wpabuf with concatenated a + b data or %NULL on failure
  195. *
  196. * Both buffers a and b will be freed regardless of the return value. Input
  197. * buffers can be %NULL which is interpreted as an empty buffer.
  198. */
  199. struct wpabuf * wpabuf_concat(struct wpabuf *a, struct wpabuf *b)
  200. {
  201. struct wpabuf *n = NULL;
  202. size_t len = 0;
  203. if (b == NULL)
  204. return a;
  205. if (a)
  206. len += wpabuf_len(a);
  207. if (b)
  208. len += wpabuf_len(b);
  209. n = wpabuf_alloc(len);
  210. if (n) {
  211. if (a)
  212. wpabuf_put_buf(n, a);
  213. if (b)
  214. wpabuf_put_buf(n, b);
  215. }
  216. wpabuf_free(a);
  217. wpabuf_free(b);
  218. return n;
  219. }
  220. /**
  221. * wpabuf_zeropad - Pad buffer with 0x00 octets (prefix) to specified length
  222. * @buf: Buffer to be padded
  223. * @len: Length for the padded buffer
  224. * Returns: wpabuf padded to len octets or %NULL on failure
  225. *
  226. * If buf is longer than len octets or of same size, it will be returned as-is.
  227. * Otherwise a new buffer is allocated and prefixed with 0x00 octets followed
  228. * by the source data. The source buffer will be freed on error, i.e., caller
  229. * will only be responsible on freeing the returned buffer. If buf is %NULL,
  230. * %NULL will be returned.
  231. */
  232. struct wpabuf * wpabuf_zeropad(struct wpabuf *buf, size_t len)
  233. {
  234. struct wpabuf *ret;
  235. size_t blen;
  236. if (buf == NULL)
  237. return NULL;
  238. blen = wpabuf_len(buf);
  239. if (blen >= len)
  240. return buf;
  241. ret = wpabuf_alloc(len);
  242. if (ret) {
  243. os_memset(wpabuf_put(ret, len - blen), 0, len - blen);
  244. wpabuf_put_buf(ret, buf);
  245. }
  246. wpabuf_free(buf);
  247. return ret;
  248. }
  249. void wpabuf_printf(struct wpabuf *buf, char *fmt, ...)
  250. {
  251. va_list ap;
  252. void *tmp = wpabuf_mhead_u8(buf) + wpabuf_len(buf);
  253. int res;
  254. va_start(ap, fmt);
  255. res = vsnprintf(tmp, buf->size - buf->used, fmt, ap);
  256. va_end(ap);
  257. if (res < 0 || (size_t) res >= buf->size - buf->used)
  258. wpabuf_overflow(buf, res);
  259. buf->used += res;
  260. }