Files
craft-libs/crates/doc-binary
aiongg 53ac626a31 next CI: derive Preview's Default, restore flatten_write's Cyan ink and main's craft-zip README line, gate the embedded-SVG test on tree, fix doc links
- craft-behavior: Preview<V> derives Default with #[default] Unchanged (clippy::derivable_impls on Rust 1.100).
- craft-prepress tests/flatten_write.rs: main's even-odd test needs the Cyan ink const.
- craft-svg: the embedded-SVG scene test needs the data-URL image resolver, which is behind feature tree.
- Docs: no links to private items (markdown, dwg, ui-patterns), no link to a crate that is not a dependency (frame), write() for the function (doc-binary), an autolink for the URL (update), Self::from_bytes (psd). craft-midi's source_timed and craft-pdf's cos::content keep only their inner docs, so their links resolve inside the module.
- README: main's craft-zip entry with its lenient reader sentence, lost in the 17 Nov merge.
2026-11-17 14:28:37 +00:00
..

craft-doc-binary

Reader and writer for the Word 97-2003 binary file format (.doc, [MS-DOC]) and the craft-wordmodel document model. It is WordCraft's reader and writer, on craft-cfb for the compound file and craft-codepage for 8-bit text.

// Not a compound file: an error, never a panic.
assert!(craft_doc_binary::read(b"not a .doc").is_err());
assert!(!craft_doc_binary::is_encrypted(b"not a .doc"));

// A document written as a .doc reads back.
let bytes = craft_doc_binary::write(&craft_wordmodel::Document::from_text("Hello")).unwrap();
let doc = craft_doc_binary::read(&bytes).unwrap();
assert_eq!(doc.body[0].as_para().map(|p| p.text.as_str()), Some("Hello"));
  • read(bytes), read_with_password(bytes, Option<&str>), is_encrypted(bytes), write(&document).
  • Text in pieces (8-bit and UTF-16), paragraph and character formatting, styles, lists, tables, sections, headers and footers, notes, comments, fields, bookmarks, pictures and OfficeArt shapes, document properties.
  • XOR obfuscation, RC4 and RC4 CryptoAPI password protection.
  • Tracked insertions and deletions, with their author and time.

write produces a Word 97-2003 file (nFib 0x00C1, a version 3 compound file): text, character and paragraph formatting, styles, fonts, lists, tables, sections with headers and footers, footnotes and endnotes, fields and hyperlinks, bookmarks, inline pictures, tracked insertions and deletions, and the document properties. Floating pictures are written in line, nested tables become paragraphs of their cell, and shapes, charts, text boxes and comments are left out, each kind logged once per save. It never writes a password-protected file.

Limits

Streams (1 GiB each, refused by craft-cfb before anything is allocated), paragraphs, pieces, property counts, table nesting and drawing records are capped; the constants are in the source next to what they bound, and each has a test. Hostile input gives an error or a best-effort document, never a panic.