Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / textenc / tcvtbyte.cxx
blob118ec9c4f1b21322c049185a847023f30915bd15
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include "rtl/textcvt.h"
24 #include "handleundefinedunicodetotextchar.hxx"
25 #include "tcvtbyte.hxx"
26 #include "tenchelp.hxx"
28 sal_Size ImplSymbolToUnicode( SAL_UNUSED_PARAMETER const void*,
29 SAL_UNUSED_PARAMETER void*,
30 const char* pSrcBuf, sal_Size nSrcBytes,
31 sal_Unicode* pDestBuf, sal_Size nDestChars,
32 SAL_UNUSED_PARAMETER sal_uInt32,
33 sal_uInt32* pInfo, sal_Size* pSrcCvtBytes )
35 sal_uChar c;
36 sal_Unicode* pEndDestBuf;
37 const char* pEndSrcBuf;
39 *pInfo = 0;
40 pEndDestBuf = pDestBuf+nDestChars;
41 pEndSrcBuf = pSrcBuf+nSrcBytes;
42 while ( pSrcBuf < pEndSrcBuf )
44 if ( pDestBuf == pEndDestBuf )
46 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
47 break;
50 /* 0-31 (all Control-Character get the same Unicode value) */
51 c = (sal_uChar)*pSrcBuf;
52 if ( c <= 0x1F )
53 *pDestBuf = (sal_Unicode)c;
54 else
55 *pDestBuf = ((sal_Unicode)c)+0xF000;
56 pDestBuf++;
57 pSrcBuf++;
60 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
61 return (nDestChars - (pEndDestBuf-pDestBuf));
64 sal_Size ImplUnicodeToSymbol( SAL_UNUSED_PARAMETER const void*,
65 SAL_UNUSED_PARAMETER void*,
66 const sal_Unicode* pSrcBuf, sal_Size nSrcChars,
67 char* pDestBuf, sal_Size nDestBytes,
68 sal_uInt32 nFlags, sal_uInt32* pInfo,
69 sal_Size* pSrcCvtChars )
71 sal_Unicode c;
72 char* pEndDestBuf;
73 const sal_Unicode* pEndSrcBuf;
75 *pInfo = 0;
76 pEndDestBuf = pDestBuf+nDestBytes;
77 pEndSrcBuf = pSrcBuf+nSrcChars;
78 while ( pSrcBuf < pEndSrcBuf )
80 if ( pDestBuf == pEndDestBuf )
82 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
83 break;
86 c = *pSrcBuf;
87 if ( (c >= 0xF000) && (c <= 0xF0FF) )
89 *pDestBuf = static_cast< char >(static_cast< unsigned char >(c-0xF000));
90 pDestBuf++;
91 pSrcBuf++;
93 // Normally 0x001F, but in many cases also symbol characters
94 // are stored in the first 256 bytes, so that we don't change
95 // these values
96 else if ( c <= 0x00FF )
98 *pDestBuf = static_cast< char >(static_cast< unsigned char >(c));
99 pDestBuf++;
100 pSrcBuf++;
102 else
104 if ( nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE )
106 /* !!! */
107 /* Only ascii characters < 0x1F */
110 /* Handle undefined and surrogates characters */
111 /* (all surrogates characters are undefined) */
112 if (!sal::detail::textenc::handleUndefinedUnicodeToTextChar(
113 &pSrcBuf, pEndSrcBuf, &pDestBuf, pEndDestBuf, nFlags,
114 pInfo))
115 break;
119 *pSrcCvtChars = nSrcChars - (pEndSrcBuf-pSrcBuf);
120 return (nDestBytes - (pEndDestBuf-pDestBuf));
123 sal_Size ImplUpperCharToUnicode( const void* pData,
124 SAL_UNUSED_PARAMETER void*,
125 const char* pSrcBuf, sal_Size nSrcBytes,
126 sal_Unicode* pDestBuf, sal_Size nDestChars,
127 SAL_UNUSED_PARAMETER sal_uInt32, sal_uInt32* pInfo,
128 sal_Size* pSrcCvtBytes )
130 sal_uChar c;
131 sal_Unicode cConv;
132 const ImplByteConvertData* pConvertData = (const ImplByteConvertData*)pData;
133 sal_Unicode* pEndDestBuf;
134 const char* pEndSrcBuf;
136 *pInfo = 0;
137 pEndDestBuf = pDestBuf+nDestChars;
138 pEndSrcBuf = pSrcBuf+nSrcBytes;
139 if ( pDestBuf == pEndDestBuf )
141 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR | RTL_TEXTTOUNICODE_INFO_DESTBUFFERTOSMALL;
142 *pSrcCvtBytes = 0;
143 return 0;
145 while ( pSrcBuf < pEndSrcBuf )
147 c = (sal_uChar)*pSrcBuf;
148 if (c < 0x80)
149 cConv = c;
150 else
151 // c <= 0xFF is implied.
152 cConv = pConvertData->mpToUniTab1[c - 0x80];
154 *pDestBuf = cConv;
155 pDestBuf++;
156 pSrcBuf++;
159 *pSrcCvtBytes = nSrcBytes - (pEndSrcBuf-pSrcBuf);
160 return (nDestChars - (pEndDestBuf-pDestBuf));
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */