Skip to content

Commit e2d4af1

Browse files
committed
Implements encoding from Parser
1 parent e6d19cb commit e2d4af1

File tree

4 files changed

+1871
-21
lines changed

4 files changed

+1871
-21
lines changed

README.md

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
[![Circle CI](https://circleci.com/gh/keichi/binary-parser.svg?style=svg)](https://circleci.com/gh/keichi/binary-parser)
44

5-
Binary-parser is a binary parser builder for [node](http://nodejs.org) that
6-
enables you to write efficient parsers in a simple and declarative manner.
5+
Binary-parser is a binary parser/encoder builder for [node](http://nodejs.org) that
6+
enables you to write efficient parsers/encoders in a simple and declarative manner.
77

88
It supports all common data types required to analyze a structured binary
9-
data. Binary-parser dynamically generates and compiles the parser code
10-
on-the-fly, which runs as fast as a hand-written parser (which takes much more
9+
data. Binary-parser dynamically generates and compiles the parser and encoder code
10+
on-the-fly, which runs as fast as a hand-written parser/encoder (which takes much more
1111
time and effort to write). Supported data types are:
1212

1313
- Integers (supports 8, 16, 32 bit signed- and unsigned integers)
@@ -31,11 +31,13 @@ $ npm install binary-parser
3131

3232
## Quick Start
3333
1. Create an empty Parser object with `new Parser()`.
34-
2. Chain builder methods to build the desired parser. (See
34+
2. Chain builder methods to build the desired parser and/or encoder. (See
3535
[API](https://github.com/Keichi/binary-parser#api) for detailed document of
3636
each methods)
3737
3. Call `Parser.prototype.parse` with an `Buffer` object passed as argument.
3838
4. Parsed result will be returned as an object.
39+
5. Or call `Parser.prototype.encode with an object passed as argument.
40+
6. Encoded result will be returned as a `Buffer` object.
3941

4042
```javascript
4143
// Module import
@@ -68,19 +70,43 @@ var buf = Buffer.from("450002c5939900002c06ef98adc24f6c850186d1", "hex");
6870

6971
// Parse buffer and show result
7072
console.log(ipHeader.parse(buf));
73+
74+
var anIpHeader = {
75+
version: 4,
76+
headerLength: 5,
77+
tos: 0,
78+
packetLength: 709,
79+
id: 37785,
80+
offset: 0,
81+
fragOffset: 0,
82+
ttl: 44,
83+
protocol: 6,
84+
checksum: 61336,
85+
src: [ 173, 194, 79, 108 ],
86+
dst: [ 133, 1, 134, 209 ] };
87+
88+
// Encode an IP header object and show result as hex string
89+
console.log(ipHeader.encode(anIpHeader).toString("hex"));
7190
```
7291

7392
## API
7493

75-
### new Parser()
94+
### new Parser([options])
7695
Constructs a Parser object. Returned object represents a parser which parses
77-
nothing.
96+
nothing. `options` is an optional object to pass options to this declarative
97+
parser.
98+
- `bufferSize` The size of the encoding buffer (when encoding is used) (default is 256 bytes).
7899

79100
### parse(buffer)
80101
Parse a `Buffer` object `buffer` with this parser and return the resulting
81102
object. When `parse(buffer)` is called for the first time, parser code is
82103
compiled on-the-fly and internally cached.
83104

105+
### encode(obj)
106+
Encode an `Object` object `obj` with this parser and return the resulting
107+
`Buffer`. When `encode(obj)` is called for the first time, encoder code is
108+
compiled on-the-fly and internally cached.
109+
84110
### create(constructorFunction)
85111
Set the constructor function that should be called to create the object
86112
returned from the `parse` method.
@@ -135,9 +161,11 @@ the following keys:
135161
- `zeroTerminated` - (Optional, defaults to `false`) If true, then this parser
136162
reads until it reaches zero.
137163
- `greedy` - (Optional, defaults to `false`) If true, then this parser reads
138-
until it reaches the end of the buffer. Will consume zero-bytes.
164+
until it reaches the end of the buffer. Will consume zero-bytes. (Note: has
165+
no effect on encoding function)
139166
- `stripNull` - (Optional, must be used with `length`) If true, then strip
140-
null characters from end of the string
167+
null characters from end of the string. (Note: has no effect on encoding, but
168+
when used, then the parse() and encode() functions are not the exact opposite)
141169

142170
### buffer(name[, options])
143171
Parse bytes as a buffer. `name` should consist only of alpha numeric
@@ -154,7 +182,8 @@ the following keys:
154182
sized buffers, string to reference another variable and function to do some
155183
calculation.
156184
- `readUntil` - (either `length` or `readUntil` is required) If `"eof"`, then
157-
this parser will read till it reaches end of the `Buffer` object.
185+
this parser will read till it reaches end of the `Buffer` object. (Note: has no
186+
effect on encoding.)
158187

159188
### array(name, options)
160189
Parse bytes as an array. `options` is an object which can have the following
@@ -271,7 +300,8 @@ current object. `options` is an object which can have the following keys:
271300
- `type` - (Required) A `Parser` object.
272301

273302
### skip(length)
274-
Skip parsing for `length` bytes.
303+
Skip parsing for `length` bytes. (Note: when encoding, the skipped bytes will be filled
304+
with zeros)
275305

276306
### endianess(endianess)
277307
Define what endianess to use in this parser. `endianess` can be either
@@ -381,13 +411,13 @@ var buffer = Buffer.from([2, /* left */ 1, 1, 0, /* right */ 0]);
381411
parser.parse(buffer);
382412
```
383413

384-
### compile()
385-
Compile this parser on-the-fly and cache its result. Usually, there is no need
386-
to call this method directly, since it's called when `parse(buffer)` is
414+
### compile() and compileEncode()
415+
Compile this parser/encoder on-the-fly and cache its result. Usually, there is no need
416+
to call this method directly, since it's called when `parse(buffer)` or `encode(obj)` is
387417
executed for the first time.
388418

389-
### getCode()
390-
Dynamically generates the code for this parser and returns it as a string.
419+
### getCode() and getCodeEncode()
420+
Dynamically generates the code for this parser/encoder and returns it as a string.
391421
Usually used for debugging.
392422

393423
### Common options
@@ -405,6 +435,21 @@ These are common options that can be specified in all parsers.
405435
});
406436
```
407437

438+
- `encoder` - Function that transforms an object property into a more desired
439+
form for encoding. This is the opposite of the above `formatter` function.
440+
```javascript
441+
var parser = new Parser().array("ipv4", {
442+
type: uint8,
443+
length: "4",
444+
formatter: function(arr) {
445+
return arr.join(".");
446+
},
447+
encoder: function(str) {
448+
return str.split(".");
449+
}
450+
});
451+
```
452+
408453
- `assert` - Do assertion on the parsed result (useful for checking magic
409454
numbers and so on). If `assert` is a `string` or `number`, the actual parsed
410455
result will be compared with it with `===` (strict equality check), and an

0 commit comments

Comments
 (0)