infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
utility.c
Go to the documentation of this file.
1
28// This file is only compiled if debugging is enabled.
29#if defined(INFIX_DEBUG_ENABLED) && INFIX_DEBUG_ENABLED
30// Use the double-tap test harness's `note` macro for debug printing if available.
31// This integrates the debug output seamlessly into the TAP test logs.
32#if defined(DBLTAP_ENABLE) && defined(DBLTAP_IMPLEMENTATION)
33#include "common/double_tap.h"
34#else
35// If not building as part of a test, fall back to a standard printf implementation.
36#include <stdio.h>
37#ifndef note
38#define note(...) \
39 do { \
40 printf("# " __VA_ARGS__); \
41 printf("\n"); \
42 } while (0)
43#endif
44#endif // DBLTAP_ENABLE
45#include "common/utility.h"
46#include <inttypes.h>
62void infix_dump_hex(const void * data, size_t size, const char * title) {
63 const uint8_t * byte = (const uint8_t *)data;
64 char line_buf[256];
65 char * buf_ptr;
66 size_t remaining_len;
67 int written;
68 note("%s (size: %llu bytes at %p)", title, (unsigned long long)size, data);
69 for (size_t i = 0; i < size; i += 16) {
70 buf_ptr = line_buf;
71 remaining_len = sizeof(line_buf);
72 // Print the address offset for the current line.
73 written = snprintf(buf_ptr, remaining_len, "0x%04llx: ", (unsigned long long)i);
74 if (written < 0 || (size_t)written >= remaining_len)
75 goto print_line;
76 buf_ptr += written;
77 remaining_len -= written;
78 // Print the hexadecimal representation of the bytes.
79 for (size_t j = 0; j < 16; ++j) {
80 if (i + j < size)
81 written = snprintf(buf_ptr, remaining_len, "%02x ", byte[i + j]);
82 else
83 written = snprintf(buf_ptr, remaining_len, " "); // Pad if at the end of the data.
84 if (written < 0 || (size_t)written >= remaining_len)
85 goto print_line;
86 buf_ptr += written;
87 remaining_len -= written;
88 if (j == 7) { // Add an extra space in the middle for readability.
89 written = snprintf(buf_ptr, remaining_len, " ");
90 if (written < 0 || (size_t)written >= remaining_len)
91 goto print_line;
92 buf_ptr += written;
93 remaining_len -= written;
94 }
95 }
96 written = snprintf(buf_ptr, remaining_len, "| ");
97 if (written < 0 || (size_t)written >= remaining_len)
98 goto print_line;
99 buf_ptr += written;
100 remaining_len -= written;
101 // Print the ASCII representation of the bytes.
102 for (size_t j = 0; j < 16; ++j) {
103 if (i + j < size) {
104 if (byte[i + j] >= 32 && byte[i + j] <= 126) // Printable ASCII characters
105 written = snprintf(buf_ptr, remaining_len, "%c", byte[i + j]);
106 else
107 written = snprintf(buf_ptr, remaining_len, "."); // Non-printable characters
108 if (written < 0 || (size_t)written >= remaining_len)
109 goto print_line;
110 buf_ptr += written;
111 remaining_len -= written;
112 }
113 }
114print_line:
115 note(" %s", line_buf);
116 }
117 note("End of %s", title);
118}
119#endif // INFIX_DEBUG_ENABLED
A lightweight, single-header TAP (Test Anything Protocol) library.
#define note(...)
Definition double_tap.h:172
A header for conditionally compiled debugging utilities.
static void infix_dump_hex(c23_maybe_unused const void *data, c23_maybe_unused size_t size, c23_maybe_unused const char *title)
Definition utility.h:115