Add highlighting, fixed len instructions and more

This commit is contained in:
2025-02-09 21:15:50 +01:00
parent 45653b6372
commit e133c2df3a
17 changed files with 753 additions and 338 deletions

View File

@@ -6,44 +6,39 @@
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <stdint.h>
#include <string.h>
#define BYTES_PER_LINE 16 // Adjust for different widths
char *hexdump_to_string(const unsigned char *data, size_t size) {
char *hexdump_to_string(const unsigned char *data, uint32_t start, uint32_t end) {
if (start >= end) return NULL; // Invalid range
// Estimate max output size: each line is approx. 80 chars
size_t estimated_size = (size / BYTES_PER_LINE + 1) * 80;
uint32_t estimated_size = ((end - start) / BYTES_PER_LINE + 1) * 60;
// Allocate memory for output string
char *output = malloc(estimated_size);
if (!output) return NULL;
size_t offset = 0; // Track the write position
uint32_t out_offset = 0; // Tracks position in output buffer
for (size_t i = 0; i < size; i += BYTES_PER_LINE) {
offset += snprintf(output + offset, estimated_size - offset, "%08zx ", i);
for (uint32_t i = start; i < end; i += BYTES_PER_LINE) {
// Print offset
out_offset += snprintf(output + out_offset, estimated_size - out_offset, "%07x ", i);
// Print hex values
for (size_t j = 0; j < BYTES_PER_LINE; j++) {
if (i + j < size)
offset += snprintf(output + offset, estimated_size - offset, "%02x ", data[i + j]);
for (uint32_t j = 0; j < BYTES_PER_LINE; j++) {
if (i + j < end)
out_offset += snprintf(output + out_offset, estimated_size - out_offset, "%02x ", data[i + j]);
else
offset += snprintf(output + offset, estimated_size - offset, " "); // Padding
if (j == 7) offset += snprintf(output + offset, estimated_size - offset, " "); // Extra space
out_offset += snprintf(output + out_offset, estimated_size - out_offset, " "); // Padding
if (j == 7)
out_offset += snprintf(output + out_offset, estimated_size - out_offset, " "); // Extra space
}
offset += snprintf(output + offset, estimated_size - offset, " |");
// Print ASCII representation
for (size_t j = 0; j < BYTES_PER_LINE; j++) {
if (i + j < size)
offset += snprintf(output + offset, estimated_size - offset, "%c",
isprint(data[i + j]) ? data[i + j] : '.');
else
offset += snprintf(output + offset, estimated_size - offset, " ");
}
offset += snprintf(output + offset, estimated_size - offset, "|\n");
out_offset += snprintf(output + out_offset, estimated_size - out_offset, "\n");
}
return output;