Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "sal/config.h"
30 : :
31 : : #include <cstddef>
32 : :
33 : : #include "osl/diagnose.h"
34 : : #include "rtl/textcvt.h"
35 : : #include "sal/types.h"
36 : :
37 : : #include "context.hxx"
38 : : #include "converter.hxx"
39 : : #include "convertsinglebytetobmpunicode.hxx"
40 : : #include "unichars.hxx"
41 : :
42 : 1788 : sal_Size rtl_textenc_convertSingleByteToBmpUnicode(
43 : : void const * data, SAL_UNUSED_PARAMETER void *, sal_Char const * srcBuf,
44 : : sal_Size srcBytes, sal_Unicode * destBuf, sal_Size destChars,
45 : : sal_uInt32 flags, sal_uInt32 * info, sal_Size * srcCvtBytes)
46 : : {
47 : : sal_Unicode const * map = static_cast<
48 : : rtl::textenc::BmpUnicodeToSingleByteConverterData const * >(
49 : 1788 : data)->byteToUnicode;
50 : 1788 : sal_uInt32 infoFlags = 0;
51 : 1788 : sal_Size converted = 0;
52 : 1788 : sal_Unicode * destBufPtr = destBuf;
53 : 1788 : sal_Unicode * destBufEnd = destBuf + destChars;
54 [ + + ]: 3576 : for (; converted < srcBytes; ++converted) {
55 : 1788 : bool undefined = true;
56 : 1788 : sal_Char b = *srcBuf++;
57 : 1788 : sal_Unicode c = map[static_cast< sal_uInt8 >(b)];
58 [ - + ]: 1788 : if (c == 0xFFFF) {
59 : 0 : goto bad_input;
60 : : }
61 [ - + ]: 1788 : if (destBufEnd - destBufPtr < 1) {
62 : 0 : goto no_output;
63 : : }
64 : 1788 : *destBufPtr++ = c;
65 : 1788 : continue;
66 : : bad_input:
67 [ # # # # ]: 0 : switch (sal::detail::textenc::handleBadInputTextToUnicodeConversion(
68 : : undefined, false, b, flags, &destBufPtr, destBufEnd,
69 [ # # ]: 0 : &infoFlags))
70 : : {
71 : : case sal::detail::textenc::BAD_INPUT_STOP:
72 : 0 : break;
73 : :
74 : : case sal::detail::textenc::BAD_INPUT_CONTINUE:
75 : 0 : continue;
76 : :
77 : : case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
78 : 0 : goto no_output;
79 : : }
80 : 0 : break;
81 : : no_output:
82 : 0 : --srcBuf;
83 : 0 : infoFlags |= RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
84 : 0 : break;
85 : : }
86 [ + - ]: 1788 : if (info != 0) {
87 : 1788 : *info = infoFlags;
88 : : }
89 [ + - ]: 1788 : if (srcCvtBytes != 0) {
90 : 1788 : *srcCvtBytes = converted;
91 : : }
92 : 1788 : return destBufPtr - destBuf;
93 : : }
94 : :
95 : 0 : sal_Size rtl_textenc_convertBmpUnicodeToSingleByte(
96 : : void const * data, void * context,
97 : : sal_Unicode const * srcBuf, sal_Size srcChars, sal_Char * destBuf,
98 : : sal_Size destBytes, sal_uInt32 flags, sal_uInt32 * info,
99 : : sal_Size * srcCvtChars)
100 : : {
101 : : std::size_t entries = static_cast<
102 : : rtl::textenc::BmpUnicodeToSingleByteConverterData const * >(
103 : 0 : data)->unicodeToByteEntries;
104 : : rtl::textenc::BmpUnicodeToSingleByteRange const * ranges = static_cast<
105 : : rtl::textenc::BmpUnicodeToSingleByteConverterData const * >(
106 : 0 : data)->unicodeToByte;
107 : 0 : sal_Unicode highSurrogate = 0;
108 : 0 : sal_uInt32 infoFlags = 0;
109 : 0 : sal_Size converted = 0;
110 : 0 : sal_Char * destBufPtr = destBuf;
111 : 0 : sal_Char * destBufEnd = destBuf + destBytes;
112 [ # # ]: 0 : if (context != 0) {
113 : : highSurrogate = static_cast< ImplUnicodeToTextContext * >(context)->
114 : 0 : m_nHighSurrogate;
115 : : }
116 [ # # ]: 0 : for (; converted < srcChars; ++converted) {
117 : 0 : bool undefined = true;
118 : 0 : sal_uInt32 c = *srcBuf++;
119 [ # # ]: 0 : if (highSurrogate == 0) {
120 [ # # ]: 0 : if (ImplIsHighSurrogate(c)) {
121 : 0 : highSurrogate = static_cast< sal_Unicode >(c);
122 : 0 : continue;
123 : : }
124 [ # # ]: 0 : } else if (ImplIsLowSurrogate(c)) {
125 : 0 : c = ImplCombineSurrogates(highSurrogate, c);
126 : : } else {
127 : 0 : undefined = false;
128 : 0 : goto bad_input;
129 : : }
130 [ # # ][ # # ]: 0 : if (ImplIsLowSurrogate(c) || ImplIsNoncharacter(c)) {
[ # # ]
131 : 0 : undefined = false;
132 : 0 : goto bad_input;
133 : : }
134 : : // Linearly searching through the ranges if probably fastest, assuming
135 : : // that most converted characters belong to the ASCII subset:
136 [ # # ]: 0 : for (std::size_t i = 0; i < entries; ++i) {
137 [ # # ]: 0 : if (c < ranges[i].unicode) {
138 : 0 : break;
139 [ # # ]: 0 : } else if (c <= sal::static_int_cast< sal_uInt32 >(
140 : 0 : ranges[i].unicode + ranges[i].range))
141 : : {
142 [ # # ]: 0 : if (destBufEnd - destBufPtr < 1) {
143 : 0 : goto no_output;
144 : : }
145 : : *destBufPtr++ = static_cast< sal_Char >(
146 : 0 : ranges[i].byte + (c - ranges[i].unicode));
147 : 0 : goto done;
148 : : }
149 : : }
150 : 0 : goto bad_input;
151 : : done:
152 : 0 : highSurrogate = 0;
153 : 0 : continue;
154 : : bad_input:
155 [ # # # # ]: 0 : switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
156 : : undefined, c, flags, &destBufPtr, destBufEnd, &infoFlags, 0,
157 [ # # ]: 0 : 0, 0))
158 : : {
159 : : case sal::detail::textenc::BAD_INPUT_STOP:
160 : 0 : highSurrogate = 0;
161 : 0 : break;
162 : :
163 : : case sal::detail::textenc::BAD_INPUT_CONTINUE:
164 : 0 : highSurrogate = 0;
165 : 0 : continue;
166 : :
167 : : case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
168 : 0 : goto no_output;
169 : : }
170 : 0 : break;
171 : : no_output:
172 : 0 : --srcBuf;
173 : 0 : infoFlags |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
174 : 0 : break;
175 : : }
176 [ # # ][ # # ]: 0 : if (highSurrogate != 0
177 : : && ((infoFlags
178 : : & (RTL_UNICODETOTEXT_INFO_ERROR
179 : : | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL))
180 : : == 0))
181 : : {
182 [ # # ]: 0 : if ((flags & RTL_UNICODETOTEXT_FLAGS_FLUSH) != 0) {
183 : 0 : infoFlags |= RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
184 : : } else {
185 [ # # # ]: 0 : switch (sal::detail::textenc::handleBadInputUnicodeToTextConversion(
186 : : false, 0, flags, &destBufPtr, destBufEnd, &infoFlags, 0,
187 [ # # ]: 0 : 0, 0))
188 : : {
189 : : case sal::detail::textenc::BAD_INPUT_STOP:
190 : : case sal::detail::textenc::BAD_INPUT_CONTINUE:
191 : 0 : highSurrogate = 0;
192 : 0 : break;
193 : :
194 : : case sal::detail::textenc::BAD_INPUT_NO_OUTPUT:
195 : 0 : infoFlags |= RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
196 : 0 : break;
197 : : }
198 : : }
199 : : }
200 [ # # ]: 0 : if (context != 0) {
201 : : static_cast< ImplUnicodeToTextContext * >(context)->m_nHighSurrogate
202 : 0 : = highSurrogate;
203 : : }
204 [ # # ]: 0 : if (info != 0) {
205 : 0 : *info = infoFlags;
206 : : }
207 [ # # ]: 0 : if (srcCvtChars != 0) {
208 : 0 : *srcCvtChars = converted;
209 : : }
210 : 0 : return destBufPtr - destBuf;
211 : : }
212 : :
213 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|