[format][library] Books: EPUB/FB2/TXT/MOBI/AZW3 into one sanitized model, CFI anchors, the event-log library, stats, CSV import, export
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
//! DRM detection (docs/FORMATS.md §2). Detection only: there is no decrypt path anywhere in Liber.
|
||||
|
||||
use crate::xml;
|
||||
use crate::zip::Zip;
|
||||
use crate::{FormatError, Limits, Result};
|
||||
|
||||
/// Font obfuscation is not DRM: these algorithms only scramble embedded fonts.
|
||||
const FONT_OBFUSCATION: &[&str] = &["http://www.idpf.org/2008/embedding", "http://ns.adobe.com/pdf/enc#RC"];
|
||||
|
||||
/// KFX and Topaz containers, refused before anything else looks at the bytes.
|
||||
pub(crate) fn refuse_other_containers(bytes: &[u8]) -> Result<()> {
|
||||
let head = &bytes[..bytes.len().min(1024)];
|
||||
if crate::text::find(head, b"\xeaDRMION\xee").is_some() {
|
||||
return Err(FormatError::Drm);
|
||||
}
|
||||
if bytes.starts_with(b"TPZ") {
|
||||
return Err(FormatError::Drm);
|
||||
}
|
||||
if bytes.starts_with(b"CONT\x02\x00") {
|
||||
return Err(FormatError::Unsupported("KFX books (convert them with the tool that made them)".into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) fn epub(zip: &Zip, limits: &Limits) -> Result<()> {
|
||||
for marker in ["META-INF/rights.xml", "META-INF/license.lcpl", "META-INF/sinf.xml"] {
|
||||
if zip.find(marker).is_some() {
|
||||
return Err(FormatError::Drm);
|
||||
}
|
||||
}
|
||||
if let Some(bytes) = zip.read_name_capped("META-INF/encryption.xml", limits.max_xml as u64)? {
|
||||
let text = crate::text::decode_xml(&bytes);
|
||||
let root = match xml::parse(&text, limits) {
|
||||
Ok(r) => r,
|
||||
// An encryption.xml Liber cannot read: treat the book as protected rather than guess.
|
||||
Err(FormatError::Unsafe(w)) => return Err(FormatError::Unsafe(w)),
|
||||
Err(_) => return Err(FormatError::Drm),
|
||||
};
|
||||
let methods: Vec<String> = root
|
||||
.descendants()
|
||||
.into_iter()
|
||||
.filter(|e| e.is("EncryptionMethod"))
|
||||
.filter_map(|e| e.attr("Algorithm").map(str::to_owned))
|
||||
.collect();
|
||||
if methods.iter().any(|m| !FONT_OBFUSCATION.contains(&m.trim())) {
|
||||
return Err(FormatError::Drm);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The PalmDOC header's encryption type (record 0, offset 12).
|
||||
pub(crate) fn mobi(record0: &[u8]) -> Result<()> {
|
||||
let enc = record0.get(12..14).map(|b| u16::from_be_bytes([b[0], b[1]])).unwrap_or(0);
|
||||
if enc != 0 { Err(FormatError::Drm) } else { Ok(()) }
|
||||
}
|
||||
Reference in New Issue
Block a user