From 9c1f66d5dcf771d57482fa0db464464f83822936 Mon Sep 17 00:00:00 2001 From: clizia Date: Mon, 11 Nov 2024 16:46:10 +0100 Subject: [PATCH] finished implementing png chunks --- src/chunk.rs | 48 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/chunk.rs b/src/chunk.rs index fccffcb..c285c45 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::fmt::{write, Display}; use crc::Crc; use crate::chunk_type::ChunkType; @@ -48,11 +48,25 @@ impl Chunk { } fn data_as_string(&self) -> Result { - todo!() + let to_stringify: Vec = self.chunk_data.clone(); + + if self.length() == 0 { + Err("diocane") + } else { + Ok(String::from_utf8(to_stringify).unwrap()) + } } fn as_bytes(&self) -> Vec { - todo!() + let bytes: Vec = self.length() + .to_be_bytes() + .iter() + .chain(self.chunk_type.bytes().iter()) + .chain(self.chunk_data.iter()) + .chain(self.crc().to_be_bytes().iter()) + .copied().collect(); + + bytes } } @@ -80,7 +94,7 @@ impl TryFrom<&[u8]> for Chunk { let mut chunk_data_vec: Vec = Vec::new(); - for byte in &value[(slice_length - 8)..(slice_length -4)] { + for byte in &value[8..(slice_length -4)] { chunk_data_vec.push(*byte) } @@ -99,14 +113,36 @@ impl TryFrom<&[u8]> for Chunk { crc: crc_from_slice, }; - Ok(chunk) + let x25: Crc = Crc::::new(&crc::CRC_32_ISO_HDLC); + + let to_hash: Vec = chunk.chunk_type + .bytes() + .iter() + .chain(chunk.chunk_data.iter()) + .copied() + .collect(); + + let crc_value = x25.checksum(&to_hash); + + if crc_value != chunk.crc { + Err("Invalid bytes (probably crc is wrong)") + } else { + Ok(chunk) + } + + } } impl Display for Chunk { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - todo!() + write!(f, "chunk length:{}\nchunk type: {}\n, chunk message {}\nCRC u32: {}", + self.length, + self.chunk_type, + self.data_as_string().unwrap(), + self.crc + ) } }