Skip to content

Commit

Permalink
let zmatlib C function handle base64 encoding newline instead of zmat.m
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Sep 21, 2022
1 parent afe5654 commit e557c65
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
3 changes: 2 additions & 1 deletion include/zmatlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/zmatlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand All @@ -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';
}

Expand Down
5 changes: 0 additions & 5 deletions zmat.m
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit e557c65

Please sign in to comment.