Advertisement
Shiranuit

Serial

Jun 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.14 KB | None | 0 0
  1. function Writer(val)
  2.     local buff = {}
  3.     if val then
  4.         if type(val) == "table" then
  5.             buff=val
  6.         elseif type(val) == "string" then
  7.             buff={}
  8.             for i=1, #val do
  9.                 buff[#buff+1]=val:sub(i,i)
  10.             end
  11.         else
  12.             buff={}
  13.         end
  14.     else
  15.         buff={}
  16.     end
  17.     local function intLenght(d)
  18.         for i=1, 7 do
  19.             local v = 2^i
  20.             local v1 = 2^(v-1)-1
  21.             if v1 < d then
  22.                 return v
  23.             end
  24.         end
  25.         return 4
  26.     end
  27.     local function write(byte)
  28.         buff[#buff+1]=string.char(byte)
  29.     end
  30.     local function writeByte(val)
  31.         if type(val)~="number" then
  32.             return
  33.         end
  34.         write(val)
  35.     end
  36.     local function writeInt(val, size)
  37.         if type(val)~="number" then
  38.             return
  39.         end
  40.         for i=1, size/8 do
  41.             write(bit.band(val,255))
  42.             val=bit.brshift(val,8)
  43.         end
  44.     end
  45.    
  46.     local function writeString(val)
  47.         if
  48.             type(val)~="string" then
  49.             return
  50.         end
  51.         writeInt(#val,32)
  52.         for i=1, #val do
  53.             write(val:sub(i,i):byte())
  54.         end
  55.     end
  56.  
  57.     local function writeBool(val)
  58.         if type(val)~="boolean" then
  59.             return
  60.         end
  61.         write(val and 1 or 0)
  62.     end
  63.    
  64.     local writeTable, writeFunc
  65.    
  66.     local function writeVar(val)
  67.         if type(val) == "number" then
  68.             write(1)
  69.             writeInt(val,32)
  70.         elseif type(val) == "string" then
  71.             write(2)
  72.             writeString(val)
  73.         elseif type(val) == "boolean" then
  74.             write(3)
  75.             writeBool(val)
  76.         elseif type(val) == "table" then
  77.             write(4)
  78.             writeTable(val)
  79.         elseif type(val) == "function" then
  80.             write(5)
  81.             writeFunc(val)
  82.         end
  83.     end
  84.  
  85.     writeTable = function(val)
  86.         if type(val)~="table" then
  87.             return
  88.         end
  89.         local n=0
  90.         for k, v in pairs(val) do
  91.             n=n+1
  92.         end
  93.         writeInt(n,32)
  94.         for k, v in pairs(val) do
  95.             writeVar(k)
  96.             writeVar(v)
  97.         end
  98.     end
  99.  
  100.     writeFunc = function(val)
  101.         if type(val)~="function" then
  102.             return
  103.         end
  104.         writeString(string.dump(val))
  105.     end
  106.    
  107.     local function getBuff()
  108.         local b = table.concat(buff,"")
  109.         buff={}
  110.         return b
  111.     end
  112.    
  113.     return {getBuff=getBuff,writeByte=writeByte,writeInt=writeInt,writeString=writeString,writeBool=writeBool,writeTable=writeTable,writeFunc=writeFunc,writeVar=writeVar}
  114. end
  115.  
  116. function Reader(val)
  117.     local buff = {}
  118.     if val then
  119.         if type(val) == "table" then
  120.             buff=val
  121.         elseif type(val) == "string" then
  122.             buff={}
  123.             for i=1, #val do
  124.                 buff[#buff+1]=val:sub(i,i)
  125.             end
  126.         else
  127.             buff={}
  128.         end
  129.     else
  130.         buff={}
  131.     end
  132.    
  133.     local function checkSize(n)
  134.         n=n or 1
  135.         for i=1, n do
  136.             if not buff[i] then
  137.                 error("Out of limit",1)
  138.             end
  139.         end
  140.     end
  141.    
  142.     local function read(n)
  143.         n = n or 1
  144.         checkSize(n)
  145.         local t = {}
  146.         for i=1, n do
  147.             t[#t+1]=buff[1]:byte()
  148.             table.remove(buff,1)
  149.         end
  150.         return t
  151.     end
  152.  
  153.     local function readByte()
  154.         checkSize()
  155.         local v = buff[1]
  156.         table.remove(buff,1)
  157.         return v:byte()
  158.     end
  159.  
  160.     local function readInt(size)
  161.         if size then
  162.             checkSize(size/8)
  163.             local val = 0
  164.             for i=1, size/8 do
  165.                 val = bit.bor(val,bit.blshift(bit.band(buff[i]:byte(),255),(i-1)*8))
  166.             end
  167.             for i=1, size/8 do
  168.                 table.remove(buff,1)
  169.             end
  170.             return val
  171.         end
  172.     end
  173.  
  174.     local function readString()
  175.         local size = readInt(32)
  176.         checkSize(size)
  177.         local t = ""
  178.         for i=1, size do
  179.             t=t..buff[1]
  180.             table.remove(buff,1)
  181.         end
  182.         return t
  183.     end
  184.  
  185.     local function readBool()
  186.         checkSize()
  187.         return read()[1]==1 and true or false
  188.     end
  189.     local readTable, readFunc
  190.     local function readVar()
  191.         checkSize()
  192.         local id = buff[1]:byte()
  193.         table.remove(buff,1)
  194.         if id == 0 then
  195.             return readByte()
  196.         elseif id == 1 then
  197.             return readInt(32)
  198.         elseif id == 2 then
  199.             return readString()
  200.         elseif id == 3 then
  201.             return readBool()
  202.         elseif id == 4 then
  203.             return readTable()
  204.         elseif id == 5 then
  205.             return readFunc()
  206.         end
  207.     end
  208.  
  209.     readTable = function()
  210.         local size = readInt(32)
  211.         if size then
  212.             local t = {}
  213.             for i=1, size do
  214.                 local key = readVar()
  215.                 local val = readVar()
  216.                 t[key]=val
  217.             end
  218.             return t
  219.         end
  220.     end
  221.  
  222.     readFunc = function()
  223.         local dt = readString()
  224.         return loadstring(dt)
  225.     end
  226.    
  227.     function getBuff()
  228.         return buff
  229.     end
  230.  
  231.     return {readByte=readByte,readInt=readInt,readString=readString,readBool=readBool,readTable=readTable,readFunc=readFunc,readVar=readVar}
  232. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement