source: icXML/icXML-devel/src/icxercesc/util/XML256TableTranscoder.cpp @ 2721

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

Fix imports in icXML modified Xerces files

File size: 7.9 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// ---------------------------------------------------------------------------
20//  Includes
21// ---------------------------------------------------------------------------
22#include <xercesc/util/BitOps.hpp>
23#include <xercesc/util/TranscodingException.hpp>
24#include <icxercesc/util/XML256TableTranscoder.hpp>
25#include <icxercesc/util/XMLString.hpp>
26#include <string.h>
27
28XERCES_CPP_NAMESPACE_BEGIN
29
30
31// ---------------------------------------------------------------------------
32//  XML256TableTranscoder: Public Destructor
33// ---------------------------------------------------------------------------
34XML256TableTranscoder::~XML256TableTranscoder()
35{
36    // We don't own the tables, we just reference them
37}
38
39
40// ---------------------------------------------------------------------------
41//  XML256TableTranscoder: Implementation of the transcoder API
42// ---------------------------------------------------------------------------
43XMLSize_t
44XML256TableTranscoder::transcodeFrom(const  XMLByte* const       srcData
45                                    , const XMLSize_t            srcCount
46                                    ,       XMLCh* const         toFill
47                                    , const XMLSize_t            maxChars
48                                    ,       XMLSize_t&           bytesEaten
49                                    ,       unsigned char* const charSizes)
50{
51    //
52    //  Calculate the max chars we can do here. Its the lesser of the
53    //  max output chars and the number of chars in the source.
54    //
55    const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars;
56
57    //
58    //  Loop through the count we have to do and map each char via the
59    //  lookup table.
60    //
61    const XMLByte*  srcPtr = srcData;
62    const XMLByte*  endPtr = (srcPtr + countToDo);
63    XMLCh*          outPtr = toFill;
64    while (srcPtr < endPtr)
65    {
66        const XMLCh uniCh = fFromTable[*srcPtr++];
67        if (uniCh != 0xFFFF)
68        {
69            *outPtr++ = uniCh;
70            continue;
71        }
72    }
73
74
75    // Set the bytes eaten
76    bytesEaten = countToDo;
77
78    // Set the character sizes to the fixed size
79    memset(charSizes, 1, countToDo);
80
81    // Return the chars we transcoded
82    return countToDo;
83}
84
85XMLSize_t
86XML256TableTranscoder::transcodeFrom
87(
88      const XMLByte* const          srcData
89    , const XMLSize_t               srcCount
90    ,       XMLBuffer &             toFill
91)
92{
93    toFill.reset();
94    toFill.ensureCapacity(srcCount);
95
96    //
97    //  Loop through the count we have to do and map each char via the
98    //  lookup table.
99    //
100    const XMLByte*  srcPtr = srcData;
101    const XMLByte*  endPtr = (srcPtr + srcCount);
102    XMLCh*          outPtr = toFill.getRawBuffer();
103    while (srcPtr < endPtr)
104    {
105        const XMLCh uniCh = fFromTable[*srcPtr++];
106        if (likely(uniCh != 0xFFFF))
107        {
108            *outPtr++ = uniCh;
109            continue;
110        }
111    }
112
113    toFill.setLen(srcCount);
114
115    // Return the chars we transcoded
116    return srcCount;
117}
118
119XMLSize_t
120XML256TableTranscoder::transcodeTo( const   XMLCh* const    srcData
121                                    , const XMLSize_t       srcCount
122                                    ,       XMLByte* const  toFill
123                                    , const XMLSize_t       maxBytes
124                                    ,       XMLSize_t&      charsEaten
125                                    , const UnRepOpts       options)
126{
127    //
128    //  Calculate the max chars we can do here. Its the lesser of the
129    //  max output chars and the number of chars in the source.
130    //
131    const XMLSize_t countToDo = srcCount < maxBytes ? srcCount : maxBytes;
132
133    //
134    //  Loop through the count we have to do and map each char via the
135    //  lookup table.
136    //
137    const XMLCh*    srcPtr = srcData;
138    const XMLCh*    endPtr = (srcPtr + countToDo);
139    XMLByte*        outPtr = toFill;
140    XMLByte         nextOut;
141    while (srcPtr < endPtr)
142    {
143        //
144        //  Get the next src char out to a temp, then do a binary search
145        //  of the 'to' table for this entry.
146        //
147        if ((nextOut = xlatOneTo(*srcPtr))!=0)
148        {
149            *outPtr++ = nextOut;
150            srcPtr++;
151            continue;
152        }
153
154        //
155        //  Its not representable so, according to the options, either
156        //  throw or use the replacement.
157        //
158        if (options == UnRep_Throw)
159        {
160            XMLCh tmpBuf[17];
161            XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
162            ThrowXMLwithMemMgr2
163            (
164                TranscodingException
165                , XMLExcepts::Trans_Unrepresentable
166                , tmpBuf
167                , getEncodingName()
168                , getMemoryManager()
169            );
170        }
171
172        // Eat the source char and use the replacement char
173        srcPtr++;
174        *outPtr++ = 0x3F;
175    }
176
177    // Set the chars eaten
178    charsEaten = countToDo;
179
180    // Return the bytes we transcoded
181    return countToDo;
182}
183
184
185bool XML256TableTranscoder::canTranscodeTo(const unsigned int toCheck)
186{
187    return (xlatOneTo(toCheck) != 0);
188}
189
190
191// ---------------------------------------------------------------------------
192//  XML256TableTranscoder: Hidden constructor
193// ---------------------------------------------------------------------------
194XML256TableTranscoder::
195XML256TableTranscoder(  const   XMLCh* const                     encodingName
196                        , const XMLSize_t                        blockSize
197                        , const XMLCh* const                     fromTable
198                        , const XMLTransService::TransRec* const toTable
199                        , const XMLSize_t                        toTableSize
200                        , MemoryManager* const                   manager) :
201
202    XMLTranscoder(encodingName, blockSize, manager)
203    , fFromTable(fromTable)
204    , fToSize(toTableSize)
205    , fToTable(toTable)
206{
207}
208
209
210// ---------------------------------------------------------------------------
211//  XML256TableTranscoder: Private helper methods
212// ---------------------------------------------------------------------------
213XMLByte XML256TableTranscoder::xlatOneTo(const XMLCh toXlat) const
214{
215    XMLSize_t lowOfs = 0;
216    XMLSize_t hiOfs = fToSize - 1;
217    do
218    {
219        // Calc the mid point of the low and high offset.
220        const XMLSize_t midOfs = ((hiOfs - lowOfs) / 2) + lowOfs;
221
222        //
223        //  If our test char is greater than the mid point char, then
224        //  we move up to the upper half. Else we move to the lower
225        //  half. If its equal, then its our guy.
226        //
227        if (toXlat > fToTable[midOfs].intCh)
228        {
229            lowOfs = midOfs;
230        }
231         else if (toXlat < fToTable[midOfs].intCh)
232        {
233            hiOfs = midOfs;
234        }
235         else
236        {
237            return fToTable[midOfs].extCh;
238        }
239    }   while (lowOfs + 1 < hiOfs);
240
241    // Check the high end of the range otherwise the
242    // last item in the table may never be found.
243        if (toXlat == fToTable[hiOfs].intCh)
244        {
245            return fToTable[hiOfs].extCh;
246        }
247
248    return 0;
249}
250
251XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.