コード例 #1
0
        /// <summary>
        /// Wrapper around Oodle Kraken Decompress. Decompresses an inputBuffer to an outputBuffer of correct size
        /// </summary>
        /// <param name="inputBuffer"></param>
        /// <param name="outputBuffer"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public static unsafe int Decompress(byte[] inputBuffer, byte[] outputBuffer)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
#pragma warning disable 162

                if (true)
                {
                    var inputHandle   = GCHandle.Alloc(inputBuffer, GCHandleType.Pinned);
                    var inputAddress  = inputHandle.AddrOfPinnedObject();
                    var outputHandle  = GCHandle.Alloc(outputBuffer, GCHandleType.Pinned);
                    var outputAddress = outputHandle.AddrOfPinnedObject();



                    var r = OodleLoadLib.OodleLZ_Decompress(inputAddress,
                                                            outputAddress,
                                                            inputBuffer.Length,
                                                            outputBuffer.Length
                                                            );

                    inputHandle.Free();
                    outputHandle.Free();

                    return(r);
                }

                return(OodleNative.OodleLZ_Decompress(
                           inputBuffer,
                           inputBuffer.Length,
                           outputBuffer,
                           outputBuffer.Length,
                           OodleNative.OodleLZ_FuzzSafe.No, OodleNative.OodleLZ_CheckCRC.No,
                           OodleNative.OodleLZ_Verbosity.None,
                           0,
                           0,
                           0,
                           0,
                           0,
                           0,
                           OodleNative.OodleLZ_Decode.Unthreaded));

#pragma warning restore 162
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return(OozNative.Kraken_Decompress(inputBuffer, inputBuffer.Length, outputBuffer,
                                                   outputBuffer.Length));
            }
            else
            {
                throw new NotImplementedException();
            }
        }
コード例 #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="inputBuffer"></param>
 /// <param name="outputBuffer"></param>
 /// <returns></returns>
 /// <exception cref="NotImplementedException"></exception>
 public static int Decompress(byte[] inputBuffer, byte[] outputBuffer)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         return(OodleNative.OodleLZ_Decompress(inputBuffer, inputBuffer.Length, outputBuffer,
                                               outputBuffer.Length, OodleNative.OodleLZ_FuzzSafe.No, OodleNative.OodleLZ_CheckCRC.No,
                                               OodleNative.OodleLZ_Verbosity.None, 0, 0, 0, 0, 0, 0, OodleNative.OodleLZ_Decode.Unthreaded));
     }
     else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
     {
         return(OozNative.Kraken_Decompress(inputBuffer, inputBuffer.Length, outputBuffer,
                                            outputBuffer.Length));
     }
     else
     {
         throw new NotImplementedException();
     }
 }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="inputBytes"></param>
        /// <param name="inputOffset"></param>
        /// <param name="inputCount"></param>
        /// <param name="outputBytes"></param>
        /// <param name="outputOffset"></param>
        /// <param name="outputCount"></param>
        /// <param name="algo"></param>
        /// <param name="level"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="NotImplementedException"></exception>
        public static int Compress(
            byte[] inputBytes,
            // int inputOffset,
            int inputCount,
            ref IEnumerable <byte> outputBuffer,
            // int outputOffset,
            // int outputCount,
            OodleNative.OodleLZ_Compressor algo   = OodleNative.OodleLZ_Compressor.Kraken,
            OodleNative.OodleLZ_Compression level = OodleNative.OodleLZ_Compression.Normal)
        {
            if (inputBytes == null)
            {
                throw new ArgumentNullException(nameof(inputBytes));
            }
            if (inputCount <= 0 || inputCount > inputBytes.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(inputCount));
            }
            if (outputBuffer == null)
            {
                throw new ArgumentNullException(nameof(outputBuffer));
            }

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var compressedBufferSizeNeeded = OodleHelper.GetCompressedBufferSizeNeeded((int)inputCount);
                var compressedBuffer           = new byte[compressedBufferSizeNeeded];

                var inputHandle   = GCHandle.Alloc(inputBytes, GCHandleType.Pinned);
                var inputAddress  = inputHandle.AddrOfPinnedObject();
                var outputHandle  = GCHandle.Alloc(compressedBuffer, GCHandleType.Pinned);
                var outputAddress = outputHandle.AddrOfPinnedObject();

                var result = (int)OodleNative.Compress(
                    (int)algo,
                    inputAddress,
                    inputCount,
                    outputAddress,
                    (int)level,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    IntPtr.Zero,
                    (long)0
                    );

                inputHandle.Free();
                outputHandle.Free();

                //resize buffer
                var writelist = new List <byte>()
                {
                    0x4B, 0x41, 0x52, 0x4B  //KARK, TODO: make this dependent on the compression algo
                };
                writelist.AddRange(BitConverter.GetBytes(inputCount));
                writelist.AddRange(compressedBuffer.Take(result));

                outputBuffer = writelist;

                return(outputBuffer.Count());
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                //TODO: use ooz to compress
                outputBuffer = inputBytes;
                return(outputBuffer.Count());


                // try
                // {
                //     var result = OozNative.Compress(
                //         (int)algo,
                //         inputAddress,
                //         outputAddress,
                //         inputCount,
                //         IntPtr.Zero,
                //         IntPtr.Zero
                //     );
                //     inputHandle.Free();
                //     outputHandle.Free();
                //     return result;
                // }
                // catch (Exception e)
                // {
                //     Console.WriteLine(e);
                //     throw;
                // }
            }
            else
            {
                throw new NotImplementedException();
            }
        }