source: NonGTP/Xerces/xercesc/util/Base64.hpp @ 188

Revision 188, 9.6 KB checked in by mattausch, 19 years ago (diff)

added xercesc to support

Line 
1/*
2 * The Apache Software License, Version 1.1
3 *
4 * Copyright (c) 2001 The Apache Software Foundation.  All rights
5 * reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in
16 *    the documentation and/or other materials provided with the
17 *    distribution.
18 *
19 * 3. The end-user documentation included with the redistribution,
20 *    if any, must include the following acknowledgment:
21 *       "This product includes software developed by the
22 *        Apache Software Foundation (http://www.apache.org/)."
23 *    Alternately, this acknowledgment may appear in the software itself,
24 *    if and wherever such third-party acknowledgments normally appear.
25 *
26 * 4. The names "Xerces" and "Apache Software Foundation" must
27 *    not be used to endorse or promote products derived from this
28 *    software without prior written permission. For written
29 *    permission, please contact apache\@apache.org.
30 *
31 * 5. Products derived from this software may not be called "Apache",
32 *    nor may "Apache" appear in their name, without prior written
33 *    permission of the Apache Software Foundation.
34 *
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46 * SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation, and was
51 * originally based on software copyright (c) 2001, International
52 * Business Machines, Inc., http://www.ibm.com .  For more information
53 * on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57/*
58 * $Id: Base64.hpp,v 1.8 2003/12/17 00:18:35 cargilld Exp $
59 */
60
61#ifndef BASE64_HPP
62#define BASE64_HPP
63
64#include <xercesc/util/XercesDefs.hpp>
65#include <xercesc/util/XMLUniDefs.hpp>
66#include <xercesc/framework/MemoryManager.hpp>
67
68XERCES_CPP_NAMESPACE_BEGIN
69
70//
71// This class provides encode/decode for RFC 2045 Base64 as
72// defined by RFC 2045, N. Freed and N. Borenstein.
73// RFC 2045: Multipurpose Internet Mail Extensions (MIME)
74// Part One: Format of Internet Message Bodies. Reference
75// 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt
76// This class is used by XML Schema binary format validation
77//
78//
79class XMLUTIL_EXPORT Base64
80{
81public :
82
83    //@{
84
85    /**
86     * Encodes octets into Base64 data
87     *
88     * NOTE: The returned buffer is dynamically allocated and is the
89     * responsibility of the caller to delete it when not longer needed.
90     * You can call XMLString::release to release this returned buffer.
91     *
92     * If a memory manager is provided, ask the memory manager to de-allocate
93     * the returned buffer.
94     *
95     * @param inputData Binary data in XMLByte stream.
96     * @param inputLength Length of the XMLByte stream.
97     * @param outputLength Length of the encoded Base64 byte stream.
98     * @param memMgr client provided memory manager
99     * @return Encoded Base64 data in XMLByte stream,
100     *      or NULL if input data can not be encoded.
101     * @see   XMLString::release(XMLByte**)
102     */
103    static XMLByte* encode(const XMLByte* const inputData
104                         , const unsigned int   inputLength
105                         , unsigned int*        outputLength                         
106                         , MemoryManager* const memMgr = 0);
107
108    /**
109     * Decodes Base64 data into octets
110     *
111     * NOTE: The returned buffer is dynamically allocated and is the
112     * responsibility of the caller to delete it when not longer needed.
113     * You can call XMLString::release to release this returned buffer.
114     *
115     * If a memory manager is provided, ask the memory manager to de-allocate
116     * the returned buffer.
117     *
118     * @param inputData Base64 data in XMLByte stream.
119     * @param outputLength Length of decoded XMLByte stream.
120     * @param memMgr client provided memory manager
121     * @return Decoded binary data in XMLByte stream,
122     *      or NULL if input data can not be decoded.
123     * @see   XMLString::release(XMLByte**)
124     */
125    static XMLByte* decode(const XMLByte* const inputData
126                         , unsigned int*        outputLength                         
127                         , MemoryManager* const memMgr = 0);
128
129    /**
130     * Decodes Base64 data into XMLCh
131     *
132     * NOTE: The returned buffer is dynamically allocated and is the
133     * responsibility of the caller to delete it when not longer needed.
134     * You can call XMLString::release to release this returned buffer.
135     *
136     * If a memory manager is provided, ask the memory manager to de-allocate
137     * the returned buffer.
138     *
139     * @param inputData Base64 data in XMLCh stream.
140     * @param outputLength Length of decoded XMLCh stream
141     * @param memMgr client provided memory manager
142     * @return Decoded binary data in XMLCh stream,
143     *      or NULL if input data can not be decoded.
144     * @see   XMLString::release(XMLCh**)
145     */
146    static XMLCh* decode(const XMLCh* const   inputData
147                       , unsigned int*        outputLength
148                       , MemoryManager* const memMgr = 0);
149
150    /**
151     * Get data length
152         *
153     * Returns length of decoded data given an array
154     * containing encoded data.
155     *
156     * @param inputData Base64 data in XMLCh stream.
157     * @return Length of decoded data,
158         *      or -1 if input data can not be decoded.
159     */
160    static int getDataLength(const XMLCh* const inputData
161        , MemoryManager* const memMgr = 0);
162
163    //@}
164
165private :
166
167    // -----------------------------------------------------------------------
168    //  Helper methods
169    // -----------------------------------------------------------------------
170
171    static void init();
172
173    static bool isData(const XMLByte& octet);
174    static bool isPad(const XMLByte& octet);
175
176    static XMLByte set1stOctet(const XMLByte&, const XMLByte&);
177    static XMLByte set2ndOctet(const XMLByte&, const XMLByte&);
178    static XMLByte set3rdOctet(const XMLByte&, const XMLByte&);
179
180    static void split1stOctet(const XMLByte&, XMLByte&, XMLByte&);
181    static void split2ndOctet(const XMLByte&, XMLByte&, XMLByte&);
182    static void split3rdOctet(const XMLByte&, XMLByte&, XMLByte&);
183
184    // -----------------------------------------------------------------------
185    //  Unimplemented constructors and operators
186    // -----------------------------------------------------------------------
187    Base64();
188    Base64(const Base64&);
189
190    // -----------------------------------------------------------------------
191    //  Private data members
192    //
193    //  base64Alphabet
194    //     The Base64 alphabet (see RFC 2045).
195    //
196    //  base64Padding
197    //     Padding character (see RFC 2045).
198    //
199    //  base64Inverse
200    //     Table used in decoding base64.
201    //
202    //  isInitialized
203    //     Set once base64Inverse is initalized.
204    //
205    //  quadsPerLine
206    //     Number of quadruplets per one line. The encoded output
207    //     stream must be represented in lines of no more
208    //     than 19 quadruplets each.
209    //
210    // -----------------------------------------------------------------------
211
212    static const XMLByte  base64Alphabet[];
213    static const XMLByte  base64Padding;
214
215    static XMLByte  base64Inverse[];
216    static bool  isInitialized;
217
218    static const unsigned int  quadsPerLine;
219};
220
221// -----------------------------------------------------------------------
222//  Helper methods
223// -----------------------------------------------------------------------
224inline bool Base64::isPad(const XMLByte& octet)
225{
226    return ( octet == base64Padding );
227}
228
229inline XMLByte Base64::set1stOctet(const XMLByte& b1, const XMLByte& b2)
230{
231    return (( b1 << 2 ) | ( b2 >> 4 ));
232}
233
234inline XMLByte Base64::set2ndOctet(const XMLByte& b2, const XMLByte& b3)
235{
236    return (( b2 << 4 ) | ( b3 >> 2 ));
237}
238
239inline XMLByte Base64::set3rdOctet(const XMLByte& b3, const XMLByte& b4)
240{
241    return (( b3 << 6 ) | b4 );
242}
243
244inline void Base64::split1stOctet(const XMLByte& ch, XMLByte& b1, XMLByte& b2) {
245    b1 = ch >> 2;
246    b2 = ( ch & 0x3 ) << 4;
247}
248
249inline void Base64::split2ndOctet(const XMLByte& ch, XMLByte& b2, XMLByte& b3) {
250    b2 |= ch >> 4;  // combine with previous value
251    b3 = ( ch & 0xf ) << 2;
252}
253
254inline void Base64::split3rdOctet(const XMLByte& ch, XMLByte& b3, XMLByte& b4) {
255    b3 |= ch >> 6;  // combine with previous value
256    b4 = ( ch & 0x3f );
257}
258
259XERCES_CPP_NAMESPACE_END
260
261#endif
Note: See TracBrowser for help on using the repository browser.