t8rt0t

zzlib.lua

Mar 31st, 2024 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1. --THIS IS NOT MINE
  2.  
  3. -- zzlib - zlib decompression in Lua - Implementation-independent code
  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 (F*CK PASTEBIN CENSORSHIP JUST HONESTLY FCK OFF) 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. if not inflateBit32 then pcall(function() os.loadAPI("libs/inflateBit32.lua") end) end
  13. if not inflateBwo then pcall(function() os.loadAPI("libs/inflateBwo.lua") end) end
  14.  
  15.  
  16. local unpack = table.unpack or unpack
  17. local infl
  18.  
  19. local lua_version = tonumber(_VERSION:match("^Lua (.*)"))
  20. if not lua_version or lua_version < 5.3 then
  21.   -- older version of Lua or Luajit being used - use bit/bit32-based implementation
  22.   --infl = require("inflate-bit32")
  23.     infl = inflateBit32
  24. else
  25.   -- From Lua 5.3, use implementation based on bitwise operators
  26.   infl = inflateBwo --require("inflate-bwo")
  27. end
  28.  
  29. local function arraytostr(array)
  30.   local tmp = {}
  31.   local size = #array
  32.   local pos = 1
  33.   local imax = 1
  34.   while size > 0 do
  35.     local bsize = size>=2048 and 2048 or size
  36.     local s = string.char(unpack(array,pos,pos+bsize-1))
  37.     pos = pos + bsize
  38.     size = size - bsize
  39.     local i = 1
  40.     while tmp[i] do
  41.       s = tmp[i]..s
  42.       tmp[i] = nil
  43.       i = i + 1
  44.     end
  45.     if i > imax then
  46.       imax = i
  47.     end
  48.     tmp[i] = s
  49.   end
  50.   local str = ""
  51.   for i=1,imax do
  52.     if tmp[i] then
  53.       str = tmp[i]..str
  54.     end
  55.   end
  56.   return str
  57. end
  58.  
  59. local function inflate_gzip(bs)
  60.   local id1,id2,cm,flg = bs.buf:byte(1,4)
  61.   if id1 ~= 31 or id2 ~= 139 then
  62.     error("invalid gzip header")
  63.   end
  64.   if cm ~= 8 then
  65.     error("only deflate format is supported")
  66.   end
  67.   bs.pos=11
  68.   if infl.band(flg,4) ~= 0 then
  69.     local xl1,xl2 = bs.buf.byte(bs.pos,bs.pos+1)
  70.     local xlen = xl2*256+xl1
  71.     bs.pos = bs.pos+xlen+2
  72.   end
  73.   if infl.band(flg,8) ~= 0 then
  74.     local pos = bs.buf:find("\0",bs.pos)
  75.     bs.pos = pos+1
  76.   end
  77.   if infl.band(flg,16) ~= 0 then
  78.     local pos = bs.buf:find("\0",bs.pos)
  79.     bs.pos = pos+1
  80.   end
  81.   if infl.band(flg,2) ~= 0 then
  82.     -- TODO: check header CRC16
  83.     bs.pos = bs.pos+2
  84.   end
  85.   local result = arraytostr(infl.main(bs))
  86.   local crc = bs:getb(8)+256*(bs:getb(8)+256*(bs:getb(8)+256*bs:getb(8)))
  87.   bs:close()
  88.   if crc ~= infl.crc32(result) then
  89.     error("checksum verification failed")
  90.   end
  91.   return result
  92. end
  93.  
  94. -- compute Adler-32 checksum
  95. local function adler32(s)
  96.   local s1 = 1
  97.   local s2 = 0
  98.   for i=1,#s do
  99.     local c = s:byte(i)
  100.     s1 = (s1+c)%65521
  101.     s2 = (s2+s1)%65521
  102.   end
  103.   return s2*65536+s1
  104. end
  105.  
  106. local function inflate_zlib(bs)
  107.   local cmf = bs.buf:byte(1)
  108.   local flg = bs.buf:byte(2)
  109.   if (cmf*256+flg)%31 ~= 0 then
  110.     error("zlib header check bits are incorrect")
  111.   end
  112.   if infl.band(cmf,15) ~= 8 then
  113.     error("only deflate format is supported")
  114.   end
  115.   if infl.rshift(cmf,4) ~= 7 then
  116.     error("unsupported window size")
  117.   end
  118.   if infl.band(flg,32) ~= 0 then
  119.     error("preset dictionary not implemented")
  120.   end
  121.   bs.pos=3
  122.   local result = arraytostr(infl.main(bs))
  123.   local adler = ((bs:getb(8)*256+bs:getb(8))*256+bs:getb(8))*256+bs:getb(8)
  124.   bs:close()
  125.   if adler ~= adler32(result) then
  126.     error("checksum verification failed")
  127.   end
  128.   return result
  129. end
  130.  
  131. local function inflate_raw(buf,offset,crc)
  132.   local bs = infl.bitstream_init(buf)
  133.   bs.pos = offset
  134.   local result = arraytostr(infl.main(bs))
  135.   if crc and crc ~= infl.crc32(result) then
  136.     error("checksum verification failed")
  137.   end
  138.   return result
  139. end
  140.  
  141. function gunzipf(filename)
  142.   local file,err = io.open(filename,"rb")
  143.   if not file then
  144.     return nil,err
  145.   end
  146.   return inflate_gzip(infl.bitstream_init(file))
  147. end
  148.  
  149. function gunzip(str)
  150.   return inflate_gzip(infl.bitstream_init(str))
  151. end
  152.  
  153. function inflate(str)
  154.   return inflate_zlib(infl.bitstream_init(str))
  155. end
  156.  
  157. local function int2le(str,pos)
  158.   local a,b = str:byte(pos,pos+1)
  159.   return b*256+a
  160. end
  161.  
  162. local function int4le(str,pos)
  163.   local a,b,c,d = str:byte(pos,pos+3)
  164.   return ((d*256+c)*256+b)*256+a
  165. end
  166.  
  167. local function nextfile(buf,p)
  168.   if int4le(buf,p) ~= 0x02014b50 then
  169.     -- end of central directory list
  170.     return
  171.   end
  172.   -- local flag = int2le(buf,p+8)
  173.   local packed = int2le(buf,p+10)~=0
  174.   local crc = int4le(buf,p+16)
  175.   local namelen = int2le(buf,p+28)
  176.   local name = buf:sub(p+46,p+45+namelen)
  177.   local offset = int4le(buf,p+42)+1
  178.   p = p+46+namelen+int2le(buf,p+30)+int2le(buf,p+32)
  179.   if int4le(buf,offset) ~= 0x04034b50 then
  180.     error("invalid local header signature")
  181.   end
  182.   local size = int4le(buf,offset+18)
  183.   local extlen = int2le(buf,offset+28)
  184.   offset = offset+30+namelen+extlen
  185.   return p,name,offset,size,packed,crc
  186. end
  187.  
  188. function files(buf)
  189.   local p = #buf-21
  190.   if int4le(buf,p) ~= 0x06054b50 then
  191.     -- not sure there is a reliable way to locate the end of central directory record
  192.     -- if it has a variable sized comment field
  193.     error(".ZIP file comments not supported")
  194.   end
  195.   local cdoffset = int4le(buf,p+16)+1
  196.   return nextfile,buf,cdoffset
  197. end
  198.  
  199. function unzip(buf,arg1,arg2)
  200.   if type(arg1) == "number" then
  201.     -- mode 1: unpack data from specified position in zip file
  202.     return inflate_raw(buf,arg1,arg2)
  203.   end
  204.   -- mode 2: search and unpack file from zip file
  205.   local filename = arg1
  206.   for _,name,offset,size,packed,crc in files(buf) do
  207.     if name == filename then
  208.       local result
  209.       if not packed then
  210.         -- no compression
  211.         result = buf:sub(offset,offset+size-1)
  212.       else
  213.         -- DEFLATE compression
  214.         result = inflate_raw(buf,offset,crc)
  215.       end
  216.       return result
  217.     end
  218.   end
  219.   error("file '"..filename.."' not found in ZIP archive")
  220. end
Advertisement
Add Comment
Please, Sign In to add comment