n_hex regression: hexadecimal encode/decode round-trips and error paths.
n_hex regression: hexadecimal encode/decode round-trips and error paths.
#include <stdio.h>
#include <string.h>
int opt = 0;
while ((opt = getopt(argc, argv, "hvV:")) != EOF) {
switch (opt) {
case 'V':
if (!strncmp("LOG_NULL", optarg, 8))
else if (!strncmp("LOG_NOTICE", optarg, 10))
else if (!strncmp("LOG_INFO", optarg, 8))
else if (!strncmp("LOG_ERR", optarg, 7))
else if (!strncmp("LOG_DEBUG", optarg, 9))
else {
fprintf(stderr, "Unknown log level %s\n", optarg);
exit(1);
}
break;
case 'v':
fprintf(stderr, "ex_hex\n");
exit(1);
case 'h':
default:
fprintf(stderr, "usage: %s [-V LOG_LEVEL]\n", argv[0]);
break;
}
}
}
static void expect_encode(
const unsigned char* data,
size_t len,
const char* want) {
if (!out) {
n_log(
LOG_ERR,
"n_hex_encode returned NULL for expected '%s'", want);
return;
}
if (out->
written != strlen(want) || strcmp(out->
data, want) != 0) {
}
}
const unsigned char a[] = {0x00, 0x01, 0xff, 0xab};
const unsigned char b[] = {0xDE, 0xAD, 0xBE, 0xEF};
}
size_t n = 123;
if (!buf || n != 4) {
n_log(
LOG_ERR,
"decode '0001ffab': got len %zu expected 4", n);
} else if (buf[0] != 0x00 || buf[1] != 0x01 || buf[2] != 0xff || buf[3] != 0xab) {
}
n = 0;
if (!buf || n != 4 || buf[0] != 0xde || buf[1] != 0xad || buf[2] != 0xbe || buf[3] != 0xef) {
n_log(
LOG_ERR,
"decode with whitespace/upper failed (len %zu)", n);
}
const unsigned char payload[] = {0x00, 0x41, 0x00, 0x42};
if (!enc || strcmp(enc->
data,
"00410042") != 0) {
} else {
n = 0;
if (!dec || n != sizeof(payload) || memcmp(dec, payload, sizeof(payload)) != 0) {
n_log(
LOG_ERR,
"round-trip with embedded NUL failed (len %zu)", n);
}
}
}
size_t n = 7;
if (buf || n != 0) {
}
n = 7;
if (buf || n != 0) {
}
n = 7;
if (!buf || n != 0) {
n_log(
LOG_ERR,
"decode empty should yield len 0 buffer (len %zu)", n);
}
}
}
const unsigned char a[] = {0x00, 0x01, 0xff, 0xab};
char buf[9];
if (w != 8 || strcmp(buf, "0001ffab") != 0) {
n_log(
LOG_ERR,
"n_hex_encode_to: got '%s' (w %zu) expected '0001ffab'", buf, w);
}
n_log(
LOG_ERR,
"n_hex_encode_to: undersized buffer should return 0");
}
unsigned char out[4];
if (
n_hex_decode_to(out,
sizeof(out),
"DEADbeef", 4) != TRUE || out[0] != 0xde || out[1] != 0xad || out[2] != 0xbe || out[3] != 0xef) {
}
}
n_log(
LOG_ERR,
"n_hex_decode_to: invalid digit should fail");
}
n_log(
LOG_ERR,
"n_hex_decode_to: undersized out should fail");
}
unsigned char rt[4];
char hb[9];
if (
n_hex_decode_to(rt,
sizeof(rt), hb,
sizeof(a)) != TRUE || memcmp(rt, a,
sizeof(a)) != 0) {
}
}
int main(
int argc,
char** argv) {
return 1;
}
return 0;
}
void process_args(int argc, char **argv)
static void test_encode(void)
static void expect_encode(const unsigned char *data, size_t len, const char *want)
static void test_bounded(void)
static void test_decode_errors(void)
static void test_decode_roundtrip(void)
#define Free(__ptr)
Free Handler to get errors.
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL ).
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
unsigned char * n_hex_decode(const char *hex, size_t *out_len)
decode a hex string (ASCII whitespace between digits is ignored) into a freshly allocated,...
size_t n_hex_encode_to(char *out, size_t out_size, const unsigned char *data, size_t len)
encode len bytes of data as lowercase hex into the caller-provided buffer out (needs out_size >= 2*le...
int n_hex_decode_to(unsigned char *out, size_t out_size, const char *hex, size_t nbytes)
decode exactly nbytes bytes from the first 2*nbytes hex digits of hex (case-insensitive,...
N_STR * n_hex_encode(const unsigned char *data, size_t len)
encode len bytes of data as a lowercase hex N_STR (2*len characters); free with free_nstr.
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
A box including a string and his lenght.
Common headers and low-level functions & define.
Hexadecimal encode/decode helpers.
N_STR and string function declaration.