/* 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 <string.h>
#include <limb/base.h>
#include <limb/u64.h>
ssize_t
base_fmt(char *dst, int base, const char *data_, size_t dlen, const char *alpha, int strict)
{
const unsigned char *data = (const unsigned char *) data_;
size_t bits, bin, bout, msk;
size_t w = 0;
switch (base) {
case 2: bits = 1; bin = 1; bout = 8; break;
case 4: bits = 2; bin = 1; bout = 4; break;
case 8: bits = 3; bin = 3; bout = 8; break;
case 16: bits = 4; bin = 1; bout = 2; break;
case 32: bits = 5; bin = 5; bout = 8; break;
case 64: bits = 6; bin = 3; bout = 4; break;
case 128: bits = 7; bin = 7; bout = 8; break;
default: return (errno = ERANGE, -1);
}
msk = (1 << bits) - 1;
while (dlen) {
size_t l, o, pad;
if (dlen < bin) {
l = dlen;
o = bout * l / bin;
if (bout * l % bin)
++o;
if (strict) pad = bout - o;
else pad = 1;
} else {
l = bin;
o = bout;
pad = 0;
}
if (dst) {
u64 u = 0;
memcpy(&u, data, l);
u64p_be(&u);
for (size_t i = 0; i < o; ++i)
dst[i] = alpha[(u >> (64 - bits * (i + 1))) & msk];
memset(dst + o, alpha[base], pad);
o += pad;
dst += o;
data += l;
} else {
o += pad;
}
w += o;
dlen -= l;
}
return w;
}