CosObjectInfo.java 13 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.internal.io.stream.IO
 *  com.adobe.internal.io.stream.OutputByteStream
 */
package com.adobe.internal.pdftoolkit.core.cos;

import com.adobe.internal.io.stream.IO;
import com.adobe.internal.io.stream.OutputByteStream;
import com.adobe.internal.pdftoolkit.core.cos.CosArray;
import com.adobe.internal.pdftoolkit.core.cos.CosContainer;
import com.adobe.internal.pdftoolkit.core.cos.CosDictionary;
import com.adobe.internal.pdftoolkit.core.cos.CosDocument;
import com.adobe.internal.pdftoolkit.core.cos.CosObject;
import com.adobe.internal.pdftoolkit.core.cos.CosObjectRef;
import com.adobe.internal.pdftoolkit.core.cos.CosOpenOptions;
import com.adobe.internal.pdftoolkit.core.cos.CosStream;
import com.adobe.internal.pdftoolkit.core.cos.CosString;
import com.adobe.internal.pdftoolkit.core.cos.XRefTable;
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.types.ASName;
import com.adobe.internal.pdftoolkit.core.util.StringOps;
import java.io.IOException;
import java.lang.ref.SoftReference;
import java.util.List;

public final class CosObjectInfo {
    private static final int isFree = 0;
    private static final int isAssigned = 1;
    private static final int isAddressed = 2;
    private static final int isLoaded = 3;
    private static final int isDirty = 4;
    private int mState;
    private int mObjNum;
    private int mObjGen;
    private long mPos;
    private CosDocument mDoc;
    private Object mObj;
    private CosObjectRef mRef;
    private CosObjectInfo mObjStmInfo;
    private int mStreamNdx;
    private boolean mIsObjStm;
    private boolean mWriteCompressed;
    private long mNextObjPos = Long.MAX_VALUE;

    CosObjectInfo(CosDocument doc, int objNum, int objGen) {
        this.mDoc = doc;
        this.mState = 1;
        this.mObjNum = objNum;
        this.mObjGen = objGen;
        this.mPos = 0;
    }

    CosObjectInfo() {
        this.mState = 0;
        this.mObjNum = 0;
        this.mObjGen = 65535;
        this.mPos = 0;
    }

    public int getObjNum() {
        return this.mObjNum;
    }

    void setObjNum(int objNum) {
        this.mObjNum = objNum;
    }

    public int getObjGen() {
        return this.mObjGen;
    }

    void setIsObjStm(boolean isObjStm) {
        this.mIsObjStm = isObjStm;
    }

    boolean isObjStm() {
        return this.mIsObjStm;
    }

    void setWriteCompressed(boolean writeCompressed) {
        this.mWriteCompressed = writeCompressed;
    }

    boolean isWriteCompressed() {
        return this.mWriteCompressed;
    }

    void setObjGen(int objGen) {
        this.mObjGen = objGen;
    }

    boolean isCompressed() {
        return this.mObjStmInfo != null;
    }

