Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: PDF 2.0 header detection #964

Open
wants to merge 4 commits into
base: dev/1.33
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions jhove-apps/src/main/java/edu/harvard/hul/ois/jhove/Jhove.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package edu.harvard.hul.ois.jhove;

/**********************************************************************
* Jhove - JSTOR/Harvard Object Validation Environment
* Copyright 2004-2007 by the President and Fellows of Harvard College
Expand All @@ -19,13 +17,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
**********************************************************************/

import edu.harvard.hul.ois.jhove.App;
import edu.harvard.hul.ois.jhove.ExitCode;
import edu.harvard.hul.ois.jhove.CoreMessageConstants;
import edu.harvard.hul.ois.jhove.JhoveBase;
import edu.harvard.hul.ois.jhove.Module;
import edu.harvard.hul.ois.jhove.OutputHandler;
package edu.harvard.hul.ois.jhove;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,14 +30,15 @@ public class Jhove {
/** Logger for this class. */
private static final Logger LOGGER = Logger.getLogger(Jhove.class.getCanonicalName());

private Jhove() {
throw new AssertionError("Should never enter private constructor");
}

private static final String C_CONFIG_OPTION = "-c";
private static final String X_CONFIG_OPTION = "-x";
private static final String NOT_FOUND = "' not found";
private static final String NOT_FOUND = "not found";
private static final String HANDLER = "Handler '";
private static final String MODULE = "Module";

private Jhove() {
throw new AssertionError("Should never enter private constructor");
}

/**
* MAIN ENTRY POINT.
Expand Down Expand Up @@ -233,17 +226,17 @@ public static void main(String[] args) {
}
Module module = je.getModule(moduleName);
if (module == null && moduleName != null) {
LOGGER.log(Level.SEVERE, "Module '" + moduleName + NOT_FOUND);
LOGGER.log(Level.SEVERE, notFoundMessage(MODULE, moduleName));
System.exit(ExitCode.ERROR.getReturnCode());
}
OutputHandler about = je.getHandler(aboutHandler);
if (about == null && aboutHandler != null) {
LOGGER.log(Level.SEVERE, HANDLER + aboutHandler + NOT_FOUND);
LOGGER.log(Level.SEVERE, notFoundMessage(HANDLER, aboutHandler));
System.exit(ExitCode.ERROR.getReturnCode());
}
OutputHandler handler = je.getHandler(handlerName);
if (handler == null && handlerName != null) {
LOGGER.log(Level.SEVERE, HANDLER + handlerName + NOT_FOUND);
LOGGER.log(Level.SEVERE, notFoundMessage(HANDLER, handlerName));
System.exit(ExitCode.ERROR.getReturnCode());
}
String[] dirFileOrUri = null;
Expand All @@ -264,9 +257,13 @@ public static void main(String[] args) {
je.setSignatureFlag(signature);
je.dispatch(app, module, about, handler, outputFile, dirFileOrUri);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage());
LOGGER.log(Level.SEVERE, e.getMessage(), e);
e.printStackTrace(System.err);
System.exit(ExitCode.ERROR.getReturnCode());
}
}

private static final String notFoundMessage(final String notFoundType, final String notFoundName) {
return String.format("%s '%s' %s", notFoundType, notFoundName, NOT_FOUND);
}
}
13 changes: 13 additions & 0 deletions jhove-bbt/scripts/create-1.33-target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ echo "TEST BASELINE: Creating baseline"
echo " - copying ${baselineRoot} baseline to ${targetRoot}"
cp -R "${baselineRoot}" "${targetRoot}"

# Insert new PDF-2.x signature nodes into the PDF audit XML files
find "${targetRoot}" -type f -name "audit-PDF-hul.jhove.xml" -exec sed -i 's/<\/signatures>/ <signature>\n <type>Magic number<\/type>\n <value>%PDF-2.<\/value>\n <offset>0x0<\/offset>\n <use>Mandatory<\/use>\n <\/signature>\n <\/signatures>/' {} \;

# Copy the two new version regression test files, where handling of PDF version declaration has been improved/changed
declare -a pdf_version_affected=("errors/modules/PDF-hul/pdf-hul-61-CERN-2005-009.pdf.jhove.xml"
"regression/modules/PDF-hul/PDF-HUL-137.pdf.jhove.xml"
"regression/modules/PDF-hul/PDF-HUL-137_fixed.pdf.jhove.xml")
for filename in "${pdf_version_affected[@]}"
do
if [[ -f "${candidateRoot}/${filename}" ]]; then
cp "${candidateRoot}/${filename}" "${targetRoot}/${filename}"
fi
done
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public static JhoveMessage getMessageInstance(final String id,
return getMessageInstance(id, message, EMPTY_MESSAGE);
}

/**
public static JhoveMessage getMessageInstance(final JhoveMessage message, final String subMessage) {
return getMessageInstance(message.getId(), message.getMessage(), subMessage);
}

/**
* Create a new JhoveMessage instance with the given id, message and
* sub-message
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ public void testMessageInstanceEmptyId() {

@Test
public void testMessageInstanceNullId() {
final String id = null;
assertThrows("IllegalArgument exception expected",
IllegalArgumentException.class,
() -> {
JhoveMessages.getMessageInstance(null, "MSG");
JhoveMessages.getMessageInstance(id, "MSG");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,9 @@ public PdfModule() {

_signature.add(new ExternalSignature(EXT, SignatureType.EXTENSION,
SignatureUseType.OPTIONAL));
_signature.add(new InternalSignature(PdfHeader.PDF_SIG_HEADER,
_signature.add(new InternalSignature(PdfHeader.PDF_1_SIG_HEADER,
SignatureType.MAGIC, SignatureUseType.MANDATORY, 0));
_signature.add(new InternalSignature(PdfHeader.PDF_2_SIG_HEADER,
SignatureType.MAGIC, SignatureUseType.MANDATORY, 0));

doc = new Document(
Expand Down Expand Up @@ -1043,24 +1045,19 @@ protected final void initParse() {
_nFonts = 0;
}

protected boolean parseHeader(RepInfo info) throws IOException {
protected boolean parseHeader(RepInfo info) {
PdfHeader header = null;
try {
header = PdfHeader.parseHeader(_parser);
} catch (PdfMalformedException e) {
info.setWellFormed(false);
info.setMessage(new ErrorMessage(MessageConstants.PDF_HUL_155, 0L)); // PDF-HUL-155
return false;
}
if (header == null) {
info.setWellFormed(false);
info.setMessage(new ErrorMessage(MessageConstants.PDF_HUL_137, 0L)); // PDF-HUL-137
} catch (PdfException e) {
if (e instanceof PdfInvalidException) {
info.setValid(false);
} else {
info.setWellFormed(false);
}
info.setMessage(new ErrorMessage(e.getJhoveMessage(), 0L)); // PDF-HUL-155
return false;
}
if (!header.isVersionValid()) {
info.setValid(false);
info.setMessage(new ErrorMessage(MessageConstants.PDF_HUL_148, 0L)); // PDF-HUL-148
}
_version = header.getVersionString();
_pdfACompliant = header.isPdfACompliant();
info.setSigMatch(_name);
Expand Down
Loading
Loading