/usr/share/octave/packages/strings-1.1.0/base64decode.m is in octave-strings 1.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | ## Copyright (C) 2007 Muthiah Annamalai <muthiah.annamalai@uta.edu>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
## -*- texinfo -*-
## @deftypefn {Function File} {@var{rval} =} base64decode (@var{code})
## @deftypefnx {Function File} {@var{rval} =} base64decode (@var{code}, @var{as_string})
## Convert a base64 @var{code} (a string of printable characters according to RFC 2045)
## into the original ASCII data set of range 0-255. If option @var{as_string} is
## passed, the return value is converted into a string.
##
## @example
## @group
## ##base64decode(base64encode('Hakuna Matata'),true)
## base64decode('SGFrdW5hIE1hdGF0YQ==',true)
## ##returns 'Hakuna Matata'
## @end group
## @end example
##
## See: http://www.ietf.org/rfc/rfc2045.txt
##
## @seealso {base64encode}
## @end deftypefn
function z = base64decode (X, as_string)
if (nargin < 1 )
print_usage;
elseif nargin == 1
as_string=false;
endif
if ( any(X(:) < 0) || any(X(:) > 255))
error("base64decode is expecting integers in the range 0 .. 255");
endif
## decompose strings into the 4xN matrices
## formatting issues.
if( rows(X) == 1 )
Y=[];
L=length(X);
for z=4:4:L
Y=[Y X(z-3:z)']; #keep adding columns
end
if min(size(Y))==1
Y=reshape(Y,[L, 1]);
else
Y=reshape(Y,[4,L/4]);
end
X=Y;
Y=[];
end
X = toascii(X);
Xa= X;
## Work backwards. Starting at step in table,
## lookup the index of the element in the table.
## 6-bit encoding table, plus 1 for padding
## 26*2 + 10 + 2 + 1 = 64 + 1, '=' is EOF stop mark.
table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
S=size(X);
SRows=S(1);
SCols=S(2);
Y=zeros(S);
## decode the incoming matrix &
## write the values into Va matrix.
Va = -1*ones(size(Xa));
iAZ = (Xa >= 'A').*(Xa <= 'Z') > 0;
Va(iAZ)=Xa(iAZ)-'A';
iaz = (Xa >= 'a').*(Xa <= 'z') > 0;
Va(iaz)=Xa(iaz)-'a'+26;
i09 = (Xa >= '0').*(Xa <= '9') > 0;
Va(i09)=Xa(i09)-'0'+52;
is = (Xa == '/') ; Va(is) = 63;
ip = (Xa == '+') ; Va(ip) = 62;
ieq = (Xa == '=') ; Va(ieq) = 0;
clear is; clear ieq; clear ip; clear i09;
clear iaz; clear iAZ; clear Xa; clear X;
Y=Va; clear Va;
Y1=Y(1,:);
if (SRows > 1)
Y2=Y(2,:);
else
Y2=zeros(1,SCols);
end;
if (SRows > 2)
Y3=Y(3,:);
else
Y3=zeros(1,SCols);
end;
if (SRows > 3)
Y4=Y(4,:);
else
Y4=zeros(1,SCols);
end;
## +1 not required due to ASCII subtraction
## actual decoding work
b1 = Y1*4 + fix(Y2/16);
b2 = mod(Y2,16)*16+fix(Y3/4);
b3 = mod(Y3,4)*64 + Y4;
ZEROS=sum(sum(Y==0));
L=length(b1)*3;
z=zeros(1,L);
z(1:3:end)=b1;
if (SRows > 1)
z(2:3:end)=b2;
else
z(2:3:end)=[];
end;
if (SRows > 2)
z(3:3:end)=b3;
else
z(3:3:end)=[];
end
## FIXME
## is this expected behaviour?
if ( as_string )
L=length(z);
while ( ( L > 0) && ( z(L)==0 ) )
L=L-1;
end
z=char(z(1:L));
end
endfunction
%!assert(base64decode(base64encode('Hakuna Matata'),true),'Hakuna Matata')
%!assert(base64decode(base64encode([1:255])),[1:255])
%!assert(base64decode(base64encode('taken'),true),'taken')
%!assert(base64decode(base64encode('sax'),true),'sax')
%!assert(base64decode(base64encode('H'),true),'H')
%!assert(base64decode(base64encode('Ta'),true),'Ta')
|