Skip to content

Commit 8a381c1

Browse files
committed
feature: enabled the FFI-based API for 'ngx.encode_base64' and 'ngx.decode_base64' in the stream subsystem.
1 parent 8745740 commit 8a381c1

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

lib/resty/core.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ require "resty.core.misc"
1010
require "resty.core.hash"
1111
require "resty.core.uri"
1212
require "resty.core.exit"
13+
require "resty.core.base64"
1314

1415

1516
if subsystem == 'http' then
16-
require "resty.core.base64"
1717
require "resty.core.var"
1818
require "resty.core.ctx"
1919
require "resty.core.request"

lib/resty/core/base64.lua

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
11
-- Copyright (C) Yichun Zhang (agentzh)
22

33

4-
local ffi = require 'ffi'
4+
local ffi = require "ffi"
55
local base = require "resty.core.base"
66

7-
local ffi_string = ffi.string
7+
88
local C = ffi.C
9+
local ffi_string = ffi.string
910
local ngx = ngx
1011
local type = type
11-
local tostring = tostring
1212
local error = error
13+
local floor = math.floor
14+
local tostring = tostring
1315
local get_string_buf = base.get_string_buf
1416
local get_size_ptr = base.get_size_ptr
15-
local floor = math.floor
17+
local subsystem = ngx.config.subsystem
1618

1719

18-
ffi.cdef[[
20+
local ngx_lua_ffi_encode_base64
21+
local ngx_lua_ffi_decode_base64
22+
23+
24+
if subsystem == "http" then
25+
ffi.cdef[[
1926
size_t ngx_http_lua_ffi_encode_base64(const unsigned char *src,
2027
size_t len, unsigned char *dst,
2128
int no_padding);
2229

2330
int ngx_http_lua_ffi_decode_base64(const unsigned char *src,
2431
size_t len, unsigned char *dst,
2532
size_t *dlen);
26-
]]
33+
]]
34+
35+
ngx_lua_ffi_encode_base64 = C.ngx_http_lua_ffi_encode_base64
36+
ngx_lua_ffi_decode_base64 = C.ngx_http_lua_ffi_decode_base64
37+
38+
elseif subsystem == "stream" then
39+
ffi.cdef[[
40+
size_t ngx_stream_lua_ffi_encode_base64(const unsigned char *src,
41+
size_t len, unsigned char *dst,
42+
int no_padding);
43+
44+
int ngx_stream_lua_ffi_decode_base64(const unsigned char *src,
45+
size_t len, unsigned char *dst,
46+
size_t *dlen);
47+
]]
48+
49+
ngx_lua_ffi_encode_base64 = C.ngx_stream_lua_ffi_encode_base64
50+
ngx_lua_ffi_decode_base64 = C.ngx_stream_lua_ffi_decode_base64
51+
end
2752

2853

2954
local function base64_encoded_length(len, no_padding)
@@ -57,8 +82,7 @@ ngx.encode_base64 = function (s, no_padding)
5782

5883
local dlen = base64_encoded_length(slen, no_padding_bool)
5984
local dst = get_string_buf(dlen)
60-
local r_dlen = C.ngx_http_lua_ffi_encode_base64(s, slen, dst,
61-
no_padding_int)
85+
local r_dlen = ngx_lua_ffi_encode_base64(s, slen, dst, no_padding_int)
6286
-- if dlen ~= r_dlen then error("discrepancy in len") end
6387
return ffi_string(dst, r_dlen)
6488
end
@@ -78,7 +102,7 @@ ngx.decode_base64 = function (s)
78102
-- print("dlen: ", tonumber(dlen))
79103
local dst = get_string_buf(dlen)
80104
local pdlen = get_size_ptr()
81-
local ok = C.ngx_http_lua_ffi_decode_base64(s, slen, dst, pdlen)
105+
local ok = ngx_lua_ffi_decode_base64(s, slen, dst, pdlen)
82106
if ok == 0 then
83107
return nil
84108
end

0 commit comments

Comments
 (0)