Skip to content

Commit a7eebb4

Browse files
moonmingzhuizhuhaomeng
authored andcommitted
feature: implemented ngx_http_lua_ffi_decode_base64mime.
1 parent a634f10 commit a7eebb4

File tree

5 files changed

+135
-2
lines changed

5 files changed

+135
-2
lines changed

README.markdown

+19-1
Original file line numberDiff line numberDiff line change
@@ -3695,6 +3695,7 @@ Nginx API for Lua
36953695
* [ngx.decode_args](#ngxdecode_args)
36963696
* [ngx.encode_base64](#ngxencode_base64)
36973697
* [ngx.decode_base64](#ngxdecode_base64)
3698+
* [ngx.decode_base64mime](#ngxdecode_base64mime)
36983699
* [ngx.crc32_short](#ngxcrc32_short)
36993700
* [ngx.crc32_long](#ngxcrc32_long)
37003701
* [ngx.hmac_sha1](#ngxhmac_sha1)
@@ -6267,7 +6268,24 @@ ngx.decode_base64
62676268

62686269
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, ssl_client_hello_by_lua**
62696270

6270-
Decodes the `str` argument as a base64 digest to the raw form. Returns `nil` if `str` is not well formed.
6271+
Decodes the `str` argument as a base64 digest to the raw form.
6272+
The `str` should be standard 'base64' encoding for RFC 3548 or RFC 4648, and will returns `nil` if is not well formed or any characters not in the base encoding alphabet.
6273+
6274+
[Back to TOC](#nginx-api-for-lua)
6275+
6276+
ngx.decode_base64mime
6277+
---------------------
6278+
**syntax:** *newstr = ngx.decode_base64mime(str)*
6279+
6280+
**context:** *set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua**
6281+
6282+
**requires:** `resty.core.base64` or `resty.core`
6283+
6284+
Decodes the `str` argument as a base64 digest to the raw form.
6285+
The `str` follows base64 transfer encoding for MIME (RFC 2045), and will discard characters outside the base encoding alphabet.
6286+
Returns `nil` if `str` is not well formed.
6287+
6288+
'''Note:''' This method requires the <code>resty.core.base64</code> or <code>resty.core</code> modules from the [lua-resty-core](https://github.com/openresty/lua-resty-core) library.
62716289

62726290
[Back to TOC](#nginx-api-for-lua)
62736291

doc/HttpLuaModule.wiki

+15-1
Original file line numberDiff line numberDiff line change
@@ -5256,7 +5256,21 @@ Since the <code>0.9.16</code> release, an optional boolean-typed <code>no_paddin
52565256
52575257
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*, ssl_client_hello_by_lua*''
52585258
5259-
Decodes the <code>str</code> argument as a base64 digest to the raw form. Returns <code>nil</code> if <code>str</code> is not well formed.
5259+
Decodes the <code>str</code> argument as a base64 digest to the raw form.
5260+
The <code>str</code> should be standard 'base64' encoding for RFC 3548 or RFC 4648, and will returns <code>nil</code> if is not well formed or any characters not in the base encoding alphabet.
5261+
5262+
== ngx.decode_base64mime ==
5263+
'''syntax:''' ''newstr = ngx.decode_base64mime(str)''
5264+
5265+
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*''
5266+
5267+
'''requires:''' <code>resty.core.base64</code> or <code>resty.core</code>
5268+
5269+
Decodes the <code>str</code> argument as a base64 digest to the raw form.
5270+
The <code>str</code> follows base64 transfer encoding for MIME (RFC 2045), and will discard characters outside the base encoding alphabet.
5271+
Returns <code>nil</code> if <code>str</code> is not well formed.
5272+
5273+
'''Note:''' This method requires the <code>resty.core.base64</code> or <code>resty.core</code> modules from the [https://github.com/openresty/lua-resty-core lua-resty-core] library.
52605274
52615275
== ngx.crc32_short ==
52625276

src/ngx_http_lua_string.c

+20
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,26 @@ ngx_http_lua_ffi_decode_base64(const u_char *src, size_t slen, u_char *dst,
424424
}
425425

426426

427+
int
428+
ngx_http_lua_ffi_decode_base64mime(const u_char *src, size_t slen, u_char *dst,
429+
size_t *dlen)
430+
{
431+
ngx_int_t rc;
432+
ngx_str_t in, out;
433+
434+
in.data = (u_char *) src;
435+
in.len = slen;
436+
437+
out.data = dst;
438+
439+
rc = ngx_http_lua_decode_base64mime(&out, &in);
440+
441+
*dlen = out.len;
442+
443+
return rc == NGX_OK;
444+
}
445+
446+
427447
size_t
428448
ngx_http_lua_ffi_unescape_uri(const u_char *src, size_t len, u_char *dst)
429449
{

src/ngx_http_lua_util.c

+80
Original file line numberDiff line numberDiff line change
@@ -4401,6 +4401,86 @@ ngx_http_lua_copy_escaped_header(ngx_http_request_t *r,
44014401
}
44024402

44034403

4404+
ngx_int_t
4405+
ngx_http_lua_decode_base64mime(ngx_str_t *dst, ngx_str_t *src)
4406+
{
4407+
size_t i;
4408+
u_char *d, *s, ch;
4409+
size_t data_len = 0;
4410+
u_char buf[4];
4411+
size_t buf_len = 0;
4412+
static u_char basis[] = {
4413+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4414+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4415+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
4416+
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
4417+
77, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
4418+
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
4419+
77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
4420+
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,
4421+
4422+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4423+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4424+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4425+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4426+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4427+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4428+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
4429+
77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
4430+
};
4431+
4432+
for (i = 0; i < src->len; i++) {
4433+
ch = src->data[i];
4434+
if (ch == '=') {
4435+
break;
4436+
}
4437+
4438+
if (basis[ch] == 77) {
4439+
continue;
4440+
}
4441+
4442+
data_len++;
4443+
}
4444+
4445+
if (data_len % 4 == 1) {
4446+
return NGX_ERROR;
4447+
}
4448+
4449+
s = src->data;
4450+
d = dst->data;
4451+
4452+
for (i = 0; i < src->len; i++) {
4453+
if (s[i] == '=') {
4454+
break;
4455+
}
4456+
4457+
if (basis[s[i]] == 77) {
4458+
continue;
4459+
}
4460+
4461+
buf[buf_len++] = s[i];
4462+
if (buf_len == 4) {
4463+
*d++ = (u_char) (basis[buf[0]] << 2 | basis[buf[1]] >> 4);
4464+
*d++ = (u_char) (basis[buf[1]] << 4 | basis[buf[2]] >> 2);
4465+
*d++ = (u_char) (basis[buf[2]] << 6 | basis[buf[3]]);
4466+
buf_len = 0;
4467+
}
4468+
}
4469+
4470+
if (buf_len > 1) {
4471+
*d++ = (u_char) (basis[buf[0]] << 2 | basis[buf[1]] >> 4);
4472+
}
4473+
4474+
if (buf_len > 2) {
4475+
*d++ = (u_char) (basis[buf[1]] << 4 | basis[buf[2]] >> 2);
4476+
}
4477+
4478+
dst->len = d - dst->data;
4479+
4480+
return NGX_OK;
4481+
}
4482+
4483+
44044484
ngx_addr_t *
44054485
ngx_http_lua_parse_addr(lua_State *L, u_char *text, size_t len)
44064486
{

src/ngx_http_lua_util.h

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ void ngx_http_lua_cleanup_free(ngx_http_request_t *r,
261261
#if (NGX_HTTP_LUA_HAVE_SA_RESTART)
262262
void ngx_http_lua_set_sa_restart(ngx_log_t *log);
263263
#endif
264+
ngx_int_t ngx_http_lua_decode_base64mime(ngx_str_t *dst, ngx_str_t *src);
264265

265266
ngx_addr_t *ngx_http_lua_parse_addr(lua_State *L, u_char *text, size_t len);
266267

0 commit comments

Comments
 (0)