diff --git a/src/xercesc/framework/psvi/XSValue.cpp b/src/xercesc/framework/psvi/XSValue.cpp index 34d94b0c1c96c7fe8b5fd42c68bb04c9ff43f542..89c1190342b0aec820f5fd648cd52224f9f46962 100644 --- a/src/xercesc/framework/psvi/XSValue.cpp +++ b/src/xercesc/framework/psvi/XSValue.cpp @@ -16,6 +16,9 @@ /* * $Log$ + * Revision 1.20 2004/12/10 10:37:55 cargilld + * Fix problem with hexbin::decode and use XMLByte instead of XMLCh for output of decoding. + * * Revision 1.19 2004/12/01 16:18:47 cargilld * Fix for bug xercesc-1304. * @@ -379,7 +382,7 @@ XSValue::XSValue(DataType const dt XSValue::~XSValue() { if (fMemAllocated) - fMemoryManager->deallocate(fData.fValue.f_strVal); + fMemoryManager->deallocate(fData.fValue.f_byteVal); } // --------------------------------------------------------------------------- @@ -1523,7 +1526,7 @@ XSValue::getActValStrings(const XMLCh* const content break; case XSValue::dt_hexBinary: { - XMLCh* decodedData = HexBin::decode(content, manager); + XMLByte* decodedData = HexBin::decodeToXMLByte(content, manager); if (!decodedData) { @@ -1532,7 +1535,7 @@ XSValue::getActValStrings(const XMLCh* const content } XSValue* retVal = new (manager) XSValue(dt_hexBinary, manager); - retVal->fData.fValue.f_strVal = decodedData; + retVal->fData.fValue.f_byteVal = decodedData; retVal->fMemAllocated = true; return retVal; break; @@ -1540,7 +1543,7 @@ XSValue::getActValStrings(const XMLCh* const content case XSValue::dt_base64Binary: { unsigned int len = 0; - XMLCh* decodedData = Base64::decode(content, &len, manager); + XMLByte* decodedData = Base64::decodeToXMLByte(content, &len, manager); if (!decodedData) { @@ -1549,7 +1552,7 @@ XSValue::getActValStrings(const XMLCh* const content } XSValue* retVal = new (manager) XSValue(dt_base64Binary, manager); - retVal->fData.fValue.f_strVal = decodedData; + retVal->fData.fValue.f_byteVal = decodedData; retVal->fMemAllocated = true; return retVal; break; diff --git a/src/xercesc/framework/psvi/XSValue.hpp b/src/xercesc/framework/psvi/XSValue.hpp index b81ca446d0de5d08462c586e17fe45610f5e3c9a..a89eacca7a3e554d14bf0eef9969fda24a570b3c 100644 --- a/src/xercesc/framework/psvi/XSValue.hpp +++ b/src/xercesc/framework/psvi/XSValue.hpp @@ -16,6 +16,9 @@ /* * $Log$ + * Revision 1.18 2004/12/10 10:37:55 cargilld + * Fix problem with hexbin::decode and use XMLByte instead of XMLCh for output of decoding. + * * Revision 1.17 2004/11/24 10:18:59 cargilld * Fix compilation error on HP for yesterday's commit. * @@ -261,6 +264,7 @@ public: float f_float; double f_double; XMLCh* f_strVal; + XMLByte* f_byteVal; struct decimal { double f_dvalue; diff --git a/src/xercesc/util/Base64.cpp b/src/xercesc/util/Base64.cpp index 9db83e2e8bdc4aa91d28857d7385a2d866efe82d..a0a31ba1e30aba6fa1b24bece0163adda0b229aa 100644 --- a/src/xercesc/util/Base64.cpp +++ b/src/xercesc/util/Base64.cpp @@ -16,6 +16,9 @@ /* * $Log$ + * Revision 1.16 2004/12/10 10:37:56 cargilld + * Fix problem with hexbin::decode and use XMLByte instead of XMLCh for output of decoding. + * * Revision 1.15 2004/09/08 13:56:21 peiyongz * Apache License Version 2.0 * @@ -299,7 +302,7 @@ int Base64::getDataLength(const XMLCh* const inputData { unsigned int retLen = 0; - XMLCh* decodedData = decode(inputData, &retLen, manager, conform); + XMLByte* decodedData = decodeToXMLByte(inputData, &retLen, manager, conform); if ( !decodedData ) return -1; @@ -379,6 +382,33 @@ XMLCh* Base64::decode(const XMLCh* const inputData return toRet; } +XMLByte* Base64::decodeToXMLByte(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; + return decode(dataInByte, decodedLen, memMgr, conform); +} + /*** * E2-54 * diff --git a/src/xercesc/util/Base64.hpp b/src/xercesc/util/Base64.hpp index df6786448afd84a80c1d11f7cc66bac4313d8ded..0f53c2dd4538e6cc03a98b47730f3650d072448f 100644 --- a/src/xercesc/util/Base64.hpp +++ b/src/xercesc/util/Base64.hpp @@ -113,14 +113,40 @@ public : * @return Decoded binary data in XMLCh stream, * or NULL if input data can not be decoded. * @see XMLString::release(XMLCh**) + * @deprecated use decodeToXMLByte instead. */ + static XMLCh* decode( const XMLCh* const inputData , unsigned int* decodedLength , MemoryManager* const memMgr = 0 , Conformance conform = Conf_RFC2045 ); - + + /** + * Decodes Base64 data into octets + * + * NOTE: The returned buffer is dynamically allocated and is the + * responsibility of the caller to delete it when not longer needed. + * You can call XMLString::release to release this returned buffer. + * + * If a memory manager is provided, ask the memory manager to de-allocate + * the returned buffer. + * + * @param inputData Base64 data in XMLCh stream. + * @param decodedLength Length of decoded XMLByte stream. + * @param memMgr client provided memory manager + * @param conform conformance specified + * @return Decoded binary data in XMLByte stream, + * or NULL if input data can not be decoded. + * @see XMLString::release(XMLByte**) + */ + static XMLByte* decodeToXMLByte( + const XMLCh* const inputData + , unsigned int* decodedLength + , MemoryManager* const memMgr = 0 + , Conformance conform = Conf_RFC2045 + ); /** * Get data length * diff --git a/src/xercesc/util/HexBin.cpp b/src/xercesc/util/HexBin.cpp index 3ff40055df0265c90241304a01323f668de9afbd..c1bd7b9cc2bff907a46105e528127932740e6b57 100644 --- a/src/xercesc/util/HexBin.cpp +++ b/src/xercesc/util/HexBin.cpp @@ -16,6 +16,9 @@ /* * $Log$ + * Revision 1.6 2004/12/10 10:37:56 cargilld + * Fix problem with hexbin::decode and use XMLByte instead of XMLCh for output of decoding. + * * Revision 1.5 2004/09/08 13:56:22 peiyongz * Apache License Version 2.0 * @@ -54,7 +57,7 @@ static const int BASELENGTH = 255; // --------------------------------------------------------------------------- // class data member // --------------------------------------------------------------------------- -bool HexBin::hexNumberTable[BASELENGTH]; +XMLByte HexBin::hexNumberTable[BASELENGTH]; bool HexBin::isInitialized = false; int HexBin::getDataLength(const XMLCh* const hexData) @@ -97,7 +100,6 @@ XMLCh* HexBin::getCanonicalRepresentation(const XMLCh* const hexData return retStr; } - XMLCh* HexBin::decode(const XMLCh* const hexData , MemoryManager* const manager) { @@ -112,29 +114,61 @@ XMLCh* HexBin::decode(const XMLCh* const hexData init(); //prepare the return string - XMLCh *retVal = (XMLCh*) manager->allocate( (strLen/2 + 1) * sizeof(XMLCh)); + int decodeLength = strLen/2; + XMLCh *retVal = (XMLCh*) manager->allocate( (decodeLength + 1) * sizeof(XMLCh)); ArrayJanitor<XMLCh> janFill(retVal, manager); + + XMLByte temp1, temp2; + for( int i = 0; i<decodeLength; i++ ) { + temp1 = hexNumberTable[hexData[i*2]]; + if (temp1 == (XMLByte) -1) + return 0; + temp2 = hexNumberTable[hexData[i*2+1]]; + if (temp2 == (XMLByte) -1) + return 0; + retVal[i] = (XMLCh)((temp1 << 4) | temp2); + } - for ( int i = 0; i < strLen; ) - { - if( !isHex(hexData[i]) || - !isHex(hexData[i+1]) ) + janFill.release(); + retVal[decodeLength] = 0; + return retVal; +} + +XMLByte* HexBin::decodeToXMLByte(const XMLCh* const hexData + , MemoryManager* const manager) +{ + if (( hexData == 0 ) || ( *hexData == 0 )) // zero length + return 0; + + int strLen = XMLString::stringLen(hexData); + if ( strLen%2 != 0 ) + return 0; + + if ( !isInitialized ) + init(); + + //prepare the return string + int decodeLength = strLen/2; + XMLByte *retVal = (XMLByte*) manager->allocate( (decodeLength + 1) * sizeof(XMLByte)); + ArrayJanitor<XMLByte> janFill(retVal, manager); + + XMLByte temp1, temp2; + for( int i = 0; i<decodeLength; i++ ) { + temp1 = hexNumberTable[hexData[i*2]]; + if (temp1 == (XMLByte) -1) return 0; - else - { - retVal[i/2] = (XMLCh)( - (((XMLByte) hexData[i]) << 4 ) | - ((XMLByte) hexData[i+1]) - ); - i+=2; - } + temp2 = hexNumberTable[hexData[i*2+1]]; + if (temp2 == (XMLByte) -1) + return 0; + retVal[i] = ((temp1 << 4) | temp2); } janFill.release(); - retVal[strLen/2] = 0; + retVal[decodeLength] = 0; return retVal; } + // ----------------------------------------------------------------------- // Helper methods // ----------------------------------------------------------------------- @@ -144,7 +178,7 @@ bool HexBin::isHex(const XMLCh& octet) if ( octet >= BASELENGTH ) return false; - return (hexNumberTable[octet]); + return (hexNumberTable[octet] != (XMLByte) -1); } void HexBin::init() @@ -154,16 +188,16 @@ void HexBin::init() int i; for ( i = 0; i < BASELENGTH; i++ ) - hexNumberTable[i] = false; + hexNumberTable[i] = -1; for ( i = chDigit_9; i >= chDigit_0; i-- ) - hexNumberTable[i] = true; + hexNumberTable[i] = (XMLByte) (i - chDigit_0); for ( i = chLatin_F; i >= chLatin_A; i-- ) - hexNumberTable[i] = true; - + hexNumberTable[i] = (XMLByte) (i - chLatin_A + 10); + for ( i = chLatin_f; i >= chLatin_a; i-- ) - hexNumberTable[i] = true; + hexNumberTable[i] = (XMLByte) (i - chLatin_a + 10); isInitialized = true; } diff --git a/src/xercesc/util/HexBin.hpp b/src/xercesc/util/HexBin.hpp index fa1edc9bb2746a642fe16d158a104552eb4a98fb..b51e1805d5a4f7c3f3a906d47fb7819f39973792 100644 --- a/src/xercesc/util/HexBin.hpp +++ b/src/xercesc/util/HexBin.hpp @@ -87,12 +87,35 @@ public : * @return Decoded binary data in XMLCh stream, * or NULL if input data can not be decoded. * @see XMLString::release(XMLCh**) + * @deprecated use decodeToXMLByte instead. */ + static XMLCh* decode( const XMLCh* const hexData , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager ); + /** + * Decodes HexBinary data into XMLByte + * + * NOTE: The returned buffer is dynamically allocated and is the + * responsibility of the caller to delete it when not longer needed. + * You can call XMLString::release to release this returned buffer. + * + * If a memory manager is provided, ask the memory manager to de-allocate + * the returned buffer. + * + * @param hexData HexBinary data in XMLCh stream. + * @param manager client provided memory manager + * @return Decoded binary data in XMLByte stream, + * or NULL if input data can not be decoded. + * @see XMLString::release(XMLByte**) + */ + static XMLByte* decodeToXMLByte( + const XMLCh* const hexData + , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager + ); + //@} @@ -126,7 +149,7 @@ private : // // ----------------------------------------------------------------------- static bool isInitialized; - static bool hexNumberTable[]; + static XMLByte hexNumberTable[]; }; XERCES_CPP_NAMESPACE_END