Example #1
0
        public void TryToFillSearchBufferBasedOnLz77Token(Lz77Buffer lz77Buffer, Lz77Token lz77Token)
        {
            if (lz77Buffer == null)
            {
                throw new ArgumentNullException(nameof(lz77Buffer));
            }

            if (lz77Token == null)
            {
                throw new ArgumentNullException(nameof(lz77Token));
            }

            for (int index = 0; index < lz77Token.Length + 1; index++)
            {
                if (index > lz77Buffer.LookAheadBuffer.Count - 1)
                {
                    break;
                }

                if (lz77Buffer.SearchBuffer.Count >= lz77Buffer.SearchBuffer.Capacity)
                {
                    lz77Buffer.SearchBuffer.RemoveAt(0);
                }

                lz77Buffer.SearchBuffer.Add(lz77Buffer.LookAheadBuffer[index]);
            }
        }
Example #2
0
        public static Lz77Buffer GetLz77Buffer6()
        {
            var lz77Buffer = new Lz77Buffer(4, 4);

            lz77Buffer.LookAheadBuffer.Add(21);
            lz77Buffer.LookAheadBuffer.Add(22);
            lz77Buffer.LookAheadBuffer.Add(23);
            lz77Buffer.LookAheadBuffer.Add(24);
            lz77Buffer.LookAheadBuffer.Add(25);
            lz77Buffer.LookAheadBuffer.Add(26);
            lz77Buffer.LookAheadBuffer.Add(27);
            lz77Buffer.LookAheadBuffer.Add(28);
            lz77Buffer.LookAheadBuffer.Add(31);
            lz77Buffer.SearchBuffer.Add(20);
            lz77Buffer.SearchBuffer.Add(21);
            lz77Buffer.SearchBuffer.Add(22);
            lz77Buffer.SearchBuffer.Add(23);
            lz77Buffer.SearchBuffer.Add(24);
            lz77Buffer.SearchBuffer.Add(25);
            lz77Buffer.SearchBuffer.Add(26);
            lz77Buffer.SearchBuffer.Add(27);
            lz77Buffer.SearchBuffer.Add(28);

            return(lz77Buffer);
        }
Example #3
0
        public static Lz77Buffer GetLz77Buffer1()
        {
            var lz77Buffer = new Lz77Buffer(2, 2);

            lz77Buffer.LookAheadBuffer.Add(20);
            lz77Buffer.LookAheadBuffer.Add(21);

            return(lz77Buffer);
        }
        public void Setup()
        {
            lz77BufferManager = new Lz77BufferManager();
            lz77Buffer        = Constants.GetLz77Buffer1();
            lz77Token         = Constants.ExpectedTokenForLz77Buffer1();
            fileReaderMock    = new Mock <IFileReader>();

            SetupFileReaderMock();
        }
        public void ConstructorSetsLookAheadBufferAsExpected()
        {
            var lz77Buffer = new Lz77Buffer(offset, length);

            var lookAheadBuffer = lz77Buffer.LookAheadBuffer;

            Assert.IsNotNull(lookAheadBuffer);
            Assert.AreEqual(0, lookAheadBuffer.Count);
            Assert.AreEqual(Math.Pow(2, length) - 1, lookAheadBuffer.Capacity);
        }
        public void ConstructorSetsSearchBufferAsExpected()
        {
            var lz77Buffer = new Lz77Buffer(offset, length);

            var searchBuffer = lz77Buffer.SearchBuffer;

            Assert.IsNotNull(searchBuffer);
            Assert.AreEqual(0, searchBuffer.Count);
            Assert.AreEqual(Math.Pow(2, offset) - 1, searchBuffer.Capacity);
        }
Example #7
0
        public Lz77Encoder(ILz77TokenExtractor lz77TokenExtractor, ILz77BufferManager lz77BufferManager, ILz77TokenWriter lz77TokenWriter)
        {
            this.lz77TokenExtractor = lz77TokenExtractor;
            this.lz77BufferManager  = lz77BufferManager;
            this.lz77TokenWriter    = lz77TokenWriter;

            Lz77Buffer = new Lz77Buffer(4, 4);

            TokensFromPreviousRun = new List <Lz77Token>();
        }
Example #8
0
        public static Lz77Buffer GetLz77Buffer3()
        {
            var lz77Buffer = new Lz77Buffer(3, 3);

            lz77Buffer.LookAheadBuffer.Add(20);
            lz77Buffer.LookAheadBuffer.Add(21);
            lz77Buffer.LookAheadBuffer.Add(22);
            lz77Buffer.SearchBuffer.Add(19);
            lz77Buffer.SearchBuffer.Add(20);
            lz77Buffer.SearchBuffer.Add(21);

            return(lz77Buffer);
        }
        private bool Lz77BufferContainsGivenSequence(Lz77Buffer lz77Buffer, List <byte> sequence, int indexOfFirstByteFoundInSearchBuffer, out byte byteThatWasNotContained)
        {
            // TODO: Maybe implement properly at some other time
            //var lookAheadBufferIndex = 0;

            for (int index = 0; index < sequence.Count; index++)
            {
                var searchBufferIndex = indexOfFirstByteFoundInSearchBuffer + index;
                var contains          = true;

                if (searchBufferIndex > lz77Buffer.SearchBuffer.Count - 1)
                {
                    contains = false;
                }
                else
                {
                    if (searchBufferIndex >= 0)
                    {
                        if (lz77Buffer.SearchBuffer[searchBufferIndex] != sequence[index])
                        {
                            contains = false;
                        }
                    }
                    else
                    {
                        contains = false;
                    }
                    // TODO: Maybe implement properly at some other time
                    //else
                    //{
                    //    if (Lz77Buffer.LookAheadBuffer[lookAheadBufferIndex] != sequence[index])
                    //    {
                    //        contains = false;
                    //    }

                    //    lookAheadBufferIndex++;
                    //}
                }

                if (!contains)
                {
                    byteThatWasNotContained = sequence[index];
                    return(false);
                }
            }

            byteThatWasNotContained = 0;
            return(true);
        }
Example #10
0
        public void EncodeFile(IFileReader fileReader, IFileWriter fileWriter, int bitsForOffset, int bitsForLength)
        {
            if (fileReader == null)
            {
                throw new ArgumentNullException(nameof(fileReader));
            }

            if (fileWriter == null)
            {
                throw new ArgumentNullException(nameof(fileWriter));
            }

            if (bitsForOffset < 3 || bitsForOffset > 15)
            {
                throw new ArgumentException($"{nameof(bitsForOffset)} should be between 3 and 15");
            }

            if (bitsForLength < 2 || bitsForLength > 7)
            {
                throw new ArgumentException($"{nameof(bitsForLength)} should be between 3 and 15");
            }

            if (Lz77Buffer.Length != bitsForLength || Lz77Buffer.Offset != bitsForOffset)
            {
                Lz77Buffer.SetOffsetAndLimit(bitsForLength, bitsForLength);
            }

            WriteHeader(fileWriter, bitsForOffset, bitsForLength);

            TokensFromPreviousRun.Clear();
            lz77BufferManager.TryToFillLookAheadBuffer(Lz77Buffer, fileReader);

            while (Lz77Buffer.LookAheadBuffer.Count > 0)
            {
                var lz77Token = lz77TokenExtractor.GetLz77TokenFromLz77Buffer(Lz77Buffer);

                lz77BufferManager.TryToFillSearchBufferBasedOnLz77Token(Lz77Buffer, lz77Token);
                lz77BufferManager.EmptyLookAheadBufferBasedOnLz77Token(Lz77Buffer, lz77Token);
                lz77BufferManager.TryToFillLookAheadBuffer(Lz77Buffer, fileReader);

                lz77TokenWriter.WriteToken(lz77Token, fileWriter, bitsForOffset, bitsForLength);

                TokensFromPreviousRun.Add(lz77Token);
            }

            fileWriter.Flush();
        }
