http://xml.apache.org/http://www.apache.org/http://www.w3.org/

Home

Readme
Release Info

Installation
Download
Build

FAQs
Samples
API Docs

DOM C++ Binding
Programming
Migration Guide

Feedback
Bug-Reporting
PDF Document

CVS Repository
Mail Archive

API Docs for SAX and DOM
 

Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

Base64.hpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2001,2004 The Apache Software Foundation.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  * 
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 /*
00018  * $Id: Base64.hpp,v 1.13 2004/09/08 13:56:21 peiyongz Exp $
00019  */
00020 
00021 #ifndef BASE64_HPP
00022 #define BASE64_HPP
00023 
00024 #include <xercesc/util/XercesDefs.hpp>
00025 #include <xercesc/util/XMLUniDefs.hpp>
00026 #include <xercesc/framework/MemoryManager.hpp>
00027 
00028 XERCES_CPP_NAMESPACE_BEGIN
00029 
00030 //
00031 // This class provides encode/decode for RFC 2045 Base64 as
00032 // defined by RFC 2045, N. Freed and N. Borenstein.
00033 // RFC 2045: Multipurpose Internet Mail Extensions (MIME)
00034 // Part One: Format of Internet Message Bodies. Reference
00035 // 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
00036 // This class is used by XML Schema binary format validation
00037 //
00038 //
00039 class  Base64
00040 {
00041 public :
00042 
00043     enum Conformance
00044     {
00045         Conf_RFC2045
00046       , Conf_Schema
00047     };
00048 
00050 
00069     static XMLByte* encode(const XMLByte* const inputData
00070                          , const unsigned int   inputLength
00071                          , unsigned int*        outputLength
00072                          , MemoryManager* const memMgr = 0);
00073 
00092     static XMLByte* decode(
00093                            const XMLByte*        const   inputData
00094                          ,       unsigned int*           decodedLength
00095                          ,       MemoryManager*  const   memMgr = 0
00096                          ,       Conformance             conform = Conf_RFC2045
00097                           );
00098 
00117     static XMLCh* decode(
00118                          const XMLCh*          const    inputData
00119                        ,       unsigned int*            decodedLength
00120                        ,       MemoryManager*  const    memMgr = 0
00121                        ,       Conformance              conform = Conf_RFC2045
00122                         );
00123 
00136     static int getDataLength(
00137                              const XMLCh*         const  inputData
00138                             ,      MemoryManager* const  memMgr = 0
00139                             ,      Conformance           conform = Conf_RFC2045
00140                              );
00141 
00143 
00159     static XMLCh* getCanonicalRepresentation
00160                   (
00161                       const XMLCh*          const inputData
00162                     ,       MemoryManager*  const memMgr = 0
00163                     ,       Conformance           conform = Conf_RFC2045
00164                   );
00165 
00166 private :
00167 
00168     // -----------------------------------------------------------------------
00169     //  Helper methods
00170     // -----------------------------------------------------------------------
00171 
00172     static XMLByte* decode(
00173                            const XMLByte*        const   inputData
00174                          ,       unsigned int*           outputLength
00175                          ,       XMLByte*&               canRepData
00176                          ,       MemoryManager*  const   memMgr = 0
00177                          ,       Conformance             conform = Conf_RFC2045
00178                           );
00179 
00180     static void init();
00181 
00182     static bool isData(const XMLByte& octet);
00183     static bool isPad(const XMLByte& octet);
00184 
00185     static XMLByte set1stOctet(const XMLByte&, const XMLByte&);
00186     static XMLByte set2ndOctet(const XMLByte&, const XMLByte&);
00187     static XMLByte set3rdOctet(const XMLByte&, const XMLByte&);
00188 
00189     static void split1stOctet(const XMLByte&, XMLByte&, XMLByte&);
00190     static void split2ndOctet(const XMLByte&, XMLByte&, XMLByte&);
00191     static void split3rdOctet(const XMLByte&, XMLByte&, XMLByte&);
00192 
00193     // -----------------------------------------------------------------------
00194     //  Unimplemented constructors and operators
00195     // -----------------------------------------------------------------------
00196     Base64();
00197     Base64(const Base64&);
00198 
00199     // -----------------------------------------------------------------------
00200     //  Private data members
00201     //
00202     //  base64Alphabet
00203     //     The Base64 alphabet (see RFC 2045).
00204     //
00205     //  base64Padding
00206     //     Padding character (see RFC 2045).
00207     //
00208     //  base64Inverse
00209     //     Table used in decoding base64.
00210     //
00211     //  isInitialized
00212     //     Set once base64Inverse is initalized.
00213     //
00214     //  quadsPerLine
00215     //     Number of quadruplets per one line. The encoded output
00216     //     stream must be represented in lines of no more
00217     //     than 19 quadruplets each.
00218     //
00219     // -----------------------------------------------------------------------
00220 
00221     static const XMLByte  base64Alphabet[];
00222     static const XMLByte  base64Padding;
00223 
00224     static XMLByte  base64Inverse[];
00225     static bool  isInitialized;
00226 
00227     static const unsigned int  quadsPerLine;
00228 };
00229 
00230 // -----------------------------------------------------------------------
00231 //  Helper methods
00232 // -----------------------------------------------------------------------
00233 inline bool Base64::isPad(const XMLByte& octet)
00234 {
00235     return ( octet == base64Padding );
00236 }
00237 
00238 inline XMLByte Base64::set1stOctet(const XMLByte& b1, const XMLByte& b2)
00239 {
00240     return (( b1 << 2 ) | ( b2 >> 4 ));
00241 }
00242 
00243 inline XMLByte Base64::set2ndOctet(const XMLByte& b2, const XMLByte& b3)
00244 {
00245     return (( b2 << 4 ) | ( b3 >> 2 ));
00246 }
00247 
00248 inline XMLByte Base64::set3rdOctet(const XMLByte& b3, const XMLByte& b4)
00249 {
00250     return (( b3 << 6 ) | b4 );
00251 }
00252 
00253 inline void Base64::split1stOctet(const XMLByte& ch, XMLByte& b1, XMLByte& b2) {
00254     b1 = ch >> 2;
00255     b2 = ( ch & 0x3 ) << 4;
00256 }
00257 
00258 inline void Base64::split2ndOctet(const XMLByte& ch, XMLByte& b2, XMLByte& b3) {
00259     b2 |= ch >> 4;  // combine with previous value
00260     b3 = ( ch & 0xf ) << 2;
00261 }
00262 
00263 inline void Base64::split3rdOctet(const XMLByte& ch, XMLByte& b3, XMLByte& b4) {
00264     b3 |= ch >> 6;  // combine with previous value
00265     b4 = ( ch & 0x3f );
00266 }
00267 
00268 XERCES_CPP_NAMESPACE_END
00269 
00270 #endif


Copyright © 1994-2004 The Apache Software Foundation. All Rights Reserved.