feature (utils): Implemented hexdump functionality

This commit is contained in:
antifallobst 2023-03-02 16:51:20 +01:00
parent e3c7eb271d
commit 2a2d5b0ab6
4 changed files with 85 additions and 0 deletions

View File

@ -32,5 +32,6 @@ bool memory_compare (void* a, void* b, uint32_t num);
void* memory_allocate (uint64_t size);
void memory_free (void* address);
void memory_allocator_init (void* base);
void memory_hexdump (uint8_t* address, uint64_t num);
#endif //NOX_MEMORY_H

View File

@ -48,4 +48,6 @@ void string_hex_64bit_to_alpha (string_t string, uint64_t value);
void string_bin_to_alpha (string_t string, uint8_t num_bits, uint64_t value);
void string_bool_to_alpha (string_t string, bool value);
bool string_is_char_alpha (char chr);
#endif //NOX_STRING_H

View File

@ -22,6 +22,7 @@
*/
#include "utils/memory.h"
#include "utils/logger.h"
#include "mm/heap.h"
heap_T g_kernel_heap;
@ -99,4 +100,78 @@ void memory_allocator_init(void* base) {
if (g_kernel_heap.start != NULL) { return; }
heap_init(&g_kernel_heap, base);
}
void memory_hexdump(uint8_t* address, uint64_t num) {
uint64_t rows = num / 8;
uint8_t remainder = num % 8;
for (uint64_t i = 0; i < rows; i++) {
char buffer[10] = ".... ....";
for (uint8_t j = 0; j < 8; j++) {
if (string_is_char_alpha((char)address[j])) {
buffer[(j < 4) ? j : (j + 1)] = (char)address[j];
}
}
log(LOG_NONE, "<Hexdump> 0x%x -> %xb %xb %xb %xb %xb %xb %xb %xb [%s]",
address, address[0], address[1], address[2], address[3], address[4], address[5], address[6], address[7], buffer);
address += 8;
}
char buffer[10] = " ";
for (uint8_t j = 0; j < remainder; j++) {
if (string_is_char_alpha((char)address[j])) {
buffer[(j < 4) ? j : (j + 1)] = (char)address[j];
} else {
buffer[(j < 4) ? j : (j + 1)] = '.';
}
}
// this is not a beautiful solution, but it should be easy to understand / maintain
switch (remainder) {
case 0: {
break;
}
case 1: {
log(LOG_NONE, "<Hexdump> 0x%x -> %xb [%s]",
address, address[0], buffer);
break;
}
case 2: {
log(LOG_NONE, "<Hexdump> 0x%x -> %xb %xb [%s]",
address, address[0], address[1], buffer);
break;
}
case 3: {
log(LOG_NONE, "<Hexdump> 0x%x -> %xb %xb %xb [%s]",
address, address[0], address[1], address[2], buffer);
break;
}
case 4: {
log(LOG_NONE, "<Hexdump> 0x%x -> %xb %xb %xb %xb [%s]",
address, address[0], address[1], address[2], address[3], buffer);
break;
}
case 5: {
log(LOG_NONE, "<Hexdump> 0x%x -> %xb %xb %xb %xb %xb [%s]",
address, address[0], address[1], address[2], address[3], address[4], buffer);
break;
}
case 6: {
log(LOG_NONE, "<Hexdump> 0x%x -> %xb %xb %xb %xb %xb %xb [%s]",
address, address[0], address[1], address[2], address[3], address[4], address[5], buffer);
break;
}
case 7: {
log(LOG_NONE, "<Hexdump> 0x%x -> %xb %xb %xb %xb %xb %xb %xb [%s]",
address, address[0], address[1], address[2], address[3], address[4], address[5], address[6], buffer);
break;
}
default: {
break;
}
}
}

View File

@ -421,4 +421,11 @@ void string_bin_to_alpha(string_t string, uint8_t num_bits, uint64_t value) {
void string_bool_to_alpha(string_t string, bool value) {
memory_copy(value ? "true" : "false", (void*)string, value ? 5 : 6);
}
bool string_is_char_alpha(char chr) {
if (chr >= '!' && chr <= '~') {
return true;
}
return false;
}