Exemple #1
0
        /**
         * Copy all the files in a manifest from input to output.  We set
         * the modification times in the output to a fixed time, so as to
         * reduce variation in the output file and make incremental OTAs
         * more efficient.
         */
        private static void copyFiles(Manifest manifest,
                                      JarFile in_, ZipOutputStream out_, DateTime timestamp)
        {
            byte[] buffer = new byte[4096];
            int    num;

            IDictionary <String, Attributes> entries = manifest.Entries;
            List <String> names = new List <String>(entries.Keys);

            names.Sort();
            foreach (String name in names)
            {
                JarEntry inEntry  = in_.GetJarEntry(name);
                JarEntry outEntry = null;
                if (inEntry.CompressionMethod == CompressionMethod.Stored)
                {
                    // Preserve the STORED method of the input entry.
                    outEntry = new JarEntry(inEntry);
                }
                else
                {
                    // Create a new entry so that the compressed len is recomputed.
                    outEntry = new JarEntry(name);
                    if (inEntry.Size > -1)
                    {
                        outEntry.Size = inEntry.Size;
                    }
                }
                outEntry.DateTime = timestamp;
                out_.PutNextEntry(outEntry);

                Stream data = in_.GetInputStream(inEntry);
                while ((num = data.Read(buffer, 0, buffer.Length)) > 0)
                {
                    out_.Write(buffer, 0, num);
                }
                out_.Flush();
            }
        }
Exemple #2
0
        /** Add the SHA1 of every file to the manifest, creating it if necessary. */
        private static Manifest addDigestsToManifest(JarFile jar)
        {
            Manifest   input  = jar.Manifest;
            Manifest   output = new Manifest();
            Attributes main   = output.MainAttributes;

            if (input != null)
            {
                main.AddAll(input.MainAttributes);
            }
            else
            {
                main.Add("Manifest-Version", "1.0");
                main.Add("Created-By", "1.0 (Android SignApk)");
            }

            byte[] buffer = new byte[4096];
            int    num;

            IEnumerable <JarEntry> jes;

            if (input == null)
            {
                jes = jar.OrderBy(j => j.Name);
            }
            else
            {
                var entries       = jar.ToDictionary(j => j.Name);
                var sortedEntries = new List <JarEntry>();
                foreach (var entry in input.Entries)
                {
                    sortedEntries.Add(entries[entry.Key]);
                }
                jes = sortedEntries;
            }

            foreach (JarEntry entry in jes)
            {
                HashAlgorithm md   = HashAlgorithm.Create("SHA1");
                String        name = entry.Name;
                if (!entry.IsDirectory && !name.Equals(JarFile.MANIFEST_NAME) &&
                    !name.Equals(CERT_SF_NAME) && !name.Equals(CERT_RSA_NAME) &&
                    !name.Equals(OTACERT_NAME) &&
                    (stripPattern == null ||
                     !stripPattern.IsMatch(name)))
                {
                    Stream data = jar.GetInputStream(entry);
                    while ((num = data.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        md.TransformBlock(buffer, 0, num, null, 0);
                    }
                    md.TransformFinalBlock(buffer, 0, 0);

                    Attributes attr = null;
                    if (input != null)
                    {
                        attr = input.GetAttributes(name);
                    }
                    attr = attr != null ? new Attributes(attr) : new Attributes();
                    attr.Add("SHA1-Digest", Convert.ToBase64String(md.Hash));
                    output.Entries.Add(name, attr);
                }
            }

            return(output);
        }