1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2017-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 Sserialize and deserialize integers and floats. |
27
|
|
|
* @see https://github.com/rochars/byte-data |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
import { IEEE754Buffer } from 'ieee754-buffer'; |
31
|
|
|
import { TwosComplementBuffer } from 'twos-complement-buffer'; |
32
|
|
|
import { UintBuffer } from 'uint-buffer'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A class to pack and unpack integers and floating-point numbers. |
36
|
|
|
* Signed integers are two's complement. |
37
|
|
|
* Floating-point numbers are IEEE 754 standard. |
38
|
|
|
*/ |
39
|
|
|
export default class NumberBuffer { |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Read one number from a byte buffer. |
43
|
|
|
* @param {number} bits The number of bits of the number. |
44
|
|
|
* @param {boolean} fp Tue for floating-point numbers. |
45
|
|
|
* @param {boolean} signed True for signed numbers. |
46
|
|
|
* @throws {Error} If the type definition is not valid. |
47
|
|
|
*/ |
48
|
|
|
constructor(bits, fp, signed) { |
49
|
|
|
/** @type {!Object} */ |
50
|
|
|
this.parser = this.getParser_(bits, fp, signed); |
51
|
|
|
/** @type {number} */ |
52
|
|
|
this.offset = Math.ceil(bits / 8); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Read one number from a byte buffer. |
57
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
58
|
|
|
* @param {number=} index The index to read. |
59
|
|
|
* @return {number} The number. |
60
|
|
|
* @throws {Error} On overflow. |
61
|
|
|
*/ |
62
|
|
|
unpack(buffer, index=0) { |
63
|
|
|
return this.parser.unpack(buffer, index); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Write one number to a byte buffer. |
68
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
69
|
|
|
* @param {number} num The number. |
70
|
|
|
* @param {number=} index The index being written in the byte buffer. |
71
|
|
|
* @return {number} The next index to write on the byte buffer. |
72
|
|
|
* @throws {Error} If num is NaN. |
73
|
|
|
* @throws {Error} On overflow. |
74
|
|
|
*/ |
75
|
|
|
pack(buffer, num, index=0) { |
76
|
|
|
return this.parser.pack(buffer, num, index); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return a parser for int, uint or fp numbers. |
81
|
|
|
* @param {number} bits The number of bits. |
82
|
|
|
* @return {!Object} |
83
|
|
|
* @throws {Error} If the type definition is not valid. |
84
|
|
|
* @private |
85
|
|
|
*/ |
86
|
|
|
getParser_(bits, fp, signed) { |
87
|
|
|
if (fp) { |
88
|
|
|
validateFloatType(bits); |
89
|
|
|
return this.getFPParser_(bits); |
90
|
|
|
} |
91
|
|
|
validateIntType(bits); |
92
|
|
|
return signed ? new TwosComplementBuffer(bits) : new UintBuffer(bits); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return a instance a parser for fp numbers. |
97
|
|
|
* @param {number} bits The number of bits. |
98
|
|
|
* @return {!Object} |
99
|
|
|
* @private |
100
|
|
|
*/ |
101
|
|
|
getFPParser_(bits) { |
102
|
|
|
if (bits === 16) { |
103
|
|
|
return new IEEE754Buffer(5, 11); |
104
|
|
|
} else if(bits === 32) { |
105
|
|
|
return new IEEE754Buffer(8, 23); |
106
|
|
|
} |
107
|
|
|
return new IEEE754Buffer(11, 52); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @type {string} The type definition error message. |
113
|
|
|
* @private |
114
|
|
|
*/ |
115
|
|
|
const TYPE_ERR = 'Unsupported type'; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Validate the type definition of floating-point numbers. |
119
|
|
|
* @param {number} bits The number of bits. |
120
|
|
|
* @throws {Error} If the type definition is not valid. |
121
|
|
|
* @private |
122
|
|
|
*/ |
123
|
|
|
function validateFloatType(bits) { |
124
|
|
|
if (!bits || bits !== 16 && bits !== 32 && bits !== 64) { |
125
|
|
|
throw new Error(TYPE_ERR + ': float, bits: ' + bits); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Validate the type definition of integers. |
131
|
|
|
* @param {number} bits The number of bits. |
132
|
|
|
* @throws {Error} If the type definition is not valid. |
133
|
|
|
* @private |
134
|
|
|
*/ |
135
|
|
|
function validateIntType(bits) { |
136
|
|
|
if (!bits || bits < 1 || bits > 53) { |
137
|
|
|
throw new Error(TYPE_ERR + ': int, bits: ' + bits); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|