|
1
|
|
|
// Type definitions for byte-data 16.0 |
|
2
|
|
|
// Project: https://github.com/rochars/byte-data |
|
3
|
|
|
// Definitions by: Rafael da Silva Rocha <https://github.com/rochars> |
|
4
|
|
|
// Definitions: https://github.com/rochars/byte-data |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Read a string of UTF-8 characters from a byte buffer. |
|
8
|
|
|
* @param {!Uint8Array|!Array<number>} buffer A byte buffer. |
|
9
|
|
|
* @param {number=} index The buffer index to start reading. |
|
10
|
|
|
* @param {number=} end The buffer index to stop reading, non inclusive. |
|
11
|
|
|
* Assumes buffer length if undefined. |
|
12
|
|
|
* @return {string} |
|
13
|
|
|
*/ |
|
14
|
|
|
export function unpackString( |
|
15
|
|
|
buffer: Uint8Array|number[], |
|
16
|
|
|
index?: number, |
|
17
|
|
|
end?: number): string; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Write a string of UTF-8 characters as a byte buffer. |
|
21
|
|
|
* @param {string} str The string to pack. |
|
22
|
|
|
* @return {!Array<number>} The UTF-8 string bytes. |
|
23
|
|
|
*/ |
|
24
|
|
|
export function packString( |
|
25
|
|
|
str: string): number[]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Write a string of UTF-8 characters to a byte buffer. |
|
29
|
|
|
* @param {string} str The string to pack. |
|
30
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
31
|
|
|
* @param {number=} index The buffer index to start writing. |
|
32
|
|
|
* Assumes zero if undefined. |
|
33
|
|
|
* @return {number} The next index to write in the buffer. |
|
34
|
|
|
*/ |
|
35
|
|
|
export function packStringTo( |
|
36
|
|
|
str: string, |
|
37
|
|
|
buffer: Uint8Array|number[], |
|
38
|
|
|
index?: number): number; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Pack a number as a byte buffer. |
|
42
|
|
|
* @param {number} value The number. |
|
43
|
|
|
* @param {!Object} theType The type definition. |
|
44
|
|
|
* @return {!Array<number>} The packed value. |
|
45
|
|
|
* @throws {Error} If the type definition is not valid. |
|
46
|
|
|
* @throws {Error} If the value is not valid. |
|
47
|
|
|
*/ |
|
48
|
|
|
export function pack( |
|
49
|
|
|
value: number, |
|
50
|
|
|
theType: object): number[]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Pack a number to a byte buffer. |
|
54
|
|
|
* @param {number} value The value. |
|
55
|
|
|
* @param {!Object} theType The type definition. |
|
56
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
57
|
|
|
* @param {number=} index The buffer index to write. Assumes 0 if undefined. |
|
58
|
|
|
* @return {number} The next index to write. |
|
59
|
|
|
* @throws {Error} If the type definition is not valid. |
|
60
|
|
|
* @throws {Error} If the value is not valid. |
|
61
|
|
|
*/ |
|
62
|
|
|
export function packTo( |
|
63
|
|
|
value: number, |
|
64
|
|
|
theType: object, |
|
65
|
|
|
buffer: Uint8Array|number[], |
|
66
|
|
|
index?: number): number; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Pack an array of numbers as a byte buffer. |
|
70
|
|
|
* @param {!Array<number>|!TypedArray} values The values. |
|
71
|
|
|
* @param {!Object} theType The type definition. |
|
72
|
|
|
* @return {!Array<number>} The packed values. |
|
73
|
|
|
* @throws {Error} If the type definition is not valid. |
|
74
|
|
|
* @throws {Error} If any of the values are not valid. |
|
75
|
|
|
*/ |
|
76
|
|
|
export function packArray( |
|
77
|
|
|
values: number[]|ArrayBufferView, |
|
78
|
|
|
theType: object): number[]; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Pack a array of numbers to a byte buffer. |
|
82
|
|
|
* @param {!Array<number>|!TypedArray} values The value. |
|
83
|
|
|
* @param {!Object} theType The type definition. |
|
84
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The output buffer. |
|
85
|
|
|
* @param {number=} index The buffer index to start writing. |
|
86
|
|
|
* Assumes zero if undefined. |
|
87
|
|
|
* @return {number} The next index to write. |
|
88
|
|
|
* @throws {Error} If the type definition is not valid. |
|
89
|
|
|
* @throws {Error} If the value is not valid. |
|
90
|
|
|
*/ |
|
91
|
|
|
export function packArrayTo( |
|
92
|
|
|
values: number[]|ArrayBufferView, |
|
93
|
|
|
theType: object, |
|
94
|
|
|
buffer: Uint8Array|number[], |
|
95
|
|
|
index?: number): number; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Unpack a number from a byte buffer. |
|
99
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
100
|
|
|
* @param {!Object} theType The type definition. |
|
101
|
|
|
* @param {number=} index The buffer index to read. Assumes zero if undefined. |
|
102
|
|
|
* @return {number} |
|
103
|
|
|
* @throws {Error} If the type definition is not valid |
|
104
|
|
|
* @throws {Error} On bad buffer length. |
|
105
|
|
|
*/ |
|
106
|
|
|
export function unpack( |
|
107
|
|
|
buffer: Uint8Array|number[], |
|
108
|
|
|
theType: object, |
|
109
|
|
|
index?: number): number; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Unpack an array of numbers from a byte buffer. |
|
113
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
114
|
|
|
* @param {!Object} theType The type definition. |
|
115
|
|
|
* @param {number=} start The buffer index to start reading. |
|
116
|
|
|
* Assumes zero if undefined. |
|
117
|
|
|
* @param {number=} end The buffer index to stop reading. |
|
118
|
|
|
* Assumes the buffer length if undefined. |
|
119
|
|
|
* @param {boolean=} safe If set to false, extra bytes in the end of |
|
120
|
|
|
* the array are ignored and input buffers with insufficient bytes will |
|
121
|
|
|
* output a empty array. If safe is set to true the function |
|
122
|
|
|
* will throw a 'Bad buffer length' error. Defaults to false. |
|
123
|
|
|
* @return {!Array<number>} |
|
124
|
|
|
* @throws {Error} If the type definition is not valid |
|
125
|
|
|
*/ |
|
126
|
|
|
export function unpackArray( |
|
127
|
|
|
buffer: Uint8Array|number[], |
|
128
|
|
|
theType: object, |
|
129
|
|
|
start?: number, |
|
130
|
|
|
end?: number, |
|
131
|
|
|
safe?: boolean): number[]; |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Unpack a array of numbers to a typed array. |
|
135
|
|
|
* @param {!Uint8Array|!Array<number>} buffer The byte buffer. |
|
136
|
|
|
* @param {!Object} theType The type definition. |
|
137
|
|
|
* @param {!TypedArray|!Array<number>} output The output array. |
|
138
|
|
|
* @param {number=} start The buffer index to start reading. |
|
139
|
|
|
* Assumes zero if undefined. |
|
140
|
|
|
* @param {number=} end The buffer index to stop reading. |
|
141
|
|
|
* Assumes the buffer length if undefined. |
|
142
|
|
|
* @param {boolean=} safe If set to false, extra bytes in the end of |
|
143
|
|
|
* the array are ignored and input buffers with insufficient bytes will |
|
144
|
|
|
* write nothing to the output array. If safe is set to true the function |
|
145
|
|
|
* will throw a 'Bad buffer length' error. Defaults to false. |
|
146
|
|
|
* @throws {Error} If the type definition is not valid |
|
147
|
|
|
*/ |
|
148
|
|
|
export function unpackArrayTo( |
|
149
|
|
|
buffer: Uint8Array|number[], |
|
150
|
|
|
theType: object, |
|
151
|
|
|
output: ArrayBufferView|number[], |
|
152
|
|
|
start?: number, |
|
153
|
|
|
end?: number, |
|
154
|
|
|
safe?: boolean): void; |
|
155
|
|
|
|