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