t8rt0t

inflateBit32.lua

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