|
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 bitPacker = require('./src/bit-packer'); |
|
12
|
|
|
let bitDepth = require('./src/bit-depth'); |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Find and return the start index of some string. |
|
16
|
|
|
* Return -1 if the string is not found. |
|
17
|
|
|
* @param {!Array<number>|Uint8Array} bytes Array of bytes. |
|
18
|
|
|
* @param {string} chunk Some string to look for. |
|
19
|
|
|
* @return {number} The start index of the first occurrence, -1 if not found |
|
20
|
|
|
*/ |
|
21
|
|
|
function findString(bytes, chunk) { |
|
22
|
|
|
let found = ""; |
|
23
|
|
|
for (let i = 0; i < bytes.length; i++) { |
|
24
|
|
|
found = fromBytes.fromBytes(bytes.slice(i, i + chunk.length), |
|
25
|
|
|
8, {"char": true}); |
|
26
|
|
|
if (found == chunk) { |
|
27
|
|
|
return i; |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
return -1; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Turn a number or string into a byte buffer. |
|
35
|
|
|
* @param {number|string} value The value. |
|
36
|
|
|
* @param {Object} type One of the available types. |
|
37
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
|
38
|
|
|
* @return {!Array<number>|!Array<string>} |
|
39
|
|
|
*/ |
|
40
|
|
|
function pack(value, type, base=10) { |
|
41
|
|
|
let theType = getSingleType(type, base); |
|
42
|
|
|
value = theType.char ? value[0] : value; |
|
43
|
|
|
return toBytes.toBytes(turnToArray(value), theType.bits, theType); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Turn a byte buffer into a readable value. |
|
48
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer An array of bytes. |
|
49
|
|
|
* @param {Object} type One of the available types. |
|
50
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
|
51
|
|
|
* @return {number|string} |
|
52
|
|
|
*/ |
|
53
|
|
|
function unpack(buffer, type, base=10) { |
|
54
|
|
|
let theType = getSingleType(type, base); |
|
55
|
|
|
return fromBytes.fromBytes(buffer, theType.bits, theType); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Turn a array of numbers into a byte buffer. |
|
60
|
|
|
* @param {!Array<number>} values The values. |
|
61
|
|
|
* @param {Object} type One of the available types. |
|
62
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
|
63
|
|
|
* @return {!Array<number>|!Array<string>} |
|
64
|
|
|
*/ |
|
65
|
|
|
function packArray(values, type, base=10) { |
|
66
|
|
|
let theType = getArrayType(type, base); |
|
67
|
|
|
return toBytes.toBytes(values, theType.bits, theType); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Turn a byte array into a sequence of readable values. |
|
72
|
|
|
* @param {!Array<number>|!Array<string>|Uint8Array} buffer The byte array. |
|
73
|
|
|
* @param {Object} type One of the available types. |
|
74
|
|
|
* @param {number} base The base of the input. Optional. Default is 10. |
|
75
|
|
|
* @return {!Array<number>|string} |
|
76
|
|
|
*/ |
|
77
|
|
|
function unpackArray(buffer, type, base=10) { |
|
78
|
|
|
let theType = getArrayType(type, base); |
|
79
|
|
|
return fromBytes.fromBytes(buffer, theType.bits, theType); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Make the type a single value type on the specified base. |
|
84
|
|
|
* @param {Object} type One of the available types. |
|
85
|
|
|
* @param {number} base The base of the input. |
|
86
|
|
|
* @return {Object} |
|
87
|
|
|
*/ |
|
88
|
|
|
function getSingleType(type, base) { |
|
89
|
|
|
let theType = Object.assign({}, type); |
|
90
|
|
|
theType.base = base; |
|
91
|
|
|
theType.single = true; |
|
92
|
|
|
return theType; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Make the type a array with the specified base. |
|
97
|
|
|
* @param {Object} type One of the available types. |
|
98
|
|
|
* @param {number} base The base of the input. |
|
99
|
|
|
* @return {Object} |
|
100
|
|
|
*/ |
|
101
|
|
|
function getArrayType(type, base) { |
|
102
|
|
|
let theType = Object.assign({}, type); |
|
103
|
|
|
theType.base = base; |
|
104
|
|
|
theType.single = false; |
|
105
|
|
|
return theType; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Make a single value an array in case it is not. |
|
110
|
|
|
* If the value is a string it stays a string. |
|
111
|
|
|
* @param {!Array<number>|number|string} values The value or values. |
|
112
|
|
|
* @return {!Array<number>|string} |
|
113
|
|
|
*/ |
|
114
|
|
|
function turnToArray(values) { |
|
115
|
|
|
if (!Array.isArray(values) && typeof values != "string") { |
|
116
|
|
|
values = [values]; |
|
117
|
|
|
} |
|
118
|
|
|
return values; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// interface |
|
122
|
|
|
module.exports.pack = pack; |
|
123
|
|
|
module.exports.unpack = unpack; |
|
124
|
|
|
module.exports.packArray = packArray; |
|
125
|
|
|
module.exports.unpackArray = unpackArray; |
|
126
|
|
|
|
|
127
|
|
|
// types |
|
128
|
|
|
module.exports.chr = {"bits": 8, "char": true, "single": true}; |
|
129
|
|
|
module.exports.bool = {"bits": 1, "single": true}; |
|
130
|
|
|
module.exports.int2 = {"bits": 2, "signed": true, "single": true}; |
|
131
|
|
|
module.exports.uInt2 = {"bits": 2, "single": true}; |
|
132
|
|
|
module.exports.int4 = {"bits": 4, "signed": true, "single": true}; |
|
133
|
|
|
module.exports.uInt4 = {"bits": 4, "single": true}; |
|
134
|
|
|
module.exports.int8 = {"bits": 8, "signed": true, "single": true}; |
|
135
|
|
|
module.exports.uInt8 = {"bits": 8, "single": true}; |
|
136
|
|
|
module.exports.int16 = {"bits": 16, "signed": true, "single": true}; |
|
137
|
|
|
module.exports.uInt16 = {"bits": 16, "single": true}; |
|
138
|
|
|
module.exports.float16 = {"bits": 16, "float": true, "single": true}; |
|
139
|
|
|
module.exports.int24 = {"bits": 24, "signed": true, "single": true}; |
|
140
|
|
|
module.exports.uInt24 = {"bits": 24, "single": true}; |
|
141
|
|
|
module.exports.int32 = {"bits": 32, "signed": true, "single": true}; |
|
142
|
|
|
module.exports.uInt32 = {"bits": 32, "single": true}; |
|
143
|
|
|
module.exports.float32 = {"bits": 32, "float": true, "single": true}; |
|
144
|
|
|
module.exports.int40 = {"bits": 40, "signed": true, "single": true}; |
|
145
|
|
|
module.exports.uInt40 = {"bits": 40, "single": true}; |
|
146
|
|
|
module.exports.int48 = {"bits": 48, "signed": true, "single": true}; |
|
147
|
|
|
module.exports.uInt48 = {"bits": 48, "single": true}; |
|
148
|
|
|
module.exports.float64 = {"bits": 64, "float": true, "single": true}; |
|
149
|
|
|
|
|
150
|
|
|
module.exports.findString = findString; |
|
151
|
|
|
module.exports.toBytes = toBytes.toBytes; |
|
152
|
|
|
module.exports.fromBytes = fromBytes.fromBytes; |
|
153
|
|
|
module.exports.packBooleans = bitPacker.packBooleans; |
|
154
|
|
|
module.exports.unpackBooleans = bitPacker.unpackBooleans; |
|
155
|
|
|
module.exports.packCrumbs = bitPacker.packCrumbs; |
|
156
|
|
|
module.exports.unpackCrumbs = bitPacker.unpackCrumbs; |
|
157
|
|
|
module.exports.packNibbles = bitPacker.packNibbles; |
|
158
|
|
|
module.exports.unpackNibbles = bitPacker.unpackNibbles; |
|
159
|
|
|
module.exports.BitDepthOffsets = bitDepth.BitDepthOffsets; |
|
160
|
|
|
module.exports.BitDepthMaxValues = bitDepth.BitDepthMaxValues; |
|
161
|
|
|
|