Changed how magic number look ups are done (now uses a hash map); added magic number for png; added unit test

This commit is contained in:
cyian-1756 2018-07-25 03:16:41 -04:00
parent 24b1c7175e
commit abeb0b0984
2 changed files with 19 additions and 6 deletions

View File

@ -20,6 +20,7 @@ import java.lang.reflect.Constructor;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
@ -45,6 +46,7 @@ public class Utils {
private static PropertiesConfiguration config;
private static HashMap<String, HashMap<String, String>> cookieCache;
private static HashMap<ByteBuffer, String> magicHash = new HashMap<>();
static {
cookieCache = new HashMap<>();
@ -732,13 +734,21 @@ public class Utils {
Utils.bytesToHumanReadable(bytesTotal);
}
public static String getEXTFromMagic(byte[] magic) {
if (Arrays.equals(magic, new byte[]{-1, -40, -1, -37, 0, 0, 0, 0})) {
return "jpeg";
} else {
LOGGER.info("Unknown magic number " + Arrays.toString(magic));
public static String getEXTFromMagic(ByteBuffer magic) {
if (magicHash.isEmpty()) {
LOGGER.debug("initialising map");
initialiseMagicHashMap();
}
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.

View File

@ -3,10 +3,13 @@ package com.rarchives.ripme.tst;
import junit.framework.TestCase;
import com.rarchives.ripme.utils.Utils;
import java.nio.ByteBuffer;
public class UtilsTest extends TestCase {
public void testGetEXTFromMagic() {
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() {