Compare commits

..
2 Commits
Author SHA1 Message Date
leonhard c89b884e59 refactor: move header structures into its own folder 2026-10-22 00:13:15 +02:00
leonhard 91819f19bf feat: add byte conversion to base header 2026-10-22 00:06:15 +02:00
5 changed files with 141 additions and 65 deletions

No files matched your search

-50
View File
@@ -1,50 +0,0 @@
const ID1: u8 = 0x1f;
const ID2: u8 = 0x8b;
pub struct BaseHeader {
compression_method: CompressionMethod,
flags: Flags,
last_modified: ModificationTime,
extra_flags: ExtraFlags,
os: Os,
}
enum CompressionMethod {
Deflate,
}
struct Flags(u8);
impl Flags {
const FTEXT: u8 = 1 << 0;
const FHCRC: u8 = 1 << 1;
const FEXTRA: u8 = 1 << 2;
const FNAME: u8 = 1 << 3;
const FCOMMENT: u8 = 1 << 4;
}
enum ModificationTime {
UnixTimestamp(u32),
NoTimestampAvailable,
}
// The meaning of these depends on the compression method
struct ExtraFlags(u8);
enum Os {
FaaFilesystem,
Amiga,
Vms,
Unix,
VmCms,
AtariTos,
HpfsFilesystem,
Macintosh,
ZSystem,
CpM,
Tops20,
NtfsFilesystem,
Qdos,
AcornRiscos,
Unknown,
}
+125
View File
@@ -0,0 +1,125 @@
const ID1: u8 = 0x1f;
const ID2: u8 = 0x8b;
pub struct BaseHeader {
compression_method: CompressionMethod,
flags: Flags,
last_modified: ModificationTime,
extra_flags: ExtraFlags,
os: Os,
}
impl BaseHeader {
const SIZE: usize = 10;
fn to_bytes(self) -> [u8; Self::SIZE] {
let mtime: [u8; 4] = self.last_modified.into();
[
ID1,
ID2,
self.compression_method.into(),
self.flags.0,
mtime[0],
mtime[1],
mtime[2],
mtime[3],
self.extra_flags.0,
self.os.into(),
]
}
}
impl From<BaseHeader> for [u8; 10] {
fn from(header: BaseHeader) -> Self {
header.to_bytes()
}
}
pub enum CompressionMethod {
Deflate,
}
impl From<CompressionMethod> for u8 {
fn from(method: CompressionMethod) -> Self {
match method {
CompressionMethod::Deflate => 8,
}
}
}
pub struct Flags(u8);
impl Flags {
pub const FTEXT: u8 = 1 << 0;
pub const FHCRC: u8 = 1 << 1;
pub const FEXTRA: u8 = 1 << 2;
pub const FNAME: u8 = 1 << 3;
pub const FCOMMENT: u8 = 1 << 4;
pub fn contains(&self, flag: u8) -> bool {
self.0 & flag != 0
}
pub fn with(self, flag: u8) -> Self {
Flags(self.0 | flag)
}
}
pub enum ModificationTime {
UnixTimestamp(u32),
NoTimestampAvailable,
}
impl From<ModificationTime> for [u8; 4] {
fn from(mtime: ModificationTime) -> Self {
use ModificationTime::*;
match mtime {
NoTimestampAvailable => [0; 4],
UnixTimestamp(ts) => ts.to_le_bytes(),
}
}
}
// The meaning of these depends on the compression method
struct ExtraFlags(u8);
pub enum Os {
FatFilesystem,
Amiga,
Vms,
Unix,
VmCms,
AtariTos,
HpfsFilesystem,
Macintosh,
ZSystem,
CpM,
Tops20,
NtfsFilesystem,
Qdos,
AcornRiscos,
Unknown,
}
impl From<Os> for u8 {
fn from(os: Os) -> Self {
use Os::*;
match os {
FatFilesystem => 0,
Amiga => 1,
Vms => 2,
Unix => 3,
VmCms => 4,
AtariTos => 5,
HpfsFilesystem => 6,
Macintosh => 7,
ZSystem => 8,
CpM => 9,
Tops20 => 10,
NtfsFilesystem => 11,
Qdos => 12,
AcornRiscos => 13,
Unknown => 255,
}
}
}
File renamed without changes.
+14
View File
@@ -0,0 +1,14 @@
mod base;
mod extra;
use std::ffi::CString;
use crate::header::base::BaseHeader;
use crate::header::extra::ExtraFieldHeader;
pub struct Header {
base_header: BaseHeader,
extra_field_header: Option<ExtraFieldHeader>,
original_file_name: Option<CString>,
comment: Option<CString>,
}
+2 -15
View File
@@ -1,18 +1,5 @@
mod base_header; mod header;
mod extra_field_header;
use std::ffi::CString;
use crate::base_header::BaseHeader;
use crate::extra_field_header::ExtraFieldHeader;
struct GzipFile { struct GzipFile {
header: FullHeader, header: header::Header,
}
struct FullHeader {
base_header: BaseHeader,
extra_field_header: Option<ExtraFieldHeader>,
original_file_name: Option<CString>,
comment: Option<CString>,
} }