Advertisement
MAG_Gen

LZSSEPM (LZSS self-extracting program maker) Online version

Sep 23rd, 2023 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.40 KB | Source Code | 0 0
  1. -- LZSS SEP-Maker (Online Built-in LZSS) https://pastebin.com/MxnRb6E8
  2. --
  3. -- This program was made to create self-extracting archives.
  4. --
  5. -- That kind of self-extracting programs(SEP) has built-in LZSS
  6. -- unpack function, so they can be launched without LZSS installed.
  7. -- You can run them with pastebin or wget or directly from
  8. -- shell (or.run is posible but unrecomended)
  9.  
  10. local B,A,R,S=bit32,bit32.band,bit32.rshift,string.sub
  11. local lzss_pack=function(I)local L,o,r,w,f,b,s,p=B.lshift,1,'',''while#I>=o do
  12. f,b=0,''for i=0,7 do
  13. if#I>=o then
  14. for j=17,2,-1 do
  15. s=S(I,o,o+j)p=w:find(s,1,1)if p then break end
  16. end
  17. if p and#s>=3 then
  18. b=b..('>I2'):pack(B.bor(L(p-1,4),#s-3))else
  19. f=B.bor(f,L(1,i))s=S(I,o,o)b=b..s
  20. end
  21. w=S(w..s,-4^6)o=o+#s
  22. else
  23. break
  24. end
  25. end
  26. if#b>0 then
  27. r=r..r.char(f)..b
  28. end
  29. end
  30. return r
  31. end
  32.  
  33.  
  34. -- Help message
  35. local tArgs={...}
  36. if not ... then print"Usage: <src> [dest] [wget] [uneffective] [rewrite]"return end
  37.  
  38. --main part (part that will be inserted at the start of file)
  39. local extractor_base=[==[P=P:sub(SiZ,ZiS)while#P>=o do f=P:byte(o)o=o+1
  40. for _=0,7 do s=nil
  41. if 0<A(f,1)then
  42. if#P>=o then s=S(P,o,o)o=o+1
  43. end
  44. elseif#P>=o+1 then
  45. t=('>I2'):unpack(P,o)p=1+R(t,4)s=S(w,p,p+A(t,15)+2)o=o+2
  46. end
  47. f=R(f,1)if s then w=S(w..s,-4096)r=r..s end
  48. end
  49. end
  50. return load(r,O,nil,_ENV)(...)--[]==]
  51.  
  52. --advanced extractor (biger then default, but can be launched directly from wget/pastebin)
  53. local extractor_wget=[==[local D,A,R,o,r,w,f,t,p,s,S,O,G,L,P=debug or{},bit32.band,bit32.rshift,1,'',''G=D.getinfo or r.char
  54. L=D.getlocal
  55. O=(G(3)or{}).source or""P=O:find"wget.lua$"and 4 or O:find"pastebin.lua$"and 6 or P
  56. S=O.sub
  57. if P then D,O=L(3,P)D,P=L(3,7)else L=shell O=L and"@/"..L.resolveProgram(arg[0])or(G(1)or{}).source or""D,L=io.open(S(O,2),'rb')L=L and error(L)P=D:read"*a"D:close()end
  58. ]==]
  59.  
  60. --default extractor (can start from file only, but smaller)
  61. local extractor_def =[==[local D,A,R,L,o,r,w,f,t,p,s,S,O,P=debug or{},bit32.band,bit32.rshift,shell,1,'',''S=r.sub
  62. O=L and"@/"..L.resolveProgram(arg[0])or(D.getinfo or r.char)(1).source or""D,L=io.open(S(O,2),'rb')L=L and error(L)P=D:read"*a"D:close()]==]
  63. -- "@/" - needed to make chunknames of packed programs similar to unpacked ones
  64.  
  65. -- read source
  66. local eq_count=0
  67. local file=fs.open(...or"",'r') or error"LZSS> Can't open source file..."
  68. local before=file.readAll()
  69. local prog=lzss_pack(before)
  70. file.close()
  71.  
  72. -- choose/print extractor
  73. local wget
  74. write"LZSSEPM> Add wget/pastebin support? [Y/n]: "
  75. if tArgs[3]=="true"or tArgs[3]=="Y"or tArgs[3]=="y"then print(tArgs[3]) wget=1
  76. elseif tArgs[3]=="false" or tArgs[3]=="N"or tArgs[3]=="n" then print(tArgs[3])
  77. else
  78.     wget = read():find"^[Yy]"
  79. end
  80. print("LZSSEPM> Extractor type :",wget and"WGET/PASTEBIN Support"or"Default")
  81. local extractor = (wget and extractor_wget or extractor_def)..extractor_base
  82. print(("LZSSEPM> Extractor size : %.2f"):format(#extractor/1024),"Kb's\n")
  83.  
  84. -- make container for compressed code
  85. k=false
  86. repeat
  87.     if prog:find('([%[%]])'..("="):rep(eq_count)..'%1') then
  88.         eq_count=eq_count+1
  89.     else
  90.         k=true
  91.     end
  92. until k
  93.  
  94. --print stats
  95. print(("LZSSEPM> Default size   : %.2f"):format(#before/1024),"Kb's")
  96. print(("LZSSEPM> After LZSS size: %.2f"):format(#prog/1024),"Kb's")
  97. print(("LZSSEPM> Final size     : %.2f"):format((#prog+#extractor+3+eq_count*2)/1024),"Kb's\n")
  98.  
  99. --Warning if compression is bad
  100. if #before <= #prog+#extractor+3+eq_count*2 then
  101.     print"LZSSEPM> Ineffective compression!"
  102.     write"LZSSEPM> Continue? [Y/n]: "
  103.     if tArgs[4]=="false"or tArgs[4]=="N" or tArgs[4]=="n"then print(tArgs[4])return
  104.     elseif tArgs[4]=="true"or tArgs[4]=="Y" or tArgs[4]=="y"then print(tArgs[4])
  105.     elseif read():find"^[^Yy]" then return
  106.     end
  107.     print()
  108. end
  109.  
  110. -- set dest file
  111. local dest = tArgs[2] or ... .. ".sep"
  112. if fs.exists(dest) then
  113.     print"LZSSEPM> Destination already exists!"
  114.     write"LZSSEPM> Continue? [Y/n]: "
  115.     if tArgs[5]=="true"or tArgs[5]=="Y"or tArgs[5]=="y"then print(tArgs[5])
  116.     elseif tArgs[5]=="false"or tArgs[5]=="N"or tArgs[5]=="n"then print(tArgs[5])return
  117.     elseif read():find"^[^Yy]"then return
  118.     end
  119. end
  120.  
  121. -- write dest file
  122. file=fs.open(dest,'wb')
  123. file.write(extractor:gsub("SiZ",#extractor+2+eq_count):gsub("ZiS",("%.2d"):format(-3-eq_count)))--extractior
  124. file.write(("="):rep(eq_count).."["..prog.."]"..("="):rep(eq_count).."]")--code
  125. file.close()
  126. print("LZSSEPM> Packing complete:",dest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement