-
Notifications
You must be signed in to change notification settings - Fork 104
Adding set_misc_base32_alphabet directive to allow the use of custom alphabets #20
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
Changes from 5 commits
b6f2a92
5b90edd
710a737
64b5de2
679a6f6
7836ddf
450e7b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,8 +14,9 @@ | |
|
|
||
|
|
||
| static void encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, | ||
| ngx_flag_t padding); | ||
| static int decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst); | ||
| ngx_flag_t padding, ngx_str_t *alphabet); | ||
| static int decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, | ||
| u_char *basis32); | ||
|
|
||
|
|
||
| ngx_int_t | ||
|
|
@@ -41,7 +42,7 @@ ngx_http_set_misc_encode_base32(ngx_http_request_t *r, ngx_str_t *res, | |
|
|
||
| src = v->data; dst = p; | ||
|
|
||
| encode_base32(v->len, src, &len, dst, conf->base32_padding); | ||
| encode_base32(v->len, src, &len, dst, conf->base32_padding, &conf->base32_alphabet); | ||
|
|
||
| res->data = p; | ||
| res->len = len; | ||
|
|
@@ -60,6 +61,12 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res, | |
| u_char *p; | ||
| u_char *src, *dst; | ||
| int ret; | ||
| static unsigned char basis32[256] = { 77 }; | ||
| ngx_uint_t i; | ||
|
|
||
| ngx_http_set_misc_loc_conf_t *conf; | ||
|
|
||
| conf = ngx_http_get_module_loc_conf(r, ngx_http_set_misc_module); | ||
|
|
||
| len = base32_decoded_length(v->len); | ||
|
|
||
|
|
@@ -72,7 +79,11 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res, | |
|
|
||
| src = v->data; dst = p; | ||
|
|
||
| ret = decode_base32(v->len, src, &len, dst); | ||
| for (i = 0; i < 32; i++) { | ||
| basis32[conf->base32_alphabet.data[i]] = i; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to move this loop into the configuration phase as well?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pardon my ignorance, I guess didn't quite understand what you meant by the configuration phase and moved the code to the wrong place. Did you mean we should initialize the array in ngx_http_set_misc_create_loc_conf and put it in the conf struct directly instead? This actually makes more sense now that I look at it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blablacio Yes, by "configuration phase", I mean the phase where nginx loads its configuration (in the master process) instead of serving actual requests in the worker processes. Yeah, I mean initializing that array only once in the |
||
| } | ||
|
|
||
| ret = decode_base32(v->len, src, &len, dst, &basis32); | ||
|
|
||
| if (ret == 0 /* OK */) { | ||
| res->data = p; | ||
|
|
@@ -113,9 +124,9 @@ ngx_http_set_misc_decode_base32(ngx_http_request_t *r, ngx_str_t *res, | |
| * */ | ||
| static void | ||
| encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, | ||
| ngx_flag_t padding) | ||
| ngx_flag_t padding, ngx_str_t *alphabet) | ||
| { | ||
| static unsigned char basis32[] = "0123456789abcdefghijklmnopqrstuv"; | ||
| unsigned char *basis32 = alphabet->data; | ||
|
|
||
| size_t len; | ||
| u_char *s; | ||
|
|
@@ -204,58 +215,9 @@ encode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, | |
|
|
||
|
|
||
| static int | ||
| decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst) | ||
| decode_base32(size_t slen, u_char *src, size_t *dlen, u_char *dst, | ||
| u_char *basis32) | ||
| { | ||
| static unsigned char basis32[] = { | ||
| /* 0 - 15 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 16 - 31 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 32 - 47 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 48 - 63 */ | ||
| 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 64 - 79 */ | ||
| 77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||
|
|
||
| /* 80 - 95 */ | ||
| 25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 96 - 111 */ | ||
| 77, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, | ||
|
|
||
| /* 112 - 127 */ | ||
| 25, 26, 27, 28, 29, 30, 31, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 128 - 143 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 144 - 159 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 160 - 175 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 176 - 191 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 192 - 207 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 208 - 223 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 224 - 239 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, | ||
|
|
||
| /* 240 - 255 */ | ||
| 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77 | ||
| }; | ||
|
|
||
| size_t len, mod; | ||
| u_char *s = src; | ||
| u_char *d = dst; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -333,6 +333,15 @@ static ngx_command_t ngx_http_set_misc_commands[] = { | |
| offsetof(ngx_http_set_misc_loc_conf_t, base32_padding), | ||
| NULL | ||
| }, | ||
| { | ||
| ngx_string("set_misc_base32_alphabet"), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like the directive name to be simpler: |
||
| NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF | ||
| |NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1, | ||
| ngx_conf_set_str_slot, | ||
| NGX_HTTP_LOC_CONF_OFFSET, | ||
| offsetof(ngx_http_set_misc_loc_conf_t, base32_alphabet), | ||
| NULL | ||
| }, | ||
| { | ||
| ngx_string("set_encode_base32"), | ||
| NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_SIF_CONF | ||
|
|
@@ -471,6 +480,8 @@ ngx_http_set_misc_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) | |
|
|
||
| ngx_conf_merge_value(conf->base32_padding, prev->base32_padding, 1); | ||
|
|
||
| ngx_conf_merge_str_value(conf->base32_alphabet, prev->base32_alphabet, "0123456789abcdefghijklmnopqrstuv"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line exceeds 80 columns. Please fix it :) |
||
|
|
||
| ngx_conf_merge_value(conf->current, prev->current, NGX_CONF_UNSET); | ||
|
|
||
| return NGX_CONF_OK; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line also exceeds 80 columns :)
BTW, I usually use the
ngx-relengscript to do such checks automatically before committing: https://github.com/openresty/nginx-devel-utils/blob/master/ngx-releng Just FYI :)Thanks!