From 04d6c04d420a13d040c089c8081993d12b710ae5 Mon Sep 17 00:00:00 2001
From: Qianqian Fang <fangqq@gmail.com>
Date: Mon, 14 Oct 2019 19:02:16 -0400
Subject: [PATCH] update encoding and decoding function help info

---
 base64decode.m       | 53 ++++++++++++++++++++--------------
 base64encode.m       | 54 +++++++++++++++++++++++++----------
 fast_match_bracket.m | 44 ++++++++++++++++++++++++++++
 gzipdecode.m         | 67 ++++++++++++++++++++++++++++++-------------
 gzipencode.m         | 55 +++++++++++++++++++++++++----------
 lz4decode.m          | 40 +++++++++++++++++++-------
 lz4encode.m          | 34 ++++++++++++++++------
 lz4hcdecode.m        | 40 +++++++++++++++++++-------
 lz4hcencode.m        | 34 ++++++++++++++++------
 lzipdecode.m         | 40 +++++++++++++++++++-------
 lzipencode.m         | 34 ++++++++++++++++------
 lzmadecode.m         | 40 +++++++++++++++++++-------
 lzmaencode.m         | 34 ++++++++++++++++------
 match_bracket.m      | 39 +++++++++++++++++++++++++
 nestbracket2dim.m    | 44 +++++++++++++++++++++++++++-
 zlibdecode.m         | 68 +++++++++++++++++++++++++++++++-------------
 zlibencode.m         | 53 +++++++++++++++++++++++-----------
 17 files changed, 590 insertions(+), 183 deletions(-)

diff --git a/base64decode.m b/base64decode.m
index 87cbdce..b4e2639 100644
--- a/base64decode.m
+++ b/base64decode.m
@@ -1,38 +1,49 @@
-function output = base64decode(input)
-%BASE64DECODE Decode Base64 string to a byte array.
+function output = base64decode(varargin)
 %
-%    output = base64decode(input)
+% output = base64decode(input)
 %
-% The function takes a Base64 string INPUT and returns a uint8 array
-% OUTPUT. JAVA must be running to use this function. The result is always
-% given as a 1-by-N array, and doesn't retrieve the original dimensions.
-%
-% See also base64encode
+% Decoding a Base64-encoded byte-stream to recover the original data
+% This function depends on JVM in MATLAB or, can optionally use the ZMat 
+% toolbox (http://github.com/fangq/zmat)
 %
 % Copyright (c) 2012, Kota Yamaguchi
 % URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
-% License : BSD, see LICENSE_*.txt
+%
+% Modified by: Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: a base64-encoded string
+%
+% output:
+%      output: the decoded binary byte-stream as a uint8 vector
+%
+% examples:
+%      bytes=base64encode('Test JSONLab');
+%      orig=char(base64decode(bytes))
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),0,'base64');
+    output=zmat(varargin{1},0,'base64');
     return;
+elseif(isoctavemesh)
+    error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
 end
