Avoid potential negative array index access to cached text.
[LibreOffice.git] / tools / source / reversemap / bestreversemap.cxx
blobf124140c19180117033f5e93693553401dad3ec5
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/.
8 */
10 #include <sal/config.h>
11 #include <rtl/textcvt.h>
13 #include <cstdlib>
14 #include <iterator>
15 #include <stdio.h>
17 namespace {
19 struct Encoder
21 rtl_UnicodeToTextConverter m_aConverter;
22 bool m_bCapable;
23 const char *m_pEncoding;
24 Encoder(rtl_TextEncoding nEncoding, const char *pEncoding)
25 : m_aConverter(rtl_createUnicodeToTextConverter(nEncoding))
26 , m_bCapable(true)
27 , m_pEncoding(pEncoding)
30 ~Encoder()
32 rtl_destroyUnicodeToTextConverter(m_aConverter);
34 void checkSupports(sal_Unicode c)
36 char aTempArray[8];
37 sal_Size nTempSize;
38 sal_uInt32 nCvtInfo;
40 sal_Size nChars = rtl_convertUnicodeToText(m_aConverter,
41 nullptr, &c, 1, aTempArray, sizeof(aTempArray),
42 RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR,
43 &nCvtInfo, &nTempSize);
44 m_bCapable = nChars > 0;
46 void reset()
48 m_bCapable = true;
50 bool isOK() const
52 return m_bCapable;
54 const char* getName() const
56 return m_pEncoding;
63 int main()
65 # define EXP(x) Encoder(x, #x)
67 Encoder aConverters[15] =
69 EXP(RTL_TEXTENCODING_MS_1361),
70 EXP(RTL_TEXTENCODING_MS_950),
71 EXP(RTL_TEXTENCODING_MS_949),
72 EXP(RTL_TEXTENCODING_MS_936),
73 EXP(RTL_TEXTENCODING_MS_932),
74 EXP(RTL_TEXTENCODING_MS_874),
75 EXP(RTL_TEXTENCODING_MS_1258),
76 EXP(RTL_TEXTENCODING_MS_1257),
77 EXP(RTL_TEXTENCODING_MS_1256),
78 EXP(RTL_TEXTENCODING_MS_1255),
79 EXP(RTL_TEXTENCODING_MS_1254),
80 EXP(RTL_TEXTENCODING_MS_1253),
81 EXP(RTL_TEXTENCODING_MS_1251),
82 EXP(RTL_TEXTENCODING_MS_1250),
83 EXP(RTL_TEXTENCODING_MS_1252)
86 printf("//Do not edit manually, generated from bestreversemap.cxx\n");
87 printf("#include <rtl/textenc.h>\n");
88 printf("#include <tools/tenccvt.hxx>\n");
89 printf("rtl_TextEncoding getBestMSEncodingByChar(sal_Unicode c)\n");
90 printf("{\n");
92 sal_Unicode c = 0;
93 while (c < 0xFFFF)
95 for (size_t i = 0; i < std::size(aConverters); ++i)
96 aConverters[i].reset();
98 int nMostCapable = -1;
100 while(c < 0xFFFF)
102 bool bSomethingCapable = false;
103 for (size_t i = 0; i < std::size(aConverters); ++i)
105 if (aConverters[i].isOK())
106 aConverters[i].checkSupports(c);
107 if (aConverters[i].isOK())
109 bSomethingCapable = true;
110 nMostCapable = i;
113 if (!bSomethingCapable)
114 break;
115 ++c;
117 sal_Unicode cEnd = c;
118 printf(" if (c < 0x%x)\n", c);
119 printf(" return %s;\n", aConverters[nMostCapable].getName());
120 while(c < 0xFFFF)
122 bool bNothingCapable = true;
123 for (size_t i = 0; i < std::size(aConverters); ++i)
125 aConverters[i].checkSupports(c);
126 if (aConverters[i].isOK())
128 bNothingCapable = false;
129 break;
132 if (!bNothingCapable)
133 break;
134 ++c;
136 if (cEnd != c)
138 if (c < 0xFFFF)
140 printf(" if (c < 0x%x)\n", c);
141 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
143 else
144 printf(" return RTL_TEXTENCODING_DONTKNOW;\n");
148 printf("}\n");
149 fflush(stdout);
151 return EXIT_SUCCESS;
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */