| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/codecs/qisciicodec.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
| 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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
| 29 | ** | - | ||||||||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 31 | ** | - | ||||||||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||||||||
| 33 | #include "qisciicodec_p.h" | - | ||||||||||||||||||
| 34 | #include "qtextcodec_p.h" | - | ||||||||||||||||||
| 35 | #include "qlist.h" | - | ||||||||||||||||||
| 36 | - | |||||||||||||||||||
| 37 | #ifndef QT_NO_CODECS | - | ||||||||||||||||||
| 38 | - | |||||||||||||||||||
| 39 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 40 | - | |||||||||||||||||||
| 41 | /*! | - | ||||||||||||||||||
| 42 | \class QIsciiCodec | - | ||||||||||||||||||
| 43 | \inmodule QtCore | - | ||||||||||||||||||
| 44 | \brief The QIsciiCodec class provides conversion to and from the ISCII encoding. | - | ||||||||||||||||||
| 45 | - | |||||||||||||||||||
| 46 | \internal | - | ||||||||||||||||||
| 47 | */ | - | ||||||||||||||||||
| 48 | - | |||||||||||||||||||
| 49 | - | |||||||||||||||||||
| 50 | struct Codecs { | - | ||||||||||||||||||
| 51 | const char name[10]; | - | ||||||||||||||||||
| 52 | ushort base; | - | ||||||||||||||||||
| 53 | }; | - | ||||||||||||||||||
| 54 | - | |||||||||||||||||||
| 55 | static const Codecs codecs [] = { | - | ||||||||||||||||||
| 56 | { "iscii-dev", 0x900 }, | - | ||||||||||||||||||
| 57 | { "iscii-bng", 0x980 }, | - | ||||||||||||||||||
| 58 | { "iscii-pnj", 0xa00 }, | - | ||||||||||||||||||
| 59 | { "iscii-gjr", 0xa80 }, | - | ||||||||||||||||||
| 60 | { "iscii-ori", 0xb00 }, | - | ||||||||||||||||||
| 61 | { "iscii-tml", 0xb80 }, | - | ||||||||||||||||||
| 62 | { "iscii-tlg", 0xc00 }, | - | ||||||||||||||||||
| 63 | { "iscii-knd", 0xc80 }, | - | ||||||||||||||||||
| 64 | { "iscii-mlm", 0xd00 } | - | ||||||||||||||||||
| 65 | }; | - | ||||||||||||||||||
| 66 | - | |||||||||||||||||||
| 67 | QTextCodec *QIsciiCodec::create(const char *name) | - | ||||||||||||||||||
| 68 | { | - | ||||||||||||||||||
| 69 | for (int i = 0; i < 9; ++i) {
| 0-45 | ||||||||||||||||||
| 70 | if (qTextCodecNameMatch(name, codecs[i].name))
| 9-36 | ||||||||||||||||||
| 71 | return new QIsciiCodec(i); executed 9 times by 1 test: return new QIsciiCodec(i);Executed by:
| 9 | ||||||||||||||||||
| 72 | } executed 36 times by 1 test: end of blockExecuted by:
| 36 | ||||||||||||||||||
| 73 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
| 74 | } | - | ||||||||||||||||||
| 75 | - | |||||||||||||||||||
| 76 | QIsciiCodec::~QIsciiCodec() | - | ||||||||||||||||||
| 77 | { | - | ||||||||||||||||||
| 78 | } | - | ||||||||||||||||||
| 79 | - | |||||||||||||||||||
| 80 | QByteArray QIsciiCodec::name() const | - | ||||||||||||||||||
| 81 | { | - | ||||||||||||||||||
| 82 | return codecs[idx].name; executed 1656 times by 1 test: return codecs[idx].name;Executed by:
| 1656 | ||||||||||||||||||
| 83 | } | - | ||||||||||||||||||
| 84 | - | |||||||||||||||||||
| 85 | int QIsciiCodec::mibEnum() const | - | ||||||||||||||||||
| 86 | { | - | ||||||||||||||||||
| 87 | /* There is no MIBEnum for Iscii */ | - | ||||||||||||||||||
| 88 | return -3000-idx; never executed: return -3000-idx; | 0 | ||||||||||||||||||
| 89 | } | - | ||||||||||||||||||
| 90 | - | |||||||||||||||||||
| 91 | static const uchar inv = 0xFF; | - | ||||||||||||||||||
| 92 | - | |||||||||||||||||||
| 93 | /* iscii range from 0xa0 - 0xff */ | - | ||||||||||||||||||
| 94 | static const uchar iscii_to_uni_table[0x60] = { | - | ||||||||||||||||||
| 95 | 0x00, 0x01, 0x02, 0x03, | - | ||||||||||||||||||
| 96 | 0x05, 0x06, 0x07, 0x08, | - | ||||||||||||||||||
| 97 | 0x09, 0x0a, 0x0b, 0x0e, | - | ||||||||||||||||||
| 98 | 0x0f, 0x20, 0x0d, 0x12, | - | ||||||||||||||||||
| 99 | - | |||||||||||||||||||
| 100 | 0x13, 0x14, 0x11, 0x15, | - | ||||||||||||||||||
| 101 | 0x16, 0x17, 0x18, 0x19, | - | ||||||||||||||||||
| 102 | 0x1a, 0x1b, 0x1c, 0x1d, | - | ||||||||||||||||||
| 103 | 0x1e, 0x1f, 0x20, 0x21, | - | ||||||||||||||||||
| 104 | - | |||||||||||||||||||
| 105 | 0x22, 0x23, 0x24, 0x25, | - | ||||||||||||||||||
| 106 | 0x26, 0x27, 0x28, 0x29, | - | ||||||||||||||||||
| 107 | 0x2a, 0x2b, 0x2c, 0x2d, | - | ||||||||||||||||||
| 108 | 0x2e, 0x2f, 0x5f, 0x30, | - | ||||||||||||||||||
| 109 | - | |||||||||||||||||||
| 110 | 0x31, 0x32, 0x33, 0x34, | - | ||||||||||||||||||
| 111 | 0x35, 0x36, 0x37, 0x38, | - | ||||||||||||||||||
| 112 | 0x39, inv, 0x3e, 0x3f, | - | ||||||||||||||||||
| 113 | 0x40, 0x41, 0x42, 0x43, | - | ||||||||||||||||||
| 114 | - | |||||||||||||||||||
| 115 | 0x46, 0x47, 0x48, 0x45, | - | ||||||||||||||||||
| 116 | 0x4a, 0x4b, 0x4c, 0x49, | - | ||||||||||||||||||
| 117 | 0x4d, 0x3c, 0x64, 0x00, | - | ||||||||||||||||||
| 118 | 0x00, 0x00, 0x00, 0x00, | - | ||||||||||||||||||
| 119 | - | |||||||||||||||||||
| 120 | 0x00, 0x66, 0x67, 0x68, | - | ||||||||||||||||||
| 121 | 0x69, 0x6a, 0x6b, 0x6c, | - | ||||||||||||||||||
| 122 | 0x6d, 0x6e, 0x6f, 0x00, | - | ||||||||||||||||||
| 123 | 0x00, 0x00, 0x00, 0x00 | - | ||||||||||||||||||
| 124 | }; | - | ||||||||||||||||||
| 125 | - | |||||||||||||||||||
| 126 | static const uchar uni_to_iscii_table[0x80] = { | - | ||||||||||||||||||
| 127 | 0x00, 0xa1, 0xa2, 0xa3, | - | ||||||||||||||||||
| 128 | 0x00, 0xa4, 0xa5, 0xa6, | - | ||||||||||||||||||
| 129 | 0xa7, 0xa8, 0xa9, 0xaa, | - | ||||||||||||||||||
| 130 | 0x00, 0xae, 0xab, 0xac, | - | ||||||||||||||||||
| 131 | - | |||||||||||||||||||
| 132 | 0xad, 0xb2, 0xaf, 0xb0, | - | ||||||||||||||||||
| 133 | 0xb1, 0xb3, 0xb4, 0xb5, | - | ||||||||||||||||||
| 134 | 0xb6, 0xb7, 0xb8, 0xb9, | - | ||||||||||||||||||
| 135 | 0xba, 0xbb, 0xbc, 0xbd, | - | ||||||||||||||||||
| 136 | - | |||||||||||||||||||
| 137 | 0xbe, 0xbf, 0xc0, 0xc1, | - | ||||||||||||||||||
| 138 | 0xc2, 0xc3, 0xc4, 0xc5, | - | ||||||||||||||||||
| 139 | 0xc6, 0xc7, 0xc8, 0xc9, | - | ||||||||||||||||||
| 140 | 0xca, 0xcb, 0xcc, 0xcd, | - | ||||||||||||||||||
| 141 | - | |||||||||||||||||||
| 142 | 0xcf, 0xd0, 0xd1, 0xd2, | - | ||||||||||||||||||
| 143 | 0xd3, 0xd4, 0xd5, 0xd6, | - | ||||||||||||||||||
| 144 | 0xd7, 0xd8, 0x00, 0x00, | - | ||||||||||||||||||
| 145 | 0xe9, 0x00, 0xda, 0xdb, | - | ||||||||||||||||||
| 146 | - | |||||||||||||||||||
| 147 | 0xdc, 0xdd, 0xde, 0xdf, | - | ||||||||||||||||||
| 148 | 0x00, 0xe3, 0xe0, 0xe1, | - | ||||||||||||||||||
| 149 | 0xe2, 0xe7, 0xe4, 0xe5, | - | ||||||||||||||||||
| 150 | 0xe6, 0xe8, 0x00, 0x00, | - | ||||||||||||||||||
| 151 | - | |||||||||||||||||||
| 152 | 0x00, 0x00, 0x00, 0x00, | - | ||||||||||||||||||
| 153 | 0x00, 0x00, 0x00, 0x00, | - | ||||||||||||||||||
| 154 | 0x01, 0x02, 0x03, 0x04, // decomposable into the uc codes listed here + nukta | - | ||||||||||||||||||
| 155 | 0x05, 0x06, 0x07, 0xce, | - | ||||||||||||||||||
| 156 | - | |||||||||||||||||||
| 157 | 0x00, 0x00, 0x00, 0x00, | - | ||||||||||||||||||
| 158 | 0xea, 0x08, 0xf1, 0xf2, | - | ||||||||||||||||||
| 159 | 0xf3, 0xf4, 0xf5, 0xf6, | - | ||||||||||||||||||
| 160 | 0xf7, 0xf8, 0xf9, 0xfa, | - | ||||||||||||||||||
| 161 | - | |||||||||||||||||||
| 162 | 0x00, 0x00, 0x00, 0x00, | - | ||||||||||||||||||
| 163 | 0x00, 0x00, 0x00, 0x00, | - | ||||||||||||||||||
| 164 | 0x00, 0x00, 0x00, 0x00, | - | ||||||||||||||||||
| 165 | 0x00, 0x00, 0x00, 0x00 | - | ||||||||||||||||||
| 166 | }; | - | ||||||||||||||||||
| 167 | - | |||||||||||||||||||
| 168 | static const uchar uni_to_iscii_pairs[] = { | - | ||||||||||||||||||
| 169 | 0x00, 0x00, | - | ||||||||||||||||||
| 170 | 0x15, 0x3c, // 0x958 | - | ||||||||||||||||||
| 171 | 0x16, 0x3c, // 0x959 | - | ||||||||||||||||||
| 172 | 0x17, 0x3c, // 0x95a | - | ||||||||||||||||||
| 173 | 0x1c, 0x3c, // 0x95b | - | ||||||||||||||||||
| 174 | 0x21, 0x3c, // 0x95c | - | ||||||||||||||||||
| 175 | 0x22, 0x3c, // 0x95d | - | ||||||||||||||||||
| 176 | 0x2b, 0x3c, // 0x95e | - | ||||||||||||||||||
| 177 | 0x64, 0x64 // 0x965 | - | ||||||||||||||||||
| 178 | }; | - | ||||||||||||||||||
| 179 | - | |||||||||||||||||||
| 180 | - | |||||||||||||||||||
| 181 | QByteArray QIsciiCodec::convertFromUnicode(const QChar *uc, int len, ConverterState *state) const | - | ||||||||||||||||||
| 182 | { | - | ||||||||||||||||||
| 183 | char replacement = '?'; | - | ||||||||||||||||||
| 184 | bool halant = false; | - | ||||||||||||||||||
| 185 | if (state) {
| 0-1440 | ||||||||||||||||||
| 186 | if (state->flags & ConvertInvalidToNull)
| 0-1440 | ||||||||||||||||||
| 187 | replacement = 0; executed 1440 times by 1 test: replacement = 0;Executed by:
| 1440 | ||||||||||||||||||
| 188 | halant = state->state_data[0]; | - | ||||||||||||||||||
| 189 | } executed 1440 times by 1 test: end of blockExecuted by:
| 1440 | ||||||||||||||||||
| 190 | int invalid = 0; | - | ||||||||||||||||||
| 191 | - | |||||||||||||||||||
| 192 | QByteArray result(2 * len, Qt::Uninitialized); //worst case | - | ||||||||||||||||||
| 193 | - | |||||||||||||||||||
| 194 | uchar *ch = reinterpret_cast<uchar *>(result.data()); | - | ||||||||||||||||||
| 195 | - | |||||||||||||||||||
| 196 | const int base = codecs[idx].base; | - | ||||||||||||||||||
| 197 | - | |||||||||||||||||||
| 198 | for (int i =0; i < len; ++i) {
| 1440-2862 | ||||||||||||||||||
| 199 | const ushort codePoint = uc[i].unicode(); | - | ||||||||||||||||||
| 200 | - | |||||||||||||||||||
| 201 | /* The low 7 bits of ISCII is plain ASCII. However, we go all the | - | ||||||||||||||||||
| 202 | * way up to 0xA0 such that we can roundtrip with convertToUnicode()'s | - | ||||||||||||||||||
| 203 | * behavior. */ | - | ||||||||||||||||||
| 204 | if(codePoint < 0xA0) {
| 0-2862 | ||||||||||||||||||
| 205 | *ch++ = static_cast<uchar>(codePoint); | - | ||||||||||||||||||
| 206 | continue; executed 2862 times by 1 test: continue;Executed by:
| 2862 | ||||||||||||||||||
| 207 | } | - | ||||||||||||||||||
| 208 | - | |||||||||||||||||||
| 209 | const int pos = codePoint - base; | - | ||||||||||||||||||
| 210 | if (pos > 0 && pos < 0x80) {
| 0 | ||||||||||||||||||
| 211 | uchar iscii = uni_to_iscii_table[pos]; | - | ||||||||||||||||||
| 212 | if (iscii > 0x80) {
| 0 | ||||||||||||||||||
| 213 | *ch++ = iscii; | - | ||||||||||||||||||
| 214 | } else if (iscii) { never executed: end of block
| 0 | ||||||||||||||||||
| 215 | Q_ASSERT((2 * iscii) < (sizeof(uni_to_iscii_pairs) / sizeof(uni_to_iscii_pairs[0]))); | - | ||||||||||||||||||
| 216 | const uchar *pair = uni_to_iscii_pairs + 2*iscii; | - | ||||||||||||||||||
| 217 | *ch++ = *pair++; | - | ||||||||||||||||||
| 218 | *ch++ = *pair++; | - | ||||||||||||||||||
| 219 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 220 | *ch++ = replacement; | - | ||||||||||||||||||
| 221 | ++invalid; | - | ||||||||||||||||||
| 222 | } never executed: end of block | 0 | ||||||||||||||||||
| 223 | } else { | - | ||||||||||||||||||
| 224 | if (uc[i].unicode() == 0x200c) { // ZWNJ
| 0 | ||||||||||||||||||
| 225 | if (halant)
| 0 | ||||||||||||||||||
| 226 | // Consonant Halant ZWNJ -> Consonant Halant Halant | - | ||||||||||||||||||
| 227 | *ch++ = 0xe8; never executed: *ch++ = 0xe8; | 0 | ||||||||||||||||||
| 228 | } else if (uc[i].unicode() == 0x200d) { // ZWJ never executed: end of block
| 0 | ||||||||||||||||||
| 229 | if (halant)
| 0 | ||||||||||||||||||
| 230 | // Consonant Halant ZWJ -> Consonant Halant Nukta | - | ||||||||||||||||||
| 231 | *ch++ = 0xe9; never executed: *ch++ = 0xe9; | 0 | ||||||||||||||||||
| 232 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 233 | *ch++ = replacement; | - | ||||||||||||||||||
| 234 | ++invalid; | - | ||||||||||||||||||
| 235 | } never executed: end of block | 0 | ||||||||||||||||||
| 236 | } | - | ||||||||||||||||||
| 237 | halant = (pos == 0x4d); | - | ||||||||||||||||||
| 238 | } never executed: end of block | 0 | ||||||||||||||||||
| 239 | result.truncate(ch - (uchar *)result.data()); | - | ||||||||||||||||||
| 240 | - | |||||||||||||||||||
| 241 | if (state) {
| 0-1440 | ||||||||||||||||||
| 242 | state->invalidChars += invalid; | - | ||||||||||||||||||
| 243 | state->state_data[0] = halant; | - | ||||||||||||||||||
| 244 | } executed 1440 times by 1 test: end of blockExecuted by:
| 1440 | ||||||||||||||||||
| 245 | return result; executed 1440 times by 1 test: return result;Executed by:
| 1440 | ||||||||||||||||||
| 246 | } | - | ||||||||||||||||||
| 247 | - | |||||||||||||||||||
| 248 | QString QIsciiCodec::convertToUnicode(const char* chars, int len, ConverterState *state) const | - | ||||||||||||||||||
| 249 | { | - | ||||||||||||||||||
| 250 | bool halant = false; | - | ||||||||||||||||||
| 251 | if (state) {
| 0 | ||||||||||||||||||
| 252 | halant = state->state_data[0]; | - | ||||||||||||||||||
| 253 | } never executed: end of block | 0 | ||||||||||||||||||
| 254 | - | |||||||||||||||||||
| 255 | QString result(len, Qt::Uninitialized); | - | ||||||||||||||||||
| 256 | QChar *uc = result.data(); | - | ||||||||||||||||||
| 257 | - | |||||||||||||||||||
| 258 | const int base = codecs[idx].base; | - | ||||||||||||||||||
| 259 | - | |||||||||||||||||||
| 260 | for (int i = 0; i < len; ++i) {
| 0 | ||||||||||||||||||
| 261 | ushort ch = (uchar) chars[i]; | - | ||||||||||||||||||
| 262 | if (ch < 0xa0)
| 0 | ||||||||||||||||||
| 263 | *uc++ = ch; never executed: *uc++ = ch; | 0 | ||||||||||||||||||
| 264 | else { | - | ||||||||||||||||||
| 265 | ushort c = iscii_to_uni_table[ch - 0xa0]; | - | ||||||||||||||||||
| 266 | if (halant && (c == inv || c == 0xe9)) {
| 0 | ||||||||||||||||||
| 267 | // Consonant Halant inv -> Consonant Halant ZWJ | - | ||||||||||||||||||
| 268 | // Consonant Halant Nukta -> Consonant Halant ZWJ | - | ||||||||||||||||||
| 269 | *uc++ = QChar(0x200d); | - | ||||||||||||||||||
| 270 | } else if (halant && c == 0xe8) { never executed: end of block
| 0 | ||||||||||||||||||
| 271 | // Consonant Halant Halant -> Consonant Halant ZWNJ | - | ||||||||||||||||||
| 272 | *uc++ = QChar(0x200c); | - | ||||||||||||||||||
| 273 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 274 | *uc++ = QChar(c+base); | - | ||||||||||||||||||
| 275 | } never executed: end of block | 0 | ||||||||||||||||||
| 276 | } | - | ||||||||||||||||||
| 277 | halant = ((uchar)chars[i] == 0xe8); | - | ||||||||||||||||||
| 278 | } never executed: end of block | 0 | ||||||||||||||||||
| 279 | result.resize(uc - result.unicode()); | - | ||||||||||||||||||
| 280 | - | |||||||||||||||||||
| 281 | if (state) {
| 0 | ||||||||||||||||||
| 282 | state->state_data[0] = halant; | - | ||||||||||||||||||
| 283 | } never executed: end of block | 0 | ||||||||||||||||||
| 284 | return result; never executed: return result; | 0 | ||||||||||||||||||
| 285 | } | - | ||||||||||||||||||
| 286 | - | |||||||||||||||||||
| 287 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 288 | - | |||||||||||||||||||
| 289 | #endif // QT_NO_CODECS | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |