Compare commits
7
Commits
8225b3d113
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
61a8758082 | ||
|
|
3ef19612d7 | ||
|
|
b40a156b13 | ||
|
|
ac313d1a7d | ||
|
|
6ce35eb6e3 | ||
|
|
c89b884e59 | ||
|
|
91819f19bf |
No files matched your search
@@ -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,
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
pub struct ExtraFieldHeader(Vec<ExtraSubField>);
|
||||
|
||||
struct ExtraSubField {
|
||||
subfield_id1: u8,
|
||||
subfield_id2: u8,
|
||||
field: Vec<u8>,
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
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 From<BaseHeader> for [u8; 10] {
|
||||
fn from(header: BaseHeader) -> Self {
|
||||
let mtime: [u8; 4] = header.last_modified.into();
|
||||
[
|
||||
ID1,
|
||||
ID2,
|
||||
header.compression_method.into(),
|
||||
header.flags.0,
|
||||
mtime[0],
|
||||
mtime[1],
|
||||
mtime[2],
|
||||
mtime[3],
|
||||
header.extra_flags.0,
|
||||
header.os.into(),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
pub struct ExtraFieldHeader(Vec<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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
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>,
|
||||
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
-14
@@ -1,18 +1,20 @@
|
||||
mod base_header;
|
||||
mod extra_field_header;
|
||||
mod crc32;
|
||||
mod header;
|
||||
|
||||
use std::ffi::CString;
|
||||
|
||||
use crate::base_header::BaseHeader;
|
||||
use crate::extra_field_header::ExtraFieldHeader;
|
||||
|
||||
struct GzipFile {
|
||||
header: FullHeader,
|
||||
pub struct GzipFile {
|
||||
header: header::Header,
|
||||
compressed_data: Vec<u8>,
|
||||
crc32: u32,
|
||||
input_size: u32,
|
||||
}
|
||||
|
||||
struct FullHeader {
|
||||
base_header: BaseHeader,
|
||||
extra_field_header: Option<ExtraFieldHeader>,
|
||||
original_file_name: Option<CString>,
|
||||
comment: Option<CString>,
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user