CMapResourceBuilder.java 37.2 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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.io.stream.InputByteStream
 *  com.adobe.internal.pdftoolkit.core.cos.CosStream
 *  com.adobe.internal.pdftoolkit.core.cos.CosToken
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException
 *  com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException
 *  com.adobe.internal.pdftoolkit.core.types.ASName
 */
package com.adobe.internal.pdftoolkit.pdf.graphics.font.impl;

import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosToken;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidDocumentException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFInvalidParameterException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.CMapObject;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFToUnicodeCMap;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.PDFWritingMode;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.CMapCodesSpace;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.CMapObjectCache;
import com.adobe.internal.pdftoolkit.pdf.graphics.font.impl.CMapObjectImpl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public final class CMapResourceBuilder {
    private final HashMap<String, HashMap<Long, Integer>> spaceCIDToUnicodeMap = new HashMap();
    static Pattern registryPattern = Pattern.compile(" */Registry \\(([A-Za-z0-9]*)\\) def");
    static Pattern orderingPattern = Pattern.compile(" */Ordering \\(([A-Za-z0-9_]*)\\) def");
    static Pattern supplementPattern = Pattern.compile(" */Supplement ([0-9]) def");
    static Pattern writingModePattern = Pattern.compile("[\\p{Print}\\s]*?/WMode\\s*([0-1])\\s*def[\\p{Print}\\s]*?");
    static Pattern cmapVersionPattern = Pattern.compile("/CMapVersion ([^ ]*) def");
    static Pattern cmapTypePattern = Pattern.compile("/CMapType ([1-2]) def");
    static Pattern cmapNamePattern = Pattern.compile(" */CMapName /([A-Za-z0-9-]*) def");
    static Pattern useCMapNamePattern = Pattern.compile(" */([A-Za-z0-9-]*) usecmap");
    static Pattern codesSpaceRangePattern = Pattern.compile("([0-9]*) begincodespacerange");
    static Pattern codesSpacePattern = Pattern.compile("\\s*<?([0-9a-fA-F]*)>\\s*<([0-9a-fA-F]*)>?");
    static Pattern beginnotdefrangePattern = Pattern.compile("([0-9]*) beginnotdefrange");
    static Pattern notdefrangePattern = Pattern.compile("<?([0-9a-fA-F]*)>\\s*<([0-9a-fA-F]*)>\\s*([0-9]*)");
    static Pattern beginbfcharPattern = Pattern.compile("\\s*([0-9]*) beginbfchar");
    static Pattern bfcharPattern = Pattern.compile("<?([0-9a-fA-F]*)>\\s*<([0-9a-fA-F]*)>?");
    static Pattern beginbfcharPatternForToUnicodeCMap = Pattern.compile("[\\p{Print}\\s]*?(([0-9]*)\\s+beginbfchar)[\\p{Print}\\s]*?");
    static String regExToMatchUnicodeCharName = "!-;?-~";
    static Pattern bfcharPatternForToUnicodeCMap = Pattern.compile("(\\s*<?\\s*([0-9a-fA-F]*)\\s*>\\s*((<\\s*([0-9a-fA-F]*)\\s*>?\\s*)|([" + regExToMatchUnicodeCharName + "]+)))[\\p{Print}\\s]*?");
    static Pattern beginbfrangePattern = Pattern.compile("\\s*([0-9]*) beginbfrange");
    static Pattern bfrangePattern = Pattern.compile("<?([0-9a-fA-F]*)>\\s*<([0-9a-fA-F]*)>\\s*<([0-9a-fA-F]*)>?");
    static Pattern beginbfrangePatternForToUnicodeCMap = Pattern.compile("[\\p{Print}\\s]*?(([0-9]*)\\s+beginbfrange)[\\p{Print}\\s]*?");
    static Pattern bfrangePatternForToUnicodeCMap = Pattern.compile("(\\s*<?\\s*([0-9a-fA-F]*)\\s*>\\s*<\\s*([0-9a-fA-F]*)\\s*>\\s*((<\\s*([0-9a-fA-F]*)\\s*>?\\s*)|([" + regExToMatchUnicodeCharName + "]+)))[\\p{Print}\\s]*?");
    static Pattern bfrangePatternWithDiscontnuousDststrForToUnicodeCMap = Pattern.compile("(<?\\s*([0-9a-fA-F]*)\\s*>\\s*<\\s*([0-9a-fA-F]*)\\s*>\\s*\\[\\s*((((<\\s*[0-9a-fA-F]*\\s*>?)|([" + regExToMatchUnicodeCharName + "]*))\\s*)*)\\])[\\p{Print}\\s]*?");
    static Pattern begincidcharPattern = Pattern.compile("([0-9]*) begincidchar");
    static Pattern cidcharPattern = Pattern.compile("\\s*<?([0-9a-fA-F]*)>\\s*([0-9]*)");
    static Pattern begincidrangePattern = Pattern.compile("([0-9]*) begincidrange");
    static Pattern cidrangePattern = Pattern.compile("\\s*<?([0-9a-fA-F]*)>\\s*<([0-9a-fA-F]*)>?\\s*([0-9]*)");
    private static final int DSTR_TYPE_SIMPLE = 0;
    private static final int DSTR_TYPE_DISCONTINUOUS = 1;
    private static CMapResourceBuilder _instance = new CMapResourceBuilder();

    private CMapResourceBuilder() {
    }

    static CMapResourceBuilder getInstance() {
        return _instance;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public static PDFWritingMode getWMode(CosStream cmapObj) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        BufferedReader cmapLineReader = null;
        InputByteStream decodedCmapStream = null;
        InputStream cmapStream = null;
        try {
            try {
                decodedCmapStream = cmapObj.getStreamDecoded();
                cmapStream = decodedCmapStream.toInputStream();
                cmapLineReader = new LineNumberReader(new InputStreamReader(cmapStream));
                String lineRead = "";
                StringBuilder strToMatch = new StringBuilder(100);
                while ((lineRead = cmapLineReader.readLine()) != null) {
                    if (!lineRead.contains(ASName.k_WMode.asString(true))) continue;
                    strToMatch.append(lineRead);
                    Matcher patternMatcher = writingModePattern.matcher(strToMatch);
                    if (!patternMatcher.matches()) continue;
                    int writingMode = Integer.parseInt(patternMatcher.group(1));
                    PDFWritingMode pDFWritingMode = PDFWritingMode.getWritingModeForValue(writingMode);
                    return pDFWritingMode;
                }
                return PDFWritingMode.HORIZONTAL;
            }
            finally {
                try {
                    if (cmapLineReader != null) {
                        cmapLineReader.close();
                    }
                }
                finally {
                    try {
                        if (cmapStream != null) {
                            cmapStream.close();
                        }
                    }
                    finally {
                        if (decodedCmapStream != null) {
                            decodedCmapStream.close();
                        }
                    }
                }
            }
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
        catch (PDFInvalidParameterException e) {
            throw new PDFIOException((Throwable)e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static ArrayList<String> getReferencedCmaps(CosStream cmapObj) throws PDFInvalidDocumentException, PDFIOException, PDFSecurityException {
        ArrayList<String> cmapList;
        BufferedReader cmapLineReader = null;
        InputByteStream decodedCmapStream = null;
        InputStream cmapStream = null;
        cmapList = null;
        try {
            try {
                decodedCmapStream = cmapObj.getStreamDecoded();
                cmapStream = decodedCmapStream.toInputStream();
                cmapLineReader = new LineNumberReader(new InputStreamReader(cmapStream));
                String lineRead = "";
                StringBuilder strToMatch = new StringBuilder(100);
                while ((lineRead = cmapLineReader.readLine()) != null) {
                    if (!lineRead.contains("usecmap")) continue;
                    strToMatch.append(lineRead);
                    Matcher m = useCMapNamePattern.matcher(strToMatch);
                    if (!m.matches()) continue;
                    if (cmapList == null) {
                        cmapList = new ArrayList<String>();
                    }
                    cmapList.add(m.group(1));
                }
            }
            finally {
                try {
                    if (cmapLineReader != null) {
                        cmapLineReader.close();
                    }
                }
                finally {
                    try {
                        if (cmapStream != null) {
                            cmapStream.close();
                        }
                    }
                    finally {
                        if (decodedCmapStream != null) {
                            decodedCmapStream.close();
                        }
                    }
                }
            }
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
        return cmapList;
    }

    public static HashSet<PDFToUnicodeCMap.CMapCodeMapping> parseToUnicodeMap(InputStream inputStream, boolean ignoreErrors) throws PDFIOException, PDFParseException {
        HashSet<PDFToUnicodeCMap.CMapCodeMapping> codeMapSet;
        codeMapSet = new HashSet<PDFToUnicodeCMap.CMapCodeMapping>();
        LineNumberReader toUnicodeCMapLineReader = new LineNumberReader(new InputStreamReader(inputStream));
        try {
            StringBuilder strToMatch = new StringBuilder(100);
            strToMatch.append(CMapResourceBuilder.validateCodeSpaceRangeExistence(toUnicodeCMapLineReader));
            Matcher beginbfcharPatternMatcher = beginbfcharPatternForToUnicodeCMap.matcher(strToMatch);
            Matcher bfcharPatternMatcher = bfcharPatternForToUnicodeCMap.matcher(strToMatch);
            Matcher beginbfrangePatternMatcher = beginbfrangePatternForToUnicodeCMap.matcher(strToMatch);
            Matcher bfrangePatternMatcher = bfrangePatternForToUnicodeCMap.matcher(strToMatch);
            Matcher bfrangeWithDiscontinuousDststrPatternMatcher = bfrangePatternWithDiscontnuousDststrForToUnicodeCMap.matcher(strToMatch);
            String lineRead = "";
            while ((lineRead = toUnicodeCMapLineReader.readLine()) != null) {
                int count;
                if (lineRead.trim().equals("")) continue;
                strToMatch.append(lineRead);
                if (strToMatch.toString().contains(ASName.k_beginbfchar.asString(true))) {
                    beginbfcharPatternMatcher.reset(strToMatch);
                    if (beginbfcharPatternMatcher.matches()) {
                        int charCount = Integer.parseInt(beginbfcharPatternMatcher.group(2));
                        strToMatch.delete(0, beginbfcharPatternMatcher.end(1));
                        count = 0;
                        while (count < charCount) {
                            block23 : {
                                lineRead = toUnicodeCMapLineReader.readLine();
                                if (lineRead == null) {
                                    throw new IllegalArgumentException("Invalid bfchar pattern in ToUnicodeCmap");
                                }
                                String[] strs = lineRead.split(" ");
                                StringBuilder strBuilder = new StringBuilder();
                                for (int i = 0; i < strs.length; ++i) {
                                    strBuilder.append(strs[i]);
                                }
                                strToMatch.append(strBuilder.toString());
                                bfcharPatternMatcher.reset(strToMatch);
                                if (!bfcharPatternMatcher.matches()) continue;
                                ++count;
                                try {
                                    CMapResourceBuilder.addCMapCodeMappingForBfcharToCodeMapSet(bfcharPatternMatcher, codeMapSet);
                                }
                                catch (NumberFormatException e) {
                                    if (ignoreErrors) break block23;
                                    throw e;
                                }
                            }
                            strToMatch.delete(0, bfcharPatternMatcher.end(1));
                        }
                        continue;
                    }
                }
                if (!strToMatch.toString().contains(ASName.k_beginbfrange.asString(true))) continue;
                beginbfrangePatternMatcher.reset(strToMatch);
                if (!beginbfrangePatternMatcher.matches()) continue;
                int rangeCount = Integer.parseInt(beginbfrangePatternMatcher.group(2));
                strToMatch.delete(0, beginbfrangePatternMatcher.end(1));
                count = 0;
                boolean matched = false;
                while (count < rangeCount) {
                    if (!ignoreErrors || strToMatch.toString().length() == 0 || !matched) {
                        lineRead = toUnicodeCMapLineReader.readLine();
                        if (lineRead == null) {
                            throw new IllegalArgumentException("Invalid bfchar range pattern in ToUnicodeCmap");
                        }
                        lineRead = lineRead.replaceAll(" ", "");
                        strToMatch.append(lineRead);
                    }
                    matched = false;
                    if (strToMatch.toString().contains("[")) {
                        String subStr;
                        if (!strToMatch.toString().contains("]") && (subStr = strToMatch.substring(strToMatch.indexOf("["))).contains("<")) continue;
                        bfrangeWithDiscontinuousDststrPatternMatcher.reset(strToMatch);
                        if (bfrangeWithDiscontinuousDststrPatternMatcher.matches()) {
                            ++count;
                            CMapResourceBuilder.addCMapCodeMappingForBfcharRangeToCodeMapSet(bfrangeWithDiscontinuousDststrPatternMatcher, 1, codeMapSet);
                            strToMatch.delete(0, bfrangeWithDiscontinuousDststrPatternMatcher.end(1));
                            matched = true;
                            continue;
                        }
                    }
                    bfrangePatternMatcher.reset(strToMatch);
                    if (!bfrangePatternMatcher.matches()) continue;
                    ++count;
                    CMapResourceBuilder.addCMapCodeMappingForBfcharRangeToCodeMapSet(bfrangePatternMatcher, 0, codeMapSet);
                    strToMatch.delete(0, bfrangePatternMatcher.end(1));
                    matched = true;
                }
            }
        }
        catch (IOException e) {
            throw new PDFIOException((Throwable)e);
        }
        finally {
            try {
                if (toUnicodeCMapLineReader != null) {
                    toUnicodeCMapLineReader.close();
                }
            }
            catch (IOException e) {
                throw new PDFIOException((Throwable)e);
            }
        }
        return codeMapSet;
    }

    private static String validateCodeSpaceRangeExistence(LineNumberReader toUnicodeCMapLineReader) throws IOException, PDFParseException {
        String lineRead = "";
        while ((lineRead = toUnicodeCMapLineReader.readLine()) != null && !lineRead.contains(ASName.k_begincodespacerange.asString(true))) {
        }
        if (lineRead != null) {
            return lineRead;
        }
        throw new PDFParseException("ToUnicodeCmap could not be parsed: missing token \"begincodespacerange\"");
    }

    private static void addCMapCodeMappingForBfcharToCodeMapSet(Matcher bfCharPatternMatcher, HashSet<PDFToUnicodeCMap.CMapCodeMapping> codeMapSet) throws PDFParseException {
        int srcCode = Integer.parseInt(bfCharPatternMatcher.group(2), 16);
        PDFToUnicodeCMap.CMapCodeMapping codeMap = new PDFToUnicodeCMap.CMapCodeMapping(srcCode);
        String dstString = "";
        if (bfCharPatternMatcher.group(5) != null) {
            dstString = bfCharPatternMatcher.group(5);
            codeMap.addHexData(CMapResourceBuilder.hexStringToByteArray(dstString));
        } else {
            dstString = bfCharPatternMatcher.group(3);
            codeMap.addCharName(dstString);
        }
        codeMapSet.add(codeMap);
    }

    private static void addCMapCodeMappingForBfcharRangeToCodeMapSet(Matcher bfCharRangePatternMatcher, int dstStringType, HashSet<PDFToUnicodeCMap.CMapCodeMapping> codeMapSet) throws PDFParseException {
        int srcCode1 = Integer.parseInt(bfCharRangePatternMatcher.group(2), 16);
        int srcCode2 = Integer.parseInt(bfCharRangePatternMatcher.group(3), 16);
        PDFToUnicodeCMap.CMapCodeMapping codeMap = new PDFToUnicodeCMap.CMapCodeMapping(srcCode1, srcCode2);
        String dstString = "";
        switch (dstStringType) {
            case 0: {
                dstString = "";
                if (bfCharRangePatternMatcher.group(6) != null) {
                    dstString = bfCharRangePatternMatcher.group(6);
                    codeMap.addHexData(CMapResourceBuilder.hexStringToByteArray(dstString));
                    break;
                }
                dstString = bfCharRangePatternMatcher.group(4);
                codeMap.addCharName(dstString);
                break;
            }
            case 1: {
                dstString = bfCharRangePatternMatcher.group(4);
                String[] unicodeStrs = dstString.split(">|\\s+");
                for (int i = 0; i < unicodeStrs.length; ++i) {
                    if (unicodeStrs[i].trim().equals("")) continue;
                    if (unicodeStrs[i].charAt(0) == '<') {
                        codeMap.addHexData(CMapResourceBuilder.hexStringToByteArray(unicodeStrs[i].substring(1, unicodeStrs[i].length())));
                        continue;
                    }
                    codeMap.addCharName(unicodeStrs[i]);
                }
                break;
            }
        }
        codeMapSet.add(codeMap);
    }

    private static byte[] hexStringToByteArray(String hexStr) throws PDFParseException {
        byte[] strBytes = hexStr.getBytes();
        if (strBytes.length % 2 == 1) {
            hexStr = "0" + hexStr;
            strBytes = hexStr.getBytes();
        }
        byte[] hexBytes = new byte[strBytes.length / 2];
        int start = 0;
        int end = strBytes.length;
        int index = 0;
        for (int i = start; i < end; i += 2) {
            byte l;
            byte h;
            try {
                h = CosToken.toHexDigit((byte)strBytes[i]);
                l = CosToken.toHexDigit((byte)strBytes[i + 1]);
            }
            catch (PDFCosParseException e) {
                throw new PDFParseException((Throwable)e);
            }
            hexBytes[index++] = (byte)(h * 16 + l);
        }
        return hexBytes;
    }

    CMapObject parseCMapFile(String fileName, boolean getCodesSpaceRangeOnly, HashMap<Long, Integer> spaceCharacterCodeToUnicodeMap, boolean printStats) throws Exception {
        CMapObject cmap = CMapObjectCache.getCmap(fileName, this, spaceCharacterCodeToUnicodeMap);
        if (cmap == null) {
            throw new MissingResourceException("Cannot load CMap resource " + fileName, "", "");
        }
        return cmap;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    CMapObject parseCMapFile(LineNumberReader f, Map<String, InputStream> useCMaps, boolean getCodesSpaceRangeOnly, HashMap<Long, Integer> spaceCharacterCodeToUnicodeMap, boolean printStats) throws Exception {
        try {
            String line;
            String registry = "?";
            String ordering = "?";
            String writingMode = "?";
            String cmapVersion = "?";
            String cmapName = "?";
            String useCMapName = null;
            CMapType cmapType = null;
            boolean isCodesSpacesDefined = false;
            CMapCodesSpace[] codesSpaces = null;
            HashMap<Long, int[]> map = new HashMap<Long, int[]>();
            HashMap<Long, Integer> charCodeToCIDMap = null;
            HashMap<Long, int[]> charCodeToCIDMapLong = null;
            int supplement = 0;
            int minBytes = 100;
            int maxBytes = 1;
            while ((line = f.readLine()) != null) {
                int i;
                int[] cidsTemp;
                long charCode2;
                Matcher m2;
                int cid;
                long charCode1;
                int charCode;
                Matcher m = registryPattern.matcher(line);
                if (m.matches()) {
                    registry = m.group(1);
                    continue;
                }
                m = orderingPattern.matcher(line);
                if (m.matches()) {
                    ordering = m.group(1);
                    continue;
                }
                m = supplementPattern.matcher(line);
                if (m.matches()) {
                    supplement = m.group(1).charAt(0) - 48;
                    continue;
                }
                m = writingModePattern.matcher(line);
                if (m.matches()) {
                    writingMode = m.group(1);
                    continue;
                }
                m = cmapVersionPattern.matcher(line);
                if (m.matches()) {
                    cmapVersion = m.group(1);
                    continue;
                }
                m = cmapTypePattern.matcher(line);
                if (m.matches()) {
                    String cmapTypeStr = m.group(1);
                    if ("1".equals(cmapTypeStr)) {
                        cmapType = CMapType.StandardCMap;
                        continue;
                    }
                    if ("2".equals(cmapTypeStr)) {
                        cmapType = CMapType.ToUnicodeMap;
                        continue;
                    }
                    throw new IllegalArgumentException("CMapType " + cmapTypeStr + " is invalid.");
                }
                m = cmapNamePattern.matcher(line);
                if (m.matches()) {
                    cmapName = m.group(1);
                    continue;
                }
                m = useCMapNamePattern.matcher(line);
                if (m.matches()) {
                    useCMapName = m.group(1);
                    CMapObject useCMapObject = null;
                    useCMapObject = useCMaps != null && useCMaps.containsKey(useCMapName) ? this.parseCMapFile(new LineNumberReader(new InputStreamReader(useCMaps.get(useCMapName))), useCMaps, getCodesSpaceRangeOnly, spaceCharacterCodeToUnicodeMap, printStats) : this.parseCMapFile(useCMapName, getCodesSpaceRangeOnly, spaceCharacterCodeToUnicodeMap, printStats);
                    codesSpaces = useCMapObject.getCodesSpaces();
                    minBytes = useCMapObject.getMinBytesNeeded();
                    maxBytes = useCMapObject.getMaxBytesAllowed();
                    if (codesSpaces == null || codesSpaces.length <= 0) continue;
                    isCodesSpacesDefined = true;
                    continue;
                }
                m = codesSpaceRangePattern.matcher(line);
                if (m.matches()) {
                    if (m.matches()) {
                        int codesSpaceCount = Integer.parseInt(m.group(1));
                        codesSpaces = new CMapCodesSpace[codesSpaceCount];
                        for (i = 0; i < codesSpaceCount; ++i) {
                            int tempBytes;
                            line = f.readLine();
                            m2 = codesSpacePattern.matcher(line);
                            if (!m2.matches()) {
                                throw new IllegalArgumentException();
                            }
                            long code1 = Long.parseLong(m2.group(1), 16);
                            long code2 = Long.parseLong(m2.group(2), 16);
                            codesSpaces[i] = new CMapCodesSpace(code1, code2);
                            if (!isCodesSpacesDefined) {
                                isCodesSpacesDefined = true;
                            }
                            if (minBytes > (tempBytes = m2.group(1).length() / 2)) {
                                minBytes = tempBytes;
                            }
                            if (maxBytes >= tempBytes) continue;
                            maxBytes = tempBytes;
                        }
                    }
                    if (!getCodesSpaceRangeOnly) continue;
                    break;
                }
                m = beginnotdefrangePattern.matcher(line);
                if (m.matches()) {
                    cmapType = CMapResourceBuilder.checkOperatorCompatibity(cmapType, CMapType.StandardCMap, isCodesSpacesDefined);
                    int rangeCount = Integer.parseInt(m.group(1));
                    for (i = 0; i < rangeCount; ++i) {
                        line = f.readLine();
                        m2 = notdefrangePattern.matcher(line);
                        if (!m2.matches()) {
                            throw new IllegalArgumentException();
                        }
                        charCode1 = Long.parseLong(m2.group(1), 16);
                        charCode2 = Long.parseLong(m2.group(2), 16);
                        cid = Integer.parseInt(m2.group(3), 10);
                        charCode = 0;
                        while ((long)charCode < charCode2 - charCode1 + 1) {
                            cidsTemp = new int[]{cid};
                            map.put((long)charCode + charCode1, cidsTemp);
                            ++charCode;
                        }
                    }
                    continue;
                }
                m = beginbfcharPattern.matcher(line);
                if (m.matches()) {
                    cmapType = CMapResourceBuilder.checkOperatorCompatibity(cmapType, CMapType.ToUnicodeMap, isCodesSpacesDefined);
                    int charCount = Integer.parseInt(m.group(1));
                    for (i = 0; i < charCount; ++i) {
                        line = f.readLine();
                        m2 = bfcharPattern.matcher(line);
                        if (!m2.matches()) {
                            throw new IllegalArgumentException();
                        }
                        long cid2 = Long.parseLong(m2.group(1), 16);
                        String usvs = m2.group(2);
                        int[] unicodes = this.parseUnicodes(usvs);
                        map.put(cid2, unicodes);
                        this.cacheSpaceCIDToUnicodeMap(cid2, unicodes, ordering);
                    }
                    continue;
                }
                m = beginbfrangePattern.matcher(line);
                if (m.matches()) {
                    cmapType = CMapResourceBuilder.checkOperatorCompatibity(cmapType, CMapType.ToUnicodeMap, isCodesSpacesDefined);
                    int rangeCount = Integer.parseInt(m.group(1));
                    for (i = 0; i < rangeCount; ++i) {
                        line = f.readLine();
                        m2 = bfrangePattern.matcher(line);
                        if (!m2.matches()) {
                            throw new IllegalArgumentException();
                        }
                        long cid1 = Long.parseLong(m2.group(1), 16);
                        long cid2 = Long.parseLong(m2.group(2), 16);
                        String usvs = m2.group(3);
                        int[] usvsVals = this.parseUnicodes(usvs);
                        int valCount = usvsVals.length;
                        int cid3 = 0;
                        while ((long)cid3 < cid2 - cid1 + 1) {
                            int[] usvsValsTemp = new int[valCount];
                            System.arraycopy(usvsVals, 0, usvsValsTemp, 0, usvsVals.length);
                            usvsValsTemp[valCount - 1] = usvsVals[valCount - 1] + cid3;
                            map.put((long)cid3 + cid1, usvsValsTemp);
                            this.cacheSpaceCIDToUnicodeMap((long)cid3 + cid1, usvsValsTemp, ordering);
                            ++cid3;
                        }
                    }
                    continue;
                }
                m = begincidcharPattern.matcher(line);
                if (m.matches()) {
                    cmapType = CMapResourceBuilder.checkOperatorCompatibity(cmapType, CMapType.StandardCMap, isCodesSpacesDefined);
                    int cidCount = Integer.parseInt(m.group(1));
                    for (i = 0; i < cidCount; ++i) {
                        line = f.readLine();
                        m2 = cidcharPattern.matcher(line);
                        if (!m2.matches()) {
                            throw new IllegalArgumentException();
                        }
                        long charCode3 = Long.parseLong(m2.group(1), 16);
                        int[] cids = new int[]{Integer.parseInt(m2.group(2), 10)};
                        map.put(charCode3, cids);
                        this.updateSpaceCharacterCodeToUnicodeMap(spaceCharacterCodeToUnicodeMap, charCode3, cids, registry, ordering);
                    }
                    continue;
                }
                m = begincidrangePattern.matcher(line);
                if (!m.matches()) continue;
                cmapType = CMapResourceBuilder.checkOperatorCompatibity(cmapType, CMapType.StandardCMap, isCodesSpacesDefined);
                int rangeCount = Integer.parseInt(m.group(1));
                for (i = 0; i < rangeCount; ++i) {
                    line = f.readLine();
                    m2 = cidrangePattern.matcher(line);
                    if (!m2.matches()) {
                        throw new IllegalArgumentException();
                    }
                    charCode1 = Long.parseLong(m2.group(1), 16);
                    charCode2 = Long.parseLong(m2.group(2), 16);
                    cid = Integer.parseInt(m2.group(3), 10);
                    charCode = 0;
                    while ((long)charCode < charCode2 - charCode1 + 1) {
                        cidsTemp = new int[]{cid + charCode};
                        map.put((long)charCode + charCode1, cidsTemp);
                        this.updateSpaceCharacterCodeToUnicodeMap(spaceCharacterCodeToUnicodeMap, (long)charCode + charCode1, cidsTemp, registry, ordering);
                        ++charCode;
                    }
                }
            }
            if (printStats) {
                System.out.println("    handling " + registry + " " + ordering + " " + cmapVersion);
            }
            if (cmapType == CMapType.StandardCMap || cmapType == CMapType.ToUnicodeMap) {
                Object it;
                long count = 0;
                int curCodesSpaceIdx = -1;
                if (useCMapName != null && !map.isEmpty()) {
                    charCodeToCIDMap = new HashMap<Long, Integer>();
                    charCodeToCIDMapLong = new HashMap<Long, int[]>();
                    for (Map.Entry entry : map.entrySet()) {
                        long code = (Long)entry.getKey();
                        this.AddCode(charCodeToCIDMap, charCodeToCIDMapLong, code, (int[])entry.getValue());
                        ++count;
                    }
                } else {
                    for (Map.Entry entry : map.entrySet()) {
                        long code = (Long)entry.getKey();
                        curCodesSpaceIdx = this.getCodesSpace(code, codesSpaces, curCodesSpaceIdx);
                        void codesSpace = codesSpaces[curCodesSpaceIdx];
                        codesSpace.addCode(code, (int[])entry.getValue());
                        ++count;
                    }
                }
                if (printStats) {
                    System.out.print("total:" + count);
                }
                if (cmapType == CMapType.StandardCMap) {
                    it = new CMapObjectImpl(registry, ordering, supplement, writingMode, cmapName, useCMapName, codesSpaces, minBytes, maxBytes, spaceCharacterCodeToUnicodeMap, charCodeToCIDMap, charCodeToCIDMapLong);
                    return it;
                }
                it = new CMapObjectImpl(registry, ordering, supplement, writingMode, cmapName, useCMapName, codesSpaces, minBytes, maxBytes, this.spaceCIDToUnicodeMap.get(ordering), null, null);
                return it;
            }
            CMapObject count = null;
            return count;
        }
        finally {
            f.close();
        }
    }

    private void AddCode(HashMap<Long, Integer> charCodeCIDMap, HashMap<Long, int[]> charCodeCIDMapLong, long code, int[] value) {
        if (code <= Integer.MAX_VALUE) {
            if (value.length > 1) {
                this.addLongCode(code, value, charCodeCIDMapLong);
            } else {
                this.addIntCode(code, value[0], charCodeCIDMap);
            }
        } else {
            this.addLongCode(code, value, charCodeCIDMapLong);
        }
    }

    private void addLongCode(long code, int[] values, HashMap<Long, int[]> charCodeCIDMapLong) {
        if (charCodeCIDMapLong == null) {
            charCodeCIDMapLong = new HashMap();
        }
        charCodeCIDMapLong.put(code, values);
    }

    private void addIntCode(long code, int value, HashMap<Long, Integer> charCodeCIDMap) {
        if (charCodeCIDMap == null) {
            charCodeCIDMap = new HashMap();
        }
        charCodeCIDMap.put(code, value);
    }

    private int getCodesSpace(long code, CMapCodesSpace[] codesSpaces, int curCodesSpaceIdx) throws IOException {
        CMapCodesSpace codesSpace = null;
        if (curCodesSpaceIdx != -1 && (codesSpace = codesSpaces[curCodesSpaceIdx]).isCodeInCodeSpace(code)) {
            return curCodesSpaceIdx;
        }
        for (int iCodesSpace = 0; iCodesSpace < codesSpaces.length; ++iCodesSpace) {
            codesSpace = codesSpaces[iCodesSpace];
            if (!codesSpace.isCodeInCodeSpace(code)) continue;
            return iCodesSpace;
        }
        throw new IOException("Code " + code + " is not in any codes space range.");
    }

    private int[] parseUnicodes(String usvs) {
        int[] values = new int[usvs.length() / 4];
        int value = -1;
        for (int idxUsv = 0; idxUsv < usvs.length(); idxUsv += 4) {
            value = idxUsv + 4 > usvs.length() ? Integer.parseInt(usvs.substring(idxUsv), 16) : Integer.parseInt(usvs.substring(idxUsv, idxUsv + 4), 16);
            values[idxUsv / 4] = value;
        }
        return values;
    }

    private void cacheSpaceCIDToUnicodeMap(long cid, int[] unicodes, String ordering) {
        if (unicodes.length == 1 && Character.getType(unicodes[0]) == 12) {
            HashMap cidToUnicode;
            if (this.spaceCIDToUnicodeMap.containsKey(ordering)) {
                cidToUnicode = this.spaceCIDToUnicodeMap.get(ordering);
            } else {
                cidToUnicode = new HashMap();
                this.spaceCIDToUnicodeMap.put(ordering, cidToUnicode);
            }
            cidToUnicode.put(cid, unicodes[0]);
        }
    }

    private void updateSpaceCharacterCodeToUnicodeMap(HashMap<Long, Integer> spaceCharacterCodeToUnicodeMap, long charCode, int[] cid, String registry, String ordering) throws Exception {
        HashMap<Long, Integer> cidToUnicode;
        String cidToUnicodeCMap;
        Set<Long> cids;
        if (cid.length == 1 && this.spaceCIDToUnicodeMap.containsKey(cidToUnicodeCMap = registry + "_" + ordering + "_UCS2") && (cids = (cidToUnicode = this.spaceCIDToUnicodeMap.get(cidToUnicodeCMap)).keySet()).contains(Long.valueOf(cid[0]))) {
            spaceCharacterCodeToUnicodeMap.put(charCode, cidToUnicode.get(Long.valueOf(cid[0])));
        }
    }

    private static CMapType checkOperatorCompatibity(CMapType existingCMapType, CMapType expectedCMapType, boolean isCodesSpacesDefined) throws IllegalArgumentException {
        if (!isCodesSpacesDefined) {
            throw new IllegalArgumentException("begincodesspacerange is not defined. Exit.");
        }
        if (existingCMapType == null) {
            return expectedCMapType;
        }
        if (expectedCMapType != existingCMapType) {
            throw new IllegalArgumentException("Mixing of operators begincidchar/begincidrange and beginbfchar/beginbfrange are found.");
        }
        return existingCMapType;
    }

    /*
     * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
     */
    static enum CMapType {
        StandardCMap(1),
        ToUnicodeMap(2);
        
        int value = 0;

        private CMapType(int type) {
            this.value = type;
        }
    }

}