Search in sources :

Example 6 with COSObjectKey

use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.

the class COSParser method validateXrefOffsets.

private boolean validateXrefOffsets(Map<COSObjectKey, Long> xrefOffset) throws IOException {
    if (xrefOffset == null) {
        return true;
    }
    for (Entry<COSObjectKey, Long> objectEntry : xrefOffset.entrySet()) {
        COSObjectKey objectKey = objectEntry.getKey();
        Long objectOffset = objectEntry.getValue();
        // see type 2 entry in xref stream
        if (objectOffset != null && objectOffset >= 0 && !checkObjectKey(objectKey, objectOffset)) {
            Log.d("PdfBox-Android", "Stop checking xref offsets as at least one (" + objectKey + ") couldn't be dereferenced");
            return false;
        }
    }
    return true;
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey)

Example 7 with COSObjectKey

use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.

the class COSParser method bfSearchForObjects.

/**
 * Brute force search for every object in the pdf.
 *
 * @throws IOException if something went wrong
 */
private void bfSearchForObjects() throws IOException {
    if (bfSearchCOSObjectKeyOffsets == null) {
        bfSearchForLastEOFMarker();
        bfSearchCOSObjectKeyOffsets = new HashMap<COSObjectKey, Long>();
        long originOffset = source.getPosition();
        long currentOffset = MINIMUM_SEARCH_OFFSET;
        long lastObjectId = Long.MIN_VALUE;
        int lastGenID = Integer.MIN_VALUE;
        long lastObjOffset = Long.MIN_VALUE;
        char[] endobjString = "ndo".toCharArray();
        char[] endobjRemainingString = "bj".toCharArray();
        boolean endOfObjFound = false;
        do {
            source.seek(currentOffset);
            int nextChar = source.read();
            currentOffset++;
            if (isWhitespace(nextChar) && isString(OBJ_MARKER)) {
                long tempOffset = currentOffset - 2;
                source.seek(tempOffset);
                int genID = source.peek();
                // is the next char a digit?
                if (isDigit(genID)) {
                    genID -= 48;
                    tempOffset--;
                    source.seek(tempOffset);
                    if (isWhitespace()) {
                        while (tempOffset > MINIMUM_SEARCH_OFFSET && isWhitespace()) {
                            source.seek(--tempOffset);
                        }
                        boolean objectIDFound = false;
                        while (tempOffset > MINIMUM_SEARCH_OFFSET && isDigit()) {
                            source.seek(--tempOffset);
                            objectIDFound = true;
                        }
                        if (objectIDFound) {
                            source.read();
                            long objectId = readObjectNumber();
                            if (lastObjOffset > 0) {
                                // add the former object ID only if there was a subsequent object ID
                                bfSearchCOSObjectKeyOffsets.put(new COSObjectKey(lastObjectId, lastGenID), lastObjOffset);
                            }
                            lastObjectId = objectId;
                            lastGenID = genID;
                            lastObjOffset = tempOffset + 1;
                            currentOffset += OBJ_MARKER.length - 1;
                            endOfObjFound = false;
                        }
                    }
                }
            } else // We could possibly implement a more intelligent algorithm if necessary
            if (nextChar == 'e' && isString(endobjString)) {
                currentOffset += endobjString.length;
                source.seek(currentOffset);
                if (source.isEOF()) {
                    endOfObjFound = true;
                    continue;
                }
                if (isString(endobjRemainingString)) {
                    currentOffset += endobjRemainingString.length;
                    endOfObjFound = true;
                    continue;
                }
            }
        } while (currentOffset < lastEOFMarker && !source.isEOF());
        if ((lastEOFMarker < Long.MAX_VALUE || endOfObjFound) && lastObjOffset > 0) {
            // if the pdf wasn't cut off in the middle or if the last object ends with a "endobj" marker
            // the last object id has to be added here so that it can't get lost as there isn't any subsequent
            // object id
            bfSearchCOSObjectKeyOffsets.put(new COSObjectKey(lastObjectId, lastGenID), lastObjOffset);
        }
        // reestablish origin position
        source.seek(originOffset);
    }
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey)

