Skip to content
This repository has been archived by the owner on Jul 14, 2023. It is now read-only.

Commit

Permalink
Added function base64enc()
Browse files Browse the repository at this point in the history
Added a base 64 encoding function for transmitting string data that
could contain delimiting characters such as whitespace and commas.

This should allow for the transport of general strings such as server
name and description across the network.
  • Loading branch information
initramfs committed Jan 29, 2014
1 parent 3fe8e7d commit 2b203a4
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions COM/server/globals.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

function string.starts(String,Start)
return string.sub(String,1,string.len(Start)) == Start
end
Expand All @@ -23,6 +25,32 @@ function toboolean(v)
return (type(v) == "string" and v == "true") or (type(v) == "number" and v ~= 0) or (type(v) == "boolean" and v)
end

function base64enc(data)
return ((data:gsub('.',
function(x)
local r,base64='', x:byte()
for i=8,1,-1 do
r = r..(base64 % 2^i - base64 % 2^(i-1) > 0 and '1' or '0')
end

return r
end
)..'0000'):gsub('%d%d%d?%d?%d?%d?',
function(x)
if (#x < 6) then
return ''
end

local c = 0
for i = 1,6 do
c = c + (x:sub(i,i) == '1' and 2^(6-i) or 0)
end

return base64:sub(c+1,c+1)
end
)..({'', '==', '='})[#data % 3+1])
end

function supports(feature)
if ServVer == -1 then
return true
Expand Down

0 comments on commit 2b203a4

Please sign in to comment.