Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local binser = {}
- binser.version = "0.0.1"
- function binser.serialize(t)
- if(type(t) ~= 'table') then
- error("serialize t argument not table.")
- end
- local bin = binser.wbyte(3)..binser.wbyte(3)..binser.wbyte(3)..binser.wtbyte({binser.getBytes(#binser.version)})..binser.version
- for k,v in pairs(t) do
- bin = bin..binser.wtbyte({6, 6, 6, 6, 6})
- if(type(k) == "number") then
- bin = bin..binser.wbyte(4)..binser.wtbyte({binser.getBytes(k)})
- elseif(type(k) == "string") then
- bin = bin..binser.wbyte(5)..binser.wtbyte({binser.getBytes(#tostring(k))})..tostring(k)
- end
- bin = bin..binser.wtbyte({7, 7, 7, 7, 7})
- if(type(v) == "number") then
- bin = bin..binser.wbyte(4)..binser.wtbyte({binser.getBytes(v)})
- elseif(type(v) == "string") then
- bin = bin..binser.wbyte(5)..binser.wtbyte({binser.getBytes(#v)})..v
- elseif(type(v) == "table") then
- bin = bin..binser.serialize(v)
- elseif(type(v)) == "boolean" then
- local boolean = 0
- if(v == true) then boolean = 1 end
- if(v == false) then boolean = 0 end
- bin = bin..binser.wbyte(6)..binser.wbyte(boolean)
- elseif(type(v) == "function") then
- error("value in function.")
- end
- end
- return bin
- end
- function binser.unserialize(str)
- local bytes = {}
- local len = #str
- for i = 1, len do
- bytes[i] = string.byte(str, i)
- --io.write(bytes[i].." ")
- end
- --print() print()
- local p = 1
- if(bytes[p + 0] == 3 and bytes[p + 1] == 3 and bytes[p + 2] == 3) then
- p = p + 3
- local len = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
- p = p + 4
- local version = string.sub(str, p, p + len - 1)
- local len = #bytes
- local tableout = {}
- local key, value = nil, nil
- while p < len do
- if(bytes[p + 0] == 6 and bytes[p + 1] == 6 and bytes[p + 2] == 6 and bytes[p + 3] == 6 and bytes[p + 4] == 6) then
- p = p + 5
- if(bytes[p] == 4) then
- p = p + 1
- key = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
- p = p + 4
- end
- if(bytes[p] == 5) then
- p = p + 1
- local length = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
- p = p + 4
- key = string.sub(str, p, p + length - 1)
- p = p + length - 1
- end
- end
- if(bytes[p + 0] == 7 and bytes[p + 1] == 7 and bytes[p + 2] == 7 and bytes[p + 3] == 7 and bytes[p + 4] == 7) then
- p = p + 5
- if(bytes[p] == 4) then
- p = p + 1
- value = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
- p = p + 4
- tableout[key] = value
- elseif(bytes[p] == 5) then
- p = p + 1
- local length = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
- p = p + 4
- value = string.sub(str, p, p + length - 1)
- tableout[key] = value
- p = p + length - 1
- elseif(bytes[p] == 6) then
- p = p + 1
- value = bytes[p]
- if(value == 1) then
- value = true
- elseif(value == 0) then
- value = false
- end
- tableout[key] = value
- end
- end
- p = p + 1
- end
- return tableout, p
- end
- return nil
- end
- function binser.wbyte(byte)
- return string.char(byte)
- end
- function binser.getBytes(number)
- local o = 255
- local n1, n2, n3, n4 = 0, 0, 0, 0
- n1 = number%o
- number = math.floor(number/o)
- n2 = number%o
- number = math.floor(number/o)
- n3 = number%o
- number = math.floor(number/o)
- n4 = number%o
- number = math.floor(number/o)
- return n1, n2, n3, n4
- end
- function binser.getNumber(bytes)
- if(type(bytes) ~= "table") then
- error("getNumber bytes not table.")
- end
- local number = 0
- local p = 0
- for i = 1, #bytes do
- number = number + bytes[i] * math.pow(255, p)
- p = p + 1
- end
- return number
- end
- function binser.wtbyte(t)
- if(type(t) ~= "table") then
- error("t not table.")
- end
- local bin = ""
- for i=1, #t do
- bin = bin..binser.wbyte(t[i])
- end
- return bin
- end
- function binser.showt(t)
- if(type(t) ~= "table") then
- error("t not table.")
- end
- for i = 1, #t do
- io.write(t[i].." ")
- end
- print()
- end
- return binser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement