#![allow(dead_code)]
use std::io::{Cursor, Read, Write};
use std::path::PathBuf;
use zip::write::SimpleFileOptions;
use zip::{CompressionMethod, ZipArchive, ZipWriter};
pub(crate) const PASSWORD: &str = "password";
pub(crate) const NONASCII_PASSWORD: &str = "äbcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOP";
pub(crate) const B64: &str = "AQIDBA==";
pub(crate) const MIME_TEXT: &str = "application/vnd.oasis.opendocument.text";
pub(crate) fn goldens_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests")
.join("goldens")
}
pub(crate) fn golden_path(name: &str) -> PathBuf {
goldens_dir().join(name)
}
pub(crate) fn load_ladder_fixture(name: &str) -> Vec<u8> {
let path = goldens_dir().with_file_name("ladder").join(name);
std::fs::read(&path).unwrap_or_else(|err| {
panic!(
"ladder fixture missing at {}: {err}. Generate with tests/ladder/make_ladder.py",
path.display()
)
})
}
pub(crate) fn load_golden(name: &str) -> Vec<u8> {
let path = golden_path(name);
std::fs::read(&path).unwrap_or_else(|err| {
panic!(
"golden missing at {}: {err}. Generate with tests/goldens/make_goldens.py",
path.display()
)
})
}
pub(crate) fn zip_namelist(bytes: &[u8]) -> Vec<String> {
let mut z = ZipArchive::new(Cursor::new(bytes)).unwrap();
(0..z.len())
.map(|i| z.by_index(i).unwrap().name().unwrap().to_string())
.collect()
}
pub(crate) fn zip_method(bytes: &[u8], name: &str) -> CompressionMethod {
let mut z = ZipArchive::new(Cursor::new(bytes)).unwrap();
let method = z.by_name(name).unwrap().compression();
method
}
pub(crate) fn read_member(zip_bytes: &[u8], path: &str) -> Vec<u8> {
let mut z = ZipArchive::new(Cursor::new(zip_bytes)).unwrap();
for i in 0..z.len() {
let mut f = z.by_index(i).unwrap();
if f.name().unwrap() == path {
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
return buf;
}
}
panic!("member {path} not in zip");
}
pub(crate) fn zip_with(files: &[(&str, &[u8])]) -> Vec<u8> {
let with_methods: Vec<_> = files
.iter()
.map(|(name, data)| {
let method = if *name == "mimetype" {
CompressionMethod::Stored
} else {
CompressionMethod::Deflated
};
(*name, *data, method)
})
.collect();
zip_with_methods(&with_methods)
}
pub(crate) fn zip_with_methods(files: &[(&str, &[u8], CompressionMethod)]) -> Vec<u8> {
let mut zip = ZipWriter::new(Cursor::new(Vec::new()));
for (name, data, method) in files {
zip.start_file(
*name,
SimpleFileOptions::default().compression_method(*method),
)
.unwrap();
zip.write_all(data).unwrap();
}
zip.finish().unwrap().into_inner()
}
pub(crate) fn append_stored_member(zip: &[u8], name: &str, body: &[u8]) -> Vec<u8> {
let mut src = ZipArchive::new(Cursor::new(zip)).unwrap();
let mut out = ZipWriter::new(Cursor::new(Vec::new()));
for i in 0..src.len() {
let mut file = src.by_index(i).unwrap();
let file_name = file.name().unwrap().to_string();
let method = file.compression();
let mut buf = Vec::new();
file.read_to_end(&mut buf).unwrap();
out.start_file(
&file_name,
SimpleFileOptions::default().compression_method(method),
)
.unwrap();
out.write_all(&buf).unwrap();
}
out.start_file(
name,
SimpleFileOptions::default().compression_method(CompressionMethod::Stored),
)
.unwrap();
out.write_all(body).unwrap();
out.finish().unwrap().into_inner()
}
pub(crate) fn pgp_two_row_zip() -> Vec<u8> {
let manifest = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0" manifest:version="1.3">
<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml" manifest:size="100">
<loext:encrypted-key>
<loext:encryption-method loext:PGPAlgorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p"/>
<loext:KeyInfo>
<loext:PGPData>
<loext:PGPKeyID>{B64}</loext:PGPKeyID>
<loext:PGPKeyPacket>{B64}</loext:PGPKeyPacket>
</loext:PGPData>
</loext:KeyInfo>
<loext:CipherData>
<loext:CipherValue>{B64}</loext:CipherValue>
</loext:CipherData>
</loext:encrypted-key>
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="http://www.w3.org/2009/xmlenc11#aes256-gcm" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
<manifest:file-entry manifest:full-path="styles.xml" manifest:media-type="text/xml" manifest:size="50">
<manifest:encryption-data>
<manifest:algorithm manifest:algorithm-name="http://www.w3.org/2009/xmlenc11#aes256-gcm" manifest:initialisation-vector="{B64}"/>
<manifest:key-derivation manifest:key-derivation-name="PGP"/>
</manifest:encryption-data>
</manifest:file-entry>
</manifest:manifest>
"#
);
zip_with(&[
("mimetype", MIME_TEXT.as_bytes()),
("META-INF/manifest.xml", manifest.as_bytes()),
("content.xml", b"encrypted-content"),
("styles.xml", b"encrypted-styles"),
])
}
pub(crate) fn strict_b64_decode(s: &str) -> Result<Vec<u8>, String> {
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let bytes = s.as_bytes();
if bytes.is_empty() {
return Ok(Vec::new());
}
if !bytes.len().is_multiple_of(4) {
return Err(format!(
"{s:?}: length {} is not a multiple of 4",
bytes.len()
));
}
let pad = bytes.iter().rev().take_while(|&&b| b == b'=').count();
if pad > 2 {
return Err(format!("{s:?}: {pad} padding bytes"));
}
let mut out = Vec::with_capacity(bytes.len() / 4 * 3);
let mut acc = 0u32;
for (i, &b) in bytes[..bytes.len() - pad].iter().enumerate() {
let Some(v) = ALPHABET.iter().position(|&a| a == b) else {
return Err(format!(
"{s:?}: byte {b:#04x} is outside the base64 alphabet"
));
};
acc = (acc << 6) | v as u32;
if i % 4 == 3 {
out.extend_from_slice(&[(acc >> 16) as u8, (acc >> 8) as u8, acc as u8]);
acc = 0;
}
}
match pad {
1 => {
acc <<= 6;
out.extend_from_slice(&[(acc >> 16) as u8, (acc >> 8) as u8]);
}
2 => {
acc <<= 12;
out.push((acc >> 16) as u8);
}
_ => {}
}
Ok(out)
}
#[cfg(feature = "crypto-ops")]
pub(crate) fn unlatched_package() -> Vec<u8> {
let golden = load_golden("lo-legacy-aes-cbc.odt");
let plain = crate::decrypt::decrypt(&golden, PASSWORD).expect("golden decrypts");
let content = read_member(&plain, "content.xml");
let mut src = ZipArchive::new(Cursor::new(&golden)).unwrap();
let mut buf = Vec::new();
let mut out = ZipWriter::new(Cursor::new(&mut buf));
for i in 0..src.len() {
let mut file = src.by_index(i).unwrap();
let name = file.name().unwrap().to_string();
let method = file.compression();
let mut body = Vec::new();
file.read_to_end(&mut body).unwrap();
if name == "content.xml" {
body = content.clone();
} else if name == "META-INF/manifest.xml" {
let xml = String::from_utf8(body).unwrap();
let start = xml.find(r#"manifest:full-path="content.xml""#).unwrap();
let open_end = start + xml[start..].find('>').unwrap();
let entry_end = start + xml[start..].find("</manifest:file-entry>").unwrap();
let head = xml[..open_end].trim_end_matches('/');
body = format!(
"{head}/>{}",
&xml[entry_end + "</manifest:file-entry>".len()..]
)
.into_bytes();
}
out.start_file(
&name,
SimpleFileOptions::default().compression_method(method),
)
.unwrap();
out.write_all(&body).unwrap();
}
out.finish().unwrap();
buf
}