Search in sources :

Example 91 with JarEntry

use of java.util.jar.JarEntry in project MinecraftForge by MinecraftForge.

the class ClassPatchManager method setup.

public void setup(Side side) {
    Pattern binpatchMatcher = Pattern.compile(String.format("binpatch/%s/.*.binpatch", side.toString().toLowerCase(Locale.ENGLISH)));
    JarInputStream jis;
    try {
        InputStream binpatchesCompressed = getClass().getResourceAsStream("/binpatches.pack.lzma");
        if (binpatchesCompressed == null) {
            FMLRelaunchLog.log(Level.ERROR, "The binary patch set is missing. Either you are in a development environment, or things are not going to work!");
            return;
        }
        LzmaInputStream binpatchesDecompressed = new LzmaInputStream(binpatchesCompressed);
        ByteArrayOutputStream jarBytes = new ByteArrayOutputStream();
        JarOutputStream jos = new JarOutputStream(jarBytes);
        Pack200.newUnpacker().unpack(binpatchesDecompressed, jos);
        jis = new JarInputStream(new ByteArrayInputStream(jarBytes.toByteArray()));
    } catch (Exception e) {
        FMLRelaunchLog.log(Level.ERROR, e, "Error occurred reading binary patches. Expect severe problems!");
        throw Throwables.propagate(e);
    }
    patches = ArrayListMultimap.create();
    do {
        try {
            JarEntry entry = jis.getNextJarEntry();
            if (entry == null) {
                break;
            }
            if (binpatchMatcher.matcher(entry.getName()).matches()) {
                ClassPatch cp = readPatch(entry, jis);
                if (cp != null) {
                    patches.put(cp.sourceClassName, cp);
                }
            } else {
                jis.closeEntry();
            }
        } catch (IOException e) {
        }
    } while (true);
    FMLRelaunchLog.fine("Read %d binary patches", patches.size());
    if (DEBUG)
        FMLRelaunchLog.fine("Patch list :\n\t%s", Joiner.on("\t\n").join(patches.asMap().entrySet()));
    patchedClasses.clear();
}
Also used : Pattern(java.util.regex.Pattern) LzmaInputStream(LZMA.LzmaInputStream) JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) LzmaInputStream(LZMA.LzmaInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException)

Example 92 with JarEntry

use of java.util.jar.JarEntry in project MinecraftForge by MinecraftForge.

the class GenDiffSet method main.

public static void main(String[] args) throws IOException {
    //Clean Vanilla jar minecraft.jar or minecraft_server.jar
    String sourceJar = args[0];
    //Directory containing obfed output classes, typically mcp/reobf/minecraft
    String targetDir = args[1];
    //Path to FML's deobfusication_data.lzma
    String deobfData = args[2];
    //Path to place generated .binpatch
    String outputDir = args[3];
    //"true" if we should destroy the target file if it generated a successful .binpatch
    String killTarget = args[4];
    LogManager.getLogger("GENDIFF").log(Level.INFO, String.format("Creating patches at %s for %s from %s", outputDir, sourceJar, targetDir));
    Delta delta = new Delta();
    FMLDeobfuscatingRemapper remapper = FMLDeobfuscatingRemapper.INSTANCE;
    remapper.setupLoadOnly(deobfData, false);
    JarFile sourceZip = new JarFile(sourceJar);
    boolean kill = killTarget.equalsIgnoreCase("true");
    File f = new File(outputDir);
    f.mkdirs();
    for (String name : remapper.getObfedClasses()) {
        //            Logger.getLogger("GENDIFF").info(String.format("Evaluating path for data :%s",name));
        String fileName = name;
        String jarName = name;
        if (RESERVED_NAMES.contains(name.toUpperCase(Locale.ENGLISH))) {
            fileName = "_" + name;
        }
        File targetFile = new File(targetDir, fileName.replace('/', File.separatorChar) + ".class");
        jarName = jarName + ".class";
        if (targetFile.exists()) {
            String sourceClassName = name.replace('/', '.');
            String targetClassName = remapper.map(name).replace('/', '.');
            JarEntry entry = sourceZip.getJarEntry(jarName);
            byte[] vanillaBytes = toByteArray(sourceZip, entry);
            byte[] patchedBytes = Files.toByteArray(targetFile);
            byte[] diff = delta.compute(vanillaBytes, patchedBytes);
            ByteArrayDataOutput diffOut = ByteStreams.newDataOutput(diff.length + 50);
            // Original name
            diffOut.writeUTF(name);
            // Source name
            diffOut.writeUTF(sourceClassName);
            // Target name
            diffOut.writeUTF(targetClassName);
            // exists at original
            diffOut.writeBoolean(entry != null);
            if (entry != null) {
                diffOut.writeInt(Hashing.adler32().hashBytes(vanillaBytes).asInt());
            }
            // length of patch
            diffOut.writeInt(diff.length);
            // patch
            diffOut.write(diff);
            File target = new File(outputDir, targetClassName + ".binpatch");
            target.getParentFile().mkdirs();
            Files.write(diffOut.toByteArray(), target);
            Logger.getLogger("GENDIFF").info(String.format("Wrote patch for %s (%s) at %s", name, targetClassName, target.getAbsolutePath()));
            if (kill) {
                targetFile.delete();
                Logger.getLogger("GENDIFF").info(String.format("  Deleted target: %s", targetFile.toString()));
            }
        }
    }
    sourceZip.close();
}
Also used : FMLDeobfuscatingRemapper(net.minecraftforge.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper) Delta(net.minecraftforge.fml.repackage.com.nothome.delta.Delta) ByteArrayDataOutput(com.google.common.io.ByteArrayDataOutput) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 93 with JarEntry

use of java.util.jar.JarEntry in project MSEC by Tencent.

the class ClassUtils method loadClasses.

/**
	 * �Ӱ�package�л�ȡ���е�Class
	 * 
	 * @param pack
	 * @return
	 */
public static void loadClasses(String pack) {
    // �Ƿ�ѭ������
    boolean recursive = true;
    // ��ȡ�������� �������滻
    String packageName = pack;
    String packageDirName = packageName.replace('.', '/');
    // ����һ��ö�ٵļ��� ������ѭ�����������Ŀ¼�µ�things
    Enumeration<URL> dirs;
    try {
        dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
        // ѭ��������ȥ
        while (dirs.hasMoreElements()) {
            // ��ȡ��һ��Ԫ��
            URL url = dirs.nextElement();
            // �õ�Э�������
            String protocol = url.getProtocol();
            // ��������ļ�����ʽ�����ڷ�������
            if ("file".equals(protocol)) {
                // ��ȡ��������·��
                String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
                // ���ļ��ķ�ʽɨ���������µ��ļ� ����ӵ�������
                findAndAddClassesInPackageByFile(packageName, filePath, recursive);
            } else if ("jar".equals(protocol)) {
                // �����jar���ļ�
                // ����һ��JarFile
                JarFile jar;
                try {
                    // ��ȡjar
                    jar = ((JarURLConnection) url.openConnection()).getJarFile();
                    // �Ӵ�jar�� �õ�һ��ö����
                    Enumeration<JarEntry> entries = jar.entries();
                    // ͬ���Ľ���ѭ������
                    while (entries.hasMoreElements()) {
                        // ��ȡjar���һ��ʵ�� ������Ŀ¼ ��һЩjar����������ļ� ��META-INF���ļ�
                        JarEntry entry = entries.nextElement();
                        String name = entry.getName();
                        // �������/��ͷ��
                        if (name.charAt(0) == '/') {
                            // ��ȡ������ַ���
                            name = name.substring(1);
                        }
                        // ���ǰ�벿�ֺͶ���İ�����ͬ
                        if (name.startsWith(packageDirName)) {
                            int idx = name.lastIndexOf('/');
                            // �����"/"��β ��һ����
                            if (idx != -1) {
                                // ��ȡ���� ��"/"�滻��"."
                                packageName = name.substring(0, idx).replace('/', '.');
                            }
                            // ������Ե�����ȥ ������һ����
                            if ((idx != -1) || recursive) {
                                // �����һ��.class�ļ� ���Ҳ���Ŀ¼
                                if (name.endsWith(".class") && !entry.isDirectory()) {
                                    // ȥ�������".class" ��ȡ����������
                                    String className = name.substring(packageName.length() + 1, name.length() - 6);
                                    try {
                                        // ��ӵ�classes
                                        Class.forName(packageName + '.' + className);
                                    } catch (ClassNotFoundException e) {
                                        // log
                                        // .error("����û��Զ�����ͼ����� �Ҳ��������.class�ļ�");
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                } catch (IOException e) {
                    // log.error("��ɨ���û�������ͼʱ��jar����ȡ�ļ�����");
                    e.printStackTrace();
                }
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
Also used : Enumeration(java.util.Enumeration) JarURLConnection(java.net.JarURLConnection) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL)

Example 94 with JarEntry

use of java.util.jar.JarEntry in project tinker by Tencent.

the class ShareSecurityCheck method verifyPatchMetaSignature.

public boolean verifyPatchMetaSignature(File path) {
    if (!SharePatchFileUtil.isLegalFile(path)) {
        return false;
    }
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(path);
        final Enumeration<JarEntry> entries = jarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            // no code
            if (jarEntry == null) {
                continue;
            }
            final String name = jarEntry.getName();
            if (name.startsWith("META-INF/")) {
                continue;
            }
            //we will check other files's mad5 written in meta files
            if (!name.endsWith(ShareConstants.META_SUFFIX)) {
                continue;
            }
            metaContentMap.put(name, SharePatchFileUtil.loadDigestes(jarFile, jarEntry));
            Certificate[] certs = jarEntry.getCertificates();
            if (certs == null) {
                return false;
            }
            if (!check(path, certs)) {
                return false;
            }
        }
    } catch (Exception e) {
        throw new TinkerRuntimeException(String.format("ShareSecurityCheck file %s, size %d verifyPatchMetaSignature fail", path.getAbsolutePath(), path.length()), e);
    } finally {
        try {
            if (jarFile != null) {
                jarFile.close();
            }
        } catch (IOException e) {
            Log.e(TAG, path.getAbsolutePath(), e);
        }
    }
    return true;
}
Also used : TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) IOException(java.io.IOException) TinkerRuntimeException(com.tencent.tinker.loader.TinkerRuntimeException) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 95 with JarEntry

use of java.util.jar.JarEntry in project atlas by alibaba.

the class ApkUtils method getApkPublicKey.

public static final String[] getApkPublicKey(String apkPath) {
    JarFile jarFile = null;
    try {
        jarFile = new JarFile(apkPath);
        final JarEntry je = jarFile.getJarEntry("classes.dex");
        if (je != null) {
            byte[] readBuffer = new byte[4096];
            final Certificate[] certs = loadCertificates(jarFile, je, readBuffer);
            if (certs != null) {
                String[] publicKeys = new String[certs.length];
                for (int i = 0; i < certs.length; i++) {
                    Certificate cert = certs[i];
                    PublicKey publicKey = cert.getPublicKey();
                    publicKeys[i] = bytesToHexString(publicKey.getEncoded());
                }
                return publicKeys;
            }
        }
    } catch (IOException e) {
    } finally {
        if (jarFile != null) {
            try {
                jarFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}
Also used : PublicKey(java.security.PublicKey) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Certificate(java.security.cert.Certificate)

Aggregations

JarEntry (java.util.jar.JarEntry)594 JarFile (java.util.jar.JarFile)290 File (java.io.File)217 IOException (java.io.IOException)187 InputStream (java.io.InputStream)134 JarOutputStream (java.util.jar.JarOutputStream)112 FileOutputStream (java.io.FileOutputStream)109 FileInputStream (java.io.FileInputStream)92 URL (java.net.URL)87 JarInputStream (java.util.jar.JarInputStream)87 ArrayList (java.util.ArrayList)67 Manifest (java.util.jar.Manifest)58 JarURLConnection (java.net.JarURLConnection)53 Test (org.junit.Test)39 HashSet (java.util.HashSet)31 ZipEntry (java.util.zip.ZipEntry)31 ZipFile (java.util.zip.ZipFile)30 OutputStream (java.io.OutputStream)29 BufferedInputStream (java.io.BufferedInputStream)26 Enumeration (java.util.Enumeration)26