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"));
}
}
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);
}
}
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"));
}
}
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);
}
}
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"));
}
}
}
Aggregations