/* 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/u64.h>
int
u64_unpack_trim(u64 *val, const char *sce_, size_t slen)
{
const u8 *sce = (const u8 *) sce_;
unsigned int b = 0;
*val = 0;
for (;;) {
/* is there a(nother) byte to read? */
if (b >= slen) return -1;
u8 byte = sce[b];
/* if we're on the last byte, add the last 8 bits and be done */
if (b == 8) {
*val |= (u64) byte << (7 * 8);
break;
}
/* extract the last 7 bits from byte and add them into our val */
*val |= (u64) (byte & 127) << (7 * b);
/* are we done? */
if (!(byte & 128))
break;
++b;
}
return b + 1;
}