1 karl 1.1 //%/////////////////////////////////////////////////////////////////////////////
2 //
|
3 kumpf 1.10 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Company, IBM,
|
4 karl 1.1 // The Open Group, Tivoli Systems
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 kumpf 1.10 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10 karl 1.1 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
|
13 kumpf 1.10 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
|
14 karl 1.1 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
15 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
16 kumpf 1.10 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
19 karl 1.1 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 //
22 //==============================================================================
23 //
24 // Author: Karl Schopmeyer (k.schopmeyer@opengroup.org)
25 //
26 // Modified By:
27 //
28 //%/////////////////////////////////////////////////////////////////////////////
29
30 #include <strstream>
|
31 kumpf 1.7 #include "Base64.h"
|
32 kumpf 1.12 #include <Pegasus/Common/ArrayInternal.h>
|
33 karl 1.1
34 PEGASUS_NAMESPACE_BEGIN
35 PEGASUS_USING_STD;
36
|
37 sage 1.5 #ifdef PEGASUS_PLATFORM_AIX_RS_IBMCXX
38 #define inline
39 #endif
40
|
41 keith.petley 1.16
42 //**********************************************************
43 /* Encode thanslates one six-bit pattern into a base-64 character.
44 Unsigned char is used to represent a six-bit stream of date.
45
46 */
47 inline PEGASUS_COMMON_LINKAGE char Base64::_Encode(Uint8 uc)
48 {
49 if (uc < 26)
50 return 'A'+uc;
51
52 if (uc < 52)
53 return 'a'+(uc-26);
54
55 if (uc < 62)
56 return '0'+(uc-52);
57
58 if (uc == 62)
59 return '+';
60
61 return '/';
62 keith.petley 1.16 };
63
64 //Helper function returns true is a character is a valid base-64 character and false otherwise.
65
66 inline Boolean Base64::_IsBase64(char c)
67 {
68
69 if (c >= 'A' && c <= 'Z')
70 return true;
71
72 if (c >= 'a' && c <= 'z')
73 return true;
74
75 if (c >= '0' && c <= '9')
76 return true;
77
78 if (c == '+')
79 return true;
80
81 if (c == '/')
82 return true;
83 keith.petley 1.16
84 if (c == '=')
85 return true;
86
87 return false;
88 };
89
90 // Translate one base-64 character into a six bit pattern
91 inline Uint8 Base64::_Decode(char c)
92 {
93 if (c >= 'A' && c <= 'Z')
94 return c - 'A';
95 if (c >= 'a' && c <= 'z')
96 return c - 'a' + 26;
97
98 if (c >= '0' && c <= '9')
99 return c - '0' + 52;
100
101 if (c == '+')
102 return 62;
103
104 keith.petley 1.16 return 63;
105 };
106
107
|
108 karl 1.1 //*************************************************************
109 /* Encode static method takes an array of 8-bit values and
110 returns a base-64 stream.
|
111 karl 1.2 ATTN: KS feb 2002 - This is probably a very slow an inefficient
112 implementation and could be improved if it is required for
113 production. Today it is only for test programs.
|
114 karl 1.1 */
|
115 kumpf 1.4 Array<Sint8> Base64::encode(const Array<Uint8>& vby)
|
116 karl 1.1 {
|
117 karl 1.2 Array<Sint8> retArray;
|
118 karl 1.1 // If nothing in input string, return empty string
119 if (vby.size() == 0)
|
120 karl 1.2 return retArray;
|
121 karl 1.1 // for every character in the input array taken 3 bytes at a time
122 for (Uint32 i=0; i < vby.size(); i+=3)
123 {
124
125 // Create from 3 8 bit values to 4 6 bit values
126 Uint8 by1=0,by2=0,by3=0;
127 by1 = vby[i];
128 if (i+1<vby.size())
129 {
130 by2 = vby[i+1];
131 };
132 if (i+2<vby.size())
133 {
134 by3 = vby[i+2];
135 }
136
137 Uint8 by4=0,by5=0,by6=0,by7=0;
138 by4 = by1>>2;
139 by5 = ((by1&0x3)<<4)|(by2>>4);
140 by6 = ((by2&0xf)<<2)|(by3>>6);
141 by7 = by3&0x3f;
142 karl 1.1
|
143 karl 1.2 retArray.append(_Encode(by4));
144 retArray.append(_Encode(by5));
|
145 karl 1.1
146 if (i+1<vby.size())
|
147 karl 1.2 retArray.append( _Encode(by6));
|
148 karl 1.1 else
|
149 gerarda 1.13 retArray.append('=');
150
|
151 karl 1.1
152 if (i+2<vby.size())
|
153 karl 1.2 retArray.append( _Encode(by7));
|
154 karl 1.1 else
|
155 karl 1.2 retArray.append('=');
|
156 karl 1.1
|
157 kumpf 1.6 /* ATTN: Need to fix this. It adds unwanted cr-lf after 4 chars.
158
|
159 karl 1.1 if (i % (76/4*3) == 0)
160 {
|
161 karl 1.2 retArray.append( '\r');
162 retArray.append( '\n');
|
163 karl 1.1 }
|
164 kumpf 1.6 */
|
165 karl 1.1 };
166
|
167 karl 1.2 return retArray;
|
168 karl 1.1 };
169 /*I checked for the zero length. The algorithm would also work for zero length input stream, but I’m pretty adamant about handling border conditions. They are often the culprits of run-time production failures.
170 The algorithm goes thru each three bytes of data at a time. The first thing I do is to shift the bits around from three 8-bit values to four 6-bit values. Then I encode the 6-bit values and add then one at a time to the output stream. This is actually quite inefficient. The STL character array is being allocated one byte at a time. The algorithm would be much faster, if I pre-allocated that array. I’ll leave that as an optimization practical exercise for the reader.
|
171 kumpf 1.9 */
|
172 karl 1.1
173 /* The decode static method takes a base-64 stream and converts it
174 to an array of 8-bit values.
175 */
|
176 kumpf 1.4 Array<Uint8> Base64::decode(const Array<Sint8> strInput)
|
177 karl 1.1 {
178 //Strip any non-base64 characters from the input
|
179 kumpf 1.15 Array<Sint8> str;
|
180 karl 1.1 for (Uint32 j=0;j<strInput.size();j++)
181 {
182 if (_IsBase64(strInput[j]))
|
183 kumpf 1.15 str.append(strInput[j]);
|
184 karl 1.1 }
|
185 karl 1.2
186 Array<Uint8> retArray;
|
187 karl 1.1
188 // Return if the input is zero length
|
189 kumpf 1.15 if (str.size() == 0)
|
190 karl 1.2 return retArray;
|
191 karl 1.1
192 // comment
|
193 kumpf 1.15 for (Uint32 i=0; i < str.size(); i+=4)
|
194 karl 1.1 {
195 char c1='A',c2='A',c3='A',c4='A';
196
197 c1 = str[i];
|
198 kumpf 1.15 if (i+1<str.size())
|
199 karl 1.1 c2 = str[i+1];
|
200 kumpf 1.15 if (i+2<str.size())
|
201 karl 1.1 c3 = str[i+2];
|
202 kumpf 1.15 if (i+3<str.size())
|
203 karl 1.1 c4 = str[i+3];
204
205 Uint8 by1=0,by2=0,by3=0,by4=0;
206 by1 = _Decode(c1);
207 by2 = _Decode(c2);
208 by3 = _Decode(c3);
209 by4 = _Decode(c4);
210 //cout << "base::64decode bytes" <<
211 // " 1 " << c1 << " " << by1 <<
212 // " 2 " << c2 << " " << by2 <<
213 // " 3 " << c3 << " " << by3 <<
214 // " 4 " << c4 << " " << by4 << endl;
215
216 // append first byte by shifting
|
217 karl 1.2 retArray.append( (by1<<2)|(by2>>4) );
|
218 karl 1.1
219 // append second byte if not padding
220 if (c3 != '=')
|
221 karl 1.2 retArray.append( ((by2&0xf)<<4)|(by3>>2) );
|
222 karl 1.1
223 if (c4 != '=')
|
224 karl 1.2 retArray.append( ((by3&0x3)<<6)|by4 );
|
225 karl 1.1 }
226
|
227 gerarda 1.13
|
228 karl 1.2 return retArray;
|
229 karl 1.1 };
230
231 PEGASUS_NAMESPACE_END
|