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