Skip to content

add no-padding option to base64 encoding #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions lib/resty/core/base64.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,48 @@ local tonumber = tonumber

ffi.cdef[[
size_t ngx_http_lua_ffi_encode_base64(const unsigned char *src,
size_t len, unsigned char *dst);
size_t len, unsigned char *dst,
int no_padding);

int ngx_http_lua_ffi_decode_base64(const unsigned char *src,
size_t len, unsigned char *dst,
size_t *dlen);
]]


local function base64_encoded_length(len)
return floor((len + 2) / 3) * 4
local function base64_encoded_length(len, no_padding)
return no_padding and floor((len * 8 + 5) / 6) or
floor((len + 2) / 3) * 4
end


ngx.encode_base64 = function (s)
ngx.encode_base64 = function (s, no_padding)
if type(s) ~= 'string' then
if not s then
s = ''
else
s = tostring(s)
end
end

local slen = #s
local dlen = base64_encoded_length(slen)
-- print("dlen: ", tonumber(dlen))
local no_padding_bool = false;
local no_padding_int = 0;

if no_padding then
if no_padding ~= true then
return error("boolean argument only")
end

no_padding_bool = true
no_padding_int = 1;
end

local dlen = base64_encoded_length(slen, no_padding_bool)
local dst = get_string_buf(dlen)
dlen = C.ngx_http_lua_ffi_encode_base64(s, slen, dst)
return ffi_string(dst, dlen)
local r_dlen = C.ngx_http_lua_ffi_encode_base64(s, slen, dst,
no_padding_int)
--if dlen ~= r_dlen then ngx.say ("discrepancy in len") end
return ffi_string(dst, r_dlen)
end


Expand Down
48 changes: 48 additions & 0 deletions t/encode-base64.t
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,51 @@ qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
[error]
-- NYI:



=== TEST 7: set base64 (number) without padding (explicitly specified)
--- http_config eval: $::HttpConfig
--- config
location = /base64 {
content_by_lua '
local s
for i = 1, 100 do
s = ngx.encode_base64(3.14, true)
end
ngx.say(s)
';
}
--- request
GET /base64
--- response_body
My4xNA
--- error_log eval
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
--- no_error_log
[error]
-- NYI:



=== TEST 8: set base64 (number) with padding (explictly specified)
--- http_config eval: $::HttpConfig
--- config
location = /base64 {
content_by_lua '
local s
for i = 1, 100 do
s = ngx.encode_base64(3.14, false)
end
ngx.say(s)
';
}
--- request
GET /base64
--- response_body
My4xNA==
--- error_log eval
qr/\[TRACE \d+ content_by_lua\(nginx\.conf:\d+\):3 loop\]/
--- no_error_log
[error]
-- NYI: