1
|
|
|
/* |
2
|
|
|
* byte-data |
3
|
|
|
* Readable data to and from byte buffers. |
4
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
5
|
|
|
* https://github.com/rochars/byte-data |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
let toBytes = require("../src/to-bytes"); |
10
|
|
|
let fromBytes = require("../src/from-bytes"); |
11
|
|
|
let helpers = require("../src/helpers"); |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Turn a number or string into a byte buffer. |
15
|
|
|
* @param {number|string} value The value. |
16
|
|
|
* @param {Object} type One of the available types. |
17
|
|
|
* @param {number} base The base of the output. Optional. Default is 10. |
18
|
|
|
* @return {!Array<number>|!Array<string>} |
19
|
|
|
*/ |
20
|
|
|
function pack(value, type, base=10) { |
21
|
|
|
let theType = helpers.getType(type, base, true); |
22
|
|
|
value = theType.char ? value.slice(0, type.bits / 8) : value; |
23
|
|
|
return toBytes.toBytes(helpers.turnToArray(value), theType); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Turn a byte buffer into a readable value. |
28
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes. |
29
|
|
|
* @param {Object} type One of the available types. |
30
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
31
|
|
|
* @return {number|string} |
32
|
|
|
*/ |
33
|
|
|
function unpack(buffer, type, base=10) { |
34
|
|
|
return fromBytes.fromBytes(buffer, helpers.getType(type, base, true)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Turn a array of numbers into a byte buffer. |
39
|
|
|
* @param {!Array<number>|string} values The values. |
40
|
|
|
* @param {Object} type One of the available types. |
41
|
|
|
* @param {number} base The base of the output. Optional. Default is 10. |
42
|
|
|
* @return {!Array<number>|!Array<string>} |
43
|
|
|
*/ |
44
|
|
|
function packArray(values, type, base=10) { |
45
|
|
|
return toBytes.toBytes(values, helpers.getType(type, base, false)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Turn a byte array into a sequence of readable values. |
50
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte array. |
51
|
|
|
* @param {Object} type One of the available types. |
52
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
53
|
|
|
* @return {!Array<number>|string} |
54
|
|
|
*/ |
55
|
|
|
function unpackArray(buffer, type, base=10) { |
56
|
|
|
return fromBytes.fromBytes(buffer, helpers.getType(type, base, false)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Find and return the start index of some string. |
61
|
|
|
* Return -1 if the string is not found. |
62
|
|
|
* @param {!Array<number>|Uint8Array} buffer A byte buffer. |
63
|
|
|
* @param {string} text Some string to look for. |
64
|
|
|
* @return {number} The start index of the first occurrence, -1 if not found |
65
|
|
|
*/ |
66
|
|
|
function findString(buffer, text) { |
67
|
|
|
let found = ""; |
68
|
|
|
for (let i = 0; i < buffer.length; i++) { |
69
|
|
|
found = unpack( |
70
|
|
|
buffer.slice(i, i + text.length + 1), |
71
|
|
|
{"bits": text.length * 8, "char": true}); |
72
|
|
|
if (found == text) { |
73
|
|
|
return i; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return -1; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Turn a struct into a byte buffer. |
81
|
|
|
* A struct is an array of values of not necessarily the same type. |
82
|
|
|
* @param {Array} struct The struct values. |
83
|
|
|
* @param {!Array<Object>} def The struct type definition. |
84
|
|
|
* @param {number} base The base of the output. Optional. Default is 10. |
85
|
|
|
* @return {!Array<number>|!Array<string>} |
86
|
|
|
*/ |
87
|
|
|
function packStruct(struct, def, base=10) { |
88
|
|
|
if (struct.length < def.length) { |
89
|
|
|
return []; |
90
|
|
|
} |
91
|
|
|
let bytes = []; |
92
|
|
|
for (let i = 0; i < def.length; i++) { |
93
|
|
|
bytes = bytes.concat(pack(struct[i], def[i], base)); |
94
|
|
|
} |
95
|
|
|
return bytes; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Turn a byte buffer into a structure. |
100
|
|
|
* A struct is an array of values of not necessarily the same type. |
101
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte buffer. |
102
|
|
|
* @param {!Array<Object>} def The struct type definition. |
103
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
104
|
|
|
* @return {Array} |
105
|
|
|
*/ |
106
|
|
|
function unpackStruct(buffer, def, base=10) { |
107
|
|
|
if (buffer.length < getStructBits(def)) { |
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
let struct = []; |
111
|
|
|
let i = 0; |
112
|
|
|
let j = 0; |
113
|
|
|
while (i < def.length) { |
114
|
|
|
let bits = def[i].bits < 8 ? 1 : def[i].bits / 8; |
115
|
|
|
struct = struct.concat( |
116
|
|
|
unpack(buffer.slice(j, j + bits), def[i], base) |
117
|
|
|
); |
118
|
|
|
j += bits; |
119
|
|
|
i++; |
120
|
|
|
} |
121
|
|
|
return struct; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
function getStructBits(def) { |
125
|
|
|
let bits = 0; |
126
|
|
|
for (let i = 0; i < def.length; i++) { |
127
|
|
|
bits += def[i].bits / 8; |
128
|
|
|
} |
129
|
|
|
return bits; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
module.exports.pack = pack; |
133
|
|
|
module.exports.findString = findString; |
134
|
|
|
module.exports.unpack = unpack; |
135
|
|
|
module.exports.packArray = packArray; |
136
|
|
|
module.exports.unpackArray = unpackArray; |
137
|
|
|
module.exports.unpackStruct = unpackStruct; |
138
|
|
|
module.exports.packStruct = packStruct; |
139
|
|
|
|