Welcome to little lamb

Code » limb » commit 5bdba03

Add siovec_off() to return the byte at a given offset

author Olivier Brunel
2023-03-17 15:38:26 UTC
committer Olivier Brunel
2023-03-19 07:59:02 UTC
parent dd60535150378a129567fb26efe67272235a260c

Add siovec_off() to return the byte at a given offset

doc/siovec.h.0.md +6 -0
doc/siovec_off.3.md +35 -0
include/limb/siovec.h +2 -0
meta/libs/limb +1 -0
src/siovec_off.c +15 -0

diff --git a/doc/siovec.h.0.md b/doc/siovec.h.0.md
index 4b3e599..5b998f3 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_off](3)
+:: Same as [siovec_off](3)
+
 : [siov_gather](3)
 :: Same as [siovec_gather](3) but with destination as first arguments
 
@@ -69,6 +72,9 @@ The following functions/macros are defined :
 : [siovec_gather0](3)
 :: Same as [siovec_gather](3) but ensures the data gathered is NUL terminated
 
+: [siovec_off](3)
+:: Return byte of data from a *struct iovec* at a given offset
+
 : [siovec_seek_gather](3)
 :: Same as [siovec_gather](3) but from a given offset
 
diff --git a/doc/siovec_off.3.md b/doc/siovec_off.3.md
new file mode 100644
index 0000000..f696f80
--- /dev/null
+++ b/doc/siovec_off.3.md
@@ -0,0 +1,35 @@
+% limb manual
+% siovec_off(3)
+
+# NAME
+
+siovec\_off - return the byte at the given offset in data from *struct iovec*
+
+# SYNOPSIS
+
+    #include <limb/siovec.h>
+
+```pre hl
+int siovec_off(const struct iovec *<em>v</em>, unsigned int <em>n</em>, size_t <em>off</em>)
+```
+
+# DESCRIPTION
+
+The `siovec_off`() function returns the byte at offset `offset` from the data
+scattered across given array `v` of `n` *struct iovec*.
+
+# RETURN VALUE
+
+The `siovec_off`() function returns the `offset`th byte of data scattered the
+specified array of *struct iovec*, or -1 and set `errno` to indicate the error.
+
+# ERRORS
+
+The function may fail if:
+
+: *EINVAL*
+:: The value `offset` is invalid
+
+# SEE ALSO
+
+[siovec_seek](3), [siovec_gather](3)
diff --git a/include/limb/siovec.h b/include/limb/siovec.h
index 932d726..2a17a49 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_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,
                                  char *dst, size_t max);
@@ -21,6 +22,7 @@ extern size_t siovec_seek_bytein(const struct iovec *v, unsigned int n, size_t o
 extern size_t siovec_search(const struct iovec *v, unsigned int n, const char *str, size_t len);
 
 #define siov_len(v,n)                       siovec_len(v, n)
+#define siov_off(v,n,o)                     siovec_off(v, n, o)
 #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 271cf9d..e96eab8 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_off.o
 obj/siovec_seek_bytechr.o
 obj/siovec_seek_bytein.o
 obj/siovec_seek_gather.o
diff --git a/src/siovec_off.c b/src/siovec_off.c
new file mode 100644
index 0000000..b94fed4
--- /dev/null
+++ b/src/siovec_off.c
@@ -0,0 +1,15 @@
+/* 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 <errno.h>
+#include "limb/siovec.h"
+
+int
+siovec_off(const struct iovec *v, unsigned int n, size_t off)
+{
+    for (int i = 0; i < n; ++i) {
+        if (v[i].iov_len > off) return ((char *) v[i].iov_base)[off];
+        off -= v[i].iov_len;
+    }
+    return (errno = EINVAL, -1);
+}