UConverterAlias.java 17.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.agl.charset;

import com.adobe.agl.charset.UConverterAliasDataReader;
import com.adobe.agl.impl.ICUData;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

final class UConverterAlias {
    static final int UNNORMALIZED = 0;
    static final int STD_NORMALIZED = 1;
    static final int AMBIGUOUS_ALIAS_MAP_BIT = 32768;
    static final int CONTAINS_OPTION_BIT = 16384;
    static final int CONVERTER_INDEX_MASK = 4095;
    static final int NUM_RESERVED_TAGS = 2;
    static final int NUM_HIDDEN_TAGS = 1;
    static int[] gConverterList = null;
    static int[] gTagList = null;
    static int[] gAliasList = null;
    static int[] gUntaggedConvArray = null;
    static int[] gTaggedAliasArray = null;
    static int[] gTaggedAliasLists = null;
    static int[] gOptionTable = null;
    static byte[] gStringTable = null;
    static byte[] gNormalizedStringTable = null;
    static final int tocLengthIndex = 0;
    private static final int converterListIndex = 1;
    private static final int tagListIndex = 2;
    private static final int aliasListIndex = 3;
    private static final int untaggedConvArrayIndex = 4;
    private static final int taggedAliasArrayIndex = 5;
    private static final int taggedAliasListsIndex = 6;
    private static final int optionTableIndex = 7;
    private static final int stringTableIndex = 8;
    private static final int normalizedStringTableIndex = 9;
    private static final int minTocLength = 9;
    private static final int offsetsCount = 10;
    static ByteBuffer gAliasData = null;
    private static final String CNVALIAS_DATA_FILE_NAME = "data/icudt40b/cnvalias.icu";
    private static final int CNVALIAS_DATA_BUFFER_SIZE = 25000;
    private static final byte IGNORE = 0;
    private static final byte ZERO = 1;
    private static final byte NONZERO = 2;
    static final byte MINLETTER = 3;
    static final byte[] asciiTypes = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 0, 0, 0, 0, 0, 0, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 0, 0, 0, 0, 0};
    static String[] gAvailableConverters = null;
    static int gAvailableConverterCount = 0;
    static byte[] gDefaultConverterNameBuffer;
    static String gDefaultConverterName;

    UConverterAlias() {
    }

    static final String GET_STRING(int idx) {
        return new String(gStringTable, 2 * idx, UConverterAlias.strlen(gStringTable, 2 * idx));
    }

    private static final String GET_NORMALIZED_STRING(int idx) {
        return new String(gNormalizedStringTable, 2 * idx, UConverterAlias.strlen(gNormalizedStringTable, 2 * idx));
    }

    public static final int strlen(byte[] sArray, int sBegin) {
        int i = sBegin;
        while (i < sArray.length && sArray[i++] != 0) {
        }
        return i - sBegin - 1;
    }

    private static final boolean isAlias(String alias) {
        if (alias == null) {
            throw new IllegalArgumentException("Alias param is null!");
        }
        return alias.length() != 0;
    }

    private static final synchronized boolean haveAliasData() throws IOException {
        boolean needInit;
        boolean bl = needInit = gAliasData == null;
        if (needInit) {
            ByteBuffer data = null;
            int[] tableArray = null;
            InputStream i = ICUData.getRequiredStream("data/icudt40b/cnvalias.icu");
            BufferedInputStream b = new BufferedInputStream(i, 25000);
            UConverterAliasDataReader reader = new UConverterAliasDataReader(b);
            tableArray = reader.readToc(10);
            int tableStart = tableArray[0];
            if (tableStart < 9) {
                throw new IOException("Invalid data format.");
            }
            gConverterList = new int[tableArray[1]];
            gTagList = new int[tableArray[2]];
            gAliasList = new int[tableArray[3]];
            gUntaggedConvArray = new int[tableArray[4]];
            gTaggedAliasArray = new int[tableArray[5]];
            gTaggedAliasLists = new int[tableArray[6]];
            gOptionTable = new int[tableArray[7]];
            gStringTable = new byte[tableArray[8] * 2];
            gNormalizedStringTable = new byte[tableArray[9] * 2];
            reader.read(gConverterList, gTagList, gAliasList, gUntaggedConvArray, gTaggedAliasArray, gTaggedAliasLists, gOptionTable, gStringTable, gNormalizedStringTable);
            data = ByteBuffer.allocate(0);
            if (gOptionTable[0] != 1) {
                throw new IOException("Unsupported alias normalization");
            }
            if (gAliasData == null) {
                gAliasData = data;
                data = null;
            }
            if (data != null) {
                // empty if block
            }
        }
        return true;
    }

    private static final int findConverter(String alias, boolean[] isAmbigous) {
        int limit;
        StringBuffer strippedName = new StringBuffer();
        UConverterAlias.stripForCompare(strippedName, alias);
        alias = strippedName.toString();
        int start = 0;
        int mid = limit = gUntaggedConvArray.length;
        int lastMid = Integer.MAX_VALUE;
        while (lastMid != (mid = (start + limit) / 2)) {
            lastMid = mid;
            String aliasToCompare = UConverterAlias.GET_NORMALIZED_STRING(gAliasList[mid]);
            int result = alias.compareTo(aliasToCompare);
            if (result < 0) {
                limit = mid;
                continue;
            }
            if (result > 0) {
                start = mid;
                continue;
            }
            if ((gUntaggedConvArray[mid] & 32768) != 0) {
                isAmbigous[0] = true;
            }
            return gUntaggedConvArray[mid] & 4095;
        }
        return Integer.MAX_VALUE;
    }

    public static final StringBuffer stripForCompare(StringBuffer dst, String name) {
        return UConverterAlias.io_stripASCIIForCompare(dst, name);
    }

    private static final char GET_CHAR_TYPE(char c) {
        return c < asciiTypes.length ? asciiTypes[c] : '\u0000';
    }

    private static final StringBuffer io_stripASCIIForCompare(StringBuffer dst, String name) {
        int nameIndex = 0;
        boolean afterDigit = false;
        block5 : while (nameIndex < name.length()) {
            char c1 = name.charAt(nameIndex++);
            char type = UConverterAlias.GET_CHAR_TYPE(c1);
            switch (type) {
                case '\u0000': {
                    afterDigit = false;
                    continue block5;
                }
                case '\u0001': {
                    char nextType;
                    if (afterDigit || nameIndex >= name.length() || (nextType = UConverterAlias.GET_CHAR_TYPE(name.charAt(nameIndex))) != '\u0001' && nextType != '\u0002') break;
                    continue block5;
                }
                case '\u0002': {
                    afterDigit = true;
                    break;
                }
                default: {
                    c1 = type;
                    afterDigit = false;
                }
            }
            dst.append(c1);
        }
        return dst;
    }

    static int compareNames(String name1, String name2) {
        int rc;
        int name1Index = 0;
        int name2Index = 0;
        char c1 = '\u0000';
        char c2 = '\u0000';
        boolean afterDigit1 = false;
        boolean afterDigit2 = false;
        block10 : do {
            char type;
            char nextType;
            if (name1Index < name1.length()) {
                c1 = name1.charAt(name1Index++);
                type = UConverterAlias.GET_CHAR_TYPE(c1);
                switch (type) {
                    case '\u0000': {
                        afterDigit1 = false;
                        continue block10;
                    }
                    case '\u0001': {
                        if (afterDigit1 || name1Index >= name1.length() || (nextType = UConverterAlias.GET_CHAR_TYPE(name1.charAt(name1Index))) != '\u0001' && nextType != '\u0002') break;
                        continue block10;
                    }
                    case '\u0002': {
                        afterDigit1 = true;
                        break;
                    }
                    default: {
                        c1 = type;
                        afterDigit1 = false;
                        break;
                    }
                }
            }
            block11 : while (name2Index < name2.length()) {
                c2 = name2.charAt(name2Index++);
                type = UConverterAlias.GET_CHAR_TYPE(c2);
                switch (type) {
                    case '\u0000': {
                        afterDigit2 = false;
                        continue block11;
                    }
                    case '\u0001': {
                        if (afterDigit2 || name1Index >= name1.length() || (nextType = UConverterAlias.GET_CHAR_TYPE(name2.charAt(name2Index))) != '\u0001' && nextType != '\u0002') break block11;
                        continue block11;
                    }
                    case '\u0002': {
                        afterDigit2 = true;
                        break block11;
                    }
                    default: {
                        c2 = type;
                        afterDigit2 = false;
                        break block11;
                    }
                }
            }
            if (name1Index >= name1.length() && name2Index >= name2.length()) {
                return 0;
            }
            rc = c1 - c2;
            if (rc != 0) break;
        } while (true);
        return rc;
    }

    static int io_countAliases(String alias) throws IOException {
        boolean[] isAmbigous;
        int convNum;
        int listOffset;
        if (UConverterAlias.haveAliasData() && UConverterAlias.isAlias(alias) && (convNum = UConverterAlias.findConverter(alias, isAmbigous = new boolean[1])) < gConverterList.length && (listOffset = gTaggedAliasArray[(gTagList.length - 1) * gConverterList.length + convNum]) != 0) {
            return gTaggedAliasLists[listOffset];
        }
        return 0;
    }

    static String io_getAlias(String alias, int n) throws IOException {
        int listOffset;
        boolean[] isAmbigous;
        int convNum;
        if (UConverterAlias.haveAliasData() && UConverterAlias.isAlias(alias) && (convNum = UConverterAlias.findConverter(alias, isAmbigous = new boolean[1])) < gConverterList.length && (listOffset = gTaggedAliasArray[(gTagList.length - 1) * gConverterList.length + convNum]) != 0) {
            int[] currListArray = gTaggedAliasLists;
            int currListArrayIndex = listOffset + 1;
            return UConverterAlias.GET_STRING(currListArray[currListArrayIndex + n]);
        }
        return null;
    }

    static final String getStandardName(String alias, String standard) throws IOException {
        int listOffset;
        if (UConverterAlias.haveAliasData() && UConverterAlias.isAlias(alias) && 0 < (listOffset = UConverterAlias.findTaggedAliasListsOffset(alias, standard)) && listOffset < gTaggedAliasLists.length) {
            int[] currListArray = gTaggedAliasLists;
            int currListArrayIndex = listOffset + 1;
            if (currListArray[0] != 0) {
                return UConverterAlias.GET_STRING(currListArray[currListArrayIndex]);
            }
        }
        return null;
    }

    static int countAliases(String alias) throws IOException {
        return UConverterAlias.io_countAliases(alias);
    }

    static String getAlias(String alias, int n) throws IOException {
        return UConverterAlias.io_getAlias(alias, n);
    }

    static String getAvailableName(int n) {
        try {
            if (0 <= n && n <= 65535) {
                String name = UConverterAlias.bld_getAvailableConverter(n);
                return name;
            }
        }
        catch (IOException ex) {
            // empty catch block
        }
        return null;
    }

    static String getCanonicalName(String alias, String standard) throws IOException {
        int convNum;
        if (UConverterAlias.haveAliasData() && UConverterAlias.isAlias(alias) && (convNum = UConverterAlias.findTaggedConverterNum(alias, standard)) < gConverterList.length) {
            return UConverterAlias.GET_STRING(gConverterList[convNum]);
        }
        return null;
    }

    static int countAvailable() {
        try {
            return UConverterAlias.bld_countAvailableConverters();
        }
        catch (IOException ex) {
            return -1;
        }
    }

    private static int getTagNumber(String tagName) {
        if (gTagList != null) {
            for (int tagNum = 0; tagNum < gTagList.length; ++tagNum) {
                if (!tagName.equals(UConverterAlias.GET_STRING(gTagList[tagNum]))) continue;
                return tagNum;
            }
        }
        return Integer.MAX_VALUE;
    }

    private static int findTaggedAliasListsOffset(String alias, String standard) {
        int tagNum = UConverterAlias.getTagNumber(standard);
        boolean[] isAmbigous = new boolean[1];
        int convNum = UConverterAlias.findConverter(alias, isAmbigous);
        if (tagNum < gTagList.length - 1 && convNum < gConverterList.length) {
            int listOffset = gTaggedAliasArray[tagNum * gConverterList.length + convNum];
            if (listOffset != 0 && gTaggedAliasLists[listOffset + 1] != 0) {
                return listOffset;
            }
            if (isAmbigous[0]) {
                for (int idx = 0; idx < gTaggedAliasArray.length; ++idx) {
                    int currTagNum;
                    int tempListOffset;
                    int currConvNum;
                    listOffset = gTaggedAliasArray[idx];
                    if (listOffset == 0 || !UConverterAlias.isAliasInList(alias, listOffset) || (tempListOffset = gTaggedAliasArray[tagNum * gConverterList.length + (currConvNum = idx - (currTagNum = idx / gConverterList.length) * gConverterList.length)]) == 0 || gTaggedAliasLists[tempListOffset + 1] == 0) continue;
                    return tempListOffset;
                }
            }
            return 0;
        }
        return Integer.MAX_VALUE;
    }

    private static int findTaggedConverterNum(String alias, String standard) {
        int tagNum = UConverterAlias.getTagNumber(standard);
        boolean[] isAmbigous = new boolean[1];
        int convNum = UConverterAlias.findConverter(alias, isAmbigous);
        if (tagNum < gTagList.length - 1 && convNum < gConverterList.length) {
            int listOffset = gTaggedAliasArray[tagNum * gConverterList.length + convNum];
            if (listOffset != 0 && UConverterAlias.isAliasInList(alias, listOffset)) {
                return convNum;
            }
            if (isAmbigous[0]) {
                int convStart = tagNum * gConverterList.length;
                int convLimit = (tagNum + 1) * gConverterList.length;
                for (int idx = convStart; idx < convLimit; ++idx) {
                    listOffset = gTaggedAliasArray[idx];
                    if (listOffset == 0 || !UConverterAlias.isAliasInList(alias, listOffset)) continue;
                    return idx - convStart;
                }
            }
        }
        return Integer.MAX_VALUE;
    }

    private static boolean isAliasInList(String alias, int listOffset) {
        if (listOffset != 0) {
            int listCount = gTaggedAliasLists[listOffset];
            int[] currList = gTaggedAliasLists;
            int currListArrayIndex = listOffset + 1;
            for (int currAlias = 0; currAlias < listCount; ++currAlias) {
                if (currList[currAlias + currListArrayIndex] == 0 || UConverterAlias.compareNames(alias, UConverterAlias.GET_STRING(currList[currAlias + currListArrayIndex])) != 0) continue;
                return true;
            }
        }
        return false;
    }

    static boolean haveAvailableConverterList() throws IOException {
        if (gAvailableConverters == null) {
            if (!UConverterAlias.haveAliasData()) {
                return false;
            }
            String[] localConverterList = new String[gConverterList.length];
            int localConverterCount = 0;
            for (int idx = 0; idx < gConverterList.length; ++idx) {
                String converterName = UConverterAlias.GET_STRING(gConverterList[idx]);
                localConverterList[localConverterCount++] = converterName;
            }
            if (gAvailableConverters == null) {
                gAvailableConverters = localConverterList;
                gAvailableConverterCount = localConverterCount;
            }
        }
        return true;
    }

    static int bld_countAvailableConverters() throws IOException {
        if (UConverterAlias.haveAvailableConverterList()) {
            return gAvailableConverterCount;
        }
        return 0;
    }

    static String bld_getAvailableConverter(int n) throws IOException {
        if (UConverterAlias.haveAvailableConverterList() && n < gAvailableConverterCount) {
            return gAvailableConverters[n];
        }
        return null;
    }

    static {
        gDefaultConverterName = null;
    }
}