LuaSrcDiet
Compresses Lua source code by removing unnecessary characters.
This is revival of LuaSrcDiet originally written by Kein-Hong Man.
Table of Contents
Introduction
LuaSrcDiet is a utility written in Lua for the purpose of turning Lua 5.1+ source code like this:
local function calc_indent(s)
local col = 0
for i = 1, #s do
local c = sub(s, i, i)
col = col + 1
if c == "\t" then -- tab
while col % 8 > 0 do col = col + 1 end
end
end--for
return math.floor(col / 8)
end
into a more compact or โsqueezedโ form (minus a lot of unnecessary characters) like this:
local function _(l)local e=0
for o=1,#l do
local n=n(l,o,o)e=e+1
if n=="\t"then
while e%8>0 do e=e+1 end
end
end
return r.floor(e/8)end
and still be able to run normally under standard Lua 5.1+ or LuaJIT 2.0+.
LuaSrcDiet reduces the size of Lua 5.1+ source files by aggressively removing all unnecessary whitespace and comments, optimizing constant tokens, and renaming local variables to shorter names. For example, LuaSrcDiet squeezes its own sources from 156 kiB down to 42 kiB. Further bzip2 or lzma compression can bring the file size further down to under 13 kiB. Thatโs 12ร reduction in size, if you donโt mind the decompression and compilation time.
LuaSrcDiet is broadly similar to Luizโs lstrip (tar.gz) for Lua 5.1, which can be found on Luizโs Libraries and tools for Lua page. LuaSrcDiet with its modified Lua source code lexer and parser allows most optimization options to be enabled or disabled separately, and can do a bit more like renaming local variable names.
There is also Matthew Wildโs squish, which incorporates LuaSrcDiet and offers more code compression options. Squish goes beyond what LuaSrcDiet does, as the latter (as a matter of policy) only sticks to source code readable by standard Lua binaries.
LuaSrcDiet and Obfuscation
Owing to the use of LuaSrcDiet among certain things like WoW add-ons, the following is a clarification of this authorโs intentions:
-
LuaSrcDiet can be used as a weak obfuscator. However, note that the structure and arrangement of the source code stays exactly the same, so do not depend on such a weak form of obfuscation if you really needed heavy-duty obfuscation.
-
LuaSrcDiet was written for the purpose of comparing minimum-sized sources with binary chunks, their compressibility, and the parsing performance of the Lua interpreter. I donโt care one iota about obfuscation, itโs compression Iโm interested in.
-
This is experimental software. If you want to use it for important stuff, be sure to apply source and binary equivalence checking. Iโm not, of course, responsible for anything you do.
-
Treat it like a text filter tool or a compiler. There is no legal requirement to acknowledge LuaSrcDiet or to place its copyright notice anywhere for the source code you processed. Your app is stuff you wrote, LuaSrcDiet is stuff I wrote. Simples.
-
Obfuscation cannot be defined precisely so we are dealing with subjective judgements. I think itโs fair if people want to apply a mild deterrent against casual plagiarism. Those desperate for original sources should instead turn their energies towards Open Source or Free Software.
Changes in This Fork
-
Code-base updated to be compatible with Lua 5.1โ5.3.
-
Added support for processing Lua 5.2โ5.3 code (except binary equivalence checking).
-
Published on LuaRocks (the Lua package manager).
-
Documentation comments converted to LDocโs format (except lparser.lua).
-
Documentation wiki pages converted to AsciiDoc.
Installation
Note: If you want to bootstrap development environment for running tests, read the next section.
Using LuaRocks
You can install luasrcdiet using LuaRocks (the Lua package manager):
luarocks install luasrcdiet
or to get the latest development version:
luarocks install --server=http://luarocks.org/dev luasrcdiet
Set Up Development Environment
-
Clone this repository:
git clone https://github.com/jirutka/luasrcdiet.git cd luasrcdiet
-
Source file
.envrc
into your shell (or manually add$(pwd)/.venv/bin
to yourPATH
):source .envrc
-
Install Lua and modules for running tests into directory
.venv
:./script/bootstrap
-
Start hacking!
-
Run linters and tests:
./script/test
Documentation
Acknowledgements
-
The original author of LuaSrcDiet and its documentation is Kein-Hong Man. History of this repository until 2012 has been recreated from release tarballs hosted on Google Code.
-
Parts of LuaSrcDiet is based on Yueliang, which is in turn based on the Lua sources.
License
This project is licensed under MIT License. For the full text of the license, see the COPYRIGHT file.