-if(exist('OCTAVE_VERSION','builtin'))
-    len=rem(numel(input),8)
-    if(len)
-       input=[input(:)', repmat(sprintf('\0'),1,(8-len))];
-    end
-    output = base64_decode(input);
-    return;
-end
-error(javachk('jvm'));
-if ischar(input), input = uint8(input); end
 
-output = typecast(org.apache.commons.codec.binary.Base64.decodeBase64(input), 'uint8')';
+error(javachk('jvm'));
 
+if(ischar(varargin{1}))
+    varargin{1}=uint8(varargin{1});
 end
 
+input=typecast(varargin{1}(:)','uint8');
+
+output = typecast(org.apache.commons.codec.binary.Base64.decodeBase64(input), 'uint8')';
+
diff --git a/base64encode.m b/base64encode.m
index 66660ab..344552e 100644
--- a/base64encode.m
+++ b/base64encode.m
@@ -1,33 +1,57 @@
-function output = base64encode(input)
-%BASE64ENCODE Encode a byte array using Base64 codec.
+function varargout = base64encode(varargin)
 %
-%    output = base64encode(input)
+% output = base64encode(input)
 %
-% The function takes a char, int8, or uint8 array INPUT and returns Base64
-% encoded string OUTPUT. JAVA must be running to use this function. Note
-% that encoding doesn't preserve input dimensions.
+% Encoding a binary vector or array using Base64
 %
-% See also base64decode
+% This function depends on JVM in MATLAB or, can optionally use the ZMat 
+% toolbox (http://github.com/fangq/zmat)
 %
 % Copyright (c) 2012, Kota Yamaguchi
 % URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
-% License : BSD, see LICENSE_*.txt
+%
+% Modified by: Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: a base64-encoded string
+%
+% output:
+%      output: the decoded binary byte-stream as a uint8 vector
+%
+% examples:
+%      bytes=base64encode('Test JSONLab');
+%      orig=char(base64decode(bytes))
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
+
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),1,'base64');
+    [varargout{1:nargout}]=zmat(varargin{1}, 1,'base64',varargin{2:end});
     return;
 end
-if(exist('OCTAVE_VERSION','builtin'))
-    output = base64_encode(uint8(input));
+
+if(ischar(varargin{1}))
+    varargin{1}=uint8(varargin{1});
+end
+
+input=typecast(varargin{1}(:)','uint8');
+
+if(isoctavemesh)
+    varargout{1} = base64_encode(uint8(input));
     return;
 end
-error(javachk('jvm'));
-if ischar(input), input = uint8(input); end
 
-output = char(org.apache.commons.codec.binary.Base64.encodeBase64Chunked(input))';
-output = regexprep(output,'\r','');
+error(javachk('jvm'));
+if ischar(input)
+    input = uint8(input);
 end
+
+varargout{1}  = char(org.apache.commons.codec.binary.Base64.encodeBase64Chunked(input))';
+varargout{1}  = regexprep(varargout{1} ,'\r','');
diff --git a/fast_match_bracket.m b/fast_match_bracket.m
index 272ccc9..825b6bc 100644
--- a/fast_match_bracket.m
+++ b/fast_match_bracket.m
@@ -1,4 +1,48 @@
 function [endpos, maxlevel] = fast_match_bracket(key,pos,startpos,brackets)
+%
+% [endpos, maxlevel] = fast_match_bracket(key,pos,startpos,brackets)
+%
+% A fast function to find the position of a closing bracket token in a string
+%
+% authors:Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      key: a preprocessed string containing only relevant opening/closing 
+%           bracket characters for accelerating the search.
+%      pos: a 1D integer vector with a length matching the length of key, 
+%           recording the corresponding position of each char. in the original string.
+%      startpos: the index in the original string as the start position to search; the
+%               startpos must be at least 1 greater than the opening bracket position
+%      brackets: (optional), a string of length 2, with the first character
+%               being the opening token and the 2nd being the closing token.
+%               if not given, brackets is set to '[]' to find matching square-brackets;
+%               for example, '{}' looks for a matching closing curly-bracket in
+%               the string key(pos(startpos,:end))
+%
+% output:
+%      endpos: if a matching bracket is found, return its position in the original 
+%              string
+%      maxlevel: return the depth of the enclosed brackets between the searched pair,
+%              includig the searching pair. For example, the matching closing-bracket 
+%              of the 1st square bracket (startpos=2) in  '[[[]],[]]' returns a 
+%              position of 9, with a maximum depth of 3; searching for the closing 
+%              bracket for the 2nd square bracket (startpos=3) returns a position of 
+%              5 and max-depth of 2.
+%
+% example:
+%      str='[[ [1,2], 1], 10, [5,10] ]';
+%      pos=find(str=='[' | str==']')
+%      key=str(pos)
+%      [p1,dep]=fast_match_bracket(key,1:length(key),3)
+%      [p2,dep]=fast_match_bracket(key,pos,2)
+%      [p3,dep]=fast_match_bracket(key,pos,3)
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
+%
+% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
+
 if(nargin<4)
     brackets='[]';
 end
diff --git a/gzipdecode.m b/gzipdecode.m
index 76e9fea..905da6b 100644
--- a/gzipdecode.m
+++ b/gzipdecode.m
@@ -1,40 +1,69 @@
-function output = gzipdecode(input)
-%GZIPDECODE Decompress input bytes using GZIP.
+function varargout = gzipdecode(varargin)
 %
-%    output = gzipdecode(input)
+% output = gzipdecode(input)
+%    or
+% output = gzipdecode(input,info)
 %
-% The function takes a compressed byte array INPUT and returns inflated
-% bytes OUTPUT. The INPUT is a result of GZIPENCODE function. The OUTPUT
-% is always an 1-by-N uint8 array. JAVA must be enabled to use the function.
-%
-% See also gzipencode typecast
+% Decompressing a GZIP-compressed byte-stream to recover the original data
+% This function depends on JVM in MATLAB or, can optionally use the ZMat 
+% toolbox (http://github.com/fangq/zmat)
 %
 % Copyright (c) 2012, Kota Yamaguchi
 % URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
-% License : BSD, see LICENSE_*.txt
+%
+% Modified by: Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: a string, int8/uint8 vector or numerical array to store the GZIP-compressed data
+%      info (optional): a struct produced by the zmat/lz4hcencode function during 
+%            compression; if not given, the inputs/outputs will be treated as a
+%            1-D vector
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=gzipencode(eye(10));
+%      orig=gzipdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),0,'gzip');
+    if(nargin>1)
+        [varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
+    else
+        [varargout{1:nargout}]=zmat(varargin{1},0,'gzip',varargin{2:end});
+    end
     return;
+elseif(isoctavemesh)
+    error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
 end
 error(javachk('jvm'));
-if ischar(input)
-  warning('gzipdecode:inputTypeMismatch', ...
-          'Input is char, but treated as uint8.');
-  input = uint8(input);
-end
-if ~isa(input, 'int8') && ~isa(input, 'uint8')
-    error('Input must be either int8 or uint8.');
+
+if(ischar(varargin{1}))
+    varargin{1}=uint8(varargin{1});
 end
 
+input=typecast(varargin{1}(:)','uint8');
+
 gzip = java.util.zip.GZIPInputStream(java.io.ByteArrayInputStream(input));
 buffer = java.io.ByteArrayOutputStream();
 org.apache.commons.io.IOUtils.copy(gzip, buffer);
 gzip.close();
-output = typecast(buffer.toByteArray(), 'uint8')';
 
-end
+if(nargout>0)
+    varargout{1} = typecast(buffer.toByteArray(), 'uint8')';
+    if(nargin>1 && isstruct(varargin{2}) && isfield(varargin{2},'type'))
+        inputinfo=varargin{2};
+	varargout{1}=typecast(varargout{1},inputinfo.type);
+	varargout{1}=reshape(varargout{1},inputinfo.size);
+    end
+end
\ No newline at end of file
diff --git a/gzipencode.m b/gzipencode.m
index 5e363e8..9ab8c2e 100644
--- a/gzipencode.m
+++ b/gzipencode.m
@@ -1,38 +1,63 @@
-function output = gzipencode(input)
-%GZIPENCODE Compress input bytes with GZIP.
+function varargout = gzipencode(varargin)
 %
-%    output = gzipencode(input)
+% output = gzipencode(input)
+%    or
+% [output, info] = gzipencode(input)
 %
-% The function takes a char, int8, or uint8 array INPUT and returns
-% compressed bytes OUTPUT as a uint8 array. Note that the compression
-% doesn't preserve input dimensions. JAVA must be enabled to use the
-% function.
+% Compress a string or numerical array using the GZIP-compression
 %
-% See also gzipdecode typecast
+% This function depends on JVM in MATLAB or, can optionally use the ZMat 
+% toolbox (http://github.com/fangq/zmat)
 %
 % Copyright (c) 2012, Kota Yamaguchi
 % URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
-% License : BSD, see LICENSE_*.txt
 %
+% Modified by: Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: the original data, can be a string, a numerical vector or array
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=gzipencode(eye(10));
+%      orig=gzipdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
+
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
+
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),1,'gzip');
+    [varargout{1:nargout}]=zmat(varargin{1},1,'gzip');
     return;
+elseif(isoctavemesh)
+    error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
 end
+
 error(javachk('jvm'));
-if ischar(input), input = uint8(input); end
-if ~isa(input, 'int8') && ~isa(input, 'uint8')
-    error('Input must be either char, int8 or uint8.');
+
+if(ischar(varargin{1}))
+    varargin{1}=uint8(varargin{1});
 end
 
+input=typecast(varargin{1}(:)','uint8');
+
 buffer = java.io.ByteArrayOutputStream();
 gzip = java.util.zip.GZIPOutputStream(buffer);
 gzip.write(input, 0, numel(input));
 gzip.close();
-output = typecast(buffer.toByteArray(), 'uint8')';
 
-end
+varargout{1} = typecast(buffer.toByteArray(), 'uint8')';
 
+if(nargout>1)
+    varargout{2}=struct('type',class(varargin{1}),'size',size(varargin{1}),'method','gzip','status',0);
+end
diff --git a/lz4decode.m b/lz4decode.m
index 7c0e479..a259fbf 100644
--- a/lz4decode.m
+++ b/lz4decode.m
@@ -1,23 +1,43 @@
-function output = lz4decode(input)
-%LZ4DECODE Decompress input bytes using lz4.
+function varargout = lz4decode(varargin)
 %
-%    output = lz4decode(input)
+% output = lz4decode(input)
+%    or
+% output = lz4decode(input,info)
 %
-% The function takes a compressed byte array INPUT and returns inflated
-% bytes OUTPUT. The INPUT is a result of LZ4DECODE function. The OUTPUT
-% is always an 1-by-N uint8 array.
+% Decompressing an LZ4-compressed byte-stream to recover the original data
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% See also lz4encode typecast
+% authors:Qianqian Fang (q.fang <at> neu.edu)
 %
-% License : BSD, see LICENSE_*.txt
+% input:
+%      input: a string, int8/uint8 vector or numerical array to store LZ4-compressed data
+%      info (optional): a struct produced by the zmat/lz4encode function during 
+%            compression; if not given, the inputs/outputs will be treated as a
+%            1-D vector
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=lz4encode(eye(10));
+%      orig=lz4decode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),0,'lz4');
-    return;
+    if(nargin>1)
+        [varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
+    else
+        [varargout{1:nargout}]=zmat(varargin{1},0,'lz4',varargin{2:end});
+    end
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
 end
diff --git a/lz4encode.m b/lz4encode.m
index 20cb36b..ecaa185 100644
--- a/lz4encode.m
+++ b/lz4encode.m
@@ -1,22 +1,38 @@
-function output = lz4encode(input)
-%LZ4ENCODE Compress input bytes with lz4.
+function varargout = lz4hcencode(varargin)
 %
-%    output = lz4encode(input)
+% output = lz4encode(input)
+%    or
+% [output, info] = lz4encode(input)
 %
-% The function takes a char, int8, or uint8 array INPUT and returns
-% compressed bytes OUTPUT as a uint8 array. Note that the compression
-% doesn't preserve input dimensions.
+% Compress a string or a numerical array using LZ4-compression
 %
-% See also lz4decode
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% License : BSD, see LICENSE_*.txt
+% authors:Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: the original data, can be a string, a numerical vector or array
+%
+% output:
+%      output: the compressed byte stream stored in a uint8 vector
+%      info: (optional) a struct storing the metadata of the input, see "help zmat" for details
+%
+% examples:
+%      [bytes, info]=lz4encode(eye(10));
+%      orig=lz4decode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
+
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),1,'lz4');
+    [varargout{1:nargout}]=zmat(varargin{1}, 1,'lz4',varargin{2:end});
     return;
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
diff --git a/lz4hcdecode.m b/lz4hcdecode.m
index cc1e359..c1dfe9e 100644
--- a/lz4hcdecode.m
+++ b/lz4hcdecode.m
@@ -1,23 +1,43 @@
-function output = lz4hcdecode(input)
-%LZ4HCDECODE Decompress input bytes using lz4hc.
+function varargout = lz4hcdecode(varargin)
 %
-%    output = lz4hcdecode(input)
+% output = lz4hcdecode(input)
+%    or
+% output = lz4hcdecode(input,info)
 %
-% The function takes a compressed byte array INPUT and returns inflated
-% bytes OUTPUT. The INPUT is a result of LZ4HCDECODE function. The OUTPUT
-% is always an 1-by-N uint8 array.
+% Decompressing an LZ4HC-compressed byte-stream to recover the original data
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% See also lz4hcencode typecast
+% authors:Qianqian Fang (q.fang <at> neu.edu)
 %
-% License : BSD, see LICENSE_*.txt
+% input:
+%      input: a string, int8/uint8 vector or numerical array to store LZ4HC-compressed data
+%      info (optional): a struct produced by the zmat/lz4hcencode function during 
+%            compression; if not given, the inputs/outputs will be treated as a
+%            1-D vector
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=lz4hcencode(eye(10));
+%      orig=lz4hcdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),0,'lz4hc');
-    return;
+    if(nargin>1)
+        [varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
+    else
+        [varargout{1:nargout}]=zmat(varargin{1},0,'lz4hc',varargin{2:end});
+    end
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
 end
diff --git a/lz4hcencode.m b/lz4hcencode.m
index 2cadd04..822b748 100644
--- a/lz4hcencode.m
+++ b/lz4hcencode.m
@@ -1,22 +1,38 @@
-function output = lz4hcencode(input)
-%LZ4HCENCODE Compress input bytes with lz4hc.
+function varargout = lz4hcencode(varargin)
 %
-%    output = lz4hcencode(input)
+% output = lz4hcencode(input)
+%    or
+% [output, info] = lz4hcencode(input)
 %
-% The function takes a char, int8, or uint8 array INPUT and returns
-% compressed bytes OUTPUT as a uint8 array. Note that the compression
-% doesn't preserve input dimensions.
+% Compress a string or a numerical array using LZ4HC-compression
 %
-% See also lz4hcdecode
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% License : BSD, see LICENSE_*.txt
+% authors:Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: the original data, can be a string, a numerical vector or array
+%
+% output:
+%      output: the compressed byte stream stored in a uint8 vector
+%      info: (optional) a struct storing the metadata of the input, see "help zmat" for details
+%
+% examples:
+%      [bytes, info]=lz4hcencode(eye(10));
+%      orig=lz4hcdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
+
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),1,'lz4hc');
+    [varargout{1:nargout}]=zmat(varargin{1}, 1,'lz4hc',varargin{2:end});
     return;
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
diff --git a/lzipdecode.m b/lzipdecode.m
index 9cc1897..bf377c6 100644
--- a/lzipdecode.m
+++ b/lzipdecode.m
@@ -1,23 +1,43 @@
-function output = lzipdecode(input)
-%LZIPDECODE Decompress input bytes using lzip.
+function varargout = lzipdecode(varargin)
 %
-%    output = lzipdecode(input)
+% output = lzipdecode(input)
+%    or
+% output = lzipdecode(input,info)
 %
-% The function takes a compressed byte array INPUT and returns inflated
-% bytes OUTPUT. The INPUT is a result of LZIPDECODE function. The OUTPUT
-% is always an 1-by-N uint8 array.
+% Decompressing an Lzip-compressed byte-stream to recover the original data
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% See also lzipencode typecast
+% authors:Qianqian Fang (q.fang <at> neu.edu)
 %
-% License : BSD, see LICENSE_*.txt
+% input:
+%      input: a string, int8/uint8 vector or numerical array to store Lzip-compressed data
+%      info (optional): a struct produced by the zmat/lzipencode function during 
+%            compression; if not given, the inputs/outputs will be treated as a
+%            1-D vector
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=lzipencode(eye(10));
+%      orig=lzipdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),0,'lzip');
-    return;
+    if(nargin>1)
+        [varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
+    else
+        [varargout{1:nargout}]=zmat(varargin{1},0,'lzip',varargin{2:end});
+    end
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
 end
diff --git a/lzipencode.m b/lzipencode.m
index ecd246b..508839a 100644
--- a/lzipencode.m
+++ b/lzipencode.m
@@ -1,22 +1,38 @@
-function output = lzipencode(input)
-%LZIPENCODE Compress input bytes with lzip.
+function varargout = lzipencode(varargin)
 %
-%    output = lzipencode(input)
+% output = lzipencode(input)
+%    or
+% [output, info] = lzipencode(input)
 %
-% The function takes a char, int8, or uint8 array INPUT and returns
-% compressed bytes OUTPUT as a uint8 array. Note that the compression
-% doesn't preserve input dimensions.
+% Compress a string or a numerical array using LZip-compression
 %
-% See also lzipdecode
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% License : BSD, see LICENSE_*.txt
+% authors:Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: the original data, can be a string, a numerical vector or array
+%
+% output:
+%      output: the compressed byte stream stored in a uint8 vector
+%      info: (optional) a struct storing the metadata of the input, see "help zmat" for details
+%
+% examples:
+%      [bytes, info]=lzipencode(eye(10));
+%      orig=lzipdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
+
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),1,'lzip');
+    [varargout{1:nargout}]=zmat(varargin{1}, 1,'lzip',varargin{2:end});
     return;
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
diff --git a/lzmadecode.m b/lzmadecode.m
index 77ebd57..9eb8d6d 100644
--- a/lzmadecode.m
+++ b/lzmadecode.m
@@ -1,23 +1,43 @@
-function output = lzmadecode(input)
-%LZMADECODE Decompress input bytes using lzma.
+function varargout = lzmadecode(varargin)
 %
