• Stars
    star
    159
  • Rank 235,916 (Top 5 %)
  • Language
    JavaScript
  • Created about 13 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Parsing binary files made easy

If you are interest in jParser then look at jBinary. This is a better version that supports both reads and write, extensions, templates ... yet preserves the declarative API of jParser.

jParser - Parsing binary files made easy.

jParser makes it easy to parse binary files in Javascript.

  • You write the structure once, it gets parsed automatically.
  • The parsing process can be extended with custom functions. It allows to parse non trivial files with ease.
  • It works both in the browser and NodeJS as it is powered by jDataView.

API

Primitive Structures:

  • Unsigned Int: uint8, uint16, uint32
  • Signed Int: int8, int16, int32
  • Float: float32, float64
  • String: char, string(len)
  • Array: array(type, len)
  • BitField: (bitCount)
  • Position: tell, skip(len), seek(pos), seek(pos, func)
  • Conditionals: if(predicate, type)

jParser Methods:

  • parse(value): Run the parsing, can be used recursively.
    • Number: Reads bitfield of given length in left-to-right mode and returns them as unsigned integer (so you can work with them using simple JavaScript binary operators). Please note that you can mix bitfields with primitive and complex types in one structure or even use them in own functions, but ALWAYS make sure that consecutive bitfields are padded to integer byte count (or 8*N bit count) before reading any other data types; most popular data formats already follow this rule but better to check out when writing own structures if you don't want to get unexpected behavior.
    • Function: Calls the function.
    • String: Dereferences the value in the structure.
    • Array: Function call, the function is the first element and arguments are the following.
    • Object: Returns an object with the same keys and parses the values.
  • tell(): Return the current position.
  • skip(count): Advance in the file by count bytes.
  • seek(position): Go to position.
  • seek(position, callback): Go to position, execute the callback and return to the previous position.
  • current: The current object being parsed. See it as a way to use what has been parsed just before.

jParser Constructor:

  • new jParser(data, structure)
    • data is a jDataView. You can give pretty much anything (String, ArrayBuffer, Node Buffer), it will be casted to jDataView automatically.
    • structure is an object with all the defined structures.

Examples

Basic C Structure You have the ability to define C-like structures. It's a Javascript object where keys are labels and values are types.

var parser = new jParser(file, {
  header: {
    fileId: 'int32',
    recordIndex: 'int32',
    hash: ['array', 'uint32', 4],
    fileName: ['string', 256],
    version: 2,
    flags: {
      precisionFlag: 1,
      marker: {
       part1: 2,
       part2: 2
      }
    },
    _reserved: 1 // padding to 8*N bits
  }
});
parser.parse('header');
// {
//   fileId: 42,
//   recordIndex: 6002,
//   hash: [4237894687, 3491173757, 3626834111, 2631772842],
//   fileName: ".\\Resources\\Excel\\Items_Weapons.xls",
//   version: 3,
//   flags: {
//     precisionFlag: 1,
//     marker: {
//       part1: 2,
//       part2: 0
//     }
//   },
//   _reserved: 0
// }

References Structures can reference other structures. Use structure name within a string in order to reference it. The following is an example from World of Warcraft model files.

nofs: {
  count: 'uint32',
  offset: 'uint32'
},
 
animationBlock: {
  interpolationType: 'uint16',
  globalSequenceID: 'int16',
  timestamps: 'nofs',
  keyFrame: 'nofs'
},
 
uvAnimation: {
  translation: 'animationBlock',
  rotation: 'animationBlock',
  scaling: 'animationBlock'
}

Helpers It is really easy to make new primitive types. You can either use existing constructions such as objects (float3) or arrays (float4). In case you want to do something more complicated, you always have the option to define a new function and use this.parse to keep parsing (hex32, string0).

float3: {
  x: 'float32',
  y: 'float32',
  z: 'float32'
},
float4: ['array', 'float32', 4],
hex32: function () {
  return '0x' + this.parse('uint32').toString(16);
},
string0: function (length) {
  return this.parse(['string', length]).replace(/\0+$/g, '');
}

Back Reference Instead of using an integer for the array size, you can put a function that will return an integer. In this function, you can use this.current to reference the englobing object being parsed.

image: {
  width: 'uint8',
  height: 'uint8',
  pixels: [
    'array',
    ['array', 'rgba', function () { return this.current.width; }],
    function () { return this.current.height; }
  ]
}

Advanced Parsing The best part of jParser is that complicated parsing logic can be expressed within the structure. It allows to parse complex files without having to split structure from parsing code.

entryHeader: {
  start: 'int32',
  count: 'int32'
},

entry: function (type) {
  var that = this;
  var header = this.parse('entryHeader');

  var res = [];
  this.seek(header.start, function () {
    for (var i = 0; i < header.count; ++i) {
      res.push(that.parse(type));
    }
  });
  return res;
},

name: {
 language: 'int32',
 text: ['string', 256]
},

file: {
  names: ['entry', 'name']
}

Get Started

NodeJS: Just use npm to install jParser and you are set :)