Example #11
0
        public void TryToFillLookAheadBuffer(Lz77Buffer lz77Buffer, IFileReader fileReader)
        {
            if (lz77Buffer == null)
            {
                throw new ArgumentNullException(nameof(lz77Buffer));
            }

            if (fileReader == null)
            {
                throw new ArgumentNullException(nameof(fileReader));
            }

            while (lz77Buffer.LookAheadBuffer.Count < lz77Buffer.LookAheadBuffer.Capacity && !fileReader.ReachedEndOfFile)
            {
                lz77Buffer.LookAheadBuffer.Add((byte)fileReader.ReadBits(8));
            }
        }
        private List <int> GetIndexesOfGivenByte(Lz77Buffer lz77Buffer, byte b)
        {
            if (!lz77Buffer.SearchBuffer.Contains(b))
            {
                return(new List <int>());
            }

            var indexes = new List <int>();

            for (int index = 0; index < lz77Buffer.SearchBuffer.Count; index++)
            {
                if (lz77Buffer.SearchBuffer[index] == b)
                {
                    indexes.Add(index);
                }
            }

            return(indexes);
        }
Example #13
0
        public void EmptyLookAheadBufferBasedOnLz77Token(Lz77Buffer lz77Buffer, Lz77Token lz77Token)
        {
            if (lz77Buffer == null)
            {
                throw new ArgumentNullException(nameof(lz77Buffer));
            }

            if (lz77Token == null)
            {
                throw new ArgumentNullException(nameof(lz77Token));
            }

            var bytesToRemove = lz77Token.Length + 1;

            while (bytesToRemove > 0 && lz77Buffer.LookAheadBuffer.Count > 0)
            {
                lz77Buffer.LookAheadBuffer.RemoveAt(0);
                bytesToRemove--;
            }
        }
 public void ConstructorThrowsArgumentExceptionForOffsetSmallerThanOne()
 {
     var lz77Buffer = new Lz77Buffer(0, length);
 }
 public void ConstructorThrowsArgumentExceptionForLengthSmallerThanOne()
 {
     var lz77Buffer = new Lz77Buffer(offset, -2);
 }
        public void ConstructorSetsLengthPropertyAsExpected()
        {
            var lz77Buffer = new Lz77Buffer(offset, length);

            Assert.AreEqual(length, lz77Buffer.Length);
        }
        public Lz77Token GetLz77TokenFromLz77Buffer(Lz77Buffer lz77Buffer)
        {
            if (lz77Buffer == null)
            {
                throw new ArgumentNullException(nameof(lz77Buffer));
            }

            if (lz77Buffer.LookAheadBuffer.Count == 0)
            {
                throw new ArgumentException("Look ahead buffer shouldn't be empty");
            }

            var firstByteToLookFor = lz77Buffer.LookAheadBuffer[0];

            if (!lz77Buffer.SearchBuffer.Contains(firstByteToLookFor))
            {
                return(new Lz77Token
                {
                    Byte = firstByteToLookFor,
                    Length = 0,
                    Position = 0
                });
            }

            var  bytesFoundFinal = 0;
            var  indexFinal      = lz77Buffer.SearchBuffer.Count - 1 - lz77Buffer.SearchBuffer.IndexOf(firstByteToLookFor);
            byte byteThatWasNotContainedFinal = 0;

            var indexes = GetIndexesOfGivenByte(lz77Buffer, firstByteToLookFor);

            foreach (var index in indexes)
            {
                var bytesFoundInCurrentIteration = 0;
                var bytesToLookFor = new List <byte> {
                    firstByteToLookFor
                };
                byte byteThatWasNotContained = 0;

                while (Lz77BufferContainsGivenSequence(lz77Buffer, bytesToLookFor, index, out byteThatWasNotContained))
                {
                    if (bytesFoundInCurrentIteration >= lz77Buffer.LookAheadBuffer.Count)
                    {
                        break;
                    }

                    bytesFoundInCurrentIteration++;

                    if (bytesFoundInCurrentIteration < lz77Buffer.LookAheadBuffer.Count)
                    {
                        bytesToLookFor.Add(lz77Buffer.LookAheadBuffer[bytesFoundInCurrentIteration]);
                    }
                    else
                    {
                        break;
                    }
                }

                if (bytesFoundInCurrentIteration >= bytesFoundFinal)
                {
                    bytesFoundFinal = bytesFoundInCurrentIteration;
                    indexFinal      = lz77Buffer.SearchBuffer.Count - 1 - index;
                    byteThatWasNotContainedFinal = byteThatWasNotContained;
                }
            }

            return(new Lz77Token
            {
                Byte = byteThatWasNotContainedFinal,
                Length = bytesFoundFinal,
                Position = indexFinal
            });
        }