source: NonGTP/Xerces/xerces/include/xercesc/util/Base64.hpp @ 358

Revision 358, 9.4 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Id: Base64.hpp,v 1.13 2004/09/08 13:56:21 peiyongz Exp $
19 */
20
21#ifndef BASE64_HPP
22#define BASE64_HPP
23
24#include <xercesc/util/XercesDefs.hpp>
25#include <xercesc/util/XMLUniDefs.hpp>
26#include <xercesc/framework/MemoryManager.hpp>
27
28XERCES_CPP_NAMESPACE_BEGIN
29
30//
31// This class provides encode/decode for RFC 2045 Base64 as
32// defined by RFC 2045, N. Freed and N. Borenstein.
33// RFC 2045: Multipurpose Internet Mail Extensions (MIME)
34// Part One: Format of Internet Message Bodies. Reference
35// 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
36// This class is used by XML Schema binary format validation
37//
38//
39class XMLUTIL_EXPORT Base64
40{
41public :
42
43    enum Conformance
44    {
45        Conf_RFC2045
46      , Conf_Schema
47    };
48
49    //@{
50
51    /**
52     * Encodes octets into Base64 data
53     *
54     * NOTE: The returned buffer is dynamically allocated and is the
55     * responsibility of the caller to delete it when not longer needed.
56     * You can call XMLString::release to release this returned buffer.
57     *
58     * If a memory manager is provided, ask the memory manager to de-allocate
59     * the returned buffer.
60     *
61     * @param inputData Binary data in XMLByte stream.
62     * @param inputLength Length of the XMLByte stream.
63     * @param outputLength Length of the encoded Base64 byte stream.
64     * @param memMgr client provided memory manager
65     * @return Encoded Base64 data in XMLByte stream,
66     *      or NULL if input data can not be encoded.
67     * @see   XMLString::release(XMLByte**)
68     */
69    static XMLByte* encode(const XMLByte* const inputData
70                         , const unsigned int   inputLength
71                         , unsigned int*        outputLength
72                         , MemoryManager* const memMgr = 0);
73
74    /**
75     * Decodes Base64 data into octets
76     *
77     * NOTE: The returned buffer is dynamically allocated and is the
78     * responsibility of the caller to delete it when not longer needed.
79     * You can call XMLString::release to release this returned buffer.
80     *
81     * If a memory manager is provided, ask the memory manager to de-allocate
82     * the returned buffer.
83     *
84     * @param inputData Base64 data in XMLByte stream.
85     * @param decodedLength Length of decoded XMLByte stream.
86     * @param memMgr client provided memory manager
87     * @param conform conformance specified
88     * @return Decoded binary data in XMLByte stream,
89     *      or NULL if input data can not be decoded.
90     * @see   XMLString::release(XMLByte**)
91     */
92    static XMLByte* decode(
93                           const XMLByte*        const   inputData
94                         ,       unsigned int*           decodedLength
95                         ,       MemoryManager*  const   memMgr = 0
96                         ,       Conformance             conform = Conf_RFC2045
97                          );
98
99    /**
100     * Decodes Base64 data into XMLCh
101     *
102     * NOTE: The returned buffer is dynamically allocated and is the
103     * responsibility of the caller to delete it when not longer needed.
104     * You can call XMLString::release to release this returned buffer.
105     *
106     * If a memory manager is provided, ask the memory manager to de-allocate
107     * the returned buffer.
108     *
109     * @param inputData Base64 data in XMLCh stream.
110     * @param decodedLength Length of decoded XMLCh stream
111     * @param memMgr client provided memory manager
112     * @param conform conformance specified
113     * @return Decoded binary data in XMLCh stream,
114     *      or NULL if input data can not be decoded.
115     * @see   XMLString::release(XMLCh**)
116     */
117    static XMLCh* decode(
118                         const XMLCh*          const    inputData
119                       ,       unsigned int*            decodedLength
120                       ,       MemoryManager*  const    memMgr = 0
121                       ,       Conformance              conform = Conf_RFC2045
122                        );
123
124    /**
125     * Get data length
126         *
127     * Returns length of decoded data given an array
128     * containing encoded data.
129     *
130     * @param inputData Base64 data in XMLCh stream.
131     * @param memMgr client provided memory manager
132     * @param conform conformance specified
133     * @return Length of decoded data,
134         *      or -1 if input data can not be decoded.
135     */
136    static int getDataLength(
137                             const XMLCh*         const  inputData
138                            ,      MemoryManager* const  memMgr = 0
139                            ,      Conformance           conform = Conf_RFC2045
140                             );
141
142    //@}
143
144     /**
145     * get canonical representation
146     *
147     * Caller is responsible for the proper deallcation
148     * of the string returned.
149     *
150     * @param inputData A string containing the Base64
151     * @param memMgr client provided memory manager
152     * @param conform conformance specified
153     *
154     * return: the canonical representation of the Base64
155     *         if it is a valid Base64
156     *         0 otherwise
157     */
158
159    static XMLCh* getCanonicalRepresentation
160                  (
161                      const XMLCh*          const inputData
162                    ,       MemoryManager*  const memMgr = 0
163                    ,       Conformance           conform = Conf_RFC2045
164                  );
165
166private :
167
168    // -----------------------------------------------------------------------
169    //  Helper methods
170    // -----------------------------------------------------------------------
171
172    static XMLByte* decode(
173                           const XMLByte*        const   inputData
174                         ,       unsigned int*           outputLength
175                         ,       XMLByte*&               canRepData
176                         ,       MemoryManager*  const   memMgr = 0
177                         ,       Conformance             conform = Conf_RFC2045
178                          );
179
180    static void init();
181
182    static bool isData(const XMLByte& octet);
183    static bool isPad(const XMLByte& octet);
184
185    static XMLByte set1stOctet(const XMLByte&, const XMLByte&);
186    static XMLByte set2ndOctet(const XMLByte&, const XMLByte&);
187    static XMLByte set3rdOctet(const XMLByte&, const XMLByte&);
188
189    static void split1stOctet(const XMLByte&, XMLByte&, XMLByte&);
190    static void split2ndOctet(const XMLByte&, XMLByte&, XMLByte&);
191    static void split3rdOctet(const XMLByte&, XMLByte&, XMLByte&);
192
193    // -----------------------------------------------------------------------
194    //  Unimplemented constructors and operators
195    // -----------------------------------------------------------------------
196    Base64();
197    Base64(const Base64&);
198
199    // -----------------------------------------------------------------------
200    //  Private data members
201    //
202    //  base64Alphabet
203    //     The Base64 alphabet (see RFC 2045).
204    //
205    //  base64Padding
206    //     Padding character (see RFC 2045).
207    //
208    //  base64Inverse
209    //     Table used in decoding base64.
210    //
211    //  isInitialized
212    //     Set once base64Inverse is initalized.
213    //
214    //  quadsPerLine
215    //     Number of quadruplets per one line. The encoded output
216    //     stream must be represented in lines of no more
217    //     than 19 quadruplets each.
218    //
219    // -----------------------------------------------------------------------
220
221    static const XMLByte  base64Alphabet[];
222    static const XMLByte  base64Padding;
223
224    static XMLByte  base64Inverse[];
225    static bool  isInitialized;
226
227    static const unsigned int  quadsPerLine;
228};
229
230// -----------------------------------------------------------------------
231//  Helper methods
232// -----------------------------------------------------------------------
233inline bool Base64::isPad(const XMLByte& octet)
234{
235    return ( octet == base64Padding );
236}
237
238inline XMLByte Base64::set1stOctet(const XMLByte& b1, const XMLByte& b2)
239{
240    return (( b1 << 2 ) | ( b2 >> 4 ));
241}
242
243inline XMLByte Base64::set2ndOctet(const XMLByte& b2, const XMLByte& b3)
244{
245    return (( b2 << 4 ) | ( b3 >> 2 ));
246}
247
248inline XMLByte Base64::set3rdOctet(const XMLByte& b3, const XMLByte& b4)
249{
250    return (( b3 << 6 ) | b4 );
251}
252
253inline void Base64::split1stOctet(const XMLByte& ch, XMLByte& b1, XMLByte& b2) {
254    b1 = ch >> 2;
255    b2 = ( ch & 0x3 ) << 4;
256}
257
258inline void Base64::split2ndOctet(const XMLByte& ch, XMLByte& b2, XMLByte& b3) {
259    b2 |= ch >> 4;  // combine with previous value
260    b3 = ( ch & 0xf ) << 2;
261}
262
263inline void Base64::split3rdOctet(const XMLByte& ch, XMLByte& b3, XMLByte& b4) {
264    b3 |= ch >> 6;  // combine with previous value
265    b4 = ( ch & 0x3f );
266}
267
268XERCES_CPP_NAMESPACE_END
269
270#endif
Note: See TracBrowser for help on using the repository browser.