Optimizations

This commit is contained in:
Philip Helger
2024-07-09 18:53:06 +02:00
parent d9168c54a5
commit 8e20badf6e
4 changed files with 74 additions and 41 deletions

View File

@@ -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;
}
}

View File

@@ -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,37 +255,32 @@ 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";
} else if (ByteArraySearcher.contains(fileContents, mustangSignature)) {
Signature = "Mustang";
} else if (ByteArraySearcher.contains(fileContents, facturxpythonSignature)) {
Signature = "Factur/X Python";
} else if (ByteArraySearcher.contains(fileContents, intarsysSignature)) {
Signature = "Intarsys";
} else if (ByteArraySearcher.contains(fileContents, konikSignature)) {
Signature = "Konik";
} else if (ByteArraySearcher.contains(fileContents, pdfMachineSignature)) {
Signature = "pdfMachine";
} else if (ByteArraySearcher.contains(fileContents, ghostscriptSignature)) {
Signature = "Ghostscript";
}
context.setSignature(Signature);
} catch (final UnsupportedEncodingException e) {
LOGGER.error(e.getMessage(), e);
if (ByteArraySearcher.contains(fileContents, symtraxSignature)) {
Signature = "Symtrax";
} else if (ByteArraySearcher.contains(fileContents, mustangSignature)) {
Signature = "Mustang";
} else if (ByteArraySearcher.contains(fileContents, facturxpythonSignature)) {
Signature = "Factur/X Python";
} else if (ByteArraySearcher.contains(fileContents, intarsysSignature)) {
Signature = "Intarsys";
} else if (ByteArraySearcher.contains(fileContents, konikSignature)) {
Signature = "Konik";
} else if (ByteArraySearcher.contains(fileContents, pdfMachineSignature)) {
Signature = "pdfMachine";
} else if (ByteArraySearcher.contains(fileContents, ghostscriptSignature)) {
Signature = "Ghostscript";
}
context.setSignature(Signature);
// step 4:validate additional data
final HashMap<String, byte[]> additionalData = zi.getAdditionalData();
for (final String filename : additionalData.keySet()) {

View File

@@ -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);

View File

@@ -1,24 +1,42 @@
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);
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H'}));
assertEquals (1, ByteArraySearcher.indexOf (haystack, new byte[] {'e'}));
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H', 'e'}));
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H', 'e'}));
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte[] {'H', 'e', 'l', 'l'}));
byte [] haystack = "Hello World".getBytes (StandardCharsets.ISO_8859_1);
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte [] { 'H' }));
assertEquals (1, ByteArraySearcher.indexOf (haystack, new byte [] { 'e' }));
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte [] { 'H', 'e' }));
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte [] { 'H', 'e' }));
assertEquals (0, ByteArraySearcher.indexOf (haystack, new byte [] { 'H', 'e', 'l', 'l' }));
assertEquals (0, ByteArraySearcher.indexOf (haystack, haystack));
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte[0]));
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte[] {'a'}));
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte[] {'h'}));
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte[] {'r', 'o'}));
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte [0]));
assertEquals (-1, ByteArraySearcher.indexOf (haystack, new byte [] { 'a' }));
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' }));
}
}