This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathluajit-msgpack-pure.lua
604 lines (528 loc) · 15.2 KB
/
luajit-msgpack-pure.lua
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
local ffi = require "ffi"
local bit = require "bit"
local math = require "math"
local IS_LUAFFI = not rawget(_G,"jit")
-- standard cdefs
ffi.cdef[[
void free(void *ptr);
void *realloc(void *ptr, size_t size);
void *malloc(size_t size);
]]
-- cache bitops
local bor,band,rshift = bit.bor,bit.band,bit.rshift
-- shared ffi data
local t_buf = ffi.new("unsigned char[8]")
local t_buf2 = ffi.new("unsigned char[8]")
-- VLA ctype constructor
local uchar_vla = ffi.typeof("unsigned char[?]")
-- endianness
local LITTLE_ENDIAN = ffi.abi("le")
local rcopy = function(dst,src,len)
local n = len-1
for i=0,n do dst[i] = src[n-i] end
end
-- buffer
local MSGPACK_SBUFFER_INIT_SIZE = 8192
local buffer = {}
local sbuffer_init = function(self)
self.size = 0
self.alloc = MSGPACK_SBUFFER_INIT_SIZE
self.data = ffi.cast("unsigned char *",ffi.C.malloc(MSGPACK_SBUFFER_INIT_SIZE))
end
local sbuffer_destroy = function(self)
ffi.C.free(buffer.data)
end
local sbuffer_realloc = function(self,len)
if self.alloc - self.size < len then
local nsize = self.alloc * 2
while nsize < self.alloc + len do nsize = nsize * 2 end
self.data = ffi.cast("unsigned char *",ffi.C.realloc(self.data,nsize))
self.alloc = nsize
end
end
local sbuffer_append_str = function(self,buf,len)
sbuffer_realloc(self,len)
ffi.copy(self.data+self.size,buf,len)
self.size = self.size + len
end
local sbuffer_append_byte = function(self,b)
sbuffer_realloc(self,1)
self.data[self.size] = b
self.size = self.size + 1
end
local sbuffer_append_tbl = function(self,t)
local len = #t
sbuffer_realloc(self,len)
local p = self.data + self.size - 1
for i=1,len do p[i] = t[i] end
self.size = self.size + len
end
local sbuffer_append_intx, sbuffer_append_int64
if LITTLE_ENDIAN then
sbuffer_append_intx = function(self,n,x,h)
local t = {h}
for i=x-8,8,-8 do t[#t+1] = band(rshift(n,i),0xff) end
t[#t+1] = band(n,0xff)
sbuffer_append_tbl(self,t)
end
sbuffer_append_int64 = function(self,n,h)
local q,r = math.floor(n/2^32),n%(2^32)
local t = {h}
for i=24,8,-8 do t[#t+1] = band(rshift(q,i),0xff) end
t[5] = band(q,0xff)
for i=24,8,-8 do t[#t+1] = band(rshift(r,i),0xff) end
t[9] = band(r,0xff)
sbuffer_append_tbl(self,t)
end
else
sbuffer_append_intx = function(self,n,x,h)
local t = {h, band(n,0xff)}
for i=8,x-8,8 do t[#t+1] = band(rshift(n,i),0xff) end
sbuffer_append_tbl(self,t)
end
sbuffer_append_int64 = function(self,n,h)
local q,r = math.floor(n/2^32),n%(2^32)
local t = {h, band(r,0xff)}
for i=8,24,8 do t[#t+1] = band(rshift(r,i),0xff) end
t[6] = band(q,0xff)
for i=8,24,8 do t[#t+1] = band(rshift(q,i),0xff) end
sbuffer_append_tbl(self,t)
end
end
--- packers
local packers = {}
packers.dynamic = function(data)
return packers[type(data)](data)
end
packers["nil"] = function()
sbuffer_append_byte(buffer,0xc0)
end
packers.boolean = function(data)
if data then -- pack true
sbuffer_append_byte(buffer,0xc3)
else -- pack false
sbuffer_append_byte(buffer,0xc2)
end
end
local set_fp_type = function(t)
local ptype,typebyte,_posinf,_neginf,_nan
if t == "double" then
typebyte,ptype = 0xcb,ffi.typeof("double *")
_posinf = {typebyte,0x7f,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}
_neginf = {typebyte,0xff,0xf0,0x00,0x00,0x00,0x00,0x00,0x00}
_nan = {typebyte,0xff,0xf8,0x00,0x00,0x00,0x00,0x00,0x00}
elseif t == "float" then
typebyte,ptype = 0xca,ffi.typeof("float *")
_posinf = {typebyte,0x7f,0x80,0x00,0x00}
_neginf = {typebyte,0xff,0x80,0x00,0x00}
_nan = {typebyte,0xff,0x88,0x00,0x00}
else return nil end
local len = ffi.sizeof(t)
if LITTLE_ENDIAN then
packers.fpnum = function(n)
ffi.cast(ptype,t_buf2)[0] = n
rcopy(t_buf,t_buf2,len)
sbuffer_append_byte(buffer,typebyte)
sbuffer_append_str(buffer,t_buf,len)
end
else
packers.fpnum = function(n)
ffi.cast(ptype,t_buf)[0] = n
sbuffer_append_byte(buffer,typebyte)
sbuffer_append_str(buffer,t_buf,len)
end
end
packers.posinf = function()
sbuffer_append_tbl(buffer,_posinf)
end
packers.neginf = function()
sbuffer_append_tbl(buffer,_neginf)
end
packers.nan = function()
sbuffer_append_tbl(buffer,_nan)
end
return true
end
set_fp_type("double") -- default
packers.number = function(n)
if math.floor(n) == n then -- integer
if n >= 0 then -- positive integer
if n < 128 then -- positive fixnum
sbuffer_append_byte(buffer,n)
elseif n < 256 then -- uint8
sbuffer_append_tbl(buffer,{0xcc,n})
elseif n < 2^16 then -- uint16
sbuffer_append_intx(buffer,n,16,0xcd)
elseif n < 2^32 then -- uint32
sbuffer_append_intx(buffer,n,32,0xce)
elseif n == math.huge then -- +inf
packers.posinf()
else -- uint64
sbuffer_append_int64(buffer,n,0xcf)
end
else -- negative integer
if n >= -32 then -- negative fixnum
sbuffer_append_byte(buffer,bor(0xe0,n))
elseif n >= -128 then -- int8
sbuffer_append_tbl(buffer,{0xd0,n})
elseif n >= -2^15 then -- int16
sbuffer_append_intx(buffer,n,16,0xd1)
elseif n >= -2^31 then -- int32
sbuffer_append_intx(buffer,n,32,0xd2)
elseif n == -math.huge then -- -inf
packers.neginf()
else -- int64
sbuffer_append_int64(buffer,n,0xd3)
end
end
elseif n ~= n then -- nan
packers.nan()
else -- floating point
packers.fpnum(n)
end
end
packers.string = function(data)
local n = #data
if n < 32 then
sbuffer_append_byte(buffer,bor(0xa0,n))
elseif n < 2^16 then
sbuffer_append_intx(buffer,n,16,0xda)
elseif n < 2^32 then
sbuffer_append_intx(buffer,n,32,0xdb)
else
error("overflow")
end
sbuffer_append_str(buffer,data,n)
end
packers["function"] = function(data)
error("unimplemented", data)
end
packers.userdata = function(data)
if IS_LUAFFI then
return packers.cdata(data)
else
error("unimplemented")
end
end
packers.thread = function(data)
error("unimplemented", data)
end
packers.array = function(data,ndata)
ndata = ndata or #data
if ndata < 16 then
sbuffer_append_byte(buffer,bor(0x90,ndata))
elseif ndata < 2^16 then
sbuffer_append_intx(buffer,ndata,16,0xdc)
elseif ndata < 2^32 then
sbuffer_append_intx(buffer,ndata,32,0xdd)
else
error("overflow")
end
for i=1,ndata do packers[type(data[i])](data[i]) end
end
packers.map = function(data,ndata)
if not ndata then
ndata = 0
for _ in pairs(data) do ndata = ndata+1 end
end
if ndata < 16 then
sbuffer_append_byte(buffer,bor(0x80,ndata))
elseif ndata < 2^16 then
sbuffer_append_intx(buffer,ndata,16,0xde)
elseif ndata < 2^32 then
sbuffer_append_intx(buffer,ndata,32,0xdf)
else
error("overflow")
end
for k,v in pairs(data) do
packers[type(k)](k)
packers[type(v)](v)
end
end
local set_table_classifier = function(f)
packers.table = function(data)
local obj_type,ndata = f(data)
packers[obj_type](data,ndata)
end
end
local table_classifier_keys = function(data)
-- slightly slower, does not access values at all
local is_map,ndata,nmax = false,0,0
for k,_ in pairs(data) do
if (type(k) == "number") and (k > 0) and (math.floor(k) == k) then
if k > nmax then nmax = k end
else is_map = true end
ndata = ndata+1
end
if (nmax ~= ndata) then -- there are holes
is_map = true
end -- else nmax == ndata == #data
return (is_map and "map" or "array"),ndata
end
local table_classifier_values = function(data)
-- slightly faster, accesses values
local is_map,ndata = false,0
for _ in pairs(data) do
ndata = ndata + 1
if rawget(data,ndata) == nil then is_map = true end
end
return (is_map and "map" or "array"),ndata
end
set_table_classifier(table_classifier_keys)
packers.cdata = function(data) -- msgpack-js
local n = ffi.sizeof(data)
if not n then
error("cannot pack cdata of unknown size")
elseif n < 2^16 then
sbuffer_append_intx(buffer,n,16,0xd8)
elseif n < 2^32 then
sbuffer_append_intx(buffer,n,32,0xd9)
else
error("overflow")
end
sbuffer_append_str(buffer,data,n)
end
-- types decoding
local types_map = {
[0xc0] = "nil",
[0xc2] = "false",
[0xc3] = "true",
[0xc4] = "nil", -- msgpack-js
[0xca] = "float",
[0xcb] = "double",
[0xcc] = "uint8",
[0xcd] = "uint16",
[0xce] = "uint32",
[0xcf] = "uint64",
[0xd0] = "int8",
[0xd1] = "int16",
[0xd2] = "int32",
[0xd3] = "int64",
[0xd8] = "buf16", -- msgpack-js
[0xd9] = "buf32", -- msgpack-js
[0xda] = "raw16",
[0xdb] = "raw32",
[0xdc] = "array16",
[0xdd] = "array32",
[0xde] = "map16",
[0xdf] = "map32",
}
local type_for = function(n)
if types_map[n] then return types_map[n]
elseif n < 0xc0 then
if n < 0x80 then return "fixnum_pos"
elseif n < 0x90 then return "fixmap"
elseif n < 0xa0 then return "fixarray"
else return "fixraw" end
elseif n > 0xdf then return "fixnum_neg"
else return "undefined" end
end
local types_len_map = {
uint16 = 2, uint32 = 4, uint64 = 8,
int16 = 2, int32 = 4, int64 = 8,
float = 4, double = 8,
}
--- unpackers
local unpackers = {}
local unpack_number
if LITTLE_ENDIAN then
unpack_number = function(buf,offset,ntype,nlen)
rcopy(t_buf,buf.data+offset+1,nlen)
return tonumber(ffi.cast(ntype,t_buf)[0])
end
else
unpack_number = function(buf,offset,ntype)
return tonumber(ffi.cast(ntype,buf.data+offset+1)[0])
end
end
local unpacker_number = function(buf,offset)
local obj_type = type_for(buf.data[offset])
local nlen = types_len_map[obj_type]
local ntype
if (obj_type == "float") or (obj_type == "double") then
ntype = obj_type .. " *"
else ntype = obj_type .. "_t *" end
if offset + nlen >= buf.size then return nil,nil end
return offset+nlen+1,unpack_number(buf,offset,ntype,nlen)
end
local unpack_map = function(buf,offset,n)
local r = {}
local k,v
for _=1,n do
offset,k = unpackers.dynamic(buf,offset)
if not offset then
-- Whole map is not available in the buffer
return nil, r
end
offset,v = unpackers.dynamic(buf,offset)
if not offset then
-- Whole map is not available in the buffer
return nil, r
end
r[k] = v
end
return offset,r
end
local unpack_array = function(buf,offset,n)
local r = {}
for i=1,n do
offset,r[i] = unpackers.dynamic(buf,offset)
if not offset then
-- Whole array is not available in the buffer
return nil, r
end
end
return offset,r
end
unpackers.dynamic = function(buf,offset)
assert(offset, "non nil offset is expected")
if offset >= buf.size then return nil,nil end
local obj_type = type_for(buf.data[offset])
return unpackers[obj_type](buf,offset)
end
unpackers.undefined = function(buf,offset)
error("unimplemented", buf, offset)
end
unpackers["nil"] = function(_,offset)
return offset+1,nil
end
unpackers["false"] = function(_,offset)
return offset+1,false
end
unpackers["true"] = function(_,offset)
return offset+1,true
end
unpackers.fixnum_pos = function(buf,offset)
return offset+1,buf.data[offset]
end
unpackers.uint8 = function(buf,offset)
if offset + 1 >= buf.size then return nil,nil end
return offset+2,buf.data[offset+1]
end
unpackers.uint16 = unpacker_number
unpackers.uint32 = unpacker_number
unpackers.uint64 = unpacker_number
unpackers.fixnum_neg = function(buf,offset)
-- alternative to cast below:
-- return offset+1,-band(bxor(buf.data[offset],0x1f),0x1f)-1
return offset+1,ffi.cast("int8_t *",buf.data)[offset]
end
unpackers.int8 = function(buf,offset)
if offset + 1 >= buf.size then return nil,nil end
return offset+2,ffi.cast("int8_t *",buf.data+offset+1)[0]
end
unpackers.int16 = unpacker_number
unpackers.int32 = unpacker_number
unpackers.int64 = unpacker_number
unpackers.float = unpacker_number
unpackers.double = unpacker_number
unpackers.fixraw = function(buf,offset)
local n = band(buf.data[offset],0x1f)
if offset + n >= buf.size then return nil,nil end
return offset+n+1,ffi.string(buf.data+offset+1,n)
end
unpackers.buf16 = function(buf,offset)
if offset + 2 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint16_t *",2)
if offset + n + 2 >= buf.size then return nil,nil end
local r = uchar_vla(n)
ffi.copy(r,buf.data+offset+3,n)
return offset+n+3,r
end
unpackers.buf32 = function(buf,offset)
if offset + 4 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint32_t *",4)
if offset + n + 4 >= buf.size then return nil,nil end
local r = uchar_vla(n)
ffi.copy(r,buf.data+offset+5,n)
return offset+n+5,r
end
unpackers.raw16 = function(buf,offset)
if offset + 2 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint16_t *",2)
if offset + n + 2 >= buf.size then return nil,nil end
return offset+n+3,ffi.string(buf.data+offset+3,n)
end
unpackers.raw32 = function(buf,offset)
if offset + 4 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint32_t *",4)
if offset + n + 4 >= buf.size then return nil,nil end
return offset+n+5,ffi.string(buf.data+offset+5,n)
end
unpackers.fixarray = function(buf,offset)
local n = band(buf.data[offset],0x0f)
return unpack_array(buf,offset+1,n)
end
unpackers.array16 = function(buf,offset)
if offset + 2 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint16_t *",2)
return unpack_array(buf,offset+3,n)
end
unpackers.array32 = function(buf,offset)
if offset + 4 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint32_t *",4)
return unpack_array(buf,offset+5,n)
end
unpackers.fixmap = function(buf,offset)
local n = band(buf.data[offset],0x0f)
return unpack_map(buf,offset+1,n)
end
unpackers.map16 = function(buf,offset)
if offset + 2 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint16_t *",2)
return unpack_map(buf,offset+3,n)
end
unpackers.map32 = function(buf,offset)
if offset + 4 >= buf.size then return nil,nil end
local n = unpack_number(buf,offset,"uint32_t *",4)
return unpack_map(buf,offset+5,n)
end
-- Main functions
local ljp_pack = function(data)
sbuffer_init(buffer)
packers.dynamic(data)
local s = ffi.string(buffer.data,buffer.size)
sbuffer_destroy(buffer)
return s
end
local ljp_unpack = function(s,offset)
if offset == nil then offset = 0 end
if type(s) ~= "string" then return false,"invalid argument" end
sbuffer_init(buffer)
sbuffer_append_str(buffer,s,#s)
local data
offset,data = unpackers.dynamic(buffer,offset)
sbuffer_destroy(buffer)
return offset,data
end
local ljp_pack_raw = function(raw)
sbuffer_init(buffer)
packers.dynamic(raw)
local data, size = buffer.data, buffer.size
buffer.data = nil
return data, size
end
local ljp_unpack_raw = function(ptr, size, offset)
if offset == nil then offset = 0 end
local buf = {
data = ffi.cast("unsigned char *", ptr),
size = size
}
local data
offset, data = unpackers.dynamic(buf, offset)
return offset, data
end
return {
pack = ljp_pack,
unpack = ljp_unpack,
pack_raw = ljp_pack_raw,
unpack_raw = ljp_unpack_raw,
set_fp_type = set_fp_type,
table_classifiers = {
keys = table_classifier_keys,
values = table_classifier_values,
},
set_table_classifier = set_table_classifier,
packers = packers,
unpackers = unpackers,
}