1
|
|
|
/* |
2
|
|
|
* helpers: functions to work with bytes and byte arrays. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
const endianness = require("endianness"); |
8
|
|
|
const bitDepths = require("../src/bit-depth.js"); |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Padding for binary strings. |
12
|
|
|
* @param {!Array<string>} bytes The bytes as binary strings. |
13
|
|
|
* @param {number} base The base. |
14
|
|
|
* @param {number} index The byte to pad. |
15
|
|
|
*/ |
16
|
|
|
function padding(bytes, base, index) { |
17
|
|
|
bytes[index] = bytePadding(bytes[index], base); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Padding with 0s for byte strings. |
22
|
|
|
* @param {string} byte The byte as a binary or hex string. |
23
|
|
|
* @param {number} base The base. |
24
|
|
|
* @returns {string} The padded byte. |
25
|
|
|
*/ |
26
|
|
|
function bytePadding(byte, base) { |
27
|
|
|
let offset = byte.length + 1; |
28
|
|
|
if (base == 2) { |
29
|
|
|
offset = 8; |
30
|
|
|
} else if (base == 16) { |
31
|
|
|
offset = 2; |
32
|
|
|
} |
33
|
|
|
return lPadZeros(byte, offset); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Fix the size of nibbles. |
38
|
|
|
* @param {!Array<string>} nibbles The nibble as a binary or hex string. |
39
|
|
|
* @param {number} base The base. |
40
|
|
|
* @param {number} index The nibble offset. |
41
|
|
|
*/ |
42
|
|
|
function paddingNibble(nibbles, base, index) { |
43
|
|
|
if (base == 2 && nibbles[index].length < 4) { |
44
|
|
|
nibbles[index] = |
45
|
|
|
new Array((5 - nibbles[index].length)).join("0") + nibbles[index]; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Fix the size of crumbs. |
51
|
|
|
* @param {!Array<string>} crumbs The nibble as a binary or hex string. |
52
|
|
|
* @param {number} base The base. |
53
|
|
|
* @param {number} index The nibble offset. |
54
|
|
|
*/ |
55
|
|
|
function paddingCrumb(crumbs, base, index) { |
56
|
|
|
if ((base == 2 || base == 16) && crumbs[index].length < 2) { |
57
|
|
|
crumbs[index] = '0' + crumbs[index]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Pad a string with zeros to the left. |
63
|
|
|
* TODO: This should support both arrays and strings. |
64
|
|
|
* @param {string} value The string (representing a binary or hex value). |
65
|
|
|
* @param {number} numZeros the max number of zeros. |
66
|
|
|
* For 1 binary byte string it should be 8. |
67
|
|
|
*/ |
68
|
|
|
function lPadZeros(value, numZeros) { |
69
|
|
|
let i = 0; |
|
|
|
|
70
|
|
|
while (value.length < numZeros) { |
71
|
|
|
value = '0' + value; |
72
|
|
|
} |
73
|
|
|
return value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Pad a array with zeros to the right. |
78
|
|
|
* @param {!Array<number>} byteArray The array. |
79
|
|
|
* @param {number} numZeros the max number of zeros. |
80
|
|
|
* For 1 binary byte string it should be 8. |
81
|
|
|
* TODO: better explanation of numZeros |
82
|
|
|
*/ |
83
|
|
|
function fixByteArraySize(byteArray, numZeros) { |
84
|
|
|
let i = 0; |
85
|
|
|
let fix = byteArray.length % numZeros; |
86
|
|
|
if (fix) { |
87
|
|
|
fix = (fix - numZeros) * -1; |
88
|
|
|
while(i < fix) { |
89
|
|
|
byteArray.push(0); |
90
|
|
|
i++; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Swap the endianness to big endian. |
97
|
|
|
* @param {!Array<number>} bytes The values. |
98
|
|
|
* @param {boolean} isBigEndian True if the bytes should be big endian. |
99
|
|
|
* @param {number} bitDepth The bitDepth of the data. |
100
|
|
|
*/ |
101
|
|
|
function makeBigEndian(bytes, isBigEndian, bitDepth) { |
102
|
|
|
if (isBigEndian) { |
103
|
|
|
endianness.endianness(bytes, bitDepths.BitDepthOffsets[bitDepth]); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Turn bytes to base 2, 10 or 16. |
109
|
|
|
* @param {!Array<string>|!Array<number>} bytes The bytes. |
110
|
|
|
* @param {number} base The base. |
111
|
|
|
* @param {Function} padFunction The function to use for padding. |
112
|
|
|
*/ |
113
|
|
|
function bytesToBase(bytes, base, padFunction=padding) { |
114
|
|
|
if (base != 10) { |
115
|
|
|
let i = 0; |
116
|
|
|
let len = bytes.length; |
117
|
|
|
while (i < len) { |
118
|
|
|
bytes[i] = bytes[i].toString(base); |
119
|
|
|
padFunction(bytes, base, i); |
120
|
|
|
i++; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Turn the output to the correct base. |
127
|
|
|
* @param {!Array<number>} bytes The bytes. |
128
|
|
|
* @param {number} bitDepth The bit depth of the data. |
129
|
|
|
* @param {number} base The desired base for the output data. |
130
|
|
|
*/ |
131
|
|
|
function outputToBase(bytes, bitDepth, base) { |
132
|
|
|
if (bitDepth == 4) { |
133
|
|
|
bytesToBase(bytes, base, paddingNibble); |
134
|
|
|
} else if (bitDepth == 2) { |
135
|
|
|
bytesToBase(bytes, base, paddingCrumb); |
136
|
|
|
} else if(bitDepth == 1) { |
137
|
|
|
bytesToBase(bytes, base, function(){}); |
138
|
|
|
}else { |
139
|
|
|
bytesToBase(bytes, base); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Make a single value an array in case it is not. |
145
|
|
|
* If the value is a string it stays a string. |
146
|
|
|
* @param {!Array<number>|number|string} values The value or values. |
147
|
|
|
* @return {!Array<number>|string} |
148
|
|
|
*/ |
149
|
|
|
function turnToArray(values) { |
150
|
|
|
if (!Array.isArray(values) && typeof values != "string") { |
151
|
|
|
values = [values]; |
152
|
|
|
} |
153
|
|
|
return values; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Turn a unsigned number to a signed number. |
158
|
|
|
* @param {number} number The number. |
159
|
|
|
* @param {number} maxValue The max range for the number bit depth. |
160
|
|
|
*/ |
161
|
|
|
function signed(number, maxValue) { |
162
|
|
|
if (number > parseInt(maxValue / 2, 10) - 1) { |
163
|
|
|
number -= maxValue; |
164
|
|
|
} |
165
|
|
|
return number; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Turn bytes to base 10. |
170
|
|
|
* @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings. |
171
|
|
|
* @param {number} base The base. |
172
|
|
|
*/ |
173
|
|
|
function bytesToInt(bytes, base) { |
174
|
|
|
if (base != 10) { |
175
|
|
|
let i = 0; |
176
|
|
|
let len = bytes.length; |
177
|
|
|
while(i < len) { |
178
|
|
|
bytes[i] = parseInt(bytes[i], base); |
179
|
|
|
i++; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
module.exports.makeBigEndian = makeBigEndian; |
185
|
|
|
module.exports.bytesToBase = bytesToBase; |
186
|
|
|
module.exports.outputToBase = outputToBase; |
187
|
|
|
module.exports.turnToArray = turnToArray; |
188
|
|
|
module.exports.signed = signed; |
189
|
|
|
module.exports.bytesToInt = bytesToInt; |
190
|
|
|
module.exports.fixByteArraySize = fixByteArraySize; |
191
|
|
|
module.exports.padding = padding; |
192
|
|
|
module.exports.paddingNibble = paddingNibble; |
193
|
|
|
module.exports.paddingCrumb = paddingCrumb; |
194
|
|
|
module.exports.bytePadding = bytePadding; |
195
|
|
|
module.exports.lPadZeros = lPadZeros; |
196
|
|
|
|