Example 8 with COSObjectKey

use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.

the class COSParser method retrieveCOSDictionary.

private COSDictionary retrieveCOSDictionary(COSObject object) throws IOException {
    COSObjectKey key = new COSObjectKey(object);
    Long offset = bfSearchCOSObjectKeyOffsets.get(key);
    if (offset != null) {
        return retrieveCOSDictionary(key, offset);
    }
    return null;
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey)

Example 9 with COSObjectKey

use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.

the class BaseParser method parseCOSDictionaryValue.

/**
 * This will parse a PDF dictionary value.
 *
 * @return The parsed Dictionary object.
 *
 * @throws IOException If there is an error parsing the dictionary object.
 */
private COSBase parseCOSDictionaryValue() throws IOException {
    long numOffset = seqSource.getPosition();
    COSBase value = parseDirObject();
    skipSpaces();
    // proceed if the given object is a number and the following is a number as well
    if ((!(value instanceof COSNumber) || !isDigit())) {
        return value;
    }
    // read the remaining information of the object number
    long genOffset = seqSource.getPosition();
    COSBase generationNumber = parseDirObject();
    skipSpaces();
    readExpectedChar('R');
    if (!(value instanceof COSInteger)) {
        Log.e("PdfBox-Android", "expected number, actual=" + value + " at offset " + numOffset);
        return COSNull.NULL;
    }
    if (!(generationNumber instanceof COSInteger)) {
        Log.e("PdfBox-Android", "expected number, actual=" + value + " at offset " + genOffset);
        return COSNull.NULL;
    }
    COSObjectKey key = new COSObjectKey(((COSInteger) value).longValue(), ((COSInteger) generationNumber).intValue());
    // dereference the object
    return getObjectFromPool(key);
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey) COSInteger(com.tom_roush.pdfbox.cos.COSInteger) COSNumber(com.tom_roush.pdfbox.cos.COSNumber) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Example 10 with COSObjectKey

use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.

the class COSWriter method getObjectKey.

/**
 * This will get the object key for the object.
 *
 * @param obj The object to get the key for.
 *
 * @return The object key for the object.
 */
private COSObjectKey getObjectKey(COSBase obj) {
    COSBase actual = obj;
    if (actual instanceof COSObject) {
        actual = ((COSObject) obj).getObject();
    }
    // PDFBOX-4540: because objectKeys is accessible from outside, it is possible
    // that a COSObject obj is already in the objectKeys map.
    COSObjectKey key = objectKeys.get(obj);
    if (key == null && actual != null) {
        key = objectKeys.get(actual);
    }
    if (key == null) {
        setNumber(getNumber() + 1);
        key = new COSObjectKey(getNumber(), 0);
        objectKeys.put(obj, key);
        if (actual != null) {
            objectKeys.put(actual, key);
        }
    }
    return key;
}
Also used : COSObjectKey(com.tom_roush.pdfbox.cos.COSObjectKey) COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase)

Aggregations

COSObjectKey (com.tom_roush.pdfbox.cos.COSObjectKey)17 COSBase (com.tom_roush.pdfbox.cos.COSBase)7 COSObject (com.tom_roush.pdfbox.cos.COSObject)6 IOException (java.io.IOException)6 COSDictionary (com.tom_roush.pdfbox.cos.COSDictionary)4 COSArray (com.tom_roush.pdfbox.cos.COSArray)2 COSInteger (com.tom_roush.pdfbox.cos.COSInteger)2 COSNumber (com.tom_roush.pdfbox.cos.COSNumber)2 COSStream (com.tom_roush.pdfbox.cos.COSStream)2 COSDocument (com.tom_roush.pdfbox.cos.COSDocument)1 COSInputStream (com.tom_roush.pdfbox.cos.COSInputStream)1 COSString (com.tom_roush.pdfbox.cos.COSString)1 COSUpdateInfo (com.tom_roush.pdfbox.cos.COSUpdateInfo)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1