Skip to content

Commit

Permalink
implemented optional serde-feature for Header/Footer
Browse files Browse the repository at this point in the history
  • Loading branch information
ph0llux committed Oct 18, 2023
1 parent 9e0394e commit ac92639
Show file tree
Hide file tree
Showing 18 changed files with 633 additions and 30 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ crc32fast = "1.3.2"
#signing
ed25519-dalek = { version = "2.0", features = [ "rand_core", "digest" ] }
redb = "1.0.5"
# optional deps for features
serde = { version = "1.0", features = ["derive"], optional = true }
hex = { version = "0.4.3", optional = true }

[features]
default = ["serde"]
serde = ["dep:serde", "dep:hex"]

[dev-dependencies]
hex = "0.4.3"
Expand Down
9 changes: 9 additions & 0 deletions src/lib/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ use crate::{
Result,
};

// - external
#[cfg(feature = "serde")]
use serde::{
Deserialize,
Serialize,
};

/// Defines all compression algorithms, which are implemented in zff.
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug,Clone,Eq,PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum CompressionAlgorithm {
/// No compression - encoded as 0 in the header.
None = 0,
Expand Down
13 changes: 13 additions & 0 deletions src/lib/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,22 @@ use byteorder::{LittleEndian, WriteBytesExt};
use rand::{rngs::OsRng, RngCore};
use typenum::consts::U12;

#[cfg(feature = "serde")]
use serde::{
Deserialize,
Serialize,
};


// - type definitions
type Nonce = AesGcmNonce<U12>; //use the (by NIST) recommended nonce size of 96-bit.

/// Defines all encryption algorithms (for use in data and header encryption), which are implemented in zff.
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug,Clone,Eq,PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum EncryptionAlgorithm {
/// AES (128-Bit) in Galois/Counter Mode operation.
/// Encoded with value 0.
Expand All @@ -63,6 +72,8 @@ impl fmt::Display for EncryptionAlgorithm {
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug,Clone,Eq,PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum KDFScheme {
/// KDF scheme PBKDF2-SHA256, with encoding value 0.
PBKDF2SHA256 = 0,
Expand All @@ -87,6 +98,8 @@ impl fmt::Display for KDFScheme {
#[repr(u8)]
#[non_exhaustive]
#[derive(Debug,Clone,Eq,PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum PBEScheme {
/// AES128-CBC encryption scheme used in pbe with the encoding value 0.
AES128CBC = 0,
Expand Down
24 changes: 24 additions & 0 deletions src/lib/footer/file_footer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// - STD
use core::borrow::Borrow;
use std::io::{Cursor, Read};
use std::fmt;

// - internal
use crate::{
Expand All @@ -22,11 +23,20 @@ use crate::header::{
EncryptionInformation,
};

// - external
#[cfg(feature = "serde")]
use serde::{
Deserialize,
Serialize,
};


/// The file footer is written at the end of each acquired file.
/// The file footer contains several metadata about the acquisition process itself: e.g. the acquisition start/end time of the appropriate file,
/// hash values, or size information.
/// The general structure of the file footer is the same for all file types.
#[derive(Debug,Clone,Eq,PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct FileFooter {
/// the version of the [FileFooter].
version: u8,
Expand Down Expand Up @@ -216,4 +226,18 @@ impl HeaderCoding for FileFooter {
let (acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data) = Self::decode_inner_content(&mut cursor)?;
Ok(FileFooter::new(version, file_number, acquisition_start, acquisition_end, hash_header, first_chunk_number, number_of_chunks, length_of_data))
}
}

// - implement fmt::Display
impl fmt::Display for FileFooter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.struct_name())
}
}

// - this is a necassary helper method for fmt::Display and serde::ser::SerializeStruct.
impl FileFooter {
fn struct_name(&self) -> &'static str {
"FileFooter"
}
}
29 changes: 27 additions & 2 deletions src/lib/footer/main_footer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// - STD
use std::io::Cursor;
use std::collections::{BTreeMap};
use std::fmt;

// - internal
use crate::{
Expand All @@ -13,9 +14,19 @@ use crate::{
ENCODING_KEY_DESCRIPTION_NOTES,
};

// - external
#[cfg(feature = "serde")]
use serde::{
Deserialize,
Serialize,
};


/// The main footer is the last thing, which is written at the end of the last segment.\
/// This footer contains a lot of variable information about the zff container (e.g. number of segments, ...).
#[derive(Debug,Clone)]
#[derive(Debug,Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct MainFooter {
version: u8,
number_of_segments: u64,
Expand Down Expand Up @@ -152,4 +163,18 @@ impl HeaderCoding for MainFooter {
description_notes,
footer_offset))
}
}
}

// - implement fmt::Display
impl fmt::Display for MainFooter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.struct_name())
}
}

// - this is a necassary helper method for fmt::Display and serde::ser::SerializeStruct.
impl MainFooter {
fn struct_name(&self) -> &'static str {
"MainFooter"
}
}
103 changes: 97 additions & 6 deletions src/lib/footer/object_footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use core::borrow::Borrow;
use std::io::{Cursor, Read};
use std::collections::HashMap;
use std::fmt;

