t8rt0t

inflate-bwo.lua

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