tadpole/engine/pkg/xxd/README.md

2.4 KiB

xxd utility

This has been a really nice helper function when helping with doing any kind of serialization work.

I've been finding that generally I've been using debug print statements in tests with --watch more than i've actually been using a real debugger.

And having a tool to just inspect the bytes at any moment has been absolutely amazing.

these 300 or so lines of code have helped me fix a lot of bugs.

zig fetch --save git+ssh://git@codeberg.org/peterino2/xxd.git

Usage - debug printing:

const xxd = @import("xxd");

test "hello xxd" {
    const teststring: []const u8 = "this is a test string";
    // second argument is the offset address, so you
    // can view multiple chunks toegether if you were to loop this.
    xxd.print(teststring, 0); 
} 

output:

this is the top 0xa0 as a result of running xxd on the build.zig file

xxd.print is a debug function like std.debug.print and outputs to stderr.

          0001 0203 0405 0607  0809 0a0b 0c0d 0e0f
00000000: 636F 6E73 7420 7374  6420 3D20 4069 6D70 |const std = @imp|
00000010: 6F72 7428 2273 7464  2229 3B0A 0A70 7562 |ort("std");..pub|
00000020: 2066 6E20 6275 696C  6428 623A 202A 7374 | fn build(b: *st|
00000030: 642E 4275 696C 6429  2076 6F69 6420 7B0A |d.Build) void {.|
00000040: 2020 2020 636F 6E73  7420 7461 7267 6574 |    const target|
00000050: 203D 2062 2E73 7461  6E64 6172 6454 6172 | = b.standardTar|
00000060: 6765 744F 7074 696F  6E73 282E 7B7D 293B |getOptions(.{});|
00000070: 0A20 2020 2063 6F6E  7374 206F 7074 696D |.    const optim|
00000080: 697A 6520 3D20 622E  7374 616E 6461 7264 |ize = b.standard|
00000090: 4F70 7469 6D69 7A65  4F70 7469 6F6E 282E |OptimizeOption(.|
000000a0: 7B7D 293B 0A0A 2020  2020 636F 6E73 7420 |{});..    const |

Usage - writers interface

Supports colors and a multitude of options if you use the writer

    pub const XxdOptions = struct {
        wordSize: u32 = 8, // value is in bits... but maybe its better if it's in bytes?
        endian: Endian = .little, // .big will reverse each word
        wordsPerGroup: u32 = 2,
        showHeader: bool = true,
        groupsPerLine: u32 = 8,
        groupSplit: u32 = 4, // extra space every N groups; 0 disables it
        showAddress: bool = true,
        baseAddress: u64 = 0,
        showAscii: bool = false,
        color: bool = true, // if true uses an ascii color code
    };

    try xxd.xxdWrite(&myWriter, bytes, options);