Skip to content

Commit 6aeb70b

Browse files
committed
Merged with keichi/binary-parser master branch
2 parents 63c718a + 3da455f commit 6aeb70b

19 files changed

+2930
-2579
lines changed

.circleci/config.yml

Lines changed: 0 additions & 70 deletions
This file was deleted.

.github/workflows/nodejs.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: build
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [10.x, 12.x, 14.x]
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Use Node.js ${{ matrix.node-version }}
24+
uses: actions/setup-node@v1
25+
with:
26+
node-version: ${{ matrix.node-version }}
27+
- run: npm ci
28+
- run: npm run build --if-present
29+
- run: npm test
30+
env:
31+
CI: true

.github/workflows/npmpublish.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3+
4+
name: publish
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v1
16+
with:
17+
node-version: 12
18+
- run: npm ci
19+
- run: npm test
20+
21+
publish-npm:
22+
needs: build
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v2
26+
- uses: actions/setup-node@v1
27+
with:
28+
node-version: 12
29+
registry-url: https://registry.npmjs.org/
30+
- run: npm ci
31+
- run: npm publish
32+
env:
33+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

README.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ library. It is currently being proposed as a Pull-Request in that project.
66
Until the *encoding* feature is merged in baseline of original project,
77
this branch is published under the name: **binary-parser-encoder** in [npm](https://npmjs.org/).
88

9+
[![build](https://github.com/Ericbla/binary-parser/workflows/build/badge.svg)](https://github.com/Ericbla/binary-parser/actions?query=workflow%3Abuild)
10+
[![npm](https://img.shields.io/npm/v/binary-parser-encoder)](https://www.npmjs.com/package/binary-parser-encoder)
911

10-
[![Circle CI](https://circleci.com/gh/Ericbla/binary-parser.svg?style=svg)](https://circleci.com/gh/Ericbla/binary-parser)
11-
12-
Binary-parser is a binary parser/encoder builder for [node](http://nodejs.org) that
13-
enables you to write efficient parsers/encoders in a simple and declarative manner.
12+
Binary-parser is a parser/encoder builder for JavaScript that enables you to write
13+
efficient binary parsers/encoders in a simple and declarative manner.
1414

1515
It supports all common data types required to analyze a structured binary
1616
data. Binary-parser dynamically generates and compiles the parser and encoder code
@@ -35,12 +35,14 @@ Binary-parser was inspired by [BinData](https://github.com/dmendel/bindata)
3535
and [binary](https://github.com/substack/node-binary).
3636

3737
## Quick Start
38+
3839
1. Create an empty Parser object with `new Parser()` or `Parser.start()`.
3940
2. Chain methods to build your desired parser and/or encoder. (See
4041
[API](https://github.com/keichi/binary-parser#api) for detailed document of
4142
each method)
42-
3. Call `Parser.prototype.parse` with an `Buffer` object passed as an argument.
43-
4. Parsed result will be returned as an object.
43+
3. Call `Parser.prototype.parse` with a `Buffer`/`Uint8Array` object passed as
44+
an argument.
45+
4. The parsed result will be returned as an object.
4446
5. Or call `Parser.prototype.encode` with an object passed as argument.
4547
6. Encoded result will be returned as a `Buffer` object.
4648

@@ -97,15 +99,15 @@ console.log(ipHeader.encode(anIpHeader).toString("hex"));
9799
## API
98100

99101
### new Parser([options])
100-
Constructs a Parser object. Returned object represents a parser which parses
101-
nothing. `options` is an optional object to pass options to this declarative
102+
Create an empty parser object that parses nothing.
103+
`options` is an optional object to pass options to this declarative
102104
parser.
103105
- `smartBufferSize` The chunk size of the encoding (smart)buffer (when encoding is used) (default is 256 bytes).
104106

105107
### parse(buffer)
106-
Parse a `Buffer` object `buffer` with this parser and return the resulting
107-
object. When `parse(buffer)` is called for the first time, the associated
108-
parser code is compiled on-the-fly and internally cached.
108+
Parse a `Buffer`/`Uint8Array` object `buffer` with this parser and return the
109+
resulting object. When `parse(buffer)` is called for the first time, the
110+
associated parser code is compiled on-the-fly and internally cached.
109111

110112
### encode(obj)
111113
Encode an `Object` object `obj` with this parser and return the resulting
@@ -163,10 +165,9 @@ characters and start with an alphabet. `options` is an object which can have
163165
the following keys:
164166

165167
- `encoding` - (Optional, defaults to `utf8`) Specify which encoding to use.
166-
Supported encodings include `"utf8"`, `"ascii"` and `"hex"`. See
167-
[`Buffer.toString`](http://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end)
168-
for more info.
169-
- `length` - (Optional) (Bytes)Length of the string. Can be a number, string or a
168+
Supported encodings include `"hex"` and all encodings supported by
169+
[`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/encoding).
170+
- `length ` - (Optional) Length of the string. Can be a number, string or a
170171
function. Use number for statically sized arrays, string to reference
171172
another variable and function to do some calculation.
172173
Note: When encoding the string is padded with a `padd` charecter to fit the length requirement.
@@ -188,9 +189,10 @@ the following keys:
188189
or utf8 < 0x80 are alowed. Note: The default padd character is *space* (or *null* when `stripNull` is used).
189190

190191
### buffer(name[, options])
191-
Parse bytes as a buffer. `name` should consist only of alpha numeric
192-
characters and start with an alphabet. `options` is an object which can have
193-
the following keys:
192+
Parse bytes as a buffer. Its type will be the same as the input to
193+
`parse(buffer)`. `name` should consist only of alpha numeric characters and
194+
start with an alphabet. `options` is an object which can have the following
195+
keys:
194196

195197
- `clone` - (Optional, defaults to `false`) By default,
196198
`buffer(name [,options])` returns a new buffer which references the same
@@ -202,9 +204,9 @@ the following keys:
202204
sized buffers, string to reference another variable and function to do some
203205
calculation.
204206
- `readUntil` - (either `length` or `readUntil` is required) If `"eof"`, then
205-
this parser will read till it reaches end of the `Buffer` object. (Note: has no
206-
effect on encoding.)
207-
If it is a function, this parser will read the buffer is read until the
207+
this parser will read till it reaches the end of the `Buffer`/`Uint8Array`
208+
object. (Note: has no effect on encoding.)
209+
If it is a function, this parser will read the buffer until the
208210
function returns true.
209211

210212
### array(name, options)
@@ -221,8 +223,9 @@ keys:
221223
required) Length of the array expressed in bytes. Can be a number, string or
222224
a function. Use number for statically sized arrays.
223225
- `readUntil` - (either `length`, `lengthInBytes`, or `readUntil` is required)
224-
If `"eof"`, then this parser reads until the end of `Buffer` object. If
225-
function it reads until the function returns true. **<u>Note</u>**: When encoding,
226+
If `"eof"`, then this parser reads until the end of the `Buffer`/`Uint8Array`
227+
object. If function it reads until the function returns true.
228+
**<u>Note</u>**: When encoding,
226229
the `buffer` second parameter of `readUntil` function is the buffer already encoded
227230
before this array. So no *read-ahead* is possible.
228231
- `encodeUntil` - a function (item, object), only used when encoding, that replaces
@@ -364,8 +367,8 @@ var parser = new Parser()
364367

365368
### seek(relOffset)
366369
Move the buffer offset for `relOffset` bytes from the current position. Use a
367-
negative `relOffset` value to rewind the offset. Previously named `skip(length)`.
368-
(Note: when encoding, the skipped bytes will be filled with zeros)
370+
negative `relOffset` value to rewind the offset. This method was previously
371+
named `skip(length)`. (Note: when encoding, the skipped bytes will be filled with zeros)
369372

370373
### endianess(endianess)
371374
Define what endianess to use in this parser. `endianess` can be either
@@ -382,7 +385,7 @@ var parser = new Parser()
382385
.int32("c");
383386
```
384387

385-
### setEncoderOptions(opts)
388+
### encoderSetOptions(opts)
386389
Set specific options for encoding.
387390
Current supported `opts` object may contain:
388391
- bitEndianess: true|false (default false) When true, tell the encoder to respect endianess BITs order, so that
@@ -391,7 +394,7 @@ Current supported `opts` object may contain:
391394
```javascript
392395
var parser = new Parser()
393396
.endianess("little")
394-
.setEncoderOpts({bitEndianess: true}) // Use BITs endianess for bits fields
397+
.encoderSetOptions({bitEndianess: true}) // Use BITs endianess for bits fields
395398
.bit4("a")
396399
.bit4("b")
397400
.uint16("c");

example/bmp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ var bmpFile = new Parser()
5555
type: bmpInfoHeader,
5656
});
5757

58-
require('fs').readFile('test.bmp', function(err, data) {
58+
require('fs').readFile('test.bmp', function (err, data) {
5959
console.log(bmpFile.parse(data));
6060
});

example/classfile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@ var ClassFile = Parser.start()
7575
.uint16('constant_pool_count')
7676
.array('cp_info', {
7777
type: CpInfo,
78-
length: function() {
78+
length: function () {
7979
return this.constant_pool_count - 1;
8080
},
8181
});
8282

83-
require('fs').readFile('Hello.class', function(err, data) {
83+
require('fs').readFile('Hello.class', function (err, data) {
8484
console.log(require('util').inspect(ClassFile.parse(data), { depth: null }));
8585
});

example/elf32.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var ELF32ProgramHeader = new Parser()
1313

1414
var ELF32ProgramHeaderTable = new Parser().array('items', {
1515
type: ELF32ProgramHeader,
16-
length: function(vars) {
16+
length: function (vars) {
1717
return vars.phnum;
1818
},
1919
});
@@ -33,14 +33,14 @@ var ELF32SectionHeader = new Parser()
3333

3434
var ELF32SectionHeaderTable = new Parser().array('items', {
3535
type: ELF32SectionHeader,
36-
length: function(vars) {
36+
length: function (vars) {
3737
return vars.shnum;
3838
},
3939
});
4040

4141
var ELF32SectionHeaderStringTable = new Parser().seek(1).array('items', {
4242
type: new Parser().string('name', { zeroTerminated: true }),
43-
lengthInBytes: function(vars) {
43+
lengthInBytes: function (vars) {
4444
var shstr = vars.section_headers.items[vars.shstrndx];
4545
return shstr.size - 1;
4646
},
@@ -72,13 +72,13 @@ var ELF32Header = new Parser()
7272
})
7373
.pointer('strings', {
7474
type: ELF32SectionHeaderStringTable,
75-
offset: function() {
75+
offset: function () {
7676
var shstr = vars.section_headers.items[vars.shstrndx];
7777
return shstr.offset;
7878
},
7979
});
8080

81-
require('fs').readFile('hello', function(err, data) {
81+
require('fs').readFile('hello', function (err, data) {
8282
var result = ELF32Header.parse(data);
8383
console.log(require('util').inspect(result, { depth: null }));
8484
});

0 commit comments

Comments
 (0)