示例#1
0
        /// <summary>
        /// 지정된 데이타를 압축한다.
        /// </summary>
        /// <param name="input">압축할 Data</param>
        /// <returns>압축된 Data</returns>
        public override byte[] Compress(byte[] input) {
            if(IsDebugEnabled)
                log.Debug(CompressorTool.SR.CompressStartMsg);

            // check input data
            if(input.IsZeroLength()) {
                if(IsDebugEnabled)
                    log.Debug(CompressorTool.SR.InvalidInputDataMsg);

                return CompressorTool.EmptyBytes;
            }

            byte[] output;

            using(var outStream = new MemoryStream(input.Length)) {
                using(var bz2 = new BZip2OutputStream(outStream, ZipLevel)) {
                    bz2.Write(input, 0, input.Length);
                }
                output = outStream.ToArray();
            }

            if(IsDebugEnabled)
                log.Debug(CompressorTool.SR.CompressResultMsg, input.Length, output.Length, output.Length / (double)input.Length);

            return output;
        }
示例#2
0
        public static void CompressToFile(string targetFilePath, MemoryStream streamData)
        {
            // Write compressed data length and compressed data to file
            FileStream fs = null;
            try
            {
                fs = new FileStream(targetFilePath, FileMode.Create);

                // Write compressed data length
                fs.Write(BitConverter.GetBytes(streamData.Length), 0, 4);

                // Compress data
                using (BZip2OutputStream gzs = new BZip2OutputStream(fs, true))
                    //ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(ms))
                {
                    fs = null;

                    gzs.Write(streamData.GetBuffer(), 0, (int)streamData.Length);
                }
            }
            finally
            {
                if (fs!=null)
                    fs.Dispose();
            }
        }
 public byte[] Compress(Stream inputStream)
 {
     using (var outputStream = new MemoryStream())
     using (var bzip2 = new BZip2OutputStream(outputStream))
     {
         inputStream.WriteTo(bzip2);
         return outputStream.ToArray();
     }
 }
示例#4
0
        // First 4 bytes stand for uncompressed data length
        // Everything else is compressed data

        public static byte[] Compress(byte[] buffer)
        {
            MemoryStream ms = new MemoryStream();

            // Write compressed data to memory stream
            using (BZip2OutputStream gzs = new BZip2OutputStream(ms, true))
            {
                gzs.Write(buffer, 0, buffer.Length);
            }

            int decompressedLength = buffer.Length;

            // Write compressed data length and compressed data to output buffer
            ms.Position = 0;

            var gz = new byte[ms.Length+4];
            Buffer.BlockCopy(BitConverter.GetBytes(decompressedLength), 0, gz, 0, 4);
            Buffer.BlockCopy(ms.GetBuffer(), 0, gz, 4, (int)ms.Length);
            return gz;
        }
        /// <summary>
        /// Creates a binary patch that can be used
        /// (by <see cref="Apply"/>) to transform <paramref name="oldData"/> into <paramref name="newData"/>.
        /// </summary>
        /// <param name="oldData">The original binary data.</param>
        /// <param name="newData">The new binary data.</param>
        /// <param name="output">A <see cref="Stream"/> to which the patch will be written.</param>
        public static void Create(byte[] oldData, byte[] newData, Stream output)
        {
            // check arguments
            if (oldData == null)
                throw new ArgumentNullException("oldData");
            if (newData == null)
                throw new ArgumentNullException("newData");
            if (output == null)
                throw new ArgumentNullException("output");
            if (!output.CanSeek)
                throw new ArgumentException("Output stream must be seekable.", "output");
            if (!output.CanWrite)
                throw new ArgumentException("Output stream must be writable.", "output");

            /* Header is
                0	8	 "BSDIFF40"
                8	8	length of bzip2ed ctrl block
                16	8	length of bzip2ed diff block
                24	8	length of new file */
            /* File is
                0	32	Header
                32	??	Bzip2ed ctrl block
                ??	??	Bzip2ed diff block
                ??	??	Bzip2ed extra block */
            byte[] header = new byte[c_headerSize];
            WriteInt64(c_fileSignature, header, 0); // "BSDIFF40"
            WriteInt64(0, header, 8);
            WriteInt64(0, header, 16);
            WriteInt64(newData.Length, header, 24);

            long startPosition = output.Position;
            output.Write(header, 0, header.Length);

            int[] I = SuffixSort(oldData);

            byte[] db = new byte[newData.Length];
            byte[] eb = new byte[newData.Length];

            int dblen = 0;
            int eblen = 0;

            using (BZip2OutputStream bz2Stream = new BZip2OutputStream(output, true) )
            {
                // compute the differences, writing ctrl as we go
                int scan = 0;
                int pos = 0;
                int len = 0;
                int lastscan = 0;
                int lastpos = 0;
                int lastoffset = 0;
                while (scan < newData.Length)
                {
                    int oldscore = 0;

                    for (int scsc = scan += len; scan < newData.Length; scan++)
                    {
                        len = Search(I, oldData, newData, scan, 0, oldData.Length, out pos);

                        for (; scsc < scan + len; scsc++)
                        {
                            if ((scsc + lastoffset < oldData.Length) && (oldData[scsc + lastoffset] == newData[scsc]))
                                oldscore++;
                        }

                        if ((len == oldscore && len != 0) || (len > oldscore + 8))
                            break;

                        if ((scan + lastoffset < oldData.Length) && (oldData[scan + lastoffset] == newData[scan]))
                            oldscore--;
                    }

                    if (len != oldscore || scan == newData.Length)
                    {
                        int s = 0;
                        int sf = 0;
                        int lenf = 0;
                        for (int i = 0; (lastscan + i < scan) && (lastpos + i < oldData.Length); )
                        {
                            if (oldData[lastpos + i] == newData[lastscan + i])
                                s++;
                            i++;
                            if (s * 2 - i > sf * 2 - lenf)
                            {
                                sf = s;
                                lenf = i;
                            }
                        }

                        int lenb = 0;
                        if (scan < newData.Length)
                        {
                            s = 0;
                            int sb = 0;
                            for (int i = 1; (scan >= lastscan + i) && (pos >= i); i++)
                            {
                                if (oldData[pos - i] == newData[scan - i])
                                    s++;
                                if (s * 2 - i > sb * 2 - lenb)
                                {
                                    sb = s;
                                    lenb = i;
                                }
                            }
                        }

                        if (lastscan + lenf > scan - lenb)
                        {
                            int overlap = (lastscan + lenf) - (scan - lenb);
                            s = 0;
                            int ss = 0;
                            int lens = 0;
                            for (int i = 0; i < overlap; i++)
                            {
                                if (newData[lastscan + lenf - overlap + i] == oldData[lastpos + lenf - overlap + i])
                                    s++;
                                if (newData[scan - lenb + i] == oldData[pos - lenb + i])
                                    s--;
                                if (s > ss)
                                {
                                    ss = s;
                                    lens = i + 1;
                                }
                            }

                            lenf += lens - overlap;
                            lenb -= lens;
                        }

                        for (int i = 0; i < lenf; i++)
                            db[dblen + i] = (byte) (newData[lastscan + i] - oldData[lastpos + i]);
                        for (int i = 0; i < (scan - lenb) - (lastscan + lenf); i++)
                            eb[eblen + i] = newData[lastscan + lenf + i];

                        dblen += lenf;
                        eblen += (scan - lenb) - (lastscan + lenf);

                        byte[] buf = new byte[8];
                        WriteInt64(lenf, buf, 0);
                        bz2Stream.Write(buf, 0, 8);

                        WriteInt64((scan - lenb) - (lastscan + lenf), buf, 0);
                        bz2Stream.Write(buf, 0, 8);

                        WriteInt64((pos - lenb) - (lastpos + lenf), buf, 0);
                        bz2Stream.Write(buf, 0, 8);

                        lastscan = scan - lenb;
                        lastpos = pos - lenb;
                        lastoffset = pos - scan;
                    }
                }
            }

            // compute size of compressed ctrl data
            long controlEndPosition = output.Position;
            WriteInt64(controlEndPosition - startPosition - c_headerSize, header, 8);

            // write compressed diff data
            using (BZip2OutputStream bz2Stream = new BZip2OutputStream(output, true))
            {
                bz2Stream.Write(db, 0, dblen);
            }

            // compute size of compressed diff data
            long diffEndPosition = output.Position;
            WriteInt64(diffEndPosition - controlEndPosition, header, 16);

            // write compressed extra data
            using (BZip2OutputStream bz2Stream = new BZip2OutputStream(output, true))
            {
                bz2Stream.Write(eb, 0, eblen);
            }

            // seek to the beginning, write the header, then seek back to end
            long endPosition = output.Position;
            output.Position = startPosition;
            output.Write(header, 0, header.Length);
            output.Position = endPosition;
        }
