MessagePack.lua 32.9 KB
Newer Older
François Perrad's avatar
François Perrad committed
1
--
François Perrad's avatar
François Perrad committed
2
-- lua-MessagePack : <http://fperrad.github.io/lua-MessagePack/>
François Perrad's avatar
François Perrad committed
3
4
5
6
7
8
9
--

local r, jit = pcall(require, 'jit')
if not r then
    jit = nil
end

François Perrad's avatar
François Perrad committed
10
11
local SIZEOF_NUMBER = string.pack and #string.pack('n', 0.0) or 8
local NUMBER_INTEGRAL = math.type and (math.type(0.0) == math.type(0)) or false
François Perrad's avatar
François Perrad committed
12
13
local maxinteger
local mininteger
François Perrad's avatar
François Perrad committed
14
15
16
17
18
19
20
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]
    NUMBER_INTEGRAL = 1 == header[12]
François Perrad's avatar
François Perrad committed
21
22
end

François Perrad's avatar
François Perrad committed
23
local assert = assert
François Perrad's avatar
François Perrad committed
24
25
local error = error
local pairs = pairs
François Perrad's avatar
François Perrad committed
26
local pcall = pcall
François Perrad's avatar
François Perrad committed
27
28
29
30
31
local setmetatable = setmetatable
local tostring = tostring
local type = type
local char = require'string'.char
local floor = require'math'.floor
François Perrad's avatar
François Perrad committed
32
local tointeger = require'math'.tointeger or floor
François Perrad's avatar
François Perrad committed
33
34
local frexp = require'math'.frexp or require'mathx'.frexp
local ldexp = require'math'.ldexp or require'mathx'.ldexp
François Perrad's avatar
François Perrad committed
35
36
37
local huge = require'math'.huge
local tconcat = require'table'.concat

François Perrad's avatar
François Perrad committed
38
--[[ debug only
François Perrad's avatar
François Perrad committed
39
local format = require'string'.format
François Perrad's avatar
François Perrad committed
40
local function hexadump (s)
François Perrad's avatar
François Perrad committed
41
    return (s:gsub('.', function (c) return format('%02X ', c:byte()) end))
François Perrad's avatar
François Perrad committed
42
43
44
end
--]]

45
local _ENV = nil
François Perrad's avatar
François Perrad committed
46
47
local m = {}

François Perrad's avatar
François Perrad committed
48
49
50
51
--[[ debug only
m.hexadump = hexadump
--]]

François Perrad's avatar
François Perrad committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
})
François Perrad's avatar
François Perrad committed
70
m.packers = packers
François Perrad's avatar
François Perrad committed
71
72
73
74
75

packers['nil'] = function (buffer)
    buffer[#buffer+1] = char(0xC0)              -- nil
end

François Perrad's avatar
minor    
François Perrad committed
76
packers['boolean'] = function (buffer, bool)
François Perrad's avatar
François Perrad committed
77
78
79
80
81
82
83
    if bool then
        buffer[#buffer+1] = char(0xC3)          -- true
    else
        buffer[#buffer+1] = char(0xC2)          -- false
    end
end

François Perrad's avatar
François Perrad committed
84
85
86
87
88
89
90
91
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)
François Perrad's avatar
François Perrad committed
92
    elseif n <= 4294967295.0 then
François Perrad's avatar
François Perrad committed
93
94
95
96
97
98
99
100
101
102
103
        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

François Perrad's avatar
François Perrad committed
104
packers['_string'] = function (buffer, str)
François Perrad's avatar
François Perrad committed
105
106
    local n = #str
    if n <= 0x1F then
François Perrad's avatar
François Perrad committed
107
        buffer[#buffer+1] = char(0xA0 + n)      -- fixstr
François Perrad's avatar
François Perrad committed
108
109
110
    elseif n <= 0xFF then
        buffer[#buffer+1] = char(0xD9,          -- str8
                                 n)
François Perrad's avatar
François Perrad committed
111
    elseif n <= 0xFFFF then
François Perrad's avatar
François Perrad committed
112
        buffer[#buffer+1] = char(0xDA,          -- str16
François Perrad's avatar
François Perrad committed
113
114
                                 floor(n / 0x100),
                                 n % 0x100)
François Perrad's avatar
François Perrad committed
115
    elseif n <= 4294967295.0 then
François Perrad's avatar
François Perrad committed
116
        buffer[#buffer+1] = char(0xDB,          -- str32
François Perrad's avatar
François Perrad committed
117
118
119
120
121
122
123
124
125
126
                                 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

François Perrad's avatar
François Perrad committed
127
128
129
130
131
132
133
134
135
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)
François Perrad's avatar
François Perrad committed
136
    elseif n <= 4294967295.0 then
François Perrad's avatar
François Perrad committed
137
138
139
140
141
142
143
144
145
146
147
148
        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)
François Perrad's avatar
François Perrad committed
149
150
151
    if str == 'string_compat' then
        packers['string'] = packers['string_compat']
    elseif str == 'string' then
François Perrad's avatar
François Perrad committed
152
153
154
155
156
157
158
159
160
        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

161
162
163
164
165
166
167
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)
François Perrad's avatar
François Perrad committed
168
    elseif n <= 4294967295.0 then
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
        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)
François Perrad's avatar
François Perrad committed
190
    elseif n <= 4294967295.0 then
191
192
193
194
195
196
197
198
199
200
201
        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)
François Perrad's avatar
François Perrad committed
202
203
204
    end
end

François Perrad's avatar
François Perrad committed
205
206
local set_array = function (array)
    if array == 'without_hole' then
François Perrad's avatar
François Perrad committed
207
        packers['_table'] = function (buffer, tbl)
François Perrad's avatar
François Perrad committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
            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
                return packers['map'](buffer, tbl, n)
            else
                return packers['array'](buffer, tbl, n)
            end
        end
    elseif array == 'with_hole' then
François Perrad's avatar
François Perrad committed
229
        packers['_table'] = function (buffer, tbl)
François Perrad's avatar
François Perrad committed
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
            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
                return packers['map'](buffer, tbl, n)
            else
                return packers['array'](buffer, tbl, max)
            end
        end
247
248
249
250
251
252
253
254
    elseif array == 'always_as_map' then
        packers['_table'] = function(buffer, tbl)
            local n = 0
            for k in pairs(tbl) do
                n = n + 1
            end
            return packers['map'](buffer, tbl, n)
        end
François Perrad's avatar
François Perrad committed
255
256
257
258
259
260
    else
        argerror('set_array', 1, "invalid option '" .. array .."'")
    end
end
m.set_array = set_array

François Perrad's avatar
François Perrad committed
261
262
263
264
packers['table'] = function (buffer, tbl)
    return packers['_table'](buffer, tbl)
end

François Perrad's avatar
minor    
François Perrad committed
265
packers['unsigned'] = function (buffer, n)
François Perrad's avatar
François Perrad committed
266
267
268
269
270
271
272
273
274
275
    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)
François Perrad's avatar
François Perrad committed
276
        elseif n <= 4294967295.0 then
François Perrad's avatar
François Perrad committed
277
278
279
280
281
282
            buffer[#buffer+1] = char(0xCE,      -- uint32
                                     floor(n / 0x1000000),
                                     floor(n / 0x10000) % 0x100,
                                     floor(n / 0x100) % 0x100,
                                     n % 0x100)
        else
François Perrad's avatar
François Perrad committed
283
284
            buffer[#buffer+1] = char(0xCF,      -- uint64
                                     0,         -- only 53 bits from double
François Perrad's avatar
François Perrad committed
285
                                     floor(n / 0x1000000000000) % 0x100,
François Perrad's avatar
François Perrad committed
286
287
288
289
290
291
                                     floor(n / 0x10000000000) % 0x100,
                                     floor(n / 0x100000000) % 0x100,
                                     floor(n / 0x1000000) % 0x100,
                                     floor(n / 0x10000) % 0x100,
                                     floor(n / 0x100) % 0x100,
                                     n % 0x100)
François Perrad's avatar
François Perrad committed
292
293
294
        end
    else
        if n >= -0x20 then
François Perrad's avatar
François Perrad committed
295
            buffer[#buffer+1] = char(0x100 + n) -- fixnum_neg
François Perrad's avatar
François Perrad committed
296
297
298
299
300
301
302
303
304
        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
François Perrad's avatar
François Perrad committed
305
            n = 4294967296.0 + n
François Perrad's avatar
François Perrad committed
306
307
308
309
310
311
            buffer[#buffer+1] = char(0xD2,      -- int32
                                     floor(n / 0x1000000),
                                     floor(n / 0x10000) % 0x100,
                                     floor(n / 0x100) % 0x100,
                                     n % 0x100)
        else
François Perrad's avatar
François Perrad committed
312
313
            buffer[#buffer+1] = char(0xD3,      -- int64
                                     0xFF,      -- only 53 bits from double
François Perrad's avatar
François Perrad committed
314
                                     floor(n / 0x1000000000000) % 0x100,
François Perrad's avatar
François Perrad committed
315
316
317
318
319
320
                                     floor(n / 0x10000000000) % 0x100,
                                     floor(n / 0x100000000) % 0x100,
                                     floor(n / 0x1000000) % 0x100,
                                     floor(n / 0x10000) % 0x100,
                                     floor(n / 0x100) % 0x100,
                                     n % 0x100)
François Perrad's avatar
tidy    
François Perrad committed
321
        end
François Perrad's avatar
François Perrad committed
322
323
324
    end
end

François Perrad's avatar
minor    
François Perrad committed
325
packers['signed'] = function (buffer, n)
François Perrad's avatar
François Perrad committed
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
    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
François Perrad's avatar
François Perrad committed
362
            n = 4294967296.0 + n
François Perrad's avatar
François Perrad committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
            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)
François Perrad's avatar
tidy    
François Perrad committed
378
        end
François Perrad's avatar
François Perrad committed
379
380
381
    end
end

François Perrad's avatar
minor    
François Perrad committed
382
local set_integer = function (integer)
François Perrad's avatar
François Perrad committed
383
384
385
386
387
    if integer == 'unsigned' then
        packers['integer'] = packers['unsigned']
    elseif integer == 'signed' then
        packers['integer'] = packers['signed']
    else
