1
|
|
|
/* |
2
|
|
|
* from-bytes: convert bytes to numbers and strings. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
const reader = require("../src/read-bytes.js"); |
8
|
|
|
const bitDepths = require("../src/bit-depth.js"); |
9
|
|
|
const helpers = require("../src/helpers.js"); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Turn a byte buffer into what the bytes represent. |
13
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes. |
14
|
|
|
* @param {number} bitDepth The bit depth of the data. |
15
|
|
|
* Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. |
16
|
|
|
* @param {Object} options The options. They are: |
17
|
|
|
* - "signed": If the numbers are signed. Default is false (unsigned). |
18
|
|
|
* - "float": True for floating point numbers. Default is false. |
19
|
|
|
* This option is available for 16, 32 and 64-bit numbers. |
20
|
|
|
* - "base": The base of the input. Default is 10. Can be 2, 10 or 16. |
21
|
|
|
* - "char": If the bytes represent a string. Default is false. |
22
|
|
|
* - "be": If the values are big endian. Default is false (little endian). |
23
|
|
|
* - "single": If it should return a single value instead of an array. |
24
|
|
|
* Default is false. |
25
|
|
|
* @return {!Array<number>|string} |
26
|
|
|
*/ |
27
|
|
|
function fromBytes(buffer, bitDepth, options={"base": 10}) { |
28
|
|
|
if (bitDepth == 64) { |
29
|
|
|
options.float = true; |
30
|
|
|
} |
31
|
|
|
if (options.float) { |
32
|
|
|
options.signed = true; |
33
|
|
|
} |
34
|
|
|
options.bits = bitDepth; |
35
|
|
|
helpers.fixFloat16Endianness(buffer, options); |
36
|
|
|
helpers.makeBigEndian(buffer, options.be, bitDepth); |
37
|
|
|
helpers.bytesToInt(buffer, options.base); |
38
|
|
|
let values = readBytes( |
39
|
|
|
buffer, |
40
|
|
|
options, |
41
|
|
|
getBitReader(bitDepth, options.float, options.char) |
42
|
|
|
); |
43
|
|
|
if (options.char) { |
44
|
|
|
values = values.join(""); |
45
|
|
|
} |
46
|
|
|
if (options.single) { |
47
|
|
|
values = values[0]; |
48
|
|
|
} |
49
|
|
|
return values; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
54
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
55
|
|
|
* @param {Object} type The type. |
56
|
|
|
* @param {Function} bitReader The function to read the bytes. |
57
|
|
|
* @return {!Array<number>|string} |
58
|
|
|
*/ |
59
|
|
|
function readBytes(bytes, type, bitReader) { |
60
|
|
|
let values = []; |
61
|
|
|
let i = 0; |
62
|
|
|
let j = 0; |
63
|
|
|
let offset = bitDepths.BitDepthOffsets[type.bits]; |
64
|
|
|
let len = bytes.length - (offset -1); |
65
|
|
|
let maxBitDepthValue = bitDepths.BitDepthMaxValues[type.bits]; |
66
|
|
|
let signFunction = type.signed && !type.float ? |
67
|
|
|
helpers.signed : function(x,y){return x;}; |
|
|
|
|
68
|
|
|
while (i < len) { |
69
|
|
|
values[j] = signFunction(bitReader(bytes, i), maxBitDepthValue); |
70
|
|
|
i += offset; |
71
|
|
|
j++; |
72
|
|
|
} |
73
|
|
|
return values; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return a function to read binary data. |
78
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
79
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
80
|
|
|
* @param {boolean} isChar True if it is a string. |
81
|
|
|
* @return {Function} |
82
|
|
|
*/ |
83
|
|
|
function getBitReader(bitDepth, isFloat, isChar) { |
84
|
|
|
let bitReader; |
85
|
|
|
if (isChar) { |
86
|
|
|
bitReader = reader.readChar; |
87
|
|
|
} else { |
88
|
|
|
bitReader = reader[getReaderFunctionName(bitDepth, isFloat)]; |
89
|
|
|
} |
90
|
|
|
return bitReader; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Build a bit reading function name based on the arguments. |
95
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
96
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
97
|
|
|
* @return {string} |
98
|
|
|
*/ |
99
|
|
|
function getReaderFunctionName(bitDepth, isFloat) { |
100
|
|
|
return 'read' + |
101
|
|
|
((bitDepth == 2 || bitDepth == 4) ? 8 : bitDepth) + |
102
|
|
|
'Bit' + |
103
|
|
|
(isFloat ? "Float" : ""); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
module.exports.fromBytes = fromBytes; |
107
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.