Last active
August 29, 2015 14:17
-
-
Save dvv/c066ce927c3ca7761a62 to your computer and use it in GitHub Desktop.
AE info encode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local M | |
do | |
-- cache | |
local char = string.char | |
-- | |
local encode_info = function(args) | |
local r = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0} -- FIXME: lowlevel createtable with 8 + 2 * #args elements? | |
local n = 0 | |
-- enlist arguments separated with NL | |
for i = 1, #args do | |
local s = args[i] | |
r[7 + 2*i] = s -- FIXME: cache 2*i? | |
r[8 + 2*i] = "\n" | |
n = n + #s + 1 -- FIXME: +1 optimized to initial n = #args | |
end | |
-- fill header | |
r[1] = "\2" -- version | |
r[2] = "\1" -- "info" | |
for i = 8, 3, -1 do | |
r[i] = char(n % 256) | |
n = n / 256 | |
end | |
-- NB: nginx accepts a table of strings, no need to concat | |
return r | |
end | |
local decode_header = function(header) | |
local _ver = header:byte(1) | |
local t = header:byte(2) -- NB: 1 "info", 3 "message" | |
local n = 0 | |
for i = 3, 8 do | |
n = n * 256 + header:byte(i) | |
end | |
return t, n | |
-- expose | |
M = { | |
decode_header = decode_header, | |
encode_info = encode_info, | |
} | |
end | |
return M |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment