~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychHardware/iViewXToolbox/cbase64/base64decode.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function y = base64decode(x)
 
 
b'%BASE64DECODE Perform base64 decoding on a string.'
 
 
b'%'
 
 
b'%   BASE64DECODE(STR) decodes the given base64 string STR.'
 
 
b'%'
 
 
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'%   decoded.'
 
 
b'%'
 
 
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'%'
 
 
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'%'
 
 
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'%   character.'
 
 
b'%'
 
 
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 == '/' ) );
9
8
  if rem(length(x), 4)
10
9
     warning('Length of base64 data not a multiple of 4; padding input.');
11
10
  end
12
11
  k = find(x == '=');
13
12
  if ~isempty(k)
14
13
     x = x(1:k(1)-1);
15
14
  end
16
15
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
17
16
  % Now perform the following mapping
18
17
  %
19
18
  %   A-Z  ->  0  - 25
20
19
  %   a-z  ->  26 - 51
21
20
  %   0-9  ->  52 - 61
22
21
  %   +    ->  62
23
22
  %   /    ->  63
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);
30
29
  x = y;
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
34
33
  if rem(nebytes, 4)
35
34
     x(end+1 : 4*nchunks) = 0;
36
35
  end
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
41
40
  %
42
41
  %    00aaaaaa 00bbbbbb 00cccccc 00dddddd
43
42
  %
44
43
  % to form
45
44
  %
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,:)
53
52
  % remove padding
54
53
  switch rem(nebytes, 4)
55
54
     case 2
56
55
        y = y(1:end-2);
57
56
     case 3
58
57
        y = y(1:end-1);
59
58
  end
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.
 
63
%
 
64
%   BASE64DECODE(STR) decodes the given base64 string STR.
 
65
%
 
66
%   Any character not part of the 65-character base64 subset set is silently
 
67
%   ignored.  Characters occuring after a '=' padding character are never
 
68
%   decoded.
 
69
%
 
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.
 
72
%
 
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.
 
75
%
 
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
 
81
%   character.
 
82
%
 
83
%   See also BASE64ENCODE.
 
84
 
 
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
 
89
 
 
90
   % check number of input arguments
 
91
   error(nargchk(1, 1, nargin));
 
92
 
 
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 == '/' ) );
 
98
 
 
99
   if rem(length(x), 4)
 
100
      warning('Length of base64 data not a multiple of 4; padding input.');
 
101
   end
 
102
 
 
103
   k = find(x == '=');
 
104
   if ~isempty(k)
 
105
      x = x(1:k(1)-1);
 
106
   end
 
107
 
 
108
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
109
   % Now perform the following mapping
 
110
   %
 
111
   %   A-Z  ->  0  - 25
 
112
   %   a-z  ->  26 - 51
 
113
   %   0-9  ->  52 - 61
 
114
   %   +    ->  62
 
115
   %   /    ->  63
 
116
 
 
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);
 
123
   x = y;
 
124
 
 
125
   nebytes = length(x);         % number of encoded bytes
 
126
   nchunks = ceil(nebytes/4);   % number of chunks/groups
 
127
 
 
128
   % add padding if necessary
 
129
   if rem(nebytes, 4)
 
130
      x(end+1 : 4*nchunks) = 0;
 
131
   end
 
132
 
 
133
   x = reshape(uint8(x), 4, nchunks);
 
134
   y = repmat(uint8(0), 3, nchunks);            % for the decoded data
 
135
 
 
136
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
137
   % Rearrange every 4 bytes into 3 bytes
 
138
   %
 
139
   %    00aaaaaa 00bbbbbb 00cccccc 00dddddd
 
140
   %
 
141
   % to form
 
142
   %
 
143
   %    aaaaaabb bbbbcccc ccdddddd
 
144
 
 
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,:)
 
147
 
 
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,:)
 
150
 
 
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,:)
 
153
 
 
154
   % remove padding
 
155
   switch rem(nebytes, 4)
 
156
      case 2
 
157
         y = y(1:end-2);
 
158
      case 3
 
159
         y = y(1:end-1);
 
160
   end
 
161
 
 
162
   % reshape to a row vector and make it a character array
 
163
   y = char(reshape(y, 1, numel(y)));