Exemple #1
0
        /// <summary>
        /// Decompresses buffer passed using LZF decompression algorithm.
        /// </summary>
        /// <param name="input">Input buffer to decompress.</param>
        /// <param name="decompressedSize">Actual original decompressed size, if known. In case
        /// size is not final, resizes output automatically.</param>
        /// <returns>Decompressed buffer.</returns>
        public static unsafe byte[] Decompress(NativeArray <byte> input, int decompressedSize = 0)
        {
            int inputCount  = input.Length;
            int outputCount = decompressedSize <= 0 ? input.Length << 1 : decompressedSize;
            var output      = new NativeArray <byte>(outputCount);
            int finalCount  = 0;

            byte[] result = null;

            try
            {
                finalCount = Decompress(input.GetPointer(), inputCount, output.GetPointer(), outputCount);

                while (finalCount == 0)
                {
                    output.Free();
                    outputCount <<= 1;
                    output        = new NativeArray <byte>(outputCount);
                    finalCount    = Decompress(input.GetPointer(), inputCount, output.GetPointer(), outputCount);
                }
            }
            finally
            {
                result = new byte[finalCount];
                if (finalCount > 0)
                {
                    Marshal.Copy((IntPtr)output.GetPointer(), result, 0, finalCount);
                }
                output.Free();
            }

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Compresses buffer passed using LZF compression algorithm.
        /// </summary>
        /// <param name="input">Input buffer to compress.</param>
        /// <returns>Compressed buffer.</returns>
        public static unsafe byte[] Compress(byte[] input)
        {
            if (input == null || input.Length == 0)
                return(new byte[0]);

            fixed(byte *inputPtr = input)
            {
                int inputCount  = input.Length;
                int outputCount = input.Length << 1;
                var output      = new NativeArray <byte>(outputCount);
                int finalCount  = 0;

                byte[] result = null;

                try
                {
                    finalCount = Compress(inputPtr, inputCount, output.GetPointer(), outputCount);

                    while (finalCount == 0)
                    {
                        output.Free();
                        outputCount <<= 1;
                        output        = new NativeArray <byte>(outputCount);
                        finalCount    = Compress(inputPtr, inputCount, output.GetPointer(), outputCount);
                    }
                }
                finally
                {
                    result = new byte[finalCount];
                    if (finalCount > 0)
                    {
                        Marshal.Copy((IntPtr)output.GetPointer(), result, 0, finalCount);
                    }
                    output.Free();
                }

                return(result);
            }
        }