-%    output = lzmadecode(input)
+% output = lzmadecode(input)
+%    or
+% output = lzmadecode(input,info)
 %
-% The function takes a compressed byte array INPUT and returns inflated
-% bytes OUTPUT. The INPUT is a result of LZMADECODE function. The OUTPUT
-% is always an 1-by-N uint8 array.
+% Decompressing an LZMA-compressed byte-stream to recover the original data
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% See also lzmaencode typecast
+% authors:Qianqian Fang (q.fang <at> neu.edu)
 %
-% License : BSD, see LICENSE_*.txt
+% input:
+%      input: a string, int8/uint8 vector or numerical array to store LZMA-compressed data
+%      info (optional): a struct produced by the zmat/lzmaencode function during 
+%            compression; if not given, the inputs/outputs will be treated as a
+%            1-D vector
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=lzmaencode(eye(10));
+%      orig=lzmadecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),0,'lzma');
-    return;
+    if(nargin>1)
+        [varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
+    else
+        [varargout{1:nargout}]=zmat(varargin{1},0,'lzma',varargin{2:end});
+    end
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
 end
diff --git a/lzmaencode.m b/lzmaencode.m
index 2738366..dcb0ac9 100644
--- a/lzmaencode.m
+++ b/lzmaencode.m
@@ -1,22 +1,38 @@
-function output = lzmaencode(input)
-%LZMAENCODE Compress input bytes with lzma.
+function varargout = lzmaencode(varargin)
 %
-%    output = lzmaencode(input)
+% output = lzmaencode(input)
+%    or
+% [output, info] = lzmaencode(input)
 %
-% The function takes a char, int8, or uint8 array INPUT and returns
-% compressed bytes OUTPUT as a uint8 array. Note that the compression
-% doesn't preserve input dimensions.
+% Compress a string or a numerical array using LZMA-compression
 %
-% See also lzmadecode
+% This function depends on the ZMat toolbox (http://github.com/fangq/zmat)
 %
-% License : BSD, see LICENSE_*.txt
+% authors:Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: the original data, can be a string, a numerical vector or array
+%
+% output:
+%      output: the compressed byte stream stored in a uint8 vector
+%      info: (optional) a struct storing the metadata of the input, see "help zmat" for details
+%
+% examples:
+%      [bytes, info]=lzmaencode(eye(10));
+%      orig=lzmadecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
 %
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
+
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),1,'lzma');
+    [varargout{1:nargout}]=zmat(varargin{1}, 1,'lzma',varargin{2:end});
     return;
 else
     error('you must install ZMat toolbox to use this feature: http://github.com/fangq/zmat')
