/// <summary> /// Attempts to parse an APNGChunk for the specified stream /// </summary> /// <param name="stream">The stream containing the APNG Chunk</param> public void Read(Stream stream) { if (!stream.CanRead) { throw new ArgumentException("Stream is not readable"); } calculatedCRC = CRC.INITIAL_CRC; long chunkStart = stream.Position; // Read the data Length chunkLength = APNGUtility.ReadStream(stream, 4); // Read the chunk type chunkType = APNGUtility.ReadStream(stream, 4); calculatedCRC = CRC.UpdateCRC(calculatedCRC, chunkType); // Read the data chunkData = APNGUtility.ReadStream(stream, ChunkLength); calculatedCRC = CRC.UpdateCRC(calculatedCRC, chunkData); // Read the CRC chunkCRC = APNGUtility.ReadStream(stream, 4); // CRC is inverted calculatedCRC = ~calculatedCRC; // Verify the CRC if (ChunkCRC != calculatedCRC) { StringBuilder sb = new StringBuilder(); sb.AppendLine(String.Format("APNG Chunk CRC Mismatch. Chunk CRC = {0}, Calculated CRC = {1}.", ChunkCRC, calculatedCRC)); sb.AppendLine(String.Format("This occurred while parsing the chunk at position {0} (0x{1:X8}) in the stream.", chunkStart, chunkStart)); throw new ApplicationException(sb.ToString()); } }
/// <summary> /// Attempts to read an APNG Header chunk from the supplied stream. /// </summary> /// <param name="stream">The stream containing the APNG Header</param> public void Read(Stream stream) { // Stream must be readable if (!stream.CanRead) { throw new ArgumentException("Stream is not readable"); } // Read the signature try { signature = APNGUtility.ReadStream(stream, 8); } catch (Exception) { // Re-throw any exceptions throw; } // Test signature for validity if (signature.Length == expectedSignature.Length) { for (int i = 0; i < expectedSignature.Length; i++) { // Invalid signature if (expectedSignature[i] != signature[i]) { throw new ApplicationException("APNG signature not found."); } } } else { // Invalid signature throw new ApplicationException("APNG signature not found."); } }