40 while ((opt = getopt(argc, argv,
"hvV:")) != EOF) {
43 if (!strncmp(
"LOG_NULL", optarg, 8))
45 else if (!strncmp(
"LOG_NOTICE", optarg, 10))
47 else if (!strncmp(
"LOG_INFO", optarg, 8))
49 else if (!strncmp(
"LOG_ERR", optarg, 7))
51 else if (!strncmp(
"LOG_DEBUG", optarg, 9))
54 fprintf(stderr,
"Unknown log level %s\n", optarg);
59 fprintf(stderr,
"ex_hex\n");
63 fprintf(stderr,
"usage: %s [-V LOG_LEVEL]\n", argv[0]);
69static void expect_encode(
const unsigned char* data,
size_t len,
const char* want) {
72 n_log(
LOG_ERR,
"n_hex_encode returned NULL for expected '%s'", want);
76 if (out->
written != strlen(want) || strcmp(out->
data, want) != 0) {
84 const unsigned char a[] = {0x00, 0x01, 0xff, 0xab};
87 const unsigned char b[] = {0xDE, 0xAD, 0xBE, 0xEF};
100 n_log(
LOG_ERR,
"decode '0001ffab': got len %zu expected 4", n);
102 }
else if (buf[0] != 0x00 || buf[1] != 0x01 || buf[2] != 0xff || buf[3] != 0xab) {
111 if (!buf || n != 4 || buf[0] != 0xde || buf[1] != 0xad || buf[2] != 0xbe || buf[3] != 0xef) {
112 n_log(
LOG_ERR,
"decode with whitespace/upper failed (len %zu)", n);
118 const unsigned char payload[] = {0x00, 0x41, 0x00, 0x42};
120 if (!enc || strcmp(enc->
data,
"00410042") != 0) {
126 if (!dec || n !=
sizeof(payload) || memcmp(dec, payload,
sizeof(payload)) != 0) {
127 n_log(
LOG_ERR,
"round-trip with embedded NUL failed (len %zu)", n);
153 if (!buf || n != 0) {
154 n_log(
LOG_ERR,
"decode empty should yield len 0 buffer (len %zu)", n);
165int main(
int argc,
char** argv) {
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_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,...
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.