File: base64decode.m

package info (click to toggle)
octave-strings 1.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 452 kB
  • ctags: 29
  • sloc: makefile: 79; cpp: 67; sh: 25
file content (180 lines) | stat: -rw-r--r-- 4,309 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
## 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 2 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/>.
##
## This code was adapted from, Paul Kienzle <pkienzle@users.sf.net>
##

## -*- 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
## @end deftypefn
## @seealso {base64encode}
##

##
## Y = base64decode(X)
##
## Convert X into string of printable characters according to RFC 2045.
## The input may be a string or a matrix of integers in the range 0..255.
##
## See: http://www.ietf.org/rfc/rfc2045.txt
##
function z = base64decode(X,as_string)
  if (nargin < 1 )
    usage("Y = base64decode(X); X need to be a 4-row N-col matrix");
  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 Xa matrix.
  ##
  for idx_r = 1:SRows
	for idx_c=1:SCols
		v=Xa(idx_r,idx_c);
		w=-1;
		if (v >= 'A' && v <= 'Z')
			w=v-'A';
		elseif (v >= 'a' && v<='z')
			w=v-'a'+26;
		elseif (v >= '0' && v<= '9')
			w=v-'0'+52;
		elseif (v == '+')
                        w=62;
                elseif (v == '/')
                        w=63;
                elseif (v == '=')
                        w=0;%ignore me
		end
		Xa(idx_r,idx_c)=w;
	end
  end

  Y=Xa;
  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

  return

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')
%!