コード例 #1
0
        public static ErrorCode Compress(byte[] inputBytes,
                                         int inputOffset,
                                         int inputCount,
                                         byte[] outputBytes,
                                         int outputOffset,
                                         ref int outputCount)
        {
            if (inputBytes == null)
            {
                throw new ArgumentNullException("inputBytes");
            }

            if (inputOffset < 0 || inputOffset >= inputBytes.Length)
            {
                throw new ArgumentOutOfRangeException("inputOffset");
            }

            if (inputCount <= 0 || inputOffset + inputCount > inputBytes.Length)
            {
                throw new ArgumentOutOfRangeException("inputCount");
            }

            if (outputBytes == null)
            {
                throw new ArgumentNullException("outputBytes");
            }

            if (outputOffset < 0 || outputOffset >= outputBytes.Length)
            {
                throw new ArgumentOutOfRangeException("outputOffset");
            }

            if (outputCount <= 0 || outputOffset + outputCount > outputBytes.Length)
            {
                throw new ArgumentOutOfRangeException("outputCount");
            }

            var outputHandle = GCHandle.Alloc(outputBytes, GCHandleType.Pinned);
            var inputHandle  = GCHandle.Alloc(inputBytes, GCHandleType.Pinned);

            ErrorCode result;

            lock (_CompressWork)
            {
                if (_Is64Bit == true)
                {
                    long dummy = outputCount;
                    result = (ErrorCode)Native64.NativeCompress(inputHandle.AddrOfPinnedObject() + inputOffset,
                                                                inputCount,
                                                                outputHandle.AddrOfPinnedObject() + outputOffset,
                                                                ref dummy,
                                                                _CompressWork);
                    if (dummy < 0 || dummy > outputCount)
                    {
                        throw new InvalidOperationException("strange output count");
                    }
                    outputCount = (int)dummy;
                }
                else
                {
                    result = (ErrorCode)Native32.NativeCompress(inputHandle.AddrOfPinnedObject() + inputOffset,
                                                                inputCount,
                                                                outputHandle.AddrOfPinnedObject() + outputOffset,
                                                                ref outputCount,
                                                                _CompressWork);
                }
            }

            return(result);
        }