📄 base64.cpp
字号:
/* * Copyright 2001,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Log: Base64.cpp,v $ * Revision 1.15 2004/09/08 13:56:21 peiyongz * Apache License Version 2.0 * * Revision 1.14 2004/08/17 21:10:33 peiyongz * fix bug in getting CanRep from decode() * * Revision 1.13 2004/08/11 16:47:32 peiyongz * Decoding and getCanRep * * Revision 1.12 2004/06/24 15:00:37 peiyongz * Schema-Errata: E2-54 new specs for base64 * * Revision 1.11 2003/12/17 00:18:35 cargilld * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data. * * Revision 1.10 2003/05/20 22:10:02 peiyongz * To differentiate external/internal memory * * Revision 1.8 2003/01/27 21:15:56 peiyongz * only zero or one space allowed in between B64 character. * * Revision 1.7 2002/12/20 22:10:20 tng * XML 1.1 * * Revision 1.6 2002/11/25 18:14:35 peiyongz * Schema Errata: E2-9 Base64 * * Revision 1.5 2002/11/04 15:22:03 tng * C++ Namespace Support. * * Revision 1.4 2002/03/19 17:01:20 peiyongz * Fix to Bug#7243 Base64 encoding is not working. * * Revision 1.3 2002/02/18 16:07:38 peiyongz * fix: "i" redefined on line 428 reported by compilers on some UNIX platforms * * Revision 1.2 2002/02/15 21:36:56 peiyongz * Interface redefined for conversion in XMLByte * * Revision 1.1.1.1 2002/02/01 22:22:09 peiyongz * sane_include * * Revision 1.6 2001/10/15 19:42:16 knoaman * Null-terminate base64Alphabet. * * Revision 1.5 2001/10/10 19:14:08 peiyongz * Patch from Petr Gotthard : encode() provided and some other changes * * Revision 1.4 2001/06/07 20:55:20 tng * Fix no newline at the end warning. By Pei Yong Zhang. * * Revision 1.3 2001/05/28 21:11:16 tng * Schema: Various DatatypeValidator fix. By Pei Yong Zhang * * Revision 1.2 2001/05/16 19:01:04 tng * Schema: Typo fix in Base64 * * Revision 1.1 2001/05/16 15:25:36 tng * Schema: Add Base64 and HexBin. By Pei Yong Zhang. * */// ---------------------------------------------------------------------------// Includes// ---------------------------------------------------------------------------#include <xercesc/util/Base64.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/Janitor.hpp>#include <xercesc/internal/XMLReader.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------// constants// ---------------------------------------------------------------------------static const int BASELENGTH = 255;static const int FOURBYTE = 4;// ---------------------------------------------------------------------------// class data member// ---------------------------------------------------------------------------// the base64 alphabet according to definition in RFC 2045const XMLByte Base64::base64Alphabet[] = { chLatin_A, chLatin_B, chLatin_C, chLatin_D, chLatin_E, chLatin_F, chLatin_G, chLatin_H, chLatin_I, chLatin_J, chLatin_K, chLatin_L, chLatin_M, chLatin_N, chLatin_O, chLatin_P, chLatin_Q, chLatin_R, chLatin_S, chLatin_T, chLatin_U, chLatin_V, chLatin_W, chLatin_X, chLatin_Y, chLatin_Z, chLatin_a, chLatin_b, chLatin_c, chLatin_d, chLatin_e, chLatin_f, chLatin_g, chLatin_h, chLatin_i, chLatin_j, chLatin_k, chLatin_l, chLatin_m, chLatin_n, chLatin_o, chLatin_p, chLatin_q, chLatin_r, chLatin_s, chLatin_t, chLatin_u, chLatin_v, chLatin_w, chLatin_x, chLatin_y, chLatin_z, chDigit_0, chDigit_1, chDigit_2, chDigit_3, chDigit_4, chDigit_5, chDigit_6, chDigit_7, chDigit_8, chDigit_9, chPlus, chForwardSlash, chNull};XMLByte Base64::base64Inverse[BASELENGTH];const XMLByte Base64::base64Padding = chEqual;bool Base64::isInitialized = false;/*** * * Memory Management Issue: * * . For memory allocated for result returned to caller (external memory), * the plugged memory manager is used if it is provided, otherwise global * new used to retain the pre-memory-manager behaviour. * * . For memory allocated for temperary buffer (internal memory), * XMLPlatformUtils::fgMemoryManager is used. * */static inline void* getExternalMemory(MemoryManager* const allocator , unsigned int const sizeToAllocate){ return allocator ? allocator->allocate(sizeToAllocate) : ::operator new(sizeToAllocate);}static inline void* getInternalMemory(unsigned int const sizeToAllocate){ return XMLPlatformUtils::fgMemoryManager->allocate(sizeToAllocate);}/*** * internal memory is deallocated by janitorArray */ static inline void returnExternalMemory(MemoryManager* const allocator , void* buffer){ allocator ? allocator->deallocate(buffer) : ::operator delete(buffer);}/** * E2-9 * * Canonical-base64Binary ::= (B64line* B64lastline)? * * B64line ::= B64x15 B64x15 B64x15 B64x15 B64x15 B64 #xA * 76 Base64 characters followed by newline * * B64x15 ::= B64 B64 B64 B64 B64 * B64 B64 B64 B64 B64 * B64 B64 B64 B64 B64 * * B64lastline ::= B64x4? B64x4? B64x4? B64x4? * B64x4? B64x4? B64x4? B64x4? * B64x4? B64x4? B64x4? B64x4? * B64x4? B64x4? B64x4? B64x4? * B64x4? B64x4? * (B64x4 | (B64 B64 B16 '=') | (B64 B04 '==')) * #xA * * B64x4 ::= B64 B64 B64 B64 * B04 ::= [AQgw] * B16 ::= [AEIMQUYcgkosw048]*/// number of quadruplets per one line ( must be >1 and <19 )const unsigned int Base64::quadsPerLine = 15;XMLByte* Base64::encode(const XMLByte* const inputData , const unsigned int inputLength , unsigned int* outputLength , MemoryManager* const memMgr){ if (!isInitialized) init(); if (!inputData || !outputLength) return 0; int quadrupletCount = ( inputLength + 2 ) / 3; if (quadrupletCount == 0) return 0; // number of rows in encoded stream ( including the last one ) int lineCount = ( quadrupletCount + quadsPerLine-1 ) / quadsPerLine; // // convert the triplet(s) to quadruplet(s) // XMLByte b1, b2, b3, b4; // base64 binary codes ( 0..63 ) unsigned int inputIndex = 0; unsigned int outputIndex = 0; XMLByte *encodedData = (XMLByte*) getExternalMemory(memMgr, (quadrupletCount*FOURBYTE+lineCount+1) * sizeof(XMLByte)); // // Process all quadruplet(s) except the last // int quad = 1; for (; quad <= quadrupletCount-1; quad++ ) { // read triplet from the input stream split1stOctet( inputData[ inputIndex++ ], b1, b2 ); split2ndOctet( inputData[ inputIndex++ ], b2, b3 ); split3rdOctet( inputData[ inputIndex++ ], b3, b4 ); // write quadruplet to the output stream encodedData[ outputIndex++ ] = base64Alphabet[ b1 ]; encodedData[ outputIndex++ ] = base64Alphabet[ b2 ]; encodedData[ outputIndex++ ] = base64Alphabet[ b3 ]; encodedData[ outputIndex++ ] = base64Alphabet[ b4 ]; if (( quad % quadsPerLine ) == 0 ) encodedData[ outputIndex++ ] = chLF; } // // process the last Quadruplet // // first octet is present always, process it split1stOctet( inputData[ inputIndex++ ], b1, b2 ); encodedData[ outputIndex++ ] = base64Alphabet[ b1 ]; if( inputIndex < inputLength ) { // second octet is present, process it split2ndOctet( inputData[ inputIndex++ ], b2, b3 ); encodedData[ outputIndex++ ] = base64Alphabet[ b2 ]; if( inputIndex < inputLength )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -