Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local a={_TYPE='module',_NAME='compress.deflatelua',_VERSION='0.3.20111128'}local assert=assert;local error=error;local ipairs=ipairs;local pairs=pairs;local print=print;local require=require;local tostring=tostring;local type=type;local setmetatable=setmetatable;local io=io;local math=math;local b=table.sort;local c=math.max;local d=string.char;local function e(...)local f={}for g=1,select('#',...)do local h=select(g,...)if type(h)~='string'then return h,''end;local i,j=pcall(require,h)if i then return j,h end;f[#f+1]=j end;error(table.concat(f,'\n'),2)end;local k;local l;local m=false;local n=k~=nil;local o,p,q;if n then o=k.band;p=k.lshift;q=k.rshift end;local function r(s)io.stderr:write(s,'\n')end;local function t(...)print('DEBUG',...)end;local function u(s,v)v=v or 1;error({s},v+1)end;local function w(x)local y={}y.outbs=x;y.window={}y.window_pos=1;return y end;local function z(y,A)local B=y.window_pos;y.outbs(A)y.window[B]=A;y.window_pos=B%32768+1 end;local function C(D)return assert(D,'unexpected end of file')end;local function E(F,k)return F%(k+k)>=k end;local function G(H)local I={}local J=setmetatable({},I)function I:__index(K)local L=H(K)J[K]=L;return L end;return J end;local M=G(function(N)return 2^N end)local O=setmetatable({},{__mode='k'})local function P(Q)local R={}function R:read()local S=Q:read(1)if S then return S:byte()end end;return R end;local function T(s)local g=1;local R={}function R:read()local U;if g<=#s then U=s:byte(g)g=g+1 end;return U end;return R end;local function V(H)local g=0;local W=''local R={}function R:read()return H()end;return R end;local function X(Y)local Z=0;local _=0;local R={}function R:nbits_left_in_byte()return _ end;if n then function R:read(a0)a0=a0 or 1;while _<a0 do local A=Y:read()if not A then return end;Z=Z+p(A,_)_=_+8 end;local F;if a0==0 then F=0 elseif a0==32 then F=Z;Z=0 else F=o(Z,q(0xffffffff,32-a0))Z=q(Z,a0)end;_=_-a0;return F end else function R:read(a0)a0=a0 or 1;while _<a0 do local A=Y:read()if not A then return end;Z=Z+M[_]*A;_=_+8 end;local a1=M[a0]local F=Z%a1;Z=(Z-F)/a1;_=_-a0;return F end end;O[R]=true;return R end;local function a2(R)local a3;if O[R]then return R elseif io.type(R)=='file'then a3=X(P(R))elseif type(R)=='string'then a3=X(T(R))elseif type(R)=='function'then a3=X(V(R))else u'unrecognized type'end;return a3 end;local function a4(R)local a3;if io.type(R)=='file'then a3=function(a5)R:write(d(a5))end elseif type(R)=='function'then a3=R else u('unrecognized type: '..tostring(R))end;return a3 end;local function a6(a7,a8)local J={}if a8 then for D,a0 in pairs(a7)do if a0~=0 then J[#J+1]={val=D,nbits=a0}end end else for g=1,#a7-2,2 do local a9,a0,aa=a7[g],a7[g+1],a7[g+2]if a0~=0 then for D=a9,aa-1 do J[#J+1]={val=D,nbits=a0}end end end end;b(J,function(ab,ac)return ab.nbits==ac.nbits and ab.val<ac.val or ab.nbits<ac.nbits end)local ad=1;local a0=0;for g,s in ipairs(J)do if s.nbits~=a0 then ad=ad*M[s.nbits-a0]a0=s.nbits end;s.code=ad;ad=ad+1 end;local ae=math.huge;local af={}for g,s in ipairs(J)do ae=math.min(ae,s.nbits)af[s.code]=s.val end;local ag=n and function(F,a0)local ah=0;for g=1,a0 do ah=p(ah,1)+o(F,1)F=q(F,1)end;return ah end or function(F,a0)local ah=0;for g=1,a0 do local ac=F%2;F=(F-ac)/2;ah=ah*2+ac end;return ah end;local ai=G(function(F)return M[ae]+ag(F,ae)end)function J:read(a3)local ad=1;local a0=0;while 1 do if a0==0 then ad=ai[C(a3:read(ae))]a0=a0+ae else local ac=C(a3:read())a0=a0+1;ad=ad*2+ac end;local D=af[ad]if D then return D end end end;return J end;local function aj(a3)local ak=2^1;local al=2^2;local am=2^3;local an=2^4;local ao=a3:read(8)local ap=a3:read(8)if ao~=31 or ap~=139 then u'not in gzip format'end;local aq=a3:read(8)local ar=a3:read(8)local as=a3:read(32)local at=a3:read(8)local au=a3:read(8)if m then t("CM=",aq)t("FLG=",ar)t("MTIME=",as)t("XFL=",at)t("OS=",au)end;if not au then u'invalid header'end;if E(ar,al)then local av=a3:read(16)local aw=0;for g=1,av do aw=a3:read(8)end;if not aw then u'invalid header'end end;local function ax(a3)repeat local U=a3:read(8)if not U then u'invalid header'end until U==0 end;if E(ar,am)then ax(a3)end;if E(ar,an)then ax(a3)end;if E(ar,ak)then local ay=a3:read(16)if not ay then u'invalid header'end;if m then t("CRC16=",ay)end end end;local function az(a3)local aq=a3:read(4)local aA=a3:read(4)local aB=a3:read(5)local aC=a3:read(1)local aD=a3:read(2)local aE=aA*16+aq;local ar=aB+aC*32+aD*64;if aq~=8 then u("unrecognized zlib compression method: "+aq)end;if aA>7 then u("invalid zlib window size: cinfo="+aA)end;local aF=2^(aA+8)if(aE*256+ar)%31~=0 then u("invalid zlib header (bad fcheck sum)")end;if aC==1 then u("FIX:TODO - FDICT not currently implemented")local aG=a3:read(32)end;return aF end;local function aH(a3)local aI=a3:read(5)local aJ=a3:read(5)local aK=C(a3:read(4))local aL=aK+4;local aM={}local aN={16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}for g=1,aL do local a0=a3:read(3)local D=aN[g]aM[D]=a0 end;local aO=a6(aM,true)local function aP(aQ)local a7={}local a0;local D=0;while D<aQ do local aR=aO:read(a3)local aS;if aR<=15 then aS=1;a0=aR elseif aR==16 then aS=3+C(a3:read(2))elseif aR==17 then aS=3+C(a3:read(3))a0=0 elseif aR==18 then aS=11+C(a3:read(7))a0=0 else error'ASSERT'end;for g=1,aS do a7[D]=a0;D=D+1 end end;local aT=a6(a7,true)return aT end;local aU=aI+257;local aV=aJ+1;local aW=aP(aU)local aX=aP(aV)return aW,aX end;local aY;local aZ;local a_;local b0;local function b1(a3,y,aW,aX)local D=aW:read(a3)if D<256 then z(y,D)elseif D==256 then return true else if not aY then local J={[257]=3}local b2=1;for g=258,285,4 do for b3=g,g+3 do J[b3]=J[b3-1]+b2 end;if g~=258 then b2=b2*2 end end;J[285]=258;aY=J end;if not aZ then local J={}if n then for g=257,285 do local b3=c(g-261,0)J[g]=q(b3,2)end else for g=257,285 do local b3=c(g-261,0)J[g]=(b3-b3%4)/4 end end;J[285]=0;aZ=J end;local b4=aY[D]local b5=aZ[D]local b6=a3:read(b5)local b7=b4+b6;if not a_ then local J={[0]=1}local b2=1;for g=1,29,2 do for b3=g,g+1 do J[b3]=J[b3-1]+b2 end;if g~=1 then b2=b2*2 end end;a_=J end;if not b0 then local J={}if n then for g=0,29 do local b3=c(g-2,0)J[g]=q(b3,1)end else for g=0,29 do local b3=c(g-2,0)J[g]=(b3-b3%2)/2 end end;b0=J end;local b8=aX:read(a3)local b9=a_[b8]local ba=b0[b8]local bb=a3:read(ba)local bc=b9+bb;for g=1,b7 do local bd=(y.window_pos-1-bc)%32768+1;z(y,assert(y.window[bd],'invalid distance'))end end;return false end;local function be(a3,y)local bf=a3:read(1)local bg=a3:read(2)local bh=0;local bi=1;local bj=2;local bk=3;if m then t('bfinal=',bf)t('btype=',bg)end;if bg==bh then a3:read(a3:nbits_left_in_byte())local b7=a3:read(16)local bl=C(a3:read(16))for g=1,b7 do local U=C(a3:read(8))z(y,U)end elseif bg==bi or bg==bj then local aW,aX;if bg==bj then aW,aX=aH(a3)else aW=a6{0,8,144,9,256,7,280,8,288,nil}aX=a6{0,5,32,nil}end;repeat local bm=b1(a3,y,aW,aX)until bm else u'unrecognized compression type'end;return bf~=0 end;function a.inflate(J)local a3=a2(J.input)local x=a4(J.output)local y=w(x)repeat local bn=be(a3,y)until bn end;local bo=a.inflate;function a.gunzip(J)local a3=a2(J.input)local x=a4(J.output)local bp=J.disable_crc;if bp==nil then bp=false end;aj(a3)local bq=0;bo{input=a3,output=bp and x or function(A)bq=l(A,bq)x(A)end}a3:read(a3:nbits_left_in_byte())local br=a3:read(32)local bs=a3:read(32)if m then t('crc32=',br)t('isize=',bs)end;if not bp and bq then if bq~=br then u('invalid compressed data--crc error')end end;if a3:read()then r'trailing garbage ignored'end end;function a.adler32(A,bt)local bu=bt%65536;local bv=(bt-bu)/65536;bu=(bu+A)%65521;bv=(bv+bu)%65521;return bv*65536+bu end;function a.inflate_zlib(J)local a3=a2(J.input)local x=a4(J.output)local bp=J.disable_crc;if bp==nil then bp=false end;local bw=az(a3)local bx=1;bo{input=a3,output=bp and x or function(A)bx=a.adler32(A,bx)x(A)end}a3:read(a3:nbits_left_in_byte())local by=a3:read(8)local bz=a3:read(8)local bA=a3:read(8)local bB=a3:read(8)local bC=((by*256+bz)*256+bA)*256+bB;if m then t('alder32=',bC)end;if not bp then if bx~=bC then u('invalid compressed data--crc error')end end;if a3:read()then r'trailing garbage ignored'end end;return a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement