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 crate::chunk_type::{self, ChunkType};
use crate::chunk_type::ChunkType;
struct Chunk {
length: u32,
@ -11,8 +11,24 @@ struct Chunk {
}
impl Chunk {
fn new(chunk_type: ChunkType, data: Vec<u8>) -> Chunk {
todo!()
fn new(chunktype: ChunkType, data: Vec<u8>) -> Chunk {
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 {
@ -32,11 +48,25 @@ impl Chunk {
}
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> {
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();
for byte in &value[(slice_length - 8)..(slice_length -4)] {
for byte in &value[8..(slice_length -4)] {
chunk_data_vec.push(*byte)
}
@ -83,14 +113,36 @@ impl TryFrom<&[u8]> for Chunk {
crc: crc_from_slice,
};
Ok(chunk)
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)
}
}
}
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
)
}
}

View File

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