-
Notifications
You must be signed in to change notification settings - Fork 2
/
MessagePack.lua
897 lines (844 loc) · 28.7 KB
/
MessagePack.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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
--
-- lua-MessagePack : <http://fperrad.github.io/lua-MessagePack/>
--
local r, jit = pcall(require, 'jit')
if not r then
jit = nil
end
local SIZEOF_NUMBER = string.pack and #string.pack('n', 0.0) or 8
local maxinteger
local mininteger
if not jit and _VERSION < 'Lua 5.3' then
-- Lua 5.1 & 5.2
local loadstring = loadstring or load
local luac = string.dump(loadstring "a = 1")
local header = { luac:sub(1, 12):byte(1, 12) }
SIZEOF_NUMBER = header[11]
end
local assert = assert
local error = error
local pairs = pairs
local pcall = pcall
local setmetatable = setmetatable
local tostring = tostring
local type = type
local char = require'string'.char
local format = require'string'.format
local floor = require'math'.floor
local tointeger = require'math'.tointeger or floor
local frexp = require'math'.frexp or require'mathx'.frexp
local ldexp = require'math'.ldexp or require'mathx'.ldexp
local huge = require'math'.huge
local tconcat = require'table'.concat
local _ENV = nil
local m = {}
--[[ debug only
local function hexadump (s)
return (s:gsub('.', function (c) return format('%02X ', c:byte()) end))
end
m.hexadump = hexadump
--]]
local function argerror (caller, narg, extramsg)
error("bad argument #" .. tostring(narg) .. " to "
.. caller .. " (" .. extramsg .. ")")
end
local function typeerror (caller, narg, arg, tname)
argerror(caller, narg, tname .. " expected, got " .. type(arg))
end
local function checktype (caller, narg, arg, tname)
if type(arg) ~= tname then
typeerror(caller, narg, arg, tname)
end
end
local packers = setmetatable({}, {
__index = function (t, k) error("pack '" .. k .. "' is unimplemented") end
})
m.packers = packers
packers['nil'] = function (buffer)
buffer[#buffer+1] = char(0xC0) -- nil
end
packers['boolean'] = function (buffer, bool)
if bool then
buffer[#buffer+1] = char(0xC3) -- true
else
buffer[#buffer+1] = char(0xC2) -- false
end
end
packers['string_compat'] = function (buffer, str)
local n = #str
if n <= 0x1F then
buffer[#buffer+1] = char(0xA0 + n) -- fixstr
elseif n <= 0xFFFF then
buffer[#buffer+1] = char(0xDA, -- str16
floor(n / 0x100),
n % 0x100)
elseif n <= 4294967295.0 then
buffer[#buffer+1] = char(0xDB, -- str32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
error"overflow in pack 'string_compat'"
end
buffer[#buffer+1] = str
end
packers['_string'] = function (buffer, str)
local n = #str
if n <= 0x1F then
buffer[#buffer+1] = char(0xA0 + n) -- fixstr
elseif n <= 0xFF then
buffer[#buffer+1] = char(0xD9, -- str8
n)
elseif n <= 0xFFFF then
buffer[#buffer+1] = char(0xDA, -- str16
floor(n / 0x100),
n % 0x100)
elseif n <= 4294967295.0 then
buffer[#buffer+1] = char(0xDB, -- str32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
error"overflow in pack 'string'"
end
buffer[#buffer+1] = str
end
packers['binary'] = function (buffer, str)
local n = #str
if n <= 0xFF then
buffer[#buffer+1] = char(0xC4, -- bin8
n)
elseif n <= 0xFFFF then
buffer[#buffer+1] = char(0xC5, -- bin16
floor(n / 0x100),
n % 0x100)
elseif n <= 4294967295.0 then
buffer[#buffer+1] = char(0xC6, -- bin32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
error"overflow in pack 'binary'"
end
buffer[#buffer+1] = str
end
local set_string = function (str)
if str == 'string_compat' then
packers['string'] = packers['string_compat']
elseif str == 'string' then
packers['string'] = packers['_string']
elseif str == 'binary' then
packers['string'] = packers['binary']
else
argerror('set_string', 1, "invalid option '" .. str .."'")
end
end
m.set_string = set_string
packers['map'] = function (buffer, tbl, n)
if n <= 0x0F then
buffer[#buffer+1] = char(0x80 + n) -- fixmap
elseif n <= 0xFFFF then
buffer[#buffer+1] = char(0xDE, -- map16
floor(n / 0x100),
n % 0x100)
elseif n <= 4294967295.0 then
buffer[#buffer+1] = char(0xDF, -- map32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
error"overflow in pack 'map'"
end
for k, v in pairs(tbl) do
packers[type(k)](buffer, k)
packers[type(v)](buffer, v)
end
end
packers['array'] = function (buffer, tbl, n)
if n <= 0x0F then
buffer[#buffer+1] = char(0x90 + n) -- fixarray
elseif n <= 0xFFFF then
buffer[#buffer+1] = char(0xDC, -- array16
floor(n / 0x100),
n % 0x100)
elseif n <= 4294967295.0 then
buffer[#buffer+1] = char(0xDD, -- array32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
error"overflow in pack 'array'"
end
for i = 1, n do
local v = tbl[i]
packers[type(v)](buffer, v)
end
end
local set_array = function (array)
if array == 'without_hole' then
packers['_table'] = function (buffer, tbl)
local is_map, n, max = false, 0, 0
for k in pairs(tbl) do
if type(k) == 'number' and k > 0 then
if k > max then
max = k
end
else
is_map = true
end
n = n + 1
end
if max ~= n then -- there are holes
is_map = true
end
if is_map then
packers['map'](buffer, tbl, n)
else
packers['array'](buffer, tbl, n)
end
end
elseif array == 'with_hole' then
packers['_table'] = function (buffer, tbl)
local is_map, n, max = false, 0, 0
for k in pairs(tbl) do
if type(k) == 'number' and k > 0 then
if k > max then
max = k
end
else
is_map = true
end
n = n + 1
end
if is_map then
packers['map'](buffer, tbl, n)
else
packers['array'](buffer, tbl, max)
end
end
elseif array == 'always_as_map' then
packers['_table'] = function(buffer, tbl)
local n = 0
for k in pairs(tbl) do
n = n + 1
end
packers['map'](buffer, tbl, n)
end
else
argerror('set_array', 1, "invalid option '" .. array .."'")
end
end
m.set_array = set_array
packers['table'] = function (buffer, tbl)
packers['_table'](buffer, tbl)
end
packers['unsigned'] = function (buffer, n)
if n >= 0 then
if n <= 0x7F then
buffer[#buffer+1] = char(n) -- fixnum_pos
elseif n <= 0xFF then
buffer[#buffer+1] = char(0xCC, -- uint8
n)
elseif n <= 0xFFFF then
buffer[#buffer+1] = char(0xCD, -- uint16
floor(n / 0x100),
n % 0x100)
elseif n <= 4294967295.0 then
buffer[#buffer+1] = char(0xCE, -- uint32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
buffer[#buffer+1] = char(0xCF, -- uint64
0, -- only 53 bits from double
floor(n / 0x1000000000000) % 0x100,
floor(n / 0x10000000000) % 0x100,
floor(n / 0x100000000) % 0x100,
floor(n / 0x1000000) % 0x100,
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
end
else
if n >= -0x20 then
buffer[#buffer+1] = char(0x100 + n) -- fixnum_neg
elseif n >= -0x80 then
buffer[#buffer+1] = char(0xD0, -- int8
0x100 + n)
elseif n >= -0x8000 then
n = 0x10000 + n
buffer[#buffer+1] = char(0xD1, -- int16
floor(n / 0x100),
n % 0x100)
elseif n >= -0x80000000 then
n = 4294967296.0 + n
buffer[#buffer+1] = char(0xD2, -- int32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
buffer[#buffer+1] = char(0xD3, -- int64
0xFF, -- only 53 bits from double
floor(n / 0x1000000000000) % 0x100,
floor(n / 0x10000000000) % 0x100,
floor(n / 0x100000000) % 0x100,
floor(n / 0x1000000) % 0x100,
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
end
end
end
packers['signed'] = function (buffer, n)
if n >= 0 then
if n <= 0x7F then
buffer[#buffer+1] = char(n) -- fixnum_pos
elseif n <= 0x7FFF then
buffer[#buffer+1] = char(0xD1, -- int16
floor(n / 0x100),
n % 0x100)
elseif n <= 0x7FFFFFFF then
buffer[#buffer+1] = char(0xD2, -- int32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
buffer[#buffer+1] = char(0xD3, -- int64
0, -- only 53 bits from double
floor(n / 0x1000000000000) % 0x100,
floor(n / 0x10000000000) % 0x100,
floor(n / 0x100000000) % 0x100,
floor(n / 0x1000000) % 0x100,
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
end
else
if n >= -0x20 then
buffer[#buffer+1] = char(0xE0 + 0x20 + n) -- fixnum_neg
elseif n >= -0x80 then
buffer[#buffer+1] = char(0xD0, -- int8
0x100 + n)
elseif n >= -0x8000 then
n = 0x10000 + n
buffer[#buffer+1] = char(0xD1, -- int16
floor(n / 0x100),
n % 0x100)
elseif n >= -0x80000000 then
n = 4294967296.0 + n
buffer[#buffer+1] = char(0xD2, -- int32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
else
buffer[#buffer+1] = char(0xD3, -- int64
0xFF, -- only 53 bits from double
floor(n / 0x1000000000000) % 0x100,
floor(n / 0x10000000000) % 0x100,
floor(n / 0x100000000) % 0x100,
floor(n / 0x1000000) % 0x100,
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100)
end
end
end
local set_integer = function (integer)
if integer == 'unsigned' then
packers['integer'] = packers['unsigned']
elseif integer == 'signed' then
packers['integer'] = packers['signed']
else
argerror('set_integer', 1, "invalid option '" .. integer .."'")
end
end
m.set_integer = set_integer
packers['float'] = function (buffer, n)
local sign = 0
if n < 0.0 then
sign = 0x80
n = -n
end
local mant, expo = frexp(n)
if mant ~= mant then
buffer[#buffer+1] = char(0xCA, -- nan
0xFF, 0x88, 0x00, 0x00)
elseif mant == huge or expo > 0x80 then
if sign == 0 then
buffer[#buffer+1] = char(0xCA, -- inf
0x7F, 0x80, 0x00, 0x00)
else
buffer[#buffer+1] = char(0xCA, -- -inf
0xFF, 0x80, 0x00, 0x00)
end
elseif (mant == 0.0 and expo == 0) or expo < -0x7E then
buffer[#buffer+1] = char(0xCA, -- zero
sign, 0x00, 0x00, 0x00)
else
expo = expo + 0x7E
mant = floor((mant * 2.0 - 1.0) * ldexp(0.5, 24))
buffer[#buffer+1] = char(0xCA,
sign + floor(expo / 0x2),
(expo % 0x2) * 0x80 + floor(mant / 0x10000),
floor(mant / 0x100) % 0x100,
mant % 0x100)
end
end
packers['double'] = function (buffer, n)
local sign = 0
if n < 0.0 then
sign = 0x80
n = -n
end
local mant, expo = frexp(n)
if mant ~= mant then
buffer[#buffer+1] = char(0xCB, -- nan
0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
elseif mant == huge or expo > 0x400 then
if sign == 0 then
buffer[#buffer+1] = char(0xCB, -- inf
0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
else
buffer[#buffer+1] = char(0xCB, -- -inf
0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
end
elseif (mant == 0.0 and expo == 0) or expo < -0x3FE then
buffer[#buffer+1] = char(0xCB, -- zero
sign, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
else
expo = expo + 0x3FE
mant = floor((mant * 2.0 - 1.0) * ldexp(0.5, 53))
buffer[#buffer+1] = char(0xCB,
sign + floor(expo / 0x10),
(expo % 0x10) * 0x10 + floor(mant / 0x1000000000000),
floor(mant / 0x10000000000) % 0x100,
floor(mant / 0x100000000) % 0x100,
floor(mant / 0x1000000) % 0x100,
floor(mant / 0x10000) % 0x100,
floor(mant / 0x100) % 0x100,
mant % 0x100)
end
end
local set_number = function (number)
if number == 'float' then
packers['number'] = function (buffer, n)
if floor(n) == n and n < maxinteger and n > mininteger then
packers['integer'](buffer, n)
else
packers['float'](buffer, n)
end
end
elseif number == 'double' then
packers['number'] = function (buffer, n)
if floor(n) == n and n < maxinteger and n > mininteger then
packers['integer'](buffer, n)
else
packers['double'](buffer, n)
end
end
else
argerror('set_number', 1, "invalid option '" .. number .."'")
end
end
m.set_number = set_number
for k = 0, 4 do
local n = tointeger(2^k)
local fixext = 0xD4 + k
packers['fixext' .. tostring(n)] = function (buffer, tag, data)
assert(#data == n, "bad length for fixext" .. tostring(n))
buffer[#buffer+1] = char(fixext,
tag < 0 and tag + 0x100 or tag)
buffer[#buffer+1] = data
end
end
packers['ext'] = function (buffer, tag, data)
local n = #data
if n <= 0xFF then
buffer[#buffer+1] = char(0xC7, -- ext8
n,
tag < 0 and tag + 0x100 or tag)
elseif n <= 0xFFFF then
buffer[#buffer+1] = char(0xC8, -- ext16
floor(n / 0x100),
n % 0x100,
tag < 0 and tag + 0x100 or tag)
elseif n <= 4294967295.0 then
buffer[#buffer+1] = char(0xC9, -- ext&32
floor(n / 0x1000000),
floor(n / 0x10000) % 0x100,
floor(n / 0x100) % 0x100,
n % 0x100,
tag < 0 and tag + 0x100 or tag)
else
error"overflow in pack 'ext'"
end
buffer[#buffer+1] = data
end
function m.pack (data)
local buffer = {}
packers[type(data)](buffer, data)
return tconcat(buffer)
end
local unpackers -- forward declaration
local function _unpack (c)
local s, i, j = c.s, c.i, c.j
if i > j then
c:underflow(i)
s, i, j = c.s, c.i, c.j
end
local val = s:sub(i, i):byte()
c.i = i+1
return unpackers[val](c, val)
end
local function unpack_str (c, n)
local s, i, j = c.s, c.i, c.j
local e = i+n-1
if e > j or n < 0 then
c:underflow(e)
s, i, j = c.s, c.i, c.j
e = i+n-1
end
c.i = i+n
return s:sub(i, e)
end
local function unpack_array (c, n)
local t = {}
for i = 1, n do
t[i] = _unpack(c)
end
return t
end
local function unpack_map (c, n)
local t = {}
for i = 1, n do
local k = _unpack(c)
local val = _unpack(c)
if k == nil or k ~= k then
k = m.sentinel
end
if k ~= nil then
t[k] = val
end
end
return t
end
local function unpack_float (c)
local s, i, j = c.s, c.i, c.j
if i+3 > j then
c:underflow(i+3)
s, i, j = c.s, c.i, c.j
end
local b1, b2, b3, b4 = s:sub(i, i+3):byte(1, 4)
local sign = b1 > 0x7F
local expo = (b1 % 0x80) * 0x2 + floor(b2 / 0x80)
local mant = ((b2 % 0x80) * 0x100 + b3) * 0x100 + b4
if sign then
sign = -1
else
sign = 1
end
local n
if mant == 0 and expo == 0 then
n = sign * 0.0
elseif expo == 0xFF then
if mant == 0 then
n = sign * huge
else
n = 0.0/0.0
end
else
n = sign * ldexp(1.0 + mant / 0x800000, expo - 0x7F)
end
c.i = i+4
return n
end
local function unpack_double (c)
local s, i, j = c.s, c.i, c.j
if i+7 > j then
c:underflow(i+7)
s, i, j = c.s, c.i, c.j
end
local b1, b2, b3, b4, b5, b6, b7, b8 = s:sub(i, i+7):byte(1, 8)
local sign = b1 > 0x7F
local expo = (b1 % 0x80) * 0x10 + floor(b2 / 0x10)
local mant = ((((((b2 % 0x10) * 0x100 + b3) * 0x100 + b4) * 0x100 + b5) * 0x100 + b6) * 0x100 + b7) * 0x100 + b8
if sign then
sign = -1
else
sign = 1
end
local n
if mant == 0 and expo == 0 then
n = sign * 0.0
elseif expo == 0x7FF then
if mant == 0 then
n = sign * huge
else
n = 0.0/0.0
end
else
n = sign * ldexp(1.0 + mant / 4503599627370496.0, expo - 0x3FF)
end
c.i = i+8
return n
end
local function unpack_uint8 (c)
local s, i, j = c.s, c.i, c.j
if i > j then
c:underflow(i)
s, i, j = c.s, c.i, c.j
end
local b1 = s:sub(i, i):byte()
c.i = i+1
return b1
end
local function unpack_uint16 (c)
local s, i, j = c.s, c.i, c.j
if i+1 > j then
c:underflow(i+1)
s, i, j = c.s, c.i, c.j
end
local b1, b2 = s:sub(i, i+1):byte(1, 2)
c.i = i+2
return b1 * 0x100 + b2
end
local function unpack_uint32 (c)
local s, i, j = c.s, c.i, c.j
if i+3 > j then
c:underflow(i+3)
s, i, j = c.s, c.i, c.j
end
local b1, b2, b3, b4 = s:sub(i, i+3):byte(1, 4)
c.i = i+4
return ((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4
end
local function unpack_uint64 (c)
local s, i, j = c.s, c.i, c.j
if i+7 > j then
c:underflow(i+7)
s, i, j = c.s, c.i, c.j
end
local b1, b2, b3, b4, b5, b6, b7, b8 = s:sub(i, i+7):byte(1, 8)
c.i = i+8
return ((((((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4) * 0x100 + b5) * 0x100 + b6) * 0x100 + b7) * 0x100 + b8
end
local function unpack_int8 (c)
local s, i, j = c.s, c.i, c.j
if i > j then
c:underflow(i)
s, i, j = c.s, c.i, c.j
end
local b1 = s:sub(i, i):byte()
c.i = i+1
if b1 < 0x80 then
return b1
else
return b1 - 0x100
end
end
local function unpack_int16 (c)
local s, i, j = c.s, c.i, c.j
if i+1 > j then
c:underflow(i+1)
s, i, j = c.s, c.i, c.j
end
local b1, b2 = s:sub(i, i+1):byte(1, 2)
c.i = i+2
if b1 < 0x80 then
return b1 * 0x100 + b2
else
return ((b1 - 0xFF) * 0x100 + (b2 - 0xFF)) - 1
end
end
local function unpack_int32 (c)
local s, i, j = c.s, c.i, c.j
if i+3 > j then
c:underflow(i+3)
s, i, j = c.s, c.i, c.j
end
local b1, b2, b3, b4 = s:sub(i, i+3):byte(1, 4)
c.i = i+4
if b1 < 0x80 then
return ((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4
else
return ((((b1 - 0xFF) * 0x100 + (b2 - 0xFF)) * 0x100 + (b3 - 0xFF)) * 0x100 + (b4 - 0xFF)) - 1
end
end
local function unpack_int64 (c)
local s, i, j = c.s, c.i, c.j
if i+7 > j then
c:underflow(i+7)
s, i, j = c.s, c.i, c.j
end
local b1, b2, b3, b4, b5, b6, b7, b8 = s:sub(i, i+7):byte(1, 8)
c.i = i+8
if b1 < 0x80 then
return ((((((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4) * 0x100 + b5) * 0x100 + b6) * 0x100 + b7) * 0x100 + b8
else
return ((((((((b1 - 0xFF) * 0x100 + (b2 - 0xFF)) * 0x100 + (b3 - 0xFF)) * 0x100 + (b4 - 0xFF)) * 0x100 + (b5 - 0xFF)) * 0x100 + (b6 - 0xFF)) * 0x100 + (b7 - 0xFF)) * 0x100 + (b8 - 0xFF)) - 1
end
end
function m.build_ext (tag, data)
return nil
end
local function unpack_ext (c, n, tag)
local s, i, j = c.s, c.i, c.j
local e = i+n-1
if e > j or n < 0 then
c:underflow(e)
s, i, j = c.s, c.i, c.j
e = i+n-1
end
c.i = i+n
return m.build_ext(tag, s:sub(i, e))
end
unpackers = {
[0xC0] = function () return nil end,
[0xC2] = function () return false end,
[0xC3] = function () return true end,
[0xC4] = function (c) return unpack_str(c, unpack_uint8(c)) end, -- bin8
[0xC5] = function (c) return unpack_str(c, unpack_uint16(c)) end, -- bin16
[0xC6] = function (c) return unpack_str(c, unpack_uint32(c)) end, -- bin32
[0xC7] = function (c) return unpack_ext(c, unpack_uint8(c), unpack_int8(c)) end,
[0xC8] = function (c) return unpack_ext(c, unpack_uint16(c), unpack_int8(c)) end,
[0xC9] = function (c) return unpack_ext(c, unpack_uint32(c), unpack_int8(c)) end,
[0xCA] = unpack_float,
[0xCB] = unpack_double,
[0xCC] = unpack_uint8,
[0xCD] = unpack_uint16,
[0xCE] = unpack_uint32,
[0xCF] = unpack_uint64,
[0xD0] = unpack_int8,
[0xD1] = unpack_int16,
[0xD2] = unpack_int32,
[0xD3] = unpack_int64,
[0xD4] = function (c) return unpack_ext(c, 1, unpack_int8(c)) end,
[0xD5] = function (c) return unpack_ext(c, 2, unpack_int8(c)) end,
[0xD6] = function (c) return unpack_ext(c, 4, unpack_int8(c)) end,
[0xD7] = function (c) return unpack_ext(c, 8, unpack_int8(c)) end,
[0xD8] = function (c) return unpack_ext(c, 16, unpack_int8(c)) end,
[0xD9] = function (c) return unpack_str(c, unpack_uint8(c)) end,
[0xDA] = function (c) return unpack_str(c, unpack_uint16(c)) end,
[0xDB] = function (c) return unpack_str(c, unpack_uint32(c)) end,
[0xDC] = function (c) return unpack_array(c, unpack_uint16(c)) end,
[0xDD] = function (c) return unpack_array(c, unpack_uint32(c)) end,
[0xDE] = function (c) return unpack_map(c, unpack_uint16(c)) end,
[0xDF] = function (c) return unpack_map(c, unpack_uint32(c)) end,
}
m.unpackers = setmetatable(unpackers, {
__index = function (t, k)
if k < 0xC0 then
if k < 0x80 then
return function (c, val) return val end
elseif k < 0x90 then
return function (c, val) return unpack_map(c, val % 0x10) end
elseif k < 0xA0 then
return function (c, val) return unpack_array(c, val % 0x10) end
else
return function (c, val) return unpack_str(c, val % 0x20) end
end
elseif k > 0xDF then
return function (c, val) return val - 0x100 end
else
return function () error("unpack '" .. format('0x%X', k) .. "' is unimplemented") end
end
end
})
local function cursor_string (str)
return {
s = str,
i = 1,
j = #str,
underflow = function (self)
error "missing bytes"
end,
}
end
local function cursor_loader (ld)
return {
s = '',
i = 1,
j = 0,
underflow = function (self, e)
self.s = self.s:sub(self.i)
e = e - self.i + 1
self.i = 1
self.j = 0
while e > self.j do
local chunk = ld()
if not chunk then
error "missing bytes"
end
self.s = self.s .. chunk
self.j = #self.s
end
end,
}
end
function m.unpack (s)
checktype('unpack', 1, s, 'string')
local cursor = cursor_string(s)
local data = _unpack(cursor)
if cursor.i < cursor.j then
error "extra bytes"
end
return data
end
function m.unpacker (src)
if type(src) == 'string' then
local cursor = cursor_string(src)
return function ()
if cursor.i <= cursor.j then
return cursor.i, _unpack(cursor)
end
end
elseif type(src) == 'function' then
local cursor = cursor_loader(src)
return function ()
if cursor.i > cursor.j then
pcall(cursor.underflow, cursor, cursor.i)
end
if cursor.i <= cursor.j then
return true, _unpack(cursor)
end
end
else
argerror('unpacker', 1, "string or function expected, got " .. type(src))
end
end
set_string'string_compat'
set_integer'unsigned'
if SIZEOF_NUMBER == 4 then
maxinteger = 16777215
mininteger = -maxinteger
m.small_lua = true
unpackers[0xCB] = nil -- double
unpackers[0xCF] = nil -- uint64
unpackers[0xD3] = nil -- int64
set_number'float'
else
maxinteger = 9007199254740991
mininteger = -maxinteger
set_number'double'
if SIZEOF_NUMBER > 8 then
m.long_double = true
end
end
set_array'without_hole'
m._VERSION = '0.4.0'
m._DESCRIPTION = "lua-MessagePack : a pure Lua implementation"
m._COPYRIGHT = "Copyright (c) 2012-2016 Francois Perrad"
return m
--
-- This library is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--