Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
--[[----------------------------------------------------------------------------
MessagePack encoder / decoder written in pure Lua 5.3
written by Sebastian Steinhauer <s.steinhauer@yahoo.de>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
--]]----------------------------------------------------------------------------
local pcall = pcall
--local select = select
--local next = next
local type = type
local string_pack = string.pack
local string_unpack = string.unpack
local string_sub = string.sub
local table_concat = table.concat
local table_unpack = table.unpack
local utf8_len = utf8.len
--[[----------------------------------------------------------------------------
Encoder
--]]----------------------------------------------------------------------------
local encode_value -- forward declaration
local encoder_functions = {
["nil"] = function()
return string_pack("B", 0xc0)
end,
["boolean"] = function(value)
if value then
return string_pack("B", 0xc3)
else
return string_pack("B", 0xc2)
end
end,
["number"] = function(value)
if value % 1 == 0 then
if value >= 0 then
if value < 128 then
return string_pack("B", value)
elseif value <= 0xff then
return string_pack("BB", 0xcc, value)
elseif value <= 0xffff then
return string_pack(">BI2", 0xcd, value)
elseif value <= 0xffffffff then
return string_pack(">BI4", 0xce, value)
else
return string_pack(">BI8", 0xcf, value)
end
else
if value >= -32 then
return string_pack("B", 0xe0 + (value + 32))
elseif value >= -128 then
return string_pack("Bb", 0xd0, value)
elseif value >= -32768 then
return string_pack(">Bi2", 0xd1, value)
elseif value >= -2147483648 then
return string_pack(">Bi4", 0xd2, value)
else
return string_pack(">Bi8", 0xd3, value)
end
end
else
local test = string_unpack("f", string_pack("f", value))
if test == value then -- check if we can use float
return string_pack(">Bf", 0xca, value)
else
return string_pack(">Bd", 0xcb, value)
end
end
end,
["string"] = function(value)
local len = #value
if utf8_len(value) then -- check if it is a real utf8 string or just byte junk
if len < 32 then
return string_pack("B", 0xa0 + len) .. value
elseif len < 256 then
return string_pack(">Bs1", 0xd9, value)
elseif len < 65536 then
return string_pack(">Bs2", 0xda, value)
else
return string_pack(">Bs4", 0xdb, value)
end
else -- encode it as byte-junk :)
if len < 256 then
return string_pack(">Bs1", 0xc4, value)
elseif len < 65536 then
return string_pack(">Bs2", 0xc5, value)
else
return string_pack(">Bs4", 0xc6, value)
end
end
end,
["table"] = function(value)
local IsArray = true
local Expected = 1
for Key in next, value do
if Key ~= Expected then
IsArray = false
break
end
Expected = Expected + 1
end
if IsArray then -- it seems to be a proper Lua array
local elements = {}
local length = 0
for i, v in ipairs(value) do
length = length + 1
elements[i] = encode_value(v)
end
if length < 16 then
return string_pack(">B", 0x90 + length) .. table_concat(elements)
elseif length < 65536 then
return string_pack(">Bi2", 0xdc, length) .. table_concat(elements)
else
return string_pack(">Bi4", 0xdd, length) .. table_concat(elements)
end
else -- encode as a map
local elements = {}
local elementsLength = 0
for k, v in next, value do
elementsLength = elementsLength + 1
elements[elementsLength] = encode_value(k)
elementsLength = elementsLength + 1
elements[elementsLength] = encode_value(v)
end
local length = elementsLength // 2
if length < 16 then
return string_pack(">B", 0x80 + length) .. table_concat(elements)
elseif length < 65536 then
return string_pack(">Bi2", 0xde, length) .. table_concat(elements)
else
return string_pack(">Bi4", 0xdf, length) .. table_concat(elements)
end
end
end,
}
function encode_value(value)
return encoder_functions[type(value)](value)
end
--[[
local function encode(...)
local data = {}
local length = 0
for i = 1, select("#", ...) do
length = length + 1
data[length] = encode_value(select(i, ...))
end
return table_concat(data)
end
]]
--[[----------------------------------------------------------------------------
Decoder
--]]----------------------------------------------------------------------------
local decode_value -- forward declaration
local decoder_functions = {
[0xc0] = function(_, position)
return nil, position
end,
[0xc2] = function(_, position)
return false, position
end,
[0xc3] = function(_, position)
return true, position
end,
[0xc4] = function(data, position)
return string_unpack(">s1", data, position)
end,
[0xc5] = function(data, position)
return string_unpack(">s2", data, position)
end,
[0xc6] = function(data, position)
return string_unpack(">s4", data, position)
end,
[0xca] = function(data, position)
return string_unpack(">f", data, position)
end,
[0xcb] = function(data, position)
return string_unpack(">d", data, position)
end,
[0xcc] = function(data, position)
return string_unpack(">B", data, position)
end,
[0xcd] = function(data, position)
return string_unpack(">I2", data, position)
end,
[0xce] = function(data, position)
return string_unpack(">I4", data, position)
end,
[0xcf] = function(data, position)
return string_unpack(">I8", data, position)
end,
[0xd0] = function(data, position)
return string_unpack(">b", data, position)
end,
[0xd1] = function(data, position)
return string_unpack(">i2", data, position)
end,
[0xd2] = function(data, position)
return string_unpack(">i4", data, position)
end,
[0xd3] = function(data, position)
return string_unpack(">i8", data, position)
end,
[0xd9] = function(data, position)
return string_unpack(">s1", data, position)
end,
[0xda] = function(data, position)
return string_unpack(">s2", data, position)
end,
[0xdb] = function(data, position)
return string_unpack(">s4", data, position)
end,
[0xdc] = function(data, position)
local length
length, position = string_unpack(">I2", data, position)
local elements, value = {}
for i = 1, length do
value, position = decode_value(data, position)
elements[i] = value
end
return elements, position
end,
[0xdd] = function(data, position)
local length
length, position = string_unpack(">I4", data, position)
local elements, value = {}
for i = 1, length do
value, position = decode_value(data, position)
elements[i] = value
end
return elements, position
end,
[0xde] = function(data, position)
local length
length, position = string_unpack(">I2", data, position)
local elements, value = {}
for i = 1, length do
value, position = decode_value(data, position)
elements[i] = value
end
return elements, position
end,
[0xdf] = function(data, position)
local length
length, position = string_unpack(">I4", data, position)
local elements, value = {}
for i = 1, length do
value, position = decode_value(data, position)
elements[i] = value
end
return elements, position
end,
}
-- add fix-array, fix-map, fix-string, fix-int stuff
for i = 0x00, 0x7f do
decoder_functions[i] = function(_, position)
return i, position
end
end
for i = 0x80, 0x8f do
decoder_functions[i] = function(data, position)
local elements, key, value = {}
for _ = 1, i - 0x80 do
key, position = decode_value(data, position)
value, position = decode_value(data, position)
elements[key] = value
end
return elements, position
end
end
for i = 0x90, 0x9f do
decoder_functions[i] = function(data, position)
local elements, value = {}
for index = 1, i - 0x90 do
value, position = decode_value(data, position)
elements[index] = value
end
return elements, position
end
end
for i = 0xa0, 0xbf do
decoder_functions[i] = function(data, position)
local length = i - 0xa0
return string_sub(data, position, position + length - 1), position + length
end
end
for i = 0xe0, 0xff do
decoder_functions[i] = function(_, position)
return -32 + (i - 0xe0), position
end
end
function decode_value(data, position)
local byte, value
byte, position = string_unpack("B", data, position)
value, position = decoder_functions[byte](data, position)
return value, position
end
--[[----------------------------------------------------------------------------
Interface
--]]----------------------------------------------------------------------------
return {
_AUTHOR = "Sebastian Steinhauer <s.steinhauer@yahoo.de>",
_VERSION = "0.6.0",
-- primary encode function
encode = function(...)
local data, ok = {}
for i = 1, select("#", ...) do
ok, data[i] = pcall(encode_value, select(i, ...))
if not ok then
return nil, "cannot encode MessagePack"
end
end
return table_concat(data)
end,
-- encode just one value
encode_one = function(value)
local ok, data = pcall(encode_value, value)
if ok then
return data
else
return nil, "cannot encode MessagePack"
end
end,
-- primary decode function
decode = function(data, position)
local values, value, ok = {}
position = position or 1
local length = 0
while position <= #data do
ok, value, position = pcall(decode_value, data, position)
if ok then
length = length + 1
values[length] = value
else
return nil, "cannot decode MessagePack"
end
end
return table_unpack(values, 1, length)
end,
-- decode just one value
decode_one = function(data, position)
local value, ok
ok, value, position = pcall(decode_value, data, position or 1)
if ok then
return value, position
else
return nil, "cannot decode MessagePack"
end
end,
}
--[[----------------------------------------------------------------------------
--]]----------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment