Compare commits

...

3 Commits

Author SHA1 Message Date
9c1f66d5dc finished implementing png chunks 2024-11-11 16:46:10 +01:00
2d5899313e implemented new() for chunk 2024-11-11 16:06:42 +01:00
8cedbaf13d cleaned chunk_type.rs 2024-11-11 13:33:24 +01:00
2 changed files with 63 additions and 18 deletions

View File

@ -1,7 +1,7 @@
use std::{fmt::Display}; use std::fmt::{write, Display};
use crc::Crc; use crc::Crc;
use crate::chunk_type::{self, ChunkType}; use crate::chunk_type::ChunkType;
struct Chunk { struct Chunk {
length: u32, length: u32,
@ -11,8 +11,24 @@ struct Chunk {
} }
impl Chunk { impl Chunk {
fn new(chunk_type: ChunkType, data: Vec<u8>) -> Chunk { fn new(chunktype: ChunkType, data: Vec<u8>) -> Chunk {
todo!() let x25: Crc<u32> = Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);
let to_hash: Vec<u8> = chunktype
.bytes()
.iter()
.chain(&data)
.copied()
.collect();
let crc_value = x25.checksum(&to_hash);
Chunk {
length: data.len() as u32,
chunk_type: chunktype,
chunk_data: data,
crc: crc_value,
}
} }
fn length(&self) -> u32 { fn length(&self) -> u32 {
@ -32,11 +48,25 @@ impl Chunk {
} }
fn data_as_string(&self) -> Result<String, &'static str> { fn data_as_string(&self) -> Result<String, &'static str> {
todo!() let to_stringify: Vec<u8> = self.chunk_data.clone();
if self.length() == 0 {
Err("diocane")
} else {
Ok(String::from_utf8(to_stringify).unwrap())
}
} }
fn as_bytes(&self) -> Vec<u8> { fn as_bytes(&self) -> Vec<u8> {
todo!() let bytes: Vec<u8> = 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
} }
} }
@ -64,7 +94,7 @@ impl TryFrom<&[u8]> for Chunk {
let mut chunk_data_vec: Vec<u8> = Vec::new(); let mut chunk_data_vec: Vec<u8> = 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) chunk_data_vec.push(*byte)
} }
@ -83,14 +113,36 @@ impl TryFrom<&[u8]> for Chunk {
crc: crc_from_slice, crc: crc_from_slice,
}; };
let x25: Crc<u32> = Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);
let to_hash: Vec<u8> = 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) Ok(chunk)
}
} }
} }
impl Display for Chunk { impl Display for Chunk {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 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
)
} }
} }

View File

@ -1,5 +1,5 @@
use core::str; use core::str;
use std::{str::FromStr, u8}; use std::str::FromStr;
fn byte_to_bits(byte: u8) -> [u8; 8] { fn byte_to_bits(byte: u8) -> [u8; 8] {
let mut bits = [0u8; 8]; let mut bits = [0u8; 8];
@ -27,7 +27,7 @@ pub struct ChunkType {
impl ChunkType { impl ChunkType {
fn bytes(&self) -> [u8; 4] { pub fn bytes(&self) -> [u8; 4] {
let bytes: [u8; 4] = [ let bytes: [u8; 4] = [
self.ancillary_byte, self.ancillary_byte,
self.private_byte, self.private_byte,
@ -58,7 +58,6 @@ impl ChunkType {
continue; continue;
} }
} }
} }
@ -68,12 +67,10 @@ impl ChunkType {
fn is_public(&self) -> bool { fn is_public(&self) -> bool {
self.private_byte.is_ascii_uppercase() self.private_byte.is_ascii_uppercase()
} }
fn is_reserved_bit_valid(&self) -> bool { fn is_reserved_bit_valid(&self) -> bool {
self.reserved_byte.is_ascii_uppercase() self.reserved_byte.is_ascii_uppercase()
} }
fn is_safe_to_copy(&self) -> bool { fn is_safe_to_copy(&self) -> bool {
@ -86,7 +83,6 @@ impl TryFrom<[u8; 4]> for ChunkType {
type Error = &'static str; type Error = &'static str;
fn try_from(value: [u8; 4]) -> Result<Self, Self::Error> { fn try_from(value: [u8; 4]) -> Result<Self, Self::Error> {
let chunk_type = ChunkType { let chunk_type = ChunkType {
ancillary_byte: value[0], ancillary_byte: value[0],
private_byte: value[1], private_byte: value[1],
@ -107,7 +103,6 @@ impl TryFrom<[u8; 4]> for ChunkType {
continue; continue;
} }
} }
} }
} }
@ -122,7 +117,6 @@ impl std::fmt::Display for ChunkType {
write!(f, "{}", s) write!(f, "{}", s)
} }
} }
impl FromStr for ChunkType { impl FromStr for ChunkType {
@ -151,7 +145,6 @@ impl FromStr for ChunkType {
continue; continue;
} }
} }
} }
} }