1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*87d2adbcSAndrew Rist * distributed with this work for additional information
6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at
10*87d2adbcSAndrew Rist *
11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist *
13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the
17*87d2adbcSAndrew Rist * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist * under the License.
19*87d2adbcSAndrew Rist *
20*87d2adbcSAndrew Rist *************************************************************/
21*87d2adbcSAndrew Rist
22*87d2adbcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "context.h"
28cdf0e10cSrcweir #include "converter.h"
29cdf0e10cSrcweir #include "convertsinglebytetobmpunicode.hxx"
30cdf0e10cSrcweir #include "unichars.h"
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include "osl/diagnose.h"
33cdf0e10cSrcweir #include "rtl/textcvt.h"
34cdf0e10cSrcweir #include "sal/types.h"
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include <cstddef>
37cdf0e10cSrcweir
rtl_textenc_convertSingleByteToBmpUnicode(ImplTextConverterData const * data,void *,sal_Char const * srcBuf,sal_Size srcBytes,sal_Unicode * destBuf,sal_Size destChars,sal_uInt32 flags,sal_uInt32 * info,sal_Size * srcCvtBytes)38cdf0e10cSrcweir sal_Size rtl_textenc_convertSingleByteToBmpUnicode(
39cdf0e10cSrcweir ImplTextConverterData const * data, void *, sal_Char const * srcBuf,
40cdf0e10cSrcweir sal_Size srcBytes, sal_Unicode * destBuf, sal_Size destChars,
41cdf0e10cSrcweir sal_uInt32 flags, sal_uInt32 * info, sal_Size * srcCvtBytes)
42cdf0e10cSrcweir {
43cdf0e10cSrcweir sal_Unicode const * map = static_cast<
44cdf0e10cSrcweir rtl::textenc::BmpUnicodeToSingleByteConverterData const * >(
45cdf0e10cSrcweir data)->byteToUnicode;
46cdf0e10cSrcweir sal_uInt32 infoFlags = 0;
47cdf0e10cSrcweir sal_Size converted = 0;
48cdf0e10cSrcweir sal_Unicode * destBufPtr = destBuf;
49cdf0e10cSrcweir sal_Unicode * destBufEnd = destBuf + destChars;
50cdf0e10cSrcweir for (; converted < srcBytes; ++converted) {
51cdf0e10cSrcweir bool undefined = true;
52cdf0e10cSrcweir sal_Char b = *srcBuf++;
53cdf0e10cSrcweir sal_Unicode c = map[static_cast< sal_uInt8 >(b)];
54cdf0e10cSrcweir if (c == 0xFFFF) {
55cdf0e10cSrcweir goto bad_input;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir if (destBufEnd - destBufPtr < 1) {
58cdf0e10cSrcweir goto no_output;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir *destBufPtr++ = c;
61cdf0e10cSrcweir continue;
62cdf0e10cSrcweir bad_input:
63cdf0e10cSrcweir switch (ImplHandleBadInputTextToUnicodeConversion(
64cdf0e10cSrcweir undefined, false, b, flags, &destBufPtr, destBufEnd,
65cdf0e10cSrcweir &infoFlags))
66cdf0e10cSrcweir {
67cdf0e10cSrcweir case IMPL_BAD_INPUT_STOP:
68cdf0e10cSrcweir break;
69cdf0e10cSrcweir
70cdf0e10cSrcweir case IMPL_BAD_INPUT_CONTINUE:
71cdf0e10cSrcweir continue;
72cdf0e10cSrcweir
73cdf0e10cSrcweir case IMPL_BAD_INPUT_NO_OUTPUT:
74cdf0e10cSrcweir goto no_output;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir break;
77cdf0e10cSrcweir no_output:
78cdf0e10cSrcweir --srcBuf;
79cdf0e10cSrcweir infoFlags |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
80cdf0e10cSrcweir break;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir if (info != 0) {
83cdf0e10cSrcweir *info = infoFlags;
84cdf0e10cSrcweir }
85cdf0e10cSrcweir if (srcCvtBytes != 0) {
86cdf0e10cSrcweir *srcCvtBytes = converted;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir return destBufPtr - destBuf;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir
rtl_textenc_convertBmpUnicodeToSingleByte(ImplTextConverterData const * data,void * context,sal_Unicode const * srcBuf,sal_Size srcChars,sal_Char * destBuf,sal_Size destBytes,sal_uInt32 flags,sal_uInt32 * info,sal_Size * srcCvtChars)91cdf0e10cSrcweir sal_Size rtl_textenc_convertBmpUnicodeToSingleByte(
92cdf0e10cSrcweir ImplTextConverterData const * data, void * context,
93cdf0e10cSrcweir sal_Unicode const * srcBuf, sal_Size srcChars, sal_Char * destBuf,
94cdf0e10cSrcweir sal_Size destBytes, sal_uInt32 flags, sal_uInt32 * info,
95cdf0e10cSrcweir sal_Size * srcCvtChars)
96cdf0e10cSrcweir {
97cdf0e10cSrcweir std::size_t entries = static_cast<
98cdf0e10cSrcweir rtl::textenc::BmpUnicodeToSingleByteConverterData const * >(
99cdf0e10cSrcweir data)->unicodeToByteEntries;
100cdf0e10cSrcweir rtl::textenc::BmpUnicodeToSingleByteRange const * ranges = static_cast<
101cdf0e10cSrcweir rtl::textenc::BmpUnicodeToSingleByteConverterData const * >(
102cdf0e10cSrcweir data)->unicodeToByte;
103cdf0e10cSrcweir sal_Unicode highSurrogate = 0;
104cdf0e10cSrcweir sal_uInt32 infoFlags = 0;
105cdf0e10cSrcweir sal_Size converted = 0;
106cdf0e10cSrcweir sal_Char * destBufPtr = destBuf;
107cdf0e10cSrcweir sal_Char * destBufEnd = destBuf + destBytes;
108cdf0e10cSrcweir if (context != 0) {
109cdf0e10cSrcweir highSurrogate = static_cast< ImplUnicodeToTextContext * >(context)->
110cdf0e10cSrcweir m_nHighSurrogate;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir for (; converted < srcChars; ++converted) {
113cdf0e10cSrcweir bool undefined = true;
114cdf0e10cSrcweir sal_uInt32 c = *srcBuf++;
115cdf0e10cSrcweir if (highSurrogate == 0) {
116cdf0e10cSrcweir if (ImplIsHighSurrogate(c)) {
117cdf0e10cSrcweir highSurrogate = static_cast< sal_Unicode >(c);
118cdf0e10cSrcweir continue;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir } else if (ImplIsLowSurrogate(c)) {
121cdf0e10cSrcweir c = ImplCombineSurrogates(highSurrogate, c);
122cdf0e10cSrcweir } else {
123cdf0e10cSrcweir undefined = false;
124cdf0e10cSrcweir goto bad_input;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir if (ImplIsLowSurrogate(c) || ImplIsNoncharacter(c)) {
127cdf0e10cSrcweir undefined = false;
128cdf0e10cSrcweir goto bad_input;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir // Linearly searching through the ranges if probably fastest, assuming
131cdf0e10cSrcweir // that most converted characters belong to the ASCII subset:
132cdf0e10cSrcweir for (std::size_t i = 0; i < entries; ++i) {
133cdf0e10cSrcweir if (c < ranges[i].unicode) {
134cdf0e10cSrcweir break;
135cdf0e10cSrcweir } else if (c <= sal::static_int_cast< sal_uInt32 >(
136cdf0e10cSrcweir ranges[i].unicode + ranges[i].range))
137cdf0e10cSrcweir {
138cdf0e10cSrcweir if (destBufEnd - destBufPtr < 1) {
139cdf0e10cSrcweir goto no_output;
140cdf0e10cSrcweir }
141cdf0e10cSrcweir *destBufPtr++ = static_cast< sal_Char >(
142cdf0e10cSrcweir ranges[i].byte + (c - ranges[i].unicode));
143cdf0e10cSrcweir goto done;
144cdf0e10cSrcweir }
145cdf0e10cSrcweir }
146cdf0e10cSrcweir goto bad_input;
147cdf0e10cSrcweir done:
148cdf0e10cSrcweir highSurrogate = 0;
149cdf0e10cSrcweir continue;
150cdf0e10cSrcweir bad_input:
151cdf0e10cSrcweir switch (ImplHandleBadInputUnicodeToTextConversion(
152cdf0e10cSrcweir undefined, c, flags, &destBufPtr, destBufEnd, &infoFlags, 0,
153cdf0e10cSrcweir 0, 0))
154cdf0e10cSrcweir {
155cdf0e10cSrcweir case IMPL_BAD_INPUT_STOP:
156cdf0e10cSrcweir highSurrogate = 0;
157cdf0e10cSrcweir break;
158cdf0e10cSrcweir
159cdf0e10cSrcweir case IMPL_BAD_INPUT_CONTINUE:
160cdf0e10cSrcweir highSurrogate = 0;
161cdf0e10cSrcweir continue;
162cdf0e10cSrcweir
163cdf0e10cSrcweir case IMPL_BAD_INPUT_NO_OUTPUT:
164cdf0e10cSrcweir goto no_output;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir break;
167cdf0e10cSrcweir no_output:
168cdf0e10cSrcweir --srcBuf;
169cdf0e10cSrcweir infoFlags |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
170cdf0e10cSrcweir break;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir if (highSurrogate != 0
173cdf0e10cSrcweir && ((infoFlags
174cdf0e10cSrcweir & (RTL_UNICODETOTEXT_INFO_ERROR
175cdf0e10cSrcweir | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
176cdf0e10cSrcweir == 0))
177cdf0e10cSrcweir {
178cdf0e10cSrcweir if ((flags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0) {
179cdf0e10cSrcweir infoFlags |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
180cdf0e10cSrcweir } else {
181cdf0e10cSrcweir switch (ImplHandleBadInputUnicodeToTextConversion(
182cdf0e10cSrcweir false, 0, flags, &destBufPtr, destBufEnd, &infoFlags, 0,
183cdf0e10cSrcweir 0, 0))
184cdf0e10cSrcweir {
185cdf0e10cSrcweir case IMPL_BAD_INPUT_STOP:
186cdf0e10cSrcweir case IMPL_BAD_INPUT_CONTINUE:
187cdf0e10cSrcweir highSurrogate = 0;
188cdf0e10cSrcweir break;
189cdf0e10cSrcweir
190cdf0e10cSrcweir case IMPL_BAD_INPUT_NO_OUTPUT:
191cdf0e10cSrcweir infoFlags |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
192cdf0e10cSrcweir break;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir }
195cdf0e10cSrcweir }
196cdf0e10cSrcweir if (context != 0) {
197cdf0e10cSrcweir static_cast< ImplUnicodeToTextContext * >(context)->m_nHighSurrogate
198cdf0e10cSrcweir = highSurrogate;
199cdf0e10cSrcweir }
200cdf0e10cSrcweir if (info != 0) {
201cdf0e10cSrcweir *info = infoFlags;
202cdf0e10cSrcweir }
203cdf0e10cSrcweir if (srcCvtChars != 0) {
204cdf0e10cSrcweir *srcCvtChars = converted;
205cdf0e10cSrcweir }
206cdf0e10cSrcweir return destBufPtr - destBuf;
207cdf0e10cSrcweir }
208