1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2018 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview Pack and unpack two's complement ints and unsigned ints. |
27
|
|
|
* @see https://github.com/rochars/byte-data |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
import TwosComplementBuffer from 'twos-complement-buffer'; |
31
|
|
|
import UintBuffer from 'uint-buffer'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A class to write and read two's complement ints and unsigned ints |
35
|
|
|
* to and from byte buffers. |
36
|
|
|
*/ |
37
|
|
|
export default class IntBuffer { |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param {number} bits The number of bits used by the integer. |
41
|
|
|
*/ |
42
|
|
|
constructor(theType) { |
43
|
|
|
/** @type {TwosComplementBuffer|UintBuffer} */ |
44
|
|
|
this.parser = theType.signed ? |
45
|
|
|
new TwosComplementBuffer(theType.bits) : new UintBuffer(theType.bits); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Write one unsigned integer to a byte buffer. |
50
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
51
|
|
|
* @param {number} num The number. |
52
|
|
|
* @param {number=} index The index being written in the byte buffer. |
53
|
|
|
* @return {number} The next index to write on the byte buffer. |
54
|
|
|
* @throws {Error} If num is NaN. |
55
|
|
|
* @throws {Error} On overflow. |
56
|
|
|
*/ |
57
|
|
|
pack(buffer, num, index=0) { |
58
|
|
|
return this.parser.pack(buffer, num, index); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Read one unsigned integer from a byte buffer. |
63
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
64
|
|
|
* @param {number=} index The index to read. |
65
|
|
|
* @return {number} The number. |
66
|
|
|
* @throws {Error} On overflow. |
67
|
|
|
*/ |
68
|
|
|
unpack(buffer, index=0) { |
69
|
|
|
return this.parser.unpack(buffer, index); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|