Files
craft-libs/crates/opc/src/write.rs
T
Dylan M. Taylor 6f05ad84a3 Move craft-opc, -odf, -kra, -ora, -penpot, -fig and -sketch onto craft-zip (#995)
* craft-kra, craft-ora: read and write through craft-zip

Both drop their own bounded entry read (and craft-ora its zip writer). New: the declared entry
count is checked before the archive is indexed (MAX_ENTRIES, 100,000), and an archive with two
entries of one name is refused. craft-ora writes a fixed timestamp. zip_mimetype moves into
craft-zip as stored_mimetype; both crates keep their function.

* craft-zip: Archive is Clone; compressed_size and read_head

For the package crates moving onto it: a clone reads behind &self with its own count,
compressed_size gives a format the ratio an entry claims, and read_head reads a signature
without inflating the rest.

* craft-sketch: read through craft-zip

Its own budgets (ratio, per-member, JSON total, image total) stay. New: the declared entry
count is checked before the archive is indexed (MAX_ENTRIES, 100,000), also when sniffing, and
an archive with two entries of one name is refused.

* craft-fig: read and write through craft-zip

The image budget (per image, total, charged for failed reads, skipped count) stays this crate's.
A failed image read is now charged its declared size, which bounds it because the read stops one
byte past it; the separate checksum check at an exact budget goes, as the read now always reaches
the end of the entry. New: the declared entry count is checked before the archive is indexed
(MAX_ENTRIES, 1,000,000), also when sniffing, and two entries of one name are refused. The
writer writes a fixed timestamp.

* craft-penpot: read and write through craft-zip

The 1 GiB byte budget is now the archive's total in craft-zip, counted from bytes actually read;
the JSON value budget and the sticky exhaustion flag stay here. Duplicate names are found by
craft-zip (the hand-read end record goes). A read that does not fit what is left is refused
before it starts and fails every later check(); one that fails otherwise is charged its declared
size. embedded_fonts reads four bytes of each candidate with read_head. New: the entry count is
checked before the archive is indexed.

* craft-opc: read and write through craft-zip

The entry-count guard and its test sample move to craft-zip; check_entry_count and
zip64_entry_count_sample stay here as wrappers. Package and LazyPackage keep their own counts and
rules and read through craft-zip's bounded read. A part that declares more than is left is now
refused before it is inflated, so it no longer spends the inflate budget (the test that pinned
the old charge is updated). An exactly repeated entry name is logged, not refused. ZipOut wraps
craft-zip's Writer; its output is unchanged in kind (level 6, fixed timestamp).

* craft-odf: read and write through craft-zip

The entry-count guard moves to craft-zip; zip_guard::check and zip64_entry_count_sample stay
public here as wrappers. Package keeps its own count and reads through craft-zip's bounded
read: a damaged file is still Ok(None), now charged its declared size. Two files of one name
are refused. encrypt read every file whole and decrypt cut a file at the limit without saying
so; both now refuse a file declaring more than the per-file limit.

* craft-ora: a needless borrow in a test; Cargo.lock for the crates now on craft-zip
2026-11-11 19:48:05 -05:00

212 lines
7.7 KiB
Rust

//! Writing a package.
use std::collections::{HashMap, HashSet};
use craft_xml::Writer;
use crate::name::{normalize, rels_path};
use crate::{CT_RELATIONSHIPS, Error, NS_CONTENT_TYPES, NS_RELATIONSHIPS, RT_BASE};
/// How an entry is stored.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Compression {
Deflate,
Store,
}
/// [`Compression::Store`] for a name whose extension says the data is already compressed
/// (pictures, video, audio, archives), which gains nothing from deflate; otherwise
/// [`Compression::Deflate`].
pub fn compression_for(name: &str) -> Compression {
const PACKED: [&str; 19] =
["png", "jpg", "jpeg", "gif", "webp", "mp4", "m4v", "m4a", "mov", "mp3", "wmv", "wma", "avi", "ogg", "webm", "zip", "docx", "xlsx", "pptx"];
let ext = name.rsplit_once('.').map_or("", |(_, e)| e);
if PACKED.iter().any(|p| ext.eq_ignore_ascii_case(p)) { Compression::Store } else { Compression::Deflate }
}
/// A ZIP archive being written. The output is deterministic: deflate level 6 and the same
/// (1980) timestamp on every entry.
#[derive(Default)]
pub struct ZipOut {
zip: craft_zip::Writer,
}
impl ZipOut {
pub fn new() -> ZipOut {
ZipOut::default()
}
/// Adds an entry. The caller keeps names unique.
pub fn add(&mut self, name: &str, data: &[u8], compression: Compression) -> Result<(), Error> {
let compression = match compression {
Compression::Deflate => craft_zip::Compression::Deflated,
Compression::Store => craft_zip::Compression::Stored,
};
self.zip.add(name, data, compression).map_err(crate::zip_error)
}
pub fn finish(self) -> Result<Vec<u8>, Error> {
self.zip.finish().map_err(crate::zip_error)
}
}
/// A ZIP archive of `entries` in order, each stored as [`compression_for`] its name says.
pub fn zip_entries(entries: &[(String, Vec<u8>)]) -> Result<Vec<u8>, Error> {
let mut zip = ZipOut::new();
for (name, bytes) in entries {
zip.add(name, bytes, compression_for(name))?;
}
zip.finish()
}
/// One relationship being written.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RelOut {
pub id: String,
/// The full relationship type URI.
pub ty: String,
pub target: String,
pub external: bool,
}
/// The relationships being built for one part. Ids are `rId1`, `rId2`… in the order added.
#[derive(Clone, Debug, Default)]
pub struct RelsOut {
pub items: Vec<RelOut>,
/// (type, target, external) → id of the first relationship made for it.
added: HashMap<(String, String, bool), String>,
}
fn full_type(kind: &str) -> String {
if kind.starts_with("http") { kind.to_string() } else { format!("{RT_BASE}{kind}") }
}
impl RelsOut {
/// Adds a relationship, or reuses the one already made for this type, target and mode.
/// `kind` is a full type URI or a last segment of the standard namespace ([`RT_BASE`]).
/// Returns its id.
pub fn add(&mut self, kind: &str, target: &str, external: bool) -> String {
let key = (full_type(kind), target.to_string(), external);
match self.added.get(&key) {
Some(id) => id.clone(),
None => self.add_new(kind, target, external),
}
}
/// Always adds a new relationship, even if an identical one exists. [`RelsOut::add`] reuses
/// the first relationship for a target, whichever call made it.
pub fn add_new(&mut self, kind: &str, target: &str, external: bool) -> String {
let ty = full_type(kind);
let id = format!("rId{}", self.items.len() + 1);
self.added.entry((ty.clone(), target.to_string(), external)).or_insert_with(|| id.clone());
self.items.push(RelOut { id: id.clone(), ty, target: target.to_string(), external });
id
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
/// The relationships part.
pub fn to_xml(&self) -> Vec<u8> {
let mut w = Writer::document();
w.open("Relationships", &[("xmlns", NS_RELATIONSHIPS)]);
for r in &self.items {
w.tag("Relationship")
.attr("Id", &r.id)
.attr("Type", &r.ty)
.attr("Target", &r.target)
.attr_opt("TargetMode", r.external.then_some("External"))
.empty();
}
w.into_bytes()
}
}
/// A package being written: parts, their content types, and `[Content_Types].xml` as the first
/// entry.
pub struct PackageOut {
files: Vec<(String, Vec<u8>, Compression)>,
defaults: Vec<(String, String)>,
overrides: Vec<(String, String)>,
}
impl Default for PackageOut {
fn default() -> PackageOut {
PackageOut {
files: Vec::new(),
defaults: vec![("rels".into(), CT_RELATIONSHIPS.into()), ("xml".into(), "application/xml".into())],
overrides: Vec::new(),
}
}
}
impl PackageOut {
/// Adds an XML part with a content type Override.
pub fn xml_part(&mut self, name: &str, content_type: &str, data: Vec<u8>) {
self.overrides.push((format!("/{name}"), content_type.to_string()));
self.files.push((name.to_string(), data, Compression::Deflate));
}
/// Adds (or replaces) the relationships part of `part`; of the package itself for `""`.
/// Nothing is written for no relationships.
pub fn rels(&mut self, part: &str, rels: &RelsOut) {
if rels.is_empty() {
return;
}
let path = rels_path(part);
match self.files.iter_mut().find(|(name, ..)| *name == path) {
Some(existing) => existing.1 = rels.to_xml(),
None => self.files.push((path, rels.to_xml(), Compression::Deflate)),
}
}
/// Adds a part with no content type entry of its own: one the `rels` and `xml` Defaults
/// cover.
pub fn raw(&mut self, name: &str, data: Vec<u8>) {
self.files.push((name.to_string(), data, compression_for(name)));
}
/// Adds a binary part. Its extension gets a Default content type; when the extension
/// already has another, the part gets an Override.
pub fn binary(&mut self, name: &str, content_type: &str, data: Vec<u8>) {
let ext = name.rsplit('.').next().unwrap_or("bin").to_ascii_lowercase();
if !self.defaults.iter().any(|(e, _)| *e == ext) {
self.defaults.push((ext, content_type.to_string()));
} else if !self.defaults.iter().any(|(e, c)| *e == ext && c == content_type) {
self.overrides.push((format!("/{name}"), content_type.to_string()));
}
self.files.push((name.to_string(), data, compression_for(name)));
}
/// `[Content_Types].xml` for what has been added.
pub fn content_types_xml(&self) -> Vec<u8> {
let mut w = Writer::document();
w.open("Types", &[("xmlns", NS_CONTENT_TYPES)]);
for (e, c) in &self.defaults {
w.empty("Default", &[("Extension", e), ("ContentType", c)]);
}
for (p, c) in &self.overrides {
w.empty("Override", &[("PartName", p), ("ContentType", c)]);
}
w.into_bytes()
}
/// The archive. A second part under a name already written (in any case) is left out with
/// a logged warning: entry names must be unique, or readers reject the package or resolve
/// it differently.
pub fn finish(self) -> Result<Vec<u8>, Error> {
let mut zip = ZipOut::new();
zip.add("[Content_Types].xml", &self.content_types_xml(), Compression::Deflate)?;
let mut written = HashSet::new();
for (name, data, compression) in &self.files {
if !written.insert(normalize(name)) {
log::warn!("opc: part {name} written twice; keeping the first");
continue;
}
zip.add(name, data, *compression)?;
}
zip.finish()
}
}