author | Olivier Brunel
<jjk@jjacky.com> 2023-04-12 17:40:03 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-05-20 18:06:36 UTC |
parent | 25cf80e0f21ef8ab7f85b358619fae27eb8d7576 |
src/doc/buffer.h.0.md | +8 | -2 |
src/doc/buffer.h/buffer_putbase32.3.md | +40 | -0 |
src/liblimb/buffer.h/buffer_putbase.c | +31 | -0 |
src/liblimb/include/limb/buffer.h | +5 | -0 |
diff --git a/src/doc/buffer.h.0.md b/src/doc/buffer.h.0.md index 7261513..4f58672 100644 --- a/src/doc/buffer.h.0.md +++ b/src/doc/buffer.h.0.md @@ -76,7 +76,13 @@ The following macros are defined : ## Functions -The following functions are defined : +The following functions/macros are defined : + +: [buffer_putbase32](3) +:: To write given data encoded in base32. + +: [buffer_putbase64](3) +:: To write given data encoded in base64. : [buffer_putescall](3) :: To write given data from a specified offset whilst escaping characters as @@ -92,4 +98,4 @@ The following functions are defined : :: To write given data as hexadecimal dump. : [buffer_putmsg](3) -:: To write a message, given as an array of NUL-terminated strings +:: To write a message, given as an array of NUL-terminated strings. diff --git a/src/doc/buffer.h/buffer_putbase32.3.md b/src/doc/buffer.h/buffer_putbase32.3.md new file mode 100644 index 0000000..6164bb0 --- /dev/null +++ b/src/doc/buffer.h/buffer_putbase32.3.md @@ -0,0 +1,40 @@ +% limb manual +% buffer_putbase32(3) + +# NAME + +buffer\_putbase32, buffer\_putbase64 - write byte array content encoded in +base32/base64 into buffer + +# SYNOPSIS + + #include <limb/buffer.h> + +```pre hl +ssize_t buffer_putbase32(buffer *<em>buf</em>, const char *<em>data</em>, size_t <em>dlen</em>, int <em>pad</em>) +ssize_t buffer_putbase64(buffer *<em>buf</em>, const char *<em>data</em>, size_t <em>dlen</em>, int <em>pad</em>) +``` + +# DESCRIPTION + +The `buffer_putbase32`() function writes the content of byte array pointed to by +`data` of length `dlen` bytes into buffer `buf` encoded in base32. Padding will +be used in the output unless `pad` is zero. + +For more about the encoding, refer to [base32_fmt](3). + +The `buffer_putbase64`() function writes the content of byte array pointed to by +`data` of length `dlen` bytes into buffer `buf` encoded in base64. Padding will +be used in the output unless `pad` is zero. + +For more about the encoding, refer to [base64_fmt](3). + +# RETURN VALUE + +These functions return the number of bytes written into `buf` on success. +Otherwise, they return -1 and set `errno` to indicate the error. + +# ERRORS + +These functions may fail and set `errno` for any of the errors specified for +[buffer_put](3). diff --git a/src/liblimb/buffer.h/buffer_putbase.c b/src/liblimb/buffer.h/buffer_putbase.c new file mode 100644 index 0000000..4ae0233 --- /dev/null +++ b/src/liblimb/buffer.h/buffer_putbase.c @@ -0,0 +1,31 @@ +/* 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/buffer.h> + +/* XXX intended for internal use, hence no check is performed on bin/bout and + * their validity. Specifically, should bout be too small the output would be + * written *outside* of buf leading to, well, problems. + */ + +ssize_t +buffer_putbase(buffer *b, const char *data, size_t dlen, + base_fmt_fn basefmt, size_t bin, size_t bout, int pad) +{ + ssize_t w = 0; + + while (dlen) { + char buf[bout]; + size_t l = (dlen > bin) ? bin : dlen; + ssize_t n = basefmt(buf, data, l, pad); + + n = buffer_put(b, buf, n); + if (n < 0) + return -1; + w += n; + data += l; + dlen -= l; + } + + return w; +} diff --git a/src/liblimb/include/limb/buffer.h b/src/liblimb/include/limb/buffer.h index 4cf6e2f..1112c77 100644 --- a/src/liblimb/include/limb/buffer.h +++ b/src/liblimb/include/limb/buffer.h @@ -30,4 +30,9 @@ extern ssize_t buffer_putesc(buffer *b, const char *s, size_t len); extern ssize_t buffer_putescs(buffer *b, const char *s); extern ssize_t buffer_puthex(buffer *b, const void *data, size_t dlen); +typedef ssize_t (*base_fmt_fn) (char *dst, const char *data, size_t dlen, int pad); +extern ssize_t buffer_putbase(buffer *b, const char *data, size_t dlen, base_fmt_fn basefmt, size_t bin, size_t bout, int pad); +#define buffer_putbase32(b,d,l,p) buffer_putbase(b, d, l, base32, 40, 64, p) +#define buffer_putbase64(b,d,l,p) buffer_putbase(b, d, l, base64, 42, 56, p) + #endif /* LIMB_BUFFER_H */