From e3303c55272f11b026d0def177d75b70b7a33b18 Mon Sep 17 00:00:00 2001 From: Qianqian Fang Date: Sat, 10 Jun 2023 18:01:15 -0400 Subject: [PATCH] base64 no longer add new line by default, use level=3 to restore --- src/zmat.cpp | 2 +- src/zmatlib.c | 4 ++-- test/run_zmat_test.m | 41 +++++++++++++++++++++-------------------- zmat.m | 2 +- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/zmat.cpp b/src/zmat.cpp index 46d0a86..12691da 100644 --- a/src/zmat.cpp +++ b/src/zmat.cpp @@ -300,5 +300,5 @@ void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[]) { */ void zmat_usage() { - printf("ZMat (v0.9.9)\nUsage:\n\t[output,info]=zmat(input,iscompress,method);\n\nPlease run 'help zmat' for more details.\n"); + mexPrintf("ZMat (v0.9.9)\nUsage:\n\t[output,info]=zmat(input,iscompress,method);\n\nPlease run 'help zmat' for more details.\n"); } diff --git a/src/zmatlib.c b/src/zmatlib.c index 75e98d6..fa6a394 100644 --- a/src/zmatlib.c +++ b/src/zmatlib.c @@ -728,7 +728,7 @@ unsigned char* base64_encode(const unsigned char* src, size_t len, in += 3; line_len += 4; - if (mode < 3 && line_len >= 72) { + if (mode > 1 && line_len >= 72) { *pos++ = '\n'; line_len = 0; } @@ -750,7 +750,7 @@ unsigned char* base64_encode(const unsigned char* src, size_t len, line_len += 4; } - if (mode < 2 && line_len) { + if (mode > 2 && line_len) { *pos++ = '\n'; } diff --git a/test/run_zmat_test.m b/test/run_zmat_test.m index 790d55a..23d8b99 100644 --- a/test/run_zmat_test.m +++ b/test/run_zmat_test.m @@ -61,7 +61,7 @@ function run_zmat_test(tests) test_zmat('blosc2zlib (scalar)', 'base64', zmat(2.2,1,'blosc2zlib'), 'BQEHCAgAAAAIAAAAKAAAAAAAAAAAAQQAAAAAAAAAAACamZmZmZkBQA==', 'level', 2); end test_zmat('blosc2zstd (scalar)', 'base64', zmat(logical(0.1),1,'blosc2zstd'), 'BQEHAQEAAAABAAAAIQAAAAAAAAAAAAUAAAAAAAAAAAAB', 'level', 2); - test_zmat('base64 (scalar)', 'base64', uint8(100), [90 65 61 61 10]); + test_zmat('base64 (scalar)', 'base64', uint8(100), [90 65 61 61]); if(isminiz) test_zmat('zlib (array)', 'zlib', uint8([1,2,3]), [120 1 1 3 0 252 255 1 2 3 0 13 0 7]); @@ -74,15 +74,15 @@ function run_zmat_test(tests) test_zmat('lzip (array)', 'lzip', uint8(reshape(1:(2*3*4), [3,2,4])), [76 90 73 80 0 20 0 0 128 157 97 211 13 93 174 25 62 219 132 40 29 52 41 93 234 35 61 128 60 72 152 87 41 88 255 253 203 224 0 163 16 142 146 24 0 0 0 0 0 0 0]); test_zmat('lz4 (array)', 'lz4', [1], [128 0 0 0 0 0 0 240 63]); test_zmat('lz4hc (array)', 'lz4hc', 'test zmat', [144 116 101 115 116 32 122 109 97 116]); - test_zmat('zstd (array)', 'base64', zmat(uint8(magic(5)),1,'zstd'), 'KLUv/SAZyQAAERcECgsYBQYMEgEHDRMZCA4UFQIPEBYDCQ==', 'level', 3); - test_zmat('blosc2blosclz (array)', 'base64', zmat(uint8(magic(4)),1,'blosc2blosclz'), 'BQEHARAAAAAQAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAQBQkEAgsHDgMKBg8NCAwB', 'level', 3); - test_zmat('blosc2lz4 (array)', 'base64', zmat(uint16(magic(3)),1,'blosc2lz4'), 'BQEHAhIAAAASAAAAMgAAAAAAAAAAAQEAAAAAAAAAAAAIAAMABAABAAUACQAGAAcAAgA=', 'level', 3); - test_zmat('blosc2lz4hc (array)', 'base64', zmat([1.1,2.1,3.1],1,'blosc2lz4hc'), 'BQEHCBgAAAAYAAAAOAAAAAAAAAAAAQIAAAAAAAAAAACamZmZmZnxP83MzMzMzABAzczMzMzMCEA=', 'level', 3); + test_zmat('zstd (array)', 'base64', zmat(uint8(magic(5)),1,'zstd'), 'KLUv/SAZyQAAERcECgsYBQYMEgEHDRMZCA4UFQIPEBYDCQ=='); + test_zmat('blosc2blosclz (array)', 'base64', zmat(uint8(magic(4)),1,'blosc2blosclz'), 'BQEHARAAAAAQAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAQBQkEAgsHDgMKBg8NCAwB'); + test_zmat('blosc2lz4 (array)', 'base64', zmat(uint16(magic(3)),1,'blosc2lz4'), 'BQEHAhIAAAASAAAAMgAAAAAAAAAAAQEAAAAAAAAAAAAIAAMABAABAAUACQAGAAcAAgA='); + test_zmat('blosc2lz4hc (array)', 'base64', zmat([1.1,2.1,3.1],1,'blosc2lz4hc'), 'BQEHCBgAAAAYAAAAOAAAAAAAAAAAAQIAAAAAAAAAAACamZmZmZnxP83MzMzMzABAzczMzMzMCEA='); if(~isminiz) - test_zmat('blosc2zlib (array)', 'base64', zmat(uint8(reshape(1:(2*3*4), [3,2,4])),1,'blosc2zlib'), 'BQEHARgAAAAYAAAAOAAAAAAAAAAAAAQAAAAAAAAAAAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxg=', 'level', 3); + test_zmat('blosc2zlib (array)', 'base64', zmat(uint8(reshape(1:(2*3*4), [3,2,4])),1,'blosc2zlib'), 'BQEHARgAAAAYAAAAOAAAAAAAAAAAAAQAAAAAAAAAAAABAgMEBQYHCAkKCwwNDg8QERITFBUWFxg='); end - test_zmat('blosc2zstd (array)', 'base64', zmat(uint8(ones(2,3,4)),1,'blosc2zstd'), 'BQEHARgAAAAYAAAAOAAAAAAAAAAAAAUAAAAAAAAAAAABAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=', 'level', 3); - test_zmat('base64 (array)', 'base64', ['test';'zmat'], [100 72 112 108 98 88 78 104 100 72 81 61 10]); + test_zmat('blosc2zstd (array)', 'base64', zmat(uint8(ones(2,3,4)),1,'blosc2zstd'), 'BQEHARgAAAAYAAAAOAAAAAAAAAAAAAUAAAAAAAAAAAABAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE='); + test_zmat('base64 (array)', 'base64', ['test';'zmat'], [100 72 112 108 98 88 78 104 100 72 81 61]); if(isminiz) test_zmat('zlib (level=9)', 'zlib', 55, [120 1 1 8 0 247 255 0 0 0 0 0 128 75 64 2 94 1 12], 'level', -9); @@ -97,18 +97,19 @@ function run_zmat_test(tests) test_zmat('lzip (level)', 'lzip', logical([1,2,3,4]), [76 90 73 80 0 20 0 0 232 190 92 247 255 255 224 0 128 0 153 211 38 246 4 0 0 0 0 0 0 0],'level', -9); test_zmat('lz4 (level)', 'lz4', 'random data', [176 114 97 110 100 111 109 32 100 97 116 97],'level', -9); test_zmat('lz4hc (level)', 'lz4hc', 1.2, [128 51 51 51 51 51 51 243 63],'level', -9); - test_zmat('zstd (level=1)', 'base64', zmat(eye(10),-1,'zstd'), 'KLUv/WAgAp0AAEgAAAAAAADwPwACAL+2UAGZwBE=', 'level', 3); - test_zmat('zstd (level=3)', 'base64', zmat(eye(10),-3,'zstd'), 'KLUv/WAgAo0AACgAAPA/AAMAv7ZQAQEzLIAF', 'level', 3); - test_zmat('zstd (level=9)', 'base64', zmat(eye(10),-9,'zstd'), 'KLUv/WAgAo0AACgAAPA/AAMAv7ZQAQEzLIAF', 'level', 3); - test_zmat('zstd (level=19)', 'base64', zmat(eye(10),-19,'zstd'), 'KLUv/WAgAoUAACAA8D8AAwDFtlABBmgEcAU=', 'level', 3); - test_zmat('blosc2blosclz (typesize=2)', 'base64', zmat(uint32(magic(4)),1,'blosc2blosclz','typesize',2), 'BQEFAkAAAABAAAAATAAAAAAAAAAAAQAAAAAAAAAAAAAkAAAAIAAAABAABQAJAAQAAgALAAcADgADAAoABgAPAA0ACAAMAAEAAAAAAA==', 'level', 3); - test_zmat('blosc2blosclz (typesize=4)', 'base64', zmat(uint32(magic(4)),1,'blosc2blosclz','typesize',4), 'BQEXBEAAAABAAAAAYAAAAAAAAAAAAQAAAAAAAAAAAAAQAAAABQAAAAkAAAAEAAAAAgAAAAsAAAAHAAAADgAAAAMAAAAKAAAABgAAAA8AAAANAAAACAAAAAwAAAABAAAA', 'level', 3); - test_zmat('blosc2blosclz (typesize=8)', 'base64', zmat(uint32(magic(4)),1,'blosc2blosclz','typesize',8), 'BQEXCEAAAABAAAAAYAAAAAAAAAAAAQAAAAAAAAAAAAAQAAAABQAAAAkAAAAEAAAAAgAAAAsAAAAHAAAADgAAAAMAAAAKAAAABgAAAA8AAAANAAAACAAAAAwAAAABAAAA', 'level', 3); - test_zmat('blosc2zstd (typesize=2)', 'base64', zmat(single(magic(4)),1,'blosc2zstd','typesize',2), 'BQGXAkAAAABAAAAAYAAAAAAAAAAAAQUAAAAAAAAAAAAAAIBBAACgQAAAEEEAAIBAAAAAQAAAMEEAAOBAAABgQQAAQEAAACBBAADAQAAAcEEAAFBBAAAAQQAAQEEAAIA/', 'level', 3); - test_zmat('blosc2zstd (typesize=4)', 'base64', zmat(single(magic(4)),1,'blosc2zstd','typesize',4), 'BQGVBEAAAABAAAAAVQAAAAAAAAAAAQUAAAAAAAAAAAAkAAAALQAAACi1L/0gQCUBAOAAAICgEIAAMOBgQCDAcFAAQIBBQEFAQEFBQUE/AgByQxMBFg==', 'level', 3); - test_zmat('blosc2zstd (typesize=8)', 'base64', zmat(single(magic(4)),1,'blosc2zstd','typesize',8), 'BQGXCEAAAABAAAAAYAAAAAAAAAAAAQUAAAAAAAAAAAAAAIBBAACgQAAAEEEAAIBAAAAAQAAAMEEAAOBAAABgQQAAQEAAACBBAADAQAAAcEEAAFBBAAAAQQAAQEEAAIA/', 'level', 3); - test_zmat('base64 (trailing newline)', 'base64', uint8(100), 'ZA==', 'level', 2); - test_zmat('base64 (all newline)', 'base64', ones(7,1), 'AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8=', 'level', 3); + test_zmat('zstd (level=1)', 'base64', zmat(eye(10),-1,'zstd'), 'KLUv/WAgAp0AAEgAAAAAAADwPwACAL+2UAGZwBE='); + test_zmat('zstd (level=3)', 'base64', zmat(eye(10),-3,'zstd'), 'KLUv/WAgAo0AACgAAPA/AAMAv7ZQAQEzLIAF'); + test_zmat('zstd (level=9)', 'base64', zmat(eye(10),-9,'zstd'), 'KLUv/WAgAo0AACgAAPA/AAMAv7ZQAQEzLIAF'); + test_zmat('zstd (level=19)', 'base64', zmat(eye(10),-19,'zstd'), 'KLUv/WAgAoUAACAA8D8AAwDFtlABBmgEcAU='); + test_zmat('blosc2blosclz (typesize=2)', 'base64', zmat(uint32(magic(4)),1,'blosc2blosclz','typesize',2), 'BQEFAkAAAABAAAAATAAAAAAAAAAAAQAAAAAAAAAAAAAkAAAAIAAAABAABQAJAAQAAgALAAcADgADAAoABgAPAA0ACAAMAAEAAAAAAA=='); + test_zmat('blosc2blosclz (typesize=4)', 'base64', zmat(uint32(magic(4)),1,'blosc2blosclz','typesize',4), 'BQEXBEAAAABAAAAAYAAAAAAAAAAAAQAAAAAAAAAAAAAQAAAABQAAAAkAAAAEAAAAAgAAAAsAAAAHAAAADgAAAAMAAAAKAAAABgAAAA8AAAANAAAACAAAAAwAAAABAAAA'); + test_zmat('blosc2blosclz (typesize=8)', 'base64', zmat(uint32(magic(4)),1,'blosc2blosclz','typesize',8), 'BQEXCEAAAABAAAAAYAAAAAAAAAAAAQAAAAAAAAAAAAAQAAAABQAAAAkAAAAEAAAAAgAAAAsAAAAHAAAADgAAAAMAAAAKAAAABgAAAA8AAAANAAAACAAAAAwAAAABAAAA'); + test_zmat('blosc2zstd (typesize=2)', 'base64', zmat(single(magic(4)),1,'blosc2zstd','typesize',2), 'BQGXAkAAAABAAAAAYAAAAAAAAAAAAQUAAAAAAAAAAAAAAIBBAACgQAAAEEEAAIBAAAAAQAAAMEEAAOBAAABgQQAAQEAAACBBAADAQAAAcEEAAFBBAAAAQQAAQEEAAIA/'); + test_zmat('blosc2zstd (typesize=4)', 'base64', zmat(single(magic(4)),1,'blosc2zstd','typesize',4), 'BQGVBEAAAABAAAAAVQAAAAAAAAAAAQUAAAAAAAAAAAAkAAAALQAAACi1L/0gQCUBAOAAAICgEIAAMOBgQCDAcFAAQIBBQEFAQEFBQUE/AgByQxMBFg=='); + test_zmat('blosc2zstd (typesize=8)', 'base64', zmat(single(magic(4)),1,'blosc2zstd','typesize',8), 'BQGXCEAAAABAAAAAYAAAAAAAAAAAAQUAAAAAAAAAAAAAAIBBAACgQAAAEEEAAIBAAAAAQAAAMEEAAOBAAABgQQAAQEAAACBBAADAQAAAcEEAAFBBAAAAQQAAQEEAAIA/'); + test_zmat('base64 (no newline)', 'base64', uint8(100), sprintf('ZA==\n'), 'level', 3); + test_zmat('base64 (trailing newline)', 'base64', ones(7,1), sprintf('AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA\n8D8='), 'level', 2); + test_zmat('base64 (all newline)', 'base64', ones(7,1), sprintf('AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA8D8AAAAAAADwPwAAAAAAAPA/AAAAAAAA\n8D8=\n'), 'level', 3); end %% if (ismember('d', tests)) diff --git a/zmat.m b/zmat.m index b911911..ed2266e 100644 --- a/zmat.m +++ b/zmat.m @@ -22,7 +22,7 @@ % the default compression level is used if iscompress is set to 1. % % zmat removes the trailing newline when iscompress=2 and method='base64' -% all newlines are removed when iscompress=3 and method='base64' +% all newlines are kept when iscompress=3 and method='base64' % % if one defines iscompress as the info struct (2nd output of zmat), zmat % will perform a decoding/decompression operation and recover the original