Skip to content

Commit d87ce4e

Browse files
committed
Wrap text at 80 characters
1 parent ce8fd29 commit d87ce4e

File tree

1 file changed

+116
-84
lines changed

1 file changed

+116
-84
lines changed

README.md

Lines changed: 116 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
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 library for [node](http://nodejs.org),
6-
which enables you to write efficient parsers in a simple & declarative way.
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.
77

8-
It supports all common data types required to analyze a structured binary data.
9-
Binary-parser dynamically generates and compiles the parser code on-the-fly,
10-
which runs as fast as a hand-written parser (which takes much more time and effort to write).
11-
Supported data types are:
8+
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
11+
time and effort to write). Supported data types are:
1212

1313
- Integers (supports 8, 16, 32 bit signed- and unsigned integers)
1414
- Floating point numbers (supports 32 and 64 bit floating point values)
1515
- Bit fields (supports bit fields with length from 1 to 32 bits)
16-
- Strings (supports various encodings, fixed-length and variable-length, zero terminated string)
16+
- Strings (supports various encodings, fixed-length and variable-length, zero
17+
terminated string)
1718
- Arrays (supports user-defined element type, fixed-length and variable-length)
1819
- Choices
1920
- User defined types
@@ -30,8 +31,9 @@ $ npm install binary-parser
3031

