コード例 #1
0
ファイル: Algo.cs プロジェクト: marcussacana/CatSceneEditor
 internal static void CompressData(byte[] inData, out byte[] outData, int CompressLevel = 9)
 {
     using (MemoryStream outMemoryStream = new MemoryStream())
         using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, CompressLevel))
             using (Stream inMemoryStream = new MemoryStream(inData)) {
                 CopyStream(inMemoryStream, outZStream);
                 outZStream.Finish();
                 outData = outMemoryStream.ToArray();
             }
 }
コード例 #2
0
        /// <summary>
        /// Encrypt a stream with a given set of headers and write to an output stream. The caller is responsible for consistency and completeness
        /// of the headers. Headers that are not known until encryption and compression are added here.
        /// </summary>
        /// <param name="outputDocumentHeaders"></param>
        /// <param name="inputStream"></param>
        /// <param name="outputStream"></param>
        public void EncryptTo(Stream inputStream, Stream outputStream, AxCryptOptions options)
        {
            if (inputStream == null)
            {
                throw new ArgumentNullException("inputStream");
            }
            if (outputStream == null)
            {
                throw new ArgumentNullException("outputStream");
            }
            if (options.HasMask(AxCryptOptions.EncryptWithCompression) && options.HasMask(AxCryptOptions.EncryptWithoutCompression))
            {
                throw new ArgumentException("Invalid options, cannot specify both with and without compression.");
            }
            if (!options.HasMask(AxCryptOptions.EncryptWithCompression) && !options.HasMask(AxCryptOptions.EncryptWithoutCompression))
            {
                throw new ArgumentException("Invalid options, must specify either with or without compression.");
            }

            DocumentHeaders.IsCompressed = options.HasMask(AxCryptOptions.EncryptWithCompression);
            V2HmacCalculator      hmacCalculator   = new V2HmacCalculator(new SymmetricKey(DocumentHeaders.GetHmacKey()));
            V2HmacStream <Stream> outputHmacStream = V2HmacStream.Create(hmacCalculator, outputStream);

            CryptoStreamBase encryptingStream = New <CryptoStreamBase>().Initialize(V2AxCryptDataStream.Create(outputHmacStream), DocumentHeaders.DataCrypto().EncryptingTransform(), CryptoStreamMode.Write);

            DocumentHeaders.WriteStartWithHmac(outputHmacStream);
            if (DocumentHeaders.IsCompressed)
            {
                using (ZOutputStream deflatingStream = new ZOutputStream(encryptingStream, -1))
                {
                    deflatingStream.FlushMode = JZlib.Z_SYNC_FLUSH;
                    inputStream.CopyTo(deflatingStream);
                    deflatingStream.FlushMode = JZlib.Z_FINISH;
                    deflatingStream.Finish();

                    _plaintextLength           = deflatingStream.TotalIn;
                    _compressedPlaintextLength = deflatingStream.TotalOut;
                    encryptingStream.FinalFlush();
                    DocumentHeaders.WriteEndWithHmac(hmacCalculator, outputHmacStream, _plaintextLength, _compressedPlaintextLength);
                }
            }
            else
            {
                try
                {
                    _compressedPlaintextLength = _plaintextLength = StreamExtensions.CopyTo(inputStream, encryptingStream);
                    encryptingStream.FinalFlush();
                    DocumentHeaders.WriteEndWithHmac(hmacCalculator, outputHmacStream, _plaintextLength, _compressedPlaintextLength);
                }
                finally
                {
                    encryptingStream.Dispose();
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Compresses the given <paramref name="input" />
        /// </summary>
        /// <param name="input">The data to compress</param>
        /// <returns></returns>
        public static byte[] Compress(byte[] input)
        {
            var sourceStream = new MemoryStream();
            var stream       = new ZOutputStream(sourceStream, 1);

            // write zlib header
            stream.Write(input);
            stream.Finish();

            return(sourceStream.GetBuffer());
        }
コード例 #4
0
 public static void DecompressData(byte[] inData, out byte[] outData)
 {
     using (MemoryStream outMemoryStream = new MemoryStream())
         using (ZOutputStream outZStream = new ZOutputStream(outMemoryStream, -1))
             using (Stream inMemoryStream = new MemoryStream(inData))
             {
                 CopyStream(inMemoryStream, outZStream);
                 outZStream.Finish();
                 outData = outMemoryStream.ToArray();
             }
 }
コード例 #5
0
ファイル: Algo.cs プロジェクト: marcussacana/CatSceneEditor
        internal static void CompressData(Stream inData, Stream outData, int CompressLevel = 9)
        {
            MemoryStream  tmp        = new MemoryStream();
            ZOutputStream outZStream = new ZOutputStream(tmp, CompressLevel);

            CopyStream(inData, outZStream);
            outZStream.Finish();
            tmp.Position = 0;
            CopyStream(tmp, outData);
            outZStream.Close();
            tmp.Close();
        }
コード例 #6
0
        private static void EncryptWithCompressionInternal(V1DocumentHeaders outputDocumentHeaders, Stream inputStream, Stream encryptingStream)
        {
            using (ZOutputStream deflatingStream = new ZOutputStream(encryptingStream, -1))
            {
                deflatingStream.FlushMode = JZlib.Z_SYNC_FLUSH;
                inputStream.CopyTo(deflatingStream);
                deflatingStream.FlushMode = JZlib.Z_FINISH;
                deflatingStream.Finish();

                outputDocumentHeaders.UncompressedLength = deflatingStream.TotalIn;
                outputDocumentHeaders.PlaintextLength    = deflatingStream.TotalOut;
            }
        }
コード例 #7
0
        private static int CompressedLength(string text)
        {
            using (MemoryStream textStream = new MemoryStream(Encoding.UTF8.GetBytes(text)))
            {
                using (ZOutputStream deflatingStream = new ZOutputStream(Stream.Null, 9))
                {
                    deflatingStream.FlushMode = JZlib.Z_NO_FLUSH;
                    textStream.CopyTo(deflatingStream);
                    deflatingStream.FlushMode = JZlib.Z_FINISH;
                    deflatingStream.Finish();

                    int length = (int)deflatingStream.TotalOut;
                    return(length);
                }
            }
        }
コード例 #8
0
        public static void BuildPatchfile(string newDir, string oldDir, string outputFile, params IPatchedFile[] files)
        {
            using (var fs = File.Open(outputFile, FileMode.Create))
                using (var totalFileInMem = new MemoryStream())
                    using (var bw = new BinaryWriter(totalFileInMem))
                    {
                        bw.Write(Encoding.ASCII.GetBytes("WzPatch\x1A")); // 8
                        bw.Write((int)2);                                 // Version?, 12
                        var checksumOffset = bw.BaseStream.Position;
                        bw.Write((int)0);                                 // Checksum, 16

                        using (var compressedSubBlob = new MemoryStream())
                            using (var ds = new ZOutputStream(compressedSubBlob, 6))
                                using (var bw_ms = new BinaryWriter(ds))
                                //using (var zs = new GZipStream(compressedSubBlob, CompressionLevel.Optimal))
                                //using (var bw_ms = new BinaryWriter(zs))
                                {
                                    foreach (var filePatchLogic in files)
                                    {
                                        var fn = filePatchLogic.Filename.Replace(newDir, "").Replace(oldDir, "").Substring(1);

                                        bw_ms.Write(fn.ToCharArray());

                                        Console.WriteLine("Writing patch step of {0} ({1})", fn, filePatchLogic);
                                        filePatchLogic.WriteToFile(bw_ms);
                                    }

                                    bw_ms.Flush();
                                    //zs.Flush();
                                    ds.Finish();

                                    Console.WriteLine("Compressed data, flushing to main stream...");
                                    compressedSubBlob.WriteTo(totalFileInMem);
                                }

                        Console.WriteLine("All done, just working on CRC...");

                        bw.BaseStream.Position = checksumOffset + 4;
                        var checksum = CRC32.CalculateChecksumStream(totalFileInMem);
                        bw.BaseStream.Position = checksumOffset;
                        bw.Write(checksum);

                        Console.WriteLine("Flushing to file...");
                        // Copy it over to the file
                        totalFileInMem.WriteTo(fs);
                    }
        }
コード例 #9
0
ファイル: Main.cs プロジェクト: marcussacana/SpriteTranslator
        private byte[] CompressData(byte[] Output)
        {
            MemoryStream  Stream     = new MemoryStream();
            ZOutputStream Compressor = new ZOutputStream(Stream, 9);

            MemoryStream Data = new MemoryStream(Output);

            CopyStream(Data, Compressor);

            Data.Close();
            Compressor.Finish();

            byte[] Content = Stream.ToArray();

            Compressor.Close();
            Stream?.Close();

            return(Content);
        }
コード例 #10
0
        /**
         * Compresses the stream.
         * @param compressionLevel the compression level (0 = best speed, 9 = best compression, -1 is default)
         * @since   2.1.3
         */
        public void FlateCompress(int compressionLevel)
        {
            if (!Document.Compress)
            {
                return;
            }
            // check if the flateCompress-method has allready been
            if (compressed)
            {
                return;
            }
            this.compressionLevel = compressionLevel;
            if (inputStream != null)
            {
                compressed = true;
                return;
            }
            // check if a filter allready exists
            PdfObject filter = PdfReader.GetPdfObject(Get(PdfName.FILTER));

            if (filter != null)
            {
                if (filter.IsName())
                {
                    if (PdfName.FLATEDECODE.Equals(filter))
                    {
                        return;
                    }
                }
                else if (filter.IsArray())
                {
                    if (((PdfArray)filter).Contains(PdfName.FLATEDECODE))
                    {
                        return;
                    }
                }
                else
                {
                    throw new PdfException("Stream could not be compressed: filter is not a name or array.");
                }
            }
            // compress
            MemoryStream stream = new MemoryStream();
            var          zip    = new ZOutputStream(stream, compressionLevel);

            if (streamBytes != null)
            {
                streamBytes.WriteTo(zip);
            }
            else
            {
                zip.Write(bytes, 0, bytes.Length);
            }
            //zip.Close();
            zip.Finish();
            // update the object
            streamBytes = stream;
            bytes       = null;
            Put(PdfName.LENGTH, new PdfNumber(streamBytes.Length));
            if (filter == null)
            {
                Put(PdfName.FILTER, PdfName.FLATEDECODE);
            }
            else
            {
                PdfArray filters = new PdfArray(filter);
                filters.Add(PdfName.FLATEDECODE);
                Put(PdfName.FILTER, filters);
            }
            compressed = true;
        }