Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / buffer.h / buffer_putbase.c

/* 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;
}