Welcome to little lamb

Code » limb » commit 4650720

Add siovec_memcmp()

author Olivier Brunel
2023-03-17 16:04:10 UTC
committer Olivier Brunel
2023-03-19 07:59:38 UTC
parent 5bdba034b0768112e984f91bc2c50d44187bc959

Add siovec_memcmp()

doc/siovec.h.0.md +6 -0
doc/siovec_memcmp.3.md +31 -0
include/limb/siovec.h +2 -0
meta/libs/limb +1 -0
src/siovec_memcmp.c +23 -0

diff --git a/doc/siovec.h.0.md b/doc/siovec.h.0.md
index 5b998f3..0553cbe 100644
--- a/doc/siovec.h.0.md
+++ b/doc/siovec.h.0.md
@@ -30,6 +30,9 @@ The following functions/macros are defined :
 : [siov_len](3)
 :: Same as [siovec_len](3)
 
+: [siov_memcmp](3)
+:: Same as [siovec_memcmp](3)
+
 : [siov_off](3)
 :: Same as [siovec_off](3)
 
@@ -72,6 +75,9 @@ The following functions/macros are defined :
 : [siovec_gather0](3)
 :: Same as [siovec_gather](3) but ensures the data gathered is NUL terminated
 
+: [siovec_memcmp](3)
+:: Compare byte array with data from array of *struct iovec*
+
 : [siovec_off](3)
 :: Return byte of data from a *struct iovec* at a given offset
 
diff --git a/doc/siovec_memcmp.3.md b/doc/siovec_memcmp.3.md
new file mode 100644
index 0000000..46fa4e8
--- /dev/null
+++ b/doc/siovec_memcmp.3.md
@@ -0,0 +1,31 @@
+% limb manual
+% siovec_memcmp(3)
+
+# NAME
+
+siovec\_memcmp - compare a byte array with data from *struct iovec* array
+
+# SYNOPSIS
+
+    #include <limb/siovec.h>
+
+```pre hl
+int siovec_memcmp(const struct iovec *<em>v</em>, unsigned int <em>n</em>, const char *<em>s</em>, size_t <em>len</em>)
+```
+
+# DESCRIPTION
+
+The `siovec_memcmp`() function compares the first `len` bytes of `s` and those
+scattered across given array `v` of `n` *struct iovec* (each interpreted as
+`unsigned char`).
+
+# RETURN VALUE
+
+The `siovec_memcmp`() function returns an integer less than, equal to, or
+greater than zero if the first `len` bytes of scattared data are found,
+respectively, to be less than, to match, or be greater than the first `len`
+bytes of `s`.
+
+# SEE ALSO
+
+[memcmp](3)
diff --git a/include/limb/siovec.h b/include/limb/siovec.h
index 2a17a49..8f4e567 100644
--- a/include/limb/siovec.h
+++ b/include/limb/siovec.h
@@ -7,6 +7,7 @@
 #include <skalibs/siovec.h>
 
 extern size_t siovec_gather0(const struct iovec *v, unsigned int n, char *dst, size_t max);
+extern int siovec_memcmp(const struct iovec *v, unsigned int n, const char *s, size_t len);
 extern int siovec_off(const struct iovec *v, unsigned int n, size_t off);
 
 extern size_t siovec_seek_gather(const struct iovec *v, unsigned int n, size_t offset,
@@ -23,6 +24,7 @@ extern size_t siovec_search(const struct iovec *v, unsigned int n, const char *s
 
 #define siov_len(v,n)                       siovec_len(v, n)
 #define siov_off(v,n,o)                     siovec_off(v, n, o)
+#define siov_memcmp(v,n,s,l)                siovec_memcmp(v, n, s, l)
 #define siov_gather(dst,max,v,n)            siovec_gather(v, n, dst, max)
 #define siov_gather0(dst,max,v,n)           siovec_gather0(v, n, dst, max)
 #define siov_scatter(v,n,sce,len)           siovec_scatter(v, n, sce, len)
diff --git a/meta/libs/limb b/meta/libs/limb
index e96eab8..f3c86aa 100644
--- a/meta/libs/limb
+++ b/meta/libs/limb
@@ -36,6 +36,7 @@ obj/u64_fmt_generic.o
 obj/u640_fmt_generic.o
 # siovec.h
 obj/siovec_gather0.o
+obj/siovec_memcmp.o
 obj/siovec_off.o
 obj/siovec_seek_bytechr.o
 obj/siovec_seek_bytein.o
diff --git a/src/siovec_memcmp.c b/src/siovec_memcmp.c
new file mode 100644
index 0000000..15646c2
--- /dev/null
+++ b/src/siovec_memcmp.c
@@ -0,0 +1,23 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <string.h>
+#include "limb/siovec.h"
+
+int
+siovec_memcmp(const struct iovec *v, unsigned int n, const char *s, size_t len)
+{
+    int r = (n) ? 0 : (len) ? -1 : 0;
+
+    for (int i = 0; len && i < n; ++i) {
+        if (!v[i].iov_len) continue;
+
+        size_t l = v[i].iov_len >= len ? len : v[i].iov_len;
+        r = memcmp(v[i].iov_base, s, l);
+        if (r) return r;
+
+        len -= l;
+    }
+
+    return r;
+}