#include <stdint.h>
#include <stdlib.h>

#include "hex.h"

static const char hexdigits[] = "0123456789ABCDEF";

char *
HexEnc(const unsigned char *src, const size_t srcLen)
{
    // it was based on libressl/crypto/x509v3/v3_utl.c:hex_to_string
    char *dst = (char *)malloc(1 + srcLen * 2);
    if (dst == NULL) {
        return NULL;
    }
    size_t i = 0;
    for (; i < srcLen; i++) {
        dst[(i * 2) + 0] = hexdigits
            [(src[i] >> (uint8_t)4) & (uint8_t)0x0F]; // NOLINT(hicpp-signed-bitwise)
        dst[(i * 2) + 1] = hexdigits[src[i] & (uint8_t)0x0F];
    }
    dst[srcLen * 2] = 0;
    return dst;
}