diff --git a/match_bracket.m b/match_bracket.m
index d6c57ff..aa61758 100644
--- a/match_bracket.m
+++ b/match_bracket.m
@@ -1,4 +1,43 @@
 function [endpos, maxlevel] = match_bracket(str,startpos,brackets)
+%
+% [endpos, maxlevel] = match_bracket(str,startpos,brackets)
+%
+% Looking for the position of a closing bracket token in a string
+%
+% authors:Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      str: the full string to be searched
+%      startpos: the index in the string as the start position to search; the
+%               startpos must be at least 1 greater than the opening bracket position
+%      brackets: (optional), a string of length 2, with the first character
+%               being the opening token and the 2nd being the closing token.
+%               if not given, brackets is set to '[]' to find matching square-brackets;
+%               for example, '{}' looks for a matching closing curly-bracket in
+%               the string key(pos(startpos,:end))
+%
+% output:
+%      endpos: if a matching bracket is found, return its position in the original 
+%              string
+%      maxlevel: return the depth of the enclosed brackets between the searched pair,
+%              includig the searching pair. For example, the matching closing-bracket 
+%              of the 1st square bracket (startpos=2) in  '[[[]],[]]' returns a 
+%              position of 9, with a maximum depth of 3; searching for the closing 
+%              bracket for the 2nd square bracket (startpos=3) returns a position of 
+%              5 and max-depth of 2.
+%
+% example:
+%      str='[[ [1,2], 1], 10, [5,10] ]';
+%      [p1,dep]=match_bracket(str,3)
+%      [p2,dep]=match_bracket(str,2)
+%      [p3,dep]=match_bracket(str,3)
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
+%
+% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
+
 if(nargin<3)
     brackets='[]';
 end
diff --git a/nestbracket2dim.m b/nestbracket2dim.m
index c499841..1562e3f 100644
--- a/nestbracket2dim.m
+++ b/nestbracket2dim.m
@@ -1,4 +1,46 @@
-function [dims,maxlevel, count] = nestbracket2dim(str,brackets)
+function [dims, maxlevel, count] = nestbracket2dim(str,brackets)
+%
+% [dims, maxlevel, count] = nestbracket2dim(str,brackets)
+%
+% Extracting the dimension vector of a JSON string formatted array
+% by analyzing the pairs of opening/closing bracket tokenss; this function 
+% only returns valid dimension information when the array is an N-D array
+%
+% authors:Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      str: a string-formatted JSON array using square-brackets for enclosing
+%           elements and comma a separator between elements
+%      brackets: (optional), a string of length 2, with the first character
+%               being the opening token and the 2nd being the closing token.
+%               if not given, brackets is set to '[]' to find matching square-brackets;
+%               for example, '{}' looks for a matching closing curly-bracket in
+%               the string key(pos(startpos,:end))
+%
+% output:
+%      dims: the speculated dimension vector with the length matching the maximum 
+%            depth of the embedded bracket pairs. When the input string encodes an
+%            N-D array, the dims vector contains all integers; however, returning
+%            an all-integer dims vector does not mean the array is rectangular.
+%      maxlevel: return the depth of the enclosed brackets in the string, i.e. the
+%            length of the dims vector.
+%      count: the relative depth from the level 0 - scanning from the left
+%            to right of the string, an opening token increases the level by 1
+%            and a closing token decreases the level by 1; a zero indicates
+%            the positions of a matching bracket of the same level.
+%
+% example:
+%      str='[[ [1,2,3], [4,2,1]], [ [10,1,0], [2,5,10]] ]'; % an N-D array
+%      [dim,dep]=nestbracket2dim(str)
+%      str='[[ [1,2,3], [4,2,1]], [ [10,1,0], [2,5]] ]'; % an invalid N-D array
+%      [dim,dep]=nestbracket2dim(str)
+
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details
+%
+% -- this function is part of jsonlab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
+
 if(nargin<2)
     brackets='[]';
 end
diff --git a/zlibdecode.m b/zlibdecode.m
index d5aca21..2566d88 100644
--- a/zlibdecode.m
+++ b/zlibdecode.m
@@ -1,40 +1,70 @@
-function output = zlibdecode(input)
-%ZLIBDECODE Decompress input bytes using ZLIB.
+function varargout = zlibdecode(varargin)
 %
-%    output = zlibdecode(input)
+% output = zlibdecode(input)
+%    or
+% output = zlibdecode(input,info)
 %
-% The function takes a compressed byte array INPUT and returns inflated
-% bytes OUTPUT. The INPUT is a result of GZIPENCODE function. The OUTPUT
-% is always an 1-by-N uint8 array. JAVA must be enabled to use the function.
-%
-% See also zlibencode typecast
+% Decompressing a ZLIB-compressed byte-stream to recover the original data
+% This function depends on JVM in MATLAB or, can optionally use the ZMat 
+% toolbox (http://github.com/fangq/zmat)
 %
 % Copyright (c) 2012, Kota Yamaguchi
 % URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
-% License : BSD, see LICENSE_*.txt
 %
+% Modified by: Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: a string, int8/uint8 vector or numerical array to store ZLIB-compressed data
+%      info (optional): a struct produced by the zmat/lz4hcencode function during 
+%            compression; if not given, the inputs/outputs will be treated as a
+%            1-D vector
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=zlibencode(eye(10));
+%      orig=zlibdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
+
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(uint8(input),0,'zlib');
+    if(nargin>1)
+        [varargout{1:nargout}]=zmat(varargin{1},varargin{2:end});
+    else
+        [varargout{1:nargout}]=zmat(varargin{1},0,'zlib',varargin{2:end});
+    end
     return;
+elseif(isoctavemesh)
+    error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
 end
 error(javachk('jvm'));
-if ischar(input)
-  warning('zlibdecode:inputTypeMismatch', ...
-          'Input is char, but treated as uint8.');
-  input = uint8(input);
-end
-if ~isa(input, 'int8') && ~isa(input, 'uint8')
-    error('Input must be either int8 or uint8.');
+
+if(ischar(varargin{1}))
+    varargin{1}=uint8(varargin{1});
 end
 
+input=typecast(varargin{1}(:)','uint8');
+
 buffer = java.io.ByteArrayOutputStream();
 zlib = java.util.zip.InflaterOutputStream(buffer);
 zlib.write(input, 0, numel(input));
 zlib.close();
-output = typecast(buffer.toByteArray(), 'uint8')';
 
-end
+if(nargout>0)
+    varargout{1} = typecast(buffer.toByteArray(), 'uint8')';
+    if(nargin>1 && isstruct(varargin{2}) && isfield(varargin{2},'type'))
+        inputinfo=varargin{2};
+	varargout{1}=typecast(varargout{1},inputinfo.type);
+	varargout{1}=reshape(varargout{1},inputinfo.size);
+    end
+end
\ No newline at end of file
diff --git a/zlibencode.m b/zlibencode.m
index eed836c..f4e9295 100644
--- a/zlibencode.m
+++ b/zlibencode.m
@@ -1,39 +1,58 @@
-function output = zlibencode(input)
-%ZLIBENCODE Compress input bytes with ZLIB.
+function varargout = zlibencode(varargin)
 %
-%    output = zlibencode(input)
+% output = zlibencode(input)
+%    or
+% [output, info] = zlibencode(input)
 %
-% The function takes a char, int8, or uint8 array INPUT and returns
-% compressed bytes OUTPUT as a uint8 array. Note that the compression
-% doesn't preserve input dimensions. JAVA must be enabled to use the
-% function.
+% Compress a string or numerical array using the ZLIB-compression
 %
-% See also zlibdecode typecast
+% This function depends on JVM in MATLAB or, can optionally use the ZMat 
+% toolbox (http://github.com/fangq/zmat)
 %
 % Copyright (c) 2012, Kota Yamaguchi
 % URL: https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities
-% License : BSD, see LICENSE_*.txt
 %
-
+% Modified by: Qianqian Fang (q.fang <at> neu.edu)
+%
+% input:
+%      input: the original data, can be a string, a numerical vector or array
+%
+% output:
+%      output: the decompressed byte stream stored in a uint8 vector; if info is 
+%            given, output will restore the original data's type and dimensions
+%
+% examples:
+%      [bytes, info]=zlibencode(eye(10));
+%      orig=zlibdecode(bytes,info);
+%
+% license:
+%     BSD or GPL version 3, see LICENSE_{BSD,GPLv3}.txt files for details 
+%
+% -- this function is part of JSONLab toolbox (http://iso2mesh.sf.net/cgi-bin/index.cgi?jsonlab)
+%
 
 if(nargin==0)
     error('you must provide at least 1 input');
 end
+
 if(exist('zmat','file')==2 || exist('zmat','file')==3)
-    output=zmat(input,1,'zlib');
+    [varargout{1:nargout}]=zmat(varargin{1},1,'zlib');
     return;
+elseif(isoctavemesh)
+    error('You must install the ZMat toolbox (http://github.com/fangq/zmat) to use this function in Octave');
 end
+
 error(javachk('jvm'));
-if ischar(input), input = uint8(input); end
-if ~isa(input, 'int8') && ~isa(input, 'uint8')
-    error('Input must be either char, int8 or uint8.');
-end
+
+input=typecast(varargin{1}(:)','uint8');
 
 buffer = java.io.ByteArrayOutputStream();
 zlib = java.util.zip.DeflaterOutputStream(buffer);
 zlib.write(input, 0, numel(input));
 zlib.close();
-output = typecast(buffer.toByteArray(), 'uint8')';
 
-end
+varargout{1} = typecast(buffer.toByteArray(), 'uint8')';
 
+if(nargout>1)
+    varargout{2}=struct('type',class(varargin{1}),'size',size(varargin{1}),'method','gzip','status',0);
+end
-- 
GitLab