examples/base64.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 140 89841bd3db8c
permissions -rw-r--r--
Fix module loading problem
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
140
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
#!/usr/bin/env lua
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
-- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <alexthkloss@web.de>
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
-- licensed under the terms of the LGPL2
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
-- http://lua-users.org/wiki/BaseSixtyFour
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
-- character table string
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
-- encoding
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
local function enc(data)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
	return ((data:gsub('.', function(x) 
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
		local r,b='',x:byte()
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
		for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
		return r;
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
	end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
		if (#x < 6) then return '' end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
		local c=0
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
		for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
		return b:sub(c+1,c+1)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
	end)..({ '', '==', '=' })[#data%3+1])
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
-- decoding
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
local function dec(data)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
	data = string.gsub(data, '[^'..b..'=]', '')
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
	return (data:gsub('.', function(x)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
		if (x == '=') then return '' end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
		local r,f='',(b:find(x)-1)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
		for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
		return r;
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
	end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
		if (#x ~= 8) then return '' end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
		local c=0
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
		for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
		return string.char(c)
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
	end))
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
end
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
return {
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
		encode = enc,
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
		decode = dec,
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
	}
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
89841bd3db8c [examples] Add pure-lua base64 implementation
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
-- the end vim: se ts=4 sw=4: