summaryrefslogtreecommitdiff
path: root/debug.c
blob: eff26548203c21d59507ff98f960aef6d17829ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* SPDX-License-Identifier: Unlicense */

#include "debug.h"

#include <stdint.h>

int g_log_level = LOG_LEVEL_INFO;

static char ByteToPrintableChar(uint32_t byte)
{
    return (byte >= 0x20 && byte <= 0x7f) ? byte : '.';
}

void FPrintRaw(FILE *s, void const *data_arg, size_t size)
{
    const size_t cols = 16;
    const size_t section_width = 8;
    if (size == 0) {
        return;
    }
    uint8_t const *data = data_arg;
    for (size_t line = 0; line < (size - 1) / cols + 1; line++) {
        fprintf(s, "%08zx: ", line * cols);
        for (size_t c = 0; c < cols; c++) {
            fprintf(s, "%02x ", data[line * cols + c]);
            if (c % section_width == section_width - 1 && c != cols - 1) {
                fprintf(s, " ");
            }
        }
        fprintf(s, " |");
        for (size_t c = 0; c < cols; c++) {
            fprintf(s, "%c", ByteToPrintableChar(data[line * cols + c]));
            if (c % section_width == section_width - 1 && c != cols - 1) {
                fprintf(s, " ");
            }
        }
        fprintf(s, "|\n");
    }
}