Merge pull request #830 from cyian-1756/moreMagicNumbers

Changed how magic number look ups are done (now uses a hash map); add…
This commit is contained in:
cyian-1756 2018-08-04 13:11:03 -04:00 committed by GitHub
commit 7674eb570e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 6 deletions

View File

@ -20,6 +20,7 @@ import java.lang.reflect.Constructor;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
@ -45,6 +46,7 @@ public class Utils {
private static PropertiesConfiguration config; private static PropertiesConfiguration config;
private static HashMap<String, HashMap<String, String>> cookieCache; private static HashMap<String, HashMap<String, String>> cookieCache;
private static HashMap<ByteBuffer, String> magicHash = new HashMap<>();
static { static {
cookieCache = new HashMap<>(); cookieCache = new HashMap<>();
@ -732,13 +734,21 @@ public class Utils {
Utils.bytesToHumanReadable(bytesTotal); Utils.bytesToHumanReadable(bytesTotal);
} }
public static String getEXTFromMagic(byte[] magic) { public static String getEXTFromMagic(ByteBuffer magic) {
if (Arrays.equals(magic, new byte[]{-1, -40, -1, -37, 0, 0, 0, 0})) { if (magicHash.isEmpty()) {
return "jpeg"; LOGGER.debug("initialising map");
} else { initialiseMagicHashMap();
LOGGER.info("Unknown magic number " + Arrays.toString(magic));
} }
return null; return magicHash.get(magic);
}
public static String getEXTFromMagic(byte[] magic) {
return getEXTFromMagic(ByteBuffer.wrap(magic));
}
private static void initialiseMagicHashMap() {
magicHash.put(ByteBuffer.wrap(new byte[]{-1, -40, -1, -37, 0, 0, 0, 0}), "jpeg");
magicHash.put(ByteBuffer.wrap(new byte[]{-119, 80, 78, 71, 13, 0, 0, 0}), "png");
} }
// Checks if a file exists ignoring it's extension. // Checks if a file exists ignoring it's extension.

View File

@ -3,6 +3,7 @@ package com.rarchives.ripme.tst;
import junit.framework.TestCase; import junit.framework.TestCase;
import com.rarchives.ripme.utils.Utils; import com.rarchives.ripme.utils.Utils;
import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -10,6 +11,7 @@ public class UtilsTest extends TestCase {
public void testGetEXTFromMagic() { public void testGetEXTFromMagic() {
assertEquals("jpeg", Utils.getEXTFromMagic(new byte[]{-1, -40, -1, -37, 0, 0, 0, 0})); assertEquals("jpeg", Utils.getEXTFromMagic(new byte[]{-1, -40, -1, -37, 0, 0, 0, 0}));
assertEquals("png", Utils.getEXTFromMagic(new byte[]{-119, 80, 78, 71, 13, 0, 0, 0}));
} }
public void testStripURLParameter() { public void testStripURLParameter() {