Bump for 3.6-28
[LibreOffice.git] / sal / textenc / handleundefinedunicodetotextchar.cxx
blobcebe302d3e5f2cf0ea33e084af2a1363bd45db27
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * Major Contributor(s):
16 * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 * (initial developer) ]
19 * All Rights Reserved.
21 * For minor contributions see the git repository.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 * instead of those above.
30 #include "sal/config.h"
32 #include "rtl/textcvt.h"
33 #include "sal/types.h"
35 #include "handleundefinedunicodetotextchar.hxx"
36 #include "tenchelp.hxx"
38 namespace {
40 bool ImplIsUnicodeIgnoreChar(sal_Unicode c, sal_uInt32 nFlags)
42 return
43 ((nFlags & RTL_UNICODETOTEXT_FLAGS_NONSPACING_IGNORE) != 0
44 && ImplIsZeroWidth(c))
45 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_CONTROL_IGNORE) != 0
46 && ImplIsControlOrFormat(c))
47 || ((nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_IGNORE) != 0
48 && ImplIsPrivateUse(c));
51 bool ImplGetUndefinedAsciiMultiByte(sal_uInt32 nFlags,
52 char * pBuf,
53 sal_Size nMaxLen)
55 if (nMaxLen == 0)
56 return false;
57 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK)
59 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_0:
60 *pBuf = 0x00;
61 break;
63 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_QUESTIONMARK:
64 default: /* RTL_UNICODETOTEXT_FLAGS_UNDEFINED_DEFAULT */
65 *pBuf = 0x3F;
66 break;
68 case RTL_UNICODETOTEXT_FLAGS_UNDEFINED_UNDERLINE:
69 *pBuf = 0x5F;
70 break;
72 return true;
75 bool ImplGetInvalidAsciiMultiByte(sal_uInt32 nFlags,
76 char * pBuf,
77 sal_Size nMaxLen)
79 if (nMaxLen == 0)
80 return false;
81 switch (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK)
83 case RTL_UNICODETOTEXT_FLAGS_INVALID_0:
84 *pBuf = 0x00;
85 break;
87 case RTL_UNICODETOTEXT_FLAGS_INVALID_QUESTIONMARK:
88 default: /* RTL_UNICODETOTEXT_FLAGS_INVALID_DEFAULT */
89 *pBuf = 0x3F;
90 break;
92 case RTL_UNICODETOTEXT_FLAGS_INVALID_UNDERLINE:
93 *pBuf = 0x5F;
94 break;
96 return true;
101 bool sal::detail::textenc::handleUndefinedUnicodeToTextChar(
102 sal_Unicode const ** ppSrcBuf, sal_Unicode const * pEndSrcBuf,
103 char ** ppDestBuf, char const * pEndDestBuf, sal_uInt32 nFlags,
104 sal_uInt32 * pInfo)
106 sal_Unicode c = **ppSrcBuf;
108 /* Should the private character map to one byte */
109 if ( (c >= RTL_TEXTCVT_BYTE_PRIVATE_START) && (c <= RTL_TEXTCVT_BYTE_PRIVATE_END) )
111 if ( nFlags & RTL_UNICODETOTEXT_FLAGS_PRIVATE_MAPTO0 )
113 **ppDestBuf = (char)(sal_uChar)(c-RTL_TEXTCVT_BYTE_PRIVATE_START);
114 (*ppDestBuf)++;
115 (*ppSrcBuf)++;
116 return true;
120 /* Should this character ignored (Private, Non Spacing, Control) */
121 if ( ImplIsUnicodeIgnoreChar( c, nFlags ) )
123 (*ppSrcBuf)++;
124 return true;
127 /* Surrogates Characters should result in */
128 /* one replacement character */
129 if (ImplIsHighSurrogate(c))
131 if ( *ppSrcBuf == pEndSrcBuf )
133 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR | RTL_UNICODETOTEXT_INFO_SRCBUFFERTOSMALL;
134 return false;
137 c = *((*ppSrcBuf)+1);
138 if (ImplIsLowSurrogate(c))
139 (*ppSrcBuf)++;
140 else
142 *pInfo |= RTL_UNICODETOTEXT_INFO_INVALID;
143 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR )
145 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
146 return false;
148 else if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_INVALID_MASK) == RTL_UNICODETOTEXT_FLAGS_INVALID_IGNORE )
150 (*ppSrcBuf)++;
151 return true;
153 else if (ImplGetInvalidAsciiMultiByte(nFlags,
154 *ppDestBuf,
155 pEndDestBuf - *ppDestBuf))
157 ++*ppSrcBuf;
158 ++*ppDestBuf;
159 return true;
161 else
163 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR
164 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
165 return false;
170 *pInfo |= RTL_UNICODETOTEXT_INFO_UNDEFINED;
171 if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR )
173 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR;
174 return false;
176 else if ( (nFlags & RTL_UNICODETOTEXT_FLAGS_UNDEFINED_MASK) == RTL_UNICODETOTEXT_FLAGS_UNDEFINED_IGNORE )
177 (*ppSrcBuf)++;
178 else if (ImplGetUndefinedAsciiMultiByte(nFlags,
179 *ppDestBuf,
180 pEndDestBuf - *ppDestBuf))
182 ++*ppSrcBuf;
183 ++*ppDestBuf;
185 else
187 *pInfo |= RTL_UNICODETOTEXT_INFO_ERROR
188 | RTL_UNICODETOTEXT_INFO_DESTBUFFERTOSMALL;
189 return false;
192 return true;