Newer
Older
* 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.
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <xercesc/util/BitOps.hpp>
#include <xercesc/util/XMLUTF16Transcoder.hpp>
#include <xercesc/util/TranscodingException.hpp>
#include <string.h>
// ---------------------------------------------------------------------------
// XMLUTF16Transcoder: Constructors and Destructor
// ---------------------------------------------------------------------------
XMLUTF16Transcoder::XMLUTF16Transcoder( const XMLCh* const encodingName
, const unsigned int blockSize
David Abram Cargill
committed
, const bool swapped
, MemoryManager* const manager) :
David Abram Cargill
committed
XMLTranscoder(encodingName, blockSize, manager)
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
, fSwapped(swapped)
{
}
XMLUTF16Transcoder::~XMLUTF16Transcoder()
{
}
// ---------------------------------------------------------------------------
// XMLUTF16Transcoder: Implementation of the transcoder API
// ---------------------------------------------------------------------------
unsigned int
XMLUTF16Transcoder::transcodeFrom( const XMLByte* const srcData
, const unsigned int srcCount
, XMLCh* const toFill
, const unsigned int maxChars
, unsigned int& bytesEaten
, unsigned char* const charSizes)
{
// If debugging, make sure that the block size is legal
#if defined(XERCES_DEBUG)
checkBlockSize(maxChars);
#endif
//
// Calculate the max chars we can do here. Its the lesser of the
// max output chars and the number of chars in the source.
//
const unsigned int srcChars = srcCount / sizeof(UTF16Ch);
const unsigned int countToDo = srcChars < maxChars ? srcChars : maxChars;
// Look at the source data as UTF16 chars
const UTF16Ch* asUTF16 = (const UTF16Ch*)srcData;
// And get a mutable pointer to the output
XMLCh* outPtr = toFill;
//
// If its swapped, we have to do a char by char swap and cast. Else
// we have to check whether our XMLCh and UTF16Ch types are the same
// size or not. If so, we can optimize by just doing a buffer copy.
//
if (fSwapped)
{
//
// And then do the swapping loop for the count we precalculated. Note
// that this also handles size conversion as well if XMLCh is not the
// same size as UTF16Ch.
//
for (unsigned int index = 0; index < countToDo; index++)
*outPtr++ = BitOps::swapBytes(*asUTF16++);
}
else
{
//
// If the XMLCh type is the same size as a UTF16 value on this
// platform, then we can do just a buffer copy straight to the target
// buffer since our source chars are UTF-16 chars. If its not, then
// we still have to do a loop and assign each one, in order to
// implicitly convert.
//
if (sizeof(XMLCh) == sizeof(UTF16Ch))
{
// Notice we convert char count to byte count here!!!
memcpy(toFill, srcData, countToDo * sizeof(UTF16Ch));
}
else
{
for (unsigned int index = 0; index < countToDo; index++)
*outPtr++ = XMLCh(*asUTF16++);
}
}
// Set the bytes eaten
bytesEaten = countToDo * sizeof(UTF16Ch);
// Set the character sizes to the fixed size
memset(charSizes, sizeof(UTF16Ch), countToDo);
// Return the chars we transcoded
return countToDo;
}
unsigned int
XMLUTF16Transcoder::transcodeTo(const XMLCh* const srcData
, const unsigned int srcCount
, XMLByte* const toFill
, const unsigned int maxBytes
, unsigned int& charsEaten
David Abram Cargill
committed
, const UnRepOpts)
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{
// If debugging, make sure that the block size is legal
#if defined(XERCES_DEBUG)
checkBlockSize(maxBytes);
#endif
//
// Calculate the max chars we can do here. Its the lesser of the
// chars that we can fit into the output buffer, and the source
// chars available.
//
const unsigned int maxOutChars = maxBytes / sizeof(UTF16Ch);
const unsigned int countToDo = srcCount < maxOutChars ? srcCount : maxOutChars;
//
// Get a pointer tot he output buffer in the UTF-16 character format
// that we need to work with. And get a mutable pointer to the source
// character buffer.
//
UTF16Ch* outPtr = (UTF16Ch*)toFill;
const XMLCh* srcPtr = srcData;
//
// If the target format is swapped from our native format, then handle
// it one way, else handle it another.
//
if (fSwapped)
{
//
// And then do the swapping loop for the count we precalculated. Note
// that this also handles size conversion as well if XMLCh is not the
// same size as UTF16Ch.
//
for (unsigned int index = 0; index < countToDo; index++)
{
// To avoid flakey compilers, use a temp
const UTF16Ch tmpCh = UTF16Ch(*srcPtr++);
*outPtr++ = BitOps::swapBytes(tmpCh);
}
}
else
{
//
// If XMLCh and UTF16Ch are the same size, we can just do a fast
// memory copy. Otherwise, we have to do a loop and downcast each
// character into its new 16 bit storage.
//
if (sizeof(XMLCh) == sizeof(UTF16Ch))
{
// Notice we convert char count to byte count here!!!
memcpy(toFill, srcData, countToDo * sizeof(UTF16Ch));
}
else
{
for (unsigned int index = 0; index < countToDo; index++)
*outPtr++ = UTF16Ch(*srcPtr++);
}
}
// Set the chars eaten to the calculated number we ate
charsEaten = countToDo;
//Return the bytes we ate. Note we convert to a byte count here!
return countToDo * sizeof(UTF16Ch);
}
David Abram Cargill
committed
bool XMLUTF16Transcoder::canTranscodeTo(const unsigned int) const