(file) Return to Base64.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 martin 1.28 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.29 //
  3 martin 1.28 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.29 //
 10 martin 1.28 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.29 //
 17 martin 1.28 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.29 //
 20 martin 1.28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.29 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.29 //
 28 martin 1.28 //////////////////////////////////////////////////////////////////////////
 29 karl   1.1  //
 30             //%/////////////////////////////////////////////////////////////////////////////
 31             
 32 kumpf  1.7  #include "Base64.h"
 33 kumpf  1.12 #include <Pegasus/Common/ArrayInternal.h>
 34 karl   1.1  
 35             PEGASUS_NAMESPACE_BEGIN
 36             PEGASUS_USING_STD;
 37             
 38 sage   1.5  #ifdef PEGASUS_PLATFORM_AIX_RS_IBMCXX
 39             #define inline
 40             #endif
 41             
 42 keith.petley 1.16 
 43                   //**********************************************************
 44                   /*  Encode thanslates one six-bit pattern into a base-64 character.
 45                       Unsigned char is used to represent a six-bit stream of date.
 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 dmitry.mikulin 1.27 }
 63 keith.petley   1.16 
 64 kumpf          1.26 // Helper function returns true is a character is a valid base-64 character
 65                     // and false otherwise.
 66 keith.petley   1.16 
 67                     inline Boolean Base64::_IsBase64(char c)
 68                     {
 69                     
 70                         if (c >= 'A' && c <= 'Z')
 71                             return true;
 72                     
 73                         if (c >= 'a' && c <= 'z')
 74                             return true;
 75                     
 76                         if (c >= '0' && c <= '9')
 77                             return true;
 78                     
 79                         if (c == '+')
 80                             return true;
 81                     
 82                         if (c == '/')
 83                             return true;
 84                     
 85                         if (c == '=')
 86                             return true;
 87 keith.petley   1.16 
 88                         return false;
 89 dmitry.mikulin 1.27 }
 90 kumpf          1.26 
 91 keith.petley   1.16  // Translate one base-64 character into a six bit pattern
 92                     inline Uint8 Base64::_Decode(char c)
 93                     {
 94                         if (c >= 'A' && c <= 'Z')
 95                             return c - 'A';
 96                         if (c >= 'a' && c <= 'z')
 97                             return c - 'a' + 26;
 98                     
 99                         if (c >= '0' && c <= '9')
100                             return c - '0' + 52;
101                     
102                         if (c == '+')
103                             return 62;
104                     
105                         return 63;
106 dmitry.mikulin 1.27 }
107 keith.petley   1.16 
108                     
109 karl           1.1  //*************************************************************
110                     /*  Encode static method takes an array of 8-bit values and
111                         returns a base-64 stream.
112 karl           1.2      ATTN: KS feb 2002 - This is probably a very slow an inefficient
113                         implementation and could be improved if it is required for
114                         production.  Today it is only for test programs.
115 karl           1.1  */
116 mike           1.24 Buffer Base64::encode(const Buffer& vby)
117 karl           1.1  {
118 mike           1.24     Buffer retArray;
119 karl           1.1      // If nothing in input string, return empty string
120                         if (vby.size() == 0)
121 karl           1.2          return retArray;
122 karl           1.1      // for every character in the input array taken 3 bytes at a time
123                         for (Uint32 i=0; i < vby.size(); i+=3)
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                     
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 kumpf          1.26             retArray.append('=');
150 gerarda        1.13 
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 dmitry.mikulin 1.27 }
169 kumpf          1.26 
170                     /*
171                         I checked for the zero length. The algorithm would also work for zero
172                         length input stream, but I’m pretty adamant about handling border
173                         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
175                         thing I do is to shift the bits around from three 8-bit values to four
176                         6-bit values. Then I encode the 6-bit values and add then one at a time
177                         to the output stream. This is actually quite inefficient. The STL
178                         character array is being allocated one byte at a time. The algorithm
179                         would be much faster, if I pre-allocated that array. I’ll leave that as
180                         an optimization practical exercise for the reader.
181 kumpf          1.9  */
182 karl           1.1  
183 kumpf          1.26 /*  The decode static method takes a base-64 stream and converts it
184 karl           1.1      to an array of 8-bit values.
185                     */
186 mike           1.24 Buffer Base64::decode(const Buffer& strInput)
187 karl           1.1  {
188                         //Strip any non-base64 characters from the input
189 mike           1.24     Buffer str;
190 karl           1.1      for (Uint32 j=0;j<strInput.size();j++)
191                         {
192                             if (_IsBase64(strInput[j]))
193 kumpf          1.15             str.append(strInput[j]);
194 karl           1.1      }
195 karl           1.2  
196 mike           1.24     Buffer retArray;
197 karl           1.1  
198                         // Return if the input is zero length
199 kumpf          1.15     if (str.size() == 0)
200 karl           1.2          return retArray;
201 karl           1.1  
202                         //  comment
203 kumpf          1.15     for (Uint32 i=0; i < str.size(); i+=4)
204 karl           1.1      {
205                             char c1='A',c2='A',c3='A',c4='A';
206                     
207                             c1 = str[i];
208 kumpf          1.15         if (i+1<str.size())
209 karl           1.1              c2 = str[i+1];
210 kumpf          1.15         if (i+2<str.size())
211 karl           1.1              c3 = str[i+2];
212 kumpf          1.15         if (i+3<str.size())
213 karl           1.1              c4 = str[i+3];
214                     
215                             Uint8 by1=0,by2=0,by3=0,by4=0;
216                             by1 = _Decode(c1);
217                             by2 = _Decode(c2);
218                             by3 = _Decode(c3);
219                             by4 = _Decode(c4);
220                             //cout << "base::64decode bytes" <<
221                             //      " 1 " << c1 << " " << by1 <<
222                             //      " 2 " << c2 << " " << by2 <<
223                             //      " 3 " << c3 << " " << by3 <<
224                             //      " 4 " << c4 << " " << by4 << endl;
225 kumpf          1.26 
226 karl           1.1          // append first byte by shifting
227 david.dillard  1.22         retArray.append( static_cast<char>((by1<<2)|(by2>>4)) );
228 karl           1.1  
229                             // append second byte if not padding
230                             if (c3 != '=')
231 david.dillard  1.22             retArray.append( static_cast<char>(((by2&0xf)<<4)|(by3>>2)) );
232 karl           1.1  
233                             if (c4 != '=')
234 david.dillard  1.22             retArray.append( static_cast<char>(((by3&0x3)<<6)|by4) );
235 karl           1.1      }
236                     
237 gerarda        1.13 
238 karl           1.2      return retArray;
239 dmitry.mikulin 1.27 }
240 karl           1.1  
241                     PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2