xmlutf8transcoder390.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 632 行 · 第 1/2 页

CPP
632
字号
/* * Copyright 2004,2004 The Apache Software Foundation. *  * Licensed 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. *//* * $Log: XMLUTF8Transcoder390.cpp,v $ * Revision 1.5  2004/09/08 13:56:46  peiyongz * Apache License Version 2.0 * * Revision 1.4  2004/06/30 19:04:17  peiyongz * XML1.0-3rd Edition: UTF_8 * * Revision 1.3  2004/02/06 18:23:55  cargilld * Misc 390 changes. * */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/util/TranscodingException.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/Transcoders/Uniconv390/XMLUTF8Transcoder390.hpp>#include <xercesc/util/UTFDataFormatException.hpp>#include <string.h>XERCES_CPP_NAMESPACE_BEGINextern "OS" void TROTASC(const XMLByte * input,                         XMLCh * output,                         unsigned int * count,                         XMLCh *table,                         int STOP,                         int * FLAG                        );//Add a long double in front of the table, the compiler will set the //table starting address on a double word boundarystruct temp{   long double pad;   XMLCh gFromTable[256];};static struct temp padding_temp={ 0,    0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007  , 0x0008, 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F  , 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017  , 0x0018, 0x0019, 0x001A, 0x001B, 0x001C, 0x001D, 0x001E, 0x001F  , 0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027  , 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D, 0x002E, 0x002F  , 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037  , 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F  , 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047  , 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F  , 0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057  , 0x0058, 0x0059, 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F  , 0x0060, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067  , 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D, 0x006E, 0x006F  , 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077  , 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, 0x007E, 0x007F  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF  , 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};// ---------------------------------------------------------------------------//  Local static data////  gUTFBytes//      A list of counts of trailing bytes for each initial byte in the input.////  gUTFByteIndicator//      For a UTF8 sequence of n bytes, n>=2, the first byte of the//      sequence must contain n 1's followed by precisely 1 0 with the//      rest of the byte containing arbitrary bits.  This array stores//      the required bit pattern for validity checking.//  gUTFByteIndicatorTest//      When bitwise and'd with the observed value, if the observed//      value is correct then a result matching gUTFByteIndicator will//      be produced.////  gUTFOffsets//      A list of values to offset each result char type, according to how//      many source bytes when into making it.////  gFirstByteMark//      A list of values to mask onto the first byte of an encoded sequence,//      indexed by the number of bytes used to create the sequence.// ---------------------------------------------------------------------------static const XMLByte gUTFBytes[256] ={        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0    ,   0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1    ,   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1    ,   2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2    ,   3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5};static const XMLByte gUTFByteIndicator[6] ={    0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};static const XMLByte gUTFByteIndicatorTest[6] ={    0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE};static const XMLUInt32 gUTFOffsets[6] ={    0, 0x3080, 0xE2080, 0x3C82080, 0xFA082080, 0x82082080};static const XMLByte gFirstByteMark[7] ={    0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC};// ---------------------------------------------------------------------------//  XMLUTF8Transcoder390: Constructors and Destructor// ---------------------------------------------------------------------------XMLUTF8Transcoder390::XMLUTF8Transcoder390(const  XMLCh* const    encodingName                                    , const unsigned int    blockSize                                    , MemoryManager* const manager) :    XMLTranscoder(encodingName, blockSize, manager){}XMLUTF8Transcoder390::~XMLUTF8Transcoder390(){}// ---------------------------------------------------------------------------//  XMLUTF8Transcoder390: Implementation of the transcoder API// ---------------------------------------------------------------------------unsigned intXMLUTF8Transcoder390::transcodeFrom(const  XMLByte* const          srcData                                , const unsigned int            srcCount                                ,       XMLCh* const            toFill                                , const unsigned int            maxChars                                ,       unsigned int&           bytesEaten                                ,       unsigned char* const    charSizes){    // Watch for pathological scenario. Shouldn't happen, but...    if (!srcCount || !maxChars)        return 0;    // If debugging, make sure that the block size is legal    #if defined(XERCES_DEBUG)    checkBlockSize(maxChars);    #endif    //    //  Get pointers to our start and end points of the input and output    //  buffers.    //    const XMLByte*  srcPtr = srcData;    const XMLByte*  srcEnd = srcPtr + srcCount;    XMLCh*          outPtr = toFill;    XMLCh*          outEnd = outPtr + maxChars;    unsigned char*  sizePtr = charSizes;    unsigned int    countDone = (srcCount<maxChars)?srcCount:maxChars;    // if this flag is set, a none ASCII character is encountered    int             flag = 0;    // Special-case ASCII, which is a leading byte value of <= 127                     TROTASC(srcPtr, outPtr, &countDone, padding_temp.gFromTable, 0xFFFF,&flag);    srcPtr+=countDone;    outPtr+=countDone;    memset(sizePtr, 1, countDone);    sizePtr+=countDone;            //    //  We now loop until we either run out of input data, or room to store    //  output chars.    //    while ((srcPtr < srcEnd) && (outPtr < outEnd))    {                if (*srcPtr <= 127)        {            *outPtr++ = XMLCh(*srcPtr++);            *sizePtr++ = 1;            continue;        }                // See how many trailing src bytes this sequence is going to require        const unsigned int trailingBytes = gUTFBytes[*srcPtr];        //        //  If there are not enough source bytes to do this one, then we        //  are done. Note that we done >= here because we are implicitly        //  counting the 1 byte we get no matter what.        //        //  If we break out here, then there is nothing to undo since we        //  haven't updated any pointers yet.        //        if (srcPtr + trailingBytes >= srcEnd)            break;        // Looks ok, so lets build up the value        // or at least let's try to do so--remembering that        // we cannot assume the encoding to be valid:        // first, test first byte        if((gUTFByteIndicatorTest[trailingBytes] & *srcPtr) != gUTFByteIndicator[trailingBytes]) {            char pos[2] = {(char)0x31, 0};             char len[2] = {(char)(trailingBytes+0x31), 0};            char byte[2] = {*srcPtr,0};            ThrowXMLwithMemMgr3(UTFDataFormatException, XMLExcepts::UTF8_FormatError, pos, byte, len, getMemoryManager());        }        /***         * http://www.unicode.org/reports/tr27/         *         * Table 3.1B. lists all of the byte sequences that are legal in UTF-8.          * A range of byte values such as A0..BF indicates that any byte from A0 to BF (inclusive)          * is legal in that position.          * Any byte value outside of the ranges listed is illegal.          * For example,          * the byte sequence <C0 AF> is illegal  since C0 is not legal in the 1st Byte column.          * The byte sequence <E0 9F 80> is illegal since in the row          *    where E0 is legal as a first byte,          *    9F is not legal as a second byte.          * The byte sequence <F4 80 83 92> is legal, since every byte in that sequence matches          * a byte range in a row of the table (the last row).          *         *         * Table 3.1B. Legal UTF-8 Byte Sequences           * Code Points              1st Byte    2nd Byte    3rd Byte    4th Byte          * =========================================================================         * U+0000..U+007F            00..7F                * -------------------------------------------------------------------------         * U+0080..U+07FF            C2..DF      80..BF               *         * -------------------------------------------------------------------------         * U+0800..U+0FFF            E0          A0..BF     80..BF            *                                       --          *                                   * U+1000..U+FFFF            E1..EF      80..BF     80..BF             *         * --------------------------------------------------------------------------         * U+10000..U+3FFFF          F0          90..BF     80..BF       80..BF          *                                       --         * U+40000..U+FFFFF          F1..F3      80..BF     80..BF       80..BF          * U+100000..U+10FFFF        F4          80..8F     80..BF       80..BF          *                                           --         * ==========================================================================         *         *  Cases where a trailing byte range is not 80..BF are underlined in the table to          *  draw attention to them. These occur only in the second byte of a sequence.         *         ***/        XMLUInt32 tmpVal = 0;        switch(trailingBytes)        {            case 1 :                // UTF-8:   [110y yyyy] [10xx xxxx]                // Unicode: [0000 0yyy] [yyxx xxxx]                //                // 0xC0, 0xC1 has been filtered out                             checkTrailingBytes(*(srcPtr+1), 1, 1);                tmpVal = *srcPtr++;                tmpVal <<= 6;                tmpVal += *srcPtr++;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?