t8rt0t

unzipAPI.lua

Mar 31st, 2024 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.27 KB | None | 0 0
  1. if not docs then os.loadAPI("apis/docs.lua") end
  2. --Type registry
  3. local types = {
  4.     ["Bits"]={colors.lightBlue,"A series of bits from 1-8. A byte would be represented by 87654321"},
  5.     ["Byte"]={colors.yellow,"A single byte, number 0-255"},
  6.     ["DecodedBlock"]={colors.lightBlue,"A list of decoded bytes"},
  7.     ["HuffmanCode"]={colors.purple,"A code for decompressing values"}
  8. }
  9. for k,v in pairs(types) do
  10.     docs.registerType(k,v[1],v[2])
  11. end
  12.  
  13. --Takes a byte and turns it into a series of bits
  14. --@Arg Byte byte: A byte 0-255 to be converted
  15. --@OptionalArg boolean bool: If true, converts the bits to false/true instead of 0/1
  16. --@Returns Bits bits: A table of the bits from 0-7
  17. local function byteToBits(byte,bool)
  18.     local bits = {}
  19.     for i=0,7 do
  20.         bits[i] = byte%2
  21.         if bool then bits[i] = bits[i] == 1 end
  22.         byte = math.floor(byte/2)
  23.     end
  24.     return bits
  25. end
  26. --Basically your python version of if statements
  27. --@Arg any element: The element do be converted into a boolean
  28. --@Returns boolean e: The finished result
  29. function bool(element)
  30.     if not element then return false end
  31.     if tonumber(element) then return number ~= 0 end
  32.     if type(element) == "string" or type(element) == "table" then return #element ~= 0 end
  33.     if type(element) == "boolean" then return element end
  34.     return true
  35. end
  36. --Increments a bit pos and the bytes index
  37. --@Arg number pos: The position within a byte
  38. --@Arg number index: The index with a bytes table
  39. --@Returns number newPos: The new position
  40. --@Returns number newIndex: The new index
  41. local function incrementPos(pos,index)
  42.     pos = pos+1
  43.     if pos >= 8 then index = index+1 end
  44.     return pos%8,index
  45. end
  46. --Gets the next bit from a byte
  47. --@Arg table bytes: The list of bytes
  48. --@Arg table bitpos: {pos= position within bit, index = index within bytes table}
  49. --@Returns number bit: The bit
  50. local function nextBit(bytes,bitpos)
  51.     print(bitpos.pos .. " " .. bitpos.index .. " ")
  52.     local b = byteToBits(bytes[bitpos.index])[bitpos.pos]
  53.     --print(b)
  54.     bitpos.pos,bitpos.index = incrementPos(bitpos.pos,bitpos.index)
  55.     return b
  56. end
  57. --Gets the next byte, resetting the bit pos to 0
  58. --@Arg table bytes: the list of bytes
  59. --@Arg table bitpos: {pos= position within bit, index = index within bytes table}
  60. --@Returns Byte b: the next byte to be used
  61. local function nextByte(bytes,bitpos)
  62.     local b = bytes[bitpos.index]
  63.     bitpos.pos,bitpos.index = 0,bitpos.index+1
  64.     return b
  65. end
  66. --Gets the next series of bits as a number, big endian
  67. --@Arg table bytes: the list of bytes
  68. --@Arg table bitpos: {pos= position within bit, index = index within bytes table}
  69. --@OptionalArg number amount: The number of bits that should be read
  70. --@Returns number num: The value of the read bits
  71. local function nextBits(bytes,bitpos,amount)
  72.     local num = 0
  73.     amount = tonumber(amount) or 1
  74.     for i=1,amount do
  75.         local b = nextBit(bytes,bitpos)
  76.         num = num*2+b
  77.     end
  78.     print(num)
  79.     return num
  80. end
  81. --Creates a huffman code
  82. --@OptionalArg table bytes: The series of bytes to create the sequence from. If not provided, will generate the default huffman
  83. --@OptionalArg table bitpos: {pos= position within bit, index = index within bytes table}
  84. --@Returns HuffmanCode h: A table with {decodeNext=function(bytes,bitpos)}
  85. local function Huffman(bytes,bitpos)
  86.     if not bytes then
  87.         return {decodeNext = function(by,bp)
  88.                 local v = nextBits(by,bp,7)
  89.                 if v <= 23 then
  90.                     return 256+v
  91.                 end
  92.                 v = 2*v+nextBit(by,bp)
  93.                 if v >= 48 and v <= 191 then
  94.                     return v - 48
  95.                 elseif v >= 192 and v <= 199 then
  96.                     return v + 88
  97.                 end
  98.                 v = 2*v+nextBit(by,bp)
  99.                 if v >= 400 then return v+256 end
  100.                 return 256 --Exit character
  101.             end,
  102.             decodeLength = function(by,bp,v)
  103.                 v = v - 254
  104.                 w = math.max(math.floor((v-7)/4),0) --number of additional bits to read
  105.                 y = (v-7)%4
  106.                 return false
  107.             end
  108.         }
  109.     end
  110. end
  111.  
  112. --from zzlib
  113. local function hufftable_create(depths)
  114.   local nvalues = #depths
  115.   local nbits = 1
  116.   local bl_count = {}
  117.   local next_code = {}
  118.   for i=1,nvalues do
  119.     local d = depths[i]
  120.     if d > nbits then
  121.       nbits = d
  122.     end
  123.     bl_count[d] = (bl_count[d] or 0) + 1
  124.   end
  125.   local table = {}
  126.   local code = 0
  127.   bl_count[0] = 0
  128.   for i=1,nbits do
  129.     code = (code + (bl_count[i-1] or 0)) * 2
  130.     next_code[i] = code
  131.   end
  132.   for i=1,nvalues do
  133.     local len = depths[i] or 0
  134.     if len > 0 then
  135.       local e = (i-1)*16 + len
  136.       local code = next_code[len]
  137.       local rcode = 0
  138.       for j=1,len do
  139.         rcode = rcode + bit.blshift(bit.band(1,bit.brshift(code,j-1)),len-j)
  140.       end
  141.       for j=0,2^nbits-1,2^len do
  142.         table[j+rcode] = e
  143.       end
  144.       next_code[len] = next_code[len] + 1
  145.     end
  146.   end
  147.   return table,nbits
  148. end
  149. --@Type Huffman
  150. local cnt = { 144, 112, 24, 8 }
  151. local dpt = { 8, 9, 7, 8 }
  152. local depths = {}
  153. for i=1,4 do
  154.     local d = dpt[i]
  155.     for j=1,cnt[i] do
  156.         table.insert(depths,d)
  157.     end
  158. end
  159. local LITTABLE,nlit = hufftable_create(depths)
  160. depths = {}
  161. for i=1,32 do
  162.     depths[i] = 5
  163. end
  164. local DISTTABLE,ndist = hufftable_create(depths)
  165.  
  166.    
  167. --Gets the stupid member thingie
  168. --@Arg File file: The file opened in rb mode to read
  169. --@Returns boolean false: If sussy stuff happens
  170. --@Returns Byte id1, Byte id2, Byte cm, number mtime, Byte xfl, byte opSys, table extra, string name, string comment, number fhcrc, table data, number crc32, number iSize
  171. local function readMember(file)
  172.     local name, comment = "",""
  173.     local fhcrc
  174.     local extra  = {}
  175.     local id1,id2 = file.read(), file.read() --bytes 1 and 2 are for the ids
  176.     local cm = file.read() --byte 3 is for the compression method, usually 0x08
  177.     local flgs = byteToBits(file.read(),true)
  178.     --Ignore flgs[0] because we dont care about text/nontext. Dealing with flgs[1] FHCRC later
  179.     local mtime = 0
  180.     for i=0,3 do
  181.         mtime = file.read()*(256^i)+mtime --changed to little endian
  182.     end
  183.     local xfl = file.read()
  184.     local opSys = file.read()
  185.     if flgs[2] then --FEXTRA
  186.         local len = file.read()
  187.         len = len+256*file.read() --changed to little endian
  188.         while len > 0 do
  189.             local si1 = file.read()
  190.             local si2 = file.read()
  191.             local sLen = file.read()
  192.             sLen = sLen+256*file.read() --changed to little endian
  193.             len = len - 4
  194.             local sData = {}
  195.             while sLen > 0 and len > 0 do
  196.                 table.insert(sData,file.read())
  197.                 sLen = sLen-1
  198.                 len = len-1
  199.             end
  200.             extra[si1+si2*256] = sData --little endian as well for index
  201.         end  
  202.     end
  203.     if flgs[3] then --FNAME
  204.         name = ""
  205.         local byte = file.read()
  206.         while byte > 0 do
  207.             name = name .. string.char(byte)
  208.             byte = file.read()
  209.         end
  210.     end
  211.     if flgs[4] then --FCOMMENT
  212.         comment = ""
  213.         local byte = file.read()
  214.         while byte > 0 do
  215.             name = name .. string.char(byte)
  216.             byte = file.read()
  217.         end
  218.     end
  219.     if flgs[1] then --dealing with FHCRC
  220.         fhcrc = file.read()
  221.         fhcrc = fhcrc+256*file.read() --made little endian
  222.     end
  223.     --Reading the data
  224.     local data = {}
  225.     while file.read(0) do
  226.         table.insert(data,file.read())
  227.     end
  228.     local crc32 = 0
  229.     for i=#data-7,#data-4 do
  230.         crc32 = crc32+data[i]*256 --little endian
  231.     end
  232.     local iSize = 0
  233.     for i=#data-3,#data do
  234.         iSize = iSize+256*data[i] --little endian
  235.     end
  236.     return id1,id2,cm,mtime,xfl,opSys,extra,name,comment,fhcrc,data,crc32,iSize
  237. end
  238. --Gets the next value from a huffman table
  239. local function getNextV(huff,bytes,bitpos)
  240.     local b = 0
  241.     for i=1,10 do
  242.         b = b*2+nextBit(bytes,bitpos)
  243.         if huff[b] then return huff[b] end
  244.     end
  245.     return 256
  246. end
  247. --@Arg table bytes: A list of bytes from the data
  248. --@Arg number pos: The position within the bytes to start at, 0-7
  249. --@Arg number index: The position of the starting byte in the bytes table
  250. --@OptionalArg table dataBlocks: The list of blocks from the bytes
  251. local function inflateBlock(bytes,pos,index, dataBlocks)
  252.     if type(dataBlocks) ~= "table" then dataBlocks = {} end
  253.     local bits = byteToBits(bytes[index])
  254.     local bitPos = {pos=pos,index=index} --Creating bitpos table to be used
  255.     local bfinal = nextBit(bytes,bitPos)
  256.     local bType = nextBit(bytes,bitPos)
  257.     bType = 2*bType+nextBit(bytes,bitPos)
  258.     if bType == 3 then return false, "you fcked up dummy" end --3 is reserved and invalid
  259.     if bType == 0 then --Easy case, with no compression
  260.         if bitPos.pos ~= 0 then --If there are remaining bits in the current byte, they get ignored
  261.             bitPos = 0
  262.             index = index+1
  263.         end
  264.         local len = nextByte(bytes,bitPos)
  265.         len = 256*len+nextByte(bytes,bitPos)
  266.         local nlen = nextByte(bytes,bitPos)
  267.         nlen = 256*nlen+nextByte(bytes,bitPos) --Not used
  268.         local data = {}
  269.         for i=1,len do
  270.             table.insert(data,nextByte(bytes,bitPos))
  271.         end
  272.         table.insert(dataBlocks,data)
  273.         if bfinal == 1 then
  274.             return dataBlocks
  275.         end
  276.         return inflateBlock(bytes,bitPos.pos,bitPos.index,dataBlocks)
  277.     end
  278.     local lit_table, dist_table
  279.     if bType == 1 then --Static huffman code, predefined
  280.         lit_table = LITTABLE
  281.         dist_table = DISTTABLE
  282.     end
  283.     if bType == 2 then --Why
  284.         --huffman = Huffman(bytes,bitPos)
  285.         local order = { 17, 18, 19, 1, 9, 8, 10, 7, 11, 6, 12, 5, 13, 4, 14, 3, 15, 2, 16 }
  286.         local hlit = 257 + nextBits(bytes,bitPos,5)
  287.         local hdist = 1 + nextBits(bytes,bitPos,5)
  288.         local hclen = 4 + nextBits(bytes,bitPos,4)
  289.         local depths = {}
  290.         for i=1,hclen do
  291.             local v = nextBits(bytes,bitPos,3)
  292.             depths[order[i]] = v
  293.         end
  294.         for i=hclen+1,19 do
  295.             depths[order[i]] = 0
  296.         end
  297.         local lengthtable,nlen = hufftable_create(depths)
  298.         local i=1
  299.         while i<=hlit+hdist do
  300.             local v = getNextV(lengthtable,bytes,bitPos)
  301.             if v < 16 then
  302.                 depths[i] = v
  303.                 i = i + 1
  304.             elseif v < 19 then
  305.                 local nbt = {2,3,7}
  306.                 local nb = nbt[v-15]
  307.                 local c = 0
  308.                 local n = 3 + nextBits(bytes,bitPos,nb)
  309.                 if v == 16 then
  310.                     c = depths[i-1]
  311.                 elseif v == 18 then
  312.                     n = n + 8
  313.                 end
  314.                 for j=1,n do
  315.                     depths[i] = c
  316.                     i = i + 1
  317.                 end
  318.             else
  319.                 error("wrong entry in depth table for literal/length alphabet: "..v);
  320.             end
  321.         end
  322.         local litdepths = {} for i=1,hlit do table.insert(litdepths,depths[i]) end
  323.         lit_table,nlit = hufftable_create(litdepths)
  324.         local distdepths = {} for i=hlit+1,#depths do table.insert(distdepths,depths[i]) end
  325.         dist_table,ndist = hufftable_create(distdepths)
  326.     end
  327.     local lit
  328.     local data = {}
  329.     local startPos = bitPos.pos+8*bitPos.index
  330.     repeat
  331.         lit = getNextV(lit_table,bytes,bitPos)
  332.         if lit < 256 then
  333.               table.insert(data,lit)
  334.         elseif lit > 256 then
  335.               local nbits = 0
  336.               local size = 3
  337.               local dist = 1
  338.               if lit < 265 then
  339.                   size = size + lit - 257
  340.               elseif lit < 285 then
  341.                   nbits = bit.brshift(lit-261,2)
  342.                   size = size + bit.blshift(bit.band(lit-261,3)+4,nbits)
  343.               else
  344.                   size = 258
  345.               end
  346.               if nbits > 0 then
  347.                   size = size + nextBits(bytes,bitPos,nbits)--bs:getb(nbits)
  348.               end
  349.               local v = getNextV(dist_table,bytes,bitPos)--bs:getv(disttable,ndist)
  350.               if v < 4 then
  351.                   dist = dist + v
  352.               else
  353.                   nbits = bit.brshift(v-2,1)
  354.                   dist = dist + bit.blshift(bit.band(v,1)+2,nbits)
  355.                   dist = dist + nextBits(bytes,bitPos,nbits)--bs:getb(nbits)
  356.               end
  357.               local p = bitPos.pos+8*bitPos.index - startPos - dist+1
  358.               while size > 0 do
  359.                   table.insert(data,data[p])
  360.                   p = p + 1
  361.                   size = size - 1
  362.               end
  363.         end
  364.     until lit == 256
  365.     table.insert(dataBlocks,data)
  366.     if bfinal == 1 then return dataBlocks end
  367.     return inflateBlock(bytes, bitPos.pos,bitPos.index,dataBlocks)
  368. end
  369.  
  370. function getUnzippedBytes(path)
  371.     if not fs.exists(path) then return false end
  372.     file = fs.open(path,"rb")
  373.     local id1,id2,cm,mtime,xfl,opSys,extra,name,comment,fhcrc,data,crc32,iSize = readMember(file)
  374.     file.close()
  375.     if id1 ~= 0x1f or id2 ~= 0x8b then
  376.         return false
  377.     end
  378.     if cm ~= 8 then
  379.         return false
  380.     end
  381.     local bytes = inflateBlock(data, 0,1)
  382.     return bytes
  383. end
Add Comment
Please, Sign In to add comment