bireader 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +561 -1
  2. package/package.json +2 -1
  3. package/src/reader.js +2983 -574
  4. package/src/writer.js +3870 -329
package/README.md CHANGED
@@ -1 +1,561 @@
1
- # TODO
1
+ # bireader
2
+
3
+ A simple binary reader and writer that keeps track of your position to quickly create file structures. Includes shared naming conventions and multiple programmable calling for easy data conversions to do low level parsing. Accepts `Uint8Array` or `Buffer`.
4
+
5
+ Supported data types are:
6
+
7
+ - [Bitfields](#bit-field) ([u]bit{1-32}{le|be}) 1-32 bit signed or unsigned value in big or little endian order
8
+ - [Bytes](#byte) ([u]int8, byte) 8 bit signed or unsigned value
9
+ - [Shorts](#short) ([u]int16, word, short{le|be}) 16 bit signed or unsigned value in big or little endian order
10
+ - [Half Floats](#half-float) (halffloat, half{le|be}) 16 bit decimal value in big or little endian order
11
+ - [Integers](#integer) ([u]int32, int, double{le|be}) 32 bit signed or unsigned value in big or little endian order
12
+ - [Floats](#float) (float{le|be}) 32 bit decimal value in big or little endian
13
+ - [Quadwords](#quadword) ([u]int64, quad, bigint{le|be}) 64 bit signed or unsigned in big or little endian
14
+ - [Double Floats](#double-float) (doublefloat, dfloat{le|be}) 64 bit decimal value in big or little endian
15
+ - [Strings](#strings) (string) Fixed and non-fixed length, UTF, pascal, wide pascal. Includes all ```TextEncoder``` types
16
+
17
+ ## Installation
18
+
19
+ ```npm install bireader```
20
+
21
+ ### Quick Start
22
+
23
+ Import the reader or writer. Create a new parser with the data and start parsing.
24
+
25
+ Includes all presents for quick parsing or programmable functions (examples below).
26
+
27
+ ```javascript
28
+ import {bireader, biwriter} from 'bireader';
29
+
30
+ //example of using mixed preset function naming and programmable calls
31
+ function header_read(data){
32
+ const br = new bireader(data)
33
+ const header = {}
34
+ header.magic = br.cstring({length:4})
35
+ const unsigned = header.magic == "foo" ? true : false
36
+ header.ver = br.int32(unsigned, unsigned == true : "big" : "little")
37
+ br.skip(2) //reserved
38
+ if(header.ver == 10){
39
+ br.endianness("big")
40
+ header.file_size = br.size
41
+ header.heigth = br.uint()
42
+ header.width = br.quad()
43
+ } else if(header.ver < 9) {
44
+ br.le()
45
+ header.file_size = br.ushort()
46
+ header.heigth = br.uword()
47
+ header.width = br.uint64()
48
+ } else {
49
+ throw new Error('Unknown version of ' + header.ver)
50
+ }
51
+ br.finished()
52
+ return header
53
+ }
54
+
55
+ function header_write(size, magic, ver, heigth, width){
56
+ const data = new Uint8Array(size)
57
+ const bw = new biwriter(data)
58
+ bw.writeString(magic, {length:4})
59
+ const unsigned = true
60
+ header.ver = bw.uint8(ver)
61
+ if(header.ver == 10){
62
+ bw.bit16() //reserved
63
+ bw.uint32be(heigth)
64
+ bw.quadbe(width)
65
+ } else if(header.ver < 9) {
66
+ bw.fskip(2) //reserved
67
+ bw.uint16le(size)
68
+ const bitsize = header.magic == "foo" ? 16 : 32
69
+ const byteOffset = 0
70
+ const bitOffset = 0
71
+ bw.bit(heigth, bitsize, byteOffset, bitOffset0, unsigned)
72
+ bw.int64le(width, byteOffset, unsigned)
73
+ } else {
74
+ throw new Error('Unknown version of ' + ver)
75
+ }
76
+ const header = bw.crop(0,size)
77
+ bw.finished()
78
+ return header
79
+ }
80
+ ```
81
+
82
+ ## Common Functions
83
+
84
+ Common functions for setup and movement shared by both (unless indicated).
85
+
86
+ <table>
87
+ <thead>
88
+ <tr>
89
+ <th align="center" colspan="2">Function</th>
90
+ <th align="center">Params (bold requires)</th>
91
+ <th align="left">Notes</th>
92
+ </tr>
93
+ </thead>
94
+ <tbody>
95
+ <tr>
96
+ <td>Name</td>
97
+ <td>new bireader(<b>data</b>, 1, 0, "big")</td>
98
+ <td align="center" rowspan="2"><b>Buffer or Uint8Array</b>, byte offset, bit offset, endian</td>
99
+ <td rowspan="2">new Constructor</td>
100
+ </tr>
101
+ <tr>
102
+ <td>Name</td>
103
+ <td>new biwriter(<b>data</b>, 0, 8, "little")</td>
104
+ </tr>
105
+ <tr>
106
+ <td>Name</td>
107
+ <td>endianness(<b>str</b>)</td>
108
+ <td align="center" rowspan="2"><b>big</b> or <b>little</b> (default little)</td>
109
+ <td rowspan="2">Can be changed at any time.</td>
110
+ </tr>
111
+ <tr>
112
+ <td>Presets</td>
113
+ <td>bigEndian()<br>big()<br>be()<br>littleEndian()<br>little()<br>le()</td>
114
+ </tr>
115
+ <tr>
116
+ <td>Name</td>
117
+ <td>skip(<b>bytes</b>, bits)</td>
118
+ <td align="center" rowspan="2"><b>bytes to skip</b>, bits to skip</td>
119
+ <td rowspan="2">Use negative to go back.<br><b>Note:</b> Remaining bits are dropped when returning to a byte read.</td>
120
+ </tr>
121
+ <tr>
122
+ <td>Aliases</td>
123
+ <td>fskip(<b>bytes</b>, bits)</td>
124
+ </tr>
125
+ <tr>
126
+ <td>Name</td>
127
+ <td>goto(<b>byte</b>, bit)</td>
128
+ <td align="center" rowspan="2"><b>Offset from current byte</b>, bit read position</td>
129
+ <td rowspan="2"><b>Note:</b> Remaining bits are drop when returning to byte data.</td>
130
+ </tr>
131
+ <tr>
132
+ <td>Aliases</td>
133
+ <td>seek(<b>byte</b>, bit)<br>fseek(<b>byte</b>, bit)<br>pointer(<b>byte</b>, bit)<br>warp(<b>byte</b>, bit)<br>fsetpos(<b>byte</b>, bit)</td>
134
+ </tr>
135
+ <tr>
136
+ <td>Name</td>
137
+ <td>rewind()</td>
138
+ <td align="center" rowspan="2">none</td>
139
+ <td rowspan="2">Moves current read position to start of data.</td>
140
+ </tr>
141
+ <tr>
142
+ <td>Aliases</td>
143
+ <td>gotostart()<br>tostart()</td>
144
+ </tr>
145
+ <tr>
146
+ <td>Name</td>
147
+ <td>ftell()</td>
148
+ <td align="center" rowspan="2">none</td>
149
+ <td rowspan="2">Gets current read position in bytes</td>
150
+ </tr>
151
+ <tr>
152
+ <td>Aliases</td>
153
+ <td>tell()<br>fgetpos()</td>
154
+ </tr>
155
+ <tr>
156
+ <td>Name</td>
157
+ <td>unrestrict()</td>
158
+ <td align="center">none</td>
159
+ <td><b>biwriter only:</b> Will extend array if data is written outside of max size (default on)</td>
160
+ </tr>
161
+ <tr>
162
+ <td>Name</td>
163
+ <td>restrict()</td>
164
+ <td align="center">none</td>
165
+ <td><b>biwriter only:</b> Won't extend array if data is written outside of max size (default off)</td>
166
+ </tr>
167
+ <tr>
168
+ <td>Name</td>
169
+ <td>crop(startOffset, endOffset)</td>
170
+ <td align="center" rowspan="2">start byte of data, end byte of data</td>
171
+ <td rowspan="2">Returns data truncated. defaults to 0 and current read / write position. <br><b>Note:</b> Does not affect supplied data or current read position.</td>
172
+ </tr>
173
+ <tr>
174
+ <td>Aliases</td>
175
+ <td>clip(startOffset, endOffset)<br>truncate(startOffset, endOffset)<br>slice(startOffset, endOffset)</td>
176
+ </tr>
177
+ <tr>
178
+ <td>Name</td>
179
+ <td>get() </td>
180
+ <td align="center" rowspan="2">none</td>
181
+ <td rowspan="2">Returns supplied data.</td>
182
+ </tr>
183
+ <tr>
184
+ <td>Aliases</td>
185
+ <td>return()<br>data</td>
186
+ </tr>
187
+ <tr>
188
+ <td>Name</td>
189
+ <td>end()</td>
190
+ <td align="center" rowspan="2">none</td>
191
+ <td rowspan="2">Removes supplied data.</td>
192
+ </tr>
193
+ <tr>
194
+ <td>Aliases</td>
195
+ <td>close()<br>done()<br>finished()</td>
196
+ </tr>
197
+ </tbody>
198
+ </table>
199
+
200
+ ## Bit field
201
+
202
+ Parse value as a bit field. There are 32 functions from bit1 to bit32 and can be signed or unsigned (with a ``u`` at the start) and in little or big endian order (``be`` or ``le`` at the end).
203
+
204
+ **Note:** Remaining bits are dropped when returning to a byte read. Example, after using ``bit4()`` then ``ubyte()``, the read locations drops the remaining 4 bits after ``bit4()`` when reading ``ubyte()``.
205
+
206
+ <table>
207
+ <thead>
208
+ <tr>
209
+ <th></th>
210
+ <th align="center">Functions</th>
211
+ <th align="left">Params (bold requires)</th>
212
+ </tr>
213
+ </thead>
214
+ <tbody>
215
+ <tr>
216
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
217
+ <td>readBit(<b>bits</b>, unsigned, endian)</td>
218
+ <td><b>number of bits</b>, if the value is returned unsigned, big or little endian</td>
219
+ </tr>
220
+ <tr>
221
+ <td>writeBit(<b>value, bits</b>, offsetBits, offsetBytes, unsigned, endian)</td>
222
+ <td><b>value to write, number of bits</b>, bits offset from current position, byte offset from current position, if the value is written unsigned, big or little endian<br>Note: Will throw error if value is outside of size of data</td>
223
+ </tr>
224
+ <tr>
225
+ <td align="center"><b>Presets (reader)</b></td>
226
+ <td>[u]bit{1-32}{le|be}(*unsigned)</td>
227
+ <td>If value is signed or not.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>first</i> augment</td>
228
+ </tr>
229
+ <tr>
230
+ <td align="center"><b>Presets (writer)</b></td>
231
+ <td>[u]bit{1-32}{le|be}(<b>value</b>, offsetBits, offsetBytes, *unsigned)</td>
232
+ <td><b>value to write</b>, byte offset from current position, if value is unsigned or not.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>fourth</i> augment</td>
233
+ </tr>
234
+ </tbody>
235
+ </table>
236
+
237
+ ## Byte
238
+
239
+ Parse value as a byte (aka int8). Can be signed or unsigned (with a ``u`` at the start).
240
+
241
+ <table>
242
+ <thead>
243
+ <tr>
244
+ <th></th>
245
+ <th align="center">Functions</th>
246
+ <th align="left">Params (bold requires)</th>
247
+ </tr>
248
+ </thead>
249
+ <tbody>
250
+ <tr>
251
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
252
+ <td>readByte(unsigned)</td>
253
+ <td>if the value is returned unsigned</td>
254
+ </tr>
255
+ <tr>
256
+ <td>writeByte(<b>value</b>, offsetBytes, unsigned)</td>
257
+ <td><b>value to write</b>, byte offset from current position, if the value is written unsigned<br>Note: Will throw error if value is outside of size of data</td>
258
+ </tr>
259
+ <tr>
260
+ <td align="center"><b>Presets (reader)</b></td>
261
+ <td>[u]{byte|int8}(*unsigned)</td>
262
+ <td>If value is signed or not.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>first</i> augment</td>
263
+ </tr>
264
+ <tr>
265
+ <td align="center"><b>Presets (writer)</b></td>
266
+ <td>[u]{byte|int8}(<b>value</b>, offsetBytes, *unsigned)</td>
267
+ <td><b>value to write</b>, byte offset from current position, if value is signed or not.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>third</i> augment</td>
268
+ </tr>
269
+ </tbody>
270
+ </table>
271
+
272
+ ## Short
273
+
274
+ Parse value as a int16 (aka short or word). Can be signed or unsigned (with a ``u`` at the start) and in little or big endian order (``be`` or ``le`` at the end).
275
+
276
+ <table>
277
+ <thead>
278
+ <tr>
279
+ <th></th>
280
+ <th align="center">Functions</th>
281
+ <th align="left">Params (bold requires)</th>
282
+ </tr>
283
+ </thead>
284
+ <tbody>
285
+ <tr>
286
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
287
+ <td>readInt16(unsigned, endian)</td>
288
+ <td>if the value is returned unsigned, big or little endian</td>
289
+ </tr>
290
+ <tr>
291
+ <td>writeInt16(<b>value</b>, offsetBytes, unsigned, endian)</td>
292
+ <td><b>value to write</b>, byte offset from current position, if the value is written unsigned, big or little endian<br>Note: Will throw error if value is outside of size of data</td>
293
+ </tr>
294
+ <tr>
295
+ <td align="center"><b>Presets (reader)</b></td>
296
+ <td>[u]{int16|word|short}{be|le}(*unsigned, *endian)</td>
297
+ <td>If value is unsigned, big or little endian.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>first</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>second</i> augment (does not overwite set endian).</td>
298
+ </tr>
299
+ <tr>
300
+ <td align="center"><b>Presets (writer)</b></td>
301
+ <td>[u]{int16|word|short}{be|le}(<b>value</b>, offsetBytes, *unsigned, *endian)</td>
302
+ <td><b>value to write</b>, if value is unsigned, big or little endian.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>third</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>fourth</i> augment (does not overwite set endian).</td>
303
+ </tr>
304
+ </tbody>
305
+ </table>
306
+
307
+ ## Half Float
308
+
309
+ Parse value as a half float (aka half). Can be in little or big endian order (``be`` or ``le`` at the end).
310
+
311
+ <table>
312
+ <thead>
313
+ <tr>
314
+ <th></th>
315
+ <th align="center">Functions</th>
316
+ <th align="left">Params (bold requires)</th>
317
+ </tr>
318
+ </thead>
319
+ <tbody>
320
+ <tr>
321
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
322
+ <td>readHalfFloat(endian)</td>
323
+ <td>big or little endian</td>
324
+ </tr>
325
+ <tr>
326
+ <td>writeHalfFloat(<b>value</b>, offsetBytes, endian)</td>
327
+ <td><b>value to write</b>, byte offset from current position, big or little endian<br>Note: Will throw error if value is outside of size of data</td>
328
+ </tr>
329
+ <tr>
330
+ <td align="center"><b>Presets (reader)</b></td>
331
+ <td>{halffloat|half}{be|le}(*endian)</td>
332
+ <td>Big or little endian.<br>*Note: functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>first</i> augment (does not overwite set endian).</td>
333
+ </tr>
334
+ <tr>
335
+ <td align="center"><b>Presets (writer)</b></td>
336
+ <td>{halffloat|half}{be|le}(<b>value</b>, offsetBytes, *endian)</td>
337
+ <td><b>value to write</b>, byte offset from current position, big or little endian.<br>*Note: and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>second</i> augment (does not overwite set endian).</td>
338
+ </tr>
339
+ </tbody>
340
+ </table>
341
+
342
+ ## Integer
343
+
344
+ Parse value as a int32 (aka int or double). Can be signed or unsigned (with a ``u`` at the start) and in little or big endian order (``be`` or ``le`` at the end).
345
+
346
+ <table>
347
+ <thead>
348
+ <tr>
349
+ <th></th>
350
+ <th align="center">Functions</th>
351
+ <th align="left">Params (bold requires)</th>
352
+ </tr>
353
+ </thead>
354
+ <tbody>
355
+ <tr>
356
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
357
+ <td>readInt32(unsigned, endian)</td>
358
+ <td>if the value is returned unsigned, big or little endian</td>
359
+ </tr>
360
+ <tr>
361
+ <td>writeInt32(<b>value</b>, offsetBytes, unsigned, endian)</td>
362
+ <td><b>value to write</b>, byte offset from current position, if the value is written unsigned, big or little endian<br>Note: Will throw error if value is outside of size of data</td>
363
+ </tr>
364
+ <tr>
365
+ <td align="center"><b>Presets (reader)</b></td>
366
+ <td>[u]{int32|int|double}{be|le}(*unsigned, *endian)</td>
367
+ <td>If value is unsigned, little or big endian.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>first</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>second</i> augment (does not overwite set endian).</td>
368
+ </tr>
369
+ <tr>
370
+ <td align="center"><b>Presets (writer)</b></td>
371
+ <td>[u]{int32|int|double}{be|le}(<b>value</b>, offsetBytes, *unsigned, *endian)</td>
372
+ <td><b>value to write</b>, byte offset from current position, if value is unsigned, little or big endian<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>third</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>fourth</i> augment (does not overwite set endian).</td>
373
+ </tr>
374
+ </tbody>
375
+ </table>
376
+
377
+ ## Float
378
+
379
+ Parse value as a float. Can be in little or big endian order (``be`` or ``le`` at the end).
380
+
381
+ <table>
382
+ <thead>
383
+ <tr>
384
+ <th></th>
385
+ <th align="center">Functions</th>
386
+ <th align="left">Params (bold requires)</th>
387
+ </tr>
388
+ </thead>
389
+ <tbody>
390
+ <tr>
391
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
392
+ <td>readFloat(endian)</td>
393
+ <td>big or little endian</td>
394
+ </tr>
395
+ <tr>
396
+ <td>writeInt64(<b>value</b>, offsetBytes, endian)</td>
397
+ <td><b>value to write</b>, byte offset from current position, big or little endian<br>Note: Will throw error if value is outside of size of data</td>
398
+ </tr>
399
+ <tr>
400
+ <td align="center"><b>Presets (reader)</b></td>
401
+ <td>float{be|le}(*endian)</td>
402
+ <td>Big or little endian.<br>*Note: functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>first</i> augment (does not overwite set endian).</td>
403
+ </tr>
404
+ <tr>
405
+ <td align="center"><b>Presets (writer)</b></td>
406
+ <td>float{be|le}(<b>value</b>, offsetBytes, *endian)</td>
407
+ <td><b>value to write</b>, byte offset from current position, big or little endian.<br>*Note: functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>third</i> augment (does not overwite set endian).</td>
408
+ </tr>
409
+ </tbody>
410
+ </table>
411
+
412
+ ## Quadword
413
+
414
+ Parse value as a int64 (aka quad or bigint). Can be signed or unsigned (with a ``u`` at the start) and in little or big endian order (``be`` or ``le`` at the end).
415
+
416
+ <table>
417
+ <thead>
418
+ <tr>
419
+ <th></th>
420
+ <th align="center">Functions</th>
421
+ <th align="left">Params (bold requires)</th>
422
+ </tr>
423
+ </thead>
424
+ <tbody>
425
+ <tr>
426
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
427
+ <td>readInt64(unsigned, endian)</td>
428
+ <td>if the value is returned unsigned, big or little endian</td>
429
+ </tr>
430
+ <tr>
431
+ <td>writeInt64(<b>value</b>, offsetBytes, unsigned, endian)</td>
432
+ <td><b>value to write</b>, byte offset from current position, if the value is written unsigned, big or little endian<br>Note: Will throw error if value is outside of size of data</td>
433
+ </tr>
434
+ <tr>
435
+ <td align="center"><b>Presets (reader)</b></td>
436
+ <td>[u]{int64|quad|bigint}{be|le}(*unsigned, *endian)</td>
437
+ <td>If value is unsigned, if value is unsigned, big or little endian.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>first</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>second</i> augment (does not overwite set endian).</td>
438
+ </tr>
439
+ <tr>
440
+ <td align="center"><b>Presets (writer)</b></td>
441
+ <td>[u]{int64|quad|bigint}{be|le}(<b>value</b>, offsetBytes, *unsigned, *endian)</td>
442
+ <td><b>value to write</b>, byte offset from current position, if value is unsigned, big or little endian.<br>*Note: functions without the starting letter <u>u</u> can still be called unsigned when <b>true</b> is the <i>third</i> augment, and functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>fourth</i> augment (does not overwite set endian).</td>
443
+ </tr>
444
+ </tbody>
445
+ </table>
446
+
447
+ ## Double Float
448
+
449
+ Parse value as a double float (aka dfloat). Can be in little or big endian order (``be`` or ``le`` at the end).
450
+
451
+ <table>
452
+ <thead>
453
+ <tr>
454
+ <th></th>
455
+ <th align="center">Functions</th>
456
+ <th align="left">Params (bold requires)</th>
457
+ </tr>
458
+ </thead>
459
+ <tbody>
460
+ <tr>
461
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
462
+ <td>readDoubleFloat(endian)</td>
463
+ <td>big or little endian</td>
464
+ </tr>
465
+ <tr>
466
+ <td>writeDoubleFloat(<b>value</b>, offsetBytes, endian)</td>
467
+ <td><b>value to write</b>, byte offset from current position, big or little endian.<br>Note: Will throw error if value is outside of size of data</td>
468
+ </tr>
469
+ <tr>
470
+ <td align="center"><b>Presets (reader)</b></td>
471
+ <td>{doublefloat|dfloat}{be|le}(*endian)</td>
472
+ <td>Big or little endian.<br>*Note: functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>first</i> augment (does not overwite set endian).</td>
473
+ </tr>
474
+ <tr>
475
+ <td align="center"><b>Presets (writer)</b></td>
476
+ <td>{doublefloat|dfloat}{be|le}(<b>value</b>, offsetBytes, *endian)</td>
477
+ <td><b>Value to write</b>, byte offset from current position, big or little endian.<br>*Note: functions without ending letters <u>be</u> or <u>le</u> can still be called the endian in the <i>third</i> augment (does not overwite set endian).</td>
478
+ </tr>
479
+ </tbody>
480
+ </table>
481
+
482
+ ## Strings
483
+
484
+ Parse a string in any format. Be sure to use options object for formatting unless using a preset. Strings with larger than 1 byte character reads can use ``be`` or ``le`` at the end for little or big endian.
485
+
486
+ Presents include C or Unicode, Ansi and multiple pascals.
487
+
488
+
489
+ <table>
490
+ <thead>
491
+ <tr>
492
+ <th></th>
493
+ <th align="center">Functions</th>
494
+ <th align="left">Params (bold requires)</th>
495
+ </tr>
496
+ </thead>
497
+ <tbody>
498
+ <tr>
499
+ <td align="center" rowspan="2"><b>Name<br>(master)</b></td>
500
+ <td>
501
+ readString({<br>
502
+ length,<br>
503
+ stringType,<br>
504
+ terminateValue,<br>
505
+ lengthReadSize,<br>
506
+ stripNull,<br>
507
+ encoding,<br>
508
+ endian<br>
509
+ })
510
+ </td>
511
+ <td><ul><li>Length for non-fixed UTF strings. If not supplied, reads until 0 or supplied terminate value (for fixed length UTF strings only)</li><li>String type. Defaults to utf-8 (utf-8, utf-16, pascal, wide-pascal accepted)</li><li>Terminate value. Default is 0x00 (for non-fixed length utf strings)</li><li>Size of the first value that defines the length of the string. Defaults to 1 as uint8, respects supplied endian (for Pascal strings only, accepts 1, 2 or 4 bytes)</li><li>Removes 0x00 characters on read (default true)</li><li>Encoding. Defaults to utf-8 on utf-8 or pascal and utf-16 on utf-16 or wide-pascal (accepts all TextDecoder options)</li><li>Endian (for wide-pascal and utf-16 character order, does not overwite set endian)</li></ul></td>
512
+ </tr>
513
+ <tr>
514
+ <td>writeString(<b>string</b>, {<br>
515
+ length,<br>
516
+ stringType,<br>
517
+ terminateValue,<br>
518
+ lengthReadSize,<br>
519
+ encoding,<br>
520
+ endian<br>
521
+ })</td>
522
+ <td><ul><li><b>String to write</b></li><li>Length for non-fixed UTF strings. If not supplied, defaults to encoded string length (for fixed length UTF strings only, will trucate if supplied value is smaller than string length)</li><li>String type. Defaults to utf-8 (utf-8, utf-16, pascal, wide-pascal accepted)</li><li>Terminate value. Default is 0x00 (for non-fixed length utf strings)</li><li>Size of the first value that defines the length of the string. Defaults to 1 as uint8, respects supplied endian (for Pascal strings only, accepts 1, 2 or 4 bytes)</li><li>Encoding. Defaults to utf-8 on utf-8 or pascal and utf-16 on utf-16 or wide-pascal (accepts all TextDecoder options)</li><li>Endian (for wide-pascal and utf-16 character order, does not overwite set endian)</li></ul></td>
523
+ </tr>
524
+ <tr>
525
+ <td align="center"><b>Presets (reader)</b></td>
526
+ <td>
527
+ {c|utf8}string(length, terminateValue, stripNull)<br><br>
528
+ ansistring(length, terminateValue, stripNull)<br><br>
529
+ {utf16|uni}string(length, terminateValue, stripNull, *endian)<br><br>
530
+ pstring(lengthReadSize, stripNull, *endian)<br><br>
531
+ pstring{1|2|4}{be|le}(stripNull, *endian)<br><br>
532
+ wpstring{be|le}(lengthReadSize, stripNull, *endian)<br><br>
533
+ wpstring{1|2|4}{be|le}(stripNull, *endian)
534
+ </td>
535
+ <td>Based on above.<br><b>Note:</b> Presets use augments not a single object. Endian only needed when not part of function name. Does not override set endian.</td>
536
+ </tr>
537
+ <tr>
538
+ <td align="center"><b>Presets (writer)</b></td>
539
+ <td>
540
+ {c|utf8}string(<b>string</b>, offset, length, terminateValue)<br><br>
541
+ ansistring(<b>string</b>, offset, length, terminateValue)<br><br>
542
+ {utf16|uni}string{be|le}(<b>string</b>, offset, length, terminateValue, *endian)<br><br>
543
+ pstring(<b>string</b>, offset, lengthWriteSize, *endian)<br><br>
544
+ pstring{1|2|4}{be|le}(<b>string</b>,offset, *endian)<br><br>
545
+ wpstring{be|le}(<b>string</b>, offset, lengthWriteSize, *endian)<br><br>
546
+ wpstring{1|2|4}{be|le}(<b>string</b>, offset, *endian)
547
+ </td>
548
+ <td>Based on above.<br><b>Note:</b> Presets use augments not a single object. Endian only needed when not part of function name. Does not override set endian.</td>
549
+ </tr>
550
+ </tbody>
551
+ </table>
552
+
553
+ ## Acknowledgements
554
+
555
+ This project was born from the desire to have a single library that could both read and write in binary with common named functions. Having been using tools like [Binary-parser](https://github.com/keichi/binary-parser), [QuickBMS](https://aluigi.altervista.org/quickbms.htm) and [010 Editor](https://www.sweetscape.com/010editor/) in the past, I wanted something I could translate quickly to a Node app and then use in a web site without having to redo work.
556
+
557
+ I'm happy to connect and grow this library if others find it useful. Pull requests or [bug reports](https://github.com/hearhellacopters/bireader/issues) are welcome!
558
+
559
+ ## License
560
+
561
+ [MIT](https://github.com/hearhellacopters/bireader/blob/main/LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bireader",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Read and write data in binary",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -18,6 +18,7 @@
18
18
  "keywords": [
19
19
  "buffer",
20
20
  "parser",
21
+ "reader",
21
22
  "binary",
22
23
  "decode",
23
24
  "unpack",