]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ncbuf2: implement ncb2_data()
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 15 Oct 2025 09:56:16 +0000 (11:56 +0200)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Fri, 17 Oct 2025 09:06:07 +0000 (11:06 +0200)
include/haproxy/ncbuf2.h
src/ncbuf2.c

index 5838b95c66d2fb7abb571982be0a72f626a368e7..0170b2a946ca5a1930f8e6764a7bae0dc80d94c1 100644 (file)
@@ -4,6 +4,11 @@
 #include <haproxy/ncbuf2-t.h>
 #include <haproxy/ncbuf_common-t.h>
 
+static inline int ncb2_is_null(const struct ncbuf2 *buf)
+{
+       return buf->size == 0;
+}
+
 struct ncbuf2 ncb2_make(char *area, ncb2_sz_t size, ncb2_sz_t head);
 
 static inline char *ncb2_orig(const struct ncbuf2 *buf)
@@ -21,6 +26,16 @@ static inline char *ncb2_wrap(const struct ncbuf2 *buf)
        return buf->area + buf->size;
 }
 
+static inline ncb2_sz_t ncb2_size(const struct ncbuf2 *buf)
+{
+       if (ncb2_is_null(buf))
+               return 0;
+
+       return buf->size;
+}
+
+ncb2_sz_t ncb2_data(const struct ncbuf2 *buf, ncb2_sz_t offset);
+
 enum ncb_ret ncb2_add(struct ncbuf2 *buf, ncb2_sz_t off,
                       const char *data, ncb2_sz_t len, enum ncb_add_mode mode);
 
index 267d8ae03b8cafdcd0758cd993e6fb445f772d30..9370efba2f946970562d4e81f0cd57d7b83c5988 100644 (file)
@@ -169,8 +169,30 @@ int ncb2_is_fragmented(const struct ncbuf2 *buf)
 
 ncb2_sz_t ncb2_data(const struct ncbuf2 *buf, ncb2_sz_t off)
 {
-       /* TODO */
-       return 0;
+       struct itbmap it = itbmap_get(buf, off);
+       unsigned char value;
+       ncb2_sz_t count = 0;
+
+       while (itbmap_is_full(&it)) {
+               count += it.bitcount;
+               it = itbmap_next(buf, &it);
+       }
+
+       if (it.b) {
+               value = *it.b & it.mask;
+               while (it.mask && !(it.mask & 0x80)) {
+                       it.mask <<= 1;
+                       value <<= 1;
+               }
+
+               while (it.mask && (it.mask & 0x80) && (value & 0x80)) {
+                       it.mask <<= 1;
+                       value <<= 1;
+                       ++count;
+               }
+       }
+
+       return count;
 }
 
 enum ncb_ret ncb2_add(struct ncbuf2 *buf, ncb2_sz_t off,