From c768ad54a5bbe96a6ad084f2c1ac8a4f29cf61b3 Mon Sep 17 00:00:00 2001
From: David N Bertoni <dbertoni@apache.org>
Date: Fri, 20 Jan 2006 22:05:46 +0000
Subject: [PATCH] Fixes for Jira issue XERCESC-1548.

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@370907 13f79535-47bb-0310-9956-ffa450edef68
---
 src/xercesc/util/Base64.cpp | 128 +++++++++++++++++-------------------
 src/xercesc/util/Base64.hpp |   9 +--
 src/xercesc/util/HexBin.cpp |  56 ++++++----------
 src/xercesc/util/HexBin.hpp |   9 +--
 4 files changed, 81 insertions(+), 121 deletions(-)

diff --git a/src/xercesc/util/Base64.cpp b/src/xercesc/util/Base64.cpp
index c1e93acd8..e6bc3d64e 100644
--- a/src/xercesc/util/Base64.cpp
+++ b/src/xercesc/util/Base64.cpp
@@ -1,4 +1,4 @@
-/*
+锘�/*
  * Copyright 2001,2004 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,10 +14,6 @@
  * limitations under the License.
  */
 
-/*
- * $Id$
- */
-
 // ---------------------------------------------------------------------------
 //  Includes
 // ---------------------------------------------------------------------------
@@ -46,22 +42,43 @@ const XMLByte Base64::base64Alphabet[] = {
     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
+    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];
+// This is an inverse table for base64 decoding.  So, if
+// base64Alphabet[17] = 'R', then base64Inverse['R'] = 17.
+//
+// For characters not in base64Alphabet then
+// base64Inverse[ch] = 0xFF.
+const XMLByte Base64::base64Inverse[BASELENGTH] =
+{
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F,
+    0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
+    0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xFF, 0xFF,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-
 /***
  *
  * Memory Management Issue:
@@ -130,9 +147,6 @@ XMLByte* Base64::encode(const XMLByte* const inputData
                       , unsigned int*        outputLength                      
                       , MemoryManager* const memMgr)
 {
-    if (!isInitialized)
-        init();
-
     if (!inputData || !outputLength)
         return 0;
 
@@ -278,29 +292,29 @@ XMLCh* Base64::decode(const XMLCh*         const   inputData
                     ,       MemoryManager* const   memMgr
                     ,       Conformance            conform )
 {
-	if (!inputData)
-		return 0;
+    if (!inputData)
+        return 0;
 
     /***
      *  Move input data to a XMLByte buffer
      */
-	unsigned int srcLen = XMLString::stringLen(inputData);
+    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[i] = (XMLByte)inputData[i];
 
-	dataInByte[srcLen] = 0;
+    dataInByte[srcLen] = 0;
 
     /***
      * Forward to the actual decoding method to do the decoding
      */
-	*decodedLen = 0;
-	XMLByte *DecodedBuf = decode(dataInByte, decodedLen, memMgr, conform);
+    *decodedLen = 0;
+    XMLByte *DecodedBuf = decode(dataInByte, decodedLen, memMgr, conform);
 
-	if (!DecodedBuf)
-		return 0;
+    if (!DecodedBuf)
+        return 0;
 
     /***
      * Move decoded data to a XMLCh buffer to return
@@ -308,9 +322,9 @@ XMLCh* Base64::decode(const XMLCh*         const   inputData
     XMLCh *toRet = (XMLCh*) getExternalMemory(memMgr, (*decodedLen+1) * sizeof(XMLCh));
                
     for (unsigned int j = 0; j < *decodedLen; j++)
-		toRet[j] = (XMLCh)DecodedBuf[j];
+        toRet[j] = (XMLCh)DecodedBuf[j];
 
-	toRet[*decodedLen] = 0;
+    toRet[*decodedLen] = 0;
 
     /***
      * Release the memory allocated in the actual decoding method
@@ -325,26 +339,26 @@ XMLByte* Base64::decodeToXMLByte(const XMLCh*         const   inputData
                     ,       MemoryManager* const   memMgr
                     ,       Conformance            conform )
 {
-	if (!inputData || !*inputData)
-		return 0;
+    if (!inputData || !*inputData)
+        return 0;
 
     /***
      *  Move input data to a XMLByte buffer
      */
-	unsigned int srcLen = XMLString::stringLen(inputData);
+    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[i] = (XMLByte)inputData[i];
 
-	dataInByte[srcLen] = 0;
+    dataInByte[srcLen] = 0;
 
     /***
      * Forward to the actual decoding method to do the decoding
      */
-	*decodedLen = 0;
-	return decode(dataInByte, decodedLen, memMgr, conform);
+    *decodedLen = 0;
+    return decode(dataInByte, decodedLen, memMgr, conform);
 }
 
 /***
@@ -361,25 +375,25 @@ XMLCh* Base64::getCanonicalRepresentation(const XMLCh*         const   inputData
                                         ,       Conformance            conform)
     
 {
-	if (!inputData || !*inputData) 
-		return 0;
+    if (!inputData || !*inputData) 
+        return 0;
 
     /***
      *  Move input data to a XMLByte buffer
      */
-	unsigned int srcLen = XMLString::stringLen(inputData);
+    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[i] = (XMLByte)inputData[i];
 
-	dataInByte[srcLen] = 0;
+    dataInByte[srcLen] = 0;
 
     /***
      * Forward to the actual decoding method to do the decoding
      */
-	unsigned int decodedLength = 0;
+    unsigned int decodedLength = 0;
     XMLByte*     canRepInByte = 0;
     XMLByte*     retStr = decode(
                               dataInByte
@@ -398,9 +412,9 @@ XMLCh* Base64::getCanonicalRepresentation(const XMLCh*         const   inputData
     XMLCh *canRepData = (XMLCh*) getExternalMemory(memMgr, (canRepLen + 1) * sizeof(XMLCh));
                
     for (unsigned int j = 0; j < canRepLen; j++)
-		canRepData[j] = (XMLCh)canRepInByte[j];
+        canRepData[j] = (XMLCh)canRepInByte[j];
 
-	canRepData[canRepLen] = 0;
+    canRepData[canRepLen] = 0;
 
     /***
      * Release the memory allocated in the actual decoding method
@@ -474,9 +488,6 @@ XMLByte* Base64::decode (   const XMLByte*        const   inputData
                           ,       Conformance             conform
                         )
 {
-    if (!isInitialized)
-        init();
-
     if ((!inputData) || (!*inputData))
         return 0;
     
@@ -667,27 +678,6 @@ XMLByte* Base64::decode (   const XMLByte*        const   inputData
     return decodedData;
 }
 
-void Base64::init()
-{
-    if (isInitialized)
-        return;
-
-    isInitialized = true;
-
-    // create inverse table for base64 decoding
-    // if base64Alphabet[ 17 ] = 'R', then base64Inverse[ 'R' ] = 17
-    // for characters not in base64Alphabet the base64Inverse[] = -1
-
-    int i;
-    // set all fields to -1
-    for ( i = 0; i < BASELENGTH; i++ )
-        base64Inverse[i] = (XMLByte)-1;
-
-    // compute inverse table
-    for ( i = 0; i < 64; i++ )
-        base64Inverse[ base64Alphabet[i] ] = (XMLByte)i;
-}
-
 bool Base64::isData(const XMLByte& octet)
 {
     return (base64Inverse[octet]!=(XMLByte)-1);
diff --git a/src/xercesc/util/Base64.hpp b/src/xercesc/util/Base64.hpp
index a8f17fbad..00261604d 100644
--- a/src/xercesc/util/Base64.hpp
+++ b/src/xercesc/util/Base64.hpp
@@ -14,10 +14,6 @@
  * limitations under the License.
  */
 
-/*
- * $Id$
- */
-
 #ifndef BASE64_HPP
 #define BASE64_HPP
 
@@ -215,8 +211,6 @@ private :
                          ,       Conformance             conform = Conf_RFC2045
                           );
 
-    static void init();
-
     static bool isData(const XMLByte& octet);
     static bool isPad(const XMLByte& octet);
 
@@ -259,8 +253,7 @@ private :
     static const XMLByte  base64Alphabet[];
     static const XMLByte  base64Padding;
 
-    static XMLByte  base64Inverse[];
-    static bool  isInitialized;
+    static const XMLByte  base64Inverse[];
 
     static const unsigned int  quadsPerLine;
 };
diff --git a/src/xercesc/util/HexBin.cpp b/src/xercesc/util/HexBin.cpp
index 576fac911..7fee5d1fb 100644
--- a/src/xercesc/util/HexBin.cpp
+++ b/src/xercesc/util/HexBin.cpp
@@ -14,10 +14,6 @@
  * limitations under the License.
  */
 
-/*
- * $Id$
- */
-
 // ---------------------------------------------------------------------------
 //  Includes
 // ---------------------------------------------------------------------------
@@ -36,8 +32,26 @@ static const int BASELENGTH = 255;
 // ---------------------------------------------------------------------------
 //  class data member
 // ---------------------------------------------------------------------------
-XMLByte HexBin::hexNumberTable[BASELENGTH];
-bool HexBin::isInitialized = false;
+const XMLByte HexBin::hexNumberTable[BASELENGTH] =
+{
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 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FF, 0xFF, 0xFF
+};
+
 
 int HexBin::getDataLength(const XMLCh* const hexData)
 {
@@ -49,9 +63,6 @@ int HexBin::getDataLength(const XMLCh* const hexData)
 
 bool HexBin::isArrayByteHex(const XMLCh* const hexData)
 {
-    if ( !isInitialized )
-        init();
-
     if (( hexData == 0 ) || ( *hexData == 0 )) // zero length
         return true;
 
@@ -89,9 +100,6 @@ XMLCh* HexBin::decode(const XMLCh*          const   hexData
     if ( strLen%2 != 0 )
         return 0;
 
-    if ( !isInitialized )
-        init();
-
     //prepare the return string
     int decodeLength = strLen/2;
     XMLCh *retVal = (XMLCh*) manager->allocate( (decodeLength + 1) * sizeof(XMLCh));
@@ -123,9 +131,6 @@ XMLByte* HexBin::decodeToXMLByte(const XMLCh*          const   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));
@@ -160,25 +165,4 @@ bool HexBin::isHex(const XMLCh& octet)
     return (hexNumberTable[octet] != (XMLByte) -1);
 }
 
-void HexBin::init()
-{
-    if ( isInitialized )
-        return;
-
-    int i;
-    for ( i = 0; i < BASELENGTH; i++ )
-        hexNumberTable[i] = (XMLByte)-1;
-
-    for ( i = chDigit_9; i >= chDigit_0; i-- )
-        hexNumberTable[i] = (XMLByte) (i - chDigit_0);
-
-    for ( i = chLatin_F; i >= chLatin_A; i-- )
-        hexNumberTable[i] = (XMLByte) (i - chLatin_A + 10);
- 
-    for ( i = chLatin_f; i >= chLatin_a; i-- )
-        hexNumberTable[i] = (XMLByte) (i - chLatin_a + 10);    
-
-    isInitialized = true;
-}
-
 XERCES_CPP_NAMESPACE_END
diff --git a/src/xercesc/util/HexBin.hpp b/src/xercesc/util/HexBin.hpp
index b51e1805d..c28d4a92b 100644
--- a/src/xercesc/util/HexBin.hpp
+++ b/src/xercesc/util/HexBin.hpp
@@ -14,10 +14,6 @@
  * limitations under the License.
  */
 
-/*
- * $Id$
- */
-
 #ifndef HEXBIN_HPP
 #define HEXBIN_HPP
 
@@ -125,8 +121,6 @@ private :
     //  Helper methods
     // -----------------------------------------------------------------------
 
-    static void init();
-
     static bool isHex(const XMLCh& octect);
 
     // -----------------------------------------------------------------------
@@ -148,8 +142,7 @@ private :
     //     arrany holding valid hexNumber character.
     //
     // -----------------------------------------------------------------------
-    static bool       isInitialized;
-    static XMLByte    hexNumberTable[];
+    static const XMLByte    hexNumberTable[];
 };
 
 XERCES_CPP_NAMESPACE_END
-- 
GitLab