Exemple #1
0
        /// <summary>
        /// Compress a stream using specified compression strength.
        /// </summary>
        /// <remarks>
        /// This returns a memory stream of the compressed results, if the incoming stream is
        /// very large this will consume a large amount memory.  In this case use the overload
        /// that takes the destination stream as a parameter instead.
        /// </remarks>
        /// <param name="source">The <see cref="Stream"/> to compress.</param>
        /// <param name="strength">The <see cref="CompressionStrength"/> of the compression.</param>
        /// <returns>Returns a <see cref="MemoryStream"/> of the compressed <see cref="Stream"/>.</returns>
        public static MemoryStream Compress(this Stream source, CompressionStrength strength)
        {
            MemoryStream destination = new MemoryStream();

            source.Compress(destination, strength, null);
            return(destination);
        }
Exemple #2
0
        /// <summary>
        /// Decompress a byte array.
        /// </summary>
        /// <param name="source">The <see cref="Byte"/> array to decompress.</param>
        /// <param name="length">The number of bytes to read into the byte array for compression.</param>
        /// <param name="startIndex">An <see cref="Int32"/> representing the start index of the byte array.</param>
        /// <returns>A decompressed <see cref="Byte"/> array.</returns>
        public static byte[] Decompress(this byte[] source, int startIndex, int length)
        {
            const byte CompressionStrengthMask = (byte)(Bits.Bit00 | Bits.Bit01);

            // Unmask compression strength from first buffer byte
            CompressionStrength strength = (CompressionStrength)(source[startIndex] & CompressionStrengthMask);

            if (strength == CompressionStrength.NoCompression)
            {
                // No compression was applied to original buffer, return specified portion of the buffer
                return(source.BlockCopy(startIndex + 1, length - 1));
            }
            else
            {
                // Create a new decompression deflater
                MemoryStream  compressedData   = new MemoryStream(source, startIndex + 1, length - 1);
                DeflateStream inflater         = new DeflateStream(compressedData, CompressionMode.Decompress);
                int           compressionDepth = (source[startIndex] & ~CompressionStrengthMask) >> 2;

                // Read uncompressed data
                byte[] destination = inflater.ReadStream();

                // When user requests muli-pass compression, there may be multiple compression passes on a buffer,
                // so we cycle through the needed uncompressions to get back to the original data
                if (strength == CompressionStrength.MultiPass && compressionDepth > 0)
                {
                    return(destination.Decompress());
                }
                else
                {
                    return(destination);
                }
            }
        }
Exemple #3
0
        // When user requests multi-pass compression, we allow multiple compression passes on a buffer because
        // this can often produce better compression results
        private static byte[] Compress(this byte[] source, int startIndex, int length, CompressionStrength strength, int compressionDepth)
        {
	        // zlib requests destination buffer to be 0.1% and 12 bytes larger than source stream...
            int destinationLength = length + (int)(length * 0.001) + 12;
	        byte[] destination = new byte[destinationLength];

            // Create a new zip deflater
            Deflater deflater = new Deflater(strength > CompressionStrength.BestCompression ? CompressionStrength.BestCompression : strength);

            deflater.SetInput(source, startIndex, length);
            deflater.Finish();
            destinationLength = deflater.Deflate(destination);
        
	        // Preprend compression depth and extract only used part of compressed buffer
	        byte[] outBuffer = new byte[++destinationLength];	
	        outBuffer[0] = (byte)compressionDepth;

	        for (int x = 1; x < destinationLength; x++)
		        outBuffer[x] = destination[x - 1];

            if (strength == CompressionStrength.MultiPass && destinationLength < length && compressionDepth < 256)
	        {
		        // See if another pass would help the compression...
                byte[] testBuffer = Compress(outBuffer, 0, outBuffer.Length, strength, compressionDepth + 1);

		        if (testBuffer.Length < outBuffer.Length)
			        return testBuffer;
		        else
			        return outBuffer;
	        }
	        else
		        return outBuffer;
        }
Exemple #4
0
 /// <summary>Compress the <paramref name="data" />.</summary>
 /// <param name="data">The uncompressed data.</param>
 /// <param name="level">Block size acts as compression level (1 to 9) with 1 giving
 /// the lowest compression and 9 the highest.</param>
 /// <returns>Returns the compressed data.</returns>
 public static byte[] Gzip(this byte[] data, CompressionStrength level = CompressionStrength.Best)
 {
     using (var ms = new MemoryStream())
     {
         GZip.GZip.Compress(new MemoryStream(data), ms, true, level);
         return(ms.ToArray());
     }
 }
