Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:15:11

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *      http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 /*
0019  * $Id$
0020  */
0021 
0022 #if !defined(XERCESC_INCLUDE_GUARD_BASE64_HPP)
0023 #define XERCESC_INCLUDE_GUARD_BASE64_HPP
0024 
0025 #include <xercesc/util/XercesDefs.hpp>
0026 #include <xercesc/util/XMLUniDefs.hpp>
0027 #include <xercesc/framework/MemoryManager.hpp>
0028 
0029 XERCES_CPP_NAMESPACE_BEGIN
0030 
0031 //
0032 // This class provides encode/decode for RFC 2045 Base64 as
0033 // defined by RFC 2045, N. Freed and N. Borenstein.
0034 // RFC 2045: Multipurpose Internet Mail Extensions (MIME)
0035 // Part One: Format of Internet Message Bodies. Reference
0036 // 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
0037 // This class is used by XML Schema binary format validation
0038 //
0039 //
0040 class XMLUTIL_EXPORT Base64
0041 {
0042 public :
0043 
0044     enum Conformance
0045     {
0046         Conf_RFC2045
0047       , Conf_Schema
0048     };
0049 
0050     //@{
0051 
0052     /**
0053      * Encodes octets into Base64 data
0054      *
0055      * NOTE: The returned buffer is dynamically allocated and is the
0056      * responsibility of the caller to delete it when not longer needed.
0057      * Use the memory manager to release the returned buffer or
0058      * operator delete() if none was provided.
0059      *
0060      * @param inputData Binary data in XMLByte stream.
0061      * @param inputLength Length of the XMLByte stream.
0062      * @param outputLength Length of the encoded Base64 byte stream.
0063      * @param memMgr client provided memory manager
0064      * @return Encoded Base64 data in XMLByte stream,
0065      *      or NULL if input data can not be encoded.
0066      */
0067     static XMLByte* encode(const XMLByte* const inputData
0068                          , const XMLSize_t      inputLength
0069                          , XMLSize_t*           outputLength
0070                          , MemoryManager* const memMgr = 0);
0071 
0072     /**
0073      * Decodes Base64 data into octets
0074      *
0075      * NOTE: The returned buffer is dynamically allocated and is the
0076      * responsibility of the caller to delete it when not longer needed.
0077      * Use the memory manager to release the returned buffer or
0078      * operator delete() if none was provided.
0079      *
0080      * @param inputData Base64 data in XMLByte stream.
0081      * @param decodedLength Length of decoded XMLByte stream.
0082      * @param memMgr client provided memory manager
0083      * @param conform conformance specified: if the input data conforms to the
0084      *                RFC 2045 it is allowed to have any number of whitespace
0085      *                characters inside; if it conforms to the XMLSchema specs,
0086      *                it is allowed to have at most one whitespace character
0087      *                between the quartets
0088      * @return Decoded binary data in XMLByte stream,
0089      *      or NULL if input data can not be decoded.
0090      */
0091     static XMLByte* decode(
0092                            const XMLByte*        const   inputData
0093                          ,       XMLSize_t*              decodedLength
0094                          ,       MemoryManager*  const   memMgr = 0
0095                          ,       Conformance             conform = Conf_RFC2045
0096                           );
0097 
0098    /**
0099      * Decodes Base64 data into octets
0100      *
0101      * NOTE: The returned buffer is dynamically allocated and is the
0102      * responsibility of the caller to delete it when not longer needed.
0103      * Use the memory manager to release the returned buffer or
0104      * operator delete() if none was provided.
0105      *
0106      * @param inputData Base64 data in XMLCh stream.
0107      * @param decodedLength Length of decoded XMLByte stream.
0108      * @param memMgr client provided memory manager
0109      * @param conform conformance specified: if the input data conforms to the
0110      *                RFC 2045 it is allowed to have any number of whitespace
0111      *                characters inside; if it conforms to the XMLSchema specs,
0112      *                it is allowed to have at most one whitespace character
0113      *                between the quartets
0114      * @return Decoded binary data in XMLByte stream,
0115      *      or NULL if input data can not be decoded.
0116      */
0117     static XMLByte* decodeToXMLByte(
0118                            const XMLCh*          const   inputData
0119                          ,       XMLSize_t*              decodedLength
0120                          ,       MemoryManager*  const   memMgr = 0
0121                          ,       Conformance             conform = Conf_RFC2045
0122                           );
0123     /**
0124      * Get data length
0125      *
0126      * Returns length of decoded data given an array
0127      * containing encoded data.
0128      *
0129      * @param inputData Base64 data in XMLCh stream.
0130      * @param memMgr client provided memory manager
0131      * @param conform conformance specified
0132      * @return Length of decoded data,
0133      *      or -1 if input data can not be decoded.
0134      */
0135     static int getDataLength(
0136                              const XMLCh*         const  inputData
0137                             ,      MemoryManager* const  memMgr = 0
0138                             ,      Conformance           conform = Conf_RFC2045
0139                              );
0140 
0141     //@}
0142 
0143      /**
0144      * get canonical representation
0145      *
0146      * Caller is responsible for the proper deallocation
0147      * of the string returned.
0148      *
0149      * @param inputData A string containing the Base64
0150      * @param memMgr client provided memory manager
0151      * @param conform conformance specified
0152      *
0153      * return: the canonical representation of the Base64
0154      *         if it is a valid Base64
0155      *         0 otherwise
0156      */
0157 
0158     static XMLCh* getCanonicalRepresentation
0159                   (
0160                       const XMLCh*          const inputData
0161                     ,       MemoryManager*  const memMgr = 0
0162                     ,       Conformance           conform = Conf_RFC2045
0163                   );
0164 
0165 private :
0166 
0167     // -----------------------------------------------------------------------
0168     //  Helper methods
0169     // -----------------------------------------------------------------------
0170 
0171     static XMLByte* decode(
0172                            const XMLByte*        const   inputData
0173                          ,       XMLSize_t*              outputLength
0174                          ,       XMLByte*&               canRepData
0175                          ,       MemoryManager*  const   memMgr = 0
0176                          ,       Conformance             conform = Conf_RFC2045
0177                           );
0178 
0179     static bool isData(const XMLByte& octet);
0180     static bool isPad(const XMLByte& octet);
0181 
0182     static XMLByte set1stOctet(const XMLByte&, const XMLByte&);
0183     static XMLByte set2ndOctet(const XMLByte&, const XMLByte&);
0184     static XMLByte set3rdOctet(const XMLByte&, const XMLByte&);
0185 
0186     static void split1stOctet(const XMLByte&, XMLByte&, XMLByte&);
0187     static void split2ndOctet(const XMLByte&, XMLByte&, XMLByte&);
0188     static void split3rdOctet(const XMLByte&, XMLByte&, XMLByte&);
0189 
0190     // -----------------------------------------------------------------------
0191     //  Unimplemented constructors and operators
0192     // -----------------------------------------------------------------------
0193     Base64();
0194     Base64(const Base64&);
0195 
0196     // -----------------------------------------------------------------------
0197     //  Private data members
0198     //
0199     //  base64Alphabet
0200     //     The Base64 alphabet (see RFC 2045).
0201     //
0202     //  base64Padding
0203     //     Padding character (see RFC 2045).
0204     //
0205     //  base64Inverse
0206     //     Table used in decoding base64.
0207     //
0208     //  isInitialized
0209     //     Set once base64Inverse is initialized.
0210     //
0211     //  quadsPerLine
0212     //     Number of quadruplets per one line. The encoded output
0213     //     stream must be represented in lines of no more
0214     //     than 19 quadruplets each.
0215     //
0216     // -----------------------------------------------------------------------
0217 
0218     static const XMLByte  base64Alphabet[];
0219     static const XMLByte  base64Padding;
0220 
0221     static const XMLByte  base64Inverse[];
0222 
0223     static const unsigned int  quadsPerLine;
0224 };
0225 
0226 // -----------------------------------------------------------------------
0227 //  Helper methods
0228 // -----------------------------------------------------------------------
0229 inline bool Base64::isPad(const XMLByte& octet)
0230 {
0231     return ( octet == base64Padding );
0232 }
0233 
0234 inline XMLByte Base64::set1stOctet(const XMLByte& b1, const XMLByte& b2)
0235 {
0236     return (( b1 << 2 ) | ( b2 >> 4 ));
0237 }
0238 
0239 inline XMLByte Base64::set2ndOctet(const XMLByte& b2, const XMLByte& b3)
0240 {
0241     return (( b2 << 4 ) | ( b3 >> 2 ));
0242 }
0243 
0244 inline XMLByte Base64::set3rdOctet(const XMLByte& b3, const XMLByte& b4)
0245 {
0246     return (( b3 << 6 ) | b4 );
0247 }
0248 
0249 inline void Base64::split1stOctet(const XMLByte& ch, XMLByte& b1, XMLByte& b2) {
0250     b1 = ch >> 2;
0251     b2 = ( ch & 0x3 ) << 4;
0252 }
0253 
0254 inline void Base64::split2ndOctet(const XMLByte& ch, XMLByte& b2, XMLByte& b3) {
0255     b2 |= ch >> 4;  // combine with previous value
0256     b3 = ( ch & 0xf ) << 2;
0257 }
0258 
0259 inline void Base64::split3rdOctet(const XMLByte& ch, XMLByte& b3, XMLByte& b4) {
0260     b3 |= ch >> 6;  // combine with previous value
0261     b4 = ( ch & 0x3f );
0262 }
0263 
0264 XERCES_CPP_NAMESPACE_END
0265 
0266 #endif