From e557c65789fb68d172e34a151fff41da67c91ca8 Mon Sep 17 00:00:00 2001 From: Qianqian Fang Date: Tue, 20 Sep 2022 22:25:39 -0400 Subject: [PATCH] let zmatlib C function handle base64 encoding newline instead of zmat.m --- include/zmatlib.h | 3 ++- src/zmatlib.c | 10 +++++----- zmat.m | 5 ----- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/include/zmatlib.h b/include/zmatlib.h index 6fd7f09..1374b66 100644 --- a/include/zmatlib.h +++ b/include/zmatlib.h @@ -146,6 +146,7 @@ char* zmat_error(int id); * @src: Data to be encoded * @len: Length of the data to be encoded * @out_len: Pointer to output length variable, or %NULL if not used + * @mode: 0 or 1, newline every 72 char and at end; 2: no new line at end, 3: no newline * Returns: Allocated buffer of out_len bytes of encoded data, * or %NULL on failure * @@ -155,7 +156,7 @@ char* zmat_error(int id); */ unsigned char* base64_encode(const unsigned char* src, size_t len, - size_t* out_len); + size_t* out_len, int mode); /** * base64_decode - Base64 decode diff --git a/src/zmatlib.c b/src/zmatlib.c index 988aee4..ed66ec1 100644 --- a/src/zmatlib.c +++ b/src/zmatlib.c @@ -195,7 +195,7 @@ int zmat_run(const size_t inputsize, unsigned char* inputstr, size_t* outputsize /** * base64 encoding */ - *outputbuf = base64_encode((const unsigned char*)inputstr, inputsize, outputsize); + *outputbuf = base64_encode((const unsigned char*)inputstr, inputsize, outputsize, clevel); } else if (zipid == zmZlib || zipid == zmGzip) { /** * zlib (.zip) or gzip (.gz) compression @@ -578,11 +578,11 @@ static const unsigned char base64_table[65] = */ unsigned char* base64_encode(const unsigned char* src, size_t len, - size_t* out_len) { + size_t* out_len, int mode) { unsigned char* out, *pos; const unsigned char* end, *in; size_t olen; - int line_len; + size_t line_len; olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ olen += olen / 72; /* line feeds */ @@ -611,7 +611,7 @@ unsigned char* base64_encode(const unsigned char* src, size_t len, in += 3; line_len += 4; - if (line_len >= 72) { + if (mode < 3 && line_len >= 72) { *pos++ = '\n'; line_len = 0; } @@ -633,7 +633,7 @@ unsigned char* base64_encode(const unsigned char* src, size_t len, line_len += 4; } - if (line_len) { + if (mode < 2 && line_len) { *pos++ = '\n'; } diff --git a/zmat.m b/zmat.m index 9e67029..b911911 100644 --- a/zmat.m +++ b/zmat.m @@ -136,11 +136,6 @@ if (strcmp(zipmethod, 'base64') && iscompress > 1) varargout{1} = char(varargout{1}); - if (iscompress == 2) - varargout{1} = regexprep(varargout{1}, '\n$', ''); - elseif (iscompress > 2) - varargout{1} = regexprep(varargout{1}, '\n', ''); - end end if (exist('inputinfo', 'var') && isfield(inputinfo, 'type'))