Skip to content
Snippets Groups Projects
HexBin.cpp 5.24 KiB
Newer Older
PeiYong Zhang's avatar
PeiYong Zhang committed
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
PeiYong Zhang's avatar
PeiYong Zhang committed
 */

// ---------------------------------------------------------------------------
//  Includes
// ---------------------------------------------------------------------------
#include <xercesc/util/HexBin.hpp>
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/XMLString.hpp>
PeiYong Zhang's avatar
PeiYong Zhang committed
#include <xercesc/util/Janitor.hpp>
PeiYong Zhang's avatar
PeiYong Zhang committed

Tinny Ng's avatar
Tinny Ng committed
XERCES_CPP_NAMESPACE_BEGIN

PeiYong Zhang's avatar
PeiYong Zhang committed
// ---------------------------------------------------------------------------
//  constants
// ---------------------------------------------------------------------------
Tinny Ng's avatar
Tinny Ng committed
static const int BASELENGTH = 255;
PeiYong Zhang's avatar
PeiYong Zhang committed

// ---------------------------------------------------------------------------
//  class data member
// ---------------------------------------------------------------------------
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, 0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
    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
};

PeiYong Zhang's avatar
PeiYong Zhang committed

int HexBin::getDataLength(const XMLCh* const hexData)
{
    if (!isArrayByteHex(hexData))
        return -1;

    return XMLString::stringLen(hexData)/2;
}

bool HexBin::isArrayByteHex(const XMLCh* const hexData)
{
    if (( hexData == 0 ) || ( *hexData == 0 )) // zero length
PeiYong Zhang's avatar
PeiYong Zhang committed

    int strLen = XMLString::stringLen(hexData);
    if ( strLen%2 != 0 )
        return false;

    for ( int i = 0; i < strLen; i++ )
Tinny Ng's avatar
Tinny Ng committed
        if( !isHex(hexData[i]) )
PeiYong Zhang's avatar
PeiYong Zhang committed
            return false;

    return true;
}

PeiYong Zhang's avatar
PeiYong Zhang committed
XMLCh* HexBin::getCanonicalRepresentation(const XMLCh*          const hexData
                                        ,       MemoryManager*  const manager)
{

    if (getDataLength(hexData) == -1)
        return 0;

    XMLCh* retStr = XMLString::replicate(hexData, manager);
PeiYong Zhang's avatar
PeiYong Zhang committed

    return retStr;
}

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;

    //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)
PeiYong Zhang's avatar
PeiYong Zhang committed
            return 0;
        temp2 = hexNumberTable[hexData[i*2+1]];
        if (temp2 == (XMLByte) -1)
            return 0;
        retVal[i] = ((temp1 << 4) | temp2);
PeiYong Zhang's avatar
PeiYong Zhang committed
    }

    janFill.release();
PeiYong Zhang's avatar
PeiYong Zhang committed
    return retVal;
}

PeiYong Zhang's avatar
PeiYong Zhang committed
// -----------------------------------------------------------------------
//  Helper methods
// -----------------------------------------------------------------------
Tinny Ng's avatar
Tinny Ng committed
bool HexBin::isHex(const XMLCh& octet)
PeiYong Zhang's avatar
PeiYong Zhang committed
{
    // sanity check to avoid out-of-bound index
PeiYong Zhang's avatar
PeiYong Zhang committed
        return false;

PeiYong Zhang's avatar
PeiYong Zhang committed
}

Tinny Ng's avatar
Tinny Ng committed
XERCES_CPP_NAMESPACE_END