示例#6
0
        [Timeout(15 * 60*1000)] // 60*1000 = 1min
        public void BZ_Basic()
        {
            TestContext.WriteLine("Creating fodder file.");
            // select a random text string
            var line = TestStrings.ElementAt(this.rnd.Next(0, TestStrings.Count)).Value;
            int n = 4000 + this.rnd.Next(1000); // number of iters
            var fname = "Pippo.txt";
            // emit many many lines into a text file:
            using (var sw = new StreamWriter(File.Create(fname)))
            {
                for (int k=0; k < n; k++)
                {
                    sw.WriteLine(line);
                }
            }
            int crcOriginal = GetCrc(fname);
            int blockSize = 0;

            Func<Stream,Stream>[] getBzStream = {
                new Func<Stream,Stream>( s0 => {
                        var decorator = new Ionic.BZip2.BZip2OutputStream(s0, blockSize);
                        return decorator;
                    }),
                new Func<Stream,Stream>( s1 => {
                        var decorator = new Ionic.BZip2.ParallelBZip2OutputStream(s1, blockSize);
                        return decorator;
                    })
            };

            int[] blockSizes = { 1,2,3,4,5,6,7,8,9 };

            for (int k=0; k < getBzStream.Length; k++)
            {
                for (int m=0; m < blockSizes.Length; m++)
                {
                    blockSize = blockSizes[m];
                    var getStream = getBzStream[k];
                    var root = Path.GetFileNameWithoutExtension(fname);
                    var ext = Path.GetExtension(fname);
                    // compress into bz2
                    var bzFname = String.Format("{0}.{1}.blocksize{2}{3}.bz2",
                                                root,
                                                (k==0)?"SingleThread":"MultiThread",
                                                blockSize, ext);

                    TestContext.WriteLine("Compress cycle ({0},{1})", k,m);
                    TestContext.WriteLine("file {0}", bzFname);
                    using (var fs = File.OpenRead(fname))
                    {
                        using (var output = File.Create(bzFname))
                        {
                            using (var compressor = getStream(output))
                            {
                                CopyStream(fs, compressor);
                            }
                        }
                    }

                    TestContext.WriteLine("Decompress");
                    var decompressedFname = Path.GetFileNameWithoutExtension(bzFname);
                    using (Stream fs = File.OpenRead(bzFname),
                           output = File.Create(decompressedFname),
                           decompressor = new Ionic.BZip2.BZip2InputStream(fs))
                    {
                        CopyStream(decompressor, output);
                    }

                    TestContext.WriteLine("Check CRC");
                    int crcDecompressed = GetCrc(decompressedFname);
                    Assert.AreEqual<int>(crcOriginal, crcDecompressed,
                                         "CRC mismatch {0:X8} != {1:X8}",
                                         crcOriginal, crcDecompressed);
                    TestContext.WriteLine("");

                    // just for the sake of disk space economy:
                    File.Delete(decompressedFname);
                    File.Delete(bzFname);
                }
            }
        }
示例#7
0
        [Timeout(15 * 60 * 1000)] // 60*1000 = 1min
        public void BZ_Basic()
        {
            TestContext.WriteLine("Creating fodder file.");
            // select a random text string
            var line  = TestStrings.ElementAt(this.rnd.Next(0, TestStrings.Count)).Value;
            int n     = 4000 + this.rnd.Next(1000); // number of iters
            var fname = "Pippo.txt";

            // emit many many lines into a text file:
            using (var sw = new StreamWriter(File.Create(fname)))
            {
                for (int k = 0; k < n; k++)
                {
                    sw.WriteLine(line);
                }
            }
            int crcOriginal = GetCrc(fname);
            int blockSize   = 0;

            Func <Stream, Stream>[] getBzStream =
            {
                new Func <Stream, Stream>(s0 => {
                    var decorator = new Ionic.BZip2.BZip2OutputStream(s0,blockSize);
                    return(decorator);
                }),
                new Func <Stream, Stream>(s1 => {
                    var decorator = new Ionic.BZip2.ParallelBZip2OutputStream(s1,blockSize);
                    return(decorator);
                })
            };

            int[] blockSizes = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            for (int k = 0; k < getBzStream.Length; k++)
            {
                for (int m = 0; m < blockSizes.Length; m++)
                {
                    blockSize = blockSizes[m];
                    var getStream = getBzStream[k];
                    var root      = Path.GetFileNameWithoutExtension(fname);
                    var ext       = Path.GetExtension(fname);
                    // compress into bz2
                    var bzFname = String.Format("{0}.{1}.blocksize{2}{3}.bz2",
                                                root,
                                                (k == 0)?"SingleThread":"MultiThread",
                                                blockSize, ext);

                    TestContext.WriteLine("Compress cycle ({0},{1})", k, m);
                    TestContext.WriteLine("file {0}", bzFname);
                    using (var fs = File.OpenRead(fname))
                    {
                        using (var output = File.Create(bzFname))
                        {
                            using (var compressor = getStream(output))
                            {
                                CopyStream(fs, compressor);
                            }
                        }
                    }

                    TestContext.WriteLine("Decompress");
                    var decompressedFname = Path.GetFileNameWithoutExtension(bzFname);
                    using (Stream fs = File.OpenRead(bzFname),
                           output = File.Create(decompressedFname),
                           decompressor = new Ionic.BZip2.BZip2InputStream(fs))
                    {
                        CopyStream(decompressor, output);
                    }

                    TestContext.WriteLine("Check CRC");
                    int crcDecompressed = GetCrc(decompressedFname);
                    Assert.AreEqual <int>(crcOriginal, crcDecompressed,
                                          "CRC mismatch {0:X8} != {1:X8}",
                                          crcOriginal, crcDecompressed);
                    TestContext.WriteLine("");

                    // just for the sake of disk space economy:
                    File.Delete(decompressedFname);
                    File.Delete(bzFname);
                }
            }
        }
