Skip to content
Snippets Groups Projects
Commit b5ba3db9 authored by Dmitry E. Oboukhov's avatar Dmitry E. Oboukhov
Browse files

Merge branch 'master' of github.com:tarantool/tarantool

parents 8dbdac93 e231a787
No related branches found
No related tags found
No related merge requests found
......@@ -47,5 +47,7 @@ void *ffi_symbols[] = {
(void *) bsdsocket_local_resolve,
(void *) bsdsocket_nonblock,
(void *) base64_decode,
(void *) base64_encode,
(void *) base64_bufsize,
(void *) SHA1internal
};
......@@ -27,6 +27,7 @@ ffi.cdef[[
extern crc32_func crc32_calc;
/* base64 */
int base64_bufsize(int binsize);
int base64_decode(const char *in_base64, int in_len, char *out_bin, int out_len);
int base64_encode(const char *in_bin, int in_len, char *out_base64, int out_len);
]]
......@@ -72,34 +73,25 @@ end
local m = {
base64_encode = function(bin)
if bin == nil then
error('Usage: base64.encode(str)')
else
-- note: need add check size of bin, bin might containe any binary data
if type(bin) == 'string' then
local blen = string.len(bin)
local slen = math.ceil(blen * 4 / 3) + 2
local so = ffi.new('char[?]', slen)
local len = ffi.C.base64_encode(bin, blen, so, slen)
bin = ffi.string(so, len)
else
bin = ''
end
if type(bin) ~= 'string' then
error('Usage: digest.base64_encode(string)')
end
return bin
local blen = #bin
local slen = ffi.C.base64_bufsize(blen)
local str = ffi.new('char[?]', slen)
local len = ffi.C.base64_encode(bin, blen, str, slen)
return ffi.string(str, len)
end,
base64_decode = function(data)
if type(data) ~= 'string' then
data = ''
else
local slen = string.len(data)
local blen = math.ceil(slen * 3 / 4)
local bo = ffi.new('char[?]', blen)
local len = ffi.C.base64_decode(data, slen, bo, blen)
data = ffi.string(bo, len)
base64_decode = function(str)
if type(str) ~= 'string' then
error('Usage: digest.base64_decode(string)')
end
return data
local slen = #str
local blen = math.ceil(slen * 3 / 4)
local bin = ffi.new('char[?]', blen)
local len = ffi.C.base64_decode(str, slen, bin, blen)
return ffi.string(bin, len)
end,
crc32 = function(str)
......
......@@ -173,21 +173,39 @@ digest.base64_decode('MTEgMDAgMTEgMDAgYWJjZGVmIEFCQ0RFRiAwMCAxMSAwMCAxMQ==')
---
- 11 00 11 00 abcdef ABCDEF 00 11 00 11
...
s = string.rep('a', 54 * 2) -- two lines in base64
---
...
b = digest.base64_encode(s)
---
...
b
---
- 'YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh
YWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh
'
...
digest.base64_decode(b) == s
---
- true
...
digest.base64_decode(nil)
---
-
- error: 'builtin/digest.lua:88: Usage: digest.base64_decode(string)'
...
digest.base64_encode(nil)
---
- error: 'builtin/digest.lua:76: Usage: base64.encode(str)'
- error: 'builtin/digest.lua:77: Usage: digest.base64_encode(string)'
...
digest.base64_encode(123)
---
-
- error: 'builtin/digest.lua:77: Usage: digest.base64_encode(string)'
...
digest.base64_decode(123)
---
-
- error: 'builtin/digest.lua:88: Usage: digest.base64_decode(string)'
...
digest = nil
---
......
......@@ -50,6 +50,10 @@ digest.base64_encode('asdfl asdf adfa zxc vzxcvz llll')
digest.base64_decode('YXNkZmwgYXNkZiBhZGZhIHp4YyB2enhjdnogbGxsbA==')
digest.base64_encode('11 00 11 00 abcdef ABCDEF 00 11 00 11')
digest.base64_decode('MTEgMDAgMTEgMDAgYWJjZGVmIEFCQ0RFRiAwMCAxMSAwMCAxMQ==')
s = string.rep('a', 54 * 2) -- two lines in base64
b = digest.base64_encode(s)
b
digest.base64_decode(b) == s
digest.base64_decode(nil)
digest.base64_encode(nil)
digest.base64_encode(123)
......
......@@ -35,6 +35,9 @@
/* {{{ encode */
extern inline int
base64_bufsize(int binsize);
enum base64_encodestep { step_A, step_B, step_C };
struct base64_encodestate {
......
......@@ -39,7 +39,7 @@ extern "C" {
#define BASE64_CHARS_PER_LINE 72
static inline int
inline int
base64_bufsize(int binsize)
{
int datasize = binsize * 4/3 + 4;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment