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