📄 base64.cpp
字号:
{ // third octet present, process it // no PAD e.g. 3cQl split3rdOctet( inputData[ inputIndex++ ], b3, b4 ); encodedData[ outputIndex++ ] = base64Alphabet[ b3 ]; encodedData[ outputIndex++ ] = base64Alphabet[ b4 ]; } else { // third octet not present // one PAD e.g. 3cQ= encodedData[ outputIndex++ ] = base64Alphabet[ b3 ]; encodedData[ outputIndex++ ] = base64Padding; } } else { // second octet not present // two PADs e.g. 3c== encodedData[ outputIndex++ ] = base64Alphabet[ b2 ]; encodedData[ outputIndex++ ] = base64Padding; encodedData[ outputIndex++ ] = base64Padding; } // write out end of the last line encodedData[ outputIndex++ ] = chLF; // write out end of string encodedData[ outputIndex ] = 0; *outputLength = outputIndex; return encodedData;}//// delete the buffer allocated by decode() if// decoding is successfully done.//// In previous version, we use XMLString::strLen(decodedData)// to get the length, this will fail for test case containing// consequtive "A", such "AAFF", or "ab56AA56". Instead of// returning 3/6, we have 0 and 3, indicating that "AA", after// decoded, is interpreted as <null> by the strLen().//// Since decode() has track of length of the decoded data, we// will get this length from decode(), instead of strLen().//int Base64::getDataLength(const XMLCh* const inputData , MemoryManager* const manager , Conformance conform ){ unsigned int retLen = 0; XMLCh* decodedData = decode(inputData, &retLen, manager, conform); if ( !decodedData ) return -1; else { returnExternalMemory(manager, decodedData); return retLen; }}XMLByte* Base64::decode(const XMLByte* const inputData , unsigned int* decodedLength , MemoryManager* const memMgr , Conformance conform ){ XMLByte* canRepInByte = 0; XMLByte* retStr = decode( inputData , decodedLength , canRepInByte , memMgr , conform); /*** * Release the canRepData */ if (retStr) returnExternalMemory(memMgr, canRepInByte); return retStr;}XMLCh* Base64::decode(const XMLCh* const inputData , unsigned int* decodedLen , MemoryManager* const memMgr , Conformance conform ){ if (!inputData) return 0; /*** * Move input data to a XMLByte buffer */ unsigned int srcLen = XMLString::stringLen(inputData); XMLByte *dataInByte = (XMLByte*) getExternalMemory(memMgr, (srcLen+1) * sizeof(XMLByte)); ArrayJanitor<XMLByte> janFill(dataInByte, memMgr ? memMgr : XMLPlatformUtils::fgMemoryManager); for (unsigned int i = 0; i < srcLen; i++) dataInByte[i] = (XMLByte)inputData[i]; dataInByte[srcLen] = 0; /*** * Forward to the actual decoding method to do the decoding */ *decodedLen = 0; XMLByte *DecodedBuf = decode(dataInByte, decodedLen, memMgr, conform); if (!DecodedBuf) return 0; /*** * Move decoded data to a XMLCh buffer to return */ XMLCh *toRet = (XMLCh*) getExternalMemory(memMgr, (*decodedLen+1) * sizeof(XMLCh)); for (unsigned int j = 0; j < *decodedLen; j++) toRet[j] = (XMLCh)DecodedBuf[j]; toRet[*decodedLen] = 0; /*** * Release the memory allocated in the actual decoding method */ returnExternalMemory(memMgr, DecodedBuf); return toRet;}/**** E2-54** Canonical-base64Binary ::= (B64 B64 B64 B64)*((B64 B64 B16 '=')|(B64 B04 '=='))? * B04 ::= [AQgw]* B16 ::= [AEIMQUYcgkosw048]* B64 ::= [A-Za-z0-9+/] ****/XMLCh* Base64::getCanonicalRepresentation(const XMLCh* const inputData , MemoryManager* const memMgr , Conformance conform) { if (!inputData || !*inputData) return 0; /*** * Move input data to a XMLByte buffer */ unsigned int srcLen = XMLString::stringLen(inputData); XMLByte *dataInByte = (XMLByte*) getExternalMemory(memMgr, (srcLen+1) * sizeof(XMLByte)); ArrayJanitor<XMLByte> janFill(dataInByte, memMgr ? memMgr : XMLPlatformUtils::fgMemoryManager); for (unsigned int i = 0; i < srcLen; i++) dataInByte[i] = (XMLByte)inputData[i]; dataInByte[srcLen] = 0; /*** * Forward to the actual decoding method to do the decoding */ unsigned int decodedLength = 0; XMLByte* canRepInByte = 0; XMLByte* retStr = decode( dataInByte , &decodedLength , canRepInByte , memMgr , conform); if (!retStr) return 0; /*** * Move canonical representation to a XMLCh buffer to return */ unsigned int canRepLen = XMLString::stringLen((char*)canRepInByte); XMLCh *canRepData = (XMLCh*) getExternalMemory(memMgr, (canRepLen + 1) * sizeof(XMLCh)); for (unsigned int j = 0; j < canRepLen; j++) canRepData[j] = (XMLCh)canRepInByte[j]; canRepData[canRepLen] = 0; /*** * Release the memory allocated in the actual decoding method */ returnExternalMemory(memMgr, retStr); returnExternalMemory(memMgr, canRepInByte); return canRepData;}// -----------------------------------------------------------------------// Helper methods// -----------------------------------------------------------------------//// return 0(null) if invalid data found.// return the buffer containning decoded data otherwise// the caller is responsible for the de-allocation of the// buffer returned.//// temporary data, rawInputData, is ALWAYS released by this function.///*** * E2-9 * * Base64Binary ::= S? B64quartet* B64final? * * B64quartet ::= B64 S? B64 S? B64 S? B64 S? * * B64final ::= B64 S? B04 S? '=' S? '=' S? * | B64 S? B64 S? B16 S? '=' S? * * B04 ::= [AQgw] * B16 ::= [AEIMQUYcgkosw048] * B64 ::= [A-Za-z0-9+/] * * * E2-54 * * Base64Binary ::= ((B64S B64S B64S B64S)* * ((B64S B64S B64S B64) | * (B64S B64S B16S '=') | * (B64S B04S '=' #x20? '=')))? * * B64S ::= B64 #x20? * B16S ::= B16 #x20? * B04S ::= B04 #x20? * * * Note that this grammar requires the number of non-whitespace characters * in the lexical form to be a multiple of four, and for equals signs to * appear only at the end of the lexical form; strings which do not meet these * constraints are not legal lexical forms of base64Binary because they * cannot successfully be decoded by base64 decoders. * * Note: * The above definition of the lexical space is more restrictive than that given * in [RFC 2045] as regards whitespace -- this is not an issue in practice. Any * string compatible with the RFC can occur in an element or attribute validated * by this type, because the 穡hiteSpace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -