source: icXML/icXML-devel/src/xercesc/util/HexBin.cpp @ 2722

Last change on this file since 2722 was 2722, checked in by cameron, 6 years ago

Original Xerces files with import mods for icxercesc

File size: 5.3 KB
Line 
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//  Includes
20// ---------------------------------------------------------------------------
21#include <xercesc/util/HexBin.hpp>
22#include <xercesc/util/XMLUniDefs.hpp>
23#include <icxercesc/util/XMLString.hpp>
24#include <xercesc/util/Janitor.hpp>
25
26XERCES_CPP_NAMESPACE_BEGIN
27
28// ---------------------------------------------------------------------------
29//  constants
30// ---------------------------------------------------------------------------
31static const int BASELENGTH = 255;
32
33// ---------------------------------------------------------------------------
34//  class data member
35// ---------------------------------------------------------------------------
36const XMLByte HexBin::hexNumberTable[BASELENGTH] =
37{
38    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
39    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
40    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
41    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 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0xFF, 0xFF,
52    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
53    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
54};
55
56
57int HexBin::getDataLength(const XMLCh* const hexData)
58{
59    if (!isArrayByteHex(hexData))
60        return -1;
61
62    return (int)XMLString::stringLen(hexData)/2;
63}
64
65bool HexBin::isArrayByteHex(const XMLCh* const hexData)
66{
67    if (( hexData == 0 ) || ( *hexData == 0 )) // zero length
68        return true;
69
70    XMLSize_t strLen = XMLString::stringLen(hexData);
71    if ( strLen%2 != 0 )
72        return false;
73
74    for ( XMLSize_t i = 0; i < strLen; i++ )
75        if( !isHex(hexData[i]) )
76            return false;
77
78    return true;
79}
80
81XMLCh* HexBin::getCanonicalRepresentation(const XMLCh*          const hexData
82                                        ,       MemoryManager*  const manager)
83{
84
85    if (getDataLength(hexData) == -1)
86        return 0;
87
88    XMLCh* retStr = XMLString::replicate(hexData, manager);
89    XMLString::upperCaseASCII(retStr);
90
91    return retStr;
92}
93
94XMLByte* HexBin::decodeToXMLByte(const XMLCh*          const   hexData
95                    ,       MemoryManager*  const   manager)
96{
97    if (( hexData == 0 ) || ( *hexData == 0 )) // zero length
98        return 0;
99
100    XMLSize_t strLen = XMLString::stringLen(hexData);
101    if ( strLen%2 != 0 )
102        return 0;
103
104    //prepare the return string
105    int decodeLength = (int)strLen/2;
106    XMLByte *retVal = (XMLByte*) manager->allocate( (decodeLength + 1) * sizeof(XMLByte));
107    ArrayJanitor<XMLByte> janFill(retVal, manager);
108   
109    XMLByte temp1, temp2;
110    for( int i = 0; i<decodeLength; i++ ) {
111        temp1 = hexNumberTable[hexData[i*2]];
112        if (temp1 == (XMLByte) -1)
113            return 0;
114        temp2 = hexNumberTable[hexData[i*2+1]];
115        if (temp2 == (XMLByte) -1)
116            return 0;
117        retVal[i] = ((temp1 << 4) | temp2);
118    }
119
120    janFill.release();
121    retVal[decodeLength] = 0;
122    return retVal;
123}
124
125
126// -----------------------------------------------------------------------
127//  Helper methods
128// -----------------------------------------------------------------------
129bool HexBin::isHex(const XMLCh& octet)
130{
131    // sanity check to avoid out-of-bound index
132    if ( octet >= BASELENGTH )
133        return false;
134
135    return (hexNumberTable[octet] != (XMLByte) -1);
136}
137
138XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.