François Perrad's avatar
François Perrad committed
388
        argerror('set_integer', 1, "invalid option '" .. integer .."'")
François Perrad's avatar
François Perrad committed
389
390
391
392
    end
end
m.set_integer = set_integer

François Perrad's avatar
François Perrad committed
393
packers['float'] = function (buffer, n)
394
    local sign = 0
François Perrad's avatar
François Perrad committed
395
    if n < 0.0 then
396
397
398
399
400
401
        sign = 0x80
        n = -n
    end
    local mant, expo = frexp(n)
    if mant ~= mant then
        buffer[#buffer+1] = char(0xCA,  -- nan
François Perrad's avatar
François Perrad committed
402
                                 0xFF, 0x88, 0x00, 0x00)
403
404
405
406
407
408
409
    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)
François Perrad's avatar
François Perrad committed
410
        end
François Perrad's avatar
François Perrad committed
411
    elseif (mant == 0.0 and expo == 0) or expo < -0x7E then
412
413
414
        buffer[#buffer+1] = char(0xCA,  -- zero
                                 sign, 0x00, 0x00, 0x00)
    else
François Perrad's avatar
François Perrad committed
415
        expo = expo + 0x7E
François Perrad's avatar
François Perrad committed
416
        mant = floor((mant * 2.0 - 1.0) * ldexp(0.5, 24))
François Perrad's avatar
François Perrad committed
417
418
419
420
421
422
423
424
425
        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)
426
    local sign = 0
François Perrad's avatar
François Perrad committed
427
    if n < 0.0 then
428
429
430
431
432
433
        sign = 0x80
        n = -n
    end
    local mant, expo = frexp(n)
    if mant ~= mant then
        buffer[#buffer+1] = char(0xCB,  -- nan
François Perrad's avatar
François Perrad committed
434
                                 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
François Perrad's avatar
François Perrad committed
435
    elseif mant == huge or expo > 0x400 then
436
437
438
439
440
441
        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)
François Perrad's avatar
François Perrad committed
442
        end
François Perrad's avatar
François Perrad committed
443
    elseif (mant == 0.0 and expo == 0) or expo < -0x3FE then
