예제 #1
0
 public void LowBitRandom()
 {
     fixed(byte *inputPtr = highRepeatRandomInput)
     fixed(byte *encodedOutputPtr = highRepeatEncodedOutput)
     fixed(byte *outputPtr        = highRepeatRandomOutput)
     {
         int compressedSize   = LZ4.Encode64(inputPtr, encodedOutputPtr, highRepeatRandomInput.Length, highRepeatEncodedOutput.Length);
         int uncompressedSize = LZ4.Decode64(encodedOutputPtr, compressedSize, outputPtr, highRepeatRandomInput.Length, true);
     }
 }
예제 #2
0
        public void Setup()
        {
            var generator = new Random(RandomSeed);

            _buffers = new List <Tuple <byte[], int> >();

            // Generate the precomputed sequences to be used when generating data.
            var sequences = new List <byte[]>();

            for (int i = 0; i < NumberOfSequences; i++)
            {
                int length   = generator.Next(1, GeneratedSequenceMaximumLength);
                var sequence = new byte[length];
                generator.NextBytes(sequence);
                sequences.Add(sequence);
            }

            // Compute the length of the maximum output data. This is an upper bound
            // to be able to always use the same buffer for decompression.
            int maximumOutputLength = (int)LZ4.MaximumOutputLength(DataMaximumLength);

            _lz4Buffer = new byte[maximumOutputLength];

            var buffer = new byte[DataMaximumLength];

            for (int i = 0; i < NumberOfOperations; i++)
            {
                var        generatedDataLength = generator.Next(DataMaximumLength);
                List <int> usedSequences       = new List <int>();
                for (var j = 0; j < generatedDataLength; j++)
                {
                    bool useSequence = generator.NextDouble() < SequenceUsageProbability;
                    if (sequences.Count > 0 && useSequence)
                    {
                        byte[] sequence;
                        bool   repeatSequence = generator.NextDouble() < SequenceRepetitionProbability;
                        if (repeatSequence && usedSequences.Count > 0)
                        {
                            int index = generator.Next(usedSequences.Count);
                            sequence = sequences[usedSequences[index]];
                        }
                        else
                        {
                            int index = generator.Next(sequences.Count);
                            sequence = sequences[index];
                            usedSequences.Add(index);
                        }

                        fixed(byte *bufferPtr = &buffer[j])
                        fixed(byte *sequencePtr = sequence)
                        {
                            int amount = Math.Min(sequence.Length, generatedDataLength - j);

                            Unsafe.CopyBlock(bufferPtr, sequencePtr, (uint)amount);
                            j += amount;
                        }
                    }
                    else
                    {
                        var spontaneousSequenceLength = Math.Min(generator.Next(GeneratedSequenceMaximumLength), generatedDataLength - j);
                        for (int k = 0; k < spontaneousSequenceLength; k++, j++)
                        {
                            buffer[j] = (byte)generator.Next(256);
                        }
                    }
                }

                // Flip bytes on the generated sequence, as required
                bool flipGeneratedSequence = generator.NextDouble() < DataFlipProbability;
                if (flipGeneratedSequence)
                {
                    for (var j = 0; j < generatedDataLength; j++)
                    {
                        bool flipGeneratedByte = generator.NextDouble() < DataByteFlipProbability;
                        if (flipGeneratedByte)
                        {
                            buffer[j] ^= (byte)generator.Next(256);
                        }
                    }
                }

                // Calculate compression size and store the generated data
                fixed(byte *bufferPtr = buffer)
                fixed(byte *lz4buffer = _lz4Buffer)
                {
                    int compressedSize = LZ4.Encode64(bufferPtr, lz4buffer, generatedDataLength, _lz4Buffer.Length);

                    byte[] unmanagedBuffer = new byte[compressedSize];
                    fixed(byte *ptr = unmanagedBuffer)
                    {
                        Unsafe.CopyBlock(ptr, lz4buffer, (uint)compressedSize);
                    }

                    _buffers.Add(new Tuple <byte[], int>(unmanagedBuffer, generatedDataLength));
                }
            }
        }