mirror of
https://github.com/storytold/indd-utils.git
synced 2026-11-30 01:54:21 +01:00
feat: read the class and unclassed-UID trees
The master page holds three tree roots and counts: objects, the class of each UID, and UIDs without a class. Database::class_of returns an object's class ID, and `indd objects` prints it.
This commit is contained in:
1 parent
326a851346
commit
1d96bb61ae
3 files changed
+72
-19
No files matched your search
+22
-3
@@ -50,9 +50,14 @@ other members.
|
||||
|---|---|
|
||||
| 0x160 | Physical page of the allocation directory (type 2) |
|
||||
| 0x3A8 | Physical page of the logical page directory (type 4) |
|
||||
| 0xB7C | Logical page of the object tree root |
|
||||
| 0xB80 | Object tree depth (2 in the blank document: root plus leaves) |
|
||||
| 0xB88 | Number of object tree leaf entries |
|
||||
| 0xB7C | Root (logical page) of the object tree |
|
||||
| 0xB80 | Root of the class tree |
|
||||
| 0xB84 | Root of the unclassed-UID tree |
|
||||
| 0xB88 | u64: entries in the object tree |
|
||||
| 0xB90 | u64: entries in the class tree |
|
||||
| 0xB98 | u64: entries in the unclassed-UID tree |
|
||||
| 0x130 | Number of object segments on data pages (185 in the blank document, equal to the count of such leaf entries) |
|
||||
| 0x134 | Total bytes of those segments (≈ 3940 × the count above in 8 sampled files) |
|
||||
|
||||
Other fields at 0x100–0x3A8 and 0xB78–0xBAC are not yet identified. 0xBAC
|
||||
holds the document's `xmp.did:` identifier as a NUL-terminated string.
|
||||
@@ -114,6 +119,20 @@ order.
|
||||
The high 16 bits are the slot number, the low 16 bits the byte count, and
|
||||
*page* is a logical page number.
|
||||
|
||||
## Class tree and unclassed-UID tree
|
||||
|
||||
Two more B+ trees use the same page types and leaf layout as the object
|
||||
tree. In both, the segment and page fields are 0.
|
||||
|
||||
- **Class tree:** the length field holds the object's class ID. Sorted by
|
||||
UID, one entry per UID. In the blank document, UID 1 has class 0xE01.
|
||||
- **Unclassed-UID tree:** lists UIDs with no class entry; the length field
|
||||
is 0. Empty in 288 of 351 files.
|
||||
|
||||
In all 351 files, every object with data is in exactly one of the two
|
||||
trees. Some UIDs in either tree have no data: 215 class-tree entries in
|
||||
total, and unclassed entries in 34 files.
|
||||
|
||||
## Slotted pages (type 9)
|
||||
|
||||
Records are stored from offset 0 upwards. A slot directory grows downwards
|
||||
|
||||
+47
-14
@@ -28,8 +28,10 @@ const SLOT_FOOTER: usize = 0xFDC;
|
||||
|
||||
// Fields of the active master page.
|
||||
const MASTER_LOGICAL_DIR: usize = 0x3A8;
|
||||
const MASTER_TREE_ROOT: usize = 0xB7C;
|
||||
const MASTER_TREE_ENTRIES: usize = 0xB88;
|
||||
/// Three u32 tree roots (logical pages): objects, classes, unclassed UIDs.
|
||||
const MASTER_TREE_ROOTS: usize = 0xB7C;
|
||||
/// Three u64 entry counts, in the same order as the roots.
|
||||
const MASTER_TREE_COUNTS: usize = 0xB88;
|
||||
|
||||
/// Largest segment stored on a data page.
|
||||
pub const SEGMENT_MAX: u32 = 0xF70;
|
||||
@@ -70,6 +72,10 @@ pub struct Database<'a> {
|
||||
bytes: &'a [u8],
|
||||
logical: BTreeMap<u32, u32>,
|
||||
entries: Vec<Entry>,
|
||||
/// (UID, class ID), sorted by UID.
|
||||
classes: Vec<(u32, u32)>,
|
||||
/// UIDs listed in the third tree. They have no class entry.
|
||||
unclassed: Vec<u32>,
|
||||
}
|
||||
|
||||
fn corrupt(msg: impl Into<String>) -> Error {
|
||||
@@ -86,17 +92,26 @@ impl<'a> Database<'a> {
|
||||
bytes: container.bytes,
|
||||
logical: BTreeMap::new(),
|
||||
entries: Vec::new(),
|
||||
classes: Vec::new(),
|
||||
unclassed: Vec::new(),
|
||||
};
|
||||
db.read_logical_table(db.u32(master + MASTER_LOGICAL_DIR)?)?;
|
||||
let root = db.u32(master + MASTER_TREE_ROOT)?;
|
||||
let expected = db.u32(master + MASTER_TREE_ENTRIES)? as usize;
|
||||
db.walk(root, 0)?;
|
||||
if db.entries.len() != expected {
|
||||
return Err(corrupt(format!(
|
||||
"object tree has {} entries, master page says {expected}",
|
||||
db.entries.len()
|
||||
)));
|
||||
let mut trees: [Vec<Entry>; 3] = Default::default();
|
||||
for (i, tree) in trees.iter_mut().enumerate() {
|
||||
let root = db.u32(master + MASTER_TREE_ROOTS + 4 * i)?;
|
||||
let expected = db.u32(master + MASTER_TREE_COUNTS + 8 * i)? as usize;
|
||||
db.walk(root, 0, tree)?;
|
||||
if tree.len() != expected {
|
||||
return Err(corrupt(format!(
|
||||
"tree {i} has {} entries, master page says {expected}",
|
||||
tree.len()
|
||||
)));
|
||||
}
|
||||
}
|
||||
let [entries, classes, unclassed] = trees;
|
||||
db.entries = entries;
|
||||
db.classes = classes.iter().map(|e| (e.uid, e.length)).collect();
|
||||
db.unclassed = unclassed.iter().map(|e| e.uid).collect();
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
@@ -155,7 +170,7 @@ impl<'a> Database<'a> {
|
||||
.ok_or_else(|| corrupt(format!("logical page {logical} is not mapped")))
|
||||
}
|
||||
|
||||
fn walk(&mut self, logical: u32, depth: usize) -> Result<(), Error> {
|
||||
fn walk(&self, logical: u32, depth: usize, out: &mut Vec<Entry>) -> Result<(), Error> {
|
||||
if depth > 32 {
|
||||
return Err(corrupt("object tree is too deep"));
|
||||
}
|
||||
@@ -166,7 +181,7 @@ impl<'a> Database<'a> {
|
||||
page_type::TREE_LEAF => {
|
||||
for i in 0..count {
|
||||
let o = base + 4 + 16 * i;
|
||||
self.entries.push(Entry {
|
||||
out.push(Entry {
|
||||
segment: self.u32(o)?,
|
||||
uid: self.u32(o + 4)?,
|
||||
length: self.u32(o + 8)?,
|
||||
@@ -181,11 +196,11 @@ impl<'a> Database<'a> {
|
||||
children.push(self.u32(base + 4 + 12 * i)?);
|
||||
}
|
||||
for child in children {
|
||||
self.walk(child, depth + 1)?;
|
||||
self.walk(child, depth + 1, out)?;
|
||||
}
|
||||
}
|
||||
other => {
|
||||
return Err(corrupt(format!("object tree page {page} has type {other}")));
|
||||
return Err(corrupt(format!("tree page {page} has type {other}")));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -196,6 +211,24 @@ impl<'a> Database<'a> {
|
||||
&self.entries
|
||||
}
|
||||
|
||||
/// Class ID of object `uid`.
|
||||
pub fn class_of(&self, uid: u32) -> Option<u32> {
|
||||
self.classes
|
||||
.binary_search_by_key(&uid, |&(u, _)| u)
|
||||
.ok()
|
||||
.map(|i| self.classes[i].1)
|
||||
}
|
||||
|
||||
/// All (UID, class ID) pairs, sorted by UID.
|
||||
pub fn classes(&self) -> &[(u32, u32)] {
|
||||
&self.classes
|
||||
}
|
||||
|
||||
/// UIDs that have no class (listed in the third tree).
|
||||
pub fn unclassed(&self) -> &[u32] {
|
||||
&self.unclassed
|
||||
}
|
||||
|
||||
fn read_slotted(
|
||||
&self,
|
||||
logical: u32,
|
||||
|
||||
+3
-2
@@ -3,7 +3,7 @@ use std::process::ExitCode;
|
||||
|
||||
const USAGE: &str = "usage:
|
||||
indd info <file.indd>... header, master page and container summary
|
||||
indd objects <file.indd> one line per object: UID, length, first bytes
|
||||
indd objects <file.indd> one line per object: UID, class, length, first bytes
|
||||
indd object <file.indd> <uid> write one object's bytes to stdout";
|
||||
|
||||
fn main() -> ExitCode {
|
||||
@@ -77,7 +77,8 @@ fn objects(path: &str) -> CliResult {
|
||||
for uid in db.uids() {
|
||||
let data = db.object(uid)?.unwrap_or_default();
|
||||
let head: Vec<String> = data.iter().take(24).map(|b| format!("{b:02x}")).collect();
|
||||
writeln!(out, "{uid}\t{}\t{}", data.len(), head.join(" "))?;
|
||||
let class = db.class_of(uid).map_or("-".into(), |c| format!("{c:#x}"));
|
||||
writeln!(out, "{uid}\t{class}\t{}\t{}", data.len(), head.join(" "))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user