]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
wpabuf: Array of wpabuf
authorJouni Malinen <jouni.malinen@oss.qualcomm.com>
Mon, 27 Oct 2025 11:44:27 +0000 (13:44 +0200)
committerJouni Malinen <j@w1.fi>
Mon, 27 Oct 2025 16:53:32 +0000 (18:53 +0200)
Add common functions for a data structure consisting of a variable
number of struct wpabuf buffers.

Signed-off-by: Jouni Malinen <jouni.malinen@oss.qualcomm.com>
src/utils/wpabuf.c
src/utils/wpabuf.h

index 77ee47288007df77533fda57ced3783000e374dd..df94fe68aa4fa0cf21ebb3bbd471a0d5a21340bb 100644 (file)
@@ -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;
+}
index 88d72bd685e5c86cd319a3a806052002b177a898..284abbf54156d3db63a33f3983510d6eb1d076d4 100644 (file)
@@ -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 */