use super::*; use crate::import::zip::ZipError; /// An archive in memory, read by range. pub(crate) struct Bytes(pub(crate) Vec); impl ZipRangeReader for Bytes { fn size(&self) -> u64 { self.0.len() as u64 } fn read(&self, offset: u64, length: u64) -> Result, ZipError> { let (start, end) = (offset as usize, (offset + length) as usize); self.0.get(start..end).map(<[u8]>::to_vec).ok_or_else(|| ZipError::Reader(format!("{offset}+{length}"))) } } /// A stored (method 0) zip of `files`, written by hand: an invented EPUB. pub(crate) fn zip(files: &[(&str, &[u8])]) -> Bytes { let mut out = Vec::new(); let mut central = Vec::new(); for (name, body) in files { let offset = out.len() as u32; let crc = crc32(body); let header = |signature: u32, central_entry: bool| { let mut h = Vec::new(); h.extend(signature.to_le_bytes()); if central_entry { h.extend(20u16.to_le_bytes()); } h.extend(20u16.to_le_bytes()); // version needed h.extend(0u16.to_le_bytes()); // flags h.extend(0u16.to_le_bytes()); // method: stored h.extend(0u32.to_le_bytes()); // time and date h.extend(crc.to_le_bytes()); h.extend((body.len() as u32).to_le_bytes()); h.extend((body.len() as u32).to_le_bytes()); h.extend((name.len() as u16).to_le_bytes()); h.extend(0u16.to_le_bytes()); // extra if central_entry { h.extend(0u16.to_le_bytes()); // comment h.extend(0u16.to_le_bytes()); // disk h.extend(0u16.to_le_bytes()); // internal h.extend(0u32.to_le_bytes()); // external h.extend(offset.to_le_bytes()); } h.extend(name.as_bytes()); h }; out.extend(header(0x0403_4b50, false)); out.extend(*body); central.extend(header(0x0201_4b50, true)); } let directory_at = out.len() as u32; out.extend(¢ral); out.extend(0x0605_4b50u32.to_le_bytes()); out.extend([0u8; 4]); out.extend((files.len() as u16).to_le_bytes()); out.extend((files.len() as u16).to_le_bytes()); out.extend((central.len() as u32).to_le_bytes()); out.extend(directory_at.to_le_bytes()); out.extend(0u16.to_le_bytes()); Bytes(out) } fn crc32(bytes: &[u8]) -> u32 { let mut crc = 0xFFFF_FFFFu32; for byte in bytes { crc ^= u32::from(*byte); for _ in 0..8 { crc = if crc & 1 == 1 { (crc >> 1) ^ 0xEDB8_8320 } else { crc >> 1 }; } } !crc } pub(crate) const CONTAINER: &[u8] = br#" "#; const COVER: &[u8] = b"\xff\xd8\xff an invented cover"; /// RULE: the core reads an EPUB's own package document — its title, every /// author, language, the identifier that is an ISBN, and the EPUB 3 cover /// image's bytes — so no shell reads an EPUB for its row. #[test] fn an_epub_says_its_title_author_language_isbn_and_cover() { let opf = r#" urn:uuid:12345678-1234-1234-1234-123456789012 urn:isbn:978-0-00-000000-2 The Lantern & the Orchard Irena Valcourt A Second Hand en "#; let book = zip(&[("META-INF/container.xml", CONTAINER), ("OEBPS/content.opf", opf.as_bytes()), ("OEBPS/images/Cover Art.JPG", COVER)]); let facts = read_epub(&book).expect("an EPUB"); assert_eq!(facts.title.as_deref(), Some("The Lantern & the Orchard")); assert_eq!(facts.author.as_deref(), Some("Irena Valcourt, A Second Hand"), "every creator no role names otherwise"); assert_eq!(facts.language.as_deref(), Some("en")); assert_eq!(facts.isbn.as_deref(), Some("9780000000002"), "the identifier that is an ISBN, not the UUID"); assert_eq!(facts.cover, Some(EpubCover { bytes: COVER.to_vec(), extension: "jpg".into() })); } /// RULE: an EPUB 2's cover is the item its `` names; an /// EPUB saying no title says none (the file's name stands in), and a cover /// the manifest names that the zip lacks is no cover, not a refusal. #[test] fn an_epub_2_cover_and_what_an_epub_leaves_unsaid() { let opf = br#" 0-00-000000-9 "#; let book = zip(&[("META-INF/container.xml", CONTAINER), ("OEBPS/content.opf", opf), ("cover.png", b"an invented png")]); let facts = read_epub(&book).expect("an EPUB 2"); assert_eq!(facts.title, None); assert_eq!(facts.isbn.as_deref(), Some("0000000009"), "ten digits an ISBN scheme says"); assert_eq!(facts.cover.map(|cover| cover.extension), Some("png".into())); let lacking = zip(&[("META-INF/container.xml", CONTAINER), ("OEBPS/content.opf", opf)]); assert_eq!(read_epub(&lacking).expect("still a book").cover, None); assert_eq!(title_from_file_name("inbox/the-lantern-orchard.epub").as_deref(), Some("the-lantern-orchard")); assert_eq!(title_from_file_name(".epub"), Some(".epub".into())); assert_eq!(title_from_file_name(" "), None); } /// RULE: a file that is no EPUB is refused by what is wrong with it. #[test] fn what_is_no_epub_is_refused_by_name() { assert!(matches!(read_epub(&Bytes(b"not a zip at all".to_vec())), Err(EpubTrouble::NotAZip(_)))); assert_eq!(read_epub(&zip(&[("mimetype", b"application/epub+zip")])), Err(EpubTrouble::NoPackage)); assert_eq!(read_epub(&zip(&[("META-INF/container.xml", CONTAINER)])), Err(EpubTrouble::Missing("OEBPS/content.opf".into()))); assert_eq!(resolve_href("../a/./b.xhtml#x", "OEBPS/sub/content.opf"), "OEBPS/a/b.xhtml"); } /// RULE: the core reads an EPUB's package by the one OPF rule /// (cotton_rules::opf, the Worker's and the fetcher's too): what read_epub /// says is what the rule says of the OPF the container names. The rule's own /// vectors (crates/cotton-rules/vectors/opf.json) hold what each OPF says. #[test] fn an_epub_s_package_is_the_rule_s_reading() { let opf = std::fs::read(std::path::Path::new(&std::env::var("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR")).join("../cotton-rules/vectors/opf/epub3-whole.opf")).expect("the rule's vector"); let book = zip(&[("META-INF/container.xml", CONTAINER), ("OEBPS/content.opf", &opf)]); let facts = read_epub(&book).expect("an EPUB 3"); assert_eq!(facts.package, cotton_rules::opf::read(&opf)); assert_eq!(facts.series.as_deref(), Some("Glass Seasons")); assert_eq!(read_package(&opf).package, facts.package, "a metadata.opf reads as the same document"); }