1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the plugins of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | // Most of the code here was originally written by Serika Kurusugawa
|
---|
43 | // a.k.a. Junji Takagi, and is included in Qt with the author's permission,
|
---|
44 | // and the grateful thanks of the Trolltech team.
|
---|
45 |
|
---|
46 | /*! \class QEucJpCodec
|
---|
47 | \reentrant
|
---|
48 | \internal
|
---|
49 | */
|
---|
50 |
|
---|
51 | /*
|
---|
52 | * Copyright (C) 1999 Serika Kurusugawa, All rights reserved.
|
---|
53 | *
|
---|
54 | * Redistribution and use in source and binary forms, with or without
|
---|
55 | * modification, are permitted provided that the following conditions
|
---|
56 | * are met:
|
---|
57 | * 1. Redistributions of source code must retain the above copyright
|
---|
58 | * notice, this list of conditions and the following disclaimer.
|
---|
59 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
60 | * notice, this list of conditions and the following disclaimer in the
|
---|
61 | * documentation and/or other materials provided with the distribution.
|
---|
62 | *
|
---|
63 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
---|
64 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
65 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
66 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
67 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
68 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
69 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
70 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
71 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
72 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
73 | * SUCH DAMAGE.
|
---|
74 | */
|
---|
75 |
|
---|
76 | #include "qeucjpcodec.h"
|
---|
77 |
|
---|
78 | QT_BEGIN_NAMESPACE
|
---|
79 |
|
---|
80 | #ifndef QT_NO_TEXTCODEC
|
---|
81 |
|
---|
82 | static const uchar Esc = 0x1b;
|
---|
83 | static const uchar Ss2 = 0x8e; // Single Shift 2
|
---|
84 | static const uchar Ss3 = 0x8f; // Single Shift 3
|
---|
85 |
|
---|
86 | #define IsKana(c) (((c) >= 0xa1) && ((c) <= 0xdf))
|
---|
87 | #define IsEucChar(c) (((c) >= 0xa1) && ((c) <= 0xfe))
|
---|
88 |
|
---|
89 | #define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar(QChar::ReplacementCharacter))
|
---|
90 |
|
---|
91 | /*!
|
---|
92 | Constructs a QEucJpCodec.
|
---|
93 | */
|
---|
94 | QEucJpCodec::QEucJpCodec() : conv(QJpUnicodeConv::newConverter(QJpUnicodeConv::Default))
|
---|
95 | {
|
---|
96 | }
|
---|
97 |
|
---|
98 | /*!
|
---|
99 | Destroys the codec.
|
---|
100 | */
|
---|
101 | QEucJpCodec::~QEucJpCodec()
|
---|
102 | {
|
---|
103 | delete (QJpUnicodeConv*)conv;
|
---|
104 | conv = 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | QByteArray QEucJpCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *state) const
|
---|
108 | {
|
---|
109 | char replacement = '?';
|
---|
110 | if (state) {
|
---|
111 | if (state->flags & ConvertInvalidToNull)
|
---|
112 | replacement = 0;
|
---|
113 | }
|
---|
114 | int invalid = 0;
|
---|
115 |
|
---|
116 | int rlen = 3*len + 1;
|
---|
117 | QByteArray rstr;
|
---|
118 | rstr.resize(rlen);
|
---|
119 | uchar* cursor = (uchar*)rstr.data();
|
---|
120 | for (int i = 0; i < len; i++) {
|
---|
121 | QChar ch = uc[i];
|
---|
122 | uint j;
|
---|
123 | if (ch.unicode() < 0x80) {
|
---|
124 | // ASCII
|
---|
125 | *cursor++ = ch.cell();
|
---|
126 | } else if ((j = conv->unicodeToJisx0201(ch.row(), ch.cell())) != 0) {
|
---|
127 | if (j < 0x80) {
|
---|
128 | // JIS X 0201 Latin ?
|
---|
129 | *cursor++ = j;
|
---|
130 | } else {
|
---|
131 | // JIS X 0201 Kana
|
---|
132 | *cursor++ = Ss2;
|
---|
133 | *cursor++ = j;
|
---|
134 | }
|
---|
135 | } else if ((j = conv->unicodeToJisx0208(ch.row(), ch.cell())) != 0) {
|
---|
136 | // JIS X 0208
|
---|
137 | *cursor++ = (j >> 8) | 0x80;
|
---|
138 | *cursor++ = (j & 0xff) | 0x80;
|
---|
139 | } else if ((j = conv->unicodeToJisx0212(ch.row(), ch.cell())) != 0) {
|
---|
140 | // JIS X 0212
|
---|
141 | *cursor++ = Ss3;
|
---|
142 | *cursor++ = (j >> 8) | 0x80;
|
---|
143 | *cursor++ = (j & 0xff) | 0x80;
|
---|
144 | } else {
|
---|
145 | // Error
|
---|
146 | *cursor++ = replacement;
|
---|
147 | ++invalid;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | rstr.resize(cursor - (uchar*)rstr.constData());
|
---|
151 |
|
---|
152 | if (state) {
|
---|
153 | state->invalidChars += invalid;
|
---|
154 | }
|
---|
155 | return rstr;
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | QString QEucJpCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const
|
---|
160 | {
|
---|
161 | uchar buf[2] = {0, 0};
|
---|
162 | int nbuf = 0;
|
---|
163 | QChar replacement = QChar::ReplacementCharacter;
|
---|
164 | if (state) {
|
---|
165 | if (state->flags & ConvertInvalidToNull)
|
---|
166 | replacement = QChar::Null;
|
---|
167 | nbuf = state->remainingChars;
|
---|
168 | buf[0] = state->state_data[0];
|
---|
169 | buf[1] = state->state_data[1];
|
---|
170 | }
|
---|
171 | int invalid = 0;
|
---|
172 |
|
---|
173 | QString result;
|
---|
174 | for (int i=0; i<len; i++) {
|
---|
175 | uchar ch = chars[i];
|
---|
176 | switch (nbuf) {
|
---|
177 | case 0:
|
---|
178 | if (ch < 0x80) {
|
---|
179 | // ASCII
|
---|
180 | result += QLatin1Char(ch);
|
---|
181 | } else if (ch == Ss2 || ch == Ss3) {
|
---|
182 | // JIS X 0201 Kana or JIS X 0212
|
---|
183 | buf[0] = ch;
|
---|
184 | nbuf = 1;
|
---|
185 | } else if (IsEucChar(ch)) {
|
---|
186 | // JIS X 0208
|
---|
187 | buf[0] = ch;
|
---|
188 | nbuf = 1;
|
---|
189 | } else {
|
---|
190 | // Invalid
|
---|
191 | result += replacement;
|
---|
192 | ++invalid;
|
---|
193 | }
|
---|
194 | break;
|
---|
195 | case 1:
|
---|
196 | if (buf[0] == Ss2) {
|
---|
197 | // JIS X 0201 Kana
|
---|
198 | if (IsKana(ch)) {
|
---|
199 | uint u = conv->jisx0201ToUnicode(ch);
|
---|
200 | result += QValidChar(u);
|
---|
201 | } else {
|
---|
202 | result += replacement;
|
---|
203 | ++invalid;
|
---|
204 | }
|
---|
205 | nbuf = 0;
|
---|
206 | } else if (buf[0] == Ss3) {
|
---|
207 | // JIS X 0212-1990
|
---|
208 | if (IsEucChar(ch)) {
|
---|
209 | buf[1] = ch;
|
---|
210 | nbuf = 2;
|
---|
211 | } else {
|
---|
212 | // Error
|
---|
213 | result += replacement;
|
---|
214 | ++invalid;
|
---|
215 | nbuf = 0;
|
---|
216 | }
|
---|
217 | } else {
|
---|
218 | // JIS X 0208-1990
|
---|
219 | if (IsEucChar(ch)) {
|
---|
220 | uint u = conv->jisx0208ToUnicode(buf[0] & 0x7f, ch & 0x7f);
|
---|
221 | result += QValidChar(u);
|
---|
222 | } else {
|
---|
223 | // Error
|
---|
224 | result += replacement;
|
---|
225 | ++invalid;
|
---|
226 | }
|
---|
227 | nbuf = 0;
|
---|
228 | }
|
---|
229 | break;
|
---|
230 | case 2:
|
---|
231 | // JIS X 0212
|
---|
232 | if (IsEucChar(ch)) {
|
---|
233 | uint u = conv->jisx0212ToUnicode(buf[1] & 0x7f, ch & 0x7f);
|
---|
234 | result += QValidChar(u);
|
---|
235 | } else {
|
---|
236 | result += replacement;
|
---|
237 | ++invalid;
|
---|
238 | }
|
---|
239 | nbuf = 0;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | if (state) {
|
---|
243 | state->remainingChars = nbuf;
|
---|
244 | state->state_data[0] = buf[0];
|
---|
245 | state->state_data[1] = buf[1];
|
---|
246 | state->invalidChars += invalid;
|
---|
247 | }
|
---|
248 | return result;
|
---|
249 | }
|
---|
250 |
|
---|
251 | int QEucJpCodec::_mibEnum()
|
---|
252 | {
|
---|
253 | return 18;
|
---|
254 | }
|
---|
255 |
|
---|
256 | QByteArray QEucJpCodec::_name()
|
---|
257 | {
|
---|
258 | return "EUC-JP";
|
---|
259 | }
|
---|
260 | #endif // QT_NO_TEXTCODEC
|
---|
261 |
|
---|
262 | QT_END_NAMESPACE
|
---|