444
445
446
        buffer[#buffer+1] = char(0xCB,  -- zero
                                 sign, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
    else
François Perrad's avatar
François Perrad committed
447
        expo = expo + 0x3FE
François Perrad's avatar
François Perrad committed
448
        mant = floor((mant * 2.0 - 1.0) * ldexp(0.5, 53))
François Perrad's avatar
François Perrad committed
449
450
451
452
453
454
455
456
457
458
459
460
        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

François Perrad's avatar
minor    
François Perrad committed
461
local set_number = function (number)
François Perrad's avatar
François Perrad committed
462
    if number == 'integer' then
François Perrad's avatar
François Perrad committed
463
        packers['number'] = packers['signed']
François Perrad's avatar
François Perrad committed
464
465
    elseif number == 'float' then
        packers['number'] = function (buffer, n)
François Perrad's avatar
François Perrad committed
466
            if floor(n) == n and n < maxinteger and n > mininteger then
François Perrad's avatar
François Perrad committed
467
                return packers['integer'](buffer, n)
François Perrad's avatar
François Perrad committed
468
469
            else
                return packers['float'](buffer, n)
François Perrad's avatar
François Perrad committed
470
471
472
473
            end
        end
    elseif number == 'double' then
        packers['number'] = function (buffer, n)
François Perrad's avatar
François Perrad committed
474
            if floor(n) == n and n < maxinteger and n > mininteger then
François Perrad's avatar
François Perrad committed
475
                return packers['integer'](buffer, n)
François Perrad's avatar
François Perrad committed
476
477
            else
                return packers['double'](buffer, n)
François Perrad's avatar
François Perrad committed
478
479
480
            end
        end
    else
François Perrad's avatar
François Perrad committed
481
        argerror('set_number', 1, "invalid option '" .. number .."'")
François Perrad's avatar
François Perrad committed
482
483
484
485
    end
end
m.set_number = set_number

François Perrad's avatar
François Perrad committed
486
for k = 0, 4 do
François Perrad's avatar
François Perrad committed
487
    local n = tointeger(2^k)
François Perrad's avatar
François Perrad committed
488
    local fixext = 0xD4 + k
489
490
    packers['fixext' .. tostring(n)] = function (buffer, tag, data)
        assert(#data == n, "bad length for fixext" .. tostring(n))
François Perrad's avatar
François Perrad committed
491
        buffer[#buffer+1] = char(fixext,
François Perrad's avatar
François Perrad committed
492
                                 tag < 0 and tag + 0x100 or tag)
François Perrad's avatar
François Perrad committed
493
494
        buffer[#buffer+1] = data
    end
François Perrad's avatar
François Perrad committed
495
496
end

François Perrad's avatar
François Perrad committed
497
packers['ext'] = function (buffer, tag, data)
François Perrad's avatar
François Perrad committed
498
499
500
501
    local n = #data
    if n <= 0xFF then
        buffer[#buffer+1] = char(0xC7,          -- ext8
                                 n,
François Perrad's avatar
François Perrad committed
502
                                 tag < 0 and tag + 0x100 or tag)
François Perrad's avatar
François Perrad committed
503
504
505
506
    elseif n <= 0xFFFF then
        buffer[#buffer+1] = char(0xC8,          -- ext16
                                 floor(n / 0x100),
                                 n % 0x100,
François Perrad's avatar
François Perrad committed
507
                                 tag < 0 and tag + 0x100 or tag)
François Perrad's avatar
François Perrad committed
508
    elseif n <= 4294967295.0 then
François Perrad's avatar
François Perrad committed
509
510
511
512
513
        buffer[#buffer+1] = char(0xC9,          -- ext&32
                                 floor(n / 0x1000000),
                                 floor(n / 0x10000) % 0x100,
                                 floor(n / 0x100) % 0x100,
                                 n % 0x100,
François Perrad's avatar
François Perrad committed
514
                                 tag < 0 and tag + 0x100 or tag)
François Perrad's avatar
François Perrad committed
515
516
517
518
519
520
    else
        error"overflow in pack 'ext'"
    end
    buffer[#buffer+1] = data
end

François Perrad's avatar
François Perrad committed
521
522
523
524
525
526
527
function m.pack (data)
    local buffer = {}
    packers[type(data)](buffer, data)
    return tconcat(buffer)
end


François Perrad's avatar
François Perrad committed
528
529
530
531
local types_map = setmetatable({
    [0xC0] = 'nil',
    [0xC2] = 'false',
    [0xC3] = 'true',
François Perrad's avatar
François Perrad committed
532
533
534
    [0xC4] = 'bin8',
    [0xC5] = 'bin16',
    [0xC6] = 'bin32',
François Perrad's avatar
François Perrad committed
535
536
537
    [0xC7] = 'ext8',
    [0xC8] = 'ext16',
    [0xC9] = 'ext32',
François Perrad's avatar
François Perrad committed
538
539
540
541
542
543
544
545
546
547
    [0xCA] = 'float',
    [0xCB] = 'double',
    [0xCC] = 'uint8',
    [0xCD] = 'uint16',
    [0xCE] = 'uint32',
    [0xCF] = 'uint64',
    [0xD0] = 'int8',
    [0xD1] = 'int16',
    [0xD2] = 'int32',
    [0xD3] = 'int64',
François Perrad's avatar
François Perrad committed
548
549
550
551
552
    [0xD4] = 'fixext1',
    [0xD5] = 'fixext2',
    [0xD6] = 'fixext4',
    [0xD7] = 'fixext8',
    [0xD8] = 'fixext16',
François Perrad's avatar
François Perrad committed
553
    [0xD9] = 'str8',
François Perrad's avatar
François Perrad committed
554
555
    [0xDA] = 'str16',
    [0xDB] = 'str32',
François Perrad's avatar
François Perrad committed
556
557
558
559
560
561
562
563
564
565
566
567
568
    [0xDC] = 'array16',
    [0xDD] = 'array32',
    [0xDE] = 'map16',
    [0xDF] = 'map32',
}, { __index = function (t, k)
        if k < 0xC0 then
            if k < 0x80 then
                return 'fixnum_pos'
            elseif k < 0x90 then
                return 'fixmap'
            elseif k < 0xA0 then
                return 'fixarray'
            else
François Perrad's avatar
François Perrad committed
569
                return 'fixstr'
François Perrad's avatar
François Perrad committed
570
571
572
573
            end
        elseif k > 0xDF then
            return 'fixnum_neg'
        else
574
            return 'reserved' .. tostring(k)
François Perrad's avatar
François Perrad committed
575
576
        end
end })
François Perrad's avatar
François Perrad committed
577
m.types_map = types_map
François Perrad's avatar
François Perrad committed
578
579
580
581

local unpackers = setmetatable({}, {
    __index = function (t, k) error("unpack '" .. k .. "' is unimplemented") end
})
François Perrad's avatar
François Perrad committed
582
m.unpackers = unpackers
François Perrad's avatar
François Perrad committed
583

François Perrad's avatar
minor    
François Perrad committed
584
local function unpack_array (c, n)
François Perrad's avatar
François Perrad committed
585
    local t = {}
François Perrad's avatar
François Perrad committed
586
    local decode = unpackers['any']
587
588
    for i = 1, n do
        t[i] = decode(c)
François Perrad's avatar
François Perrad committed
589
590
591
592
    end
    return t
end

François Perrad's avatar
minor    
François Perrad committed
593
local function unpack_map (c, n)
François Perrad's avatar
François Perrad committed
594
    local t = {}
François Perrad's avatar
François Perrad committed
595
    local decode = unpackers['any']
François Perrad's avatar
François Perrad committed
596
    for i = 1, n do
François Perrad's avatar
François Perrad committed
597
        local k = decode(c)
François Perrad's avatar
François Perrad committed
598
        local val = decode(c)
François Perrad's avatar
François Perrad committed
599
        if k == nil or k ~= k then
François Perrad's avatar
François Perrad committed
600
601
602
603
604
            k = m.sentinel
        end
        if k ~= nil then
            t[k] = val
        end
François Perrad's avatar
François Perrad committed
605
606
607
608
609
610
611
    end
    return t
end

unpackers['any'] = function (c)
    local s, i, j = c.s, c.i, c.j
    if i > j then
François Perrad's avatar
François Perrad committed
612
613
        c:underflow(i)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
614
615
616
617
618
619
    end
    local val = s:sub(i, i):byte()
    c.i = i+1
    return unpackers[types_map[val]](c, val)
end

François Perrad's avatar
minor    
François Perrad committed
620
unpackers['nil'] = function ()
François Perrad's avatar
François Perrad committed
621
622
623
    return nil
end

François Perrad's avatar
minor    
François Perrad committed
624
unpackers['false'] = function ()
François Perrad's avatar
François Perrad committed
625
626
627
    return false
end

François Perrad's avatar
minor    
François Perrad committed
628
unpackers['true'] = function ()
François Perrad's avatar
François Perrad committed
629
630
631
    return true
end

François Perrad's avatar
minor    
François Perrad committed
632
unpackers['float'] = function (c)
François Perrad's avatar
François Perrad committed
633
634
    local s, i, j = c.s, c.i, c.j
    if i+3 > j then
François Perrad's avatar
François Perrad committed
635
636
        c:underflow(i+3)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
637
638
639
640
641
642
643
644
645
646
647
648
    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
François Perrad's avatar
François Perrad committed
649
        n = sign * 0.0
650
651
652
653
    elseif expo == 0xFF then
        if mant == 0 then
            n = sign * huge
        else
François Perrad's avatar
François Perrad committed
654
            n = 0.0/0.0
655
        end
François Perrad's avatar
François Perrad committed
656
    else
François Perrad's avatar
François Perrad committed
657
        n = sign * ldexp(1.0 + mant / 0x800000, expo - 0x7F)
François Perrad's avatar
François Perrad committed
658
659
660
661
662
    end
    c.i = i+4
    return n
end

François Perrad's avatar
minor    
François Perrad committed
663
unpackers['double'] = function (c)
François Perrad's avatar
François Perrad committed
664
665
    local s, i, j = c.s, c.i, c.j
    if i+7 > j then
François Perrad's avatar
François Perrad committed
666
667
        c:underflow(i+7)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
668
669
670
671
672
673
674
675
676
677
678
679
    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
François Perrad's avatar
François Perrad committed
680
        n = sign * 0.0
681
682
683
684
    elseif expo == 0x7FF then
        if mant == 0 then
            n = sign * huge
        else
François Perrad's avatar
François Perrad committed
685
            n = 0.0/0.0
686
        end
François Perrad's avatar
François Perrad committed
687
    else
François Perrad's avatar
François Perrad committed
688
        n = sign * ldexp(1.0 + mant / 4503599627370496.0, expo - 0x3FF)
François Perrad's avatar
François Perrad committed
689
690
691
692
693
694
695
696
697
    end
    c.i = i+8
    return n
end

unpackers['fixnum_pos'] = function (c, val)
    return val
end

François Perrad's avatar
minor    
François Perrad committed
698
unpackers['uint8'] = function (c)
François Perrad's avatar
François Perrad committed
699
700
    local s, i, j = c.s, c.i, c.j
    if i > j then
François Perrad's avatar
François Perrad committed
701
702
        c:underflow(i)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
703
704
705
706
707
708
    end
    local b1 = s:sub(i, i):byte()
    c.i = i+1
    return b1
end

François Perrad's avatar
minor    
François Perrad committed
709
unpackers['uint16'] = function (c)
François Perrad's avatar
François Perrad committed
710
711
    local s, i, j = c.s, c.i, c.j
    if i+1 > j then
François Perrad's avatar
François Perrad committed
712
713
        c:underflow(i+1)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
714
715
716
717
718
719
    end
    local b1, b2 = s:sub(i, i+1):byte(1, 2)
    c.i = i+2
    return b1 * 0x100 + b2
end

François Perrad's avatar
minor    
François Perrad committed
720
unpackers['uint32'] = function (c)
François Perrad's avatar
François Perrad committed
721
722
    local s, i, j = c.s, c.i, c.j
    if i+3 > j then
François Perrad's avatar
François Perrad committed
723
724
        c:underflow(i+3)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
725
726
727
728
729
730
    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

François Perrad's avatar
minor    
François Perrad committed
731
unpackers['uint64'] = function (c)
François Perrad's avatar
François Perrad committed
732
733
    local s, i, j = c.s, c.i, c.j
    if i+7 > j then
François Perrad's avatar
François Perrad committed
734
735
        c:underflow(i+7)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
736
737
738
739
740
741
    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

François Perrad's avatar
François Perrad committed
742
unpackers['fixnum_neg'] = function (c, val)
François Perrad's avatar
François Perrad committed
743
    return val - 0x100
François Perrad's avatar
François Perrad committed
744
745
end

François Perrad's avatar
minor    
François Perrad committed
746
unpackers['int8'] = function (c)
François Perrad's avatar
François Perrad committed
747
748
    local s, i, j = c.s, c.i, c.j
    if i > j then
François Perrad's avatar
François Perrad committed
749
750
        c:underflow(i)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
751
752
753
    end
    local b1 = s:sub(i, i):byte()
    c.i = i+1
François Perrad's avatar
François Perrad committed
754
755
756
757
758
    if b1 < 0x80 then
        return b1
    else
        return b1 - 0x100
    end
François Perrad's avatar
François Perrad committed
759
760
end

François Perrad's avatar
minor    
François Perrad committed
761
unpackers['int16'] = function (c)
François Perrad's avatar
François Perrad committed
762
763
    local s, i, j = c.s, c.i, c.j
    if i+1 > j then
François Perrad's avatar
François Perrad committed
764
765
        c:underflow(i+1)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
766
767
768
    end
    local b1, b2 = s:sub(i, i+1):byte(1, 2)
    c.i = i+2
François Perrad's avatar
François Perrad committed
769
770
771
772
773
    if b1 < 0x80 then
        return b1 * 0x100 + b2
    else
        return ((b1 - 0xFF) * 0x100 + (b2 - 0xFF)) - 1
    end
François Perrad's avatar
François Perrad committed
774
775
end

François Perrad's avatar
minor    
François Perrad committed
776
unpackers['int32'] = function (c)
François Perrad's avatar
François Perrad committed
777
778
    local s, i, j = c.s, c.i, c.j
    if i+3 > j then
François Perrad's avatar
François Perrad committed
779
780
        c:underflow(i+3)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
781
782
783
    end
    local b1, b2, b3, b4 = s:sub(i, i+3):byte(1, 4)
    c.i = i+4
François Perrad's avatar
François Perrad committed
784
785
786
787
788
    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
François Perrad's avatar
François Perrad committed
789
790
end

François Perrad's avatar
minor    
François Perrad committed
791
unpackers['int64'] = function (c)
François Perrad's avatar
François Perrad committed
792
793
    local s, i, j = c.s, c.i, c.j
    if i+7 > j then
François Perrad's avatar
François Perrad committed
794
795
        c:underflow(i+7)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
796
797
798
    end
    local b1, b2, b3, b4, b5, b6, b7, b8 = s:sub(i, i+7):byte(1, 8)
    c.i = i+8
François Perrad's avatar
François Perrad committed
799
800
801
802
803
    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
François Perrad's avatar
François Perrad committed
804
805
end

François Perrad's avatar
François Perrad committed
806
unpackers['fixstr'] = function (c, val)
François Perrad's avatar
François Perrad committed
807
808
809
810
    local s, i, j = c.s, c.i, c.j
    local n = val % 0x20
    local e = i+n-1
    if e > j then
François Perrad's avatar
François Perrad committed
811
812
        c:underflow(e)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
813
        e = i+n-1
François Perrad's avatar
François Perrad committed
814
815
816
817
818
    end
    c.i = i+n
    return s:sub(i, e)
end

François Perrad's avatar
François Perrad committed
819
820
821
822
823
824
825
826
827
828
829
830
831
unpackers['str8'] = function (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 n = s:sub(i, i):byte()
    i = i+1
    c.i = i
    local e = i+n-1
    if e > j then
        c:underflow(e)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
832
        e = i+n-1
François Perrad's avatar
François Perrad committed
833
834
835
836
837
    end
    c.i = i+n
    return s:sub(i, e)
end

François Perrad's avatar
François Perrad committed
838
unpackers['str16'] = function (c)
François Perrad's avatar
François Perrad committed
839
840
    local s, i, j = c.s, c.i, c.j
    if i+1 > j then
François Perrad's avatar
François Perrad committed
841
842
        c:underflow(i+1)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
843
844
845
    end
    local b1, b2 = s:sub(i, i+1):byte(1, 2)
    i = i+2
François Perrad's avatar
François Perrad committed
846
    c.i = i
François Perrad's avatar
François Perrad committed
847
848
849
    local n = b1 * 0x100 + b2
    local e = i+n-1
    if e > j then
François Perrad's avatar
François Perrad committed
850
851
        c:underflow(e)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
852
        e = i+n-1
François Perrad's avatar
François Perrad committed
853
854
855
856
857
    end
    c.i = i+n
    return s:sub(i, e)
end

François Perrad's avatar
François Perrad committed
858
unpackers['str32'] = function (c)
François Perrad's avatar
François Perrad committed
859
860
    local s, i, j = c.s, c.i, c.j
    if i+3 > j then
François Perrad's avatar
François Perrad committed
861
862
        c:underflow(i+3)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
863
864
865
    end
    local b1, b2, b3, b4 = s:sub(i, i+3):byte(1, 4)
    i = i+4
François Perrad's avatar
François Perrad committed
866
    c.i = i
François Perrad's avatar
François Perrad committed
867
868
    local n = ((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4
    local e = i+n-1
869
    if e > j or n < 0 then
François Perrad's avatar
François Perrad committed
870
871
        c:underflow(e)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
872
        e = i+n-1
François Perrad's avatar
François Perrad committed
873
874
875
876
877
    end
    c.i = i+n
    return s:sub(i, e)
end

François Perrad's avatar
François Perrad committed
878
879
880
881
unpackers['bin8'] = unpackers['str8']
unpackers['bin16'] = unpackers['str16']
unpackers['bin32'] = unpackers['str32']

François Perrad's avatar
François Perrad committed
882
883
884
885
unpackers['fixarray'] = function (c, val)
    return unpack_array(c, val % 0x10)
end

François Perrad's avatar
minor    
François Perrad committed
886
unpackers['array16'] = function (c)
François Perrad's avatar
François Perrad committed
887
888
    local s, i, j = c.s, c.i, c.j
    if i+1 > j then
François Perrad's avatar
François Perrad committed
889
890
        c:underflow(i+1)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
891
892
893
894
895
896
    end
    local b1, b2 = s:sub(i, i+1):byte(1, 2)
    c.i = i+2
    return unpack_array(c, b1 * 0x100 + b2)
end

François Perrad's avatar
minor    
François Perrad committed
897
unpackers['array32'] = function (c)
François Perrad's avatar
François Perrad committed
898
899
    local s, i, j = c.s, c.i, c.j
    if i+3 > j then
François Perrad's avatar
François Perrad committed
900
901
        c:underflow(i+3)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
902
903
904
905
906
907
908
909
910
911
    end
    local b1, b2, b3, b4 = s:sub(i, i+3):byte(1, 4)
    c.i = i+4
    return unpack_array(c, ((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4)
end

unpackers['fixmap'] = function (c, val)
    return unpack_map(c, val % 0x10)
end

François Perrad's avatar
minor    
François Perrad committed
912
unpackers['map16'] = function (c)
François Perrad's avatar
François Perrad committed
913
914
    local s, i, j = c.s, c.i, c.j
    if i+1 > j then
François Perrad's avatar
François Perrad committed
915
916
        c:underflow(i+1)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
917
918
919
920
921
922
    end
    local b1, b2 = s:sub(i, i+1):byte(1, 2)
    c.i = i+2
    return unpack_map(c, b1 * 0x100 + b2)
end

François Perrad's avatar
minor    
François Perrad committed
923
unpackers['map32'] = function (c)
François Perrad's avatar
François Perrad committed
924
925
    local s, i, j = c.s, c.i, c.j
    if i+3 > j then
François Perrad's avatar
François Perrad committed
926
927
        c:underflow(i+3)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
928
929
930
931
932
933
    end
    local b1, b2, b3, b4 = s:sub(i, i+3):byte(1, 4)
    c.i = i+4
    return unpack_map(c, ((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4)
end

François Perrad's avatar
François Perrad committed
934
function m.build_ext (tag, data)
François Perrad's avatar
François Perrad committed
935
936
937
    return nil
end

François Perrad's avatar
François Perrad committed
938
for k = 0, 4 do
François Perrad's avatar
François Perrad committed
939
    local n = tointeger(2^k)
940
    unpackers['fixext' .. tostring(n)] = function (c)
François Perrad's avatar
François Perrad committed
941
942
943
944
945
        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
François Perrad's avatar
François Perrad committed
946
        local tag = s:sub(i, i):byte()
François Perrad's avatar
François Perrad committed
947
948
949
950
951
952
        i = i+1
        c.i = i
        local e = i+n-1
        if e > j then
            c:underflow(e)
            s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
953
            e = i+n-1
François Perrad's avatar
François Perrad committed
954
955
        end
        c.i = i+n
François Perrad's avatar
François Perrad committed
956
        return m.build_ext(tag < 0x80 and tag or tag - 0x100, s:sub(i, e))
François Perrad's avatar
François Perrad committed
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
    end
end

unpackers['ext8'] = function (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 n = s:sub(i, i):byte()
    i = i+1
    c.i = i
    if i > j then
        c:underflow(i)
        s, i, j = c.s, c.i, c.j
    end
François Perrad's avatar
François Perrad committed
973
    local tag = s:sub(i, i):byte()
François Perrad's avatar
François Perrad committed
974
975
976
977
978
979
    i = i+1
    c.i = i
    local e = i+n-1
    if e > j then
        c:underflow(e)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
980
        e = i+n-1
François Perrad's avatar
François Perrad committed
981
982
    end
    c.i = i+n
François Perrad's avatar
François Perrad committed
983
    return m.build_ext(tag < 0x80 and tag or tag - 0x100, s:sub(i, e))
François Perrad's avatar
François Perrad committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
end

unpackers['ext16'] = function (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)
    i = i+2
    c.i = i
    local n = b1 * 0x100 + b2
    if i > j then
        c:underflow(i)
        s, i, j = c.s, c.i, c.j
    end
François Perrad's avatar
François Perrad committed
1000
    local tag = s:sub(i, i):byte()
François Perrad's avatar
François Perrad committed
1001
1002
1003
1004
1005
1006
    i = i+1
    c.i = i
    local e = i+n-1
    if e > j then
        c:underflow(e)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
1007
        e = i+n-1
François Perrad's avatar
François Perrad committed
1008
1009
    end
    c.i = i+n
François Perrad's avatar
François Perrad committed
1010
    return m.build_ext(tag < 0x80 and tag or tag - 0x100, s:sub(i, e))
François Perrad's avatar
François Perrad committed
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
end

unpackers['ext32'] = function (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)
    i = i+4
    c.i = i
    local n = ((b1 * 0x100 + b2) * 0x100 + b3) * 0x100 + b4
    if i > j then
        c:underflow(i)
        s, i, j = c.s, c.i, c.j
    end
François Perrad's avatar
François Perrad committed
1027
    local tag = s:sub(i, i):byte()
François Perrad's avatar
François Perrad committed
1028
1029
1030
    i = i+1
    c.i = i
    local e = i+n-1
1031
    if e > j or n < 0 then
François Perrad's avatar
François Perrad committed
1032
1033
        c:underflow(e)
        s, i, j = c.s, c.i, c.j
François Perrad's avatar
François Perrad committed
1034
        e = i+n-1
François Perrad's avatar
François Perrad committed
1035
1036
    end
    c.i = i+n
François Perrad's avatar
François Perrad committed
1037
    return m.build_ext(tag < 0x80 and tag or tag - 0x100, s:sub(i, e))
François Perrad's avatar
François Perrad committed
1038
1039
end

François Perrad's avatar
François Perrad committed
1040

François Perrad's avatar
François Perrad committed
1041
1042
1043
local function cursor_string (str)
    return {
        s = str,
François Perrad's avatar
François Perrad committed
1044
        i = 1,
François Perrad's avatar
François Perrad committed
1045
        j = #str,
François Perrad's avatar
François Perrad committed
1046
1047
1048
1049
        underflow = function (self)
                        error "missing bytes"
                    end,
    }
François Perrad's avatar
François Perrad committed
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
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)
François Perrad's avatar
François Perrad committed
1077
    local data = unpackers['any'](cursor)
François Perrad's avatar
François Perrad committed
1078
1079
1080
1081
1082
1083
    if cursor.i < cursor.j then
        error "extra bytes"
    end
    return data
end

François Perrad's avatar
François Perrad committed
1084
1085
function m.unpacker (src)
    if type(src) == 'string' then
François Perrad's avatar
François Perrad committed
1086
        local cursor = cursor_string(src)
François Perrad's avatar
François Perrad committed
1087
1088
1089
1090
1091
        return function ()
            if cursor.i <= cursor.j then
                return cursor.i, unpackers['any'](cursor)
            end
        end
François Perrad's avatar
François Perrad committed
1092
    elseif type(src) == 'function' then
François Perrad's avatar
François Perrad committed
1093
        local cursor = cursor_loader(src)
François Perrad's avatar
François Perrad committed
1094
1095
1096
1097
1098
        return function ()
            if cursor.i > cursor.j then
                pcall(cursor.underflow, cursor, cursor.i)
            end
            if cursor.i <= cursor.j then
François Perrad's avatar
François Perrad committed
1099
                return true, unpackers['any'](cursor)
François Perrad's avatar
François Perrad committed
1100
            end
François Perrad's avatar
François Perrad committed
1101
        end
1102
    else
François Perrad's avatar
François Perrad committed
1103
        argerror('unpacker', 1, "string or function expected, got " .. type(src))
François Perrad's avatar
François Perrad committed
1104
1105
    end
end
François Perrad's avatar
François Perrad committed
1106

François Perrad's avatar
François Perrad committed
1107
set_string'string_compat'
1108
set_integer'unsigned'
François Perrad's avatar
François Perrad committed
1109
1110
1111
if NUMBER_INTEGRAL then
    set_number'integer'
elseif SIZEOF_NUMBER == 4 then
François Perrad's avatar
François Perrad committed
1112
1113
    maxinteger = 16777215
    mininteger = -maxinteger
1114
    m.small_lua = true
1115
1116
1117
    unpackers['double'] = nil
    unpackers['uint64'] = nil
    unpackers['int64'] = nil
François Perrad's avatar
François Perrad committed
1118
1119
    set_number'float'
else
François Perrad's avatar
François Perrad committed
1120
1121
    maxinteger = 9007199254740991
    mininteger = -maxinteger
François Perrad's avatar
François Perrad committed
1122
    set_number'double'
François Perrad's avatar
François Perrad committed
1123
1124
1125
    if SIZEOF_NUMBER > 8 then
        m.long_double = true
    end
François Perrad's avatar
François Perrad committed
1126
end
François Perrad's avatar
François Perrad committed
1127
set_array'without_hole'
François Perrad's avatar
François Perrad committed
1128

François Perrad's avatar
François Perrad committed
1129
m._VERSION = '0.3.6'
François Perrad's avatar
François Perrad committed
1130
m._DESCRIPTION = "lua-MessagePack : a pure Lua implementation"
François Perrad's avatar
François Perrad committed
1131
m._COPYRIGHT = "Copyright (c) 2012-2016 Francois Perrad"
François Perrad's avatar
François Perrad committed
1132
1133
1134
1135
1136
return m
--
-- This library is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--