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 gerarda 1.13 #ifdef PEGASUS_OS_OS400
35 // OS400 is EBCDIC so it needs to convert string and characters to ASCII
36 // prior to decode and encode. The encoding and decoding are done in ASCII.
|
37 gerarda 1.14 #include <Pegasus/Common/OS400ConvertChar.h>
38 // This converts all string literal to ccsid 819
39 #pragma convert(819)
|
40 gerarda 1.13 #endif
41
|
42 karl 1.1 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 karl 1.1 //*************************************************************
50 /* Encode static method takes an array of 8-bit values and
51 returns a base-64 stream.
|
52 karl 1.2 ATTN: KS feb 2002 - This is probably a very slow an inefficient
53 implementation and could be improved if it is required for
54 production. Today it is only for test programs.
|
55 karl 1.1 */
|
56 kumpf 1.4 Array<Sint8> Base64::encode(const Array<Uint8>& vby)
|
57 karl 1.1 {
|
58 karl 1.2 Array<Sint8> retArray;
|
59 karl 1.1 // If nothing in input string, return empty string
60 if (vby.size() == 0)
|
61 karl 1.2 return retArray;
|
62 karl 1.1 // for every character in the input array taken 3 bytes at a time
63
|
64 gerarda 1.13 #ifdef PEGASUS_OS_OS400
65 //For OS400 convert from ebcdic to ascii
66 EtoA((char *)vby.getData(), vby.size());
67 #endif
68
|
69 karl 1.1 for (Uint32 i=0; i < vby.size(); i+=3)
70 {
71
72 // Create from 3 8 bit values to 4 6 bit values
73 Uint8 by1=0,by2=0,by3=0;
74 by1 = vby[i];
75 if (i+1<vby.size())
76 {
77 by2 = vby[i+1];
78 };
79 if (i+2<vby.size())
80 {
81 by3 = vby[i+2];
82 }
83
84 Uint8 by4=0,by5=0,by6=0,by7=0;
85 by4 = by1>>2;
86 by5 = ((by1&0x3)<<4)|(by2>>4);
87 by6 = ((by2&0xf)<<2)|(by3>>6);
88 by7 = by3&0x3f;
89
|
90 karl 1.2 retArray.append(_Encode(by4));
91 retArray.append(_Encode(by5));
|
92 karl 1.1
93 if (i+1<vby.size())
|
94 karl 1.2 retArray.append( _Encode(by6));
|
95 karl 1.1 else
|
96 gerarda 1.13 retArray.append('=');
97
|
98 karl 1.1
99 if (i+2<vby.size())
|
100 karl 1.2 retArray.append( _Encode(by7));
|
101 karl 1.1 else
|
102 karl 1.2 retArray.append('=');
|
103 karl 1.1
|
104 kumpf 1.6 /* ATTN: Need to fix this. It adds unwanted cr-lf after 4 chars.
105
|
106 karl 1.1 if (i % (76/4*3) == 0)
107 {
|
108 karl 1.2 retArray.append( '\r');
109 retArray.append( '\n');
|
110 karl 1.1 }
|
111 kumpf 1.6 */
|
112 karl 1.1 };
113
|
114 gerarda 1.13 #ifdef PEGASUS_OS_OS400
115 //For OS400 convert from ascii to ebcdic
116 AtoE((char *)retArray.getData(), retArray.size());
117 #endif
118
|
119 karl 1.2 return retArray;
|
120 karl 1.1 };
121 /*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.
122 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.
|
123 kumpf 1.9 */
|
124 karl 1.1
125 /* The decode static method takes a base-64 stream and converts it
126 to an array of 8-bit values.
127 */
|
128 kumpf 1.4 Array<Uint8> Base64::decode(const Array<Sint8> strInput)
|
129 karl 1.1 {
|
130 gerarda 1.13 #ifdef PEGASUS_OS_OS400
131 //For OS400 convert from ebcdic to ascii
132 EtoA((char *)strInput.getData(), strInput.size());
133 #endif
134
|
135 karl 1.1 //Strip any non-base64 characters from the input
|
136 kumpf 1.15 Array<Sint8> str;
|
137 karl 1.1 for (Uint32 j=0;j<strInput.size();j++)
138 {
139 if (_IsBase64(strInput[j]))
|
140 kumpf 1.15 str.append(strInput[j]);
|
141 karl 1.1 }
|
142 karl 1.2
143 Array<Uint8> retArray;
|
144 karl 1.1
145 // Return if the input is zero length
|
146 kumpf 1.15 if (str.size() == 0)
|
147 karl 1.2 return retArray;
|
148 karl 1.1
149 // comment
|
150 kumpf 1.15 for (Uint32 i=0; i < str.size(); i+=4)
|
151 karl 1.1 {
152 char c1='A',c2='A',c3='A',c4='A';
153
154 c1 = str[i];
|
155 kumpf 1.15 if (i+1<str.size())
|
156 karl 1.1 c2 = str[i+1];
|
157 kumpf 1.15 if (i+2<str.size())
|
158 karl 1.1 c3 = str[i+2];
|
159 kumpf 1.15 if (i+3<str.size())
|
160 karl 1.1 c4 = str[i+3];
161
162 Uint8 by1=0,by2=0,by3=0,by4=0;
163 by1 = _Decode(c1);
164 by2 = _Decode(c2);
165 by3 = _Decode(c3);
166 by4 = _Decode(c4);
167 //cout << "base::64decode bytes" <<
168 // " 1 " << c1 << " " << by1 <<
169 // " 2 " << c2 << " " << by2 <<
170 // " 3 " << c3 << " " << by3 <<
171 // " 4 " << c4 << " " << by4 << endl;
172
173 // append first byte by shifting
|
174 karl 1.2 retArray.append( (by1<<2)|(by2>>4) );
|
175 karl 1.1
176 // append second byte if not padding
177 if (c3 != '=')
|
178 karl 1.2 retArray.append( ((by2&0xf)<<4)|(by3>>2) );
|
179 karl 1.1
180 if (c4 != '=')
|
181 karl 1.2 retArray.append( ((by3&0x3)<<6)|by4 );
|
182 karl 1.1 }
183
|
184 gerarda 1.13 #ifdef PEGASUS_OS_OS400
185 //For OS400 convert from ascii to ebcdic
186 AtoE((char *)retArray.getData(), retArray.size());
187 #endif
188
|
189 karl 1.2 return retArray;
|
190 karl 1.1 };
191
192 //**********************************************************
193 /* Encode thanslates one six-bit pattern into a base-64 character.
194 Unsigned char is used to represent a six-bit stream of date.
195
196 */
197 inline PEGASUS_COMMON_LINKAGE char Base64::_Encode(Uint8 uc)
198 {
199 if (uc < 26)
200 return 'A'+uc;
201
202 if (uc < 52)
203 return 'a'+(uc-26);
204
205 if (uc < 62)
206 return '0'+(uc-52);
207
208 if (uc == 62)
209 return '+';
210
211 karl 1.1 return '/';
212 };
213
214 //Helper function returns true is a character is a valid base-64 character and false otherwise.
215
216 inline Boolean Base64::_IsBase64(char c)
217 {
218
219 if (c >= 'A' && c <= 'Z')
220 return true;
221
222 if (c >= 'a' && c <= 'z')
223 return true;
224
225 if (c >= '0' && c <= '9')
226 return true;
227
228 if (c == '+')
229 return true;
230
231 if (c == '/')
232 karl 1.1 return true;
233
234 if (c == '=')
235 return true;
236
237 return false;
238 };
239
240 // Translate one base-64 character into a six bit pattern
241 inline Uint8 Base64::_Decode(char c)
242 {
243 if (c >= 'A' && c <= 'Z')
244 return c - 'A';
245 if (c >= 'a' && c <= 'z')
246 return c - 'a' + 26;
247
248 if (c >= '0' && c <= '9')
249 return c - '0' + 52;
250
251 if (c == '+')
252 return 62;
253 karl 1.1
254 return 63;
255 };
256
257
258 PEGASUS_NAMESPACE_END
|