-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathbuffer.ts
224 lines (208 loc) · 6.84 KB
/
buffer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
namespace pins {
/**
* Get the size in bytes of specified number format.
*/
//%
export function sizeOf(format: NumberFormat) {
switch (format) {
case NumberFormat.Int8LE:
case NumberFormat.UInt8LE:
case NumberFormat.Int8BE:
case NumberFormat.UInt8BE:
return 1;
case NumberFormat.Int16LE:
case NumberFormat.UInt16LE:
case NumberFormat.Int16BE:
case NumberFormat.UInt16BE:
return 2;
case NumberFormat.Int32LE:
case NumberFormat.Int32BE:
case NumberFormat.UInt32BE:
case NumberFormat.UInt32LE:
case NumberFormat.Float32BE:
case NumberFormat.Float32LE:
return 4;
case NumberFormat.Float64BE:
case NumberFormat.Float64LE:
return 8;
}
return 0;
}
/**
* Create a new buffer initalized to bytes from given array.
* @param bytes data to initalize with
*/
//%
export function createBufferFromArray(bytes: number[]) {
let buf = control.createBuffer(bytes.length)
for (let i = 0; i < bytes.length; ++i)
buf[i] = bytes[i]
return buf
}
function getFormat(pychar: string, isBig: boolean) {
switch (pychar) {
case 'B':
return NumberFormat.UInt8LE
case 'b':
return NumberFormat.Int8LE
case 'H':
return isBig ? NumberFormat.UInt16BE : NumberFormat.UInt16LE
case 'h':
return isBig ? NumberFormat.Int16BE : NumberFormat.Int16LE
case 'I':
case 'L':
return isBig ? NumberFormat.UInt32BE : NumberFormat.UInt32LE
case 'i':
case 'l':
return isBig ? NumberFormat.Int32BE : NumberFormat.Int32LE
case 'f':
return isBig ? NumberFormat.Float32BE : NumberFormat.Float32LE
case 'd':
return isBig ? NumberFormat.Float64BE : NumberFormat.Float64LE
default:
return null as NumberFormat
}
}
function packUnpackCore(format: string, nums: number[], buf: Buffer, isPack: boolean, off = 0) {
let isBig = false
let idx = 0
for (let i = 0; i < format.length; ++i) {
switch (format[i]) {
case ' ':
case '<':
case '=':
isBig = false
break
case '>':
case '!':
isBig = true
break
case 'x':
off++
break
default:
let fmt = getFormat(format[i], isBig)
if (fmt === null) {
control.fail("Not supported format character: " + format[i])
} else {
if (buf) {
if (isPack)
buf.setNumber(fmt, off, nums[idx++])
else
nums.push(buf.getNumber(fmt, off))
}
off += pins.sizeOf(fmt)
}
break
}
}
return off
}
export function packedSize(format: string) {
return packUnpackCore(format, null, null, true)
}
export function packBuffer(format: string, nums: number[]) {
let buf = control.createBuffer(packedSize(format))
packUnpackCore(format, nums, buf, true)
return buf
}
export function packIntoBuffer(format: string, buf: Buffer, offset: number, nums: number[]) {
packUnpackCore(format, nums, buf, true, offset)
}
export function unpackBuffer(format: string, buf: Buffer, offset = 0) {
let res: number[] = []
packUnpackCore(format, res, buf, false, offset)
return res
}
}
// see http://msgpack.org/ for the spec
// it currently only implements numbers and their sequances
// once we handle any type and typeof expressions we can do more
namespace msgpack {
function tagFormat(tag: number) {
switch (tag) {
case 0xCB: return NumberFormat.Float64BE
case 0xCC: return NumberFormat.UInt8BE
case 0xCD: return NumberFormat.UInt16BE
case 0xCE: return NumberFormat.UInt32BE
case 0xD0: return NumberFormat.Int8BE
case 0xD1: return NumberFormat.Int16BE
case 0xD2: return NumberFormat.Int32BE
default:
return null
}
}
function packNumberCore(buf: Buffer, offset: number, num: number) {
let tag = 0xCB
if (num == (num << 0) || num == (num >>> 0)) {
if (-31 <= num && num <= 127) {
if (buf) buf[offset] = num
return 1
} else if (0 <= num) {
if (num <= 0xff) {
tag = 0xCC
} else if (num <= 0xffff) {
tag = 0xCD
} else {
tag = 0xCE
}
} else {
if (-0x7f <= num) {
tag = 0xD0
} else if (-0x7fff <= num) {
tag = 0xD1
} else {
tag = 0xD2
}
}
}
let fmt = tagFormat(tag)
if (buf) {
buf[offset] = tag
buf.setNumber(fmt, offset + 1, num)
}
return pins.sizeOf(fmt) + 1
}
/**
* Unpacks a buffer into a number array.
*/
export function unpackNumberArray(buf: Buffer, offset = 0): number[] {
let res: number[] = []
while (offset < buf.length) {
let fmt = tagFormat(buf[offset++])
if (fmt === null) {
let v = buf.getNumber(NumberFormat.Int8BE, offset - 1)
if (-31 <= v && v <= 127)
res.push(v)
else
return null
} else {
res.push(buf.getNumber(fmt, offset))
offset += pins.sizeOf(fmt)
}
// padding at the end
while (buf[offset] === 0xc1) offset++;
}
return res
}
/**
* Pack a number array into a buffer.
* @param nums the numbers to be packed
*/
export function packNumberArray(nums: number[]): Buffer {
let off = 0
for (let n of nums) {
off += packNumberCore(null, off, n)
}
let buf = control.createBuffer(off)
off = 0
for (let n of nums) {
off += packNumberCore(buf, off, n)
}
return buf
}
}
interface Buffer {
[index: number]: number;
// rest defined in buffer.cpp
}