3132
## Quick Start
3233
1. Create an empty Parser object with `new Parser()`.
33-
2. Chain builder methods to build the desired parser. (See [API](https://github.com/Keichi/binary-parser#api) for detailed document
34-
of each methods)
34+
2. Chain builder methods to build the desired parser. (See
35+
[API](https://github.com/Keichi/binary-parser#api) for detailed document of
36+
each methods)
3537
3. Call `Parser.prototype.parse` with an `Buffer` object passed as argument.
3638
4. Parsed result will be returned as an object.
3739

@@ -71,23 +73,24 @@ console.log(ipHeader.parse(buf));
7173
## API
7274

7375
### new Parser()
74-
Constructs a Parser object. Returned object represents a parser which parses nothing.
76+
Constructs a Parser object. Returned object represents a parser which parses
77+
nothing.
7578

7679
### parse(buffer[, callback])
77-
Parse a `Buffer` object `buffer` with this parser and return the resulting object.
78-
When `parse(buffer)` is called for the first time, parser code is compiled on-the-fly
79-
and internally cached.
80+
Parse a `Buffer` object `buffer` with this parser and return the resulting
81+
object. When `parse(buffer)` is called for the first time, parser code is
82+
compiled on-the-fly and internally cached.
8083

8184
### create(constructorFunction)
82-
Set the constructor function that should be called to create the object returned from
83-
the `parse` method.
85+
Set the constructor function that should be called to create the object
86+
returned from the `parse` method.
8487

8588
### [u]int{8, 16, 32}{le, be}(name [,options])
86-
Parse bytes as an integer and store it in a variable named `name`. `name` should consist
87-
only of alphanumeric characters and start with an alphabet.
88-
Number of bits can be chosen from 8, 16 and 32.
89-
Byte-ordering can be either `l` for little endian or `b` for big endian.
90-
With no prefix, it parses as a signed number, with `u` prefixed as an unsigned number.
89+
Parse bytes as an integer and store it in a variable named `name`. `name`
90+
should consist only of alphanumeric characters and start with an alphabet.
91+
Number of bits can be chosen from 8, 16 and 32. Byte-ordering can be either
92+
`l` for little endian or `b` for big endian. With no prefix, it parses as a
93+
signed number, with `u` prefixed as an unsigned number.
9194

9295
```javascript
9396
var parser = new Parser()
@@ -100,13 +103,14 @@ var parser = new Parser()
100103
```
101104

102105
### bit\[1-32\](name [,options])
103-
Parse bytes as a bit field and store it in variable `name`. There are 32 methods from
104-
`bit1` to `bit32` each corresponding to 1-bit-length to 32-bits-length bit field.
106+
Parse bytes as a bit field and store it in variable `name`. There are 32
107+
methods from `bit1` to `bit32` each corresponding to 1-bit-length to
108+
32-bits-length bit field.
105109

106110
### {float, double}{le, be}(name [,options])
107-
Parse bytes as an floating-point value and store it in a variable
108-
named `name`. `name` should consist only of alphanumeric characters and start
109-
with an alphabet.
111+
Parse bytes as an floating-point value and store it in a variable named
112+
`name`. `name` should consist only of alphanumeric characters and start with
113+
an alphabet.
110114

111115
```javascript
112116
var parser = new Parser()
@@ -117,43 +121,57 @@ var parser = new Parser()
117121
```
118122

119123
### string(name [,options])
120-
Parse bytes as a string. `name` should consist only of alpha numeric characters and start
121-
with an alphabet. `options` is an object; following options are available:
122-
123-
- `encoding` - (Optional, defaults to `utf8`) Specify which encoding to use. `'utf8'`, `'ascii'`, `'hex'` and else
124-
are valid. See [`Buffer.toString`](http://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end) for more info.
125-
- `length ` - (Optional) Length of the string. Can be a number, string or a function.
126-
Use number for statically sized arrays, string to reference another variable and
127-
function to do some calculation.
128-
- `zeroTerminated` - (Optional, defaults to `false`) If true, then this parser reads until it reaches zero.
129-
- `greedy` - (Optional, defaults to `false`) If true, then this parser reads until it reaches the end of the buffer. Will consume zero-bytes.
130-
- `stripNull` - (Optional, must be used with `length`) If true, then strip null characters from end of the string
124+
Parse bytes as a string. `name` should consist only of alpha numeric
125+
characters and start with an alphabet. `options` is an object; following
126+
options are available:
127+
128+
- `encoding` - (Optional, defaults to `utf8`) Specify which encoding to use.
129+
`'utf8'`, `'ascii'`, `'hex'` and else are valid. See
130+
[`Buffer.toString`](http://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end)
131+
for more info.
132+
- `length ` - (Optional) Length of the string. Can be a number, string or a
133+
function. Use number for statically sized arrays, string to reference
134+
another variable and function to do some calculation.
135+
- `zeroTerminated` - (Optional, defaults to `false`) If true, then this parser
136+
reads until it reaches zero.
137+
- `greedy` - (Optional, defaults to `false`) If true, then this parser reads
138+
until it reaches the end of the buffer. Will consume zero-bytes.
139+
- `stripNull` - (Optional, must be used with `length`) If true, then strip
140+
null characters from end of the string
131141

132142
### buffer(name [,options])
133-
Parse bytes as a buffer. `name` should consist only of alpha numeric characters and start
134-
with an alphabet. `options` is an object; following options are available:
135-
136-
- `clone` - (Optional, defaults to `false`) By default, `buffer(name [,options])` returns a new buffer which references
137-
the same memory as the parser input, but offset and cropped by a certain range. If this option is true, input buffer
138-
will be cloned and a new buffer referncing another memory is returned.
139-
- `length ` - (either `length` or `readUntil` is required) Length of the buffer. Can be a number, string or a function.
140-
Use number for statically sized buffers, string to reference another variable and
141-
function to do some calculation.
142-
- `readUntil` - (either `length` or `readUntil` is required) If `'eof'`, then this parser
143-
will read till it reaches end of the `Buffer` object.
144-
143+
Parse bytes as a buffer. `name` should consist only of alpha numeric
144+
characters and start with an alphabet. `options` is an object; following
145+
options are available:
146+
147+
- `clone` - (Optional, defaults to `false`) By default,
148+
`buffer(name [,options])` returns a new buffer which references the same
149+
memory as the parser input, but offset and cropped by a certain range. If
150+
this option is true, input buffer will be cloned and a new buffer referncing
151+
another memory is returned.
152+
- `length ` - (either `length` or `readUntil` is required) Length of the
153+
buffer. Can be a number, string or a function. Use number for statically
154+
sized buffers, string to reference another variable and function to do some
155+
calculation.
156+
- `readUntil` - (either `length` or `readUntil` is required) If `'eof'`, then
157+
this parser will read till it reaches end of the `Buffer` object.
145158

146159
### array(name [,options])
147-
Parse bytes as an array. `options` is an object; following options are available:
148-
149-
- `type` - (Required) Type of the array element. Can be a string or an user defined Parser object.
150-
If it's a string, you have to choose from [u]int{8, 16, 32}{le, be}.
151-
- `length` - (either `length`, `lengthInBytes`, or `readUntil` is required) Length of the array. Can be a number, string or a function.
152-
Use number for statically sized arrays.
153-
- `lengthInBytes` - (either `length`, `lengthInBytes`, or `readUntil` is required) Length of the array expressed in bytes. Can be a number, string or a function.
154-
Use number for statically sized arrays.
155-
- `readUntil` - (either `length`, `lengthInBytes`, or `readUntil` is required) If `'eof'`, then this parser
156-
reads until the end of `Buffer` object. If function it reads until the function returns true.
160+
Parse bytes as an array. `options` is an object; following options are
161+
available:
162+
163+
- `type` - (Required) Type of the array element. Can be a string or an user
164+
defined Parser object. If it's a string, you have to choose from [u]int{8,
165+
16, 32}{le, be}.
166+
- `length` - (either `length`, `lengthInBytes`, or `readUntil` is required)
167+
Length of the array. Can be a number, string or a function. Use number for
168+
statically sized arrays.
169+
- `lengthInBytes` - (either `length`, `lengthInBytes`, or `readUntil` is
170+
required) Length of the array expressed in bytes. Can be a number, string or
171+
a function. Use number for statically sized arrays.
172+
- `readUntil` - (either `length`, `lengthInBytes`, or `readUntil` is required)
173+
If `'eof'`, then this parser reads until the end of `Buffer` object. If
174+
function it reads until the function returns true.
157175

158176
```javascript
159177
var parser = new Parser()
@@ -209,15 +227,17 @@ var parser = new Parser()
209227
```
210228

211229
### choice(name [,options])
212-
Choose one parser from several choices according to a field value.
213-
Combining `choice` with `array` is useful for parsing a typical
214-
[Type-Length-Value](http://en.wikipedia.org/wiki/Type-length-value) styled format.
215-
216-
- `tag` - (Required) The value used to determine which parser to use from the `choices`
217-
Can be a string pointing to another field or a function.
218-
- `choices` - (Required) An object which key is an integer and value is the parser which is executed
219-
when `tag` equals the key value.
220-
- `defaultChoice` - (Optional) In case of the tag value doesn't match any of `choices` use this parser.
230+
Choose one parser from several choices according to a field value. Combining
231+
`choice` with `array` is useful for parsing a typical
232+
[Type-Length-Value](http://en.wikipedia.org/wiki/Type-length-value) styled
233+
format.
234+
235+
- `tag` - (Required) The value used to determine which parser to use from the
236+
`choices` Can be a string pointing to another field or a function.
237+
- `choices` - (Required) An object which key is an integer and value is the
238+
parser which is executed when `tag` equals the key value.
239+
- `defaultChoice` - (Optional) In case of the tag value doesn't match any of
240+
`choices` use this parser.
221241

222242
```javascript
223243
var parser1 = ...;
@@ -246,8 +266,8 @@ Nest a parser in this position. Parse result of the nested parser is stored in t
246266
Skip parsing for `length` bytes.
247267

248268
### endianess(endianess)
249-
Define what endianess to use in this parser. `endianess` can be either `'little'` or `'big'`.
250-
The default endianess of `Parser` is set to big-endian.
269+
Define what endianess to use in this parser. `endianess` can be either
270+
`'little'` or `'big'`. The default endianess of `Parser` is set to big-endian.
251271

252272
```javascript
253273
var parser = new Parser()
@@ -261,7 +281,9 @@ var parser = new Parser()
261281
```
262282

263283
### namely(alias)
264-
Set an alias to this parser, so there will be an opportunity to refer to it by name in methods like `.array`, `.nest` and `.choice`, instead of requirement to have an instance of it.
284+
Set an alias to this parser, so there will be an opportunity to refer to it by
285+
name in methods like `.array`, `.nest` and `.choice`, instead of requirement
286+
to have an instance of it.
265287

266288
Especially, the parser may reference itself:
267289

@@ -306,11 +328,18 @@ var buffer = new Buffer([ 2,
306328
parser.parse(buffer);
307329
```
308330

309-
For most of the cases there is almost no difference to the instance-way of referencing, but this method provides the way to parse recursive trees, where each node could reference the node of the same type from the inside.
331+
For most of the cases there is almost no difference to the instance-way of
332+
referencing, but this method provides the way to parse recursive trees, where
333+
each node could reference the node of the same type from the inside.
310334

311-
Also, when you reference a parser using its instance twice, the generated code will contain two similar parts of the code included, while with the named approach, it will include a function with a name, and will just call this function for every case of usage.
335+
Also, when you reference a parser using its instance twice, the generated code
336+
will contain two similar parts of the code included, while with the named
337+
approach, it will include a function with a name, and will just call this
338+
function for every case of usage.
312339

313-
NB: This style could lead to circular references and infinite recursion, to avoid this, ensure that every possible path has its end. Also, this recursion is not tail-optimized, so could lead to memory leaks when it goes too deep.
340+
NB: This style could lead to circular references and infinite recursion, to
341+
avoid this, ensure that every possible path has its end. Also, this recursion
342+
is not tail-optimized, so could lead to memory leaks when it goes too deep.
314343

315344
An example of referencing other patches:
316345

@@ -344,9 +373,9 @@ parser.parse(buffer);
344373
```
345374

346375
### compile()
347-
Compile this parser on-the-fly and cache its result. Usually, there is no need to
348-
call this method directly, since it's called when `parse(buffer)` is executed
349-
for the first time.
376+
Compile this parser on-the-fly and cache its result. Usually, there is no need
377+
to call this method directly, since it's called when `parse(buffer)` is
378+
executed for the first time.
350379

351380
### getCode()
352381
Dynamically generates the code for this parser and returns it as a string.
@@ -355,7 +384,8 @@ Usually used for debugging.
355384
### Common options
356385
These are common options that can be specified in all parsers.
357386

358-
- `formatter` - Function that transforms the parsed value into a more desired form.
387+
- `formatter` - Function that transforms the parsed value into a more desired
388+
form.
359389
```javascript
360390
var parser = new Parser()
361391
.array('ipv4', {
@@ -365,11 +395,12 @@ These are common options that can be specified in all parsers.
365395
});
366396
```
367397

368-
- `assert` - Do assertion on the parsed result (useful for checking magic numbers and so on).
369-
If `assert` is a `string` or `number`, the actual parsed result will be compared with it
370-
with `===` (strict equality check), and an exception is thrown if they mismatch.
371-
On the other hand, if `assert` is a function, that function is executed with one argument
372-
(parsed result) and if it returns false, an exception is thrown.
398+
- `assert` - Do assertion on the parsed result (useful for checking magic
399+
numbers and so on). If `assert` is a `string` or `number`, the actual parsed
400+
result will be compared with it with `===` (strict equality check), and an
401+
exception is thrown if they mismatch. On the other hand, if `assert` is a
402+
function, that function is executed with one argument (parsed result) and if
403+
it returns false, an exception is thrown.
373404

374405
```javascript
375406
// simple maginc number validation
@@ -393,7 +424,8 @@ On the other hand, if `assert` is a function, that function is executed with one
393424
See `example` for more complex examples.
394425

395426
## Support
396-
Please report issues to the [issue tracker](https://github.com/Keichi/binary-parser/issues) if you
397-
have any difficulties using this module, found a bug, or request a new feature.
427+
Please report issues to the
428+
[issue tracker](https://github.com/Keichi/binary-parser/issues) if you have
429+
any difficulties using this module, found a bug, or request a new feature.
398430

399431
Pull requests with fixes and improvements are welcomed!

0 commit comments

Comments
 (0)