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 {Object} type One of the available types. |
15
|
|
|
* @return {!Array<number>|number|string} |
16
|
|
|
*/ |
17
|
|
|
function fromBytes(buffer, type) { |
18
|
|
|
let bitDepth = type.bits; |
19
|
|
|
helpers.fixFloat16Endianness(buffer, type); |
20
|
|
|
helpers.makeBigEndian(buffer, type.be, bitDepth); |
21
|
|
|
bytesToInt(buffer, type.base); |
22
|
|
|
let values = readBytes( |
23
|
|
|
buffer, |
24
|
|
|
type, |
25
|
|
|
getBitReader(type) |
26
|
|
|
); |
27
|
|
|
if (type.single) { |
28
|
|
|
values = getSingleValue(values, type); |
29
|
|
|
} |
30
|
|
|
return values; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return a function to read binary data. |
35
|
|
|
* @param {Object} type One of the available types. |
36
|
|
|
* @return {Function} |
37
|
|
|
*/ |
38
|
|
|
function getBitReader(type) { |
39
|
|
|
return type.char ? |
40
|
|
|
reader.readChar : reader[getReaderName(type.bits, type.float)]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Return the first value from the result value array. |
45
|
|
|
* @param {!Array<number>|string} values The values. |
46
|
|
|
* @param {Object} type One of the available types. |
47
|
|
|
* @return {number|string} |
48
|
|
|
*/ |
49
|
|
|
function getSingleValue(values, type) { |
50
|
|
|
if (type.char) { |
51
|
|
|
values = values.slice(0, type.bits / 8); |
52
|
|
|
} else { |
53
|
|
|
values = values[0]; |
54
|
|
|
} |
55
|
|
|
return values; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
60
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
61
|
|
|
* @param {Object} type The type. |
62
|
|
|
* @param {Function} bitReader The function to read the bytes. |
63
|
|
|
* @return {!Array<number>|string} |
64
|
|
|
*/ |
65
|
|
|
function readBytes(bytes, type, bitReader) { |
66
|
|
|
let values = []; |
67
|
|
|
let i = 0; |
68
|
|
|
let offset = type.bits < 8 ? 1 : type.bits / 8; |
69
|
|
|
let len = bytes.length - (offset -1); |
70
|
|
|
let maxBitDepthValue = bitDepths.BitDepthMaxValues[type.bits]; |
71
|
|
|
let signFunction = type.signed && !type.float ? |
72
|
|
|
helpers.signed : function(x){return x;}; |
73
|
|
|
while (i < len) { |
74
|
|
|
values.push(signFunction(bitReader(bytes, i, type), maxBitDepthValue)); |
75
|
|
|
i += offset; |
76
|
|
|
} |
77
|
|
|
if (type.char) { |
78
|
|
|
values = values.join(""); |
79
|
|
|
} |
80
|
|
|
return values; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Build a bit reading function name based on the arguments. |
85
|
|
|
* @param {number} bits The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
86
|
|
|
* @param {boolean} float True if the values are IEEE floating point numbers. |
87
|
|
|
* @return {string} |
88
|
|
|
*/ |
89
|
|
|
function getReaderName(bits, float) { |
90
|
|
|
return 'read' + (bits < 8 ? 8 : bits) + |
91
|
|
|
'Bit' + (float ? "Float" : ""); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Turn bytes to base 10. |
96
|
|
|
* @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings. |
97
|
|
|
* @param {number} base The base. |
98
|
|
|
*/ |
99
|
|
|
function bytesToInt(bytes, base) { |
100
|
|
|
if (base != 10) { |
101
|
|
|
let i = 0; |
102
|
|
|
let len = bytes.length; |
103
|
|
|
while(i < len) { |
104
|
|
|
bytes[i] = parseInt(bytes[i], base); |
105
|
|
|
i++; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
module.exports.fromBytes = fromBytes; |
111
|
|
|
|