Exemplo n.º 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;
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 3
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;
        }
Exemplo n.º 4
0
        /// <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;
        }
Exemplo n.º 5
0
        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;
        }