CosStream.java 40 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 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.io.ByteWriterFactory
 *  com.adobe.internal.io.ByteWriterFactory$Fixed
 *  com.adobe.internal.io.stream.IO
 *  com.adobe.internal.io.stream.InputByteStream
 *  com.adobe.internal.io.stream.OutputByteStream
 *  com.adobe.internal.io.stream.StreamManager
 */
package com.adobe.internal.pdftoolkit.core.cos;

import com.adobe.internal.io.ByteWriterFactory;
import com.adobe.internal.io.stream.IO;
import com.adobe.internal.io.stream.InputByteStream;
import com.adobe.internal.io.stream.OutputByteStream;
import com.adobe.internal.io.stream.StreamManager;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosBoolean;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosEncryption;
import com.adobe.internal.pdftoolkit.core.cos.CosLinearization;
import com.adobe.internal.pdftoolkit.core.cos.CosName;
import com.adobe.internal.pdftoolkit.core.cos.CosNull;
import com.adobe.internal.pdftoolkit.core.cos.CosNumeric;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectInfo;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectStream;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosRepairUtils;
import com.adobe.internal.pdftoolkit.core.cos.CosUtils;
import com.adobe.internal.pdftoolkit.core.cos.DecryptingInputStream;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFCosParseException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFIOException;
import com.adobe.internal.pdftoolkit.core.exceptions.PDFSecurityException;
import com.adobe.internal.pdftoolkit.core.filter.CustomFilterRegistry;
import com.adobe.internal.pdftoolkit.core.filter.FilterParams;
import com.adobe.internal.pdftoolkit.core.filter.FilterStream;
import com.adobe.internal.pdftoolkit.core.securityframework.EncryptionHandlerState;
import com.adobe.internal.pdftoolkit.core.types.ASName;
import com.adobe.internal.pdftoolkit.core.util.BooleanHolder;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipException;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class CosStream
extends CosDictionary {
    private boolean mDataInPDF;
    private long mPos;
    private InputByteStream mDataStream;
    private boolean mDataEncoded;
    private boolean mToEncrypt;
    private boolean mIsEncrypted;
    private CosArray mOutputFilters;
    private ASName mCryptFilter;
    private boolean mCryptFilterSet;

    CosStream(CosDocument doc, Map map, CosObjectInfo info, long pos) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        super(doc, map, info);
        this.init(true, pos, null, true, true);
    }

    CosStream(CosDocument doc, CosObjectInfo info) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        super(doc, new LinkedHashMap<ASName, CosObject>(), info);
        this.init(false, 0, null, true, true);
    }

    CosStream(CosDocument doc, CosObjectInfo info, InputByteStream rep) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        super(doc, new LinkedHashMap<ASName, CosObject>(), info);
        this.init(false, 0, rep, true, true);
    }

    private void init(boolean dataInPDF, long pos, InputByteStream decoded, boolean toEncrypt, boolean isEncrypted) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        this.mDataInPDF = dataInPDF;
        this.mPos = pos;
        if (!dataInPDF) {
            this.setCachedStream(false, decoded);
            this.put(ASName.k_Length, 0);
        } else {
            CosObject lenObj = this.get(ASName.k_Length);
            if (!(lenObj instanceof CosNumeric)) {
                long length = CosRepairUtils.getStreamLength(this.getDocument(), pos);
                if (length == -1) {
                    throw new PDFCosParseException("Expected 'endstream' for stream at pos " + pos);
                }
                this.setRepairedValue(ASName.k_Length, this.getDocument().createCosNumeric(length));
            }
        }
        this.mToEncrypt = toEncrypt;
        this.mIsEncrypted = isEncrypted;
    }

    @Override
    void close() throws IOException {
        this.releaseStreams();
        this.mOutputFilters = null;
        super.close();
    }

    @Override
    void release() throws IOException {
        this.releaseStreams();
        this.mDataInPDF = true;
        this.mIsEncrypted = this.mToEncrypt;
        super.release();
    }

    private void releaseStreams() throws IOException {
        this.setCachedStream(false, null);
    }

    private InputByteStream getCachedStream(boolean encoded) {
        if (encoded != this.mDataEncoded) {
            return null;
        }
        return this.mDataStream;
    }

    private void setCachedStream(boolean encoded, InputByteStream newStream) throws IOException {
        if (this.mDataStream != null) {
            this.mDataStream.close();
        }
        this.mDataStream = newStream;
        this.mDataEncoded = encoded;
    }

    @Override
    public CosObject put(ASName key, CosObject cosObject) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            CosObject oldValue;
            if (!(!this.mDataInPDF && !this.mDataEncoded || key != ASName.k_Filter && key != ASName.k_DecodeParms || cosObject.equals(oldValue = this.get(key)))) {
                InputByteStream stm = this.getStream(false, false, true);
                this.mCryptFilterSet = false;
                this.setCachedStream(false, stm);
                this.mDataInPDF = false;
            }
            return super.put(key, cosObject);
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    @Override
    public CosObject remove(ASName key) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            CosObject oldValue;
            if ((this.mDataInPDF || this.mDataEncoded) && (key == ASName.k_Filter || key == ASName.k_DecodeParms) && (oldValue = this.get(key)) != null) {
                InputByteStream stm = this.getStream(false, false, true);
                this.mCryptFilterSet = false;
                this.setCachedStream(false, stm);
                this.mDataInPDF = false;
            }
            return super.remove(key);
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    @Override
    public int getType() {
        return 7;
    }

    public long getLength() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        return this.getLong(ASName.k_Length);
    }

    public InputByteStream getStreamDecoded() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            return this.getStream(false, true, true);
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public InputStream getStreamDecodedNoCopying() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        InputByteStream ibs = null;
        OutputByteStream os = null;
        try {
            InputStream inputStream;
            block10 : {
                try {
                    BooleanHolder wasDecrypted = new BooleanHolder(false);
                    InputStream is = this.getStreamForCopying(false, wasDecrypted);
                    if (wasDecrypted.getValue()) {
                        os = this.getStreamManager().getUnregisteredOutputByteStream(ByteWriterFactory.Fixed.GROWABLE, is.available());
                        this.copyStream((T)os, is, false);
                        os.flush();
                        ibs = os.closeAndConvert();
                        os = null;
                        InputStream inputStream2 = ibs.toInputStream();
                        Object var7_8 = null;
                        if (os != null) {
                            os.close();
                        }
                        if (ibs != null) {
                            ibs.close();
                        }
                        return inputStream2;
                    }
                    inputStream = is;
                    Object var7_9 = null;
                    if (os == null) break block10;
                }
                catch (Throwable var6_11) {
                    Object var7_10 = null;
                    if (os != null) {
                        os.close();
                    }
                    if (ibs != null) {
                        ibs.close();
                    }
                    throw var6_11;
                }
                os.close();
            }
            if (ibs != null) {
                ibs.close();
            }
            return inputStream;
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    public InputByteStream getStreamEncoded() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            return this.getStream(true, true, true);
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    public <T> boolean copyStream(T destStm, boolean encoded) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            return this.copyStream(destStm, this.getStreamForCopying(encoded, new BooleanHolder(false)), encoded);
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    private <T> boolean copyStream(T destStm, InputStream srcStm, boolean encoded) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        InputStream is = null;
        try {
            boolean bytesCopied222;
            try {
                InputStream inputStream = is = srcStm != null ? srcStm : this.getStreamForCopying(encoded, new BooleanHolder(false));
                if (is != null) {
                    long bytesCopied222 = 0;
                    try {
                        if (destStm instanceof OutputStream) {
                            bytesCopied222 = IO.copy((InputStream)is, (OutputStream)((OutputStream)destStm));
                        } else {
                            if (!(destStm instanceof OutputByteStream)) throw new PDFIOException("Destination stream must be either OutputStream or OutputByteStream.");
                            bytesCopied222 = IO.copy((InputStream)is, (OutputByteStream)((OutputByteStream)destStm));
                        }
                    }
                    catch (EOFException e) {
                        bytesCopied222 = 1;
                    }
                    catch (ZipException e3) {
                        throw new PDFCosParseException(e3);
                    }
                    boolean e3 = bytesCopied222 != 0;
                    Object var9_11 = null;
                    try {
                        if (is == null) return e3;
                        is.close();
                        return e3;
                    }
                    catch (IOException e) {
                        throw new PDFIOException(e);
                    }
                }
                bytesCopied222 = false;
                Object var9_12 = null;
            }
            catch (IOException e) {
                throw new PDFIOException(e);
            }
            try {}
            catch (IOException e) {
                throw new PDFIOException(e);
            }
            if (is == null) return bytesCopied222;
            is.close();
            return bytesCopied222;
        }
        catch (Throwable var8_17) {
            Object var9_13 = null;
            try {}
            catch (IOException e) {
                throw new PDFIOException(e);
            }
            if (is == null) throw var8_17;
            is.close();
            throw var8_17;
        }
    }

    private InputStream getStreamForCopying(boolean encoded, BooleanHolder wasDecrypted) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        InputStream is = null;
        InputByteStream ibs = this.getCachedStream(encoded);
        if (ibs == null) {
            if (this.mDataInPDF) {
                EncryptionHandlerState encryptHandle;
                ibs = this.getStreamDataFromPDF();
                is = ibs.toInputStream();
                boolean hasAcroBug = false;
                if (this.needsDecryption(ibs) && !(hasAcroBug = this.checkForAcroBug())) {
                    encryptHandle = this.getDocument().getEncryption().getStreamDecryptionStateHandler(this, ibs);
                    is = new DecryptingInputStream(this, is, encryptHandle);
                    wasDecrypted.setValue(true);
                }
                if (this.needsDecoding(encoded)) {
                    is = this.buildInputFilterStream(is);
                    if (hasAcroBug) {
                        encryptHandle = this.getDocument().getEncryption().getStreamDecryptionStateHandler(this, ibs);
                        is = new DecryptingInputStream(this, is, encryptHandle);
                        wasDecrypted.setValue(true);
                    }
                }
            } else if (!encoded && (ibs = this.getCachedStream(true)) != null) {
                is = this.buildInputFilterStream(ibs.toInputStream());
            }
        }
        if (is != null) {
            return is;
        }
        if (ibs != null) {
            return ibs.toInputStream();
        }
        return null;
    }

    InputByteStream getStream(boolean encoded, boolean slice, boolean register) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        InputByteStream ibs = this.getCachedStream(encoded);
        if (ibs == null) {
            if (this.mDataInPDF && (this.needsDecryption(ibs = this.getStreamDataFromPDF()) || this.needsDecoding(encoded))) {
                ibs.close();
                ibs = null;
            }
            if (ibs == null) {
                OutputByteStream obs = register ? this.getStreamManager().getOutputByteStreamDecryptedDocument(ByteWriterFactory.Fixed.GROWABLE, this.getLong(ASName.k_Length).longValue()) : this.getStreamManager().getUnregisteredOutputByteStream(ByteWriterFactory.Fixed.GROWABLE, this.getInt(ASName.k_Length).intValue());
                this.copyStream((T)obs, encoded);
                ibs = obs.closeAndConvert();
            } else if (slice) {
                ibs = ibs.slice();
            }
        } else if (slice) {
            ibs = ibs.slice();
        }
        return ibs;
    }

    private InputByteStream getStreamDataFromPDF() throws PDFIOException, PDFCosParseException, PDFSecurityException {
        CosDocument doc = this.getDocument();
        Long length = this.getLong(ASName.k_Length);
        InputByteStream ibs = null;
        if (length == null) {
            if (doc.getUseRepairList()) {
                return this.setRepairedStreamLength(doc, this.mPos);
            }
            throw new PDFCosParseException("CosStream with object number -  " + this.getObjNum() + " does not have a length entry.");
        }
        try {
            boolean isStreamLengthOk;
            ibs = doc.getStream(this.mPos, length);
            if (doc.getUseRepairList() && !(isStreamLengthOk = CosRepairUtils.isStreamEndValid(doc, this.mPos + length))) {
                ibs = this.setRepairedStreamLength(doc, this.mPos);
            }
        }
        catch (PDFIOException e) {
            if (doc.getUseRepairList()) {
                ibs = this.setRepairedStreamLength(doc, this.mPos);
            }
            throw new PDFCosParseException(e);
        }
        return ibs;
    }

    private InputByteStream setRepairedStreamLength(CosDocument doc, long currentPos) throws PDFIOException {
        long actuallength = CosRepairUtils.getStreamLength(this.getDocument(), this.mPos);
        InputByteStream ibs = this.getDocument().getStream(this.mPos, actuallength);
        this.setRepairedValue(ASName.k_Length, this.getDocument().createCosNumeric(actuallength));
        return ibs;
    }

    protected void adjustPos(long offset) {
        this.mPos += offset;
    }

    ASName getCryptFilter() throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        if (this.mCryptFilterSet) {
            return this.mCryptFilter;
        }
        ASName filterName = null;
        CosObject filter = this.get(ASName.k_Filter);
        if (filter != null) {
            CosArray params;
            int cryptInd;
            if (filter.getType() == 3) {
                if (((CosName)filter).nameValue().equals(ASName.k_Crypt)) {
                    CosDictionary params2 = this.getCosDictionary(ASName.k_DecodeParms);
                    filterName = params2 != null ? params2.getName(ASName.k_Name) : ASName.k_Identity;
                }
            } else if (filter.getType() == 5 && (cryptInd = ((CosArray)filter).findName(ASName.k_Crypt)) >= 0 && (params = this.getCosArray(ASName.k_DecodeParms)) != null && (filterName = params.getCosDictionary(cryptInd).getName(ASName.k_Name)) == null) {
                filterName = ASName.k_Identity;
            }
        }
        this.mCryptFilterSet = true;
        this.mCryptFilter = filterName;
        return filterName;
    }

    private boolean checkForAcroBug() throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        CosObject filter = this.get(ASName.k_Filter);
        if (filter instanceof CosArray && ((CosArray)filter).findName(ASName.k_Crypt) > 0) {
            return true;
        }
        return false;
    }

    private ASName getCryptFilter(CosArray filterList) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        ASName filterName = null;
        if (filterList != null) {
            Iterator<CosObject> filterIter = filterList.iterator();
            while (filterIter.hasNext()) {
                CosArray filterItem = (CosArray)filterIter.next();
                if (((CosName)filterItem.get(0)).nameValue() != ASName.k_Crypt) continue;
                CosObject params = filterItem.get(1);
                filterName = params instanceof CosDictionary ? ((CosDictionary)params).getName(ASName.k_Name) : ASName.k_Identity;
                break;
            }
            this.mCryptFilterSet = true;
            this.mCryptFilter = filterName;
        }
        return filterName;
    }

    private boolean needsDecoding(boolean encoded) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        if (encoded) {
            return false;
        }
        CosObject filter = this.get(ASName.k_Filter);
        if (filter instanceof CosArray) {
            if (((CosArray)filter).isEmpty()) {
                return false;
            }
            if (((CosArray)filter).size() > 1) {
                return true;
            }
            filter = ((CosArray)filter).get(0);
        }
        if (filter instanceof CosName && ((CosName)filter).nameValue() != ASName.k_Crypt) {
            return true;
        }
        return false;
    }

    private boolean needsEncoding() throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        if (this.mOutputFilters == null) {
            return this.needsDecoding(false);
        }
        if (this.mOutputFilters.isEmpty()) {
            return false;
        }
        if (this.mOutputFilters.size() == 1 && ((CosName)((CosArray)this.mOutputFilters.get(0)).get(0)).nameValue() == ASName.k_Crypt) {
            return false;
        }
        return true;
    }

    public void newDataDecoded(InputByteStream byteStream) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        this.newData(byteStream, false);
    }

    public void newDataEncoded(InputByteStream byteStream) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        this.newData(byteStream, true);
    }

    private void newData(InputByteStream byteStream, boolean encoded) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            this.mDataInPDF = false;
            this.getInfo().markDirty();
            this.setCachedStream(encoded, byteStream);
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    void setToEncrypt(boolean encrypted) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        if (this.mToEncrypt == encrypted) {
            return;
        }
        this.getInfo().markDirty();
        this.mToEncrypt = encrypted;
    }

    void setIsEncrypted(boolean encrypted) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        if (this.mIsEncrypted == encrypted) {
            return;
        }
        this.getInfo().markDirty();
        this.mIsEncrypted = encrypted;
    }

    public void setOutputFiltersList(CosArray filters) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        this.mOutputFilters = filters;
        this.getInfo().markDirty();
    }

    public CosArray getOutputFiltersList() {
        return this.mOutputFilters;
    }

    public CosArray getInputFiltersList() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            return this.getInputFilters(false);
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    CosArray getInputFilters(boolean removeCryptFilter) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        CosArray filterList = this.getDocument().createCosArray();
        CosArray filter = null;
        CosObject filterObj = this.get(ASName.k_Filter);
        CosObject paramsObj = this.get(ASName.k_DecodeParms);
        if (filterObj instanceof CosName && (!removeCryptFilter || ((CosName)filterObj).nameValue() != ASName.k_Crypt)) {
            filter = this.getDocument().createCosArray();
            filter.add(filterObj);
            filter.add(paramsObj != null ? paramsObj : this.getDocument().createCosNull());
            filterList.add(filter);
        } else if (filterObj instanceof CosArray) {
            CosObject filterName = null;
            CosObject filterParams = null;
            for (int i = 0; i < ((CosArray)filterObj).size(); ++i) {
                filter = this.getDocument().createCosArray();
                filterName = ((CosArray)filterObj).get(i);
                if (removeCryptFilter && filterName instanceof CosName && ((CosName)filterName).nameValue() == ASName.k_Crypt) continue;
                if (paramsObj != null) {
                    filterParams = ((CosArray)paramsObj).get(i);
                }
                filter.add(filterName);
                filter.add(filterParams != null ? filterParams : this.getDocument().createCosNull());
                filterList.add(filter);
            }
        }
        return filterList;
    }

    private boolean filtersChanged(CosArray filterList, boolean ignoreCrypt) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        CosObject oldFilterList = this.get(ASName.k_Filter);
        CosObject oldParamsList = this.get(ASName.k_DecodeParms);
        ASName oldFilter = null;
        ASName newFilter = null;
        int oldIndex = 0;
        int newIndex = 0;
        do {
            oldFilter = this.getFilter(oldFilterList, oldIndex);
            if (ignoreCrypt && oldFilter == ASName.k_Crypt) {
                oldFilter = this.getFilter(oldFilterList, ++oldIndex);
            }
            newFilter = this.getFilter(filterList, newIndex);
            if (ignoreCrypt && newFilter == ASName.k_Crypt) {
                newFilter = this.getFilter(filterList, ++newIndex);
            }
            if (oldFilter == null && newFilter == null) {
                return false;
            }
            if (oldFilter != newFilter) {
                return true;
            }
            if (!this.getParams(oldParamsList, oldIndex).equals(this.getParams(filterList, newIndex))) {
                return true;
            }
            ++oldIndex;
            ++newIndex;
        } while (true);
    }

    private ASName getFilter(CosObject filterObject, int index) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        if (filterObject instanceof CosName) {
            if (index != 0) {
                return null;
            }
            return ((CosName)filterObject).nameValue();
        }
        if (filterObject instanceof CosArray) {
            if (index >= ((CosArray)filterObject).size()) {
                return null;
            }
            CosObject objectAtIndex = ((CosArray)filterObject).get(index);
            if (objectAtIndex instanceof CosName) {
                return ((CosName)objectAtIndex).nameValue();
            }
            if (objectAtIndex instanceof CosArray) {
                return ((CosName)((CosArray)objectAtIndex).get(0)).nameValue();
            }
        }
        return null;
    }

    private CosObject getParams(Object paramsObject, int index) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        if (paramsObject instanceof CosDictionary) {
            if (index != 0) {
                paramsObject = null;
            }
        } else if (paramsObject instanceof CosArray) {
            paramsObject = index >= ((CosArray)paramsObject).size() ? null : ((CosArray)paramsObject).get(index);
        }
        if (!(paramsObject instanceof CosDictionary)) {
            return this.getDocument().createCosNull();
        }
        return (CosDictionary)paramsObject;
    }

    boolean needsEncryption() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        return this.needsDecryptionOrEncryption(null, true);
    }

    boolean needsDecryption(InputByteStream stream) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        return this.needsDecryptionOrEncryption(stream, false);
    }

    private boolean needsDecryptionOrEncryption(InputByteStream stream, boolean encrypting) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        try {
            ASName type;
            boolean needsIt;
            CosEncryption encryptionHandler = this.getDocument().getEncryption();
            boolean encrypted = encrypting ? this.mToEncrypt : this.mIsEncrypted;
            boolean bl = needsIt = encrypting ? encryptionHandler.needsEncryption() : encryptionHandler.needsDecryption();
            if (!encrypted || stream != null && stream.length() == 0) {
                return false;
            }
            CosObject cosType = this.get(ASName.k_Type);
            ASName aSName = type = cosType instanceof CosName ? ((CosName)cosType).nameValue() : null;
            if (!encryptionHandler.getEncryptionState() || !needsIt || ASName.k_XRef.equals(type)) {
                return false;
            }
            int shouldDoIt = this.getDocument().getEncryption().shouldDecryptOrEncrypt(type, this.getCryptFilter(), encrypting);
            if (shouldDoIt != 0) {
                return shouldDoIt > 0;
            }
            if (this.isDirty() || this instanceof CosObjectStream) {
                return true;
            }
            if (this.getCryptFilter() != null || this.getDocument().getLinearization() == null) {
                return false;
            }
            return true;
        }
        catch (IOException e) {
            throw new PDFIOException(e);
        }
    }

    private InputStream buildInputFilterStream(InputStream inputStream) throws PDFCosParseException, IOException, PDFIOException, PDFSecurityException {
        Iterator filterIter2;
        Iterator filterIter2;
        CosObject rawFilter = this.get(ASName.k_Filter);
        FilterParams params = FilterStream.buildFilterParams(this.get(ASName.k_DecodeParms));
        if (rawFilter instanceof CosName) {
            ArrayList<CosObject> filterList = new ArrayList<CosObject>();
            filterList.add(rawFilter);
            filterIter2 = filterList.iterator();
        } else {
            filterIter2 = rawFilter == null ? this.getDocument().createCosArray().iterator() : ((CosArray)rawFilter).iterator();
        }
        while (filterIter2.hasNext()) {
            ASName curFilter = ((CosName)filterIter2.next()).nameValue();
            if (curFilter == ASName.k_Crypt) continue;
            inputStream = FilterStream.applyFilter(inputStream, curFilter, params, this.getCustomFilterRegistry());
        }
        return inputStream;
    }

    private void checkOutputFilters() throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        CosArray filterList = this.mOutputFilters;
        if (this.mOutputFilters == null) {
            filterList = this.getInputFiltersList();
        }
        for (int i = 0; i < filterList.size(); ++i) {
            CosObject cosCols;
            CosDictionary paramsDict;
            CosArray filterAtIndex = (CosArray)filterList.get(i);
            if (!(filterAtIndex.get(0) instanceof CosName) || ((CosName)filterAtIndex.get(0)).nameValue() != ASName.k_CCITTFaxDecode) continue;
            int cols = 1728;
            if (filterAtIndex.get(1) instanceof CosDictionary && (cosCols = (paramsDict = (CosDictionary)filterAtIndex.get(1)).get(ASName.k_Columns)) instanceof CosNumeric) {
                int mode;
                cols = cosCols.intValue();
                CosObject cosMode = paramsDict.get(ASName.k_K);
                CosObject cosRows = paramsDict.get(ASName.k_Rows);
                if (cosMode instanceof CosNumeric && (mode = cosMode.intValue()) == -1 && paramsDict.size() == (cosRows == null ? 2 : 3)) {
                    return;
                }
            }
            CosDictionary newParams = this.getDocument().createDirectCosDictionary();
            newParams.put(ASName.k_Columns, cols);
            newParams.put(ASName.k_K, -1);
            filterAtIndex.add(1, newParams != null ? newParams : this.getDocument().createCosNull());
            this.setOutputFiltersList(filterList);
            return;
        }
    }

    @Override
    public String toString() {
        return this.toString(true);
    }

    public long getStreamDataOffset() {
        long objPos = this.getObjPos();
        if (objPos == 0 || this.mPos == 0) {
            return -1;
        }
        return this.mPos - objPos;
    }

    @Override
    void writeOut(OutputByteStream dstByteStm, boolean inString, boolean inDebugMode) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        this.writeOut(dstByteStm, inString, inDebugMode, false);
    }

    private void setFlateOuputFilter() throws PDFCosParseException, PDFIOException, PDFSecurityException {
        ASName type;
        CosObject cosType = this.get(ASName.k_Type);
        ASName aSName = type = cosType instanceof CosName ? ((CosName)cosType).nameValue() : null;
        if (type != ASName.k_Metadata) {
            this.put(ASName.k_Filter, this.getDocument().createCosName(ASName.k_FlateDecode));
            this.remove(ASName.k_DecodeParms);
        }
    }

    private boolean isFlateFilterObject(CosObject filterObj) throws PDFCosParseException, PDFIOException, PDFSecurityException {
        if (filterObj instanceof CosName && filterObj.nameValue() == ASName.k_FlateDecode) {
            return true;
        }
        if (filterObj instanceof CosArray && ((CosArray)filterObj).size() == 1 && ((CosArray)filterObj).get(0).nameValue() == ASName.k_FlateDecode) {
            return true;
        }
        return false;
    }

    /*
     * Exception decompiling
     */
    void writeOut(OutputByteStream dstByteStm, boolean inString, boolean inDebugMode, boolean saveToCopy) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        // This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
        // org.benf.cfr.reader.util.ConfusedCFRException: Missing node tying up JSR block
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op02WithProcessedDataAndRefs.tieUpRelations(Op02WithProcessedDataAndRefs.java:2811)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op02WithProcessedDataAndRefs.copyBlock(Op02WithProcessedDataAndRefs.java:2800)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op02WithProcessedDataAndRefs.inlineJSR(Op02WithProcessedDataAndRefs.java:2756)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op02WithProcessedDataAndRefs.processJSRs(Op02WithProcessedDataAndRefs.java:2512)
        // org.benf.cfr.reader.bytecode.analysis.opgraph.Op02WithProcessedDataAndRefs.processJSR(Op02WithProcessedDataAndRefs.java:2443)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:368)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:217)
        // org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:162)
        // org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:95)
        // org.benf.cfr.reader.entities.Method.analyse(Method.java:355)
        // org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:768)
        // org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:700)
        // org.benf.cfr.reader.Main.doJar(Main.java:134)
        // org.benf.cfr.reader.Main.main(Main.java:189)
        throw new IllegalStateException("Decompilation failed");
    }

    private void setInputFilters(CosArray filterList) throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
        if (filterList != null && this.filtersChanged(filterList, false)) {
            CosObject outputFilter = null;
            CosArray outputParams = null;
            if (filterList.size() == 1) {
                CosArray filter = (CosArray)filterList.get(0);
                outputFilter = filter.get(0);
                outputParams = FilterStream.updateCustomFilterParams(((CosName)outputFilter).nameValue(), filter.get(1), this.getCustomFilterRegistry());
            } else if (filterList.size() != 0) {
                boolean outputParamsNull = true;
                outputFilter = this.getDocument().createCosArray();
                outputParams = this.getDocument().createCosArray();
                Iterator<CosObject> iter = filterList.iterator();
                while (iter.hasNext()) {
                    CosArray listItem = (CosArray)iter.next();
                    CosObject filter = listItem.get(0);
                    CosObject params = listItem.get(1);
                    ((CosArray)outputFilter).add(filter);
                    params = FilterStream.updateCustomFilterParams(((CosName)filter).nameValue(), params, this.getCustomFilterRegistry());
                    if (params == null) {
                        outputParams.add(this.getDocument().createCosNull());
                        continue;
                    }
                    outputParams.add(params);
                    outputParamsNull = false;
                }
                if (outputParamsNull) {
                    outputParams = null;
                }
            }
            super.remove(ASName.k_Filter);
            if (outputFilter != null) {
                super.put(ASName.k_Filter, outputFilter);
            }
            super.remove(ASName.k_DecodeParms);
            if (outputParams != null && !(outputParams instanceof CosNull)) {
                super.put(ASName.k_DecodeParms, outputParams);
            }
        }
    }

    private void trimFilterLists(CosArray inFilters, CosArray outFilters, CosArray inFiltersTrimmed, CosArray outFiltersTrimmed, boolean hasAcroBug) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
        CosArray filterItem;
        Iterator<CosObject> filterIter = inFilters.iterator();
        while (filterIter.hasNext()) {
            filterItem = (CosArray)filterIter.next();
            if (((CosName)filterItem.get(0)).nameValue() == ASName.k_Crypt) continue;
            inFiltersTrimmed.add(filterItem);
        }
        filterIter = outFilters.iterator();
        while (filterIter.hasNext()) {
            filterItem = (CosArray)filterIter.next();
            if (((CosName)filterItem.get(0)).nameValue() == ASName.k_Crypt) continue;
            outFiltersTrimmed.add(filterItem);
        }
        if (hasAcroBug) {
            return;
        }
        while (!inFiltersTrimmed.isEmpty() && !outFiltersTrimmed.isEmpty()) {
            CosArray inFilterItemLast = (CosArray)inFiltersTrimmed.get(inFiltersTrimmed.size() - 1);
            CosArray outFilterItemLast = (CosArray)outFiltersTrimmed.get(outFiltersTrimmed.size() - 1);
            if (((CosName)inFilterItemLast.get(0)).nameValue() != ((CosName)outFilterItemLast.get(0)).nameValue() || !inFilterItemLast.get(1).equals(outFilterItemLast.get(1))) break;
            inFiltersTrimmed.remove(inFiltersTrimmed.size() - 1);
            outFiltersTrimmed.remove(outFiltersTrimmed.size() - 1);
        }
    }

    private long writeStreamDict(OutputByteStream outStream, boolean inString, boolean inDebugMode) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
        long lengthPosition = 0;
        outStream.write(60);
        outStream.write(60);
        for (Map.Entry entry : this.mData.entrySet()) {
            ASName key = (ASName)entry.getKey();
            key.write(outStream);
            CosObject value = (CosObject)entry.getValue();
            if (value.isIndirect() || value instanceof CosBoolean || value instanceof CosNumeric || value instanceof CosNull) {
                outStream.write(32);
            }
            if (key == ASName.k_Length) {
                lengthPosition = outStream.getPosition();
            }
            value.writeOut(outStream, inString, inDebugMode);
        }
        outStream.write(62);
        outStream.write(62);
        return lengthPosition;
    }

    private CustomFilterRegistry getCustomFilterRegistry() {
        return this.getDocument().getOptions().getCustomFilterRegistry();
    }

    @Override
    public boolean equals(CosObject value) {
        HashMap<Integer, HashSet<Integer>> alreadyComparedCosObjectPairsList = new HashMap<Integer, HashSet<Integer>>();
        return this.safeEquals(value, alreadyComparedCosObjectPairsList);
    }

    @Override
    boolean safeEquals(CosObject value, HashMap<Integer, HashSet<Integer>> alreadyComparedCosObjectPairsList) {
        if (!(value instanceof CosStream) || value.getDocument() != this.getDocument()) {
            return false;
        }
        if (value == this) {
            return true;
        }
        if (this.cosObjectPairAlreadyInList(new Integer[]{this.getObjNum(), value.getObjNum()}, alreadyComparedCosObjectPairsList) == 0) {
            return true;
        }
        CosStream valueStream = (CosStream)value;
        if (!super.safeEquals(valueStream, alreadyComparedCosObjectPairsList)) {
            return false;
        }
        try {
            return Arrays.equals(CosUtils.digestStream(valueStream.getStreamDecoded(), "SHA-1"), CosUtils.digestStream(this.getStreamDecoded(), "SHA-1"));
        }
        catch (Exception e) {
            throw new RuntimeException("problem occured while equating " + valueStream.getObjNum() + " with " + this.getObjNum(), e);
        }
    }

    public boolean isEncoded() throws PDFCosParseException, PDFIOException, PDFSecurityException, IOException {
        if (this.mDataInPDF) {
            return this.needsDecoding(false);
        }
        return this.mDataEncoded;
    }
}