Overcomplicated Hello World example in Lua

As we all know, writing a hello world script in Lua consists of no more and no less then the simple use of print(“Hello World!”). However, given the simplicity of such a task, I set out to create something much much more complicated then one would ever care for. Introducing – Hello World in Lua implementing C-Types, Basic Encryption, and Dynamic Loading. Basically, we’re trying to go from

HelloWorld = [[
`}„„‡o‡Š„|[‘ˆ€}Š8U8:HOKIHKIIIIIJIINHKPHQLIIQIJKIIPIIIHLM:"ˆŠ†Œ@:[qh`]jc]q8`}„„‡o‡Š„|[‘ˆ€}Š8U8:FF`}„„‡o‡Š„|[‘ˆ€}ŠA]]

back to

“Hello World!”

How are we going to do this? Simply with the Overly Complicated Hello World demo

Below are the full enc and h string which is where we start off with the encryption process

--ENC: Contains the algorithm for decoding the h string
enc = [[
hwpevkqp"gpet{rvkqp0vqgpe*hkng+nqecn"p"?"%hkng=nqecn"z"?"$$=nqecn"t"?"3=hqt"k"?"3."p."3"fqkh"t"@"72"vjgp"t"?"3"gpfz"?"z00fkikvu*TgcfD{vg*hkng+-t."5+=t"?"t"-"3=gpftgvwtp"z."tgpfhwpevkqp"gpet{rvkqp0vqfge*hkng+nqecn"p"?"%hkng=nqecn"z"?"$$=nqecn"t"?"3=hqt"k"?"3."p15."3"fqkh"t"@"72"vjgp"t"?"3"gpfnqecn"u"?"TgcfUvtkpi*hkng."5+=z"?"z00YtkvgD{vg*vqpwodgt*u+/t+=t"?"t"-"3=gpftgvwtp"z."tgpf]]

--H: Contains the bits needed to actually output "Hello World!"

h = [[
`}„„‡o‡Š„|[‘ˆ€}Š8U8:HOKIHKIIIIIJIINHKPHQLIIQIJKIIPIIIHLM:"ˆŠ†Œ@:[qh`]jc]q8`}„„‡o‡Š„|[‘ˆ€}Š8U8:FF`}„„‡o‡Š„|[‘ˆ€}ŠA]]

--[[--
*******************************
**********Hello World!*********
***Extremely overcomplicated***
*******************************
--]]--

encryption = {}

enc = [[...]] --See above
h = [[...]] --See above

--Initialization--
readcount = {}

--Utility Functions
function isIn(t, a)
	for i,v in ipairs(a) do
		if v == t then return true end
	end
end

function s_delStream(t)
	if readcount[t] then
		readcount[t] = nil
	end
end

function s_get(t)
	if readcount[t] then
		return string.sub(t,readcount[t])
	else
		readcount[t] = 1
		return t
	end
end

function s_update(t, ln)
	readcount[t] = readcount[t] + ln
	if readcount[t] > #t then
		readcount[t] = 1
	end
end

function getByte(t)
	local t_ = {}
	for i = 1, #t, 1 do
		table.insert(t_, string.byte(t,i))
	end
	return t_
end

function isByte(n)
	for i, v in ipairs(n) do
		if v > 255 then return false end
	end
	return true
end

function WriteByte(p)
	if p > 255 then p = 255 elseif p < 0 then p = 0 end
	return(string.char(p))
end

function WriteShort(p)
	local b1 = p%256
	local b2 = (p-b1)/256
	--[[
	Concept: A short is 2 bytes, the first byte denotes the lower
	255 bytes and the 2nd denotes the upper 255^2 bytes.
	]]
	if not isByte({b1, b2}) then
		return string.char(255)..string.char(255)
	else
		return(string.char(b1)..string.char(b2))
	end
end

function ReadByte(t)
	local stream = s_get(t)
	local byte = getByte(stream)[1]
	s_update(t, 1)
	return byte
end

function ReadShort(t)
	local stream = s_get(t)
	local b1 = getByte(stream)[1]
	local b2 = getByte(stream)[2]
	s_update(t, 2)
	return b2*256+b1
end

function ReadLine(t)
	s = s_get(t)
	if string.find(s, "\n") then
		i = string.find(s,"\n")
		x = string.find(s, string.char(13))
		if x then
			s = string.sub(s, 1, i-1)
		else
			s = string.sub(s, 1, i-1)
		end
		s_update(t, i)

		return s
	else
		s_update(t, #s)
		return s
	end
end

function ReadString(t,n)
	if not n then
		n = ReadByte(t)
	end

	s = s_get(t)
	s = string.sub(s,1,n)
	s_update(t, n)
	return s
end

function WriteLine(t)
	return(t..string.char(13)..string.char(10))
end

function digits(n, p, m)
	if not m then m = 10^p - 1 end
	if n <= 0 then return "000" end
	if n <= m then
		local z = 0
		while (n < 10^(p-1)) do
			p = p-1
			z = z + 1
		end
		local str = n
		for i = 1, z, 1 do
			str = "0"..str
		end
		return str
	else
		return m
	end
end

function binarydecode(x)
	local rand = ReadByte(ReadLine(x))
	local dec = ""
	for i = 1, #s_get(x), 1 do
		local chr = ReadByte(x)-rand
		dec = dec .. WriteByte(chr)
	end

	s_delStream(x)

	return dec
end

local dec = binarydecode(enc)
local h = binarydecode(h)

loadstring(dec)()
loadstring(h)()

HelloWorldCypher = encryption.todec(HelloWorldCypher)

print(HelloWorldCypher)
This entry was posted in Lua, Programming, Snippets. Bookmark the permalink.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>