LuaSrcDiet
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
while col % 8 > 0 do col = col + 1 end
end
end
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.
LuaSrcDiet reduces the size of Lua 5.1.x 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 156KB down
to 42KB. Further bzip2 or lzma compression can bring the file size
further down to under 13KB. That's 12X reduction in size, if you
don't mind the decompression and compilation time.
LuaSrcDiet is broadly similar to Luiz's
lstrip
(tar.gz link) 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.
What's New
As of version 0.12.0, LuaSrcDiet has been moved to a new project host
site at: http://code.google.com/p/luasrcdiet/
- Version 0.12.1 (2012-04-07):
- BUG FIX: Long comment adds an extra 2 characters when using --keep option.
- Faster function call syntax sugar optimization using a one-pass token deletion loop.
- Version 0.12.0 (2011-09-13):
- Added convenient single-file versions of LuaSrcDiet in various sizes.
- BUG FIX: String optimization of "\ddd" -- "\00101" was incorrectly optimized to "\101".
- Added --opt-srcequiv: Source equivalence checking. Tries hard to compare 'before' and 'after' lexer token streams for equivalence.
- Added --opt-binequiv: Binary chunk equivalence checking. Tries hard to compare 'before' and 'after' binary chunks for equivalence.
- When using --opt-eols, the last EOL character is now removed.
- --opt-experimental: Turns on a few experimental optimizations:
- Semicolon (';') operator removal (deleted or turned into whitespace).
- Function call syntax sugar optimization. E.g.
f("string")
is turned into f"string"
- Plugins are now embedded into single-file versions.
- First release of completed documentation files.
- New Makefile and numerous minor updates.
- Old code for Lua 5.0 removed (the last working version can be found in the previous release.)
- Version 0.11.2 (2008-06-08):
- Improved local variable name allocation, better reuse leading to less identifiers used.
- Added experimental --plugin option with an example plugin script.
- Added a SLOC plugin to count SLOC for Lua 5.1 source files.
- Added a HTML plugin to see globals and locals marked.
- Version 0.11.1 (2008-06-03):
- Fixed a local variable rename bug that generates names that are keywords.
- Added --opt-entropy option for locals variables to reduce symbol entropy.
- Implemented --detail info for strings, numbers and local variables
- Added explanatory notes on local variable optimization.
- Version 0.11.0 (2008-05-28):
- Local variable name optimization.
- Many options and sample output added.
- Version 0.10.2 (2008-05-27):
- Aggressive optimizations for string and number tokens.
- Minor bug fixes.
- Version 0.10.1 (2008-05-25):
- Totally rewritten for Lua 5.1.x.
Acknowledgements
LuaSrcDiet was originally hosted on LuaForge.
Thanks to the LuaForge team for their contributions to the Lua ecosystem.
LuaSrcDiet was developed exclusively using the
SciTE editor, on
Cygwin, and managed using
SVN.
Parts of LuaSrcDiet is based on Yueliang,
which is in turn based on the Lua sources.
2012-04-07 khman