t8rt0t

libs/zzlib.lua

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