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