npm install jParser
var fs = require('fs');
var jParser = require('jParser');

fs.readFile('file.bin', function (err, data) {
  var parser = new jParser(data, {
    magic: ['array', 'uint8', 4]
  });
  console.log(parser.parse('magic'));
});

Browser: I've patched jQuery to allow to download binary files using the best binary format. You include this patched jQuery, jDataView and jParser and you are set :)

<script src="https://raw.github.com/vjeux/jDataView/master/jquery/jquery-1.7.1-binary-ajax.js"></script>
<script src="https://raw.github.com/vjeux/jDataView/master/src/jdataview.js"></script>
<script src="https://raw.github.com/vjeux/jParser/master/src/jparser.js"></script>

<script>
$.get('file.bin', function (data) {
  var parser = new jParser(data, {
    magic: ['array', 'uint8', 4]
  });
  console.log(parser.parse('magic'));
}, 'dataview');
</script>

Caveats

This tool works thanks to a feature that is not in the Javascript specification: When you iterate over an object keys, the keys will be listed in their order of insertion. Note that Chrome and Opera do not respect this implicit rule for keys that are numbers.

If you follow those two rules, the library will work in all the current Javascript implementations.

  • Do not start a key name with a digit
  • Do not put the same key twice in the same object

Demos

ICO Parser. This is a basic example to parse a binary file in NodeJS. It shows how to solve many common issues with binary file parsing.

Tar Extractor. This is a basic example to parse a binary file in the browser.

World of Warcraft Model Viewer. It uses jParser to read the binary model and then WebGL to display it.

Diablo 3 Internal Files.

More Repositories

1

mp4-h264-re-encode

Pure re-encoding of an mp4-h264 video file with the web APIs as well as in-depth description of how it works.
JavaScript
432
star
2

jsRayTracer

JavaScript
165
star
3

video-editor

Created with CodeSandbox
JavaScript
142
star
4

soulver.js

Rewrite of fantastic Soulver application
JavaScript
135
star
5

jspp

C++ shaped into Javascript
C++
99
star
6

GithubLogin

Login to Github API in the browser
58
star
7

markdown-react

React Render for Standard Markdown
JavaScript
52
star
8

jsWoWModelViewer

Display World of Warcraft Models (M2) in WebGL
JavaScript
50
star
9

react-xtags

Using React to implement xtags
JavaScript
49
star
10

react-cli

46
star
11

jsxdom

DOM backend for JSX
JavaScript
46
star
12

jsCollaborativePresentation

Experiment: Can we write a collaborative Javascript presentation?
JavaScript
30
star
13

image-recolor

Change the color of an image to a specific color you have in mind.
JavaScript
22
star
14

download-file-sync

Does exactly what you expect
JavaScript
19
star
15

stylelint-plugin-prettier

15
star
16

prism-react

Prism Highlighting using React
JavaScript
14
star
17

watcher

Recompile your Coffee/SASS/Haml/Less/... files as soon as they change
CoffeeScript
14
star
18

jailtime

Put your node code in jail and monitor all its communications
JavaScript
13
star
19

jsHTMLDiff

Diff between two HTML fragments
13
star
20

AsyncAwait

12
star
21

meter

JavaScript
11
star
22

jsSmallHash

Compress data into the smallest string
JavaScript
11
star
23

MysqliWrapper

Short and Secure Queries
9
star
24

TMSubtitles

AngelScript
8
star
25

prettier-browser

HTML
7
star
26

jsMatch

Wrapper around regexp to make it user friendly
5
star
27

Fooo

Warcraft 3 Remake
5
star
28

Gallery

Lightbox like the one on Facebook
JavaScript
4
star
29

phpFileDownload

file_get_contents handling cookies and posts
PHP
4
star
30

GameTheory

CoffeeScript
4
star
31

phpMysqliWrapper

Short and secure queries
PHP
4
star
32

meta-oss-markdown

All the markdown files from the top 150 Meta Open Source repositories
3
star
33

pyStormLib

Python StormLib Wrapper
Python
3
star
34

sc2patches

All the Starcraft 2 Patches Diff
HLSL
3
star
35

insert-require

CoffeeScript
3
star
36

XSON

Smallest JSON equivalent in XML
3
star
37

FacebookDiffs

JavaScript
2
star
38

phpMatch

Wrapper around preg_match to make it user friendly
PHP
2
star
39

viking-chess

Created with CodeSandbox
JavaScript
1
star
40

spring01-minecraft

Trackmania Spring A01 but in Minecraft
1
star
41

weekly-challenge-1-stockfish-chess

1
star
42

jsPredicate

Design by Contract - Pre and Post conditions
JavaScript
1
star
43

atom-insert-require

CoffeeScript
1
star
44

react-server-rendering

React server rendering demo
JavaScript
1
star
45

ImpJs

Image Processing Library in Javascript
CoffeeScript
1
star
46

trackmania-totd-medal-distribution

Trackmania Track of the Day Medal Distribution
1
star
47

jsFullDispatch

Advanced function overloading, or multi-methods. It allows to dispatch through custom functions.
JavaScript
1
star