source: icXML/icXML-devel/src/icxercesc/util/XMLASCIITranscoder.cpp

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

Initial imports for icXML v0.9

File size: 8.6 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 <icxercesc/util/XMLASCIITranscoder.hpp>
23#include <icxercesc/util/XMLString.hpp>
24#include <xercesc/util/TranscodingException.hpp>
25#include <string.h>
26
27XERCES_CPP_NAMESPACE_BEGIN
28
29// ---------------------------------------------------------------------------
30//  XMLASCIITranscoder: Constructors and Destructor
31// ---------------------------------------------------------------------------
32XMLASCIITranscoder::XMLASCIITranscoder( const   XMLCh* const   encodingName
33                                        , const XMLSize_t      blockSize
34                                        , MemoryManager* const manager) :
35
36    XMLTranscoder(encodingName, blockSize, manager)
37{
38}
39
40
41XMLASCIITranscoder::~XMLASCIITranscoder()
42{
43}
44
45
46// ---------------------------------------------------------------------------
47//  XMLASCIITranscoder: Implementation of the transcoder API
48// ---------------------------------------------------------------------------
49XMLSize_t
50XMLASCIITranscoder::transcodeFrom(  const   XMLByte* const       srcData
51                                    , const XMLSize_t            srcCount
52                                    ,       XMLCh* const         toFill
53                                    , const XMLSize_t            maxChars
54                                    ,       XMLSize_t&           bytesEaten
55                                    ,       unsigned char* const charSizes)
56{
57    //
58    //  Calculate the max chars we can do here. Its the lesser of the
59    //  max output chars and the source byte count.
60    //
61    const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars;
62
63    //
64    //  Now loop through that many source chars and just cast each one
65    //  over to the XMLCh format. Check each source that its really a
66    //  valid ASCI char.
67    //
68    const XMLByte*  srcPtr = srcData;
69    XMLCh*          outPtr = toFill;
70    XMLSize_t       countDone = 0;
71    for (; countDone < countToDo; countDone++)
72    {
73        // Do the optimistic work up front
74        if (*srcPtr < 0x80)
75        {
76            *outPtr++ = XMLCh(*srcPtr++);
77            continue;
78        }
79
80        //
81        //  We got non source encoding char. If we got more than 32 chars,
82        //  the just break out. We'll come back here later to hit this again
83        //  and give an error much closer to the real source position.
84        //
85        if (countDone > 32)
86            break;
87
88        XMLCh tmpBuf[17];
89        XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
90        ThrowXMLwithMemMgr2
91        (
92            TranscodingException
93            , XMLExcepts::Trans_Unrepresentable
94            , tmpBuf
95            , getEncodingName()
96            , getMemoryManager()
97        );
98    }
99
100    // Set the bytes we ate
101    bytesEaten = countDone;
102
103    // Set the char sizes to the fixed size
104    memset(charSizes, 1, countDone);
105
106    // Return the chars we transcoded
107    return countDone;
108}
109
110XMLSize_t
111XMLASCIITranscoder::transcodeFrom(  const   XMLByte* const       srcData
112                                    , const XMLSize_t            srcCount
113                                    ,       XMLCh* const         toFill
114                                    , const XMLSize_t            maxChars
115                                    ,       XMLSize_t&           bytesEaten)
116{
117    //
118    //  Calculate the max chars we can do here. Its the lesser of the
119    //  max output chars and the source byte count.
120    //
121    const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars;
122
123    //
124    //  Now loop through that many source chars and just cast each one
125    //  over to the XMLCh format. Check each source that its really a
126    //  valid ASCI char.
127    //
128    const XMLByte*  srcPtr = srcData;
129    XMLCh*          outPtr = toFill;
130    XMLSize_t       countDone = 0;
131    for (; countDone < countToDo; countDone++)
132    {
133        // Do the optimistic work up front
134        if (*srcPtr < 0x80)
135        {
136            *outPtr++ = XMLCh(*srcPtr++);
137            continue;
138        }
139
140        //
141        //  We got non source encoding char. If we got more than 32 chars,
142        //  the just break out. We'll come back here later to hit this again
143        //  and give an error much closer to the real source position.
144        //
145        if (countDone > 32)
146            break;
147
148        XMLCh tmpBuf[17];
149        XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
150        ThrowXMLwithMemMgr2
151        (
152            TranscodingException
153            , XMLExcepts::Trans_Unrepresentable
154            , tmpBuf
155            , getEncodingName()
156            , getMemoryManager()
157        );
158    }
159
160    // Set the bytes we ate
161    bytesEaten = countDone;
162
163    // Return the chars we transcoded
164    return countDone;
165}
166
167XMLSize_t XMLASCIITranscoder::transcodeFrom
168(
169      const XMLByte* const          srcData
170    , const XMLSize_t               srcCount
171    ,       XMLBuffer &             toFill
172)
173{
174    //
175    //  Now loop through that many source chars and just cast each one
176    //  over to the XMLCh format. Check each source that its really a
177    //  valid ASCI char.
178    //
179
180    toFill.reset();
181    toFill.ensureCapacity(srcCount);
182
183    const XMLByte * srcPtr = srcData;
184    const XMLByte * const endPtr = srcPtr + srcCount;
185    XMLCh*          outPtr = toFill.getRawBuffer();
186
187    while (srcPtr < endPtr)
188    {
189        // Do the optimistic work up front
190        if (likely(*srcPtr < 0x80))
191        {
192            *outPtr++ = XMLCh(*srcPtr++);
193        }
194        else
195        {
196            XMLCh tmpBuf[17];
197            XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
198            ThrowXMLwithMemMgr2
199            (
200                TranscodingException
201                , XMLExcepts::Trans_Unrepresentable
202                , tmpBuf
203                , getEncodingName()
204                , getMemoryManager()
205            );
206        }
207    }
208
209    toFill.setLen(srcCount);
210
211    // Return the chars we transcoded
212    return srcCount;
213
214}
215
216XMLSize_t
217XMLASCIITranscoder::transcodeTo(const   XMLCh* const    srcData
218                                , const XMLSize_t       srcCount
219                                ,       XMLByte* const  toFill
220                                , const XMLSize_t       maxBytes
221                                ,       XMLSize_t&      charsEaten
222                                , const UnRepOpts       options)
223{
224    //
225    //  Calculate the max chars we can do here. Its the lesser of the
226    //  max output chars and the source byte count.
227    //
228    const XMLSize_t countToDo = srcCount < maxBytes ? srcCount : maxBytes;
229
230    const XMLCh*    srcPtr = srcData;
231    XMLByte*        outPtr = toFill;
232    for (XMLSize_t index = 0; index < countToDo; index++)
233    {
234        // If its legal, do it and jump back to the top
235        if (*srcPtr < 0x80)
236        {
237            *outPtr++ = XMLByte(*srcPtr++);
238            continue;
239        }
240
241        //
242        //  Its not representable so use a replacement char. According to
243        //  the options, either throw or use the replacement.
244        //
245        if (options == UnRep_Throw)
246        {
247            XMLCh tmpBuf[17];
248            XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
249            ThrowXMLwithMemMgr2
250            (
251                TranscodingException
252                , XMLExcepts::Trans_Unrepresentable
253                , tmpBuf
254                , getEncodingName()
255                , getMemoryManager()
256            );
257        }
258
259        // Use the replacement char
260        *outPtr++ = 0x1A;
261        srcPtr++;
262    }
263
264    // Set the chars we ate
265    charsEaten = countToDo;
266
267    // Return the byte we transcoded
268    return countToDo;
269}
270
271
272bool XMLASCIITranscoder::canTranscodeTo(const unsigned int toCheck)
273{
274    return (toCheck < 0x80);
275}
276
277XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.