xref: /aoo42x/main/sal/textenc/textcvt.c (revision 647f063d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "rtl/textcvt.h"
25 #include "gettextencodingdata.h"
26 #include "tenchelp.h"
27 
28 /* ======================================================================= */
29 
ImplDummyToUnicode(const sal_Char * pSrcBuf,sal_Size nSrcBytes,sal_Unicode * pDestBuf,sal_Size nDestChars,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtBytes)30 static sal_Size ImplDummyToUnicode( const sal_Char* pSrcBuf, sal_Size nSrcBytes,
31                                     sal_Unicode* pDestBuf, sal_Size nDestChars,
32                                     sal_uInt32 nFlags, sal_uInt32* pInfo,
33                                     sal_Size* pSrcCvtBytes )
34 {
35     sal_Unicode*        pEndDestBuf;
36     const sal_Char*     pEndSrcBuf;
37 
38     if ( ((nFlags & RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR) ||
39          ((nFlags & RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK) == RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR) )
40     {
41         *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR |
42                   RTL_TEXTTOUNICODE_INFO_UNDEFINED |
43                   RTL_TEXTTOUNICODE_INFO_MBUNDEFINED;
44         return 0;
45     }
46 
47     *pInfo = 0;
48     pEndDestBuf = pDestBuf+nDestChars;
49     pEndSrcBuf  = pSrcBuf+nSrcBytes;
50     while ( pSrcBuf < pEndSrcBuf )
51     {
52         if ( pDestBuf == pEndDestBuf )
53         {
54             *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
55             break;
56         }
57 
58         *pDestBuf = (sal_Unicode)(sal_uChar)*pSrcBuf;
59         pDestBuf++;
60         pSrcBuf++;
61     }
62 
63     *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
64     return (nDestChars - (pEndDestBuf-pDestBuf));
65 }
66 
67 /* ----------------------------------------------------------------------- */
68 
ImplUnicodeToDummy(const sal_Unicode * pSrcBuf,sal_Size nSrcChars,sal_Char * pDestBuf,sal_Size nDestBytes,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtChars)69 static sal_Size ImplUnicodeToDummy( const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
70                                     sal_Char* pDestBuf, sal_Size nDestBytes,
71                                     sal_uInt32 nFlags, sal_uInt32* pInfo,
72                                     sal_Size* pSrcCvtChars )
73 {
74     sal_Char*               pEndDestBuf;
75     const sal_Unicode*      pEndSrcBuf;
76 
77     if ( ((nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR) )
78     {
79         *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR |
80                   RTL_UNICODETOTEXT_INFO_UNDEFINED;
81         return 0;
82     }
83 
84     *pInfo = 0;
85     pEndDestBuf = pDestBuf+nDestBytes;
86     pEndSrcBuf  = pSrcBuf+nSrcChars;
87     while ( pSrcBuf < pEndSrcBuf )
88     {
89         if ( pDestBuf == pEndDestBuf )
90         {
91             *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
92             break;
93         }
94 
95         *pDestBuf = (sal_Char)(sal_uChar)(*pSrcBuf & 0x00FF);
96         pDestBuf++;
97         pSrcBuf++;
98     }
99 
100     *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
101     return (nDestBytes - (pEndDestBuf-pDestBuf));
102 }
103 
104 /* ======================================================================= */
105 
rtl_createTextToUnicodeConverter(rtl_TextEncoding eTextEncoding)106 rtl_TextToUnicodeConverter SAL_CALL rtl_createTextToUnicodeConverter( rtl_TextEncoding eTextEncoding )
107 {
108     const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
109     if ( pData )
110         return (rtl_TextToUnicodeConverter) &pData->maConverter;
111     else
112         return 0;
113 }
114 
115 /* ----------------------------------------------------------------------- */
116 
rtl_destroyTextToUnicodeConverter(rtl_TextToUnicodeConverter hContext)117 void SAL_CALL rtl_destroyTextToUnicodeConverter( rtl_TextToUnicodeConverter hContext )
118 {
119     (void) hContext; /* unused */
120 }
121 
122 /* ----------------------------------------------------------------------- */
123 
rtl_createTextToUnicodeContext(rtl_TextToUnicodeConverter hConverter)124 rtl_TextToUnicodeContext SAL_CALL rtl_createTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter )
125 {
126     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
127     if ( !pConverter )
128         return 0;
129     else if ( pConverter->mpCreateTextToUnicodeContext )
130         return (rtl_TextToUnicodeContext)pConverter->mpCreateTextToUnicodeContext();
131     else
132         return (rtl_TextToUnicodeContext)1;
133 }
134 
135 /* ----------------------------------------------------------------------- */
136 
rtl_destroyTextToUnicodeContext(rtl_TextToUnicodeConverter hConverter,rtl_TextToUnicodeContext hContext)137 void SAL_CALL rtl_destroyTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
138                                                rtl_TextToUnicodeContext hContext )
139 {
140     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
141     if ( pConverter && hContext && pConverter->mpDestroyTextToUnicodeContext )
142         pConverter->mpDestroyTextToUnicodeContext( (void*)hContext );
143 }
144 
145 /* ----------------------------------------------------------------------- */
146 
rtl_resetTextToUnicodeContext(rtl_TextToUnicodeConverter hConverter,rtl_TextToUnicodeContext hContext)147 void SAL_CALL rtl_resetTextToUnicodeContext( rtl_TextToUnicodeConverter hConverter,
148                                              rtl_TextToUnicodeContext hContext )
149 {
150     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
151     if ( pConverter && hContext && pConverter->mpResetTextToUnicodeContext )
152         pConverter->mpResetTextToUnicodeContext( (void*)hContext );
153 }
154 
155 /* ----------------------------------------------------------------------- */
156 
rtl_convertTextToUnicode(rtl_TextToUnicodeConverter hConverter,rtl_TextToUnicodeContext hContext,const sal_Char * pSrcBuf,sal_Size nSrcBytes,sal_Unicode * pDestBuf,sal_Size nDestChars,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtBytes)157 sal_Size SAL_CALL rtl_convertTextToUnicode( rtl_TextToUnicodeConverter hConverter,
158                                             rtl_TextToUnicodeContext hContext,
159                                             const sal_Char* pSrcBuf, sal_Size nSrcBytes,
160                                             sal_Unicode* pDestBuf, sal_Size nDestChars,
161                                             sal_uInt32 nFlags, sal_uInt32* pInfo,
162                                             sal_Size* pSrcCvtBytes )
163 {
164     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
165 
166     /* Only temporaer, because we don't want die, if we don't have a
167        converter, because not all converters are implemented yet */
168     if ( !pConverter )
169     {
170         return ImplDummyToUnicode( pSrcBuf, nSrcBytes,
171                                    pDestBuf, nDestChars,
172                                    nFlags, pInfo, pSrcCvtBytes );
173     }
174 
175     return pConverter->mpConvertTextToUnicodeProc( pConverter->mpConvertData,
176                                                    (void*)hContext,
177                                                    pSrcBuf, nSrcBytes,
178                                                    pDestBuf, nDestChars,
179                                                    nFlags, pInfo,
180                                                    pSrcCvtBytes );
181 }
182 
183 /* ======================================================================= */
184 
rtl_createUnicodeToTextConverter(rtl_TextEncoding eTextEncoding)185 rtl_UnicodeToTextConverter SAL_CALL rtl_createUnicodeToTextConverter( rtl_TextEncoding eTextEncoding )
186 {
187     const ImplTextEncodingData* pData = Impl_getTextEncodingData( eTextEncoding );
188     if ( pData )
189         return (rtl_TextToUnicodeConverter) &pData->maConverter;
190     else
191         return 0;
192 }
193 
194 /* ----------------------------------------------------------------------- */
195 
rtl_destroyUnicodeToTextConverter(rtl_UnicodeToTextConverter hConverter)196 void SAL_CALL rtl_destroyUnicodeToTextConverter( rtl_UnicodeToTextConverter hConverter )
197 {
198     (void) hConverter; /* unused */
199 }
200 
201 /* ----------------------------------------------------------------------- */
202 
rtl_createUnicodeToTextContext(rtl_UnicodeToTextConverter hConverter)203 rtl_UnicodeToTextContext SAL_CALL rtl_createUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter )
204 {
205     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
206     if ( !pConverter )
207         return 0;
208     else if ( pConverter->mpCreateUnicodeToTextContext )
209         return (rtl_UnicodeToTextContext)pConverter->mpCreateUnicodeToTextContext();
210     else
211         return (rtl_UnicodeToTextContext)1;
212 }
213 
214 /* ----------------------------------------------------------------------- */
215 
rtl_destroyUnicodeToTextContext(rtl_UnicodeToTextConverter hConverter,rtl_UnicodeToTextContext hContext)216 void SAL_CALL rtl_destroyUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
217                                                rtl_UnicodeToTextContext hContext )
218 {
219     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
220     if ( pConverter && hContext && pConverter->mpDestroyUnicodeToTextContext )
221         pConverter->mpDestroyUnicodeToTextContext( (void*)hContext );
222 }
223 
224 /* ----------------------------------------------------------------------- */
225 
rtl_resetUnicodeToTextContext(rtl_UnicodeToTextConverter hConverter,rtl_UnicodeToTextContext hContext)226 void SAL_CALL rtl_resetUnicodeToTextContext( rtl_UnicodeToTextConverter hConverter,
227                                              rtl_UnicodeToTextContext hContext )
228 {
229     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
230     if ( pConverter && hContext && pConverter->mpResetUnicodeToTextContext )
231         pConverter->mpResetUnicodeToTextContext( (void*)hContext );
232 }
233 
234 /* ----------------------------------------------------------------------- */
235 
rtl_convertUnicodeToText(rtl_UnicodeToTextConverter hConverter,rtl_UnicodeToTextContext hContext,const sal_Unicode * pSrcBuf,sal_Size nSrcChars,sal_Char * pDestBuf,sal_Size nDestBytes,sal_uInt32 nFlags,sal_uInt32 * pInfo,sal_Size * pSrcCvtChars)236 sal_Size SAL_CALL rtl_convertUnicodeToText( rtl_UnicodeToTextConverter hConverter,
237                                             rtl_UnicodeToTextContext hContext,
238                                             const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
239                                             sal_Char* pDestBuf, sal_Size nDestBytes,
240                                             sal_uInt32 nFlags, sal_uInt32* pInfo,
241                                             sal_Size* pSrcCvtChars )
242 {
243     const ImplTextConverter* pConverter = (const ImplTextConverter*)hConverter;
244 
245     /* Only temporaer, because we don't want die, if we don't have a
246        converter, because not all converters are implemented yet */
247     if ( !pConverter )
248     {
249         return ImplUnicodeToDummy( pSrcBuf, nSrcChars,
250                                    pDestBuf, nDestBytes,
251                                    nFlags, pInfo, pSrcCvtChars );
252     }
253 
254     return pConverter->mpConvertUnicodeToTextProc( pConverter->mpConvertData,
255                                                    (void*)hContext,
256                                                    pSrcBuf, nSrcChars,
257                                                    pDestBuf, nDestBytes,
258                                                    nFlags, pInfo,
259                                                    pSrcCvtChars );
260 }
261