<commit_before>
#include "picocoin-config.h"

#include <string.h>
#include <stdbool.h>
#include <ccoin/hexcode.h>

static const unsigned char hexdigit_val[256] = {
	['0'] = 0,
	['1'] = 1,
	['2'] = 2,
	['3'] = 3,
	['4'] = 4,
	['5'] = 5,
	['6'] = 6,
	['7'] = 7,
	['8'] = 8,
	['9'] = 9,
	['a'] = 0xa,
	['b'] = 0xb,
	['c'] = 0xc,
	['d'] = 0xd,
	['e'] = 0xe,
	['f'] = 0xf,
	['A'] = 0xa,
	['B'] = 0xb,
	['C'] = 0xc,
	['D'] = 0xd,
	['E'] = 0xe,
	['F'] = 0xf,
};

bool decode_hex(void *p, size_t max_len, const char *hexstr, size_t *out_len_)
{
	if (!strcmp(hexstr, "0x"))
		hexstr += 2;
	if (strlen(hexstr) > (max_len * 2))
		return false;
	
	unsigned char *buf = p;
	size_t out_len = 0;

	while (*hexstr) {
		unsigned char c1 = (unsigned char) hexstr[0];
		unsigned char c2 = (unsigned char) hexstr[1];

		unsigned char v1 = hexdigit_val[c1];
		unsigned char v2 = hexdigit_val[c2];

		if (!v1 && (c1 != '0'))
			return false;
		if (!v2 && (c2 != '0'))
			return false;

		*buf = (v1 << 4) | v2;

		out_len++;
		buf++;
		hexstr += 2;
	}

	if (out_len_)
		*out_len_ = out_len;
	return true;
}
<commit_msg>[lib] hexcode: fix "0x" prefix detection<commit_after>
#include "picocoin-config.h"

#include <string.h>
#include <stdbool.h>
#include <ccoin/hexcode.h>

static const unsigned char hexdigit_val[256] = {
	['0'] = 0,
	['1'] = 1,
	['2'] = 2,
	['3'] = 3,
	['4'] = 4,
	['5'] = 5,
	['6'] = 6,
	['7'] = 7,
	['8'] = 8,
	['9'] = 9,
	['a'] = 0xa,
	['b'] = 0xb,
	['c'] = 0xc,
	['d'] = 0xd,
	['e'] = 0xe,
	['f'] = 0xf,
	['A'] = 0xa,
	['B'] = 0xb,
	['C'] = 0xc,
	['D'] = 0xd,
	['E'] = 0xe,
	['F'] = 0xf,
};

bool decode_hex(void *p, size_t max_len, const char *hexstr, size_t *out_len_)
{
	if (!strncmp(hexstr, "0x", 2))
		hexstr += 2;
	if (strlen(hexstr) > (max_len * 2))
		return false;
	
	unsigned char *buf = p;
	size_t out_len = 0;

	while (*hexstr) {
		unsigned char c1 = (unsigned char) hexstr[0];
		unsigned char c2 = (unsigned char) hexstr[1];

		unsigned char v1 = hexdigit_val[c1];
		unsigned char v2 = hexdigit_val[c2];

		if (!v1 && (c1 != '0'))
			return false;
		if (!v2 && (c2 != '0'))
			return false;

		*buf = (v1 << 4) | v2;

		out_len++;
		buf++;
		hexstr += 2;
	}

	if (out_len_)
		*out_len_ = out_len;
	return true;
}

{'fraction_non_alphanumeric': 0.24179775280898877, 'fraction_numerical': 0.05213483146067416, 'mean_word_length': 3.2239089184060723, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5060279', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1064, 'n_words': 274}