From: Jouni Malinen Date: Mon, 27 Oct 2025 11:44:27 +0000 (+0200) Subject: wpabuf: Array of wpabuf X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=660dd57c6fc0c094e11d818aae9a5dc0f04ec714;p=thirdparty%2Fhostap.git wpabuf: Array of wpabuf Add common functions for a data structure consisting of a variable number of struct wpabuf buffers. Signed-off-by: Jouni Malinen --- diff --git a/src/utils/wpabuf.c b/src/utils/wpabuf.c index 77ee47288..df94fe68a 100644 --- a/src/utils/wpabuf.c +++ b/src/utils/wpabuf.c @@ -168,7 +168,11 @@ struct wpabuf * wpabuf_alloc_copy(const void *data, size_t len) struct wpabuf * wpabuf_dup(const struct wpabuf *src) { - struct wpabuf *buf = wpabuf_alloc(wpabuf_len(src)); + struct wpabuf *buf; + + if (!src) + return NULL; + buf = wpabuf_alloc(wpabuf_len(src)); if (buf) wpabuf_put_data(buf, wpabuf_head(src), wpabuf_len(src)); return buf; @@ -338,3 +342,54 @@ struct wpabuf * wpabuf_parse_bin(const char *buf) return ret; } + + +struct wpabuf_array * wpabuf_array_alloc(void) +{ + struct wpabuf_array *wa; + + wa = os_zalloc(sizeof(*wa)); + return wa; +} + + +void wpabuf_array_free(struct wpabuf_array *wa) +{ + unsigned int idx; + + if (!wa) + return; + for (idx = 0; idx < wa->num; idx++) + wpabuf_free(wa->buf[idx]); + os_free(wa->buf); + os_free(wa); +} + + +int wpabuf_array_add(struct wpabuf_array *wa, struct wpabuf *buf) +{ + struct wpabuf **n; + + if (!wa || !buf) + return -1; + n = os_realloc(wa->buf, (wa->num + 1) * sizeof(struct wpabuf *)); + if (!n) + return -1; + wa->buf = n; + wa->buf[wa->num++] = buf; + return 0; +} + + +void wpabuf_array_remove(struct wpabuf_array *wa, unsigned int idx) +{ + if (!wa || wa->num == 0 || idx >= wa->num) + return; + wpabuf_free(wa->buf[idx]); + while (idx + 1 < wa->num) { + wa->buf[idx] = wa->buf[idx + 1]; + idx++; + } + wa->num--; + wa->buf[wa->num] = NULL; +} diff --git a/src/utils/wpabuf.h b/src/utils/wpabuf.h index 88d72bd68..284abbf54 100644 --- a/src/utils/wpabuf.h +++ b/src/utils/wpabuf.h @@ -194,4 +194,15 @@ static inline void wpabuf_put_str(struct wpabuf *dst, const char *str) wpabuf_put_data(dst, str, os_strlen(str)); } + +struct wpabuf_array { + unsigned int num; + struct wpabuf **buf; +}; + +struct wpabuf_array * wpabuf_array_alloc(void); +void wpabuf_array_free(struct wpabuf_array *wa); +int wpabuf_array_add(struct wpabuf_array *wa, struct wpabuf *buf); +void wpabuf_array_remove(struct wpabuf_array *wa, unsigned int idx); + #endif /* WPABUF_H */