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