Search in sources :

Example 91 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class StreamValidationProcess method checkDictionaryEntries.

/**
 * Check dictionary entries. Only the Length entry is mandatory. In a PDF/A file, F, FFilter and FDecodeParms are
 * forbidden
 *
 * @param context the preflight context.
 * @param streamObj the stream to check.
 */
protected void checkDictionaryEntries(PreflightContext context, COSStream streamObj) {
    boolean len = streamObj.containsKey(COSName.LENGTH);
    boolean f = streamObj.containsKey(COSName.F);
    boolean ffilter = streamObj.containsKey(COSName.F_FILTER);
    boolean fdecParams = streamObj.containsKey(COSName.F_DECODE_PARMS);
    if (!len) {
        addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_LENGTH_MISSING, "Stream length is missing"));
    }
    if (f || ffilter || fdecParams) {
        addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_FX_KEYS, "F, FFilter or FDecodeParms keys are present in the stream dictionary"));
    }
}
Also used : ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 92 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class StreamValidationProcess method checkStreamLength.

protected void checkStreamLength(PreflightContext context, COSObject cObj) throws ValidationException {
    COSStream streamObj = (COSStream) cObj.getObject();
    int length = streamObj.getInt(COSName.LENGTH);
    InputStream ra = null;
    try {
        ra = context.getSource().getInputStream();
        Long offset = context.getDocument().getDocument().getXrefTable().get(new COSObjectKey(cObj));
        // ---- go to the beginning of the object
        long skipped = 0;
        if (offset != null) {
            while (skipped != offset) {
                long curSkip = ra.skip(offset - skipped);
                if (curSkip < 0) {
                    addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_DAMAGED, "Unable to skip bytes in the PDFFile to check stream length"));
                    return;
                }
                skipped += curSkip;
            }
            // ---- go to the stream key word
            if (readUntilStream(ra)) {
                int c = ra.read();
                if (c == '\r') {
                    ra.read();
                }
                // else c is '\n' no more character to read
                // ---- Here is the true beginning of the Stream Content.
                // ---- Read the given length of bytes and check the 10 next bytes
                // ---- to see if there are endstream.
                byte[] buffer = new byte[1024];
                int nbBytesToRead = length;
                do {
                    int cr;
                    if (nbBytesToRead > buffer.length) {
                        cr = ra.read(buffer);
                    } else {
                        cr = ra.read(buffer, 0, nbBytesToRead);
                    }
                    if (cr == -1) {
                        addStreamLengthValidationError(context, cObj, length, "");
                        return;
                    } else {
                        nbBytesToRead -= cr;
                    }
                } while (nbBytesToRead > 0);
                int len = "endstream".length() + 2;
                byte[] buffer2 = new byte[len];
                for (int i = 0; i < len; ++i) {
                    buffer2[i] = (byte) ra.read();
                }
                // ---- check the content of 10 last characters
                String endStream = new String(buffer2, Charsets.ISO_8859_1);
                if (buffer2[0] == '\r' && buffer2[1] == '\n') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else if (buffer2[0] == '\r' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else if (buffer2[0] == '\n' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else {
                    if (!endStream.startsWith("endStream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                }
            } else {
                addStreamLengthValidationError(context, cObj, length, "");
            }
        }
    } catch (IOException e) {
        throw new ValidationException("Unable to read a stream to validate: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(ra);
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) InputStream(java.io.InputStream) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException)

Example 93 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class TrailerValidationProcess method checkLinearizedDictionnary.

/**
 * Check if mandatory keys of linearized dictionary are present.
 *
 * @param ctx the preflight context.
 * @param linearizedDict the linearization dictionary.
 */
protected void checkLinearizedDictionnary(PreflightContext ctx, COSDictionary linearizedDict) {
    // ---- check if all keys are authorized in a linearized dictionary
    // ---- Linearized dictionary must contain the lhoent keys
    boolean l = false;
    boolean h = false;
    boolean o = false;
    boolean e = false;
    boolean n = false;
    boolean t = false;
    for (Object key : linearizedDict.keySet()) {
        if (!(key instanceof COSName)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_DICTIONARY_KEY_INVALID, "Invalid key in The Linearized dictionary"));
            return;
        }
        String cosName = ((COSName) key).getName();
        if (cosName.equals(DICTIONARY_KEY_LINEARIZED_L)) {
            l = true;
        }
        if (cosName.equals(DICTIONARY_KEY_LINEARIZED_H)) {
            h = true;
        }
        if (cosName.equals(DICTIONARY_KEY_LINEARIZED_O)) {
            o = true;
        }
        if (cosName.equals(DICTIONARY_KEY_LINEARIZED_E)) {
            e = true;
        }
        if (cosName.equals(DICTIONARY_KEY_LINEARIZED_N)) {
            n = true;
        }
        if (cosName.equals(DICTIONARY_KEY_LINEARIZED_T)) {
            t = true;
        }
    }
    if (!(l && h && o && e && t && n)) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_DICT_INVALID, "Invalid key in The Linearized dictionary"));
    }
}
Also used : COSName(org.apache.pdfbox.cos.COSName) COSObject(org.apache.pdfbox.cos.COSObject) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) COSString(org.apache.pdfbox.cos.COSString)

Example 94 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class TrailerValidationProcess method checkTrailersForLinearizedPDF14.

/**
 * Extracts and compares first and last trailers for PDF version between 1.1 and 1.4.
 *
 * @param ctx the preflight context.
 */
