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