Binary Viewer
A NodeJS module to pretty view binary content.
Requires NodeJS 10.3+.
Example usage:
const FormatStringParser BinaryContentFormatter TextPrinter = ; // A format string describing the binary data.const formatString = ` Hello { uint8 x; uint8 y; // format strings can have comments}; Vertex { Hello a; Hello b;}; /* Multline comments are supported too. Yay.*/ Main { uint32 count; uint8 values[count]; int32 foo; Vertex data[2]; float myfloat;}; root = Main; `; // Parse the format string into an AST. Syntactic validation is performed here.const formatStringAst = formatString; // Create a binary blob. In a real application this might e.g. be obtained from an API.const ab = 64;const dataView = ab;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView;dataView; // Interpret the binary blob using the format AST we parsed earlier. Throws on error.const binaryContentAst = formatStringAst dataView; // Return the interpreted binary blob as a nicely indented text string.const textContent = binaryContentAst spacer: ' ' assignment: ': '; /*`textContent` is a string with the following content: Main { uint32 count: 8; uint8 values[8]: [ 10, 20, 30, 40, 50, 60, 70, 80 ]; int32 foo: 2; Vertex data[2]: [ Vertex { Hello a: { uint8 x: 5; uint8 y: 15; }; Hello b: { uint8 x: 25; uint8 y: 35; }; }; Vertex { Hello a: { uint8 x: 6; uint8 y: 16; }; Hello b: { uint8 x: 26; uint8 y: 36; }; }; ]; float myfloat: 7294.33;}; */
The src/test/examples/examples.test.ts
file contains more examples.
Building
If the pegjs grammar file (src/main/parser.pegjs
) hasn't been changed, run the following:
$ npm run build
If the pegjs grammar file has been changed, run the following:
$ npm run pegjs
$ npm run build
Tests
Run the unit tests as follows:
$ npm run test