    public CosObject getObject() throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
        return this.getObject(true);
    }

    public CosObject getObject(boolean loadIfRequired) throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
        CosObject obj;
        CosObject cosObject = obj = this.mDoc != null ? this.mDoc.getRepairedValue(this.getObjNum()) : null;
        if (obj != null) {
            return obj;
        }
        if (this.mObj == null || this.mObj instanceof CosObject) {
            return (CosObject)this.mObj;
        }
        obj = (CosObject)((SoftReference)this.mObj).get();
        if (loadIfRequired && obj == null) {
            this.mState = 2;
            obj = this.mDoc.getXRef().getIndirectObject(this);
            this.mObj = new SoftReference<CosObject>(obj);
        }
        return obj;
    }

    void setObject(CosObject obj) {
        this.mDoc = obj.getDocument();
        this.mObj = this.mDoc.isCacheEnabled() ? new SoftReference<CosObject>((CosObject)obj) : obj;
    }

    void clearObject() {
        this.mObj = null;
    }

    public long getPos() {
        return this.mPos;
    }

    void setPosInternal(long pos) {
        this.mPos = pos;
    }

    void setPos(long pos) {
        if (this.mPos != pos) {
            if (this.mPos != 0) {
                Object obj = this.mObj;
                if (obj instanceof SoftReference) {
                    obj = ((SoftReference)this.mObj).get();
                }
                if (obj instanceof CosStream) {
                    ((CosStream)obj).adjustPos(pos - this.mPos);
                }
            }
            this.mPos = pos;
        }
    }

    void setNextObjPos(long pos) {
        this.mNextObjPos = pos;
    }

    long getNextObjPos() {
        return this.mNextObjPos;
    }

    public int getStreamNdx() {
        return this.mStreamNdx;
    }

    void setStreamNdx(int ndx) {
        this.mStreamNdx = ndx;
    }

    public CosObjectInfo getStreamInfo() {
        return this.mObjStmInfo;
    }

    void setStreamInfo(CosObjectInfo stm) {
        this.mObjStmInfo = stm;
        this.mWriteCompressed = false;
    }

    public CosObjectRef getRef() {
        return this.mRef;
    }

    void setRef(CosObjectRef ref) {
        this.mRef = ref;
    }

    boolean isFree() {
        return this.mState == 0;
    }

    void markFree() {
        this.mState = 0;
        this.mObj = null;
    }

    boolean isAssigned() {
        return this.mState == 1;
    }

    void markAssigned() {
        this.mState = 1;
    }

    boolean isAddressed() {
        return this.mState == 2;
    }

    void markAddressed() {
        this.mState = 2;
    }

    boolean isLoaded() {
        return this.mState == 3;
    }

    void markLoaded() {
        this.mState = 3;
    }

    boolean isDirty() {
        return this.mState == 4;
    }

    void markDirty() throws PDFCosParseException, IOException, PDFSecurityException, PDFIOException {
        this.mState = 4;
        if (this.mDoc != null) {
            this.mDoc.markDirty();
        }
        if (this.mObj == null || this.mObj instanceof SoftReference) {
            this.mObj = this.getObject();
        }
    }

    public int getState() {
        return this.mState;
    }

    void setState(int state) {
        this.mState = state;
    }

    boolean flushCachedObject() {
        if (this.isDirty()) {
            return false;
        }
        if (this.isLoaded()) {
            this.markAddressed();
            this.mObj = null;
        }
        return true;
    }

    boolean markNotDirty() {
        if (!this.isDirty()) {
            return false;
        }
        this.markLoaded();
        if (this.mObj instanceof CosObject) {
            this.mObj = new SoftReference<Object>(this.mObj);
        }
        return true;
    }

    long writeIndirectObject(OutputByteStream obs, long pos) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        return this.writeIndirectObject(obs, pos, false);
    }

    long writeIndirectObject(OutputByteStream obs, long pos, boolean saveToCopy) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        if (this.isFree()) {
            return pos;
        }
        obs.write(StringOps.toByteArray(Integer.toString(this.mObjNum)));
        obs.write(32);
        obs.write(StringOps.toByteArray(Integer.toString(this.mObjGen)));
        obs.write(StringOps.toByteArray(" obj\n"));
        CosObject obj = this.getObject();
        this.loadStrings(obj, null, null);
        this.mPos = pos;
        this.mObjStmInfo = null;
        this.mWriteCompressed = false;
        this.mStreamNdx = 0;
        this.mNextObjPos = Long.MAX_VALUE;
        if (obj instanceof CosStream) {
            ((CosStream)obj).writeOut(obs, false, false, saveToCopy);
        } else {
            obj.writeOut(obs);
        }
        obs.write(StringOps.toByteArray("\nendobj\n"));
        return obs.getPosition();
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    void loadStrings(CosObject obj, CosObject parent, ASName key2) throws PDFCosParseException, PDFIOException, IOException, PDFSecurityException {
        if (obj instanceof CosString) {
            if (parent != null && ((CosString)obj).getIsEncrypted() && this.mDoc.getOptions().skipCorruptObjects()) {
                try {
                    ((CosString)obj).byteArrayValue();
                    return;
                }
                catch (Exception e) {
                    CosString repairedString = new CosString(this.mDoc, new byte[0], 0, 0, false, null);
                    repairedString.setParentObj((CosDictionary)parent);
                    ((CosDictionary)parent).setRepairedValue(key2, repairedString);
                }
                return;
            } else {
                ((CosString)obj).byteArrayValue();
            }
            return;
        } else {
            if (obj instanceof CosArray) {
                int i = 0;
                while (i < ((CosArray)obj).size()) {
                    CosObject child;
                    if (((CosArray)obj).getRef(i) == null && !(child = ((CosArray)obj).get(i)).isIndirect()) {
                        this.loadStrings(child, null, null);
                    }
                    ++i;
                }
                return;
            }
            if (!(obj instanceof CosDictionary)) return;
            List<ASName> keys = ((CosDictionary)obj).getKeys();
            for (ASName key2 : keys) {
                CosObject child;
                if (((CosDictionary)obj).getRef(key2) != null || (child = ((CosDictionary)obj).get(key2)).isIndirect()) continue;
                this.loadStrings(child, obj, key2);
            }
        }
    }

    void writeXRefTableEntry(OutputByteStream obs) throws IOException {
        if (this.isFree()) {
            this.writeInt(obs, 0, 10);
            obs.write(32);
            this.writeInt(obs, 65535, 5);
            obs.write(StringOps.toByteArray(" f\r\n"));
        } else {
            this.writeInt(obs, this.mPos, 10);
            obs.write(32);
            this.writeInt(obs, this.mObjGen, 5);
            obs.write(StringOps.toByteArray(" n\r\n"));
        }
    }

    void writeInt(OutputByteStream obs, long num, int size) throws IOException {
        int i;
        int maxDig = 10;
        if (size > maxDig) {
            size = maxDig;
        }
        char[] digits = new char[maxDig];
        int work = 1000000000;
        for (i = 0; i < maxDig - 1; ++i) {
            digits[i] = (char)(48 + num / (long)work);
            num %= (long)work;
            work /= 10;
        }
        digits[i] = (char)(48 + num);
        for (i = maxDig - size; i < maxDig; ++i) {
            obs.write((int)digits[i]);
        }
    }

    void writeXRefStreamEntry(OutputByteStream buf, int[] fieldsizes) throws PDFCosParseException, IOException, PDFSecurityException {
        if (this.isCompressed()) {
            buf.write(IO.longToByteArray((long)2, (int)fieldsizes[0]));
            buf.write(IO.longToByteArray((long)this.mObjStmInfo.getObjNum(), (int)fieldsizes[1]));
            buf.write(IO.longToByteArray((long)this.mStreamNdx, (int)fieldsizes[2]));
        } else if (this.isFree()) {
            buf.write(IO.longToByteArray((long)0, (int)fieldsizes[0]));
            buf.write(IO.longToByteArray((long)0, (int)fieldsizes[1]));
            buf.write(IO.longToByteArray((long)65535, (int)fieldsizes[2]));
        } else {
            buf.write(IO.longToByteArray((long)1, (int)fieldsizes[0]));
            buf.write(IO.longToByteArray((long)this.mPos, (int)fieldsizes[1]));
            buf.write(IO.longToByteArray((long)this.mObjGen, (int)fieldsizes[2]));
        }
    }

    public void copyValuesFrom(CosObjectInfo template) {
        this.mState = template.mState;
        this.mObjNum = template.mObjNum;
        this.mObjGen = template.mObjGen;
        this.mPos = template.mPos;
        this.mDoc = template.mDoc;
        this.mObj = template.mObj;
        this.mRef = template.mRef;
        this.mObjStmInfo = template.mObjStmInfo;
        this.mStreamNdx = template.mStreamNdx;
        this.mIsObjStm = template.mIsObjStm;
        this.mWriteCompressed = template.mWriteCompressed;
        this.mNextObjPos = template.mNextObjPos;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("[");
        builder.append(this.mObjNum);
        builder.append(":");
        builder.append(this.mObjGen);
        builder.append("] = ");
        switch (this.mState) {
            case 2: {
                builder.append("addressed");
                break;
            }
            case 1: {
                builder.append("assigned");
                break;
            }
            case 4: {
                builder.append("dirty");
                break;
            }
            case 0: {
                builder.append("free");
                break;
            }
            case 3: {
                builder.append("loaded");
            }
        }
        builder.append(" at 0x");
        builder.append(Long.toHexString(this.mPos));
        return builder.toString();
    }
}