Exemplo n.º 1
1
  /**
   * Load the policies from the specified file. Also checks that the policies are correctly signed.
   */
  private static void loadPolicies(
      File jarPathName, CryptoPermissions defaultPolicy, CryptoPermissions exemptPolicy)
      throws Exception {

    JarFile jf = new JarFile(jarPathName);

    Enumeration<JarEntry> entries = jf.entries();
    while (entries.hasMoreElements()) {
      JarEntry je = entries.nextElement();
      InputStream is = null;
      try {
        if (je.getName().startsWith("default_")) {
          is = jf.getInputStream(je);
          defaultPolicy.load(is);
        } else if (je.getName().startsWith("exempt_")) {
          is = jf.getInputStream(je);
          exemptPolicy.load(is);
        } else {
          continue;
        }
      } finally {
        if (is != null) {
          is.close();
        }
      }

      // Enforce the signer restraint, i.e. signer of JCE framework
      // jar should also be the signer of the two jurisdiction policy
      // jar files.
      JarVerifier.verifyPolicySigned(je.getCertificates());
    }
    // Close and nullify the JarFile reference to help GC.
    jf.close();
    jf = null;
  }
Exemplo n.º 2
0
  /**
   * load from the specified jar filled with help files in the [language] directory in the jar
   *
   * @param file the jar file
   */
  private void loadFromJar(File file) {
    if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) {
      try {
        int counter = 0;
        JarInputStream jis;
        JarEntry je;
        counter = 0;
        jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file)));
        je = jis.getNextJarEntry();

        while (je != null) {
          String mnemo = trimEntryName(je);
          if (je.getName().toLowerCase().matches(helproot + "/" + language + "/.*.htm")
              && !exists(mnemo)) {
            addToCache(jis, mnemo);
            counter++;
          }
          je = jis.getNextJarEntry();
        }
        jis.close();

        System.out.println(
            "+ " + String.valueOf(counter) + "\thelp text(s) from:\t" + file.getCanonicalPath());
      } catch (IOException ignored) {
      }
    }
  }
Exemplo n.º 3
0
    public String hasSameContent(JarFile2 file, JarEntry entry) throws IOException {

      String thisName = null;

      Long crcL = new Long(entry.getCrc());

      // check if this jar contains files with the passed in entry's crc
      if (_crcToEntryMap.containsKey(crcL)) {
        // get the Linked List with files with the crc
        LinkedList ll = (LinkedList) _crcToEntryMap.get(crcL);
        // go through the list and check for content match
        ListIterator li = ll.listIterator(0);
        if (li != null) {
          while (li.hasNext()) {
            JarEntry thisEntry = (JarEntry) li.next();

            // check for content match
            InputStream oldIS = getJarFile().getInputStream(thisEntry);
            InputStream newIS = file.getJarFile().getInputStream(entry);

            if (!differs(oldIS, newIS)) {
              thisName = thisEntry.getName();
              return thisName;
            }
          }
        }
      }

      return thisName;
    }
Exemplo n.º 4
0
  public static List getClasseNamesInPackage(String jarName, String packageName) {
    ArrayList classes = new ArrayList();

    packageName = packageName.replaceAll("\\.", "/");
    if (debug) System.out.println("Jar " + jarName + " looking for " + packageName);
    try {
      JarInputStream jarFile = new JarInputStream(new FileInputStream(jarName));
      JarEntry jarEntry;

      while (true) {
        jarEntry = jarFile.getNextJarEntry();
        if (jarEntry == null) {
          break;
        }
        if ((jarEntry.getName().startsWith(packageName))
            && (jarEntry.getName().endsWith(".class"))) {
          if (debug) System.out.println("Found " + jarEntry.getName().replaceAll("/", "\\."));
          classes.add(jarEntry.getName().replaceAll("/", "\\."));
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return classes;
  }
  /**
   * Load classes from given file. File could be either directory or JAR file.
   *
   * @param clsLdr Class loader to load files.
   * @param file Either directory or JAR file which contains classes or references to them.
   * @return Set of found and loaded classes or empty set if file does not exist.
   * @throws GridSpiException Thrown if given JAR file references to none existed class or
   *     IOException occurred during processing.
   */
  static Set<Class<? extends GridTask<?, ?>>> getClasses(ClassLoader clsLdr, File file)
      throws GridSpiException {
    Set<Class<? extends GridTask<?, ?>>> rsrcs = new HashSet<Class<? extends GridTask<?, ?>>>();

    if (file.exists() == false) {
      return rsrcs;
    }

    GridUriDeploymentFileResourceLoader fileRsrcLdr =
        new GridUriDeploymentFileResourceLoader(clsLdr, file);

    if (file.isDirectory()) {
      findResourcesInDirectory(fileRsrcLdr, file, rsrcs);
    } else {
      try {
        for (JarEntry entry : U.asIterable(new JarFile(file.getAbsolutePath()).entries())) {
          Class<? extends GridTask<?, ?>> rsrc = fileRsrcLdr.createResource(entry.getName(), false);

          if (rsrc != null) {
            rsrcs.add(rsrc);
          }
        }
      } catch (IOException e) {
        throw new GridSpiException(
            "Failed to discover classes in file: " + file.getAbsolutePath(), e);
      }
    }

    return rsrcs;
  }
 private Certificate[] getCertificates(final File container, final String entry)
     throws IOException {
   if (container.isDirectory()) {
     return null;
   }
   final JarFile jarFile = this.jarFiles.get(container);
   if (jarFile == null) {
     return null;
   }
   final JarEntry ent = jarFile.getJarEntry(entry);
   return (Certificate[]) ((ent == null) ? null : ent.getCertificates());
 }
Exemplo n.º 7
0
    private void index() throws IOException {
      Enumeration entries = _jar.entries();

      _nameToEntryMap = new HashMap();
      _crcToEntryMap = new HashMap();

      _entries = new ArrayList();
      if (_debug) {
        System.out.println("indexing: " + _jar.getName());
      }
      if (entries != null) {
        while (entries.hasMoreElements()) {
          JarEntry entry = (JarEntry) entries.nextElement();

          long crc = entry.getCrc();

          Long crcL = new Long(crc);

          if (_debug) {
            System.out.println("\t" + entry.getName() + " CRC " + crc);
          }

          _nameToEntryMap.put(entry.getName(), entry);
          _entries.add(entry);

          // generate the CRC to entries map
          if (_crcToEntryMap.containsKey(crcL)) {
            // key exist, add the entry to the correcponding
            // linked list

            // get the linked list
            LinkedList ll = (LinkedList) _crcToEntryMap.get(crcL);

            // put in the new entry
            ll.add(entry);

            // put it back in the hash map
            _crcToEntryMap.put(crcL, ll);
          } else {
            // create a new entry in the hashmap for the new key

            // first create the linked list and put in the new
            // entry
            LinkedList ll = new LinkedList();
            ll.add(entry);

            // create the new entry in the hashmap
            _crcToEntryMap.put(crcL, ll);
          }
        }
      }
    }
Exemplo n.º 8
0
 void expand(File jar, File dir) throws IOException {
   JarFile jarFile = new JarFile(jar);
   try {
     Enumeration<JarEntry> entries = jarFile.entries();
     while (entries.hasMoreElements()) {
       JarEntry je = entries.nextElement();
       if (!je.isDirectory()) {
         copy(jarFile.getInputStream(je), new File(dir, je.getName()));
       }
     }
   } finally {
     jarFile.close();
   }
 }
 private static Set<String> findStandardMBeansFromJar(URL codeBase) throws Exception {
   InputStream is = codeBase.openStream();
   JarInputStream jis = new JarInputStream(is);
   Set<String> names = new TreeSet<String>();
   JarEntry entry;
   while ((entry = jis.getNextJarEntry()) != null) {
     String name = entry.getName();
     if (!name.endsWith(".class")) continue;
     name = name.substring(0, name.length() - 6);
     name = name.replace('/', '.');
     names.add(name);
   }
   return names;
 }
Exemplo n.º 10
0
  /**
   * Enumerates the resouces in a give package name. This works even if the resources are loaded
   * from a jar file!
   *
   * <p>Adapted from code by mikewse on the java.sun.com message boards.
   * http://forum.java.sun.com/thread.jsp?forum=22&thread=30984
   *
   * @param packageName The package to enumerate
   * @return A Set of Strings for each resouce in the package.
   */
  public static Set getResoucesInPackage(String packageName) throws IOException {
    String localPackageName;
    if (packageName.endsWith("/")) {
      localPackageName = packageName;
    } else {
      localPackageName = packageName + '/';
    }

    Enumeration dirEnum = ClassLoader.getSystemResources(localPackageName);

    Set names = new HashSet();

    // Loop CLASSPATH directories
    while (dirEnum.hasMoreElements()) {
      URL resUrl = (URL) dirEnum.nextElement();

      // Pointing to filesystem directory
      if (resUrl.getProtocol().equals("file")) {
        File dir = new File(resUrl.getFile());
        File[] files = dir.listFiles();
        if (files != null) {
          for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory()) continue;
            names.add(localPackageName + file.getName());
          }
        }

        // Pointing to Jar file
      } else if (resUrl.getProtocol().equals("jar")) {
        JarURLConnection jconn = (JarURLConnection) resUrl.openConnection();
        JarFile jfile = jconn.getJarFile();
        Enumeration entryEnum = jfile.entries();
        while (entryEnum.hasMoreElements()) {
          JarEntry entry = (JarEntry) entryEnum.nextElement();
          String entryName = entry.getName();
          // Exclude our own directory
          if (entryName.equals(localPackageName)) continue;
          String parentDirName = entryName.substring(0, entryName.lastIndexOf('/') + 1);
          if (!parentDirName.equals(localPackageName)) continue;
          names.add(entryName);
        }
      } else {
        // Invalid classpath entry
      }
    }

    return names;
  }
Exemplo n.º 11
0
  public static void unzipInteralZip(
      ClassLoader classLoader, String resourcePath, File libDir, boolean debug) {
    if (debug) System.out.println("Extracting " + resourcePath);
    libDir.mkdir();
    URL resource = classLoader.getResource(resourcePath);
    if (resource == null) {
      System.err.println("Could not find the " + resourcePath + " on classpath!");
      System.exit(1);
    }
    class PrintDot extends TimerTask {
      public void run() {
        System.out.print(".");
      }
    }
    Timer timer = new Timer();
    PrintDot task = new PrintDot();
    timer.schedule(task, 0, 2000);

    try {

      BufferedInputStream bis = new BufferedInputStream(resource.openStream());
      JarInputStream jis = new JarInputStream(bis);
      JarEntry je = null;
      while ((je = jis.getNextJarEntry()) != null) {
        java.io.File f =
            new java.io.File(libDir.toString() + java.io.File.separator + je.getName());
        if (je.isDirectory()) {
          f.mkdir();
          continue;
        }
        File parentDir = new File(f.getParent());
        if (!parentDir.exists()) {
          parentDir.mkdir();
        }
        FileOutputStream fileOutStream = new FileOutputStream(f);
        writeStreamTo(jis, fileOutStream, 8 * KB);
        if (f.getPath().endsWith("pack.gz")) {
          unpack(f);
          fileOutStream.close();
          f.delete();
        }
      }

    } catch (Exception exc) {
      task.cancel();
      exc.printStackTrace();
    }
    task.cancel();
  }
Exemplo n.º 12
0
 private static void completePackage(Set seenClasses, JarFile jar, String packageName) {
   int len = packageName.length();
   Enumeration entries = jar.entries();
   while (entries.hasMoreElements()) {
     JarEntry entry = (JarEntry) entries.nextElement();
     String name = entry.getName();
     if (name.startsWith(packageName) && name.endsWith(".class") && name.lastIndexOf('/') == len) {
       // Trim ".class" from end
       name = name.substring(0, name.length() - 6);
       if (seenClasses.add(name)) {
         System.out.println(name);
       }
     }
   }
 }
Exemplo n.º 13
0
    public boolean contains(JarFile2 f, JarEntry e) throws IOException {

      JarEntry thisEntry = getEntryByName(e.getName());

      // Look up name in 'this' Jar2File - if not exist return false
      if (thisEntry == null) return false;

      // Check CRC - if no match - return false
      if (thisEntry.getCrc() != e.getCrc()) return false;

      // Check contents - if no match - return false
      InputStream oldIS = getJarFile().getInputStream(thisEntry);
      InputStream newIS = f.getJarFile().getInputStream(e);
      boolean retValue = differs(oldIS, newIS);

      return !retValue;
    }
Exemplo n.º 14
0
 VerifierStream(Manifest man, JarEntry je, InputStream is, JarVerifier jv) throws IOException {
   this.is = is;
   this.jv = jv;
   this.mev = new ManifestEntryVerifier(man);
   this.jv.beginEntry(je, mev);
   this.numLeft = je.getSize();
   if (this.numLeft == 0) this.jv.update(-1, this.mev);
 }
Exemplo n.º 15
0
    public String getBestMatch(JarFile2 file, JarEntry entry) throws IOException {
      // check for same name and same content, return name if found
      if (contains(file, entry)) {
        return (entry.getName());
      }

      // return name of same content file or null
      return (hasSameContent(file, entry));
    }
Exemplo n.º 16
0
  public String verify(JarFile jar, String... algorithms) throws IOException {
    if (algorithms == null || algorithms.length == 0) algorithms = new String[] {"MD5", "SHA"};
    else if (algorithms.length == 1 && algorithms[0].equals("-")) return null;

    try {
      Manifest m = jar.getManifest();
      if (m.getEntries().isEmpty()) return "No name sections";

      for (Enumeration<JarEntry> e = jar.entries(); e.hasMoreElements(); ) {
        JarEntry je = e.nextElement();
        if (MANIFEST_ENTRY.matcher(je.getName()).matches()) continue;

        Attributes nameSection = m.getAttributes(je.getName());
        if (nameSection == null) return "No name section for " + je.getName();

        for (String algorithm : algorithms) {
          try {
            MessageDigest md = MessageDigest.getInstance(algorithm);
            String expected = nameSection.getValue(algorithm + "-Digest");
            if (expected != null) {
              byte digest[] = Base64.decodeBase64(expected);
              copy(jar.getInputStream(je), md);
              if (!Arrays.equals(digest, md.digest()))
                return "Invalid digest for "
                    + je.getName()
                    + ", "
                    + expected
                    + " != "
                    + Base64.encodeBase64(md.digest());
            } else reporter.error("could not find digest for " + algorithm + "-Digest");
          } catch (NoSuchAlgorithmException nsae) {
            return "Missing digest algorithm " + algorithm;
          }
        }
      }
    } catch (Exception e) {
      return "Failed to verify due to exception: " + e.getMessage();
    }
    return null;
  }
Exemplo n.º 17
0
  /**
   * scans inside a jar for possible language directories
   *
   * @param file the jar file
   * @param paths the list to add to
   * @param helproot the root directory of all help directories
   */
  private void searchJarPath(File file, List<String> paths) {
    if (file.getName().toLowerCase().endsWith(".jar") && file.isFile()) {
      try {
        JarInputStream jis;
        JarEntry je;
        jis = new JarInputStream(new BufferedInputStream(new FileInputStream(file)));
        je = jis.getNextJarEntry();

        while (je != null) {
          if (je.getName().startsWith(helproot)) {
            String[] name = je.getName().split("/");
            if (name.length > 1) {
              addToList(paths, name[1]);
            }
          }
          je = jis.getNextJarEntry();
        }
        jis.close();
      } catch (IOException ignored) {
      }
    }
  }
Exemplo n.º 18
0
 public CodeSigner[] getCodeSigners(JarFile jar, JarEntry entry) {
   String name = entry.getName();
   if (eagerValidation && sigFileSigners.get(name) != null) {
     /*
      * Force a read of the entry data to generate the
      * verification hash.
      */
     try {
       InputStream s = jar.getInputStream(entry);
       byte[] buffer = new byte[1024];
       int n = buffer.length;
       while (n != -1) {
         n = s.read(buffer, 0, buffer.length);
       }
       s.close();
     } catch (IOException e) {
     }
   }
   return getCodeSigners(name);
 }
Exemplo n.º 19
0
  /**
   * Enables to get the name of an object in the spoken language thanks to the jar file
   *
   * @param jarName the file that contains the object classes
   * @return the object name in the spoken language
   */
  private String getLangName(File jarName) {
    String name = null;

    try {
      URL url = jarName.toURI().toURL();
      JarInputStream jarFile = new JarInputStream(url.openStream());
      JarEntry jarEntry = jarFile.getNextJarEntry();
      while (jarEntry != null) {
        if (!jarEntry.isDirectory()
            && jarEntry.getName().contains(Configuration.instance().getLanguage())) {
          int lang_index = jarEntry.getName().lastIndexOf(Configuration.instance().getLanguage());
          name = jarEntry.getName().substring(lang_index + 3, jarEntry.getName().length() - 6);
        }
        jarEntry = jarFile.getNextJarEntry();
      }
    } catch (Exception e) {
      LOG.error("Error getLangName " + jarName + " " + e);
    }
    return name;
  }
Exemplo n.º 20
0
 private static String trimEntryName(JarEntry je) {
   String[] s = je.getName().toLowerCase().replace(".htm", "").split("/");
   return s[s.length - 1];
 }
Exemplo n.º 21
0
    private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "0");
      // Process the output directory or jar output.
      new PackageReader(pkg, in).read();

      if (_props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
      if (_props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "50");
      pkg.ensureAllClassFiles();
      // Now write out the files.
      HashSet classesToWrite = new HashSet(pkg.getClasses());
      for (Iterator i = pkg.getFiles().iterator(); i.hasNext(); ) {
        Package.File file = (Package.File) i.next();
        String name = file.nameString;
        JarEntry je = new JarEntry(Utils.getJarEntryName(name));
        boolean deflate;

        deflate =
            (keepDeflateHint)
                ? (((file.options & Constants.FO_DEFLATE_HINT) != 0)
                    || ((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
                : deflateHint;

        boolean needCRC = !deflate; // STORE mode requires CRC

        if (needCRC) crc.reset();
        bufOut.reset();
        if (file.isClassStub()) {
          Package.Class cls = file.getStubClass();
          assert (cls != null);
          new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
          classesToWrite.remove(cls); // for an error check
        } else {
          // collect data & maybe CRC
          file.writeTo(needCRC ? crcOut : bufOut);
        }
        je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
        if (needCRC) {
          if (verbose > 0)
            Utils.log.info("stored size=" + bufOut.size() + " and crc=" + crc.getValue());

          je.setMethod(JarEntry.STORED);
          je.setSize(bufOut.size());
          je.setCrc(crc.getValue());
        }
        if (keepModtime) {
          je.setTime(file.modtime);
          // Convert back to milliseconds
          je.setTime((long) file.modtime * 1000);
        } else {
          je.setTime((long) modtime * 1000);
        }
        out.putNextEntry(je);
        bufOut.writeTo(out);
        out.closeEntry();
        if (verbose > 0) Utils.log.info("Writing " + Utils.zeString((ZipEntry) je));
      }
      assert (classesToWrite.isEmpty());
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "100");
      pkg.reset(); // reset for the next segment, if any
    }
 public java.security.cert.Certificate[] getCertificates() {
   Certificate[] certs = je.getCertificates();
   return certs == null ? null : (Certificate[]) certs.clone();
 }
Exemplo n.º 23
0
  /** called when we reach the end of entry in one of the read() methods. */
  private void processEntry(ManifestEntryVerifier mev) throws IOException {
    if (!parsingBlockOrSF) {
      JarEntry je = mev.getEntry();
      if ((je != null) && (je.signers == null)) {
        je.signers = mev.verify(verifiedSigners, sigFileSigners);
        je.certs = mapSignersToCertArray(je.signers);
      }
    } else {

      try {
        parsingBlockOrSF = false;

        if (debug != null) {
          debug.println("processEntry: processing block");
        }

        String uname = mev.getEntry().getName().toUpperCase(Locale.ENGLISH);

        if (uname.endsWith(".SF")) {
          String key = uname.substring(0, uname.length() - 3);
          byte bytes[] = baos.toByteArray();
          // add to sigFileData in case future blocks need it
          sigFileData.put(key, bytes);
          // check pending blocks, we can now process
          // anyone waiting for this .SF file
          Iterator it = pendingBlocks.iterator();
          while (it.hasNext()) {
            SignatureFileVerifier sfv = (SignatureFileVerifier) it.next();
            if (sfv.needSignatureFile(key)) {
              if (debug != null) {
                debug.println("processEntry: processing pending block");
              }

              sfv.setSignatureFile(bytes);
              sfv.process(sigFileSigners, manifestDigests);
            }
          }
          return;
        }

        // now we are parsing a signature block file

        String key = uname.substring(0, uname.lastIndexOf("."));

        if (signerCache == null) signerCache = new ArrayList();

        if (manDig == null) {
          synchronized (manifestRawBytes) {
            if (manDig == null) {
              manDig = new ManifestDigester(manifestRawBytes);
              manifestRawBytes = null;
            }
          }
        }

        SignatureFileVerifier sfv =
            new SignatureFileVerifier(signerCache, manDig, uname, baos.toByteArray());

        if (sfv.needSignatureFileBytes()) {
          // see if we have already parsed an external .SF file
          byte[] bytes = (byte[]) sigFileData.get(key);

          if (bytes == null) {
            // put this block on queue for later processing
            // since we don't have the .SF bytes yet
            // (uname, block);
            if (debug != null) {
              debug.println("adding pending block");
            }
            pendingBlocks.add(sfv);
            return;
          } else {
            sfv.setSignatureFile(bytes);
          }
        }
        sfv.process(sigFileSigners, manifestDigests);

      } catch (IOException ioe) {
        // e.g. sun.security.pkcs.ParsingException
        if (debug != null) debug.println("processEntry caught: " + ioe);
        // ignore and treat as unsigned
      } catch (SignatureException se) {
        if (debug != null) debug.println("processEntry caught: " + se);
        // ignore and treat as unsigned
      } catch (NoSuchAlgorithmException nsae) {
        if (debug != null) debug.println("processEntry caught: " + nsae);
        // ignore and treat as unsigned
      } catch (CertificateException ce) {
        if (debug != null) debug.println("processEntry caught: " + ce);
        // ignore and treat as unsigned
      }
    }
  }
Exemplo n.º 24
0
  /** Creates a patch from the two passed in files, writing the result to <code>os</code>. */
  public static void createPatch(String oldPath, String newPath, OutputStream os, boolean minimal)
      throws IOException {
    JarFile2 oldJar = new JarFile2(oldPath);
    JarFile2 newJar = new JarFile2(newPath);

    try {
      Iterator entries;
      HashMap moved = new HashMap();
      HashSet visited = new HashSet();
      HashSet implicit = new HashSet();
      HashSet moveSrc = new HashSet();
      HashSet newEntries = new HashSet();

      // FIRST PASS
      // Go through the entries in new jar and
      // determine which files are candidates for implicit moves
      // ( files that has the same filename and same content in old.jar
      // and new.jar )
      // and for files that cannot be implicitly moved, we will either
      // find out whether it is moved or new (modified)
      entries = newJar.getJarEntries();
      if (entries != null) {
        while (entries.hasNext()) {
          JarEntry newEntry = (JarEntry) entries.next();
          String newname = newEntry.getName();

          // Return best match of contents, will return a name match if possible
          String oldname = oldJar.getBestMatch(newJar, newEntry);
          if (oldname == null) {
            // New or modified entry
            if (_debug) {
              System.out.println("NEW: " + newname);
            }
            newEntries.add(newname);
          } else {
            // Content already exist - need to do a move

            // Should do implicit move? Yes, if names are the same, and
            // no move command already exist from oldJar
            if (oldname.equals(newname) && !moveSrc.contains(oldname)) {
              if (_debug) {
                System.out.println(newname + " added to implicit set!");
              }
              implicit.add(newname);
            } else {
              // The 1.0.1/1.0 JarDiffPatcher cannot handle
              // multiple MOVE command with same src.
              // The work around here is if we are going to generate
              // a MOVE command with duplicate src, we will
              // instead add the target as a new file.  This way
              // the jardiff can be applied by 1.0.1/1.0
              // JarDiffPatcher also.
              if (!minimal && (implicit.contains(oldname) || moveSrc.contains(oldname))) {

                // generate non-minimal jardiff
                // for backward compatibility

                if (_debug) {

                  System.out.println("NEW: " + newname);
                }
                newEntries.add(newname);
              } else {
                // Use newname as key, since they are unique
                if (_debug) {
                  System.err.println("moved.put " + newname + " " + oldname);
                }
                moved.put(newname, oldname);
                moveSrc.add(oldname);
              }
              // Check if this disables an implicit 'move <oldname> <oldname>'
              if (implicit.contains(oldname) && minimal) {

                if (_debug) {
                  System.err.println("implicit.remove " + oldname);

                  System.err.println("moved.put " + oldname + " " + oldname);
                }
                implicit.remove(oldname);
                moved.put(oldname, oldname);
                moveSrc.add(oldname);
              }
            }
          }
        }
      } // if (entries != null)

      // SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> -
      // <source of move commands> - <new or modified entries>
      ArrayList deleted = new ArrayList();
      entries = oldJar.getJarEntries();
      if (entries != null) {
        while (entries.hasNext()) {
          JarEntry oldEntry = (JarEntry) entries.next();
          String oldName = oldEntry.getName();
          if (!implicit.contains(oldName)
              && !moveSrc.contains(oldName)
              && !newEntries.contains(oldName)) {
            if (_debug) {
              System.err.println("deleted.add " + oldName);
            }
            deleted.add(oldName);
          }
        }
      }

      // DEBUG
      if (_debug) {
        // DEBUG:  print out moved map
        entries = moved.keySet().iterator();
        if (entries != null) {
          System.out.println("MOVED MAP!!!");
          while (entries.hasNext()) {
            String newName = (String) entries.next();
            String oldName = (String) moved.get(newName);
            System.out.println("key is " + newName + " value is " + oldName);
          }
        }

        // DEBUG:  print out IMOVE map
        entries = implicit.iterator();
        if (entries != null) {
          System.out.println("IMOVE MAP!!!");
          while (entries.hasNext()) {
            String newName = (String) entries.next();
            System.out.println("key is " + newName);
          }
        }
      }

      JarOutputStream jos = new JarOutputStream(os);

      // Write out all the MOVEs and REMOVEs
      createIndex(jos, deleted, moved);

      // Put in New and Modified entries
      entries = newEntries.iterator();
      if (entries != null) {

        while (entries.hasNext()) {
          String newName = (String) entries.next();
          if (_debug) {
            System.out.println("New File: " + newName);
          }
          writeEntry(jos, newJar.getEntryByName(newName), newJar);
        }
      }

      jos.finish();
      jos.close();

    } catch (IOException ioE) {
      throw ioE;
    } finally {
      try {
        oldJar.getJarFile().close();
      } catch (IOException e1) {
        // ignore
      }
      try {
        newJar.getJarFile().close();
      } catch (IOException e1) {
        // ignore
      }
    } // finally
  }
Exemplo n.º 25
0
  /**
   * This method scans to see which entry we're parsing and keeps various state information
   * depending on what type of file is being parsed.
   */
  public void beginEntry(JarEntry je, ManifestEntryVerifier mev) throws IOException {
    if (je == null) return;

    if (debug != null) {
      debug.println("beginEntry " + je.getName());
    }

    String name = je.getName();

    /*
     * Assumptions:
     * 1. The manifest should be the first entry in the META-INF directory.
     * 2. The .SF/.DSA/.EC files follow the manifest, before any normal entries
     * 3. Any of the following will throw a SecurityException:
     *    a. digest mismatch between a manifest section and
     *       the SF section.
     *    b. digest mismatch between the actual jar entry and the manifest
     */

    if (parsingMeta) {
      String uname = name.toUpperCase(Locale.ENGLISH);
      if ((uname.startsWith("META-INF/") || uname.startsWith("/META-INF/"))) {

        if (je.isDirectory()) {
          mev.setEntry(null, je);
          return;
        }

        if (uname.equals(JarFile.MANIFEST_NAME) || uname.equals(JarIndex.INDEX_NAME)) {
          return;
        }

        if (SignatureFileVerifier.isBlockOrSF(uname)) {
          /* We parse only DSA, RSA or EC PKCS7 blocks. */
          parsingBlockOrSF = true;
          baos.reset();
          mev.setEntry(null, je);
          return;
        }

        // If a META-INF entry is not MF or block or SF, they should
        // be normal entries. According to 2 above, no more block or
        // SF will appear. Let's doneWithMeta.
      }
    }

    if (parsingMeta) {
      doneWithMeta();
    }

    if (je.isDirectory()) {
      mev.setEntry(null, je);
      return;
    }

    // be liberal in what you accept. If the name starts with ./, remove
    // it as we internally canonicalize it with out the ./.
    if (name.startsWith("./")) name = name.substring(2);

    // be liberal in what you accept. If the name starts with /, remove
    // it as we internally canonicalize it with out the /.
    if (name.startsWith("/")) name = name.substring(1);

    // only set the jev object for entries that have a signature
    // (either verified or not)
    if (sigFileSigners.get(name) != null || verifiedSigners.get(name) != null) {
      mev.setEntry(name, je);
      return;
    }

    // don't compute the digest for this entry
    mev.setEntry(null, je);

    return;
  }