Fix GNU C++ version check
[LibreOffice.git] / sal / textenc / converter.cxx
blob60e6a33837085cb943a3f997a0d6924d8cea7016
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 <o3tl/safeint.hxx>
23 #include <rtl/textcvt.h>
24 #include <sal/types.h>
26 #include "converter.hxx"
27 #include "tenchelp.hxx"
28 #include "unichars.hxx"
30 sal::detail::textenc::BadInputConversionAction
31 sal::detail::textenc::handleBadInputTextToUnicodeConversion(
32 bool bUndefined, bool bMultiByte, char cByte, sal_uInt32 nFlags,
33 sal_Unicode ** pDestBufPtr, const sal_Unicode * pDestBufEnd, sal_uInt32 * pInfo)
35 *pInfo |= bUndefined
36 ? (bMultiByte
37 ? RTL_TEXTTOUNICODE_INFO_MBUNDEFINED
38 : RTL_TEXTTOUNICODE_INFO_UNDEFINED)
39 : RTL_TEXTTOUNICODE_INFO_INVALID;
40 switch (nFlags
41 & (bUndefined
42 ? (bMultiByte
43 ? RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_MASK
44 : RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MASK)
45 : RTL_TEXTTOUNICODE_FLAGS_INVALID_MASK))
47 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR:
48 case RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR:
49 case RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR:
50 *pInfo |= RTL_TEXTTOUNICODE_INFO_ERROR;
51 return BAD_INPUT_STOP;
53 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_IGNORE:
54 case RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_IGNORE:
55 case RTL_TEXTTOUNICODE_FLAGS_INVALID_IGNORE:
56 return BAD_INPUT_CONTINUE;
58 case RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_MAPTOPRIVATE:
59 if (*pDestBufPtr != pDestBufEnd)
61 *(*pDestBufPtr)++ = RTL_TEXTCVT_BYTE_PRIVATE_START
62 | static_cast<unsigned char>(cByte);
63 return BAD_INPUT_CONTINUE;
65 else
66 return BAD_INPUT_NO_OUTPUT;
68 default: // RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT,
69 // RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT,
70 // RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
71 if (*pDestBufPtr != pDestBufEnd)
73 *(*pDestBufPtr)++ = RTL_TEXTENC_UNICODE_REPLACEMENT_CHARACTER;
74 return BAD_INPUT_CONTINUE;
76 else
77 return BAD_INPUT_NO_OUTPUT;
81 sal::detail::textenc::BadInputConversionAction
82 sal::detail::textenc::handleBadInputUnicodeToTextConversion(
83 bool bUndefined, sal_uInt32 nUtf32, sal_uInt32 nFlags, char ** pDestBufPtr,
84 const char * pDestBufEnd, sal_uInt32 * pInfo, char const * pPrefix,
85 sal_Size nPrefixLen, bool * pPrefixWritten)
87 // TODO! RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACE
88 // RTL_UNICODETOTEXT_FLAGS_UNDEFINED_REPLACESTR
90 char cReplace;
92 if (bUndefined)
94 if (ImplIsControlOrFormat(nUtf32))
96 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE) != 0)
97 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
99 else if (ImplIsPrivateUse(nUtf32))
101 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE) != 0)
102 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
103 else if ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0) != 0)
104 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0;
106 else if (ImplIsZeroWidth(nUtf32))
108 if ((nFlags & RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE) != 0)
109 nFlags = RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE;
112 *pInfo |= bUndefined ? RTL_UNICODETOTEXT_INFO_UNDEFINED :
113 RTL_UNICODETOTEXT_INFO_INVALID;
114 switch (nFlags & (bUndefined ? RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK :
115 RTL_UNICODETOTEXT_FLAGS_INVALID_MASK))
117 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR:
118 case RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR:
119 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
120 return BAD_INPUT_STOP;
122 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE:
123 case RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE:
124 if (pPrefixWritten)
125 *pPrefixWritten = false;
126 return BAD_INPUT_CONTINUE;
128 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0:
129 case RTL_UNICODETOTEXT_FLAGS_INVALID_0:
130 cReplace = 0;
131 break;
133 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK:
134 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK:
135 default: // RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT,
136 // RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT
137 cReplace = '?';
138 break;
140 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE:
141 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE:
142 cReplace = '_';
143 break;
145 if (o3tl::make_unsigned(pDestBufEnd - *pDestBufPtr) > nPrefixLen)
147 while (nPrefixLen-- > 0)
148 *(*pDestBufPtr)++ = *pPrefix++;
149 *(*pDestBufPtr)++ = cReplace;
150 if (pPrefixWritten)
151 *pPrefixWritten = true;
152 return BAD_INPUT_CONTINUE;
154 return BAD_INPUT_NO_OUTPUT;
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */