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