示例#8
0
            public BZ2Compressor([DefaultParameterValue(DEFAULT_COMPRESSLEVEL)]int compresslevel) {
                this.compresslevel = compresslevel;

                this.output = new MemoryStream();
                this.bz2Output = new BZip2OutputStream(this.output, true);
            }
示例#9
0
        public void BZ_StreamCopy()
        {
            var src = new MemoryStream(System.Text.Encoding.ASCII.GetBytes("Hello"));

            var ms = new MemoryStream();

            using (var output = new BZip2OutputStream(ms, true))
            {
                src.CopyTo(output);
            }

            Assert.IsTrue(ms.Length > 0);
            ms.Seek(0, SeekOrigin.Begin);

            using (var input = new BZip2InputStream(ms, true))
            {
                var dst = new MemoryStream();
                input.CopyTo(dst);

                Assert.AreEqual(dst.Length, src.Length);

                src.Position = 0;
                dst.Position = 0;

                while (src.Position != src.Length)
                {
                    Assert.AreEqual(src.ReadByte(), dst.ReadByte());
                }
            }
        }
示例#10
0
文件: Client.cs 项目: RSA-PM/RSA-PM
        byte[] EncodeMessage(byte[] recipient_pubkey, byte[] msgid, byte[] replyTo, string txt, byte[] prvkey, byte[] pubkey, out byte[] aes_key, out byte[] aes_iv)
        {
            if (replyTo == null)
                replyTo = new byte[16];

            var txtbuf = Encoding.UTF8.GetBytes(txt);
            var SignMessage = prvkey != null;
            byte[] hash = null;
            if (SignMessage)
            {
                using (var rsa = new RSACryptoServiceProvider())
                {
                    RSAParameters rsap;
                    Shared.LoadKey2(Shared.prvToPem(prvkey), null, out rsap);
                    rsa.ImportParameters(rsap);
                    using (var ms = new MemoryStream()) //sign
                    {
                        ms.Write(msgid, 0, msgid.Length);
                        ms.Write(replyTo, 0, replyTo.Length);
                        ms.WriteShort((short)txtbuf.Length);
                        ms.Write(txtbuf, 0, txtbuf.Length);
                        ms.WriteShort((short)pubkey.Length);
                        ms.Write(pubkey, 0, pubkey.Length);
                        ms.WriteShort((short)recipient_pubkey.Length);
                        ms.Write(recipient_pubkey, 0, recipient_pubkey.Length);
                        ms.Position = 0;
                        hash = rsa.SignData(ms, SHA512OID);
                    }
                }
            }

            byte[] c1;
            using (var ms1 = new MemoryStream())
            using (var ms = new BZip2OutputStream(ms1))
            {
                ms.Write(txtbuf, 0, txtbuf.Length);
                ms.Close();
                c1 = ms1.ToArray();
            }

            var compressText = c1.Length < txtbuf.Length;

            byte[] aesmsg;
            using (var aes = new RijndaelManaged())
            {
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (var encryptor = aes.CreateEncryptor())
                    using (CryptoStream sw2 = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        aes_key = aes.Key;
                        aes_iv = aes.IV;
                        sw2.WriteByte((Byte)((compressText ? 1 : 0) | (SignMessage ? 2 : 0)));
                        sw2.Write(msgid, 0, msgid.Length);
                        sw2.Write(replyTo, 0, replyTo.Length);
                        if (compressText)
                        {
                            sw2.WriteShort((short)c1.Length);
                            sw2.Write(c1, 0, c1.Length);
                        }
                        else
                        {
                            sw2.WriteShort((short)txtbuf.Length);
                            sw2.Write(txtbuf, 0, txtbuf.Length);
                        }
                        if (SignMessage)
                        {
                            sw2.WriteShort((short)pubkey.Length);
                            sw2.Write(pubkey, 0, pubkey.Length);
                            sw2.WriteShort((short)hash.Length);
                            sw2.Write(hash, 0, hash.Length);
                        }
                    }
                    msEncrypt.Flush();
                    aesmsg = msEncrypt.ToArray();
                }
            }
            return aesmsg;
        }
示例#11
0
 public override void SetBaseStream(Stream baseStream)
 {
     zipStream = new BZip2OutputStream(baseStream);
 }
示例#12
0
 public DotNetZipBZip2OutputStream(Stream inputStream)
 {
     zipStream = new BZip2OutputStream(inputStream);
 }