|
1
|
|
|
/** |
|
2
|
|
|
* from-bytes: Numbers and strings from bytes. |
|
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
|
4
|
|
|
* https://github.com/rochars/byte-data |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
const Type = require("../src/type"); |
|
8
|
|
|
const endianness = require("endianness"); |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Turn a byte buffer into what the bytes represent. |
|
12
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes. |
|
13
|
|
|
* @param {Object} type One of the available types. |
|
14
|
|
|
* @return {!Array<number>|number|string} |
|
15
|
|
|
*/ |
|
16
|
|
|
function fromBytes(buffer, type) { |
|
17
|
|
|
if (type.be) { |
|
18
|
|
|
endianness(buffer, type.offset); |
|
19
|
|
|
} |
|
20
|
|
|
if (type.base != 10) { |
|
21
|
|
|
bytesFromBase(buffer, type.base); |
|
22
|
|
|
} |
|
23
|
|
|
return readBytes(buffer, type); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Turn numbers and strings to bytes. |
|
28
|
|
|
* @param {!Array<number>|number|string} values The data. |
|
29
|
|
|
* @param {Object} type One of the available types. |
|
30
|
|
|
* @return {!Array<number>|!Array<string>} the data as a byte buffer. |
|
31
|
|
|
*/ |
|
32
|
|
|
function toBytes(values, type) { |
|
33
|
|
|
let bytes = writeBytes(values, type); |
|
34
|
|
|
if (type.be) { |
|
35
|
|
|
endianness(bytes, type.offset); |
|
36
|
|
|
} |
|
37
|
|
|
if (type.base != 10) { |
|
38
|
|
|
bytesToBase(bytes, type.base); |
|
39
|
|
|
formatOutput(bytes, type); |
|
40
|
|
|
} |
|
41
|
|
|
return bytes; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
|
46
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
47
|
|
|
* @param {Object} type The type. |
|
48
|
|
|
* @return {!Array<number>|string} |
|
49
|
|
|
*/ |
|
50
|
|
|
function readBytes(bytes, type) { |
|
51
|
|
|
let values = []; |
|
52
|
|
|
let i = 0; |
|
53
|
|
|
let len = bytes.length - (type.offset - 1); |
|
54
|
|
|
while (i < len) { |
|
55
|
|
|
values.push( |
|
56
|
|
|
type.overflow(type.sign(type.reader(bytes, i, type))) |
|
57
|
|
|
); |
|
58
|
|
|
i += type.offset; |
|
59
|
|
|
} |
|
60
|
|
|
if (type.char) { |
|
61
|
|
|
values = values.join(""); |
|
62
|
|
|
} |
|
63
|
|
|
return values; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Write values as bytes. |
|
68
|
|
|
* @param {!Array<number>|number|string} values The data. |
|
69
|
|
|
* @param {Object} type One of the available types. |
|
70
|
|
|
* @return {!Array<number>} the bytes. |
|
71
|
|
|
*/ |
|
72
|
|
|
function writeBytes(values, type) { |
|
73
|
|
|
let i = 0; |
|
74
|
|
|
let j = 0; |
|
75
|
|
|
let len = values.length; |
|
76
|
|
|
let bytes = []; |
|
77
|
|
|
while (i < len) { |
|
78
|
|
|
j = type.writer(bytes, type.overflow(values[i]), j); |
|
79
|
|
|
i++; |
|
80
|
|
|
} |
|
81
|
|
|
return bytes; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get the full type spec for the reading/writing. |
|
86
|
|
|
* @param {Object} type One of the available types. |
|
87
|
|
|
* @param {number} base The base of the input. |
|
88
|
|
|
* @return {Object} |
|
89
|
|
|
*/ |
|
90
|
|
|
function getType(type, base) { |
|
91
|
|
|
let theType = Object.assign(new Type({}), type); |
|
92
|
|
|
theType.base = base; |
|
93
|
|
|
return theType; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Turn bytes to base 10 from base 2 or 16. |
|
98
|
|
|
* @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings. |
|
99
|
|
|
* @param {number} base The base. |
|
100
|
|
|
*/ |
|
101
|
|
|
function bytesFromBase(bytes, base) { |
|
102
|
|
|
let i = 0; |
|
103
|
|
|
let len = bytes.length; |
|
104
|
|
|
while(i < len) { |
|
105
|
|
|
bytes[i] = parseInt(bytes[i], base); |
|
106
|
|
|
i++; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Turn the output to the correct base. |
|
112
|
|
|
* @param {Array} bytes The bytes. |
|
113
|
|
|
* @param {Object} type The type. |
|
114
|
|
|
*/ |
|
115
|
|
|
function formatOutput(bytes, type) { |
|
116
|
|
|
let i = 0; |
|
117
|
|
|
let len = bytes.length; |
|
118
|
|
|
let offset = getOutputByteOffset(type); |
|
119
|
|
|
while(i < len) { |
|
120
|
|
|
bytes[i] = Array(offset - bytes[i].length).join("0") + bytes[i]; |
|
121
|
|
|
i++; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the number of chars a non-string output should have |
|
127
|
|
|
* according to the number of bits used by the type. |
|
128
|
|
|
* @param {Object} type The type. |
|
129
|
|
|
* @return {number} |
|
130
|
|
|
*/ |
|
131
|
|
|
function getOutputByteOffset(type) { |
|
132
|
|
|
return (type.base == 2 ? 8 : 2) + 1; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Turn bytes from base 10 to base 2 or 16. |
|
137
|
|
|
* @param {!Array<string>|Array<number>} bytes The bytes. |
|
138
|
|
|
* @param {number} base The base. |
|
139
|
|
|
*/ |
|
140
|
|
|
function bytesToBase(bytes, base) { |
|
141
|
|
|
let i = 0; |
|
142
|
|
|
let len = bytes.length; |
|
143
|
|
|
while (i < len) { |
|
144
|
|
|
bytes[i] = bytes[i].toString(base); |
|
145
|
|
|
i++; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
exports.getType = getType; |
|
150
|
|
|
exports.toBytes = toBytes; |
|
151
|
|
|
exports.fromBytes = fromBytes; |
|
152
|
|
|
|