Advertisement
ZNZNCOOP

BinarySerializer

Dec 26th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. local binser = {}
  2.  
  3. binser.version = "0.0.1"
  4.  
  5. function binser.serialize(t)
  6.     if(type(t) ~= 'table') then
  7.         error("serialize t argument not table.")
  8.     end
  9.     local bin = binser.wbyte(3)..binser.wbyte(3)..binser.wbyte(3)..binser.wtbyte({binser.getBytes(#binser.version)})..binser.version
  10.     for k,v in pairs(t) do
  11.         bin = bin..binser.wtbyte({6, 6, 6, 6, 6})
  12.         if(type(k) == "number") then
  13.             bin = bin..binser.wbyte(4)..binser.wtbyte({binser.getBytes(k)})
  14.         elseif(type(k) == "string") then
  15.             bin = bin..binser.wbyte(5)..binser.wtbyte({binser.getBytes(#tostring(k))})..tostring(k)
  16.         end
  17.         bin = bin..binser.wtbyte({7, 7, 7, 7, 7})
  18.         if(type(v) == "number") then
  19.             bin = bin..binser.wbyte(4)..binser.wtbyte({binser.getBytes(v)})
  20.         elseif(type(v) == "string") then
  21.             bin = bin..binser.wbyte(5)..binser.wtbyte({binser.getBytes(#v)})..v
  22.         elseif(type(v) == "table") then
  23.             bin = bin..binser.serialize(v)
  24.         elseif(type(v)) == "boolean" then
  25.             local boolean = 0
  26.             if(v == true) then boolean = 1 end
  27.             if(v == false) then boolean = 0 end
  28.             bin = bin..binser.wbyte(6)..binser.wbyte(boolean)
  29.         elseif(type(v) == "function") then
  30.             error("value in function.")
  31.         end
  32.     end
  33.     return bin
  34. end
  35.  
  36. function binser.unserialize(str)
  37.     local bytes = {}
  38.     local len = #str
  39.     for i = 1, len do
  40.         bytes[i] = string.byte(str, i)
  41.         --io.write(bytes[i].." ")
  42.     end
  43.     --print() print()
  44.     local p = 1
  45.     if(bytes[p + 0] == 3 and bytes[p + 1] == 3 and bytes[p + 2] == 3) then
  46.         p = p + 3
  47.         local len = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
  48.         p = p + 4
  49.         local version = string.sub(str, p, p + len - 1)
  50.         local len = #bytes
  51.         local tableout = {}
  52.         local key, value = nil, nil
  53.         while p < len do
  54.             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
  55.                 p = p + 5
  56.                 if(bytes[p] == 4) then
  57.                     p = p + 1
  58.                     key = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
  59.                     p = p + 4
  60.                 end
  61.                 if(bytes[p] == 5) then
  62.                     p = p + 1
  63.                     local length = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
  64.                     p = p + 4
  65.                     key = string.sub(str, p, p + length - 1)
  66.                     p = p + length - 1
  67.                 end
  68.             end
  69.             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
  70.                 p = p + 5
  71.                 if(bytes[p] == 4) then
  72.                     p = p + 1
  73.                     value = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
  74.                     p = p + 4
  75.                     tableout[key] = value
  76.                 elseif(bytes[p] == 5) then
  77.                     p = p + 1
  78.                     local length = binser.getNumber({bytes[p + 0], bytes[p + 1], bytes[p + 2], bytes[p + 3]})
  79.                     p = p + 4
  80.                     value = string.sub(str, p, p + length - 1)
  81.                     tableout[key] = value
  82.                     p = p + length - 1
  83.                 elseif(bytes[p] == 6) then
  84.                     p = p + 1
  85.                     value = bytes[p]
  86.                     if(value == 1) then
  87.                         value = true
  88.                     elseif(value == 0) then
  89.                         value = false
  90.                     end
  91.                     tableout[key] = value
  92.                 end
  93.             end
  94.             p = p + 1
  95.         end
  96.         return tableout, p
  97.     end
  98.     return nil
  99. end
  100.  
  101. function binser.wbyte(byte)
  102.     return string.char(byte)
  103. end
  104.  
  105. function binser.getBytes(number)
  106.     local o = 255
  107.     local n1, n2, n3, n4 = 0, 0, 0, 0
  108.     n1 = number%o
  109.     number = math.floor(number/o)
  110.     n2 = number%o
  111.     number = math.floor(number/o)
  112.     n3 = number%o
  113.     number = math.floor(number/o)
  114.     n4 = number%o
  115.     number = math.floor(number/o)
  116.     return n1, n2, n3, n4
  117. end
  118.  
  119. function binser.getNumber(bytes)
  120.     if(type(bytes) ~= "table") then
  121.         error("getNumber bytes not table.")
  122.     end
  123.     local number = 0
  124.     local p = 0
  125.     for i = 1, #bytes do
  126.         number = number + bytes[i] * math.pow(255, p)
  127.         p = p + 1
  128.     end
  129.     return number
  130. end
  131.  
  132. function binser.wtbyte(t)
  133.     if(type(t) ~= "table") then
  134.         error("t not table.")
  135.     end
  136.     local bin = ""
  137.     for i=1, #t do
  138.         bin = bin..binser.wbyte(t[i])
  139.     end
  140.     return bin
  141. end
  142.  
  143. function binser.showt(t)
  144.     if(type(t) ~= "table") then
  145.         error("t not table.")
  146.     end
  147.     for i = 1, #t do
  148.         io.write(t[i].." ")
  149.     end
  150.     print()
  151. end
  152.  
  153. return binser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement