1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qlatincodec_p.h"
41 #include "qlist.h"
42 
43 QT_BEGIN_NAMESPACE
44 
~QLatin1Codec()45 QLatin1Codec::~QLatin1Codec()
46 {
47 }
48 
convertToUnicode(const char * chars,int len,ConverterState *) const49 QString QLatin1Codec::convertToUnicode(const char *chars, int len, ConverterState *) const
50 {
51     if (chars == nullptr)
52         return QString();
53 
54     return QString::fromLatin1(chars, len);
55 }
56 
57 
convertFromUnicode(const QChar * ch,int len,ConverterState * state) const58 QByteArray QLatin1Codec::convertFromUnicode(const QChar *ch, int len, ConverterState *state) const
59 {
60     const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
61     QByteArray r(len, Qt::Uninitialized);
62     char *d = r.data();
63     int invalid = 0;
64     for (int i = 0; i < len; ++i) {
65         if (ch[i] > QChar(0xff)) {
66             d[i] = replacement;
67             ++invalid;
68         } else {
69             d[i] = (char)ch[i].cell();
70         }
71     }
72     if (state) {
73         state->invalidChars += invalid;
74     }
75     return r;
76 }
77 
name() const78 QByteArray QLatin1Codec::name() const
79 {
80     return "ISO-8859-1";
81 }
82 
aliases() const83 QList<QByteArray> QLatin1Codec::aliases() const
84 {
85     QList<QByteArray> list;
86     list << "latin1"
87          << "CP819"
88          << "IBM819"
89          << "iso-ir-100"
90          << "csISOLatin1";
91     return list;
92 }
93 
94 
mibEnum() const95 int QLatin1Codec::mibEnum() const
96 {
97     return 4;
98 }
99 
100 
~QLatin15Codec()101 QLatin15Codec::~QLatin15Codec()
102 {
103 }
104 
convertToUnicode(const char * chars,int len,ConverterState *) const105 QString QLatin15Codec::convertToUnicode(const char* chars, int len, ConverterState *) const
106 {
107     if (chars == nullptr)
108         return QString();
109 
110     QString str = QString::fromLatin1(chars, len);
111     QChar *uc = str.data();
112     while(len--) {
113         switch(uc->unicode()) {
114             case 0xa4:
115                 *uc = QChar(0x20ac);
116                 break;
117             case 0xa6:
118                 *uc = QChar(0x0160);
119                 break;
120             case 0xa8:
121                 *uc = QChar(0x0161);
122                 break;
123             case 0xb4:
124                 *uc = QChar(0x017d);
125                 break;
126             case 0xb8:
127                 *uc = QChar(0x017e);
128                 break;
129             case 0xbc:
130                 *uc = QChar(0x0152);
131                 break;
132             case 0xbd:
133                 *uc = QChar(0x0153);
134                 break;
135             case 0xbe:
136                 *uc = QChar(0x0178);
137                 break;
138             default:
139                 break;
140         }
141         uc++;
142     }
143     return str;
144 }
145 
convertFromUnicode(const QChar * in,int length,ConverterState * state) const146 QByteArray QLatin15Codec::convertFromUnicode(const QChar *in, int length, ConverterState *state) const
147 {
148     const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?';
149     QByteArray r(length, Qt::Uninitialized);
150     char *d = r.data();
151     int invalid = 0;
152     for (int i = 0; i < length; ++i) {
153         uchar c;
154         ushort uc = in[i].unicode();
155         if (uc < 0x0100) {
156             if (uc > 0xa3) {
157                 switch(uc) {
158                 case 0xa4:
159                 case 0xa6:
160                 case 0xa8:
161                 case 0xb4:
162                 case 0xb8:
163                 case 0xbc:
164                 case 0xbd:
165                 case 0xbe:
166                     c = replacement;
167                     ++invalid;
168                     break;
169                 default:
170                     c = (unsigned char) uc;
171                     break;
172                 }
173             } else {
174                 c = (unsigned char) uc;
175             }
176         } else {
177             if (uc == 0x20ac)
178                 c = 0xa4;
179             else if ((uc & 0xff00) == 0x0100) {
180                 switch(uc) {
181                 case 0x0160:
182                     c = 0xa6;
183                     break;
184                 case 0x0161:
185                     c = 0xa8;
186                     break;
187                 case 0x017d:
188                     c = 0xb4;
189                     break;
190                 case 0x017e:
191                     c = 0xb8;
192                     break;
193                 case 0x0152:
194                     c = 0xbc;
195                     break;
196                 case 0x0153:
197                     c = 0xbd;
198                     break;
199                 case 0x0178:
200                     c = 0xbe;
201                     break;
202                 default:
203                     c = replacement;
204                     ++invalid;
205                 }
206             } else {
207                 c = replacement;
208                 ++invalid;
209             }
210         }
211         d[i] = (char)c;
212     }
213     if (state) {
214         state->remainingChars = 0;
215         state->invalidChars += invalid;
216     }
217     return r;
218 }
219 
220 
name() const221 QByteArray QLatin15Codec::name() const
222 {
223     return "ISO-8859-15";
224 }
225 
aliases() const226 QList<QByteArray> QLatin15Codec::aliases() const
227 {
228     QList<QByteArray> list;
229     list << "latin9";
230     return list;
231 }
232 
mibEnum() const233 int QLatin15Codec::mibEnum() const
234 {
235     return 111;
236 }
237 
238 QT_END_NAMESPACE
239