|
| 1 | +// Hexdump based on |
| 2 | +// https://gist.github.com/KaneRoot/caa34cba0a317fb6f96d3a4f93b1e228 |
| 3 | + |
| 4 | +/// Import the Zig Standard Library |
| 5 | +const std = @import("std"); |
| 6 | + |
| 7 | +/// Import the WebAssembly Logger |
| 8 | +const wasmlog = @import("wasmlog.zig"); |
| 9 | + |
| 10 | +/// Dump the buffer in hex |
| 11 | +pub fn hexdump(buffer: [*:0]const u8, len: usize) void { |
| 12 | + var hexb: u32 = 0; |
| 13 | + var ascii: [16]u8 = undefined; |
| 14 | + // First line, first left side (simple number). |
| 15 | + log(" {d:0>4}: ", .{hexb}); |
| 16 | + |
| 17 | + // Loop on all values in the buffer (i from 0 to len). |
| 18 | + var i: u32 = 0; |
| 19 | + while (i < len) : (i += 1) { |
| 20 | + // Print actual hexadecimal value. |
| 21 | + log("{X:0>2} ", .{buffer[i]}); |
| 22 | + |
| 23 | + // What to print (simple ascii text, right side). |
| 24 | + if (buffer[i] >= ' ' and buffer[i] <= '~') { |
| 25 | + ascii[(i % 16)] = buffer[i]; |
| 26 | + } else { |
| 27 | + ascii[(i % 16)] = '.'; |
| 28 | + } |
| 29 | + |
| 30 | + // Next input is a multiple of 8 = extra space. |
| 31 | + if ((i + 1) % 8 == 0) { |
| 32 | + log(" ", .{}); |
| 33 | + } |
| 34 | + |
| 35 | + // No next input: print the right amount of spaces. |
| 36 | + if ((i + 1) == len) { |
| 37 | + // Each line is 16 bytes to print, each byte takes 3 characters. |
| 38 | + var missing_spaces = 3 * (15 - (i % 16)); |
| 39 | + // Missing an extra space if the current index % 16 is less than 7. |
| 40 | + if ((i % 16) < 7) { |
| 41 | + missing_spaces += 1; |
| 42 | + } |
| 43 | + while (missing_spaces > 0) : (missing_spaces -= 1) { |
| 44 | + log(" ", .{}); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Every 16 bytes: print ascii text and line return. |
| 49 | + |
| 50 | + // Case 1: it's been 16 bytes AND it's the last byte to print. |
| 51 | + if ((i + 1) % 16 == 0 and (i + 1) == len) { |
| 52 | + log("{s}\n", .{ascii[0..ascii.len]}); |
| 53 | + } |
| 54 | + // Case 2: it's been 16 bytes but it's not the end of the buffer. |
| 55 | + else if ((i + 1) % 16 == 0 and (i + 1) != len) { |
| 56 | + log("{s}\n", .{ascii[0..ascii.len]}); |
| 57 | + hexb += 16; |
| 58 | + log(" {d:0>4}: ", .{hexb}); |
| 59 | + } |
| 60 | + // Case 3: not the end of the 16 bytes row but it's the end of the buffer. |
| 61 | + else if ((i + 1) % 16 != 0 and (i + 1) == len) { |
| 62 | + log(" {s}\n", .{ascii[0..((i + 1) % 16)]}); |
| 63 | + } |
| 64 | + // Case 4: not the end of the 16 bytes row and not the end of the buffer. |
| 65 | + // Do nothing. |
| 66 | + } |
| 67 | + |
| 68 | + log("\n", .{}); |
| 69 | +} |
| 70 | + |
| 71 | +/// Print to the WebAssembly Logger |
| 72 | +fn log( |
| 73 | + comptime format: []const u8, |
| 74 | + args: anytype, |
| 75 | +) void { |
| 76 | + // Format the message |
| 77 | + const slice = std.fmt.bufPrint(buf[buflen..], format, args) catch { |
| 78 | + wasmlog.Console.log("*** log error: buf too small", .{}); |
| 79 | + return; |
| 80 | + }; |
| 81 | + buflen += slice.len; |
| 82 | + |
| 83 | + // Print the formatted message |
| 84 | + if (std.mem.containsAtLeast(u8, slice, 1, "\n")) { |
| 85 | + wasmlog.Console.log("{s}", .{buf[0 .. buflen - 1]}); |
| 86 | + buflen = 0; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Log Buffer |
| 91 | +var buf: [256]u8 = [_]u8{0} ** 256; |
| 92 | +var buflen: usize = 0; |
0 commit comments