Exemple #5
0
        /// <summary>
        /// Compress a file using specified compression strength (not PKZip compatible...)
        /// </summary>
        /// <param name="sourceFilename">Source file name.</param>
        /// <param name="destinationFilename">Destination file name.</param>
        /// <param name="strength">Stength of compression to apply.</param>
        /// <param name="progressHandler">User function to call with compression progress updates.</param>
        public static void Compress(String sourceFilename, String destinationFilename, CompressionStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            FileStream sourceFileStream = File.Open(sourceFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream destFileStream = File.Create(destinationFilename);

            sourceFileStream.Compress(destFileStream, strength, progressHandler);

            destFileStream.Flush();
            destFileStream.Close();
            sourceFileStream.Close();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="Deflater"/> class.
        /// </summary>
        /// <param name="level">
        /// the compression level, a value between NO_COMPRESSION
        /// and BEST_COMPRESSION.
        /// </param>
        /// <param name="noZlibHeaderOrFooter">
        /// true, if we should suppress the Zlib/RFC1950 header at the
        /// beginning and the adler checksum at the end of the output.  This is
        /// useful for the GZIP/PKZIP formats.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException">if lvl is out of range.</exception>
        public Deflater(CompressionStrength level, bool noZlibHeaderOrFooter)
        {
            if (level < 0 || (int)level > 9)
            {
                throw new ArgumentOutOfRangeException(nameof(level));
            }

            pending = new DeflaterPending();
            engine  = new DeflaterEngine(pending);
            this.noZlibHeaderOrFooter = noZlibHeaderOrFooter;
            Strategy = DeflateStrategy.Default;
            Strength = level;
            Reset();
        }
Exemple #7
0
        /// <summary>
        /// Compress <paramref name="instream">input stream</paramref> sending
        /// result to <paramref name="outputstream">output stream</paramref>
        /// </summary>
        /// <param name="outstream"></param>
        /// <param name="instream"></param>
        /// <param name="strength">Strngth of the Compression</param>
        /// <remarks>I had to change the Origial Compression Methode in a way that it woun't close
        /// the <paramref name="instream">input stream</paramref> and that it seeks to the Beginning
        /// of it on startup</remarks>
        protected static void Compress(Stream instream, Stream outstream, CompressionStrength strength)
        {
            System.IO.Stream bos = outstream;
            System.IO.Stream bis = instream;
            bis.Seek(0, System.IO.SeekOrigin.Begin);
            int ch = bis.ReadByte();

            ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream bzos = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(bos, (int)strength);
            while (ch != -1)
            {
                bzos.WriteByte((byte)ch);
                ch = bis.ReadByte();
            }
            bzos.Close();
        }
Exemple #8
0
        // When user requests multi-pass compression, we allow multiple compression passes on a buffer because
        // this can often produce better compression results
        private static byte[] Compress(this byte[] source, int startIndex, int length, CompressionStrength strength, int compressionDepth)
        {
            if (strength == CompressionStrength.NoCompression)
            {
                // No compression requested, return specified portion of the buffer
                byte[] outBuffer = new byte[++length];
                outBuffer[0] = (byte)strength;

                for (int x = 1; x < length; x++)
                    outBuffer[x] = source[startIndex + x - 1];

                return outBuffer;
            }
            else
            {
                // Create a new compression deflater
                MemoryStream compressedData = new MemoryStream();
                DeflateStream deflater = new DeflateStream(compressedData, CompressionMode.Compress);

                // Provide data for compression
                deflater.Write(source, startIndex, length);
                deflater.Close();

                byte[] destination = compressedData.ToArray();
                int destinationLength = destination.Length;

                // Preprend compression depth and extract only used part of compressed buffer
                byte[] outBuffer = new byte[++destinationLength];
                
                // First two bits are reserved for compression strength - this leaves 6 bits for a maximum of 64 compressions
                outBuffer[0] = (byte)((compressionDepth << 2) | (int)strength);

                for (int x = 1; x < destinationLength; x++)
                    outBuffer[x] = destination[x - 1];

                if (strength == CompressionStrength.MultiPass && destinationLength < length && compressionDepth < 64)
                {
                    // See if another pass would help the compression...
                    byte[] testBuffer = outBuffer.Compress(0, outBuffer.Length, strength, compressionDepth + 1);

                    if (testBuffer.Length < outBuffer.Length)
                        return testBuffer;
                    else
                        return outBuffer;
                }
                else
                    return outBuffer;
            }
        }
Exemple #9
0
        /// <summary>Compress a byte array using specified compression strength.</summary>
        public static byte[] Compress(this byte[] source, int startIndex, int length, CompressionStrength strength)
        {
	        return source.Compress(startIndex, length, strength, 0);
        }
Exemple #10
0
 /// <summary>Compress a byte array using specified compression strength.</summary>
 public static byte[] Compress(this byte[] source, CompressionStrength strength)
 {
     return source.Compress(0, source.Length, strength, 0);
 }
Exemple #11
0
        /// <summary>
        /// Compress the <paramref name="inStream">input stream</paramref> sending
        /// result data to <paramref name="outStream">output stream</paramref>.
        /// </summary>
        /// <param name="inStream">The readable stream to compress.</param>
        /// <param name="outStream">The output stream to receive the compressed data.</param>
        /// <param name="isStreamOwner">Both streams are closed on completion if true.</param>
        /// <param name="level">Block size acts as compression level (1 to 9) with 1 giving
        /// the lowest compression and 9 the highest.</param>
        public static void Compress(Stream inStream, Stream outStream, bool isStreamOwner, CompressionStrength level)
        {
            if (inStream == null || outStream == null)
            {
                throw new Exception("Null Stream");
            }

            try
            {
                using (var bzipOutput = new GZipOutputStream(outStream, level, 4096))
                {
                    bzipOutput.IsStreamOwner = isStreamOwner;
                    StreamUtils.Copy(inStream, bzipOutput, new byte[4096]);
                }
            }
            finally
            {
                if (isStreamOwner)
                {
                    // outStream is closed by the GZipOutputStream if stream owner
                    inStream.Dispose();
                }
            }
        }
Exemple #12
0
        /// <summary>
        /// Compress a file using specified compression strength (not PKZip compatible...)
        /// </summary>
        /// <param name="sourceFileName">Source file name.</param>
        /// <param name="destinationFileName">Destination file name.</param>
        /// <param name="strength">Stength of compression to apply.</param>
        /// <param name="progressHandler">User function to call with compression progress updates.</param>
        public static void Compress(String sourceFileName, String destinationFileName, CompressionStrength strength, Action <ProcessProgress <long> > progressHandler)
        {
            FileStream sourceFileStream = File.Open(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream destFileStream   = File.Create(destinationFileName);

            sourceFileStream.Compress(destFileStream, strength, progressHandler);

            destFileStream.Flush();
            destFileStream.Close();
            sourceFileStream.Close();
        }
Exemple #13
0
 /// <summary>
 /// Initializes a new instance of the server.
 /// </summary>
 protected ServerBase()
     : base()
 {
     m_serverID = Guid.NewGuid();
     m_clientIDs = new List<Guid>();
     m_textEncoding = Encoding.ASCII;
     m_currentState = ServerState.NotRunning;
     m_maxClientConnections = DefaultMaxClientConnections;
     m_handshake = DefaultHandshake;
     m_handshakeTimeout = DefaultHandshakeTimeout;
     m_sharedSecret = DefaultSharedSecret;
     m_encryption = DefaultEncryption;
     m_secureSession = DefaultSecureSession;
     m_receiveTimeout = DefaultReceiveTimeout;
     m_receiveBufferSize = DefaultReceiveBufferSize;
     m_compression = DefaultCompression;
     m_persistSettings = DefaultPersistSettings;
     m_settingsCategory = DefaultSettingsCategory;
 }
Exemple #14
0
        protected ServerBase()
		{
			// Setup the default values.
			m_receiveBufferSize = 8192;
			m_maximumClients = - 1;
			m_handshake = true;
			m_encryption = CipherStrength.None;
			m_compression = CompressionStrength.NoCompression;
			//m_crcCheck = CRCCheckType.None;
			m_enabled = true;
			m_textEncoding = System.Text.Encoding.ASCII;
			m_serverID = Guid.NewGuid(); // Create an ID for the server.
			m_clientIDs = new List<Guid>();
			m_settingsCategory = this.GetType().Name;			
			m_startTime = 0;
			m_stopTime = 0;
			m_buffer = new byte[m_receiveBufferSize];
		}
Exemple #15
0
 /// <summary>
 /// Compress a byte array using specified compression method.
 /// </summary>
 /// <param name="source">The <see cref="Byte"/> array to compress.</param>
 /// <param name="length">The number of bytes to read into the byte array for compression.</param>
 /// <param name="startIndex">An <see cref="Int32"/> representing the start index of the byte array.</param>
 /// <param name="strength">The specified <see cref="CompressionStrength"/>.</param>
 /// <returns>A compressed version of the source <see cref="Byte"/> array.</returns>
 public static byte[] Compress(this byte[] source, int startIndex, int length, CompressionStrength strength)
 {
     return(source.Compress(startIndex, length, strength, 0));
 }
Exemple #16
0
        /// <summary>
        /// Performs the necessary compression and encryption on the data contained in the <paramref name="buffer"/>.
        /// </summary>
        /// <param name="buffer">The buffer containing the data to be processed.</param>
        /// <param name="offset">The offset in the <paramref name="buffer"/> from where data is to be processed.</param>
        /// <param name="length">The length of data in <paramref name="buffer"/> starting at the <paramref name="offset"/>.</param>
        /// <param name="cryptoLevel">One of the <see cref="CipherStrength"/> values.</param>
        /// <param name="cryptoKey">The key to be used for encrypting the data in the <paramref name="buffer"/>.</param>
        /// <param name="compressLevel">One of the <see cref="CompressionStrength"/> values.</param>
        public static void ProcessTransmit(ref byte[] buffer, ref int offset, ref int length, CipherStrength cryptoLevel, string cryptoKey, CompressionStrength compressLevel)
        {
            if (compressLevel != CompressionStrength.NoCompression)
            {
                // Compress the data.
                buffer = new MemoryStream(buffer, offset, length).Compress(compressLevel).ToArray();
                offset = 0;
                length = buffer.Length;
            }

            if (cryptoLevel != CipherStrength.None)
            {
                // Encrypt the data.
                buffer = buffer.Encrypt(offset, length, Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
                offset = 0;
                length = buffer.Length;
            }

            //if (cryptoLevel == CipherStrength.None && compressLevel == CompressionStrength.NoCompression)
            //{
            //    if (length - offset == data.Length)
            //        return data;
            //    else
            //        return data.BlockCopy(offset, length);
            //}
            //else
            //{
            //    if (compressLevel != CompressionStrength.NoCompression)
            //    {
            //        // Compress the data.
            //        data = new MemoryStream(data, offset, length).Compress(compressLevel).ToArray();
            //        offset = 0;
            //        length = data.Length;
            //    }

            //    if (cryptoLevel != CipherStrength.None)
            //    {
            //        // Encrypt the data.
            //        data = data.Encrypt(offset, length, Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
            //    }

            //    return data;
            //}
        }
Exemple #17
0
 /// <summary>
 /// Creates a new deflater with given compression level.
 /// </summary>
 /// <param name="level">
 /// the compression level, a value between NO_COMPRESSION
 /// and BEST_COMPRESSION, or DEFAULT_COMPRESSION.
 /// </param>
 /// <exception cref="System.ArgumentOutOfRangeException">if lvl is out of range.</exception>
 public Deflater(CompressionStrength level)
     : this((int)level, false)
 {
     // JRC: I added this to take the CompressionLevel enumeration :)
 }
Exemple #18
0
 /// <summary>
 /// Compress a file using specified compression strength (not PKZip compatible...)
 /// </summary>
 /// <param name="sourceFilename">Source file name.</param>
 /// <param name="destinationFilename">Destination file name.</param>
 /// <param name="strength">Stength of compression to apply.</param>
 public static void Compress(String sourceFilename, String destinationFilename, CompressionStrength strength)
 {
     Compress(sourceFilename, destinationFilename, strength, null);
 }
Exemple #19
0
        private static bool Zip(string directoryPath, string outputFilePath, CompressionStrength strength)
        {
            EditorUtility.DisplayProgressBar("Creating .unitypackage", "Packing " + outputFilePath, 0f);

            var zipper = Get7zPath();

            outputFilePath = Path.GetFullPath(outputFilePath);

            if (File.Exists(outputFilePath))
            {
                File.Delete(outputFilePath);
            }

            int RunWith(string args)
            {
                if (DebugLog)
                {
                    Debug.Log(zipper + " " + args);
                }
                var processStartInfo = new ProcessStartInfo(zipper, args);

                // processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                if (directoryPath != null)
                {
                    processStartInfo.WorkingDirectory = directoryPath;
                }
                processStartInfo.UseShellExecute = DebugLog;
                processStartInfo.CreateNoWindow  = !DebugLog;
                var process = Process.Start(processStartInfo);

                process?.WaitForExit();
                var res = process?.ExitCode;

                if (res.HasValue && res == 0)
                {
                    return(res.Value);
                }
                if (DebugLog)
                {
                    Debug.LogError("Result: " + res + " for " + processStartInfo);
                }
                return(res ?? -1);
            }

            // Command line syntax:
            // https://sevenzip.osdn.jp/chm/cmdline/syntax.htm

            // Compression switches:
            // https://sevenzip.osdn.jp/chm/cmdline/switches/method.htm

            var files   = directoryPath + "/*";           // string.Join(" ", filenames.Select(f => "\"" + f.Replace("\\", "/") + "\"").ToArray());
            var tarPath = Path.GetDirectoryName(outputFilePath) + "/archtemp.tar";

            if (File.Exists(tarPath))
            {
                File.Delete(tarPath);
            }
            var args_tar = $"a -ttar \"{tarPath}\" \"{files}\"";

            EditorUtility.DisplayProgressBar("Creating .unitypackage", "Packing .tar file for " + outputFilePath, 0.1f);
            var code = RunWith(args_tar);

            if (code == 0)
            {
                EditorUtility.DisplayProgressBar("Creating .unitypackage", "Packing .tar.gz file for " + outputFilePath + ", compression: " + strength, 0.4f);
                var args_tgz = $"a -tgzip \"{outputFilePath}\" \"{tarPath}\" -mx" + (int)strength;
                if (File.Exists(outputFilePath))
                {
                    File.Delete(outputFilePath);
                }
                code = RunWith(args_tgz);
                if (DebugLog)
                {
                    Debug.Log(code);
                }
                var success = code == 0;
                if (success)
                {
                    File.Delete(tarPath);
                }
            }

            EditorUtility.DisplayProgressBar("Creating .unitypackage", "Done", 1f);
            EditorUtility.ClearProgressBar();
            return(code == 0);
        }
Exemple #20
0
 public static bool TryCreateTgz(string contentDirectory, string outputFilePath, CompressionStrength strength = CompressionStrength.Normal)
 {
     if (Zip(contentDirectory, outputFilePath, strength))
     {
         return(true);
     }
     return(false);
 }
Exemple #21
0
        /// <summary>
        /// Performs the necessary uncompression and decryption on the data contained in the <paramref name="buffer"/>.
        /// </summary>
        /// <param name="buffer">The buffer containing the data to be processed.</param>
        /// <param name="offset">The offset in the <paramref name="buffer"/> from where data is to be processed.</param>
        /// <param name="length">The length of data in <paramref name="buffer"/> starting at the <paramref name="offset"/>.</param>
        /// <param name="cryptoLevel">One of the <see cref="CipherStrength"/> values.</param>
        /// <param name="cryptoKey">The key to be used for decrypting the data in the <paramref name="buffer"/>.</param>
        /// <param name="compressLevel">One of the <see cref="CompressionStrength"/> values.</param>
        public static void ProcessReceived(ref byte[] buffer, ref int offset, ref int length, CipherStrength cryptoLevel, string cryptoKey, CompressionStrength compressLevel)
        {
            if (cryptoLevel != CipherStrength.None || compressLevel != CompressionStrength.NoCompression)
            {
                // Make a copy of the data to be processed.
                byte[] temp = buffer.BlockCopy(offset, length);

                if (cryptoLevel != CipherStrength.None)
                {
                    // Decrypt the data.
                    temp = temp.Decrypt(Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
                    offset = 0;
                    length = temp.Length;
                }
                
                if (compressLevel != CompressionStrength.NoCompression)
                {
                    // Uncompress the data.
                    temp = new MemoryStream(temp).Decompress().ToArray();
                    offset = 0;
                    length = temp.Length;
                }

                if (temp.Length > buffer.Length)
                    // Processed data cannot fit in the existing buffer.
                    buffer = temp;
                else
                    // Copy the processed data into the existing buffer.
                    Buffer.BlockCopy(temp, offset, buffer, offset, length);
            }

            //if (cryptoLevel == CipherStrength.None && compressLevel == CompressionStrength.NoCompression)
            //{
            //    if (length - offset == data.Length)
            //        return data;
            //    else
            //        return data.BlockCopy(offset, length);
            //}
            //else
            //{
            //    if (cryptoLevel != CipherStrength.None)
            //    {
            //        data = data.Decrypt(offset, length, Encoding.ASCII.GetBytes(cryptoKey), cryptoLevel);
            //        offset = 0;
            //        length = data.Length;
            //    }

            //    if (compressLevel != CompressionStrength.NoCompression)
            //    {
            //        data = new MemoryStream(data, offset, length).Decompress().ToArray();
            //    }

            //    return data;
            //}
        }
Exemple #22
0
        /// <summary>
        /// Writes pack file to given location.
        /// </summary>
        /// <param name="filePath"></param>
        public void Save(string filePath, CompressionStrength compression = CompressionStrength.Default)
        {
            var blankLength = this.Header.BlankLength;
            var fileCount   = this.Count;

            using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                using (var bw = new BinaryWriter(fs))
                {
                    var entryStarts = new Dictionary <IPackListEntry, long>();

                    // Header
                    bw.Write(this.Header.Signature);
                    bw.Write(this.Header.FormatVersion);
                    bw.Write(this.Header.PackVersion);
                    bw.Write(fileCount);
                    bw.Write(this.Header.FileTime1.ToFileTimeUtc());
                    bw.Write(this.Header.FileTime2.ToFileTimeUtc());
                    bw.Write(Encoding.UTF8.GetBytes(this.Header.BasePath.PadRight(480, '\0')));
                    bw.Write(fileCount);

                    var headerLengthsStart = bw.BaseStream.Position;
                    bw.Write(this.Header.ListLength);
                    bw.Write(blankLength);
                    bw.Write(this.Header.DataLength);
                    bw.Write(this.Header.Zero);

                    // List
                    var entryListStart = bw.BaseStream.Position;
                    foreach (var entry in _entries.Values)
                    {
                        bw.Write((byte)entry.NameType);

                        if (entry.NameType <= PackListNameType.L64)
                        {
                            var size  = (0x10 * ((byte)entry.NameType + 1));
                            var bytes = Encoding.UTF8.GetBytes(entry.RelativePath.PadRight(size - 1, '\0'));

                            bw.Write(bytes);
                        }
                        else if (entry.NameType == PackListNameType.L96)
                        {
                            var size  = 0x60;
                            var bytes = Encoding.UTF8.GetBytes(entry.RelativePath.PadRight(size - 1, '\0'));

                            bw.Write(bytes);
                        }
                        else if (entry.NameType == PackListNameType.LDyn)
                        {
                            var bytes = Encoding.UTF8.GetBytes(entry.RelativePath + '\0');

                            bw.Write(bytes.Length);
                            bw.Write(bytes);
                        }
                        else
                        {
                            throw new Exception("Unknown entry name type '" + entry.NameType + "'.");
                        }

                        bw.Write(entry.Seed);
                        bw.Write(entry.Zero);

                        entryStarts[entry] = bw.BaseStream.Position;
                        bw.Write(0);                 // DataOffset
                        bw.Write(0);                 // CompressedSize
                        bw.Write(0);                 // DecompressedSize

                        bw.Write(entry.IsCompressed ? 1 : 0);
                        bw.Write(entry.FileTime1.ToFileTimeUtc());
                        bw.Write(entry.FileTime2.ToFileTimeUtc());
                        bw.Write(entry.FileTime3.ToFileTimeUtc());
                        bw.Write(entry.FileTime4.ToFileTimeUtc());
                        bw.Write(entry.FileTime5.ToFileTimeUtc());
                    }

                    var entryListEnd    = bw.BaseStream.Position;
                    var entryListLength = (int)(entryListEnd - entryListStart);

                    bw.Write(new byte[blankLength]);

                    // Data
                    var dataOffset    = 0;
                    var dataListStart = bw.BaseStream.Position;
                    foreach (var entry in _entries.Values)
                    {
                        // Get data
                        var data             = entry.GetData();
                        var uncompressedSize = data.Length;

                        if (entry.IsCompressed)
                        {
                            // Compress data
                            var    dataStart = bw.BaseStream.Position;
                            byte[] compressed;

                            using (var ms = new MemoryStream())
                            {
                                var zlib = new ZOutputStream(ms, (int)compression);
                                zlib.Write(data, 0, data.Length);
                                zlib.finish();

                                compressed = ms.ToArray();
                            }

                            var mt = new MTRandom((entry.Seed << 7) ^ 0xA9C36DE1);
                            for (var i = 0; i < compressed.Length; ++i)
                            {
                                compressed[i] = (byte)(compressed[i] ^ mt.GetUInt32());
                            }

                            data = compressed;
                        }

                        bw.Write(data);

                        var dataEnd        = bw.BaseStream.Position;
                        var compressedSize = data.Length;
                        var listPos        = entryStarts[entry];

                        // Overwrite entry information
                        bw.BaseStream.Seek(listPos, SeekOrigin.Begin);
                        bw.Write(dataOffset);
                        bw.Write(compressedSize);
                        bw.Write(uncompressedSize);
                        bw.BaseStream.Seek(dataEnd, SeekOrigin.Begin);

                        dataOffset += compressedSize;
                    }

                    var dataListEnd    = bw.BaseStream.Position;
                    var dataListLength = (int)(dataListEnd - dataListStart);

                    bw.BaseStream.Seek(headerLengthsStart, SeekOrigin.Begin);
                    bw.Write(entryListLength + blankLength);
                    bw.Seek(4, SeekOrigin.Current);
                    bw.Write(dataListLength);
                    bw.BaseStream.Seek(dataListEnd, SeekOrigin.Begin);
                }
        }
Exemple #23
0
 /// <summary>
 /// Compress a byte array using specified compression method.
 /// </summary>
 /// <param name="source">The <see cref="Byte"/> array to compress.</param>
 /// <param name="strength">The specified <see cref="CompressionStrength"/>.</param>
 /// <returns>A compressed version of the source <see cref="Byte"/> array.</returns>
 public static byte[] Compress(this byte[] source, CompressionStrength strength)
 {
     return(source.Compress(0, source.Length, strength, 0));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="Deflater"/> class.
 /// </summary>
 /// <param name="level">
 /// the compression level, a value between 0..9.
 /// </param>
 /// <exception cref="ArgumentOutOfRangeException">if level is out of range.</exception>
 public Deflater(CompressionStrength level)
     : this(level, false)
 {
 }
Exemple #25
0
        // When user requests multi-pass compression, we allow multiple compression passes on a buffer because
        // this can often produce better compression results
        private static byte[] Compress(this byte[] source, int startIndex, int length, CompressionStrength strength, int compressionDepth)
        {
            if (strength == CompressionStrength.NoCompression)
            {
                // No compression requested, return specified portion of the buffer
                byte[] outBuffer = new byte[++length];
                outBuffer[0] = (byte)strength;

                for (int x = 1; x < length; x++)
                {
                    outBuffer[x] = source[startIndex + x - 1];
                }

                return(outBuffer);
            }
            else
            {
                // Create a new compression deflater
                MemoryStream  compressedData = new MemoryStream();
                DeflateStream deflater       = new DeflateStream(compressedData, CompressionMode.Compress);

                // Provide data for compression
                deflater.Write(source, startIndex, length);
                deflater.Close();

                byte[] destination       = compressedData.ToArray();
                int    destinationLength = destination.Length;

                // Preprend compression depth and extract only used part of compressed buffer
                byte[] outBuffer = new byte[++destinationLength];

                // First two bits are reserved for compression strength - this leaves 6 bits for a maximum of 64 compressions
                outBuffer[0] = (byte)((compressionDepth << 2) | (int)strength);

                for (int x = 1; x < destinationLength; x++)
                {
                    outBuffer[x] = destination[x - 1];
                }

                if (strength == CompressionStrength.MultiPass && destinationLength < length && compressionDepth < 64)
                {
                    // See if another pass would help the compression...
                    byte[] testBuffer = outBuffer.Compress(0, outBuffer.Length, strength, compressionDepth + 1);

                    if (testBuffer.Length < outBuffer.Length)
                    {
                        return(testBuffer);
                    }
                    else
                    {
                        return(outBuffer);
                    }
                }
                else
                {
                    return(outBuffer);
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="GZipOutputStream"/> class.
 /// </summary>
 /// <param name="baseOutputStream">The stream to read data (to be compressed) from.</param>
 /// <param name="level">The level.</param>
 /// <param name="size">Size of the buffer to use.</param>
 public GZipOutputStream(Stream baseOutputStream, CompressionStrength level = CompressionStrength.Best, int size = 4096)
     : base(baseOutputStream, new Deflater(level, true), size)
 {
 }
Exemple #27
0
        /// <summary>
        /// Compress a stream onto given output stream using specified compression strength.
        /// </summary>
        /// <param name="source">The <see cref="Stream"/> to compress.</param>
        /// <param name="strength">The <see cref="CompressionStrength"/> of the compression.</param>
        /// <param name="destination">The <see cref="Stream"/> destination.</param>
        /// <param name="progressHandler">The progress handler to check progress.</param>
        public static void Compress(this Stream source, Stream destination, CompressionStrength strength, Action <ProcessProgress <long> > progressHandler)
        {
            ProcessProgressHandler <long> progress = null;

            byte[] inBuffer = new byte[BufferSize];
            byte[] outBuffer;
            byte[] lengthBuffer;
            int    read;
            long   total = 0, length = -1;

            // Send initial progress event
            if ((object)progressHandler != null)
            {
                try
                {
                    if (source.CanSeek)
                    {
                        length = source.Length;
                    }
                }
                catch
                {
                    length = -1;
                }

                // Create a new progress handler to track compression progress
                progress          = new ProcessProgressHandler <long>(progressHandler, "Compress", length);
                progress.Complete = 0;
            }

            // Read initial buffer
            read = source.Read(inBuffer, 0, BufferSize);

            // Write compression version into stream
            byte[] version = new byte[1];
            version[0] = CompressionVersion;
            destination.Write(version, 0, 1);

            while (read > 0)
            {
                // Compress buffer - note that we are only going to compress used part of buffer,
                // we don't want any left over garbage to end up in compressed stream...
                outBuffer = inBuffer.Compress(0, read, strength);

                // The output stream is hopefully smaller than the input stream, so we prepend the final size of
                // each compressed buffer into the destination output stream so that we can safely uncompress
                // the stream in a "chunked" fashion later...
                lengthBuffer = BitConverter.GetBytes(outBuffer.Length);
                destination.Write(lengthBuffer, 0, lengthBuffer.Length);
                destination.Write(outBuffer, 0, outBuffer.Length);

                // Update compression progress
                if ((object)progressHandler != null)
                {
                    total            += read;
                    progress.Complete = total;
                }

                // Read next buffer
                read = source.Read(inBuffer, 0, BufferSize);
            }
        }
Exemple #28
0
        /// <summary>Compress a stream using specified compression strength</summary>
        /// <remarks>
        /// This returns a memory stream of the compressed results, if the incoming stream is
        /// very large this will consume a large amount memory.  In this case use the overload
        /// that takes the destination stream as a parameter instead.
        /// </remarks>
        public static MemoryStream Compress(this Stream source, CompressionStrength strength)
        {
	        MemoryStream destination = new MemoryStream();
	        Compress(source, destination, strength, null);
	        return destination;
        }
Exemple #29
0
 /// <summary>
 /// Compress a file using specified compression strength (not PKZip compatible...)
 /// </summary>
 /// <param name="sourceFileName">Source file name.</param>
 /// <param name="destinationFileName">Destination file name.</param>
 /// <param name="strength">Stength of compression to apply.</param>
 public static void Compress(String sourceFileName, String destinationFileName, CompressionStrength strength)
 {
     Compress(sourceFileName, destinationFileName, strength, null);
 }
Exemple #30
0
        /// <summary>Compress a stream onto given output stream using specified compression strength.</summary>
        public static void Compress(this Stream source, Stream destination, CompressionStrength strength, Action<ProcessProgress<long>> progressHandler)
        {
            ProcessProgress<long>.Handler progress = null;
            byte[] inBuffer = new byte[BufferSize];
	        byte[] outBuffer;
	        byte[] lengthBuffer;
	        int read;
	        long total = 0, length = -1;

	        // Send initial progress event
	        if (progressHandler != null)
	        {
		        try
		        {
			        if (source.CanSeek)
				        length = source.Length;
		        }
		        catch
		        {
			        length = -1;
		        }

                // Create a new progress handler to track compression progress
                progress = new ProcessProgress<long>.Handler(progressHandler, "Compress", length);
                progress.Complete = 0;
            }

	        // Read initial buffer
	        read = source.Read(inBuffer, 0, BufferSize);

	        // Write compression version into stream
	        byte[] version = new byte[1];
	        version[0] = CompressionVersion;
	        destination.Write(version, 0, 1);

	        while (read > 0)
	        {
		        // Compress buffer - note that we are only going to compress used part of buffer,
		        // we don't want any left over garbage to end up in compressed stream...
			    outBuffer = Compress(inBuffer, 0, read, strength);

		        // The output stream is hopefully smaller than the input stream, so we prepend the final size of
		        // each compressed buffer into the destination output stream so that we can safely uncompress
		        // the stream in a "chunked" fashion later...
		        lengthBuffer = BitConverter.GetBytes(outBuffer.Length);
		        destination.Write(lengthBuffer, 0, lengthBuffer.Length);
		        destination.Write(outBuffer, 0, outBuffer.Length);

		        // Update compression progress
		        if (progressHandler != null)
		        {
			        total += read;
			        progress.Complete = total;
		        }

		        // Read next buffer
		        read = source.Read(inBuffer, 0, BufferSize);
	        }
        }
Exemple #31
0
        /// <summary>
        /// Initializes a new instance of the client.
        /// </summary>
        protected ClientBase()
            : base()
        {
            m_serverID = Guid.Empty;
            m_clientID = Guid.NewGuid();
            m_textEncoding = Encoding.ASCII;
            m_currentState = ClientState.Disconnected;
            m_maxConnectionAttempts = DefaultMaxConnectionAttempts;
            m_handshake = DefaultHandshake;
            m_handshakeTimeout = DefaultHandshakeTimeout;
            m_sharedSecret = DefaultSharedSecret;
            m_encryption = DefaultEncryption;
            m_secureSession = DefaultSecureSession;
            m_receiveTimeout = DefaultReceiveTimeout;
            m_receiveBufferSize = DefaultReceiveBufferSize;
            m_compression = DefaultCompression;
            m_persistSettings = DefaultPersistSettings;
            m_settingsCategory = DefaultSettingsCategory;

        }
Exemple #32
0
        protected ClientBase()
		{
			// Setup the default values.
			m_receiveBufferSize = 8192;
			m_receiveTimeout = - 1;
			m_maximumConnectionAttempts = - 1;
			m_textEncoding = Encoding.ASCII;
			m_handshake = true;
			m_encryption = CipherStrength.None;
			m_compression = CompressionStrength.NoCompression;
			//m_crcCheck = CRCCheckType.None;
			m_enabled = true;
			m_clientID = Guid.NewGuid();
			m_settingsCategory = this.GetType().Name;			
			m_connectionWaitHandle = new System.Threading.ManualResetEvent(false);
			m_buffer = new byte[m_receiveBufferSize];
		}