1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20 #include <sal/config.h>
21
22 #include <cassert>
23
24 #include <rtl/textcvt.h>
25 #include <sal/log.hxx>
26
27 #include "gettextencodingdata.hxx"
28 #include "tenchelp.hxx"
29
30 /* ======================================================================= */
31
ImplDummyToUnicode(const char * pSrcBuf,sal_Size nSrcBytes,sal_Unicode * pDestBuf,sal_Size nDestChars,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtBytes)32 static sal_Size ImplDummyToUnicode( const char* pSrcBuf, sal_Size nSrcBytes,
33 sal_Unicode* pDestBuf, sal_Size nDestChars,
34 sal_uInt32 nFlags, sal_uInt32* pInfo,
35 sal_Size* pSrcCvtBytes )
36 {
37 sal_Unicode* pEndDestBuf;
38 const char* pEndSrcBuf;
39
40 if ( ((nFlags & RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR) ||
41 ((nFlags & RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR) )
42 {
43 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR |
44 RTL_TEXTTOUNICODE_INFO_UNDEFINED |
45 RTL_TEXTTOUNICODE_INFO_MBUNDEFINED;
46 return 0;
47 }
48
49 *pInfo = 0;
50 pEndDestBuf = pDestBuf+nDestChars;
51 pEndSrcBuf = pSrcBuf+nSrcBytes;
52 while ( pSrcBuf < pEndSrcBuf )
53 {
54 if ( pDestBuf == pEndDestBuf )
55 {
56 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOOSMALL;
57 break;
58 }
59
60 *pDestBuf = static_cast<sal_Unicode>(static_cast<unsigned char>(*pSrcBuf));
61 pDestBuf++;
62 pSrcBuf++;
63 }
64
65 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
66 return (nDestChars - (pEndDestBuf-pDestBuf));
67 }
68
69 /* ----------------------------------------------------------------------- */
70
ImplUnicodeToDummy(const sal_Unicode * pSrcBuf,sal_Size nSrcChars,char * pDestBuf,sal_Size nDestBytes,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtChars)71 static sal_Size ImplUnicodeToDummy( const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
72 char* pDestBuf, sal_Size nDestBytes,
73 sal_uInt32 nFlags, sal_uInt32* pInfo,
74 sal_Size* pSrcCvtChars )
75 {
76 char* pEndDestBuf;
77 const sal_Unicode* pEndSrcBuf;
78
79 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR )
80 {
81 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR |
82 RTL_UNICODETOTEXT_INFO_UNDEFINED;
83 return 0;
84 }
85
86 *pInfo = 0;
87 pEndDestBuf = pDestBuf+nDestBytes;
88 pEndSrcBuf = pSrcBuf+nSrcChars;
89 while ( pSrcBuf < pEndSrcBuf )
90 {
91 if ( pDestBuf == pEndDestBuf )
92 {
93 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
94 break;
95 }
96
97 *pDestBuf = static_cast<char>(static_cast<unsigned char>(*pSrcBuf & 0x00FF));
98 pDestBuf++;
99 pSrcBuf++;
100 }
101
102 *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
103 return (nDestBytes - (pEndDestBuf-pDestBuf));
104 }
105
106 /* ======================================================================= */
107
rtl_createTextToUnicodeConverter(rtl_TextEncoding eTextEncoding)108 rtl_TextToUnicodeConverter SAL_CALL rtl_createTextToUnicodeConverter( rtl_TextEncoding eTextEncoding )
109 {
110 const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
111 if ( pData )
112 return static_cast<rtl_TextToUnicodeConverter>(const_cast<ImplTextConverter *>(&pData->maConverter));
113 return nullptr;
114 }
115
116 /* ----------------------------------------------------------------------- */
117
rtl_destroyTextToUnicodeConverter(SAL_UNUSED_PARAMETER rtl_TextToUnicodeConverter)118 void SAL_CALL rtl_destroyTextToUnicodeConverter(
119 SAL_UNUSED_PARAMETER rtl_TextToUnicodeConverter )
120 {}
121
122 /* ----------------------------------------------------------------------- */
123
rtl_createTextToUnicodeContext(rtl_TextToUnicodeConverter hConverter)124 rtl_TextToUnicodeContext SAL_CALL rtl_createTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter )
125 {
126 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
127 if ( !pConverter )
128 return nullptr;
129 if ( pConverter->mpCreateTextToUnicodeContext )
130 return pConverter->mpCreateTextToUnicodeContext();
131 return reinterpret_cast<rtl_TextToUnicodeContext>(1);
132 }
133
134 /* ----------------------------------------------------------------------- */
135
rtl_destroyTextToUnicodeContext(rtl_TextToUnicodeConverter hConverter,rtl_TextToUnicodeContext hContext)136 void SAL_CALL rtl_destroyTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
137 rtl_TextToUnicodeContext hContext )
138 {
139 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
140 if ( pConverter && hContext && pConverter->mpDestroyTextToUnicodeContext )
141 pConverter->mpDestroyTextToUnicodeContext( hContext );
142 }
143
144 /* ----------------------------------------------------------------------- */
145
rtl_resetTextToUnicodeContext(rtl_TextToUnicodeConverter hConverter,rtl_TextToUnicodeContext hContext)146 void SAL_CALL rtl_resetTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
147 rtl_TextToUnicodeContext hContext )
148 {
149 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
150 if ( pConverter && hContext && pConverter->mpResetTextToUnicodeContext )
151 pConverter->mpResetTextToUnicodeContext( hContext );
152 }
153
154 /* ----------------------------------------------------------------------- */
155
rtl_convertTextToUnicode(rtl_TextToUnicodeConverter hConverter,rtl_TextToUnicodeContext hContext,const char * pSrcBuf,sal_Size nSrcBytes,sal_Unicode * pDestBuf,sal_Size nDestChars,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtBytes)156 sal_Size SAL_CALL rtl_convertTextToUnicode( rtl_TextToUnicodeConverter hConverter,
157 rtl_TextToUnicodeContext hContext,
158 const char* pSrcBuf, sal_Size nSrcBytes,
159 sal_Unicode* pDestBuf, sal_Size nDestChars,
160 sal_uInt32 nFlags, sal_uInt32* pInfo,
161 sal_Size* pSrcCvtBytes )
162 {
163 assert(
164 (nFlags & RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK)
165 <= RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT);
166 assert(
167 (nFlags & RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK)
168 <= RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT);
169 assert(
170 (nFlags & RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK)
171 <= RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT);
172 assert(
173 (nFlags
174 & ~(RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK
175 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK
176 | RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK
177 | RTL_TEXTTOUNICODE_FLAGS_FLUSH
178 | RTL_TEXTTOUNICODE_FLAGS_GLOBAL_SIGNATURE))
179 == 0);
180
181 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
182
183 /* Only temporary, because we don't want die, if we don't have a
184 converter, because not all converters are implemented yet */
185 if ( !pConverter )
186 {
187 SAL_WARN("sal.textenc", "Missing rtl_TextToUnicodeConverter");
188 return ImplDummyToUnicode( pSrcBuf, nSrcBytes,
189 pDestBuf, nDestChars,
190 nFlags, pInfo, pSrcCvtBytes );
191 }
192
193 return pConverter->mpConvertTextToUnicodeProc( pConverter->mpConvertData,
194 hContext,
195 pSrcBuf, nSrcBytes,
196 pDestBuf, nDestChars,
197 nFlags, pInfo,
198 pSrcCvtBytes );
199 }
200
201 /* ======================================================================= */
202
rtl_createUnicodeToTextConverter(rtl_TextEncoding eTextEncoding)203 rtl_UnicodeToTextConverter SAL_CALL rtl_createUnicodeToTextConverter( rtl_TextEncoding eTextEncoding )
204 {
205 const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
206 if ( pData )
207 return static_cast<rtl_TextToUnicodeConverter>(const_cast<ImplTextConverter *>(&pData->maConverter));
208 return nullptr;
209 }
210
211 /* ----------------------------------------------------------------------- */
212
rtl_destroyUnicodeToTextConverter(SAL_UNUSED_PARAMETER rtl_UnicodeToTextConverter)213 void SAL_CALL rtl_destroyUnicodeToTextConverter(
214 SAL_UNUSED_PARAMETER rtl_UnicodeToTextConverter )
215 {}
216
217 /* ----------------------------------------------------------------------- */
218
rtl_createUnicodeToTextContext(rtl_UnicodeToTextConverter hConverter)219 rtl_UnicodeToTextContext SAL_CALL rtl_createUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter )
220 {
221 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
222 if ( !pConverter )
223 return nullptr;
224 if ( pConverter->mpCreateUnicodeToTextContext )
225 return pConverter->mpCreateUnicodeToTextContext();
226 return reinterpret_cast<rtl_UnicodeToTextContext>(1);
227 }
228
229 /* ----------------------------------------------------------------------- */
230
rtl_destroyUnicodeToTextContext(rtl_UnicodeToTextConverter hConverter,rtl_UnicodeToTextContext hContext)231 void SAL_CALL rtl_destroyUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
232 rtl_UnicodeToTextContext hContext )
233 {
234 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
235 if ( pConverter && hContext && pConverter->mpDestroyUnicodeToTextContext )
236 pConverter->mpDestroyUnicodeToTextContext( hContext );
237 }
238
239 /* ----------------------------------------------------------------------- */
240
rtl_resetUnicodeToTextContext(rtl_UnicodeToTextConverter hConverter,rtl_UnicodeToTextContext hContext)241 void SAL_CALL rtl_resetUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
242 rtl_UnicodeToTextContext hContext )
243 {
244 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
245 if ( pConverter && hContext && pConverter->mpResetUnicodeToTextContext )
246 pConverter->mpResetUnicodeToTextContext( hContext );
247 }
248
249 /* ----------------------------------------------------------------------- */
250
rtl_convertUnicodeToText(rtl_UnicodeToTextConverter hConverter,rtl_UnicodeToTextContext hContext,const sal_Unicode * pSrcBuf,sal_Size nSrcChars,char * pDestBuf,sal_Size nDestBytes,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtChars)251 sal_Size SAL_CALL rtl_convertUnicodeToText( rtl_UnicodeToTextConverter hConverter,
252 rtl_UnicodeToTextContext hContext,
253 const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
254 char* pDestBuf, sal_Size nDestBytes,
255 sal_uInt32 nFlags, sal_uInt32* pInfo,
256 sal_Size* pSrcCvtChars )
257 {
258 assert(
259 (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK)
260 <= RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT);
261 assert(
262 (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK)
263 <= RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT);
264 assert(
265 (nFlags
266 & ~(RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK
267 | RTL_UNICODETOTEXT_FLAGS_INVALID_MASK
268 | RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE
269 | RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACESTR
270 | RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0
271 | RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE
272 | RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE
273 | RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE
274 | RTL_UNICODETOTEXT_FLAGS_NOCOMPOSITE
275 | RTL_UNICODETOTEXT_FLAGS_FLUSH
276 | RTL_UNICODETOTEXT_FLAGS_GLOBAL_SIGNATURE))
277 == 0);
278
279 const ImplTextConverter* pConverter = static_cast<const ImplTextConverter*>(hConverter);
280
281 /* Only temporary, because we don't want die, if we don't have a
282 converter, because not all converters are implemented yet */
283 if ( !pConverter )
284 {
285 SAL_WARN("sal.textenc", "Missing rtl_UnicodeToTextConverter");
286 return ImplUnicodeToDummy( pSrcBuf, nSrcChars,
287 pDestBuf, nDestBytes,
288 nFlags, pInfo, pSrcCvtChars );
289 }
290
291 return pConverter->mpConvertUnicodeToTextProc( pConverter->mpConvertData,
292 hContext,
293 pSrcBuf, nSrcChars,
294 pDestBuf, nDestBytes,
295 nFlags, pInfo,
296 pSrcCvtChars );
297 }
298
299 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
300