Compresses Lua source code by removing unnecessary characters.
This is revival of LuaSrcDiet originally written by Kein-Hong Man.
Table of Contents
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.
Note: If you want to bootstrap development environment for running tests, read the next section.
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
.envrcinto your shell (or manually add
Install Lua and modules for running tests into directory
Run linters and tests: