Ejemplo n.º 1
0
        public TextWriter GetTextWriter()
        {
            if (pw == null)
            {
                HashAlgorithm digest;
                try {
                    digest = HashAlgorithm.Create("MD5");
                } catch (TargetException e) {
                    throw new IOException("No MD5 implementation", e);
                }

                dos = new DigestOutputStream(new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write), digest);
                pw  = new TrapCloseTextWriter(this, dos);

                // Write the headers....
                String version = compatibleVersion ?? typeof(OutputFile).Assembly.GetName().Version.ToString();
                pw.WriteLine("/* " + CSharpCCGlobals.GetIdString(toolName, Path.GetFileName(file)) + " Version " + version + " */");
                if (options != null)
                {
                    pw.WriteLine("/* CSharpCCOptions:" + Options.GetOptionsString(options) + " */");
                }
            }

            return(pw);
        }
Ejemplo n.º 2
0
        public OutputFile(string file, String compatibleVersion, String[] options)
        {
            this.file = file;
            this.compatibleVersion = compatibleVersion;
            this.options = options;

            if (File.Exists(file)) {
                // Generate the checksum of the file, and compare with any value
                // stored
                // in the file.

                StreamReader br = new StreamReader(file);
                MD5 digest;
                try {
                    digest = MD5.Create("MD5");
                } catch (TargetInvocationException e) {
                    throw new IOException("No MD5 implementation", e);
                }

                DigestOutputStream digestStream = new DigestOutputStream(Stream.Null, digest);
                StreamWriter pw = new StreamWriter(digestStream);
                String line;
                String existingMD5 = null;
                while ((line = br.ReadLine()) != null) {
                    if (line.StartsWith(MD5_LINE_PART_1)) {
                        existingMD5 = line.Replace(MD5_LINE_PART_1q, "")
                            .Replace(MD5_LINE_PART_2q, "");
                    } else {
                        pw.WriteLine(line);
                    }
                }

                pw.Close();
                String calculatedDigest = ToHexString(digestStream.Hash());

                if (existingMD5 == null || !existingMD5.Equals(calculatedDigest)) {
                    // No checksum in file, or checksum differs.
                    needToWrite = false;

                    if (compatibleVersion != null) {
                        CheckVersion(file, compatibleVersion);
                    }

                    if (options != null) {
                        CheckOptions(file, options);
                    }

                } else {
                    // The file has not been altered since JavaCC created it.
                    // Rebuild it.
                    Console.Out.WriteLine("File \"" + Path.GetFileName(file) + "\" is being rebuilt.");
                    needToWrite = true;
                }
            } else {
                // File does not exist
                Console.Out.WriteLine("File \"" + Path.GetFileName(file) + "\" does not exist.  Will create one.");
                needToWrite = true;
            }
        }
Ejemplo n.º 3
0
        private void WriteTo(Stream os)
        {
            MessageDigest foot = Constants.newMessageDigest();
            var           dos  = new DigestOutputStream(os, foot);

            // Write the header.
            //
            var tmp = new byte[128];

            Array.Copy(SigDirc, 0, tmp, 0, SigDirc.Length);
            NB.encodeInt32(tmp, 4, /* version */ 2);
            NB.encodeInt32(tmp, 8, _entryCnt);
            dos.Write(tmp, 0, 12);

            // Write the individual file entries.
            //
            if (_lastModified <= 0)
            {
                // Write a new index, as no entries require smudging.
                //
                for (int i = 0; i < _entryCnt; i++)
                {
                    _sortedEntries[i].write(dos);
                }
            }
            else
            {
                int smudge_s  = (int)(_lastModified / 1000);
                int smudge_ns = ((int)(_lastModified % 1000)) * 1000000;
                for (int i = 0; i < _entryCnt; i++)
                {
                    DirCacheEntry e = _sortedEntries[i];
                    if (e.mightBeRacilyClean(smudge_s, smudge_ns))
                    {
                        e.smudgeRacilyClean();
                    }
                    e.write(dos);
                }
            }

            if (_cacheTree != null)
            {
                var bb = new LocalFileBuffer();
                _cacheTree.write(tmp, bb);
                bb.close();

                NB.encodeInt32(tmp, 0, ExtTree);
                NB.encodeInt32(tmp, 4, (int)bb.Length);
                dos.Write(tmp, 0, 8);
                bb.writeTo(dos, null);
            }
            var hash = foot.Digest();

            os.Write(hash, 0, hash.Length);
            os.Close();
        }
Ejemplo n.º 4
0
        private void WriteTo(Stream os)
        {
            MessageDigest foot = Constants.newMessageDigest();
            var           dos  = new DigestOutputStream(os, foot);

            // Write the header.
            //
            var tmp = new byte[128];

            Array.Copy(SigDirc, 0, tmp, 0, SigDirc.Length);
            NB.encodeInt32(tmp, 4, /* version */ 2);
            NB.encodeInt32(tmp, 8, _entryCnt);
            dos.Write(tmp, 0, 12);

            // Write the individual file entries.
            //
            if (_lastModified == DateTime.MinValue)
            {
                // Write a new index, as no entries require smudging.
                //
                for (int i = 0; i < _entryCnt; i++)
                {
                    _sortedEntries[i].write(dos);
                }
            }
            else
            {
                var smudge_s  = _lastModified.ToUnixTime();
                var smudge_ns = _lastModified.Millisecond * 1000000;                 // [henon] <--- this could be done with much more precision in C# since DateTime has 100 nanosec ticks
                for (int i = 0; i < _entryCnt; i++)
                {
                    DirCacheEntry e = _sortedEntries[i];
                    if (e.mightBeRacilyClean(smudge_s, smudge_ns))
                    {
                        e.smudgeRacilyClean();
                    }
                    e.write(dos);
                }
            }

            if (_cacheTree != null)
            {
                var bb = new TemporaryBuffer();
                _cacheTree.write(tmp, bb);
                bb.close();

                NB.encodeInt32(tmp, 0, ExtTree);
                NB.encodeInt32(tmp, 4, (int)bb.Length);
                dos.Write(tmp, 0, 8);
                bb.writeTo(dos, null);
            }
            var hash = foot.Digest();

            os.Write(hash, 0, hash.Length);
            os.Close();
        }
Ejemplo n.º 5
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void WriteTo(OutputStream os)
        {
            MessageDigest      foot = Constants.NewMessageDigest();
            DigestOutputStream dos  = new DigestOutputStream(os, foot);
            bool extended           = false;

            for (int i = 0; i < entryCnt; i++)
            {
                extended |= sortedEntries[i].IsExtended;
            }
            // Write the header.
            //
            byte[] tmp = new byte[128];
            System.Array.Copy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.Length);
            NB.EncodeInt32(tmp, 4, extended ? 3 : 2);
            NB.EncodeInt32(tmp, 8, entryCnt);
            dos.Write(tmp, 0, 12);
            // Write the individual file entries.
            //
            if (snapshot == null)
            {
                // Write a new index, as no entries require smudging.
                //
                for (int i_1 = 0; i_1 < entryCnt; i_1++)
                {
                    sortedEntries[i_1].Write(dos);
                }
            }
            else
            {
                int smudge_s  = (int)(snapshot.LastModified() / 1000);
                int smudge_ns = ((int)(snapshot.LastModified() % 1000)) * 1000000;
                for (int i_1 = 0; i_1 < entryCnt; i_1++)
                {
                    DirCacheEntry e = sortedEntries[i_1];
                    if (e.MightBeRacilyClean(smudge_s, smudge_ns))
                    {
                        e.SmudgeRacilyClean();
                    }
                    e.Write(dos);
                }
            }
            if (tree != null)
            {
                TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
                tree.Write(tmp, bb);
                bb.Close();
                NB.EncodeInt32(tmp, 0, EXT_TREE);
                NB.EncodeInt32(tmp, 4, (int)bb.Length());
                dos.Write(tmp, 0, 8);
                bb.WriteTo(dos, null);
            }
            os.Write(foot.Digest());
            os.Close();
        }
Ejemplo n.º 6
0
        /// <exception cref="System.IO.IOException"></exception>
        /// <exception cref="System.IO.FileNotFoundException"></exception>
        /// <exception cref="Sharpen.Error"></exception>
        private FilePath ToTemp(MessageDigest md, int type, long len, InputStream @is)
        {
            bool     delete = true;
            FilePath tmp    = NewTempFile();

            try
            {
                FileOutputStream fOut = new FileOutputStream(tmp);
                try
                {
                    OutputStream @out = fOut;
                    if (config.GetFSyncObjectFiles())
                    {
                        @out = Channels.NewOutputStream(fOut.GetChannel());
                    }
                    DeflaterOutputStream cOut = Compress(@out);
                    DigestOutputStream   dOut = new DigestOutputStream(cOut, md);
                    WriteHeader(dOut, type, len);
                    byte[] buf = Buffer();
                    while (len > 0)
                    {
                        int n = @is.Read(buf, 0, (int)Math.Min(len, buf.Length));
                        if (n <= 0)
                        {
                            throw ShortInput(len);
                        }
                        dOut.Write(buf, 0, n);
                        len -= n;
                    }
                    dOut.Flush();
                    cOut.Finish();
                }
                finally
                {
                    if (config.GetFSyncObjectFiles())
                    {
                        fOut.GetChannel().Force(true);
                    }
                    fOut.Close();
                }
                delete = false;
                return(tmp);
            }
            finally
            {
                if (delete)
                {
                    FileUtils.Delete(tmp);
                }
            }
        }
Ejemplo n.º 7
0
        /** Write a .SF file with a digest of the specified manifest. */
        private static void writeSignatureFile(Manifest manifest, Stream out_)
        {
            Manifest   sf   = new Manifest();
            Attributes main = sf.MainAttributes;

            main.Add("Signature-Version", "1.0");
            main.Add("Created-By", "1.0 (Android SignApk)");

            DigestOutputStream digestStream = new DigestOutputStream("SHA1");
            StreamWriter       print        = new StreamWriter(digestStream, new UTF8Encoding());

            // Digest of the entire manifest
            manifest.Write(digestStream);
            print.Flush();
            main.Add("SHA1-Digest-Manifest", Convert.ToBase64String(digestStream.Hash));

            IDictionary <String, Attributes> entries = manifest.Entries;

            foreach (var entry in entries)
            {
                // Digest of the manifest stanza for this entry.
                print.Write("Name: " + entry.Key + "\r\n");
                foreach (var att in entry.Value)
                {
                    print.Write(att.Key + ": " + att.Value + "\r\n");
                }
                print.Write("\r\n");
                print.Flush();

                Attributes sfAttr = new Attributes();
                sfAttr.Add("SHA1-Digest", Convert.ToBase64String(digestStream.Hash));
                sf.Entries.Add(entry.Key, sfAttr);
            }

            sf.Write(out_);

            // A bug in the java.util.jar implementation of Android platforms
            // up to version 1.6 will cause a spurious IOException to be thrown
            // if the length of the signature file is a multiple of 1024 bytes.
            // As a workaround, add an extra CRLF in this case.
            if ((out_.Length % 1024) == 0)
            {
                var b = Encoding.UTF8.GetBytes("\r\n");
                out_.Write(b, 0, b.Length);
            }
        }
Ejemplo n.º 8
0
 /// <summary>Create a new writer instance.</summary>
 /// <remarks>Create a new writer instance.</remarks>
 /// <param name="dst">
 /// the stream this instance outputs to. If not already buffered
 /// it will be automatically wrapped in a buffered stream.
 /// </param>
 protected internal PackIndexWriter(OutputStream dst)
 {
     @out = new DigestOutputStream(dst is BufferedOutputStream ? dst : new BufferedOutputStream
                                       (dst), Constants.NewMessageDigest());
     tmp = new byte[4 + Constants.OBJECT_ID_LENGTH];
 }
Ejemplo n.º 9
0
        public TextWriter GetTextWriter()
        {
            if (pw == null) {
                HashAlgorithm digest;
                try {
                    digest = HashAlgorithm.Create("MD5");
                } catch (TargetException e) {
                    throw new IOException("No MD5 implementation", e);
                }

                dos = new DigestOutputStream(new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write), digest);
                pw = new TrapCloseTextWriter(this, dos);

                // Write the headers....
                String version = compatibleVersion ?? typeof(OutputFile).Assembly.GetName().Version.ToString();
                pw.WriteLine("/* " + CSharpCCGlobals.GetIdString(toolName, Path.GetFileName(file)) + " Version " + version + " */");
                if (options != null) {
                    pw.WriteLine("/* CSharpCCOptions:" + Options.GetOptionsString(options) + " */");
                }
            }

            return pw;
        }
Ejemplo n.º 10
0
        /** Write a .SF file with a digest of the specified manifest. */
        private static void writeSignatureFile(Manifest manifest, Stream out_)
        {
            Manifest sf = new Manifest();
            Attributes main = sf.MainAttributes;
            main.Add("Signature-Version", "1.0");
            main.Add("Created-By", "1.0 (Android SignApk)");

            DigestOutputStream digestStream = new DigestOutputStream("SHA1");
            StreamWriter print = new StreamWriter(digestStream, new UTF8Encoding());

            // Digest of the entire manifest
            manifest.Write(digestStream);
            print.Flush();
            main.Add("SHA1-Digest-Manifest", Convert.ToBase64String(digestStream.Hash));

            IDictionary<String, Attributes> entries = manifest.Entries;
            foreach (var entry in entries)
            {
                // Digest of the manifest stanza for this entry.
                print.Write("Name: " + entry.Key + "\r\n");
                foreach (var att in entry.Value)
                {
                    print.Write(att.Key + ": " + att.Value + "\r\n");
                }
                print.Write("\r\n");
                print.Flush();

                Attributes sfAttr = new Attributes();
                sfAttr.Add("SHA1-Digest", Convert.ToBase64String(digestStream.Hash));
                sf.Entries.Add(entry.Key, sfAttr);
            }

            sf.Write(out_);

            // A bug in the java.util.jar implementation of Android platforms
            // up to version 1.6 will cause a spurious IOException to be thrown
            // if the length of the signature file is a multiple of 1024 bytes.
            // As a workaround, add an extra CRLF in this case.
            if ((out_.Length % 1024) == 0)
            {
                var b = Encoding.UTF8.GetBytes("\r\n");
                out_.Write(b, 0, b.Length);
            }
        }
Ejemplo n.º 11
0
        public OutputFile(string file, String compatibleVersion, String[] options)
        {
            this.file = file;
            this.compatibleVersion = compatibleVersion;
            this.options           = options;

            if (File.Exists(file))
            {
                // Generate the checksum of the file, and compare with any value
                // stored
                // in the file.

                StreamReader br = new StreamReader(file);
                MD5          digest;
                try {
                    digest = MD5.Create("MD5");
                } catch (TargetInvocationException e) {
                    throw new IOException("No MD5 implementation", e);
                }

                DigestOutputStream digestStream = new DigestOutputStream(Stream.Null, digest);
                StreamWriter       pw           = new StreamWriter(digestStream);
                String             line;
                String             existingMD5 = null;
                while ((line = br.ReadLine()) != null)
                {
                    if (line.StartsWith(MD5_LINE_PART_1))
                    {
                        existingMD5 = line.Replace(MD5_LINE_PART_1q, "")
                                      .Replace(MD5_LINE_PART_2q, "");
                    }
                    else
                    {
                        pw.WriteLine(line);
                    }
                }

                pw.Close();
                String calculatedDigest = ToHexString(digestStream.Hash());

                if (existingMD5 == null || !existingMD5.Equals(calculatedDigest))
                {
                    // No checksum in file, or checksum differs.
                    needToWrite = false;

                    if (compatibleVersion != null)
                    {
                        CheckVersion(file, compatibleVersion);
                    }

                    if (options != null)
                    {
                        CheckOptions(file, options);
                    }
                }
                else
                {
                    // The file has not been altered since JavaCC created it.
                    // Rebuild it.
                    Console.Out.WriteLine("File \"" + Path.GetFileName(file) + "\" is being rebuilt.");
                    needToWrite = true;
                }
            }
            else
            {
                // File does not exist
                Console.Out.WriteLine("File \"" + Path.GetFileName(file) + "\" does not exist.  Will create one.");
                needToWrite = true;
            }
        }
Ejemplo n.º 12
0
        /// <exception cref="System.IO.IOException"></exception>
        internal virtual void WriteTo(OutputStream os)
        {
            MessageDigest      foot = Constants.NewMessageDigest();
            DigestOutputStream dos  = new DigestOutputStream(os, foot);
            bool extended           = false;

            for (int i = 0; i < entryCnt; i++)
            {
                extended |= sortedEntries[i].IsExtended;
            }
            // Write the header.
            //
            byte[] tmp = new byte[128];
            System.Array.Copy(SIG_DIRC, 0, tmp, 0, SIG_DIRC.Length);
            NB.EncodeInt32(tmp, 4, extended ? 3 : 2);
            NB.EncodeInt32(tmp, 8, entryCnt);
            dos.Write(tmp, 0, 12);
            // Write the individual file entries.
            int smudge_s;
            int smudge_ns;

            if (myLock != null)
            {
                // For new files we need to smudge the index entry
                // if they have been modified "now". Ideally we'd
                // want the timestamp when we're done writing the index,
                // so we use the current timestamp as a approximation.
                myLock.CreateCommitSnapshot();
                snapshot  = myLock.GetCommitSnapshot();
                smudge_s  = (int)(snapshot.LastModified() / 1000);
                smudge_ns = ((int)(snapshot.LastModified() % 1000)) * 1000000;
            }
            else
            {
                // Used in unit tests only
                smudge_ns = 0;
                smudge_s  = 0;
            }
            // Check if tree is non-null here since calling updateSmudgedEntries
            // will automatically build it via creating a DirCacheIterator
            bool writeTree = tree != null;

            if (repository != null && entryCnt > 0)
            {
                UpdateSmudgedEntries();
            }
            for (int i_1 = 0; i_1 < entryCnt; i_1++)
            {
                DirCacheEntry e = sortedEntries[i_1];
                if (e.MightBeRacilyClean(smudge_s, smudge_ns))
                {
                    e.SmudgeRacilyClean();
                }
                e.Write(dos);
            }
            if (writeTree)
            {
                TemporaryBuffer bb = new TemporaryBuffer.LocalFile();
                tree.Write(tmp, bb);
                bb.Close();
                NB.EncodeInt32(tmp, 0, EXT_TREE);
                NB.EncodeInt32(tmp, 4, (int)bb.Length());
                dos.Write(tmp, 0, 8);
                bb.WriteTo(dos, null);
            }
            writeIndexChecksum = foot.Digest();
            os.Write(writeIndexChecksum);
            os.Close();
        }