Page 1 of 1

Zimpel (Encoding / Decoding of files)

Posted: Sun Jan 17, 2016 3:32 pm
by rmcode
A small module able to encode and decode files based on the classic LZ78 algorithm.

Instructions

Zimpel can encode and decode strings:

Code: Select all

local Zimpel = require('Zimpel')

local code = Zimpel.encode( "I have come here to chew bubblegum and kick ass...and I'm all out of bubblegum." )
print( code )

local msg = Zimpel.decode( code );
print( msg )
There are also functions for encoding and decoding tables:

Code: Select all

local Zimpel = require('Zimpel')

local example = {
    bubblegum = false,
    kickass = true,
    equipment = {
        "sunglasses",
        "shotgun"
    }
}

local code = Zimpel.encodeTable( example )
print( code )

local dtable = Zimpel.decodeTable( code );
for i, v in pairs( dtable ) do
    print( i, v )
end
Example output for ("Hello World. How are you?"):

Code: Select all

 0H   0e   0l   3o   0    0W   0o   0r   3d   0.   5H   7w   5a   8e   5y   7u   0?
I wrote it for fun, but I thought it might be useful for some people. Could be used to encode save games so they can't be edited (easily).

The code lives on github.

Re: Zimpel (Encoding / Decoding of files)

Posted: Mon Jan 18, 2016 8:23 am
by Jack5500
So this is basicly serialisation. Can is serialise/encode userdata as well? What about Images?

Re: Zimpel (Encoding / Decoding of files)

Posted: Mon Jan 18, 2016 8:40 am
by rmcode
Jack5500 wrote:Can is serialise/encode userdata as well? What about Images?
Only if you turn them into a string :P

Re: Zimpel (Encoding / Decoding of files)

Posted: Mon Jan 18, 2016 8:47 am
by zorg
One could combine the resulting serialized strings from robin's ser/lady/smallfolk with this as well, really useful! :3

Re: Zimpel (Encoding / Decoding of files)

Posted: Mon Jan 18, 2016 10:24 am
by Frohman
Not to detract from this, but can't you use love's (now built in) compression wrapper functions to achieve the same thing, faster?
See; https://love2d.org/wiki/love.math.compress and https://love2d.org/wiki/love.math.decompress

Nice work, though!

Re: Zimpel (Encoding / Decoding of files)

Posted: Mon Jan 18, 2016 10:44 am
by rmcode
Frohman wrote:Not to detract from this, but can't you use love's (now built in) compression wrapper functions to achieve the same thing, faster?
Most certainly!
Nice work, though!
Thanks - it was fun to write :)

Re: Zimpel (Encoding / Decoding of files)

Posted: Mon Jan 18, 2016 10:45 am
by zorg
Frohman wrote:Not to detract from this, but can't you use love's (now built in) compression wrapper functions to achieve the same thing, faster?
Sure, but love can only use lz4 and DEFLATE algorithms (the latter in two "flavours"), and this one is an LZ78 implementation, so totally different! :3

Re: Zimpel (Encoding / Decoding of files)

Posted: Mon Jan 18, 2016 11:52 am
by rmcode
zorg wrote:Sure, but love can only use lz4 and DEFLATE algorithms (the latter in two "flavours"), and this one is an LZ78 implementation, so totally different! :3
DEFLATE is based on LZ77 (IIRC) and then encoded via Huffman. Would be totally futile (= fun) to try writing that one in Lua too :D

Re: Zimpel (Encoding / Decoding of files)

Posted: Wed Jan 20, 2016 6:17 pm
by rmcode
Small bugfix release:

Code: Select all

# Version 1.0.1 ( 2016-01-20 )
- Fixed encoding of tables containing strings with apostrophes
- Fixed encoding of tables containing sequences