1
function y = base64decode(x)
b'%BASE64DECODE Perform base64 decoding on a string.'
b'% BASE64DECODE(STR) decodes the given base64 string STR.'
b'% Any character not part of the 65-character base64 subset set is silently'
b"% ignored. Characters occuring after a '=' padding character are never"
b"% STR doesn't have to be a string. The only requirement is that it is a"
b'% vector containing values in the range 0-255.'
b'% If the length of the string to decode (after ignoring non-base64 chars) is'
b'% not a multiple of 4, then a warning is generated.'
b'% This function is used to decode strings from the Base64 encoding specified'
b'% in RFC 2045 - MIME (Multipurpose Internet Mail Extensions). The Base64'
b'% encoding is designed to represent arbitrary sequences of octets in a form'
b'% that need not be humanly readable. A 65-character subset ([A-Za-z0-9+/=])'
b'% of US-ASCII is used, enabling 6 bits to be represented per printable'
b'% See also BASE64ENCODE.'
b'% Author: Peter J. Acklam'
b'% Time-stamp: 2004-09-20 08:20:50 +0200'
b'% E-mail: pjacklam@online.no'
b'% URL: http://home.online.no/~pjacklam'
2
1
% check number of input arguments
3
2
error(nargchk(1, 1, nargin));
4
3
% remove non-base64 chars
5
4
x = x ( ( 'A' <= x & x <= 'Z' ) ...
6
5
| ( 'a' <= x & x <= 'z' ) ...
7
6
| ( '0' <= x & x <= '9' ) ...
8
7
| ( x == '+' ) | ( x == '=' ) | ( x == '/' ) );
10
9
warning('Length of base64 data not a multiple of 4; padding input.');
12
11
k = find(x == '=');
16
15
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
16
% Now perform the following mapping
24
23
y = repmat(uint8(0), size(x));
25
24
i = 'A' <= x & x <= 'Z'; y(i) = - 'A' + x(i);
26
25
i = 'a' <= x & x <= 'z'; y(i) = 26 - 'a' + x(i);
27
26
i = '0' <= x & x <= '9'; y(i) = 52 - '0' + x(i);
28
27
i = x == '+'; y(i) = 62 - '+' + x(i);
29
28
i = x == '/'; y(i) = 63 - '/' + x(i);
31
30
nebytes = length(x); % number of encoded bytes
32
31
nchunks = ceil(nebytes/4); % number of chunks/groups
33
32
% add padding if necessary
35
34
x(end+1 : 4*nchunks) = 0;
37
36
x = reshape(uint8(x), 4, nchunks);
38
37
y = repmat(uint8(0), 3, nchunks); % for the decoded data
39
38
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
39
% Rearrange every 4 bytes into 3 bytes
42
41
% 00aaaaaa 00bbbbbb 00cccccc 00dddddd
46
45
% aaaaaabb bbbbcccc ccdddddd
47
46
y(1,:) = bitshift(x(1,:), 2); % 6 highest bits of y(1,:)
48
47
y(1,:) = bitor(y(1,:), bitshift(x(2,:), -4)); % 2 lowest bits of y(1,:)
49
48
y(2,:) = bitshift(x(2,:), 4); % 4 highest bits of y(2,:)
50
49
y(2,:) = bitor(y(2,:), bitshift(x(3,:), -2)); % 4 lowest bits of y(2,:)
51
50
y(3,:) = bitshift(x(3,:), 6); % 2 highest bits of y(3,:)
52
51
y(3,:) = bitor(y(3,:), x(4,:)); % 6 lowest bits of y(3,:)
54
53
switch rem(nebytes, 4)
60
59
% reshape to a row vector and make it a character array
61
60
y = char(reshape(y, 1, numel(y)));
b'\\ No newline at end of file'
61
function y = base64decode(x)
62
%BASE64DECODE Perform base64 decoding on a string.
64
% BASE64DECODE(STR) decodes the given base64 string STR.
66
% Any character not part of the 65-character base64 subset set is silently
67
% ignored. Characters occuring after a '=' padding character are never
70
% STR doesn't have to be a string. The only requirement is that it is a
71
% vector containing values in the range 0-255.
73
% If the length of the string to decode (after ignoring non-base64 chars) is
74
% not a multiple of 4, then a warning is generated.
76
% This function is used to decode strings from the Base64 encoding specified
77
% in RFC 2045 - MIME (Multipurpose Internet Mail Extensions). The Base64
78
% encoding is designed to represent arbitrary sequences of octets in a form
79
% that need not be humanly readable. A 65-character subset ([A-Za-z0-9+/=])
80
% of US-ASCII is used, enabling 6 bits to be represented per printable
83
% See also BASE64ENCODE.
85
% Author: Peter J. Acklam
86
% Time-stamp: 2004-09-20 08:20:50 +0200
87
% E-mail: pjacklam@online.no
88
% URL: http://home.online.no/~pjacklam
90
% check number of input arguments
91
error(nargchk(1, 1, nargin));
93
% remove non-base64 chars
94
x = x ( ( 'A' <= x & x <= 'Z' ) ...
95
| ( 'a' <= x & x <= 'z' ) ...
96
| ( '0' <= x & x <= '9' ) ...
97
| ( x == '+' ) | ( x == '=' ) | ( x == '/' ) );
100
warning('Length of base64 data not a multiple of 4; padding input.');
108
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109
% Now perform the following mapping
117
y = repmat(uint8(0), size(x));
118
i = 'A' <= x & x <= 'Z'; y(i) = - 'A' + x(i);
119
i = 'a' <= x & x <= 'z'; y(i) = 26 - 'a' + x(i);
120
i = '0' <= x & x <= '9'; y(i) = 52 - '0' + x(i);
121
i = x == '+'; y(i) = 62 - '+' + x(i);
122
i = x == '/'; y(i) = 63 - '/' + x(i);
125
nebytes = length(x); % number of encoded bytes
126
nchunks = ceil(nebytes/4); % number of chunks/groups
128
% add padding if necessary
130
x(end+1 : 4*nchunks) = 0;
133
x = reshape(uint8(x), 4, nchunks);
134
y = repmat(uint8(0), 3, nchunks); % for the decoded data
136
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137
% Rearrange every 4 bytes into 3 bytes
139
% 00aaaaaa 00bbbbbb 00cccccc 00dddddd
143
% aaaaaabb bbbbcccc ccdddddd
145
y(1,:) = bitshift(x(1,:), 2); % 6 highest bits of y(1,:)
146
y(1,:) = bitor(y(1,:), bitshift(x(2,:), -4)); % 2 lowest bits of y(1,:)
148
y(2,:) = bitshift(x(2,:), 4); % 4 highest bits of y(2,:)
149
y(2,:) = bitor(y(2,:), bitshift(x(3,:), -2)); % 4 lowest bits of y(2,:)
151
y(3,:) = bitshift(x(3,:), 6); % 2 highest bits of y(3,:)
152
y(3,:) = bitor(y(3,:), x(4,:)); % 6 lowest bits of y(3,:)
155
switch rem(nebytes, 4)
162
% reshape to a row vector and make it a character array
163
y = char(reshape(y, 1, numel(y)));