// - internal
use crate::{
Expand All @@ -27,9 +28,16 @@ use crate::header::{

// - external
use byteorder::{LittleEndian, BigEndian, ReadBytesExt};
#[cfg(feature = "serde")]
use serde::{
Deserialize,
Serialize,
};


/// Each object contains its own object footer.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum ObjectFooter {
/// A physical object contains a [ObjectFooterPhysical].
Physical(ObjectFooterPhysical),
Expand Down Expand Up @@ -123,8 +131,25 @@ impl From<ObjectFooterLogical> for ObjectFooter {
}
}

// - implement fmt::Display
impl fmt::Display for ObjectFooter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.struct_name())
}
}

// - this is a necassary helper method for fmt::Display and serde::ser::SerializeStruct.
impl ObjectFooter {
fn struct_name(&self) -> &'static str {
"ObjectFooter"
}
}


/// Each object contains its own object footer (and this is the encrypted variant).
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(Deserialize))]
pub enum EncryptedObjectFooter {
/// A physical object contains a [EncryptedObjectFooterPhysical].
Physical(EncryptedObjectFooterPhysical),
Expand Down Expand Up @@ -221,8 +246,12 @@ impl EncryptedObjectFooter {
}
}



/// Encrypted footer.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(Deserialize))]
pub struct EncryptedObjectFooterPhysical {
pub version: u8,
pub object_number: u64,
Expand Down Expand Up @@ -289,6 +318,21 @@ impl EncryptedObjectFooterPhysical {
}
}

// - implement fmt::Display
impl fmt::Display for EncryptedObjectFooterPhysical {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.struct_name())
}
}

// - this is a necassary helper method for fmt::Display and serde::ser::SerializeStruct.
impl EncryptedObjectFooterPhysical {
fn struct_name(&self) -> &'static str {
"EncryptedObjectFooterPhysical"
}
}


impl HeaderCoding for EncryptedObjectFooterPhysical {
type Item = EncryptedObjectFooterPhysical;
fn version(&self) -> u8 {
Expand Down Expand Up @@ -323,7 +367,8 @@ impl HeaderCoding for EncryptedObjectFooterPhysical {
/// - the first chunk number, which is used for this physical dump
/// - the total number of chunks, used for this physical dump
/// - a hash header with the appropriate hash values of the underlying physical dump
#[derive(Debug,Clone)]
#[derive(Debug,Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ObjectFooterPhysical {
pub version: u8,
pub object_number: u64,
Expand Down Expand Up @@ -420,6 +465,20 @@ impl ObjectFooterPhysical {
}
}

// - implement fmt::Display
impl fmt::Display for ObjectFooterPhysical {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.struct_name())
}
}

// - this is a necassary helper method for fmt::Display and serde::ser::SerializeStruct.
impl ObjectFooterPhysical {
fn struct_name(&self) -> &'static str {
"ObjectFooterPhysical"
}
}

impl HeaderCoding for ObjectFooterPhysical {
type Item = ObjectFooterPhysical;
fn version(&self) -> u8 {
Expand Down Expand Up @@ -457,7 +516,9 @@ impl HeaderCoding for ObjectFooterPhysical {
}

/// Encrypted footer.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", derive(Deserialize))]
pub struct EncryptedObjectFooterLogical {
pub version: u8,
pub object_number: u64,
Expand Down Expand Up @@ -528,6 +589,20 @@ impl EncryptedObjectFooterLogical {
}
}

// - implement fmt::Display
impl fmt::Display for EncryptedObjectFooterLogical {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.struct_name())
}
}

// - this is a necassary helper method for fmt::Display and serde::ser::SerializeStruct.
impl EncryptedObjectFooterLogical {
fn struct_name(&self) -> &'static str {
"EncryptedObjectFooterLogical"
}
}

impl HeaderCoding for EncryptedObjectFooterLogical {
type Item = EncryptedObjectFooterLogical;
fn version(&self) -> u8 {
Expand Down Expand Up @@ -563,7 +638,9 @@ impl HeaderCoding for EncryptedObjectFooterLogical {
/// - a [HashMap] in which offsets of the corresponding file headers can be found.
/// - a [HashMap] in which segment numbers the corresponding file footers can be found.
/// - a [HashMap] in which offsets the corresponding file footers can be found.
#[derive(Debug,Clone)]
#[derive(Debug,Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct ObjectFooterLogical {
version: u8,
object_number: u64,
Expand Down Expand Up @@ -758,6 +835,20 @@ impl ObjectFooterLogical {
}
}

// - implement fmt::Display
impl fmt::Display for ObjectFooterLogical {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.struct_name())
}
}

// - this is a necassary helper method for fmt::Display and serde::ser::SerializeStruct.
impl ObjectFooterLogical {
fn struct_name(&self) -> &'static str {
"ObjectFooterLogical"
}
}

impl HeaderCoding for ObjectFooterLogical {
type Item = ObjectFooterLogical;

Expand Down
Loading

0 comments on commit ac92639

Please sign in to comment.