Compare commits

..

No commits in common. "9c1f66d5dcf771d57482fa0db464464f83822936" and "5d9baecbac5ac1422410c62256e2277889b5490b" have entirely different histories.

2 changed files with 18 additions and 63 deletions

View File

@ -1,7 +1,7 @@
use std::fmt::{write, Display};
use std::{fmt::Display};
use crc::Crc;
use crate::chunk_type::ChunkType;
use crate::chunk_type::{self, ChunkType};
struct Chunk {
length: u32,
@ -11,24 +11,8 @@ struct Chunk {
}
impl Chunk {
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 new(chunk_type: ChunkType, data: Vec<u8>) -> Chunk {
todo!()
}
fn length(&self) -> u32 {
@ -48,25 +32,11 @@ impl Chunk {
}
fn data_as_string(&self) -> Result<String, &'static str> {
let to_stringify: Vec<u8> = self.chunk_data.clone();
if self.length() == 0 {
Err("diocane")
} else {
Ok(String::from_utf8(to_stringify).unwrap())
}
todo!()
}
fn as_bytes(&self) -> Vec<u8> {
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
todo!()
}
}
@ -94,7 +64,7 @@ impl TryFrom<&[u8]> for Chunk {
let mut chunk_data_vec: Vec<u8> = Vec::new();
for byte in &value[8..(slice_length -4)] {
for byte in &value[(slice_length - 8)..(slice_length -4)] {
chunk_data_vec.push(*byte)
}
@ -113,36 +83,14 @@ impl TryFrom<&[u8]> for Chunk {
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 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "chunk length:{}\nchunk type: {}\n, chunk message {}\nCRC u32: {}",
self.length,
self.chunk_type,
self.data_as_string().unwrap(),
self.crc
)
todo!()
}
}

View File

@ -1,5 +1,5 @@
use core::str;
use std::str::FromStr;
use std::{str::FromStr, u8};
fn byte_to_bits(byte: u8) -> [u8; 8] {
let mut bits = [0u8; 8];
@ -27,7 +27,7 @@ pub struct ChunkType {
impl ChunkType {
pub fn bytes(&self) -> [u8; 4] {
fn bytes(&self) -> [u8; 4] {
let bytes: [u8; 4] = [
self.ancillary_byte,
self.private_byte,
@ -58,6 +58,7 @@ impl ChunkType {
continue;
}
}
}
@ -67,10 +68,12 @@ 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 {
@ -83,6 +86,7 @@ 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],
@ -103,6 +107,7 @@ impl TryFrom<[u8; 4]> for ChunkType {
continue;
}
}
}
}
@ -117,6 +122,7 @@ impl std::fmt::Display for ChunkType {
write!(f, "{}", s)
}
}
impl FromStr for ChunkType {
@ -145,6 +151,7 @@ impl FromStr for ChunkType {
continue;
}
}
}
}