Compare commits

...
5 Commits
5 changed files with 117 additions and 17 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,
}
+32 -1
View File
@@ -1,7 +1,38 @@
pub struct ExtraFieldHeader(Vec<ExtraSubField>);
struct ExtraSubField {
pub struct ExtraSubField {
subfield_id1: u8,
subfield_id2: u8,
field: Vec<u8>,
}
impl From<ExtraFieldHeader> for Vec<u8> {
fn from(extra_field_header: ExtraFieldHeader) -> Self {
let mut result = Vec::new();
let xlen: usize = extra_field_header
.0
.iter()
.map(|extra_sub_field| extra_sub_field.field.len())
.sum();
let xlen = xlen.to_le_bytes();
result.push(xlen[0]);
result.push(xlen[1]);
for extra_sub_field in extra_field_header.0 {
result.append(&mut extra_sub_field.into());
}
result
}
}
impl From<ExtraSubField> for Vec<u8> {
fn from(mut extra_sub_field: ExtraSubField) -> Self {
let mut result = Vec::new();
let len = extra_sub_field.field.len().to_le_bytes();
result.push(extra_sub_field.subfield_id1);
result.push(extra_sub_field.subfield_id2);
result.push(len[0]);
result.push(len[1]);
result.append(&mut extra_sub_field.field);
result
}
}
+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
}
}