Compare commits

...
4 Commits
4 changed files with 85 additions and 16 deletions

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
use std::mem::{self, MaybeUninit};
pub struct Crc(u64);
pub struct Crc32Encoder {
crc_table: [Crc; 256],
}
impl Crc32Encoder {
pub fn new() -> Self {
// SAFETY: the values will be initialized in the correct order in the following for-loop
let mut crc_table: [MaybeUninit<Crc>; 256] = unsafe { MaybeUninit::uninit().assume_init() };
for n in 0u8..=255 {
let mut c: u64 = n.into();
for _ in 0..8 {
if c & 1 == 0 {
c = 0xedb88320 ^ (c >> 1);
} else {
c = c >> 1;
}
}
crc_table[n as usize] = MaybeUninit::new(Crc(c));
}
let crc_table = unsafe { mem::transmute(crc_table) };
Self { crc_table }
}
pub fn update(&self, crc: &Crc, mut buffer: impl Iterator<Item = u8>) -> Crc {
let mut c = crc.0 ^ 0xffffffff;
while let Some(n) = buffer.next() {
c = self.crc_table[((c ^ n as u64) & 0xff) as usize].0 ^ (c >> 8);
}
Crc(c ^ 0xffffffff)
}
pub fn encode(&self, buffer: impl Iterator<Item = u8>) -> Crc {
self.update(&Crc(0), buffer)
}
}
+7 -15
View File
@@ -9,32 +9,24 @@ pub struct BaseHeader {
os: Os,
}
impl BaseHeader {
const SIZE: usize = 10;
fn to_bytes(self) -> [u8; Self::SIZE] {
let mtime: [u8; 4] = self.last_modified.into();
impl From<BaseHeader> for [u8; 10] {
fn from(header: BaseHeader) -> Self {
let mtime: [u8; 4] = header.last_modified.into();
[
ID1,
ID2,
self.compression_method.into(),
self.flags.0,
header.compression_method.into(),
header.flags.0,
mtime[0],
mtime[1],
mtime[2],
mtime[3],
self.extra_flags.0,
self.os.into(),
header.extra_flags.0,
header.os.into(),
]
}
}
impl From<BaseHeader> for [u8; 10] {
fn from(header: BaseHeader) -> Self {
header.to_bytes()
}
}
pub enum CompressionMethod {
Deflate,
}
+23
View File
@@ -11,4 +11,27 @@ pub struct Header {
extra_field_header: Option<ExtraFieldHeader>,
original_file_name: Option<CString>,
comment: Option<CString>,
crc16: Option<u16>,
}
impl From<Header> for Vec<u8> {
fn from(header: Header) -> Self {
let mut result = Vec::new();
result.extend::<[u8; 10]>(header.base_header.into());
if let Some(extra_field_header) = header.extra_field_header {
result.append(&mut extra_field_header.into());
}
if let Some(original_file_name) = header.original_file_name {
result.extend_from_slice(original_file_name.as_bytes_with_nul());
}
if let Some(comment) = header.comment {
result.extend_from_slice(comment.as_bytes_with_nul());
}
if let Some(crc) = header.crc16 {
let crc = crc.to_le_bytes();
result.push(crc[0]);
result.push(crc[1]);
}
result
}
}
+16 -1
View File
@@ -1,5 +1,20 @@
mod crc32;
mod header;
struct GzipFile {
pub struct GzipFile {
header: header::Header,
compressed_data: Vec<u8>,
crc32: u32,
input_size: u32,
}
impl From<GzipFile> for Vec<u8> {
fn from(mut gzip_file: GzipFile) -> Self {
let mut result = Vec::new();
result.append(&mut gzip_file.header.into());
result.append(&mut gzip_file.compressed_data);
result.extend(gzip_file.crc32.to_le_bytes());
result.extend(gzip_file.input_size.to_le_bytes());
result
}
}