#include "bej_common.h"

uint64_t bejGetUnsignedInteger(const uint8_t* bytes, uint8_t numOfBytes)
{
    uint64_t num = 0;
    for (uint8_t i = 0; i < numOfBytes; ++i)
    {
        num |= (uint64_t)(*(bytes + i)) << (i * 8);
    }
    return num;
}

uint64_t bejGetNnint(const uint8_t* nnint)
{
    // In nnint, first byte indicate how many bytes are there. Remaining bytes
    // represent the value in little-endian format.
    const uint8_t size = *nnint;
    return bejGetUnsignedInteger(nnint + sizeof(uint8_t), size);
}

uint8_t bejGetNnintSize(const uint8_t* nnint)
{
    // In nnint, first byte indicate how many bytes are there.
    return *nnint + sizeof(uint8_t);
}

uint8_t bejNnintEncodingSizeOfUInt(uint64_t val)
{
    uint8_t bytes = 0;
    do
    {
        // Even if the value is 0, we need a byte for that.
        ++bytes;
        val = val >> 8;
    } while (val != 0);
    // Need 1 byte to add the nnint length.
    return bytes + 1;
}

uint8_t bejNnintLengthFieldOfUInt(uint64_t val)
{
    // From the size of the encoded value, we need 1 byte for the length field.
    return bejNnintEncodingSizeOfUInt(val) - 1;
}
