pub const HEADER_DIR_TOKEN: &str = "@header_dir";
use crate::err;
use crate::error::{ErrorKind, Result};
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Location {
Inline { encoding: TextEncoding },
Embedded,
Attachment { position: u64, size: u64 },
Path { path: String, index: Option<u64> },
Url { url: String, index: Option<u64> },
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum TextEncoding {
Base64,
Hex,
}
impl Location {
pub fn parse(text: &str) -> Result<Self> {
let text = text.trim();
if let Some(location) = Self::parse_parenthesised(text)? {
return Ok(location);
}
let (scheme, rest) = match text.split_once(':') {
Some((scheme, rest)) => (scheme, Some(rest)),
None => (text, None),
};
match scheme {
"embedded" => Ok(Location::Embedded),
"inline" => {
let encoding = match rest {
Some("base64") => TextEncoding::Base64,
Some("hex") => TextEncoding::Hex,
other => {
return Err(err!(
BadAttribute,
"inline encoding must be base64 or hex, got {:?}",
other.unwrap_or("")
));
}
};
Ok(Location::Inline { encoding })
}
"attachment" => {
let rest = rest.unwrap_or("");
let (position, size) = rest.split_once(':').ok_or_else(|| {
err!(BadAttribute, "attachment needs position:size, got {rest:?}")
})?;
Ok(Location::Attachment {
position: parse_u64(position, "attachment position")?,
size: parse_u64(size, "attachment size")?,
})
}
other => Err(err!(BadAttribute, "unknown location scheme {other:?}")),
}
}
fn parse_parenthesised(text: &str) -> Result<Option<Self>> {
let (scheme, rest) = match text.strip_prefix("path(") {
Some(rest) => ("path", rest),
None => match text.strip_prefix("url(") {
Some(rest) => ("url", rest),
None => return Ok(None),
},
};
let close = rest.rfind(')').ok_or_else(|| {
err!(BadAttribute, "{scheme}(...) is missing its closing parenthesis")
})?;
let locator = rest[..close].trim();
if locator.is_empty() {
return Err(err!(BadAttribute, "{scheme}(...) has an empty locator"));
}
let index = match rest[close + 1..].trim() {
"" => None,
tail => {
let digits = tail
.strip_prefix(':')
.ok_or_else(|| err!(BadAttribute, "unexpected {tail:?} after {scheme}(...)"))?;
Some(parse_block_id(digits.trim())?)
}
};
Ok(Some(if scheme == "path" {
Location::Path { path: locator.to_string(), index }
} else {
Location::Url { url: locator.to_string(), index }
}))
}
pub fn is_external(&self) -> bool {
matches!(self, Location::Path { .. } | Location::Url { .. })
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum ByteOrder {
#[default]
Little,
Big,
}
impl ByteOrder {
pub fn parse(name: &str) -> Result<Self> {
Ok(match name {
"little" => ByteOrder::Little,
"big" => ByteOrder::Big,
other => return Err(err!(BadAttribute, "unknown byteOrder {other:?}")),
})
}
pub fn name(self) -> &'static str {
match self {
ByteOrder::Little => "little",
ByteOrder::Big => "big",
}
}
pub fn is_native(self) -> bool {
(self == ByteOrder::Little) == cfg!(target_endian = "little")
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Codec {
Zlib,
Lz4,
Lz4Hc,
Zstd,
Other(String),
}
impl Codec {
pub fn name(&self) -> &str {
match self {
Codec::Zlib => "zlib",
Codec::Lz4 => "lz4",
Codec::Lz4Hc => "lz4hc",
Codec::Zstd => "zstd",
Codec::Other(name) => name,
}
}
pub fn is_supported(&self) -> bool {
!matches!(self, Codec::Other(_))
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Compression {
pub codec: Codec,
pub uncompressed_size: u64,
pub shuffle_item_size: Option<u64>,
pub subblocks: Vec<(u64, u64)>,
}
impl Compression {
pub fn parse_subblocks(text: &str) -> Result<Vec<(u64, u64)>> {
text.split(':')
.map(|pair| {
let (compressed, uncompressed) = pair.trim().split_once(',').ok_or_else(|| {
err!(BadAttribute, "a subblock must be `compressed,uncompressed`, got {pair:?}")
})?;
let number = |field: &str| -> Result<u64> {
field.trim().parse::<u64>().map_err(|_| {
err!(BadAttribute, "a subblock length must be an integer, got {field:?}")
})
};
Ok((number(compressed)?, number(uncompressed)?))
})
.collect()
}
pub fn parse(text: &str) -> Result<Self> {
let mut fields = text.split(':');
let method = fields.next().unwrap_or("");
let (name, shuffled) = match method.strip_suffix("+sh") {
Some(name) => (name, true),
None => (method, false),
};
let codec = match name {
"zlib" => Codec::Zlib,
"lz4" => Codec::Lz4,
"lz4hc" => Codec::Lz4Hc,
"zstd" => Codec::Zstd,
other => Codec::Other(other.to_string()),
};
let uncompressed_size = fields
.next()
.ok_or_else(|| err!(BadAttribute, "compression needs an uncompressed size"))
.and_then(|s| parse_u64(s, "uncompressed size"))?;
let shuffle_item_size = match (shuffled, fields.next()) {
(true, Some(item)) => {
let size = parse_u64(item, "shuffle item size")?;
if size < 2 {
return Err(err!(
BadAttribute,
"a shuffled block needs an item size of 2 or more, got {size}"
));
}
Some(size)
}
(true, None) => {
return Err(err!(BadAttribute, "a shuffled block must state its item size"));
}
(false, _) => None,
};
Ok(Compression { codec, uncompressed_size, shuffle_item_size, subblocks: Vec::new() })
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ChecksumAlgorithm {
Sha1,
Sha256,
Sha512,
Sha3_256,
Sha3_512,
}
impl ChecksumAlgorithm {
pub fn parse(name: &str) -> Result<Self> {
Ok(match name {
"sha-1" | "sha1" => ChecksumAlgorithm::Sha1,
"sha-256" | "sha256" => ChecksumAlgorithm::Sha256,
"sha-512" | "sha512" => ChecksumAlgorithm::Sha512,
"sha3-256" => ChecksumAlgorithm::Sha3_256,
"sha3-512" => ChecksumAlgorithm::Sha3_512,
other => return Err(err!(Unsupported, "unknown checksum algorithm {other:?}")),
})
}
pub fn name(self) -> &'static str {
match self {
ChecksumAlgorithm::Sha1 => "sha-1",
ChecksumAlgorithm::Sha256 => "sha-256",
ChecksumAlgorithm::Sha512 => "sha-512",
ChecksumAlgorithm::Sha3_256 => "sha3-256",
ChecksumAlgorithm::Sha3_512 => "sha3-512",
}
}
pub fn digest_len(self) -> usize {
match self {
ChecksumAlgorithm::Sha1 => 20,
ChecksumAlgorithm::Sha256 | ChecksumAlgorithm::Sha3_256 => 32,
ChecksumAlgorithm::Sha512 | ChecksumAlgorithm::Sha3_512 => 64,
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Checksum {
pub algorithm: ChecksumAlgorithm,
pub digest: Vec<u8>,
}
impl Checksum {
pub fn parse(text: &str) -> Result<Self> {
let (name, digest) = text
.split_once(':')
.ok_or_else(|| err!(BadAttribute, "checksum needs algorithm:digest, got {text:?}"))?;
let algorithm = ChecksumAlgorithm::parse(name)?;
if digest.len() != algorithm.digest_len() * 2 {
return Err(err!(
BadAttribute,
"{} needs {} hex digits, got {}",
algorithm.name(),
algorithm.digest_len() * 2,
digest.len()
));
}
let bytes = (0..digest.len())
.step_by(2)
.map(|i| u8::from_str_radix(&digest[i..i + 2], 16))
.collect::<core::result::Result<Vec<u8>, _>>()
.map_err(|_| err!(BadAttribute, "checksum digest is not hexadecimal"))?;
Ok(Checksum { algorithm, digest: bytes })
}
}
fn parse_block_id(text: &str) -> Result<u64> {
let (digits, radix) = match text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) {
Some(hex) => (hex, 16),
None => (text, 10),
};
if digits.is_empty() {
return Err(err!(BadAttribute, "a block identifier is empty"));
}
u64::from_str_radix(digits, radix)
.map_err(|_| err!(BadAttribute, "{text:?} is not a block identifier"))
}
fn parse_u64(text: &str, what: &str) -> Result<u64> {
text.parse::<u64>().map_err(|_| {
crate::error::Error::new(
ErrorKind::BadAttribute,
format!("{what} must be a decimal integer, got {text:?}"),
)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn locations_parse() {
assert_eq!(
Location::parse("inline:base64").unwrap(),
Location::Inline { encoding: TextEncoding::Base64 }
);
assert_eq!(
Location::parse("inline:hex").unwrap(),
Location::Inline { encoding: TextEncoding::Hex }
);
assert_eq!(Location::parse("embedded").unwrap(), Location::Embedded);
assert_eq!(
Location::parse("attachment:9869:23042").unwrap(),
Location::Attachment { position: 9869, size: 23042 }
);
}
#[test]
fn path_and_url_are_parenthesised() {
assert_eq!(
Location::parse("url(file://host/blocks.xisb)").unwrap(),
Location::Url { url: "file://host/blocks.xisb".into(), index: None }
);
assert_eq!(
Location::parse("path(/data/blocks.xisb)").unwrap(),
Location::Path { path: "/data/blocks.xisb".into(), index: None }
);
assert_eq!(
Location::parse("url(http://host:8080/a/b?c=d&e=f)").unwrap(),
Location::Url { url: "http://host:8080/a/b?c=d&e=f".into(), index: None }
);
}
#[test]
fn block_identifiers_may_be_hexadecimal_or_decimal() {
assert_eq!(
Location::parse("path(/d/b.xisb):0x7a73526b584c6167").unwrap(),
Location::Path { path: "/d/b.xisb".into(), index: Some(0x7a73_526b_584c_6167) }
);
assert_eq!(
Location::parse("url(file:///d/b.xisb):0").unwrap(),
Location::Url { url: "file:///d/b.xisb".into(), index: Some(0) }
);
assert_eq!(
Location::parse("path(/d/b.xisb):42").unwrap(),
Location::Path { path: "/d/b.xisb".into(), index: Some(42) }
);
}
#[test]
fn a_locator_may_contain_parentheses() {
assert_eq!(
Location::parse("url(ftp://ftp.example.com/public/example(2016).dat)").unwrap(),
Location::Url {
url: "ftp://ftp.example.com/public/example(2016).dat".into(),
index: None
}
);
}
#[test]
fn bad_locations_are_rejected() {
for text in [
"",
"inline",
"inline:utf8",
"attachment",
"attachment:1",
"wat",
"path(", "path()", "url()",
"path(/a):", "path(/a):zz", "path(/a)junk", ] {
assert!(Location::parse(text).is_err(), "{text:?} should not parse");
}
}
#[test]
fn byte_order_defaults_to_little_endian() {
assert_eq!(ByteOrder::default(), ByteOrder::Little);
assert_eq!(ByteOrder::parse("little").unwrap(), ByteOrder::Little);
assert_eq!(ByteOrder::parse("big").unwrap(), ByteOrder::Big);
assert!(ByteOrder::parse("network").is_err());
assert!(ByteOrder::parse("Little").is_err(), "the spelling is lower case");
}
#[test]
fn compression_parses_with_and_without_shuffling() {
assert_eq!(
Compression::parse("zlib:30000").unwrap(),
Compression {
codec: Codec::Zlib,
uncompressed_size: 30000,
shuffle_item_size: None,
subblocks: Vec::new(),
}
);
assert_eq!(
Compression::parse("zlib+sh:30000:4").unwrap(),
Compression {
codec: Codec::Zlib,
uncompressed_size: 30000,
shuffle_item_size: Some(4),
subblocks: Vec::new(),
}
);
assert_eq!(Compression::parse("lz4hc+sh:100:2").unwrap().codec, Codec::Lz4Hc);
}
#[test]
fn a_shuffled_block_must_say_how_wide_its_items_are() {
assert!(Compression::parse("zlib+sh:30000").is_err());
assert!(Compression::parse("zlib+sh:30000:1").is_err());
}
#[test]
fn checksums_parse_both_spellings() {
let digest = "d60477d1c651b6bc42a8aa938a6132006257d76f229a2d7481db84d23dea8b96";
let c = Checksum::parse(&format!("sha256:{digest}")).unwrap();
assert_eq!(c.algorithm, ChecksumAlgorithm::Sha256);
assert_eq!(c.digest.len(), 32);
assert_eq!(c.digest[0], 0xd6);
assert_eq!(Checksum::parse(&format!("sha-256:{digest}")).unwrap(), c);
}
#[test]
fn a_digest_of_the_wrong_length_is_rejected() {
assert!(Checksum::parse("sha256:abcd").is_err());
assert!(Checksum::parse("sha256:zz").is_err());
assert!(Checksum::parse("md5:00112233445566778899aabbccddeeff").is_err());
}
}