Skip to content

Commit e3f3fad

Browse files
committed
feature: implemented 'ngx.crc32_short()' and 'ngx.crc32_long()' via FFI.
1 parent f916a53 commit e3f3fad

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

lib/resty/core/hash.lua

+44
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ local subsystem = ngx.config.subsystem
1818
local ngx_lua_ffi_md5
1919
local ngx_lua_ffi_md5_bin
2020
local ngx_lua_ffi_sha1_bin
21+
local ngx_lua_ffi_crc32_long
22+
local ngx_lua_ffi_crc32_short
2123

2224

2325
if subsystem == "http" then
@@ -30,11 +32,19 @@ if subsystem == "http" then
3032

3133
int ngx_http_lua_ffi_sha1_bin(const unsigned char *src, size_t len,
3234
unsigned char *dst);
35+
36+
unsigned int ngx_http_lua_ffi_crc32_long(const unsigned char *src,
37+
size_t len);
38+
39+
unsigned int ngx_http_lua_ffi_crc32_short(const unsigned char *src,
40+
size_t len);
3341
]]
3442

3543
ngx_lua_ffi_md5 = C.ngx_http_lua_ffi_md5
3644
ngx_lua_ffi_md5_bin = C.ngx_http_lua_ffi_md5_bin
3745
ngx_lua_ffi_sha1_bin = C.ngx_http_lua_ffi_sha1_bin
46+
ngx_lua_ffi_crc32_short = C.ngx_http_lua_ffi_crc32_short
47+
ngx_lua_ffi_crc32_long = C.ngx_http_lua_ffi_crc32_long
3848

3949
elseif subsystem == "stream" then
4050
ffi.cdef[[
@@ -46,11 +56,19 @@ elseif subsystem == "stream" then
4656

4757
int ngx_stream_lua_ffi_sha1_bin(const unsigned char *src, size_t len,
4858
unsigned char *dst);
59+
60+
unsigned int ngx_stream_lua_ffi_crc32_long(const unsigned char *src,
61+
size_t len);
62+
63+
unsigned int ngx_stream_lua_ffi_crc32_short(const unsigned char *src,
64+
size_t len);
4965
]]
5066

5167
ngx_lua_ffi_md5 = C.ngx_stream_lua_ffi_md5
5268
ngx_lua_ffi_md5_bin = C.ngx_stream_lua_ffi_md5_bin
5369
ngx_lua_ffi_sha1_bin = C.ngx_stream_lua_ffi_sha1_bin
70+
ngx_lua_ffi_crc32_short = C.ngx_stream_lua_ffi_crc32_short
71+
ngx_lua_ffi_crc32_long = C.ngx_stream_lua_ffi_crc32_long
5472
end
5573

5674

@@ -105,6 +123,32 @@ ngx.sha1_bin = function (s)
105123
end
106124

107125

126+
ngx.crc32_short = function (s)
127+
if type(s) ~= "string" then
128+
if not s then
129+
s = ""
130+
else
131+
s = tostring(s)
132+
end
133+
end
134+
135+
return ngx_lua_ffi_crc32_short(s, #s)
136+
end
137+
138+
139+
ngx.crc32_long = function (s)
140+
if type(s) ~= "string" then
141+
if not s then
142+
s = ""
143+
else
144+
s = tostring(s)
145+
end
146+
end
147+
148+
return ngx_lua_ffi_crc32_long(s, #s)
149+
end
150+
151+
108152
return {
109153
version = base.version
110154
}

0 commit comments

Comments
 (0)