author | Olivier Brunel
<jjk@jjacky.com> 2023-04-30 14:02:57 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-05-20 18:06:39 UTC |
parent | 874b52cbe2e948e6afa4b552f290d34fded6d8da |
src/doc/saencdata.h.0.md | +0 | -21 |
src/doc/saencdata.h/saencdata.3.md | +0 | -40 |
src/doc/samisc.h.0.md | +3 | -0 |
src/doc/samisc.h/sapatrim_put.3.md | +30 | -0 |
src/liblimb/include/limb/saencdata.h | +0 | -12 |
src/liblimb/include/limb/samisc.h | +2 | -0 |
src/liblimb/saencdata.h/saencdata.c | +0 | -32 |
src/liblimb/samisc.h/sapatrim_put.c | +22 | -0 |
diff --git a/src/doc/saencdata.h.0.md b/src/doc/saencdata.h.0.md deleted file mode 100644 index 7f1e0c7..0000000 --- a/src/doc/saencdata.h.0.md +++ /dev/null @@ -1,21 +0,0 @@ -% limb manual -% saencdata.h(0) - -# NAME - -saencdata.h - encode data into stralloc - -# SYNOPSIS - - #include <limb/saencdata.h> - -# DESCRIPTION - -The header defines functions used to encode data into a given stralloc. - -## Functions - -The following functions are defined : - -: [saencdata](3) -:: Encode given data into given stralloc diff --git a/src/doc/saencdata.h/saencdata.3.md b/src/doc/saencdata.h/saencdata.3.md deleted file mode 100644 index 4e1c8f2..0000000 --- a/src/doc/saencdata.h/saencdata.3.md +++ /dev/null @@ -1,40 +0,0 @@ -% limb manual -% saencdata(3) - -# NAME - -saencdata - encode data (integer (u64) or blob (byte array)) into stralloc - -# SYNOPSIS - - #include <limb/saencdata.h> - -```pre hl -int saencdata(stralloc *<em>sa</em>, const u16 <em>id</em>, - const void *<em>val</em>, const size_t <em>size</em>) -``` - -# DESCRIPTION - -The `saencdata`() function will encode into the given stralloc `sa` the -specified `val` as a byte array that can e.g. be stored in a file. - -First the given `id` is stored in little endian. Its most significant bit -determines what kind of data is pointed to by `val` : - -- If set, `val` must point to a blob/byte array of length `size` that will be - stored as-is. -- If unset, `val` must point to an u64 - -## Encoding details - -When encoding a blob/byte array, first its `size` will be stored using -[`u64_pack_trim`](3) then the blob stored as-is. - -When encoding an integer (u64), it will be stored via [`u64_pack_trim`](3) - - -# RETURN VALUE - -`saencdata`() return 1 on success and 0 on failure, i.e. failure to allocate -memory within the given `sa` diff --git a/src/doc/samisc.h.0.md b/src/doc/samisc.h.0.md index c7ddfc4..d0fed88 100644 --- a/src/doc/samisc.h.0.md +++ b/src/doc/samisc.h.0.md @@ -26,3 +26,6 @@ The following functions are defined : : [saoff2ptr](3) :: Transform an array of offsets into an array of pointers from an stralloc + +: [sapatrim_put](3) +:: Encode data in [patrim](5) format into the specified stralloc. diff --git a/src/doc/samisc.h/sapatrim_put.3.md b/src/doc/samisc.h/sapatrim_put.3.md new file mode 100644 index 0000000..5d3939b --- /dev/null +++ b/src/doc/samisc.h/sapatrim_put.3.md @@ -0,0 +1,30 @@ +% limb manual +% sapatrim_put(3) + +# NAME + +sapatrim_put - encode data in PATRIM format into an stralloc + +# SYNOPSIS + + #include <limb/samisc.h> + +```pre hl +int sapatrim_put(stralloc *<em>sa</em>, u64 <em>id</em>, u64 <em>u</em>, const char *<em>data</em>) +``` + +# DESCRIPTION + +The `sapatrim_put`() function will encode into the given stralloc `sa` the +specified `id` in [patrim](5) format. + +If it represents an (unsigned) integer, `u` must be its value, and `data` is +ignored. + +If it represents a blob/byte array, `u` must be its size, and `data` point to +said data (of length `u`). + +# RETURN VALUE + +The `sapatrim_put`() function returns 1 on success and 0 on failure, i.e. +failure to allocate memory within the given `sa`. diff --git a/src/liblimb/include/limb/saencdata.h b/src/liblimb/include/limb/saencdata.h deleted file mode 100644 index 383bc4a..0000000 --- a/src/liblimb/include/limb/saencdata.h +++ /dev/null @@ -1,12 +0,0 @@ -/* 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 */ -#ifndef LIMB_SAENCDATA_h -#define LIMB_SAENCDATA_h - -#include <skalibs/stralloc.h> -#include <limb/int.h> - -extern int saencdata(stralloc *sa, const u16 id, const void *val, const size_t size); - -#endif /* LIMB_SAENCDATA_h */ diff --git a/src/liblimb/include/limb/samisc.h b/src/liblimb/include/limb/samisc.h index abd1648..6ed117d 100644 --- a/src/liblimb/include/limb/samisc.h +++ b/src/liblimb/include/limb/samisc.h @@ -5,7 +5,9 @@ #define LIMB_SAMISC_H #include <skalibs/stralloc.h> +#include <limb/int.h> +extern int sapatrim_put(stralloc *sa, u64 id, u64 u, const char *data); extern int sacoloff(stralloc *sa, size_t from, size_t end); extern void saoff2ptr(stralloc *sa, size_t arroff, size_t n); extern int sacolptr(stralloc *sa, size_t from, size_t end); diff --git a/src/liblimb/saencdata.h/saencdata.c b/src/liblimb/saencdata.h/saencdata.c deleted file mode 100644 index 2e95486..0000000 --- a/src/liblimb/saencdata.h/saencdata.c +++ /dev/null @@ -1,32 +0,0 @@ -/* 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 <limb/saencdata.h> -#include <limb/u16.h> -#include <limb/u64.h> - -int -saencdata(stralloc *sa, const u16 id, const void *val, const size_t size) -{ - int is_blob = id & 0x8000; - int salen = sa->len; - - /* 2 bytes for id, then we might need up to 9 bytes to store our integer */ - if (!stralloc_readyplus(sa, 2 + 9)) - return 0; - u16_pack(sa->s + sa->len, id); - sa->len += 2; - - if (is_blob) { - sa->len += u64_pack_trim(sa->s + sa->len, 9, (u64) size); - if (!stralloc_catb(sa, * (char **) val, size)) - goto err; - } else { - sa->len += u64_pack_trim(sa->s + sa->len, 9, * (u64 *) val); - } - - return 1; -err: - sa->len = salen; - return 0; -} diff --git a/src/liblimb/samisc.h/sapatrim_put.c b/src/liblimb/samisc.h/sapatrim_put.c new file mode 100644 index 0000000..a854b45 --- /dev/null +++ b/src/liblimb/samisc.h/sapatrim_put.c @@ -0,0 +1,22 @@ +/* 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 <limb/patrim.h> +#include <limb/samisc.h> + +int +sapatrim_put(stralloc *sa, u64 id, u64 u, const char *data) +{ + size_t salen = sa->len; + + if (!stralloc_readyplus(sa, 18)) return 0; + + sa->len += patrim_put(sa->s + sa->len, 18, 0, id, u); + + if (patrim_isblob(id) && !stralloc_catb(sa, data, (size_t) u)) { + sa->len = salen; + return 0; + } + + return 1; +}