Base64.hpp

Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 /*
00019  * $Id: Base64.hpp 568078 2007-08-21 11:43:25Z amassari $
00020  */
00021 
00022 #ifndef BASE64_HPP
00023 #define BASE64_HPP
00024 
00025 #include <xercesc/util/XercesDefs.hpp>
00026 #include <xercesc/util/XMLUniDefs.hpp>
00027 #include <xercesc/framework/MemoryManager.hpp>
00028 
00029 XERCES_CPP_NAMESPACE_BEGIN
00030 
00031 //
00032 // This class provides encode/decode for RFC 2045 Base64 as
00033 // defined by RFC 2045, N. Freed and N. Borenstein.
00034 // RFC 2045: Multipurpose Internet Mail Extensions (MIME)
00035 // Part One: Format of Internet Message Bodies. Reference
00036 // 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
00037 // This class is used by XML Schema binary format validation
00038 //
00039 //
00040 class XMLUTIL_EXPORT Base64
00041 {
00042 public :
00043 
00044     enum Conformance
00045     {
00046         Conf_RFC2045
00047       , Conf_Schema
00048     };
00049 
00051 
00070     static XMLByte* encode(const XMLByte* const inputData
00071                          , const unsigned int   inputLength
00072                          , unsigned int*        outputLength
00073                          , MemoryManager* const memMgr = 0);
00074 
00097     static XMLByte* decode(
00098                            const XMLByte*        const   inputData
00099                          ,       unsigned int*           decodedLength
00100                          ,       MemoryManager*  const   memMgr = 0
00101                          ,       Conformance             conform = Conf_RFC2045
00102                           );
00103 
00128     static XMLCh* decode(
00129                          const XMLCh*          const    inputData
00130                        ,       unsigned int*            decodedLength
00131                        ,       MemoryManager*  const    memMgr = 0
00132                        ,       Conformance              conform = Conf_RFC2045
00133                         );
00134    
00157     static XMLByte* decodeToXMLByte(
00158                            const XMLCh*          const   inputData
00159                          ,       unsigned int*           decodedLength
00160                          ,       MemoryManager*  const   memMgr = 0
00161                          ,       Conformance             conform = Conf_RFC2045
00162                           );
00175     static int getDataLength(
00176                              const XMLCh*         const  inputData
00177                             ,      MemoryManager* const  memMgr = 0
00178                             ,      Conformance           conform = Conf_RFC2045
00179                              );
00180 
00182 
00198     static XMLCh* getCanonicalRepresentation
00199                   (
00200                       const XMLCh*          const inputData
00201                     ,       MemoryManager*  const memMgr = 0
00202                     ,       Conformance           conform = Conf_RFC2045
00203                   );
00204 
00205 private :
00206 
00207     // -----------------------------------------------------------------------
00208     //  Helper methods
00209     // -----------------------------------------------------------------------
00210 
00211     static XMLByte* decode(
00212                            const XMLByte*        const   inputData
00213                          ,       unsigned int*           outputLength
00214                          ,       XMLByte*&               canRepData
00215                          ,       MemoryManager*  const   memMgr = 0
00216                          ,       Conformance             conform = Conf_RFC2045
00217                           );
00218 
00219     static void init();
00220 
00221     static bool isData(const XMLByte& octet);
00222     static bool isPad(const XMLByte& octet);
00223 
00224     static XMLByte set1stOctet(const XMLByte&, const XMLByte&);
00225     static XMLByte set2ndOctet(const XMLByte&, const XMLByte&);
00226     static XMLByte set3rdOctet(const XMLByte&, const XMLByte&);
00227 
00228     static void split1stOctet(const XMLByte&, XMLByte&, XMLByte&);
00229     static void split2ndOctet(const XMLByte&, XMLByte&, XMLByte&);
00230     static void split3rdOctet(const XMLByte&, XMLByte&, XMLByte&);
00231 
00232     // -----------------------------------------------------------------------
00233     //  Unimplemented constructors and operators
00234     // -----------------------------------------------------------------------
00235     Base64();
00236     Base64(const Base64&);
00237 
00238     // -----------------------------------------------------------------------
00239     //  Private data members
00240     //
00241     //  base64Alphabet
00242     //     The Base64 alphabet (see RFC 2045).
00243     //
00244     //  base64Padding
00245     //     Padding character (see RFC 2045).
00246     //
00247     //  base64Inverse
00248     //     Table used in decoding base64.
00249     //
00250     //  isInitialized
00251     //     Set once base64Inverse is initalized.
00252     //
00253     //  quadsPerLine
00254     //     Number of quadruplets per one line. The encoded output
00255     //     stream must be represented in lines of no more
00256     //     than 19 quadruplets each.
00257     //
00258     // -----------------------------------------------------------------------
00259 
00260     static const XMLByte  base64Alphabet[];
00261     static const XMLByte  base64Padding;
00262 
00263     static XMLByte  base64Inverse[];
00264     static bool  isInitialized;
00265 
00266     static const unsigned int  quadsPerLine;
00267 };
00268 
00269 // -----------------------------------------------------------------------
00270 //  Helper methods
00271 // -----------------------------------------------------------------------
00272 inline bool Base64::isPad(const XMLByte& octet)
00273 {
00274     return ( octet == base64Padding );
00275 }
00276 
00277 inline XMLByte Base64::set1stOctet(const XMLByte& b1, const XMLByte& b2)
00278 {
00279     return (( b1 << 2 ) | ( b2 >> 4 ));
00280 }
00281 
00282 inline XMLByte Base64::set2ndOctet(const XMLByte& b2, const XMLByte& b3)
00283 {
00284     return (( b2 << 4 ) | ( b3 >> 2 ));
00285 }
00286 
00287 inline XMLByte Base64::set3rdOctet(const XMLByte& b3, const XMLByte& b4)
00288 {
00289     return (( b3 << 6 ) | b4 );
00290 }
00291 
00292 inline void Base64::split1stOctet(const XMLByte& ch, XMLByte& b1, XMLByte& b2) {
00293     b1 = ch >> 2;
00294     b2 = ( ch & 0x3 ) << 4;
00295 }
00296 
00297 inline void Base64::split2ndOctet(const XMLByte& ch, XMLByte& b2, XMLByte& b3) {
00298     b2 |= ch >> 4;  // combine with previous value
00299     b3 = ( ch & 0xf ) << 2;
00300 }
00301 
00302 inline void Base64::split3rdOctet(const XMLByte& ch, XMLByte& b3, XMLByte& b4) {
00303     b3 |= ch >> 6;  // combine with previous value
00304     b4 = ( ch & 0x3f );
00305 }
00306 
00307 XERCES_CPP_NAMESPACE_END
00308 
00309 #endif

Generated on Thu Feb 19 11:35:43 2009 for Xerces-C++ by  doxygen 1.5.4