t8rt0t

libs/inflate-bit32.lua

Apr 19th, 2024
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.26 KB | None | 0 0
  1.  
  2. -- zzlib-bit32 - zlib decompression in Lua - version using bit/bit32 libraries
  3.  
  4. -- Copyright (c) 2016-2024 Francois Galea <fgalea at free.fr>
  5. -- This program is free software. It comes without any warranty, to
  6. -- the extent permitted by applicable law. You can redistribute it
  7. -- and/or modify it under the terms of the Do What The Fck You Want
  8. -- To Public License, Version 2, as published by Sam Hocevar. See
  9. -- the COPYING file or http://www.wtfpl.net/ for more details.
  10.  
  11.  
  12. local inflate = {}
  13.  
  14. local bit = bit32 or bit
  15.  
  16. inflate.band = bit.band
  17. inflate.rshift = bit.rshift
  18.  
  19. function inflate.bitstream_init(file)
  20.   local bs = {
  21.     file = file,  -- the open file handle
  22.     buf = nil,    -- character buffer
  23.     len = nil,    -- length of character buffer
  24.     pos = 1,      -- position in char buffer, next to be read
  25.     b = 0,        -- bit buffer
  26.     n = 0,        -- number of bits in buffer
  27.   }
  28.   -- get rid of n first bits
  29.   function bs:flushb(n)
  30.     self.n = self.n - n
  31.     self.b = bit.rshift(self.b,n)
  32.   end
  33.   -- returns the next byte from the stream, excluding any half-read bytes
  34.   function bs:next_byte()
  35.     if self.pos > self.len then
  36.       self.buf = self.file:read(4096)
  37.       self.len = self.buf:len()
  38.       self.pos = 1
  39.     end
  40.     local pos = self.pos
  41.     self.pos = pos + 1
  42.     return self.buf:byte(pos)
  43.   end
  44.   -- peek a number of n bits from stream
  45.   function bs:peekb(n)
  46.     while self.n < n do
  47.       self.b = self.b + bit.lshift(self:next_byte(),self.n)
  48.       self.n = self.n + 8
  49.     end
  50.     return bit.band(self.b,bit.lshift(1,n)-1)
  51.   end
  52.   -- get a number of n bits from stream
  53.   function bs:getb(n)
  54.     local ret = bs:peekb(n)
  55.     self.n = self.n - n
  56.     self.b = bit.rshift(self.b,n)
  57.     return ret
  58.   end
  59.   -- get next variable-size of maximum size=n element from stream, according to Huffman table
  60.   function bs:getv(hufftable,n)
  61.     local e = hufftable[bs:peekb(n)]
  62.     local len = bit.band(e,15)
  63.     local ret = bit.rshift(e,4)
  64.     self.n = self.n - len
  65.     self.b = bit.rshift(self.b,len)
  66.     return ret
  67.   end
  68.   function bs:close()
  69.     if self.file then
  70.       self.file:close()
  71.     end
  72.   end
  73.   if type(file) == "string" then
  74.     bs.file = nil
  75.     bs.buf = file
  76.   else
  77.     bs.buf = file:read(4096)
  78.   end
  79.   bs.len = bs.buf:len()
  80.   return bs
  81. end
  82.  
  83. local function hufftable_create(depths)
  84.   local nvalues = #depths
  85.   local nbits = 1
  86.   local bl_count = {}
  87.   local next_code = {}
  88.   for i=1,nvalues do
  89.     local d = depths[i]
  90.     if d > nbits then
  91.       nbits = d
  92.     end
  93.     bl_count[d] = (bl_count[d] or 0) + 1
  94.   end
  95.   local table = {}
  96.   local code = 0
  97.   bl_count[0] = 0
  98.   for i=1,nbits do
  99.     code = (code + (bl_count[i-1] or 0)) * 2
  100.     next_code[i] = code
  101.   end
  102.   for i=1,nvalues do
  103.     local len = depths[i] or 0
  104.     if len > 0 then
  105.       local e = (i-1)*16 + len
  106.       local code = next_code[len]
  107.       local rcode = 0
  108.       for j=1,len do
  109.         rcode = rcode + bit.lshift(bit.band(1,bit.rshift(code,j-1)),len-j)
  110.       end
  111.       for j=0,2^nbits-1,2^len do
  112.         table[j+rcode] = e
  113.       end
  114.       next_code[len] = next_code[len] + 1
  115.     end
  116.   end
  117.   return table,nbits
  118. end
  119.  
  120. local function block_loop(out,bs,nlit,ndist,littable,disttable)
  121.   local lit
  122.   repeat
  123.     lit = bs:getv(littable,nlit)
  124.     if lit < 256 then
  125.       table.insert(out,lit)
  126.     elseif lit > 256 then
  127.       local nbits = 0
  128.       local size = 3
  129.       local dist = 1
  130.       if lit < 265 then
  131.         size = size + lit - 257
  132.       elseif lit < 285 then
  133.         nbits = bit.rshift(lit-261,2)
  134.         size = size + bit.lshift(bit.band(lit-261,3)+4,nbits)
  135.       else
  136.         size = 258
  137.       end
  138.       if nbits > 0 then
  139.         size = size + bs:getb(nbits)
  140.       end
  141.       local v = bs:getv(disttable,ndist)
  142.       if v < 4 then
  143.         dist = dist + v
  144.       else
  145.         nbits = bit.rshift(v-2,1)
  146.         dist = dist + bit.lshift(bit.band(v,1)+2,nbits)
  147.         dist = dist + bs:getb(nbits)
  148.       end
  149.       local p = #out-dist+1
  150.       while size > 0 do
  151.         table.insert(out,out[p])
  152.         p = p + 1
  153.         size = size - 1
  154.       end
  155.     end
  156.   until lit == 256
  157. end
  158.  
  159. local function block_dynamic(out,bs)
  160.   local order = { 17, 18, 19, 1, 9, 8, 10, 7, 11, 6, 12, 5, 13, 4, 14, 3, 15, 2, 16 }
  161.   local hlit = 257 + bs:getb(5)
  162.   local hdist = 1 + bs:getb(5)
  163.   local hclen = 4 + bs:getb(4)
  164.   local depths = {}
  165.   for i=1,hclen do
  166.     local v = bs:getb(3)
  167.     depths[order[i]] = v
  168.   end
  169.   for i=hclen+1,19 do
  170.     depths[order[i]] = 0
  171.   end
  172.   local lengthtable,nlen = hufftable_create(depths)
  173.   local i=1
  174.   while i<=hlit+hdist do
  175.     local v = bs:getv(lengthtable,nlen)
  176.     if v < 16 then
  177.       depths[i] = v
  178.       i = i + 1
  179.     elseif v < 19 then
  180.       local nbt = {2,3,7}
  181.       local nb = nbt[v-15]
  182.       local c = 0
  183.       local n = 3 + bs:getb(nb)
  184.       if v == 16 then
  185.         c = depths[i-1]
  186.       elseif v == 18 then
  187.         n = n + 8
  188.       end
  189.       for j=1,n do
  190.         depths[i] = c
  191.         i = i + 1
  192.       end
  193.     else
  194.       error("wrong entry in depth table for literal/length alphabet: "..v);
  195.     end
  196.   end
  197.   local litdepths = {} for i=1,hlit do table.insert(litdepths,depths[i]) end
  198.   local littable,nlit = hufftable_create(litdepths)
  199.   local distdepths = {} for i=hlit+1,#depths do table.insert(distdepths,depths[i]) end
  200.   local disttable,ndist = hufftable_create(distdepths)
  201.   block_loop(out,bs,nlit,ndist,littable,disttable)
  202. end
  203.  
  204. local function block_static(out,bs)
  205.   local cnt = { 144, 112, 24, 8 }
  206.   local dpt = { 8, 9, 7, 8 }
  207.   local depths = {}
  208.   for i=1,4 do
  209.     local d = dpt[i]
  210.     for j=1,cnt[i] do
  211.       table.insert(depths,d)
  212.     end
  213.   end
  214.   local littable,nlit = hufftable_create(depths)
  215.   depths = {}
  216.   for i=1,32 do
  217.     depths[i] = 5
  218.   end
  219.   local disttable,ndist = hufftable_create(depths)
  220.   block_loop(out,bs,nlit,ndist,littable,disttable)
  221. end
  222.  
  223. local function block_uncompressed(out,bs)
  224.   bs:flushb(bit.band(bs.n,7))
  225.   local len = bs:getb(16)
  226.   if bs.n > 0 then
  227.     error("Unexpected.. should be zero remaining bits in buffer.")
  228.   end
  229.   local nlen = bs:getb(16)
  230.   if bit.bxor(len,nlen) ~= 65535 then
  231.     error("LEN and NLEN don't match")
  232.   end
  233.   for i=1,len do
  234.     table.insert(out,bs:next_byte())
  235.   end
  236. end
  237.  
  238. function inflate.main(bs)
  239.   local last,type
  240.   local output = {}
  241.   repeat
  242.     local block
  243.     last = bs:getb(1)
  244.     type = bs:getb(2)
  245.     if type == 0 then
  246.       block_uncompressed(output,bs)
  247.     elseif type == 1 then
  248.       block_static(output,bs)
  249.     elseif type == 2 then
  250.       block_dynamic(output,bs)
  251.     else
  252.       error("unsupported block type")
  253.     end
  254.   until last == 1
  255.   bs:flushb(bit.band(bs.n,7))
  256.   return output
  257. end
  258.  
  259. local crc32_table
  260. function inflate.crc32(s,crc)
  261.   if not crc32_table then
  262.     crc32_table = {}
  263.     for i=0,255 do
  264.       local r=i
  265.       for j=1,8 do
  266.         r = bit.bxor(bit.rshift(r,1),bit.band(0xedb88320,bit.bnot(bit.band(r,1)-1)))
  267.       end
  268.       crc32_table[i] = r
  269.     end
  270.   end
  271.   crc = bit.bnot(crc or 0)
  272.   for i=1,#s do
  273.     local c = s:byte(i)
  274.     crc = bit.bxor(crc32_table[bit.bxor(c,bit.band(crc,0xff))],bit.rshift(crc,8))
  275.   end
  276.   crc = bit.bnot(crc)
  277.   if crc<0 then
  278.     -- in Lua < 5.2, sign extension was performed
  279.     crc = crc + 4294967296
  280.   end
  281.   return crc
  282. end
  283.  
  284. return inflate
Advertisement
Add Comment
Please, Sign In to add comment