Impulse Graph C++ Engine 2.4.0
High-Performance Zero-Copy C-ABI Kernel and C++20 Fluent VM Engine
Loading...
Searching...
No Matches
impulse_hexio.h
Go to the documentation of this file.
1
10
11#ifndef IMPULSE_HEXIO_H
12#define IMPULSE_HEXIO_H
13
14#include <stdint.h>
15#include <stddef.h>
16#include <stdio.h>
17#include <string.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23static inline int impulse_hexio_nibble(char c) {
24 if (c >= '0' && c <= '9') return c - '0';
25 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
26 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
27 return -1;
28}
29
35static inline int impulse_hexio_read_buf(const uint8_t* buf, size_t buf_len,
36 size_t offset, size_t count,
37 char* out_hex, size_t out_hex_cap) {
38 static const char kHexDigits[] = "0123456789ABCDEF";
39 if (!buf || !out_hex || offset > buf_len || count > buf_len - offset ||
40 count > (SIZE_MAX - 1) / 2 || out_hex_cap < count * 2 + 1) {
41 return -1;
42 }
43 for (size_t i = 0; i < count; ++i) {
44 uint8_t b = buf[offset + i];
45 out_hex[i * 2] = kHexDigits[(b >> 4) & 0x0F];
46 out_hex[i * 2 + 1] = kHexDigits[b & 0x0F];
47 }
48 out_hex[count * 2] = '\0';
49 return (int)count;
50}
51
57static inline int impulse_hexio_write_buf(uint8_t* buf, size_t buf_len,
58 size_t offset, const char* hex_str) {
59 if (!buf || !hex_str || offset > buf_len) return -1;
60 size_t written = 0;
61 const char* p = hex_str;
62 while (*p) {
63 while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') ++p;
64 if (*p == '\0') break;
65 int hi = impulse_hexio_nibble(p[0]);
66 int lo = (p[0] != '\0') ? impulse_hexio_nibble(p[1]) : -1;
67 if (hi < 0 || lo < 0) return -1;
68 if (offset + written >= buf_len) return -1;
69 buf[offset + written] = (uint8_t)((hi << 4) | lo);
70 ++written;
71 p += 2;
72 }
73 return (int)written;
74}
75
79static inline int impulse_hexio_read_file(const char* path, size_t offset, size_t count,
80 char* out_hex, size_t out_hex_cap) {
81 if (!path || !out_hex || out_hex_cap < count * 2 + 1) return -1;
82 FILE* f = fopen(path, "rb");
83 if (!f) return -1;
84 if (fseek(f, (long)offset, SEEK_SET) != 0) {
85 fclose(f);
86 return -1;
87 }
88 static const char kHexDigits[] = "0123456789ABCDEF";
89 for (size_t i = 0; i < count; ++i) {
90 int c = fgetc(f);
91 if (c == EOF) {
92 fclose(f);
93 return -1;
94 }
95 uint8_t b = (uint8_t)c;
96 out_hex[i * 2] = kHexDigits[(b >> 4) & 0x0F];
97 out_hex[i * 2 + 1] = kHexDigits[b & 0x0F];
98 }
99 out_hex[count * 2] = '\0';
100 fclose(f);
101 return (int)count;
102}
103
107static inline int impulse_hexio_write_file(const char* path, size_t offset, const char* hex_str) {
108 if (!path || !hex_str) return -1;
109 FILE* f = fopen(path, "r+b");
110 if (!f) return -1;
111 if (fseek(f, (long)offset, SEEK_SET) != 0) {
112 fclose(f);
113 return -1;
114 }
115 size_t written = 0;
116 const char* p = hex_str;
117 while (*p) {
118 while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') ++p;
119 if (*p == '\0') break;
120 int hi = impulse_hexio_nibble(p[0]);
121 int lo = (p[0] != '\0') ? impulse_hexio_nibble(p[1]) : -1;
122 if (hi < 0 || lo < 0) {
123 fclose(f);
124 return -1;
125 }
126 uint8_t b = (uint8_t)((hi << 4) | lo);
127 if (fputc(b, f) == EOF) {
128 fclose(f);
129 return -1;
130 }
131 ++written;
132 p += 2;
133 }
134 fflush(f);
135 fclose(f);
136 return (int)written;
137}
138
140static inline void impulse_hexio_render_u16_le(uint16_t val, char out_hex[5]) {
141 uint8_t bytes[2];
142 bytes[0] = (uint8_t)(val & 0xFFu);
143 bytes[1] = (uint8_t)((val >> 8) & 0xFFu);
144 impulse_hexio_read_buf(bytes, 2, 0, 2, out_hex, 5);
145}
146
148static inline void impulse_hexio_render_u32_le(uint32_t val, char out_hex[9]) {
149 uint8_t bytes[4];
150 for (int i = 0; i < 4; ++i) bytes[i] = (uint8_t)((val >> (i * 8)) & 0xFFu);
151 impulse_hexio_read_buf(bytes, 4, 0, 4, out_hex, 9);
152}
153
155static inline void impulse_hexio_render_u64_le(uint64_t val, char out_hex[17]) {
156 uint8_t bytes[8];
157 for (int i = 0; i < 8; ++i) bytes[i] = (uint8_t)((val >> (i * 8)) & 0xFFu);
158 impulse_hexio_read_buf(bytes, 8, 0, 8, out_hex, 17);
159}
160
161#ifdef __cplusplus
162}
163#endif
164
165#endif /* IMPULSE_HEXIO_H */
static int impulse_hexio_nibble(char c)
Definition impulse_hexio.h:23
static int impulse_hexio_read_buf(const uint8_t *buf, size_t buf_len, size_t offset, size_t count, char *out_hex, size_t out_hex_cap)
Definition impulse_hexio.h:35
static void impulse_hexio_render_u32_le(uint32_t val, char out_hex[9])
Definition impulse_hexio.h:148
static void impulse_hexio_render_u16_le(uint16_t val, char out_hex[5])
Definition impulse_hexio.h:140
static int impulse_hexio_write_file(const char *path, size_t offset, const char *hex_str)
Definition impulse_hexio.h:107
static void impulse_hexio_render_u64_le(uint64_t val, char out_hex[17])
Definition impulse_hexio.h:155
static int impulse_hexio_write_buf(uint8_t *buf, size_t buf_len, size_t offset, const char *hex_str)
Definition impulse_hexio.h:57
static int impulse_hexio_read_file(const char *path, size_t offset, size_t count, char *out_hex, size_t out_hex_cap)
Definition impulse_hexio.h:79