From 2d5899313e7cb7ba3b38b05af58ecadf43e09c45 Mon Sep 17 00:00:00 2001 From: clizia Date: Mon, 11 Nov 2024 16:06:42 +0100 Subject: [PATCH] implemented new() for chunk --- src/chunk.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/chunk.rs b/src/chunk.rs index 868bdd9..fccffcb 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -1,7 +1,7 @@ -use std::{fmt::Display}; +use std::fmt::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) -> Chunk { - todo!() + fn new(chunktype: ChunkType, data: Vec) -> Chunk { + let x25: Crc = Crc::::new(&crc::CRC_32_ISO_HDLC); + + let to_hash: Vec = 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 {