Optimizations
This commit is contained in:
@@ -34,4 +34,23 @@ public final class ByteArraySearcher {
|
||||
public static boolean contains(byte[] haystack, byte[] needle) {
|
||||
return indexOf (haystack, needle) >= 0;
|
||||
}
|
||||
|
||||
public static boolean startsWith(byte[] haystack, byte[] needle) {
|
||||
if (needle.length > haystack.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Any needle to search?
|
||||
if (needle.length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int j = 0; j < needle.length; j++) {
|
||||
if (haystack[j] != needle[j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
@@ -73,7 +74,7 @@ public class PDFValidator extends Validator {
|
||||
|
||||
zfXML = null;
|
||||
// file existence must have been checked before
|
||||
if (!ByteArraySearcher.contains(fileContents, new byte[]{'%', 'P', 'D', 'F'})) {
|
||||
if (!ByteArraySearcher.startsWith(fileContents, new byte[]{'%', 'P', 'D', 'F'})) {
|
||||
context.addResultItem(
|
||||
new ValidationResultItem(ESeverity.fatal, "Not a PDF file " + pdfFilename).setSection(20).setPart(EPart.pdf));
|
||||
|
||||
@@ -254,14 +255,13 @@ public class PDFValidator extends Validator {
|
||||
zfXML = zi.getUTF8();
|
||||
|
||||
// step 3 find signatures
|
||||
try {
|
||||
final byte[] symtraxSignature = "Symtrax".getBytes("UTF-8");
|
||||
final byte[] mustangSignature = "via mustangproject".getBytes("UTF-8");
|
||||
final byte[] facturxpythonSignature = "by Alexis de Lattre".getBytes("UTF-8");
|
||||
final byte[] intarsysSignature = "intarsys ".getBytes("UTF-8");
|
||||
final byte[] konikSignature = "Konik".getBytes("UTF-8");
|
||||
final byte[] pdfMachineSignature = "pdfMachine from Broadgun Software".getBytes("UTF-8");
|
||||
final byte[] ghostscriptSignature = "%%Invocation:".getBytes("UTF-8");
|
||||
final byte[] symtraxSignature = "Symtrax".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] mustangSignature = "via mustangproject".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] facturxpythonSignature = "by Alexis de Lattre".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] intarsysSignature = "intarsys ".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] konikSignature = "Konik".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] pdfMachineSignature = "pdfMachine from Broadgun Software".getBytes(StandardCharsets.UTF_8);
|
||||
final byte[] ghostscriptSignature = "%%Invocation:".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
if (ByteArraySearcher.contains(fileContents, symtraxSignature)) {
|
||||
Signature = "Symtrax";
|
||||
@@ -281,10 +281,6 @@ public class PDFValidator extends Validator {
|
||||
|
||||
context.setSignature(Signature);
|
||||
|
||||
} catch (final UnsupportedEncodingException e) {
|
||||
LOGGER.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
// step 4:validate additional data
|
||||
final HashMap<String, byte[]> additionalData = zi.getAdditionalData();
|
||||
for (final String filename : additionalData.keySet()) {
|
||||
|
||||
@@ -114,7 +114,7 @@ public class ZUGFeRDValidator {
|
||||
if (disableNotices) {
|
||||
xv.disableNotices();
|
||||
}
|
||||
isPDF = ByteArraySearcher.indexOf(content, new byte[] {'%', 'P', 'D', 'F'}) == 0;
|
||||
isPDF = ByteArraySearcher.startsWith(content, new byte[] {'%', 'P', 'D', 'F'});
|
||||
if (isPDF) {
|
||||
// Avoid reading again from file
|
||||
pdfv.setFilenameAndContents(contextFilename, content);
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
package org.mustangproject.validator;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ByteArraySearcherTest {
|
||||
public class ByteArraySearcherTest
|
||||
{
|
||||
@Test
|
||||
public void testIndexOf () {
|
||||
byte [] haystack = "Hello World".getBytes (StandardCharsets.ISO_8859_1);
|
||||
@@ -21,4 +24,19 @@ public class ByteArraySearcherTest {
|
||||
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte [] { 'h' }));
|
||||
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte [] { 'r', 'o' }));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartsWith () {
|
||||
byte [] haystack = "Hello World".getBytes (StandardCharsets.ISO_8859_1);
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H' }));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'e' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H', 'e' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H', 'e' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, new byte [] { 'H', 'e', 'l', 'l' }));
|
||||
assertTrue (ByteArraySearcher.startsWith (haystack, haystack));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [0]));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'a' }));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'h' }));
|
||||
assertFalse (ByteArraySearcher.startsWith (haystack, new byte [] { 'r', 'o' }));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user