1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the internationalization module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
// Most of the code here was originally written by Serika Kurusugawa
30
// a.k.a. Junji Takagi, and is included in Qt with the author's permission,
31
// and the grateful thanks of the Trolltech team.
33
/*! \class QEucJpCodec qeucjpcodec.h
37
\brief The QEucJpCodec class provides conversion to and from EUC-JP character sets.
39
More precisely, the QEucJpCodec class subclasses QTextCodec to
40
provide support for EUC-JP, the main legacy encoding for Unix
43
The environment variable \c UNICODEMAP_JP can be used to fine-tune
44
QJisCodec, QSjisCodec and QEucJpCodec. The \l QJisCodec
45
documentation describes how to use this variable.
47
Most of the code here was written by Serika Kurusugawa,
48
a.k.a. Junji Takagi, and is included in Qt with the author's
49
permission and the grateful thanks of the Trolltech team. Here is
50
the copyright statement for that code:
54
Copyright (C) 1999 Serika Kurusugawa. All rights reserved.
56
Redistribution and use in source and binary forms, with or without
57
modification, are permitted provided that the following conditions
60
\i Redistributions of source code must retain the above copyright
61
notice, this list of conditions and the following disclaimer.
62
\i Redistributions in binary form must reproduce the above copyright
63
notice, this list of conditions and the following disclaimer in the
64
documentation and/or other materials provided with the distribution.
67
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS".
68
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
81
* Copyright (C) 1999 Serika Kurusugawa, All rights reserved.
83
* Redistribution and use in source and binary forms, with or without
84
* modification, are permitted provided that the following conditions
86
* 1. Redistributions of source code must retain the above copyright
87
* notice, this list of conditions and the following disclaimer.
88
* 2. Redistributions in binary form must reproduce the above copyright
89
* notice, this list of conditions and the following disclaimer in the
90
* documentation and/or other materials provided with the distribution.
92
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
93
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
95
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
96
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
97
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
98
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
100
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
101
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
105
#include "qeucjpcodec.h"
107
static const uchar Esc = 0x1b;
108
static const uchar Ss2 = 0x8e; // Single Shift 2
109
static const uchar Ss3 = 0x8f; // Single Shift 3
111
#define IsKana(c) (((c) >= 0xa1) && ((c) <= 0xdf))
112
#define IsEucChar(c) (((c) >= 0xa1) && ((c) <= 0xfe))
114
#define QValidChar(u) ((u) ? QChar((ushort)(u)) : QChar(QChar::ReplacementCharacter))
117
Constructs a QEucJpCodec.
119
QEucJpCodec::QEucJpCodec() : conv(QJpUnicodeConv::newConverter(QJpUnicodeConv::Default))
126
QEucJpCodec::~QEucJpCodec()
128
delete (QJpUnicodeConv*)conv;
132
QByteArray QEucJpCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *state) const
134
char replacement = '?';
136
if (state->flags & ConvertInvalidToNull)
141
int rlen = 3*len + 1;
144
uchar* cursor = (uchar*)rstr.data();
145
for (int i = 0; i < len; i++) {
148
if (ch.unicode() < 0x80) {
150
*cursor++ = ch.cell();
151
} else if ((j = conv->unicodeToJisx0201(ch.row(), ch.cell())) != 0) {
153
// JIS X 0201 Latin ?
160
} else if ((j = conv->unicodeToJisx0208(ch.row(), ch.cell())) != 0) {
162
*cursor++ = (j >> 8) | 0x80;
163
*cursor++ = (j & 0xff) | 0x80;
164
} else if ((j = conv->unicodeToJisx0212(ch.row(), ch.cell())) != 0) {
167
*cursor++ = (j >> 8) | 0x80;
168
*cursor++ = (j & 0xff) | 0x80;
171
*cursor++ = replacement;
175
rstr.resize(cursor - (uchar*)rstr.constData());
178
state->invalidChars += invalid;
184
QString QEucJpCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const
188
QChar replacement = QChar::ReplacementCharacter;
190
if (state->flags & ConvertInvalidToNull)
191
replacement = QChar::Null;
192
nbuf = state->remainingChars;
193
buf[0] = state->state_data[0];
194
buf[1] = state->state_data[1];
199
for (int i=0; i<len; i++) {
205
result += QLatin1Char(ch);
206
} else if (ch == Ss2 || ch == Ss3) {
207
// JIS X 0201 Kana or JIS X 0212
210
} else if (IsEucChar(ch)) {
216
result += replacement;
224
uint u = conv->jisx0201ToUnicode(ch);
225
result += QValidChar(u);
227
result += replacement;
231
} else if (buf[0] == Ss3) {
238
result += replacement;
245
uint u = conv->jisx0208ToUnicode(buf[0] & 0x7f, ch & 0x7f);
246
result += QValidChar(u);
249
result += replacement;
258
uint u = conv->jisx0212ToUnicode(buf[1] & 0x7f, ch & 0x7f);
259
result += QValidChar(u);
261
result += replacement;
268
state->remainingChars = nbuf;
269
state->state_data[0] = buf[0];
270
state->state_data[1] = buf[1];
271
state->invalidChars += invalid;
276
int QEucJpCodec::_mibEnum()
281
QByteArray QEucJpCodec::_name()