Skip to content

Commit 7279fd0

Browse files
committed
[feat] add built-in base64decode support for octave
1 parent e79454f commit 7279fd0

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

base64decode.m

+36-7
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,45 @@
3333
if (exist('zmat', 'file') == 2 || exist('zmat', 'file') == 3)
3434
output = zmat(varargin{1}, 0, 'base64');
3535
return
36-
elseif (isoctavemesh)
37-
which('matlab.net.base64decode');
38-
if (~isempty(which('matlab.net.base64decode')))
39-
output = matlab.net.base64decode(varargin{1});
40-
else
41-
error('You must install the ZMat toolbox (http://github.com/NeuroJSON/zmat) to use this function in Octave');
36+
end
37+
38+
jvmerr = javachk('jvm');
39+
40+
if (isoctavemesh || isempty(jvmerr))
41+
map = uint8(zeros(1, 256) + 65);
42+
map(uint8(['A':'Z', 'a':'z', '0':'9', '+/='])) = 0:64;
43+
map(uint8('-_')) = 62:63;
44+
x = map(varargin{1}(:));
45+
46+
x(x > 64) = []; % remove non-base64 chars
47+
x(x == 64) = []; % remove padding characters
48+
49+
nebytes = length(x);
50+
nchunks = ceil(nebytes / 4);
51+
if rem(nebytes, 4) > 0
52+
x(end + 1:4 * nchunks) = 0;
4253
end
54+
x = reshape(uint8(x), 4, nchunks);
55+
output = repmat(uint8(0), 3, nchunks);
56+
57+
output(1, :) = bitshift(x(1, :), 2);
58+
output(1, :) = bitor(output(1, :), bitshift(x(2, :), -4));
59+
output(2, :) = bitshift(x(2, :), 4);
60+
output(2, :) = bitor(output(2, :), bitshift(x(3, :), -2));
61+
output(3, :) = bitshift(x(3, :), 6);
62+
output(3, :) = bitor(output(3, :), x(4, :));
63+
64+
switch rem(nebytes, 4)
65+
case 2
66+
output = output(1:end - 2);
67+
case 3
68+
output = output(1:end - 1);
69+
end
70+
output = output(:)';
71+
return
4372
end
4473

45-
error(javachk('jvm'));
74+
error(jvmerr);
4675

4776
if (ischar(varargin{1}))
4877
varargin{1} = uint8(varargin{1});

0 commit comments

Comments
 (0)