Skip to content

Commit 04d6c04

Browse files
committed
update encoding and decoding function help info
1 parent 689cb40 commit 04d6c04

17 files changed

+590
-183
lines changed

base64decode.m

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
1-
function output = base64decode(input)
2-
%BASE64DECODE Decode Base64 string to a byte array.
1+
function output = base64decode(varargin)
32
%
4-
% output = base64decode(input)
3+
% output = base64decode(input)
54
%
6-
% The function takes a Base64 string INPUT and returns a uint8 array
7-
% OUTPUT. JAVA must be running to use this function. The result is always
8-
% given as a 1-by-N array, and doesn't retrieve the original dimensions.
9-
%
10-
% See also base64encode
5+
% Decoding a Base64-encoded byte-stream to recover the original data
6+
% This function depends on JVM in MATLAB or, can optionally use the ZMat
7+
% toolbox (http://github.com/fangq/zmat)
118
%
129
% Copyright (c) 2012, Kota Yamaguchi
1310
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
14-
% License : BSD, see LICENSE_*.txt
11+
%
12+
% Modified by: Qianqian Fang (q.fang <at> neu.edu)
13+
%
14+
% input:
15+
% input: a base64-encoded string
16+
%
17+
% output:
18+
% output: the decoded binary byte-stream as a uint8 vector
19+
%
20+
% examples:
21+
% bytes=base64encode('Test JSONLab');
22+
% orig=char(base64decode(bytes))
23+
%
24+
% license:
25+
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
26+
%
27+
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
1528
%
1629

1730
if(nargin==0)
1831
error('you must provide at least 1 input');
1932
end
2033
if(exist('zmat','file')==2 || exist('zmat','file')==3)
21-
output=zmat(uint8(input),0,'base64');
34+
output=zmat(varargin{1},0,'base64');
2235
return;
36+
elseif(isoctavemesh)
37+
error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
2338
end
24-
if(exist('OCTAVE_VERSION','builtin'))
25-
len=rem(numel(input),8)
26-
if(len)
27-
input=[input(:)', repmat(sprintf('\0'),1,(8-len))];
28-
end
29-
output = base64_decode(input);
30-
return;
31-
end
32-
error(javachk('jvm'));
33-
if ischar(input), input = uint8(input); end
3439

35-
output = typecast(org.apache.commons.codec.binary.Base64.decodeBase64(input), 'uint8')';
40+
error(javachk('jvm'));
3641

42+
if(ischar(varargin{1}))
43+
varargin{1}=uint8(varargin{1});
3744
end
3845

46+
input=typecast(varargin{1}(:)','uint8');
47+
48+
output = typecast(org.apache.commons.codec.binary.Base64.decodeBase64(input), 'uint8')';
49+

base64encode.m

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,57 @@
1-
function output = base64encode(input)
2-
%BASE64ENCODE Encode a byte array using Base64 codec.
1+
function varargout = base64encode(varargin)
32
%
4-
% output = base64encode(input)
3+
% output = base64encode(input)
54
%
6-
% The function takes a char, int8, or uint8 array INPUT and returns Base64
7-
% encoded string OUTPUT. JAVA must be running to use this function. Note
8-
% that encoding doesn't preserve input dimensions.
5+
% Encoding a binary vector or array using Base64
96
%
10-
% See also base64decode
7+
% This function depends on JVM in MATLAB or, can optionally use the ZMat
8+
% toolbox (http://github.com/fangq/zmat)
119
%
1210
% Copyright (c) 2012, Kota Yamaguchi
1311
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
14-
% License : BSD, see LICENSE_*.txt
12+
%
13+
% Modified by: Qianqian Fang (q.fang <at> neu.edu)
14+
%
15+
% input:
16+
% input: a base64-encoded string
17+
%
18+
% output:
19+
% output: the decoded binary byte-stream as a uint8 vector
20+
%
21+
% examples:
22+
% bytes=base64encode('Test JSONLab');
23+
% orig=char(base64decode(bytes))
24+
%
25+
% license:
26+
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
27+
%
28+
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
1529
%
1630

1731
if(nargin==0)
1832
error('you must provide at least 1 input');
1933
end
34+
2035
if(exist('zmat','file')==2 || exist('zmat','file')==3)
21-
output=zmat(uint8(input),1,'base64');
36+
[varargout{1:nargout}]=zmat(varargin{1}, 1,'base64',varargin{2:end});
2237
return;
2338
end
24-
if(exist('OCTAVE_VERSION','builtin'))
25-
output = base64_encode(uint8(input));
39+
40+
if(ischar(varargin{1}))
41+
varargin{1}=uint8(varargin{1});
42+
end
43+
44+
input=typecast(varargin{1}(:)','uint8');
45+
46+
if(isoctavemesh)
47+
varargout{1} = base64_encode(uint8(input));
2648
return;
2749
end
28-
error(javachk('jvm'));
29-
if ischar(input), input = uint8(input); end
3050

31-
output = char(org.apache.commons.codec.binary.Base64.encodeBase64Chunked(input))';
32-
output = regexprep(output,'\r','');
51+
error(javachk('jvm'));
52+
if ischar(input)
53+
input = uint8(input);
3354
end
55+
56+
varargout{1} = char(org.apache.commons.codec.binary.Base64.encodeBase64Chunked(input))';
57+
varargout{1} = regexprep(varargout{1} ,'\r','');

fast_match_bracket.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,48 @@
11
function [endpos, maxlevel] = fast_match_bracket(key,pos,startpos,brackets)
2+
%
3+
% [endpos, maxlevel] = fast_match_bracket(key,pos,startpos,brackets)
4+
%
5+
% A fast function to find the position of a closing bracket token in a string
6+
%
7+
% authors:Qianqian Fang (q.fang <at> neu.edu)
8+
%
9+
% input:
10+
% key: a preprocessed string containing only relevant opening/closing
11+
% bracket characters for accelerating the search.
12+
% pos: a 1D integer vector with a length matching the length of key,
13+
% recording the corresponding position of each char. in the original string.
14+
% startpos: the index in the original string as the start position to search; the
15+
% startpos must be at least 1 greater than the opening bracket position
16+
% brackets: (optional), a string of length 2, with the first character
17+
% being the opening token and the 2nd being the closing token.
18+
% if not given, brackets is set to '[]' to find matching square-brackets;
19+
% for example, '{}' looks for a matching closing curly-bracket in
20+
% the string key(pos(startpos,:end))
21+
%
22+
% output:
23+
% endpos: if a matching bracket is found, return its position in the original
24+
% string
25+
% maxlevel: return the depth of the enclosed brackets between the searched pair,
26+
% includig the searching pair. For example, the matching closing-bracket
27+
% of the 1st square bracket (startpos=2) in '[[[]],[]]' returns a
28+
% position of 9, with a maximum depth of 3; searching for the closing
29+
% bracket for the 2nd square bracket (startpos=3) returns a position of
30+
% 5 and max-depth of 2.
31+
%
32+
% example:
33+
% str='[[ [1,2], 1], 10, [5,10] ]';
34+
% pos=find(str=='[' | str==']')
35+
% key=str(pos)
36+
% [p1,dep]=fast_match_bracket(key,1:length(key),3)
37+
% [p2,dep]=fast_match_bracket(key,pos,2)
38+
% [p3,dep]=fast_match_bracket(key,pos,3)
39+
%
40+
% license:
41+
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
42+
%
43+
% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
44+
%
45+
246
if(nargin<4)
347
brackets='[]';
448
end

gzipdecode.m

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,69 @@
1-
function output = gzipdecode(input)
2-
%GZIPDECODE Decompress input bytes using GZIP.
1+
function varargout = gzipdecode(varargin)
32
%
4-
% output = gzipdecode(input)
3+
% output = gzipdecode(input)
4+
% or
5+
% output = gzipdecode(input,info)
56
%
6-
% The function takes a compressed byte array INPUT and returns inflated
7-
% bytes OUTPUT. The INPUT is a result of GZIPENCODE function. The OUTPUT
8-
% is always an 1-by-N uint8 array. JAVA must be enabled to use the function.
9-
%
10-
% See also gzipencode typecast
7+
% Decompressing a GZIP-compressed byte-stream to recover the original data
8+
% This function depends on JVM in MATLAB or, can optionally use the ZMat
9+
% toolbox (http://github.com/fangq/zmat)
1110
%
1211
% Copyright (c) 2012, Kota Yamaguchi
1312
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
14-
% License : BSD, see LICENSE_*.txt
13+
%
14+
% Modified by: Qianqian Fang (q.fang <at> neu.edu)
15+
%
16+
% input:
17+
% input: a string, int8/uint8 vector or numerical array to store the GZIP-compressed data
18+
% info (optional): a struct produced by the zmat/lz4hcencode function during
19+
% compression; if not given, the inputs/outputs will be treated as a
20+
% 1-D vector
21+
%
22+
% output:
23+
% output: the decompressed byte stream stored in a uint8 vector; if info is
24+
% given, output will restore the original data's type and dimensions
25+
%
26+
% examples:
27+
% [bytes, info]=gzipencode(eye(10));
28+
% orig=gzipdecode(bytes,info);
29+
%
30+
% license:
31+
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
32+
%
33+
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
1534
%
1635

1736
if(nargin==0)
1837
error('you must provide at least 1 input');
1938
end
2039
if(exist('zmat','file')==2 || exist('zmat','file')==3)
21-
output=zmat(uint8(input),0,'gzip');
40+
if(nargin>1)
41+
[varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
42+
else
43+
[varargout{1:nargout}]=zmat(varargin{1},0,'gzip',varargin{2:end});
44+
end
2245
return;
46+
elseif(isoctavemesh)
47+
error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
2348
end
2449
error(javachk('jvm'));
25-
if ischar(input)
26-
warning('gzipdecode:inputTypeMismatch', ...
27-
'Input is char, but treated as uint8.');
28-
input = uint8(input);
29-
end
30-
if ~isa(input, 'int8') && ~isa(input, 'uint8')
31-
error('Input must be either int8 or uint8.');
50+
51+
if(ischar(varargin{1}))
52+
varargin{1}=uint8(varargin{1});
3253
end
3354

55+
input=typecast(varargin{1}(:)','uint8');
56+
3457
gzip = java.util.zip.GZIPInputStream(java.io.ByteArrayInputStream(input));
3558
buffer = java.io.ByteArrayOutputStream();
3659
org.apache.commons.io.IOUtils.copy(gzip, buffer);
3760
gzip.close();
38-
output = typecast(buffer.toByteArray(), 'uint8')';
3961

40-
end
62+
if(nargout>0)
63+
varargout{1} = typecast(buffer.toByteArray(), 'uint8')';
64+
if(nargin>1 && isstruct(varargin{2}) && isfield(varargin{2},'type'))
65+
inputinfo=varargin{2};
66+
varargout{1}=typecast(varargout{1},inputinfo.type);
67+
varargout{1}=reshape(varargout{1},inputinfo.size);
68+
end
69+
end

gzipencode.m

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
1-
function output = gzipencode(input)
2-
%GZIPENCODE Compress input bytes with GZIP.
1+
function varargout = gzipencode(varargin)
32
%
4-
% output = gzipencode(input)
3+
% output = gzipencode(input)
4+
% or
5+
% [output, info] = gzipencode(input)
56
%
6-
% The function takes a char, int8, or uint8 array INPUT and returns
7-
% compressed bytes OUTPUT as a uint8 array. Note that the compression
8-
% doesn't preserve input dimensions. JAVA must be enabled to use the
9-
% function.
7+
% Compress a string or numerical array using the GZIP-compression
108
%
11-
% See also gzipdecode typecast
9+
% This function depends on JVM in MATLAB or, can optionally use the ZMat
10+
% toolbox (http://github.com/fangq/zmat)
1211
%
1312
% Copyright (c) 2012, Kota Yamaguchi
1413
% URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
15-
% License : BSD, see LICENSE_*.txt
1614
%
15+
% Modified by: Qianqian Fang (q.fang <at> neu.edu)
16+
%
17+
% input:
18+
% input: the original data, can be a string, a numerical vector or array
19+
%
20+
% output:
21+
% output: the decompressed byte stream stored in a uint8 vector; if info is
22+
% given, output will restore the original data's type and dimensions
23+
%
24+
% examples:
25+
% [bytes, info]=gzipencode(eye(10));
26+
% orig=gzipdecode(bytes,info);
27+
%
28+
% license:
29+
% BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
30+
%
31+
% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
32+
%
33+
1734

1835
if(nargin==0)
1936
error('you must provide at least 1 input');
2037
end
38+
2139
if(exist('zmat','file')==2 || exist('zmat','file')==3)
22-
output=zmat(uint8(input),1,'gzip');
40+
[varargout{1:nargout}]=zmat(varargin{1},1,'gzip');
2341
return;
42+
elseif(isoctavemesh)
43+
error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
2444
end
45+
2546
error(javachk('jvm'));
26-
if ischar(input), input = uint8(input); end
27-
if ~isa(input, 'int8') && ~isa(input, 'uint8')
28-
error('Input must be either char, int8 or uint8.');
47+
48+
if(ischar(varargin{1}))
49+
varargin{1}=uint8(varargin{1});
2950
end
3051

52+
input=typecast(varargin{1}(:)','uint8');
53+
3154
buffer = java.io.ByteArrayOutputStream();
3255
gzip = java.util.zip.GZIPOutputStream(buffer);
3356
gzip.write(input, 0, numel(input));
3457
gzip.close();
35-
output = typecast(buffer.toByteArray(), 'uint8')';
3658

37-
end
59+
varargout{1} = typecast(buffer.toByteArray(), 'uint8')';
3860

61+
if(nargout>1)
62+
varargout{2}=struct('type',class(varargin{1}),'size',size(varargin{1}),'method','gzip','status',0);
63+
end

0 commit comments

Comments
 (0)