Пример #1
0
 /// <summary>Decodes the specified input.</summary>
 /// <param name="input">The input.</param>
 /// <param name="inputOffset">The input offset.</param>
 /// <param name="inputLength">Length of the input.</param>
 /// <param name="output">The output.</param>
 /// <param name="outputOffset">The output offset.</param>
 /// <param name="outputLength">Length of the output.</param>
 /// <param name="knownOutputLength">Set it to <c>true</c> if output length is known.</param>
 /// <returns>Number of bytes written.</returns>
 public static int Decode(
     byte[] input,
     int inputOffset,
     int inputLength,
     byte[] output,
     int outputOffset,
     int outputLength       = 0,
     bool knownOutputLength = false)
 {
     return(Decoder.Decode(input, inputOffset, inputLength, output, outputOffset, outputLength, knownOutputLength));
 }
Пример #2
0
        /// <summary>Performs the quick auto-test on given compression service.</summary>
        /// <param name="service">The service.</param>
        /// <returns>A service or <c>null</c> if it failed.</returns>
        private static ILZ4Service AutoTest(ILZ4Service service)
        {
            const string loremIpsum =
                "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut " +
                "labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco " +
                "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in " +
                "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " +
                "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

            // generate some well-known array of bytes
            const string inputText = loremIpsum + loremIpsum + loremIpsum + loremIpsum + loremIpsum;
            var          original  = Encoding.UTF8.GetBytes(inputText);

            // LZ4 test
            {
                // compress it
                var encoded       = new byte[MaximumOutputLength(original.Length)];
                var encodedLength = service.Encode(original, 0, original.Length, encoded, 0, encoded.Length);
                if (encodedLength < 0)
                {
                    return(null);
                }

                // decompress it (knowing original length)
                var decoded        = new byte[original.Length];
                var decodedLength1 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, true);
                if (decodedLength1 != original.Length)
                {
                    return(null);
                }
                var outputText1 = Encoding.UTF8.GetString(decoded, 0, decoded.Length);
                if (outputText1 != inputText)
                {
                    return(null);
                }

                // decompress it (not knowing original length)
                var decodedLength2 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, false);
                if (decodedLength2 != original.Length)
                {
                    return(null);
                }
                var outputText2 = Encoding.UTF8.GetString(decoded, 0, decoded.Length);
                if (outputText2 != inputText)
                {
                    return(null);
                }
            }

            // LZ4HC
            {
                // compress it
                var encoded       = new byte[MaximumOutputLength(original.Length)];
                var encodedLength = service.EncodeHC(original, 0, original.Length, encoded, 0, encoded.Length);
                if (encodedLength < 0)
                {
                    return(null);
                }

                // decompress it (knowing original length)
                var decoded        = new byte[original.Length];
                var decodedLength1 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, true);
                if (decodedLength1 != original.Length)
                {
                    return(null);
                }
                var outputText1 = Encoding.UTF8.GetString(decoded, 0, decoded.Length);
                if (outputText1 != inputText)
                {
                    return(null);
                }

                // decompress it (not knowing original length)
                var decodedLength2 = service.Decode(encoded, 0, encodedLength, decoded, 0, decoded.Length, false);
                if (decodedLength2 != original.Length)
                {
                    return(null);
                }
                var outputText2 = Encoding.UTF8.GetString(decoded, 0, decoded.Length);
                if (outputText2 != inputText)
                {
                    return(null);
                }
            }

            return(service);
        }