protected void checkTrailersForLinearizedPDF14(PreflightContext ctx) {
    COSDictionary first = ctx.getXrefTrailerResolver().getFirstTrailer();
    if (first == null) {
        addValidationError(ctx, new ValidationError(ERROR_SYNTAX_TRAILER, "There are no trailer in the PDF file"));
    } else {
        COSDictionary last = ctx.getXrefTrailerResolver().getLastTrailer();
        COSDocument cosDoc = new COSDocument();
        checkMainTrailer(ctx, first);
        if (!compareIds(first, last, cosDoc)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_ID_CONSISTENCY, "ID is different in the first and the last trailer"));
        }
        COSUtils.closeDocumentQuietly(cosDoc);
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError)

Example 95 with ValidationError

use of org.apache.pdfbox.preflight.ValidationResult.ValidationError in project pdfbox by apache.

the class TrailerValidationProcess method checkMainTrailer.

/**
 * check if all keys are authorized in a trailer dictionary and if the type is valid.
 *
 * @param ctx the preflight context.
 * @param trailer the trailer dictionary.
 */
protected void checkMainTrailer(PreflightContext ctx, COSDictionary trailer) {
    boolean id = false;
    boolean root = false;
    boolean size = false;
    boolean prev = false;
    boolean info = false;
    boolean encrypt = false;
    for (Object key : trailer.keySet()) {
        if (!(key instanceof COSName)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_DICTIONARY_KEY_INVALID, "Invalid key in The trailer dictionary"));
            return;
        }
        String cosName = ((COSName) key).getName();
        if (cosName.equals(TRAILER_DICTIONARY_KEY_ENCRYPT)) {
            encrypt = true;
        }
        if (cosName.equals(TRAILER_DICTIONARY_KEY_SIZE)) {
            size = true;
        }
        if (cosName.equals(TRAILER_DICTIONARY_KEY_PREV)) {
            prev = true;
        }
        if (cosName.equals(TRAILER_DICTIONARY_KEY_ROOT)) {
            root = true;
        }
        if (cosName.equals(TRAILER_DICTIONARY_KEY_INFO)) {
            info = true;
        }
        if (cosName.equals(TRAILER_DICTIONARY_KEY_ID)) {
            id = true;
        }
    }
    COSDocument cosDocument = ctx.getDocument().getDocument();
    // PDF/A Trailer dictionary must contain the ID key
    if (!id) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_MISSING_ID, "The trailer dictionary doesn't contain ID"));
    } else {
        COSBase trailerId = trailer.getItem(TRAILER_DICTIONARY_KEY_ID);
        if (!COSUtils.isArray(trailerId, cosDocument)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains an id but it isn't an array"));
        }
    }
    // PDF/A Trailer dictionary mustn't contain the Encrypt key
    if (encrypt) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_ENCRYPT, "The trailer dictionary contains Encrypt"));
    }
    // PDF Trailer dictionary must contain the Size key
    if (!size) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_MISSING_SIZE, "The trailer dictionary doesn't contain Size"));
    } else {
        COSBase trailerSize = trailer.getItem(TRAILER_DICTIONARY_KEY_SIZE);
        if (!COSUtils.isInteger(trailerSize, cosDocument)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains a size but it isn't an integer"));
        }
    }
    // PDF Trailer dictionary must contain the Root key
    if (!root) {
        addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_MISSING_ROOT, "The trailer dictionary doesn't contain Root"));
    } else {
        COSBase trailerRoot = trailer.getItem(TRAILER_DICTIONARY_KEY_ROOT);
        if (!COSUtils.isDictionary(trailerRoot, cosDocument)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains a root but it isn't a dictionary"));
        }
    }
    // PDF Trailer dictionary may contain the Prev key
    if (prev) {
        COSBase trailerPrev = trailer.getItem(TRAILER_DICTIONARY_KEY_PREV);
        if (!COSUtils.isInteger(trailerPrev, cosDocument)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains a prev but it isn't an integer"));
        }
    }
    // PDF Trailer dictionary may contain the Info key
    if (info) {
        COSBase trailerInfo = trailer.getItem(TRAILER_DICTIONARY_KEY_INFO);
        if (!COSUtils.isDictionary(trailerInfo, cosDocument)) {
            addValidationError(ctx, new ValidationError(PreflightConstants.ERROR_SYNTAX_TRAILER_TYPE_INVALID, "The trailer dictionary contains an info but it isn't a dictionary"));
        }
    }
}
Also used : COSName(org.apache.pdfbox.cos.COSName) COSDocument(org.apache.pdfbox.cos.COSDocument) COSBase(org.apache.pdfbox.cos.COSBase) COSObject(org.apache.pdfbox.cos.COSObject) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) COSString(org.apache.pdfbox.cos.COSString)

Aggregations

ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)108 COSBase (org.apache.pdfbox.cos.COSBase)36 COSDictionary (org.apache.pdfbox.cos.COSDictionary)28 IOException (java.io.IOException)27 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSObject (org.apache.pdfbox.cos.COSObject)13 COSArray (org.apache.pdfbox.cos.COSArray)10 COSStream (org.apache.pdfbox.cos.COSStream)10 COSString (org.apache.pdfbox.cos.COSString)8 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)8 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)8 COSName (org.apache.pdfbox.cos.COSName)7 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)7 PreflightPath (org.apache.pdfbox.preflight.PreflightPath)6 InputStream (java.io.InputStream)5 ArrayList (java.util.ArrayList)5 PDStream (org.apache.pdfbox.pdmodel.common.PDStream)5 PreflightParser (org.apache.pdfbox.preflight.parser.PreflightParser)5 DublinCoreSchema (org.apache.xmpbox.schema.DublinCoreSchema)5 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)4