public void TestMultipleIDATChunksAreTreatedAsASingleDeflatedBuffer()
        {
            byte[] originalData        = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xA0 };
            byte[] deflateOriginalData = DeflateBytes(originalData);

            int splitAtIndex = 4;

            byte[] chunk1Data = deflateOriginalData.Take(splitAtIndex).ToArray();
            byte[] chunk2Data = deflateOriginalData.Skip(splitAtIndex).ToArray();

            List <PNGChunk> chunks = new List <PNGChunk>()
            {
                new PNGChunk(PNGChunk.StringFromType(PNGChunk.ChunkType.CgBI), new byte[] { }, 0),
                new PNGChunk(PNGChunk.StringFromType(PNGChunk.ChunkType.IDAT), chunk1Data, 0),
                new PNGChunk(PNGChunk.StringFromType(PNGChunk.ChunkType.IDAT), chunk2Data, 0)
            };

            IEnumerable <PNGChunk> decrushedChunks = PNGDecrusher.DecrushChunks(chunks);

            byte[] zlibChunk1 = decrushedChunks.First().Data;
            byte[] zlibChunk2 = decrushedChunks.Last().Data;

            byte[] zlibChunksCombined = new byte[zlibChunk1.Length + zlibChunk2.Length];
            zlibChunk1.CopyTo(zlibChunksCombined, 0);
            zlibChunk2.CopyTo(zlibChunksCombined, zlibChunk1.Length);

            byte[] dezlibbedData = DecompressZlibBytes(zlibChunksCombined);

            Assert.AreEqual(originalData.Length, dezlibbedData.Length);
            CollectionAssert.AreEqual(originalData, dezlibbedData);
        }
        public void TestZlibHeadersAreAddedBack()
        {
            IEnumerable <PNGChunk> chunks = PNGChunkParser.ChunksFromStream(new MemoryStream(Crushed10x10White));

            IEnumerable <PNGChunk> decrushedChunks = PNGDecrusher.DecrushChunks(chunks);
            PNGChunk decrushedIdatChunk            = decrushedChunks.First(c => c.Type == PNGChunk.ChunkType.IDAT);

            using (MemoryStream idatDataStream = new MemoryStream(decrushedIdatChunk.Data))
            {
                // basic check of the zlib header
                // http://tools.ietf.org/html/rfc1950#page-4
                int CMF = idatDataStream.ReadByte();
                int FLG = idatDataStream.ReadByte();

                int compressionMethod = CMF & 0x0F;
                int compressionInfo   = (CMF & 0xF0) >> 4;

                int COMPRESSION_METHOD_DEFLATE = 8;
                Assert.AreEqual(COMPRESSION_METHOD_DEFLATE, compressionMethod);

                using (DeflateStream deflateStream = new DeflateStream(idatDataStream, CompressionMode.Decompress))
                    using (MemoryStream decompressedStream = new MemoryStream())
                    {
                        deflateStream.CopyTo(decompressedStream);
                        Assert.AreEqual(410, decompressedStream.Length);
                    }
            }
        }
        public void TestChunksAreCorrectlyParsed()
        {
            using (MemoryStream stream = new MemoryStream(Simple10x10WhitePNG))
            {
                PNGChunk[] chunks = PNGChunkParser.ChunksFromStream(stream).ToArray();
                Assert.AreEqual(6, chunks.Length);

                PNGChunk idatChunk = chunks[4];
                Assert.AreEqual("IDAT", idatChunk.TypeString);
                Assert.AreEqual(PNGChunk.ChunkType.IDAT, idatChunk.Type);
                Assert.AreEqual(17, idatChunk.Data.Length);
            }
        }
Exemple #4
0
        private static void WriteChunkToWriter(PNGChunk chunk, BinaryWriter writer)
        {
            writer.WriteNetworkOrder((uint)chunk.Data.Length);

            byte[] typeString = Encoding.UTF8.GetBytes(chunk.TypeString);
            if (typeString.Length != 4)
            {
                throw new InvalidDataException("PNG chunk type must be a 4 character string");
            }

            writer.Write(typeString);
            writer.Write(chunk.Data);
            writer.WriteNetworkOrder(chunk.DataCRC);
        }
Exemple #5
0
        private static void WriteChunkToWriter(PNGChunk chunk, BinaryWriter writer)
        {
            writer.WriteNetworkOrder((uint)chunk.Data.Length);

            byte[] typeString = Encoding.UTF8.GetBytes(chunk.TypeString);
            if (typeString.Length != 4)
            {
                throw new InvalidDataException("PNG chunk type must be a 4 character string");
            }

            writer.Write(typeString);
            writer.Write(chunk.Data);
            writer.WriteNetworkOrder(chunk.DataCRC);
        }
        public void TestAppleChunkIsRemovedWhenDecrushing()
        {
            IEnumerable <PNGChunk> chunks = new List <PNGChunk>()
            {
                new PNGChunk(PNGChunk.StringFromType(PNGChunk.ChunkType.CgBI), new byte[] { 0x01, 0x02, 0x03 }, 12345),
                new PNGChunk(PNGChunk.StringFromType(PNGChunk.ChunkType.IDAT), DeflateBytes(new byte[] { 0x01, 0x02, 0x03 }), 12345),
                new PNGChunk("IEND", new byte[] { }, 0xFFFFFF)
            };

            IEnumerable <PNGChunk> fixedChunks = PNGDecrusher.DecrushChunks(chunks);

            Assert.AreEqual(2, fixedChunks.Count());
            Assert.AreEqual(PNGChunk.ChunkType.IDAT, fixedChunks.ToArray()[0].Type);
            Assert.AreEqual("IEND", fixedChunks.ToArray()[1].TypeString);
        }
        public void TestDecrushedIDATDataIsSameAsOriginalAfterDecompressing()
        {
            byte[] originalData         = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
            byte[] deflatedOriginalData = DeflateBytes(originalData);

            List <PNGChunk> chunks = new List <PNGChunk>()
            {
                new PNGChunk(PNGChunk.StringFromType(PNGChunk.ChunkType.CgBI), new byte[] { }, 0),
                new PNGChunk(PNGChunk.StringFromType(PNGChunk.ChunkType.IDAT), deflatedOriginalData, 0)
            };

            IEnumerable <PNGChunk> decrushedChunks = PNGDecrusher.DecrushChunks(chunks);

            Assert.AreEqual(1, decrushedChunks.Count());

            byte[] zlibData      = decrushedChunks.First().Data;
            byte[] dezlibbedData = DecompressZlibBytes(zlibData);

            Assert.AreEqual(originalData.Length, dezlibbedData.Length);
            CollectionAssert.AreEqual(originalData, dezlibbedData);
        }
Exemple #8
0
        private static PNGChunk IDATChunkWithBytes(byte[] bytes)
        {
            string type = PNGChunk.StringFromType(PNGChunk.ChunkType.IDAT);

            return(new PNGChunk(type, bytes, CalculateCRCForChunk(type, bytes)));
        }