1use crate::{
2 Chunk, Error, ExGuid, FileType, Node, ObjectData, PropertySets, Reference, RevisionIndex,
3 Store, Value,
4 bytes::Cursor,
5 store::{Fragment, ListTail, StoreState, crc, transaction_crc},
6};
7
8use std::{
9 collections::{BTreeMap, BTreeSet},
10 sync::Arc,
11};
12
13#[cfg(test)]
14mod tests;
15
16type Result<T> = std::result::Result<T, Error>;
17
18#[cfg(test)]
19thread_local! {
20 /// When set, `fresh_guid` counts from it instead of drawing randomness, so tests can
21 /// compare builds byte for byte.
22 pub(crate) static GUIDS: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) };
23}
24
25pub(crate) fn fresh_guid() -> Result<[u8; 16]> {
26 let mut guid = [0; 16];
27 getrandom::fill(&mut guid).map_err(|_| Error {
28 offset: 0,
29 message: "System random source failed",
30 })?;
31 #[cfg(test)]
32 if let Some(next) = GUIDS.get() {
33 GUIDS.set(Some(next + 1));
34 guid[..8].copy_from_slice(&next.to_le_bytes());
35 guid[8..].copy_from_slice(&0x5eed_u64.to_le_bytes());
36 }
37 guid[7] = (guid[7] & 0x0f) | 0x40;
38 guid[8] = (guid[8] & 0x3f) | 0x80;
39 Ok(guid)
40}
41
42impl ExGuid {
43 pub(crate) fn encode(self, data: &mut Vec<u8>) {
44 data.extend_from_slice(&self.guid);
45 data.extend_from_slice(&self.n.to_le_bytes());
46 }
47}
48
49pub(crate) fn node(id: u16, reference: Option<Reference>, payload: &[u8]) -> Result<Vec<u8>> {
50 let size = 4 + if reference.is_some() { 12 } else { 0 } + payload.len();
51 if size > 0x1fff {
52 return Err(Error {
53 offset: 0,
54 message: "File node exceeds the format size limit",
55 });
56 }
57 let (base, chunk) = match reference {
58 Some(Reference::Data(chunk)) => (1, Some(chunk)),
59 Some(Reference::NodeList(chunk)) => (2, Some(chunk)),
60 None => (0, None),
61 };
62 let header = 0x80000000 | (base << 27) | (u32::try_from(size).unwrap() << 10) | u32::from(id);
63 let mut bytes = header.to_le_bytes().to_vec();
64 if let Some(chunk) = chunk {
65 bytes.extend_from_slice(&chunk.offset.to_le_bytes());
66 bytes.extend_from_slice(
67 &u32::try_from(chunk.length)
68 .map_err(|_| Error {
69 offset: 0,
70 message: "Chunk exceeds the encoded length limit",
71 })?
72 .to_le_bytes(),
73 );
74 }
75 bytes.extend_from_slice(payload);
76 Ok(bytes)
77}
78
79/// A `RevisionRoleAndContextDeclarationFND` payload: `rid` current under `context` in the
80/// default role.
81pub(crate) fn label_payload(rid: ExGuid, context: ExGuid) -> Vec<u8> {
82 let mut payload = Vec::new();
83 rid.encode(&mut payload);
84 payload.extend_from_slice(&1_u32.to_le_bytes());
85 context.encode(&mut payload);
86 payload
87}
88
89pub(crate) fn append(data: &mut Vec<u8>, bytes: &[u8]) -> Result<Chunk> {
90 let length = u64::from(u32::try_from(bytes.len()).map_err(|_| Error {
91 offset: 0,
92 message: "Chunk exceeds the encoded length limit",
93 })?);
94 data.resize(data.len().next_multiple_of(8), 0);
95 let offset = u64::try_from(data.len()).unwrap();
96 data.extend_from_slice(bytes);
97 Ok(Chunk { offset, length })
98}
99
100pub(crate) fn append_list(data: &mut Vec<u8>, id: u32, nodes: &[Vec<u8>]) -> Result<Chunk> {
101 append(data, &fragment(id, 0, nodes))
102}
103
104/// The file-node list fragment `sequence` of list `id`, holding `nodes` and no successor.
105fn fragment(id: u32, sequence: u32, nodes: &[Vec<u8>]) -> Vec<u8> {
106 let mut bytes = 0xa4567ab1f5f7f4c4_u64.to_le_bytes().to_vec();
107 bytes.extend_from_slice(&id.to_le_bytes());
108 bytes.extend_from_slice(&sequence.to_le_bytes());
109 for node in nodes {
110 bytes.extend_from_slice(node);
111 }
112 bytes.resize((bytes.len() + 20).next_multiple_of(8) - 20, 0);
113 bytes.extend_from_slice(&u64::MAX.to_le_bytes());
114 bytes.extend_from_slice(&0_u32.to_le_bytes());
115 bytes.extend_from_slice(&0x8bc215c38233ba4b_u64.to_le_bytes());
116 bytes
117}
118
119pub(crate) fn compact(id: ExGuid, table: &BTreeMap<u32, [u8; 16]>) -> Result<[u8; 4]> {
120 let index = table
121 .iter()
122 .find_map(|(index, guid)| (*guid == id.guid).then_some(*index))
123 .ok_or(Error {
124 offset: 0,
125 message: "Object identity is absent from its global ID table",
126 })?;
127 if id.n > 255 {
128 return Err(Error {
129 offset: 0,
130 message: "Object extension exceeds CompactID capacity",
131 });
132 }
133 Ok(((index << 8) | id.n).to_le_bytes())
134}
135
136/// The global id table entries a property object's references name.
137pub(crate) fn table_entries(bytes: &[u8]) -> Result<Vec<u32>> {
138 let mut entries = Vec::new();
139 for property in PropertySets::parse(bytes)?.sets.iter().flatten() {
140 if let Value::References { compact_ids, .. } = property.value {
141 entries.extend(
142 compact_ids
143 .as_chunks::<4>()
144 .0
145 .iter()
146 .map(|id| u32::from_le_bytes(*id) >> 8),
147 );
148 }
149 }
150 Ok(entries)
151}
152
153fn field_length(property: &crate::Property<'_>, set_lengths: &[usize]) -> usize {
154 match &property.value {
155 Value::NoData => 0,
156 Value::Bytes(bytes) => bytes.len() + usize::from(property.id >> 26 & 31 == 7) * 4,
157 Value::References { .. } => usize::from(property.id >> 26 & 1 != 0) * 4,
158 Value::Sets(children) => {
159 let prefix = if property.id >> 26 & 31 == 16 {
160 if children.is_empty() { 4 } else { 8 }
161 } else {
162 0
163 };
164 prefix + children.clone().map(|i| set_lengths[i]).sum::<usize>()
165 }
166 }
167}
168
169fn property_set_lengths(properties: &PropertySets<'_>) -> Vec<usize> {
170 let mut lengths = vec![0; properties.sets.len()];
171 for (i, set) in properties.sets.iter().enumerate().rev() {
172 lengths[i] =
173 2 + set.len() * 4 + set.iter().map(|p| field_length(p, &lengths)).sum::<usize>();
174 }
175 lengths
176}
177
178fn patch_properties(
179 blob: &[u8],
180 updates: &[(u32, &[u8])],
181 inserts: &[(u32, &[u8])],
182 nested_references: &[u8],
183) -> Result<Vec<u8>> {
184 let properties = PropertySets::parse(blob)?;
185 let root = &properties.sets[0];
186 let ids = properties.root_ids.as_ptr().addr() - blob.as_ptr().addr();
187 let ids_end = ids + properties.root_ids.len();
188 let body_end = blob.len() - properties.padding.len();
189 let set_lengths = property_set_lengths(&properties);
190 let mut offsets = Vec::with_capacity(root.len());
191 let mut offset = ids_end;
192 for property in root {
193 offsets.push(offset);
194 offset += field_length(property, &set_lengths);
195 }
196 let mut patches = Vec::new();
197 let mut added_ids = Vec::new();
198 let mut added_fields = Vec::new();
199 let mut added_references = Vec::new();
200 let object_header = u32::from_le_bytes(blob[..4].try_into().unwrap());
201 let mut object_count = i64::from(object_header & 0xffffff);
202 let mut seen = BTreeSet::new();
203 for (i, &(property, value)) in updates.iter().chain(inserts).enumerate() {
204 if !seen.insert(property & 0x7fffffff) {
205 return Err(Error {
206 offset: 0,
207 message: "Duplicate property update",
208 });
209 }
210 let kind = (property >> 26) & 31;
211 let valid = match kind {
212 2 => value.is_empty(),
213 3..=6 => value.len() == 1 << (kind - 3),
214 7 => value.len() < 0x40000000,
215 8 => value.len() == 4,
216 9 => value.len().is_multiple_of(4) && value.len() / 4 <= 0xffffff,
217 // An encoded property-set array is inserted whole; its references follow.
218 16 => i >= updates.len() && value.len() >= 4,
219 _ => {
220 return Err(Error {
221 offset: 0,
222 message: "Property type cannot be patched",
223 });
224 }
225 };
226 if !valid || (kind != 2 && property & 0x80000000 != 0) {
227 return Err(Error {
228 offset: 0,
229 message: "Replacement has an invalid property value",
230 });
231 }
232 let matches: Vec<_> = root
233 .iter()
234 .enumerate()
235 .filter(|(_, p)| p.id & 0x7fffffff == property & 0x7fffffff)
236 .collect();
237 let adding = i >= updates.len();
238 if matches.len() != usize::from(!adding) {
239 return Err(Error {
240 offset: 0,
241 message: "Property is missing or duplicated",
242 });
243 }
244 let mut encoded = Vec::new();
245 match kind {
246 7 => encoded.extend_from_slice(&(value.len() as u32).to_le_bytes()),
247 9 => encoded.extend_from_slice(&(value.len() as u32 / 4).to_le_bytes()),
248 _ => {}
249 }
250 if (3..=7).contains(&kind) || kind == 16 {
251 encoded.extend_from_slice(value);
252 }
253 if adding {
254 added_ids.extend_from_slice(&property.to_le_bytes());
255 added_fields.extend_from_slice(&encoded);
256 if (8..=9).contains(&kind) {
257 object_count += (value.len() / 4) as i64;
258 added_references.extend_from_slice(value);
259 }
260 } else {
261 let (index, previous) = matches[0];
262 if kind == 2 && previous.id != property {
263 patches.push((
264 ids + index * 4,
265 ids + index * 4 + 4,
266 index,
267 property.to_le_bytes().to_vec(),
268 ));
269 }
270 let start = offsets[index];
271 let end = start + field_length(previous, &set_lengths);
272 if blob[start..end] != encoded {
273 patches.push((start, end, index, encoded));
274 }
275 if let Value::References { compact_ids, .. } = previous.value {
276 object_count += (value.len() / 4) as i64 - (compact_ids.len() / 4) as i64;
277 if compact_ids != value {
278 let start = compact_ids.as_ptr().addr() - blob.as_ptr().addr();
279 patches.push((start, start + compact_ids.len(), index, value.to_vec()));
280 }
281 }
282 }
283 }
284 object_count += (nested_references.len() / 4) as i64;
285 if !(0..=0xffffff).contains(&object_count) {
286 return Err(Error {
287 offset: 0,
288 message: "Object reference stream exceeds the format limit",
289 });
290 }
291 let header = (object_header & 0xff000000) | object_count as u32;
292 if header != object_header {
293 patches.push((0, 4, 0, header.to_le_bytes().to_vec()));
294 }
295 added_references.extend_from_slice(nested_references);
296 if !added_references.is_empty() {
297 let end = 4 + (object_header as usize & 0xffffff) * 4;
298 patches.push((end, end, usize::MAX, added_references));
299 }
300 if !inserts.is_empty() {
301 let count = u16::try_from(root.len() + inserts.len()).map_err(|_| Error {
302 offset: ids - 2,
303 message: "Root property count exceeds the format limit",
304 })?;
305 patches.push((ids - 2, ids, 0, count.to_le_bytes().to_vec()));
306 patches.push((ids_end, ids_end, usize::MAX - 1, added_ids));
307 patches.push((body_end, body_end, usize::MAX, added_fields));
308 }
309 if patches.is_empty() {
310 return Ok(blob.to_vec());
311 }
312 patches.sort_by_key(|(start, end, order, _)| (*start, *end, *order));
313 let mut changed = Vec::new();
314 let mut cursor = 0;
315 for (start, end, _, value) in patches {
316 if start < cursor {
317 return Err(Error {
318 offset: start,
319 message: "Property patches overlap",
320 });
321 }
322 changed.extend_from_slice(&blob[cursor..start]);
323 changed.extend_from_slice(&value);
324 cursor = end;
325 }
326 changed.extend_from_slice(&blob[cursor..body_end]);
327 changed.resize(changed.len().next_multiple_of(8), 0);
328 PropertySets::parse(&changed)?;
329 Ok(changed)
330}
331
332pub(crate) struct ObjectEdit<'a> {
333 pub object: ExGuid,
334 pub updates: &'a [(u32, &'a [u8])],
335 pub inserts: &'a [(u32, &'a [u8])],
336}
337
338/// Objects of `revision` with `edits` applied.
339pub(crate) fn patched(
340 revision: &crate::ResolvedRevision<'_>,
341 edits: &[ObjectEdit<'_>],
342) -> Result<BTreeMap<ExGuid, PropertyObject>> {
343 let mut changed = BTreeMap::new();
344 for edit in edits {
345 if changed.contains_key(&edit.object) {
346 return Err(Error {
347 offset: 0,
348 message: "Duplicate object edit",
349 });
350 }
351 let object = revision.objects.get(&edit.object).ok_or(Error {
352 offset: 0,
353 message: "Object is absent from the active revision",
354 })?;
355 let ObjectData::Properties(blob) = object.data else {
356 return Err(Error {
357 offset: 0,
358 message: "Object does not contain editable properties",
359 });
360 };
361 changed.insert(
362 edit.object,
363 PropertyObject {
364 jcid: object.jcid,
365 bytes: patch_properties(blob, edit.updates, edit.inserts, &[])?,
366 global_ids: Arc::clone(&object.global_ids),
367 },
368 );
369 }
370 Ok(changed)
371}
372
373/// Whether `after` stores as `before` does: the same type and bytes, naming the same
374/// identities. Stored tables keep only the entries an object names, so those decide.
375pub(crate) fn unchanged(before: &crate::Object<'_>, after: &crate::Object<'_>) -> Result<bool> {
376 if before.jcid != after.jcid || before.data != after.data {
377 return Ok(false);
378 }
379 let entries = match after.data {
380 ObjectData::Properties(bytes) => table_entries(bytes)?,
381 _ => Vec::new(),
382 };
383 Ok(entries
384 .iter()
385 .all(|entry| before.global_ids.get(entry) == after.global_ids.get(entry)))
386}
387
388/// `object` as a replacement a revision stores under `id`.
389pub(crate) fn replacement(id: ExGuid, object: &crate::Object<'_>) -> Result<PropertyObject> {
390 let mut replacement = match object.data {
391 ObjectData::Properties(_) => PropertyObject::from_object(object)?,
392 ObjectData::File {
393 reference,
394 extension,
395 } => {
396 let text = |bytes: &[u8]| {
397 String::from_utf16(
398 &bytes
399 .as_chunks::<2>()
400 .0
401 .iter()
402 .map(|pair| u16::from_le_bytes([pair[0], pair[1]]))
403 .collect::<Vec<_>>(),
404 )
405 .map_err(|_| Error {
406 offset: 0,
407 message: "Invalid UTF-16 file-data declaration",
408 })
409 };
410 let mut replacement = PropertyObject::file(id, &text(reference)?, &text(extension)?)?;
411 replacement.jcid = object.jcid;
412 replacement
413 }
414 ObjectData::Encrypted(_) => {
415 return Err(Error {
416 offset: 0,
417 message: "Page edits only produce property objects",
418 });
419 }
420 };
421 replacement.reference(id)?;
422 Ok(replacement)
423}
424
425/// The object type OneNote gives embedded picture payload declarations; embedded files
426/// use `EMBEDDED_FILE_JCID` with the same declaration shape, and a printout's XPS package
427/// `PRINTOUT_FILE_JCID` (`corpus/printout`).
428pub(crate) const FILE_DATA_JCID: u32 = 0x80039;
429pub(crate) const EMBEDDED_FILE_JCID: u32 = 0x80036;
430pub(crate) const PRINTOUT_FILE_JCID: u32 = 0x8003a;
431
432/// The reference and extension of a file-data declaration built by `PropertyObject::file`.
433fn file_declaration(bytes: &[u8]) -> Result<(&[u8], &[u8])> {
434 let malformed = || Error {
435 offset: 0,
436 message: "Malformed file-data declaration",
437 };
438 let mut rest = bytes;
439 let mut parts = Vec::new();
440 for _ in 0..2 {
441 let length = usize::try_from(u32::from_le_bytes(
442 rest.get(..4).ok_or_else(malformed)?.try_into().unwrap(),
443 ))
444 .map_err(|_| malformed())?;
445 let bytes = rest.get(4..4 + length).ok_or_else(malformed)?;
446 parts.push(bytes);
447 rest = &rest[4 + length..];
448 }
449 if !rest.is_empty() {
450 return Err(malformed());
451 }
452 Ok((parts[0], parts[1]))
453}
454
455/// An object of type `jcid` stored as `bytes`, as a revision declares it.
456pub(crate) fn declared(
457 jcid: u32,
458 bytes: &[u8],
459 global_ids: Arc<BTreeMap<u32, [u8; 16]>>,
460) -> Result<crate::Object<'_>> {
461 let data = if is_file_declaration(jcid) {
462 let (reference, extension) = file_declaration(bytes)?;
463 ObjectData::File {
464 reference,
465 extension,
466 }
467 } else {
468 ObjectData::Properties(bytes)
469 };
470 Ok(crate::Object {
471 jcid,
472 reference_count: 0,
473 data,
474 global_ids,
475 })
476}
477
478fn is_file_declaration(jcid: u32) -> bool {
479 matches!(
480 jcid,
481 FILE_DATA_JCID | EMBEDDED_FILE_JCID | PRINTOUT_FILE_JCID
482 )
483}
484
485#[derive(Clone)]
486pub(crate) struct PropertyObject {
487 pub jcid: u32,
488 pub bytes: Vec<u8>,
489 pub global_ids: Arc<BTreeMap<u32, [u8; 16]>>,
490}
491
492pub(crate) enum RevisionEdit {
493 Update(BTreeMap<ExGuid, PropertyObject>),
494 #[cfg(test)]
495 Create {
496 roots: BTreeMap<u32, ExGuid>,
497 objects: BTreeMap<ExGuid, PropertyObject>,
498 },
499}
500
501impl PropertyObject {
502 /// A file-data object declaring an embedded payload by its store identity, or an
503 /// external payload by name; `extension` includes its leading dot.
504 pub fn file(id: ExGuid, reference: &str, extension: &str) -> Result<Self> {
505 if reference.is_empty() || reference.contains('\0') || extension.contains('\0') {
506 return Err(Error {
507 offset: 0,
508 message: "File-data references and extensions are nonempty and contain no NUL",
509 });
510 }
511 let mut bytes = Vec::new();
512 for text in [reference, extension] {
513 let encoded: Vec<u8> = text.encode_utf16().flat_map(u16::to_le_bytes).collect();
514 bytes.extend_from_slice(&u32::try_from(encoded.len()).unwrap().to_le_bytes());
515 bytes.extend_from_slice(&encoded);
516 }
517 Ok(Self {
518 jcid: FILE_DATA_JCID,
519 bytes,
520 global_ids: Arc::new(BTreeMap::from([(0, id.guid)])),
521 })
522 }
523
524 pub fn from_object(object: &crate::Object<'_>) -> Result<Self> {
525 let ObjectData::Properties(bytes) = object.data else {
526 return Err(Error {
527 offset: 0,
528 message: "Object does not contain editable properties",
529 });
530 };
531 Ok(Self {
532 jcid: object.jcid,
533 bytes: bytes.to_vec(),
534 global_ids: Arc::clone(&object.global_ids),
535 })
536 }
537
538 pub fn set(&mut self, values: &[(u32, &[u8])]) -> Result<()> {
539 let properties = PropertySets::parse(&self.bytes)?;
540 let (updates, inserts): (Vec<_>, Vec<_>) = values.iter().copied().partition(|(id, _)| {
541 properties.sets[0]
542 .iter()
543 .any(|p| p.id & 0x7fffffff == id & 0x7fffffff)
544 });
545 self.bytes = patch_properties(&self.bytes, &updates, &inserts, &[])?;
546 Ok(())
547 }
548
549 /// Replaces a property-set array such as note tags: each set lists scalar values
550 /// inline, while an object reference (a kind-8 identity) is supplied as the compact
551 /// identity `reference` produced and joins the object's reference stream in order.
552 pub fn set_sets(&mut self, id: u32, element: u32, sets: &[Vec<(u32, Vec<u8>)>]) -> Result<()> {
553 if (id >> 26) & 31 != 16 || (element >> 26) & 31 != 17 {
554 return Err(Error {
555 offset: 0,
556 message: "Property-set arrays need array and element identifiers",
557 });
558 }
559 self.remove(&[id])?;
560 if sets.is_empty() {
561 return Ok(());
562 }
563 let mut encoded = (sets.len() as u32).to_le_bytes().to_vec();
564 encoded.extend_from_slice(&element.to_le_bytes());
565 let mut references = Vec::new();
566 for set in sets {
567 encoded.extend_from_slice(&u16::try_from(set.len()).unwrap().to_le_bytes());
568 for (field, _) in set {
569 encoded.extend_from_slice(&field.to_le_bytes());
570 }
571 for (field, value) in set {
572 match (field >> 26) & 31 {
573 3..=6 => {
574 assert_eq!(value.len(), 1 << (((field >> 26) & 31) - 3));
575 encoded.extend_from_slice(value);
576 }
577 7 => {
578 encoded.extend_from_slice(&(value.len() as u32).to_le_bytes());
579 encoded.extend_from_slice(value);
580 }
581 8 => {
582 assert_eq!(value.len(), 4);
583 references.extend_from_slice(value);
584 }
585 _ => {
586 return Err(Error {
587 offset: 0,
588 message: "Property-set arrays hold scalars and single references",
589 });
590 }
591 }
592 }
593 }
594 self.bytes = patch_properties(&self.bytes, &[], &[(id, &encoded)], &references)?;
595 Ok(())
596 }
597
598 pub fn remove(&mut self, ids: &[u32]) -> Result<()> {
599 let properties = PropertySets::parse(&self.bytes)?;
600 let removed = |id: u32| {
601 ids.iter()
602 .any(|wanted| id & 0x7fffffff == wanted & 0x7fffffff)
603 };
604 if !properties.sets[0].iter().any(|p| removed(p.id)) {
605 return Ok(());
606 }
607 let lengths = property_set_lengths(&properties);
608 let mut offset = properties.root_ids.as_ptr().addr() - self.bytes.as_ptr().addr()
609 + properties.root_ids.len();
610 let mut retained_ids = Vec::new();
611 let mut fields = Vec::new();
612 let mut references: [Vec<u8>; 3] = std::array::from_fn(|_| Vec::new());
613 for property in &properties.sets[0] {
614 let end = offset + field_length(property, &lengths);
615 if !removed(property.id) {
616 retained_ids.extend_from_slice(&property.id.to_le_bytes());
617 fields.extend_from_slice(&self.bytes[offset..end]);
618 let mut pending = vec![property];
619 while let Some(field) = pending.pop() {
620 match &field.value {
621 Value::References {
622 stream,
623 compact_ids,
624 } => {
625 let index = match stream {
626 crate::IdStream::Objects => 0,
627 crate::IdStream::ObjectSpaces => 1,
628 crate::IdStream::Contexts => 2,
629 };
630 references[index].extend_from_slice(compact_ids);
631 }
632 Value::Sets(children) => {
633 for child in children.clone().rev() {
634 pending.extend(properties.sets[child].iter().rev());
635 }
636 }
637 _ => {}
638 }
639 }
640 }
641 offset = end;
642 }
643 let mut cursor = crate::bytes::Cursor {
644 bytes: &self.bytes,
645 offset: 0,
646 };
647 let streams = crate::properties::reference_streams(&mut cursor)?;
648 let mut bytes = Vec::new();
649 for (stream, retained) in streams.iter().zip(&references) {
650 if stream.offset == 0 {
651 continue;
652 }
653 let header = u32::from_le_bytes(
654 self.bytes[stream.offset - 4..stream.offset]
655 .try_into()
656 .unwrap(),
657 );
658 let count = u32::try_from(retained.len() / 4).unwrap();
659 bytes.extend_from_slice(&((header & 0xff000000) | count).to_le_bytes());
660 bytes.extend_from_slice(retained);
661 }
662 bytes.extend_from_slice(&u16::try_from(retained_ids.len() / 4).unwrap().to_le_bytes());
663 bytes.extend_from_slice(&retained_ids);
664 bytes.extend_from_slice(&fields);
665 bytes.resize(bytes.len().next_multiple_of(8), 0);
666 PropertySets::parse(&bytes)?;
667 self.bytes = bytes;
668 Ok(())
669 }
670
671 pub fn reference(&mut self, id: ExGuid) -> Result<[u8; 4]> {
672 if !self.global_ids.values().any(|guid| *guid == id.guid) {
673 let mut index = 0;
674 for key in self.global_ids.keys() {
675 if *key != index {
676 break;
677 }
678 index += 1;
679 }
680 if index >= 0xffffff || id.guid == [0; 16] {
681 return Err(Error {
682 offset: 0,
683 message: "Object identity cannot be added to the global ID table",
684 });
685 }
686 Arc::make_mut(&mut self.global_ids).insert(index, id.guid);
687 }
688 compact(id, &self.global_ids)
689 }
690
691 pub fn copy_property(&mut self, source: &Self, id: u32) -> Result<()> {
692 let properties = PropertySets::parse(&source.bytes)?;
693 let mut target = Self {
694 jcid: self.jcid,
695 bytes: self.bytes.clone(),
696 global_ids: Arc::clone(&self.global_ids),
697 };
698 target.remove(&[id])?;
699 let lengths = property_set_lengths(&properties);
700 let mut offset = properties.root_ids.as_ptr().addr() - source.bytes.as_ptr().addr()
701 + properties.root_ids.len();
702 let mut selected = None;
703 for property in &properties.sets[0] {
704 let end = offset + field_length(property, &lengths);
705 if property.id & 0x7fffffff == id & 0x7fffffff {
706 selected = Some((property, &source.bytes[offset..end]));
707 break;
708 }
709 offset = end;
710 }
711 if let Some((property, field)) = selected {
712 let mut added: [Vec<u8>; 3] = std::array::from_fn(|_| Vec::new());
713 let mut pending = vec![property];
714 while let Some(property) = pending.pop() {
715 match &property.value {
716 Value::References {
717 stream,
718 compact_ids,
719 } => {
720 let index = match stream {
721 crate::IdStream::Objects => 0,
722 crate::IdStream::ObjectSpaces => 1,
723 crate::IdStream::Contexts => 2,
724 };
725 let mut cursor = crate::bytes::Cursor {
726 bytes: compact_ids,
727 offset: 0,
728 };
729 while !cursor.bytes.is_empty() {
730 let id = cursor.compact(&source.global_ids)?;
731 added[index].extend_from_slice(&target.reference(id)?);
732 }
733 }
734 Value::Sets(children) => {
735 for child in children.clone().rev() {
736 pending.extend(properties.sets[child].iter().rev());
737 }
738 }
739 _ => {}
740 }
741 }
742 let old = PropertySets::parse(&target.bytes)?;
743 let count = u16::try_from(old.sets[0].len() + 1).map_err(|_| Error {
744 offset: 0,
745 message: "Root property count exceeds the format limit",
746 })?;
747 let streams = crate::properties::reference_streams(&mut crate::bytes::Cursor {
748 bytes: &target.bytes,
749 offset: 0,
750 })?;
751 let stream_count = (0..3)
752 .rev()
753 .find(|i| streams[*i].offset != 0 || !added[*i].is_empty())
754 .unwrap()
755 + 1;
756 let mut bytes = Vec::new();
757 for i in 0..stream_count {
758 let stream = &streams[i];
759 let count = u32::try_from((stream.bytes.len() + added[i].len()) / 4)
760 .ok()
761 .filter(|n| *n <= 0xffffff)
762 .ok_or(Error {
763 offset: 0,
764 message: "Reference stream exceeds the format limit",
765 })?;
766 let reserved = if stream.offset == 0 {
767 0
768 } else {
769 u32::from_le_bytes(
770 target.bytes[stream.offset - 4..stream.offset]
771 .try_into()
772 .unwrap(),
773 ) & 0x3f000000
774 };
775 let flags = match (i, stream_count) {
776 (0, 1) => 0x80000000,
777 (0 | 1, 3) => 0x40000000,
778 _ => 0,
779 };
780 bytes.extend_from_slice(&(count | reserved | flags).to_le_bytes());
781 bytes.extend_from_slice(stream.bytes);
782 bytes.extend_from_slice(&added[i]);
783 }
784 bytes.extend_from_slice(&count.to_le_bytes());
785 bytes.extend_from_slice(old.root_ids);
786 bytes.extend_from_slice(&property.id.to_le_bytes());
787 let start =
788 old.root_ids.as_ptr().addr() - target.bytes.as_ptr().addr() + old.root_ids.len();
789 bytes.extend_from_slice(&target.bytes[start..target.bytes.len() - old.padding.len()]);
790 bytes.extend_from_slice(field);
791 bytes.resize(bytes.len().next_multiple_of(8), 0);
792 PropertySets::parse(&bytes)?;
793 target.bytes = bytes;
794 }
795 *self = target;
796 Ok(())
797 }
798}
799
800/// A password-protected section's key, under which a writer stores what it appends.
801pub(crate) trait Protection {
802 /// The stored bytes an object's clear bytes were decoded from.
803 fn stored(&self, clear: &[u8]) -> Option<&[u8]>;
804 fn seal_property(&self, clear: &[u8]) -> Result<Vec<u8>>;
805 fn seal_file(&self, clear: &[u8]) -> Result<Vec<u8>>;
806 fn open_property(&self, stored: &[u8]) -> Result<zeroize::Zeroizing<Vec<u8>>>;
807 fn open_file(&self, stored: &[u8]) -> Result<zeroize::Zeroizing<Vec<u8>>>;
808 /// The encryption data's chunk, which every revision names.
809 fn metadata(&self) -> Chunk;
810}
811
812/// The global-ID entries a group of objects declared together uses: each object's own
813/// and those its properties reference. A section's group stores only these.
814fn used_entries<'o>(
815 table: &BTreeMap<u32, [u8; 16]>,
816 objects: impl IntoIterator<Item = (ExGuid, &'o crate::Object<'o>)>,
817) -> Result<Vec<u32>> {
818 let mut used = Vec::new();
819 for (id, object) in objects {
820 used.push(u32::from_le_bytes(compact(id, table)?) >> 8);
821 if let ObjectData::Properties(bytes) = object.data {
822 used.extend(table_entries(bytes)?);
823 }
824 }
825 used.sort_unstable();
826 used.dedup();
827 Ok(used)
828}
829
830/// A space's revision as appending a revision and reading it back leaves it.
831#[derive(Clone)]
832pub(crate) struct LiveRevision<'a> {
833 pub revision: crate::ResolvedRevision<'a>,
834 /// Incoming references of each object reachable from the roots; a root counts once.
835 incoming: BTreeMap<ExGuid, u32>,
836 /// Reachable read-only property objects by type and content, which identical new
837 /// objects alias.
838 readonly: BTreeMap<u32, BTreeMap<&'a [u8], BTreeSet<ExGuid>>>,
839 /// Revisions in the dependency chain, counted to 512; zero before the first.
840 pub depth: usize,
841}
842
843impl<'a> LiveRevision<'a> {
844 pub(crate) fn new(revision: crate::ResolvedRevision<'a>, depth: usize) -> Result<Self> {
845 let incoming = if depth == 0 {
846 BTreeMap::new()
847 } else {
848 revision.checked_counts()?
849 };
850 let mut readonly = BTreeMap::new();
851 for id in incoming.keys() {
852 index(&mut readonly, *id, &revision.objects[id], true);
853 }
854 Ok(Self {
855 revision,
856 incoming,
857 readonly,
858 depth,
859 })
860 }
861
862 pub(crate) fn is_reachable(&self, id: ExGuid) -> bool {
863 self.incoming.contains_key(&id)
864 }
865
866 /// Validates replacements and drops what the revision would not store: new read-only
867 /// objects identical to reachable or earlier ones, whose references move to those, and
868 /// reachable objects that stay as they are.
869 pub(crate) fn prepare(
870 &self,
871 mut replacements: BTreeMap<ExGuid, PropertyObject>,
872 is_section: bool,
873 ) -> Result<BTreeMap<ExGuid, PropertyObject>> {
874 let revision = &self.revision;
875 for (id, replacement) in &replacements {
876 if let Some(object) = revision.objects.get(id) {
877 if object.jcid & 0x100000 != 0 {
878 return Err(Error {
879 offset: 0,
880 message: "Read-only object requires a new identity",
881 });
882 }
883 if replacement.jcid != object.jcid
884 || !matches!(object.data, ObjectData::Properties(_))
885 {
886 return Err(Error {
887 offset: 0,
888 message: "An existing object's type cannot be changed",
889 });
890 }
891 } else if !is_section && replacement.jcid != 0x20001 {
892 return Err(Error {
893 offset: 0,
894 message: "New objects in a table of contents are its entries",
895 });
896 }
897 if replacement.global_ids.keys().any(|i| *i > 0xffffff) {
898 return Err(Error {
899 offset: 0,
900 message: "Invalid property object declaration",
901 });
902 }
903 compact(*id, &replacement.global_ids)?;
904 if is_file_declaration(replacement.jcid) {
905 if !is_section {
906 return Err(Error {
907 offset: 0,
908 message: "File-data objects require a section file",
909 });
910 }
911 file_declaration(&replacement.bytes)?;
912 } else if replacement.jcid & 0x20000 == 0 {
913 return Err(Error {
914 offset: 0,
915 message: "Invalid property object declaration",
916 });
917 } else {
918 PropertySets::parse(&replacement.bytes)?;
919 }
920 }
921 // Native coalescing of duplicate readonly styles can leave dangling references.
922 let mut aliases = BTreeMap::new();
923 for (id, replacement) in &replacements {
924 if revision.objects.contains_key(id)
925 || replacement.jcid & 0x100000 == 0
926 || PropertySets::parse(&replacement.bytes)?
927 .sets
928 .iter()
929 .flatten()
930 .any(|p| matches!(p.value, Value::References { .. }))
931 {
932 continue;
933 }
934 let existing = self
935 .readonly
936 .get(&replacement.jcid)
937 .and_then(|contents| contents.get(replacement.bytes.as_slice()))
938 .and_then(|ids| ids.first().copied());
939 let existing = existing.or_else(|| {
940 replacements.range(..id).find_map(|(other, object)| {
941 (object.jcid == replacement.jcid && object.bytes == replacement.bytes)
942 .then_some(*aliases.get(other).unwrap_or(other))
943 })
944 });
945 if let Some(existing) = existing {
946 aliases.insert(*id, existing);
947 }
948 }
949 for id in aliases.keys() {
950 replacements.remove(id);
951 }
952 for object in replacements.values_mut() {
953 if is_file_declaration(object.jcid) {
954 continue;
955 }
956 // The compact IDs this object's table gives aliased identities.
957 let targets: Vec<(u32, ExGuid)> = aliases
958 .iter()
959 .filter(|(id, _)| id.n <= 0xff)
960 .filter_map(|(id, existing)| {
961 let (index, _) = object.global_ids.iter().find(|(_, g)| **g == id.guid)?;
962 Some(((index << 8) | id.n, *existing))
963 })
964 .collect();
965 if targets.is_empty() {
966 continue;
967 }
968 let mut remapped = Vec::new();
969 for property in PropertySets::parse(&object.bytes)?.sets.iter().flatten() {
970 if let Value::References {
971 stream: crate::IdStream::Objects,
972 compact_ids,
973 } = property.value
974 {
975 for bytes in compact_ids.as_chunks::<4>().0 {
976 let raw = u32::from_le_bytes(*bytes);
977 if let Some((_, existing)) = targets.iter().find(|(id, _)| *id == raw) {
978 let offset = bytes.as_ptr().addr() - object.bytes.as_ptr().addr();
979 remapped.push((offset, *existing));
980 }
981 }
982 }
983 }
984 for (offset, id) in remapped {
985 let reference = object.reference(id)?;
986 object.bytes[offset..offset + 4].copy_from_slice(&reference);
987 }
988 }
989 // Detached objects must pass the final reachability check even when unchanged. Equal
990 // bytes are the same object when the table entries they name agree: stored tables
991 // keep only those.
992 let mut unchanged = Vec::new();
993 for (id, replacement) in &replacements {
994 if let Some(object) = revision.objects.get(id)
995 && self.is_reachable(*id)
996 && object.data == ObjectData::Properties(&replacement.bytes)
997 && (Arc::ptr_eq(&object.global_ids, &replacement.global_ids)
998 || table_entries(&replacement.bytes)?.iter().all(|entry| {
999 object.global_ids.get(entry) == replacement.global_ids.get(entry)
1000 }))
1001 {
1002 unchanged.push(*id);
1003 }
1004 }
1005 for id in unchanged {
1006 replacements.remove(&id);
1007 }
1008 Ok(replacements)
1009 }
1010
1011 /// Stores prepared replacements as the next revision, which checkpoints when the chain
1012 /// would exceed 512 revisions.
1013 pub(crate) fn commit(
1014 &mut self,
1015 replacements: Vec<(ExGuid, crate::Object<'a>)>,
1016 ) -> Result<Commit> {
1017 self.commit_as(replacements, self.revision.roots.clone())
1018 }
1019
1020 /// `commit`, the revision taking `roots`: a dependent revision declares the roots it adds
1021 /// or moves and inherits the rest, so dropping one takes a checkpoint.
1022 pub(crate) fn commit_as(
1023 &mut self,
1024 replacements: Vec<(ExGuid, crate::Object<'a>)>,
1025 roots: BTreeMap<u32, ExGuid>,
1026 ) -> Result<Commit> {
1027 let dropped = self
1028 .revision
1029 .roots
1030 .keys()
1031 .any(|role| !roots.contains_key(role));
1032 let declared: BTreeMap<u32, ExGuid> = roots
1033 .iter()
1034 .filter(|(role, id)| self.revision.roots.get(role) != Some(id))
1035 .map(|(role, id)| (*role, *id))
1036 .collect();
1037 let before: BTreeSet<ExGuid> = self.revision.roots.values().copied().collect();
1038 self.revision.roots = roots;
1039 let mut changed = BTreeSet::new();
1040 // Whether each object whose count may move was reachable before.
1041 let mut touched = BTreeMap::new();
1042 let (mut added, mut removed) = (Vec::new(), Vec::new());
1043 for (id, object) in replacements {
1044 changed.insert(id);
1045 let reachable = self.is_reachable(id);
1046 touched.insert(id, reachable);
1047 if reachable {
1048 moved_references(
1049 &self.revision.objects[&id],
1050 &object,
1051 &mut removed,
1052 &mut added,
1053 )?;
1054 }
1055 self.revision.objects.insert(id, object);
1056 }
1057 let roots: BTreeSet<ExGuid> = self.revision.roots.values().copied().collect();
1058 if roots.len() != self.revision.roots.len() {
1059 return Err(Error {
1060 offset: 0,
1061 message: "Object is the root of multiple roles",
1062 });
1063 }
1064 // Being a root counts as one reference, which a moved or dropped root gives up.
1065 removed.extend(
1066 before
1067 .iter()
1068 .filter(|id| !roots.contains(id) && self.incoming.contains_key(id))
1069 .copied(),
1070 );
1071 added.extend(
1072 roots
1073 .into_iter()
1074 .filter(|id| !self.incoming.contains_key(id) || !before.contains(id)),
1075 );
1076 // Attaching before detaching keeps a moved subtree from being walked twice.
1077 while let Some(id) = added.pop() {
1078 let object = self.revision.objects.get(&id).ok_or(Error {
1079 offset: 0,
1080 message: "Reachable object has no declaration",
1081 })?;
1082 let count = self.incoming.entry(id).or_default();
1083 touched.entry(id).or_insert(*count > 0);
1084 *count = count.checked_add(1).ok_or(Error {
1085 offset: 0,
1086 message: "Object reference count overflows",
1087 })?;
1088 if *count == 1 {
1089 added.extend(object.references()?.objects);
1090 index(&mut self.readonly, id, object, true);
1091 }
1092 }
1093 while let Some(id) = removed.pop() {
1094 touched.entry(id).or_insert(true);
1095 let count = self.incoming.get_mut(&id).unwrap();
1096 *count -= 1;
1097 if *count == 0 {
1098 self.incoming.remove(&id);
1099 let object = &self.revision.objects[&id];
1100 removed.extend(object.references()?.objects);
1101 index(&mut self.readonly, id, object, false);
1102 }
1103 }
1104 for (&id, &reachable) in &touched {
1105 let count = self.incoming.get(&id).copied();
1106 if changed.contains(&id) && count.is_none() {
1107 return Err(Error {
1108 offset: 0,
1109 message: "Edited object is not reachable in the resulting revision",
1110 });
1111 }
1112 let object = self.revision.objects.get_mut(&id).unwrap();
1113 if (reachable || count.is_some()) && object.reference_count != count.unwrap_or(0) {
1114 object.reference_count = count.unwrap_or(0);
1115 changed.insert(id);
1116 }
1117 }
1118 // Native cold-open fails on long dependency chains; cap their depth at 512.
1119 let checkpoint = self.depth == 0 || self.depth >= 512 || dropped;
1120 self.depth = if checkpoint { 1 } else { self.depth + 1 };
1121 Ok(Commit {
1122 checkpoint,
1123 roots: if checkpoint {
1124 self.revision.roots.clone()
1125 } else {
1126 declared
1127 },
1128 changed,
1129 touched: touched.into_keys().collect(),
1130 })
1131 }
1132
1133 /// Gives the objects a revision declares the id tables reading it back yields: objects
1134 /// sharing a table are declared in one group, which keeps the entries they use.
1135 pub(crate) fn settle(&mut self, declared: impl IntoIterator<Item = ExGuid>) -> Result<()> {
1136 let mut groups = BTreeMap::<_, Vec<_>>::new();
1137 for id in declared {
1138 let table = Arc::clone(&self.revision.objects[&id].global_ids);
1139 groups.entry(table).or_default().push(id);
1140 }
1141 for (table, ids) in groups {
1142 let used = used_entries(
1143 &table,
1144 ids.iter().map(|id| (*id, &self.revision.objects[id])),
1145 )?;
1146 if used.iter().eq(table.keys()) {
1147 continue;
1148 }
1149 let kept: Arc<BTreeMap<_, _>> = Arc::new(
1150 table
1151 .iter()
1152 .filter(|(entry, _)| used.binary_search(entry).is_ok())
1153 .map(|(entry, guid)| (*entry, *guid))
1154 .collect(),
1155 );
1156 for id in ids {
1157 self.revision.objects.get_mut(&id).unwrap().global_ids = Arc::clone(&kept);
1158 }
1159 }
1160 Ok(())
1161 }
1162}
1163
1164/// Adds a reachable read-only property object to `readonly`, or removes an unreachable one.
1165fn index<'a>(
1166 readonly: &mut BTreeMap<u32, BTreeMap<&'a [u8], BTreeSet<ExGuid>>>,
1167 id: ExGuid,
1168 object: &crate::Object<'a>,
1169 reachable: bool,
1170) {
1171 let ObjectData::Properties(bytes) = object.data else {
1172 return;
1173 };
1174 if object.jcid & 0x100000 == 0 {
1175 return;
1176 }
1177 let ids = readonly
1178 .entry(object.jcid)
1179 .or_default()
1180 .entry(bytes)
1181 .or_default();
1182 if reachable {
1183 ids.insert(id);
1184 } else {
1185 ids.remove(&id);
1186 }
1187}
1188
1189/// Adds the object references `before` has and `after`, a later version of the object,
1190/// lacks to `removed`, and those it gains to `added`.
1191fn moved_references(
1192 before: &crate::Object<'_>,
1193 after: &crate::Object<'_>,
1194 removed: &mut Vec<ExGuid>,
1195 added: &mut Vec<ExGuid>,
1196) -> Result<()> {
1197 let compact_ids = |object: &crate::Object<'_>| -> Result<Vec<[u8; 4]>> {
1198 let ObjectData::Properties(bytes) = object.data else {
1199 return Ok(Vec::new());
1200 };
1201 let mut ids = Vec::new();
1202 for property in PropertySets::parse(bytes)?.sets.iter().flatten() {
1203 if let Value::References {
1204 stream: crate::IdStream::Objects,
1205 compact_ids,
1206 } = property.value
1207 {
1208 ids.extend(compact_ids.as_chunks::<4>().0.iter().copied());
1209 }
1210 }
1211 Ok(ids)
1212 };
1213 let decode = |ids: &[[u8; 4]], table| -> Result<Vec<ExGuid>> {
1214 let bytes = ids.concat();
1215 let mut cursor = crate::bytes::Cursor {
1216 bytes: &bytes,
1217 offset: 0,
1218 };
1219 std::iter::from_fn(|| (!cursor.bytes.is_empty()).then(|| cursor.compact(table))).collect()
1220 };
1221 // Equal compact IDs name the same objects where the later table keeps the earlier entries.
1222 let (old, new) = (&before.global_ids, &after.global_ids);
1223 let extended = Arc::ptr_eq(old, new) || {
1224 let mut later = new.iter();
1225 old.iter()
1226 .all(|entry| later.find(|later| later.0 >= entry.0) == Some(entry))
1227 };
1228 if extended {
1229 let (old_ids, new_ids) = (compact_ids(before)?, compact_ids(after)?);
1230 let (gone, gained) = differing(&old_ids, &new_ids);
1231 removed.extend(decode(gone, old)?);
1232 added.extend(decode(gained, new)?);
1233 } else {
1234 let (old_refs, new_refs) = (before.references()?.objects, after.references()?.objects);
1235 let (gone, gained) = differing(&old_refs, &new_refs);
1236 removed.extend_from_slice(gone);
1237 added.extend_from_slice(gained);
1238 }
1239 Ok(())
1240}
1241
1242/// The parts of two sequences between their common prefix and suffix: removing the first
1243/// from `before` and adding the second yields `after` as a multiset.
1244pub(crate) fn differing<'s, T: PartialEq>(before: &'s [T], after: &'s [T]) -> (&'s [T], &'s [T]) {
1245 let prefix = before.iter().zip(after).take_while(|(a, b)| a == b).count();
1246 let suffix = before[prefix..]
1247 .iter()
1248 .rev()
1249 .zip(after[prefix..].iter().rev())
1250 .take_while(|(a, b)| a == b)
1251 .count();
1252 (
1253 &before[prefix..before.len() - suffix],
1254 &after[prefix..after.len() - suffix],
1255 )
1256}
1257
1258/// A revision `LiveRevision::commit` stored.
1259#[derive(Clone)]
1260pub(crate) struct Commit {
1261 /// Whether the revision declares every object, depending on none.
1262 pub checkpoint: bool,
1263 /// The roots it declares: every root of a checkpoint, else those it adds or moves.
1264 pub roots: BTreeMap<u32, ExGuid>,
1265 /// The objects it declares otherwise: the replacements and those whose counts moved.
1266 pub changed: BTreeSet<ExGuid>,
1267 /// Objects whose reachability or reference count may have moved.
1268 pub touched: BTreeSet<ExGuid>,
1269}
1270
1271/// Revisions in the dependency chain of `rid`, counted to 512.
1272pub(crate) fn chain_depth(index: &RevisionIndex<'_>, space: ExGuid, rid: ExGuid) -> usize {
1273 std::iter::successors(Some(rid), |id| {
1274 index.spaces[&space].revisions[id].dependency
1275 })
1276 .take(512)
1277 .count()
1278}
1279
1280/// A test fixture: `source` with one revision of `space` storing `edit`'s objects.
1281#[cfg(test)]
1282pub(crate) fn write_revision(
1283 source: &[u8],
1284 space: ExGuid,
1285 edit: impl FnOnce(&crate::ResolvedRevision<'_>) -> Result<BTreeMap<ExGuid, PropertyObject>>,
1286) -> Result<Vec<u8>> {
1287 write_revisions(source, update(space, edit))
1288}
1289
1290/// One space's active revision edited into its update.
1291#[cfg(test)]
1292fn update(
1293 space: ExGuid,
1294 edit: impl FnOnce(&crate::ResolvedRevision<'_>) -> Result<BTreeMap<ExGuid, PropertyObject>>,
1295) -> impl FnOnce(&RevisionIndex<'_>) -> Result<BTreeMap<ExGuid, RevisionEdit>> {
1296 move |index| {
1297 let revision = index.resolve(space, index.active(space)?)?;
1298 Ok(BTreeMap::from([(
1299 space,
1300 RevisionEdit::Update(edit(&revision)?),
1301 )]))
1302 }
1303}
1304
1305/// A test fixture: the transaction appending `edit`'s revisions to `source`.
1306#[cfg(test)]
1307pub(crate) fn revisions(
1308 source: &[u8],
1309 edit: impl FnOnce(&RevisionIndex<'_>) -> Result<BTreeMap<ExGuid, RevisionEdit>>,
1310) -> Result<Option<crate::Transaction>> {
1311 let store = Store::parse(source)?;
1312 let index = RevisionIndex::parse(&store)?;
1313 index.validate_current()?;
1314 build_on(&index, &[], edit)
1315}
1316
1317/// A test fixture: `source` with `edit`'s revisions appended.
1318#[cfg(test)]
1319pub(crate) fn write_revisions(
1320 source: &[u8],
1321 edit: impl FnOnce(&RevisionIndex<'_>) -> Result<BTreeMap<ExGuid, RevisionEdit>>,
1322) -> Result<Vec<u8>> {
1323 publish(source, true, edit)
1324}
1325
1326#[cfg(test)]
1327fn publish(
1328 source: &[u8],
1329 validate: bool,
1330 edit: impl FnOnce(&RevisionIndex<'_>) -> Result<BTreeMap<ExGuid, RevisionEdit>>,
1331) -> Result<Vec<u8>> {
1332 // The parsed source is released before the result is parsed.
1333 let output = build(source, validate, edit)?;
1334 check(&output, validate)?;
1335 Ok(output)
1336}
1337
1338/// Parses a written image, and with `validate` requires its current revisions complete.
1339pub(crate) fn check(output: &[u8], validate: bool) -> Result<()> {
1340 let store = Store::parse(output)?;
1341 let index = RevisionIndex::parse(&store)?;
1342 if validate {
1343 index.validate_current()?;
1344 }
1345 Ok(())
1346}
1347
1348/// The written image, unchecked: `check` follows once the caller has released whatever
1349/// `edit` borrowed.
1350#[cfg(test)]
1351fn build(
1352 source: &[u8],
1353 validate: bool,
1354 edit: impl FnOnce(&RevisionIndex<'_>) -> Result<BTreeMap<ExGuid, RevisionEdit>>,
1355) -> Result<Vec<u8>> {
1356 let store = Store::parse(source)?;
1357 let index = RevisionIndex::parse(&store)?;
1358 if validate {
1359 index.validate_current()?;
1360 }
1361 applied(source, build_on(&index, &[], edit)?.as_ref())
1362}
1363
1364/// `write_revision` on a source the caller has parsed and validated.
1365#[cfg(test)]
1366pub(crate) fn write_revision_on(
1367 index: &RevisionIndex<'_>,
1368 space: ExGuid,
1369 edit: impl FnOnce(&crate::ResolvedRevision<'_>) -> Result<BTreeMap<ExGuid, PropertyObject>>,
1370) -> Result<Vec<u8>> {
1371 let output = applied(
1372 index.store.data,
1373 build_on(index, &[], update(space, edit))?.as_ref(),
1374 )?;
1375 check(&output, true)?;
1376 Ok(output)
1377}
1378
1379#[cfg(test)]
1380thread_local! {
1381 /// Revisions `build_on` has built on this thread.
1382 pub(crate) static BUILDS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
1383}
1384
1385/// The transaction appending `edit`'s revisions and the `payloads` to the parsed store;
1386/// none when nothing changes.
1387pub(crate) fn build_on(
1388 index: &RevisionIndex<'_>,
1389 payloads: &[([u8; 16], &[u8])],
1390 edit: impl FnOnce(&RevisionIndex<'_>) -> Result<BTreeMap<ExGuid, RevisionEdit>>,
1391) -> Result<Option<crate::Transaction>> {
1392 #[cfg(test)]
1393 BUILDS.with(|builds| builds.set(builds.get() + 1));
1394 let store = index.store;
1395 let source = store.data;
1396 let is_section = store.header.file_type == FileType::Section;
1397 if !store.checksum_mismatches.is_empty() {
1398 return Err(Error {
1399 offset: store.checksum_mismatches[0],
1400 message: "Cannot write a file with transaction checksum damage",
1401 });
1402 }
1403 let changes = edit(index)?;
1404 let mut appending = Appending::new(store.state()?);
1405 for (space, change) in changes {
1406 let current = (ExGuid::default(), 1_u32);
1407 let (rid, label, revision, replacements, new_space) = match change {
1408 RevisionEdit::Update(objects) => {
1409 let rid = index.active(space)?;
1410 (
1411 Some(rid),
1412 current,
1413 index.resolve(space, rid)?,
1414 objects,
1415 false,
1416 )
1417 }
1418 #[cfg(test)]
1419 RevisionEdit::Create { roots, objects } => {
1420 if !is_section || space.guid == [0; 16] || index.spaces.contains_key(&space) {
1421 return Err(Error {
1422 offset: 0,
1423 message: "Choose a new object-space identity in a section file",
1424 });
1425 }
1426 if roots.is_empty() || objects.is_empty() {
1427 return Err(Error {
1428 offset: 0,
1429 message: "New object space needs roots and objects",
1430 });
1431 }
1432 (
1433 None,
1434 current,
1435 crate::ResolvedRevision {
1436 roots,
1437 objects: BTreeMap::new(),
1438 },
1439 objects,
1440 true,
1441 )
1442 }
1443 };
1444 let depth = rid.map_or(0, |rid| chain_depth(index, space, rid));
1445 let mut live = LiveRevision::new(revision, depth)?;
1446 let replacements = live.prepare(replacements, is_section)?;
1447 if replacements.is_empty() {
1448 continue;
1449 }
1450 let replaced: BTreeSet<ExGuid> = replacements.keys().copied().collect();
1451 let created = replaced
1452 .iter()
1453 .filter(|id| !live.revision.objects.contains_key(id))
1454 .copied()
1455 .collect();
1456 let commit = live.commit(
1457 replacements
1458 .iter()
1459 .map(|(id, replacement)| {
1460 let global_ids = Arc::clone(&replacement.global_ids);
1461 Ok((
1462 *id,
1463 declared(replacement.jcid, &replacement.bytes, global_ids)?,
1464 ))
1465 })
1466 .collect::<Result<Vec<_>>>()?,
1467 )?;
1468 appending.revision(
1469 &Sealing {
1470 space,
1471 previous: rid,
1472 new_space,
1473 label,
1474 rid: None,
1475 live: &live,
1476 commit: &commit,
1477 replaced: &replaced,
1478 created: &created,
1479 },
1480 &[(0, source)],
1481 None,
1482 )?;
1483 }
1484 appending.payloads(payloads, None)?;
1485 Ok(appending.finish()?.map(|(transaction, _)| transaction))
1486}
1487
1488/// `source` with `transaction` applied.
1489pub(crate) fn applied(source: &[u8], transaction: Option<&crate::Transaction>) -> Result<Vec<u8>> {
1490 let mut output = source.to_vec();
1491 if let Some(transaction) = transaction {
1492 transaction.apply(&mut output)?;
1493 }
1494 Ok(output)
1495}
1496
1497/// Payload identities the file-data store of `store` declares, in order.
1498pub(crate) fn declared_payloads(store: &Store<'_>) -> Vec<[u8; 16]> {
1499 store
1500 .lists
1501 .values()
1502 .flat_map(|list| &list.nodes)
1503 .filter(|node| node.id == 0x94 && !node.freed())
1504 .filter_map(|node| node.payload.get(..16).and_then(|g| g.try_into().ok()))
1505 .collect()
1506}
1507
1508/// Writes the live objects of each `edited` revision that differ from its space's active
1509/// revision in the validated `source` as one revision per space, embedding the `payloads`
1510/// it lacks.
1511#[cfg(test)]
1512pub(crate) fn squash(
1513 source: &RevisionIndex<'_>,
1514 edited: &[(ExGuid, &crate::ResolvedRevision<'_>)],
1515 payloads: &[([u8; 16], &[u8])],
1516) -> Result<Option<crate::Transaction>> {
1517 let existing = declared_payloads(source.store);
1518 let payloads: Vec<_> = payloads
1519 .iter()
1520 .filter(|(guid, _)| !existing.contains(guid))
1521 .copied()
1522 .collect();
1523 build_on(source, &payloads, |index| {
1524 let mut changes = BTreeMap::new();
1525 for (space, after) in edited {
1526 let before = index.resolve_active(*space)?;
1527 if before.roots != after.roots {
1528 return Err(Error {
1529 offset: 0,
1530 message: "Page edits cannot change revision roots",
1531 });
1532 }
1533 // Retired styles and deleted content stay in history; only live objects are written.
1534 let live = after.reachable()?;
1535 let mut changed = BTreeMap::new();
1536 for (id, object) in &after.objects {
1537 if !live.contains(id)
1538 || before
1539 .objects
1540 .get(id)
1541 .map(|previous| unchanged(previous, object))
1542 .transpose()?
1543 == Some(true)
1544 {
1545 continue;
1546 }
1547 changed.insert(*id, replacement(*id, object)?);
1548 }
1549 changes.insert(*space, RevisionEdit::Update(changed));
1550 }
1551 Ok(changes)
1552 })
1553}
1554
1555/// A whole revision for `rewrite`, with the labels it is current under.
1556pub(crate) struct Labelled<'a> {
1557 pub labels: Vec<(ExGuid, u32)>,
1558 pub revision: crate::ResolvedRevision<'a>,
1559}
1560
1561/// `skeleton` (`create::skeleton`) with `spaces` appended, each revision a checkpoint under
1562/// its labels, and `payloads`, all stored under `key` when one is given.
1563pub(crate) fn rewrite(
1564 skeleton: Vec<u8>,
1565 spaces: &[(ExGuid, Vec<Labelled<'_>>)],
1566 payloads: &[([u8; 16], &[u8])],
1567 key: Option<&crate::protected::Key>,
1568) -> Result<Vec<u8>> {
1569 let store = Store::parse(&skeleton)?;
1570 let state = store.state()?;
1571 let declared: BTreeSet<ExGuid> = state.spaces.keys().copied().collect();
1572 let mut appending = Appending::new(state);
1573 let opened = key
1574 .map(|key| -> Result<_> {
1575 let mut container = 0xfb6ba385dad1a067_u64.to_le_bytes().to_vec();
1576 container.extend_from_slice(key.metadata());
1577 container.extend_from_slice(&0x2649294f8e198b3c_u64.to_le_bytes());
1578 Ok(crate::protected::Opened::sealing(
1579 key,
1580 appending.append(&container)?,
1581 ))
1582 })
1583 .transpose()?;
1584 let protection = opened.as_ref().map(|opened| opened as &dyn Protection);
1585 for (space, revisions) in spaces {
1586 let new = !declared.contains(space);
1587 let mut manifest = Vec::new();
1588 let mut labels = Vec::new();
1589 for (
1590 index,
1591 Labelled {
1592 labels: names,
1593 revision,
1594 },
1595 ) in revisions.iter().enumerate()
1596 {
1597 let mut live = LiveRevision::new(
1598 crate::ResolvedRevision {
1599 roots: revision.roots.clone(),
1600 objects: BTreeMap::new(),
1601 },
1602 0,
1603 )?;
1604 let commit = live.commit(
1605 revision
1606 .objects
1607 .iter()
1608 .map(|(id, object)| {
1609 let mut object = object.clone();
1610 object.reference_count = 0;
1611 (*id, object)
1612 })
1613 .collect(),
1614 )?;
1615 let all: BTreeSet<ExGuid> = live.revision.objects.keys().copied().collect();
1616 let (first, others) = names.split_first().ok_or(Error {
1617 offset: 0,
1618 message: "A rewritten revision needs a label",
1619 })?;
1620 let (rid, _) = appending.manifest(
1621 &Sealing {
1622 space: *space,
1623 previous: None,
1624 new_space: new && index == 0,
1625 label: *first,
1626 rid: None,
1627 live: &live,
1628 commit: &commit,
1629 replaced: &all,
1630 created: &all,
1631 },
1632 &[],
1633 protection,
1634 &mut manifest,
1635 )?;
1636 for (context, role) in others {
1637 let mut payload = Vec::new();
1638 rid.encode(&mut payload);
1639 payload.extend_from_slice(&role.to_le_bytes());
1640 let id = if *context == ExGuid::default() {
1641 0x5c
1642 } else {
1643 context.encode(&mut payload);
1644 0x5d
1645 };
1646 labels.push(node(id, None, &payload)?);
1647 }
1648 }
1649 manifest.extend(labels);
1650 appending.close(*space, new, &manifest)?;
1651 }
1652 appending.payloads(payloads, protection)?;
1653 let (transaction, _) = appending.finish()?.ok_or(Error {
1654 offset: 0,
1655 message: "A rewritten section holds nothing",
1656 })?;
1657 applied(&skeleton, Some(&transaction))
1658}
1659
1660/// A space's revision as `LiveRevision::commit` left it, ready to append.
1661pub(crate) struct Sealing<'r, 'a> {
1662 pub space: ExGuid,
1663 /// The revision it depends on unless it is a checkpoint; none for a new space or label.
1664 pub previous: Option<ExGuid>,
1665 pub new_space: bool,
1666 /// The context and role it is current under.
1667 pub label: (ExGuid, u32),
1668 /// Its identity, where the caller chose one; a fresh one otherwise.
1669 pub rid: Option<ExGuid>,
1670 pub live: &'r LiveRevision<'a>,
1671 pub commit: &'r Commit,
1672 /// Objects whose bytes the revision stores rather than referencing stored bytes.
1673 pub replaced: &'r BTreeSet<ExGuid>,
1674 /// Replaced objects the space did not hold before.
1675 pub created: &'r BTreeSet<ExGuid>,
1676}
1677
1678/// The chunk `bytes` occupies in a store whose bytes `segments` hold at their offsets.
1679fn located(segments: &[(u64, &[u8])], bytes: &[u8]) -> Result<Chunk> {
1680 let address = bytes.as_ptr().addr();
1681 segments
1682 .iter()
1683 .find_map(|(offset, segment)| {
1684 let start = address.checked_sub(segment.as_ptr().addr())?;
1685 (start + bytes.len() <= segment.len()).then(|| Chunk {
1686 offset: offset + start as u64,
1687 length: bytes.len() as u64,
1688 })
1689 })
1690 .ok_or(Error {
1691 offset: 0,
1692 message: "Stored object data lies outside the store",
1693 })
1694}
1695
1696/// A transaction under construction: bytes appended at the end of a store and patches
1697/// inside it, advancing `state` as its lists grow.
1698pub(crate) struct Appending {
1699 pub state: StoreState,
1700 base: crate::Stamp,
1701 append: Vec<u8>,
1702 patches: Vec<(u64, Vec<u8>)>,
1703 /// File-node counts of the lists this transaction writes, as log entries.
1704 counts: Vec<(u32, usize)>,
1705 /// Nodes the root list gains: new object spaces and the file-data store.
1706 root_nodes: Vec<Vec<u8>>,
1707}
1708
1709impl Appending {
1710 pub(crate) fn new(state: StoreState) -> Self {
1711 let base = state.stamp.clone();
1712 let mut append = Vec::with_capacity(1 << 16);
1713 // Native files reserve 1 KiB per transaction-log fragment; a fragment that ends the
1714 // file keeps that room before the first new chunk.
1715 let (tail, _) = state.log;
1716 if (tail.offset + tail.length).next_multiple_of(8) >= base.length {
1717 let reserved = tail.offset + tail.length.max(1024) + 32;
1718 append.resize(reserved.saturating_sub(base.length) as usize, 0);
1719 }
1720 Self {
1721 state,
1722 base,
1723 append,
1724 patches: Vec::new(),
1725 counts: Vec::new(),
1726 root_nodes: Vec::new(),
1727 }
1728 }
1729
1730 fn end(&self) -> u64 {
1731 self.base.length + self.append.len() as u64
1732 }
1733
1734 pub(crate) fn append(&mut self, bytes: &[u8]) -> Result<Chunk> {
1735 let length = u64::from(u32::try_from(bytes.len()).map_err(|_| Error {
1736 offset: 0,
1737 message: "Chunk exceeds the encoded length limit",
1738 })?);
1739 let padding = self.end().next_multiple_of(8) - self.end();
1740 self.append.resize(self.append.len() + padding as usize, 0);
1741 let offset = self.end();
1742 self.append.extend_from_slice(bytes);
1743 Ok(Chunk { offset, length })
1744 }
1745
1746 /// Writes `bytes` at a file offset: into the appended bytes, or as a patch.
1747 fn write(&mut self, offset: u64, bytes: &[u8]) {
1748 match offset.checked_sub(self.base.length) {
1749 Some(at) => {
1750 let at = at as usize;
1751 self.append[at..at + bytes.len()].copy_from_slice(bytes);
1752 }
1753 None => self.patches.push((offset, bytes.to_vec())),
1754 }
1755 }
1756
1757 fn allocate_list(&mut self) -> Result<u32> {
1758 self.state.max_list = self.state.max_list.checked_add(1).ok_or(Error {
1759 offset: 0,
1760 message: "File-node list identities are exhausted",
1761 })?;
1762 Ok(self.state.max_list)
1763 }
1764
1765 /// Appends `nodes` as a new list.
1766 fn list(&mut self, nodes: &[Vec<u8>]) -> Result<(Chunk, ListTail)> {
1767 let id = self.allocate_list()?;
1768 let chunk = self.append(&fragment(id, 0, nodes))?;
1769 self.counts.push((id, nodes.len()));
1770 Ok((
1771 chunk,
1772 ListTail {
1773 id,
1774 fragments: 1,
1775 last: chunk,
1776 nodes: nodes.len(),
1777 end: chunk.offset + 16 + nodes.iter().map(Vec::len).sum::<usize>() as u64,
1778 },
1779 ))
1780 }
1781
1782 /// Appends `nodes` to the list ending at `tail` as its next fragment.
1783 fn extend(&mut self, tail: &mut ListTail, nodes: &[Vec<u8>]) -> Result<()> {
1784 let chunk = self.append(&fragment(tail.id, tail.fragments, nodes))?;
1785 let next = tail.last.offset + tail.last.length - 20;
1786 if next - tail.end >= 4 {
1787 self.write(tail.end, &node(0xff, None, &[])?);
1788 }
1789 let mut reference = chunk.offset.to_le_bytes().to_vec();
1790 reference.extend_from_slice(&(chunk.length as u32).to_le_bytes());
1791 self.write(next, &reference);
1792 *tail = ListTail {
1793 id: tail.id,
1794 fragments: tail.fragments.checked_add(1).ok_or(Error {
1795 offset: 0,
1796 message: "File-node fragment sequences are exhausted",
1797 })?,
1798 last: chunk,
1799 nodes: tail.nodes + nodes.len(),
1800 end: chunk.offset + 16 + nodes.iter().map(Vec::len).sum::<usize>() as u64,
1801 };
1802 self.counts.push((tail.id, tail.nodes));
1803 Ok(())
1804 }
1805
1806 /// Appends a space's next revision: its manifest, object groups and new data. Returns the
1807 /// revision's identity and where it stored each replaced object's bytes.
1808 pub(crate) fn revision(
1809 &mut self,
1810 sealing: &Sealing<'_, '_>,
1811 segments: &[(u64, &[u8])],
1812 protection: Option<&dyn Protection>,
1813 ) -> Result<(ExGuid, Vec<(ExGuid, Chunk)>)> {
1814 let mut manifest = Vec::new();
1815 let written = self.manifest(sealing, segments, protection, &mut manifest)?;
1816 self.close(sealing.space, sealing.new_space, &manifest)?;
1817 Ok(written)
1818 }
1819
1820 /// Appends a revision's object groups and new data, and its manifest's nodes to
1821 /// `manifest` for `close`; returns its identity and where it stored each replaced
1822 /// object's bytes.
1823 pub(crate) fn manifest(
1824 &mut self,
1825 sealing: &Sealing<'_, '_>,
1826 segments: &[(u64, &[u8])],
1827 protection: Option<&dyn Protection>,
1828 manifest: &mut Vec<Vec<u8>>,
1829 ) -> Result<(ExGuid, Vec<(ExGuid, Chunk)>)> {
1830 let is_section = self.state.file_type == FileType::Section;
1831 let Sealing {
1832 space,
1833 label,
1834 live,
1835 commit,
1836 ..
1837 } = *sealing;
1838 let revision = &live.revision;
1839 let checkpoint = commit.checkpoint;
1840 let selected: Vec<(&ExGuid, &crate::Object<'_>)> = if checkpoint {
1841 revision.objects.iter().collect()
1842 } else {
1843 commit
1844 .changed
1845 .iter()
1846 .map(|id| (id, &revision.objects[id]))
1847 .collect()
1848 };
1849 // A table-of-contents manifest has one global id table (sections group objects,
1850 // each group with its own table); OneNote resolves every node against it.
1851 let toc_table = if is_section {
1852 None
1853 } else {
1854 if checkpoint
1855 && selected.iter().any(|(_, object)| {
1856 object.jcid != 0x20001 || !matches!(object.data, ObjectData::Properties(_))
1857 })
1858 {
1859 return Err(Error {
1860 offset: 0,
1861 message: "TOC checkpoint requires table-of-contents property objects",
1862 });
1863 }
1864 let guids: BTreeSet<_> = selected
1865 .iter()
1866 .flat_map(|(_, object)| object.global_ids.values().copied())
1867 .collect();
1868 if guids.len() > 0x1000000 {
1869 return Err(Error {
1870 offset: 0,
1871 message: "Global ID table exceeds CompactID capacity",
1872 });
1873 }
1874 Some(
1875 guids
1876 .into_iter()
1877 .enumerate()
1878 .map(|(i, guid)| (u32::try_from(i).unwrap(), guid))
1879 .collect::<BTreeMap<_, _>>(),
1880 )
1881 };
1882 let mut groups = BTreeMap::<_, Vec<_>>::new();
1883 for (id, object) in selected {
1884 groups
1885 .entry(toc_table.as_ref().unwrap_or(&object.global_ids))
1886 .or_default()
1887 .push((*id, object));
1888 }
1889 let new_rid = match sealing.rid {
1890 Some(rid) => rid,
1891 None => ExGuid {
1892 guid: fresh_guid()?,
1893 n: 1,
1894 },
1895 };
1896 let mut start = Vec::new();
1897 new_rid.encode(&mut start);
1898 if checkpoint {
1899 ExGuid::default()
1900 } else {
1901 sealing.previous.ok_or(Error {
1902 offset: 0,
1903 message: "A dependent revision needs the revision it follows",
1904 })?
1905 }
1906 .encode(&mut start);
1907 if !is_section {
1908 start.extend_from_slice(&0_u64.to_le_bytes());
1909 }
1910 start.extend_from_slice(&label.1.to_le_bytes());
1911 start.extend_from_slice(&(if protection.is_some() { 2_u16 } else { 0 }).to_le_bytes());
1912 let contextual = label.0 != ExGuid::default();
1913 if contextual {
1914 label.0.encode(&mut start);
1915 }
1916 if sealing.new_space {
1917 let mut payload = Vec::new();
1918 space.encode(&mut payload);
1919 payload.extend_from_slice(&0_u32.to_le_bytes());
1920 manifest.push(node(0x14, None, &payload)?);
1921 }
1922 manifest.push(node(
1923 match (is_section, contextual) {
1924 (true, true) => 0x1f,
1925 (true, false) => 0x1e,
1926 (false, _) => 0x1b,
1927 },
1928 None,
1929 &start,
1930 )?);
1931 // MS-ONESTORE 2.5.19 has every revision of a protected space name its key; OneNote
1932 // names it only in revisions without a dependency, and reads either.
1933 if let Some(protection) = protection {
1934 manifest.push(node(
1935 0x7c,
1936 Some(Reference::Data(protection.metadata())),
1937 &[],
1938 )?);
1939 }
1940 let mut stored = Vec::new();
1941 for (table, objects) in groups {
1942 let mut payload = Vec::new();
1943 let mut group = if is_section {
1944 ExGuid {
1945 guid: fresh_guid()?,
1946 n: 1,
1947 }
1948 .encode(&mut payload);
1949 vec![node(0xb4, None, &payload)?, node(0x22, None, &[])?]
1950 } else {
1951 vec![node(0x21, None, &[0])?]
1952 };
1953 // A table read from a long-lived page names every session that edited it; the
1954 // group stores the entries its objects use.
1955 let used = used_entries(table, objects.iter().copied())?;
1956 for (id, guid) in table {
1957 if is_section && used.binary_search(id).is_err() {
1958 continue;
1959 }
1960 let mut entry = id.to_le_bytes().to_vec();
1961 entry.extend_from_slice(guid);
1962 group.push(node(0x24, None, &entry)?);
1963 }
1964 group.push(node(0x28, None, &[])?);
1965 let mut override_crc = u32::MAX;
1966 for (id, object) in objects {
1967 let mut declaration = compact(id, table)?.to_vec();
1968 override_crc = crc(
1969 override_crc,
1970 &object.reference_count.to_le_bytes(),
1971 FileType::Section,
1972 );
1973 match object.data {
1974 ObjectData::File {
1975 reference,
1976 extension,
1977 } => {
1978 declaration.extend_from_slice(&object.jcid.to_le_bytes());
1979 declaration.extend_from_slice(&object.reference_count.to_le_bytes());
1980 for bytes in [reference, extension] {
1981 declaration.extend_from_slice(
1982 &u32::try_from(bytes.len() / 2).unwrap().to_le_bytes(),
1983 );
1984 declaration.extend_from_slice(bytes);
1985 }
1986 group.push(node(0x73, None, &declaration)?);
1987 }
1988 ObjectData::Properties(bytes) => {
1989 let references = object.references()?;
1990 let flags = u8::from(!references.objects.is_empty())
1991 | (u8::from(
1992 !references.object_spaces.is_empty()
1993 || !references.contexts.is_empty(),
1994 ) << 1);
1995 let data = if toc_table.is_some() {
1996 let mut mapped = bytes.to_vec();
1997 for property in PropertySets::parse(bytes)?.sets.iter().flatten() {
1998 if let Value::References { compact_ids, .. } = property.value {
1999 let offset =
2000 compact_ids.as_ptr().addr() - bytes.as_ptr().addr();
2001 for (i, value) in
2002 compact_ids.as_chunks::<4>().0.iter().enumerate()
2003 {
2004 let value = u32::from_le_bytes(*value);
2005 let target = ExGuid {
2006 guid: object.global_ids[&(value >> 8)],
2007 n: value & 255,
2008 };
2009 mapped[offset + i * 4..offset + i * 4 + 4]
2010 .copy_from_slice(&compact(target, table)?);
2011 }
2012 }
2013 }
2014 self.append(&mapped)?
2015 } else if sealing.replaced.contains(&id) {
2016 let chunk = match protection {
2017 Some(protection) => {
2018 self.append(&protection.seal_property(bytes)?)?
2019 }
2020 None => self.append(bytes)?,
2021 };
2022 stored.push((id, chunk));
2023 chunk
2024 } else {
2025 located(
2026 segments,
2027 match protection {
2028 Some(protection) => protection.stored(bytes).ok_or(Error {
2029 offset: 0,
2030 message: "Protected object has no stored form",
2031 })?,
2032 None => bytes,
2033 },
2034 )?
2035 };
2036 // A table-of-contents object is declared when the revision is a
2037 // checkpoint or the object is new, and revised otherwise.
2038 let declared = checkpoint || sealing.created.contains(&id);
2039 if is_section {
2040 declaration.extend_from_slice(&object.jcid.to_le_bytes());
2041 declaration.push(flags);
2042 } else if declared {
2043 let body = 1_u64 | (u64::from(flags & 1) << 16);
2044 declaration.extend_from_slice(&body.to_le_bytes()[..6]);
2045 } else {
2046 declaration.extend_from_slice(&u32::from(flags).to_le_bytes());
2047 }
2048 declaration.extend_from_slice(&object.reference_count.to_le_bytes());
2049 let readonly = object.jcid & 0x100000 != 0;
2050 if readonly {
2051 // A protected declaration hashes the plaintext as aligned.
2052 let mut hash = md5::Context::new();
2053 hash.consume(bytes);
2054 if protection.is_some() {
2055 hash.consume(&[0; 7][..(8 - bytes.len() % 8) % 8]);
2056 }
2057 declaration.extend_from_slice(&hash.finalize().0);
2058 }
2059 group.push(node(
2060 if is_section {
2061 if readonly { 0xc5 } else { 0xa5 }
2062 } else if declared {
2063 0x2e
2064 } else {
2065 0x42
2066 },
2067 Some(Reference::Data(data)),
2068 &declaration,
2069 )?);
2070 }
2071 ObjectData::Encrypted(_) => {
2072 return Err(Error {
2073 offset: 0,
2074 message: "Encrypted objects cannot be checkpointed",
2075 });
2076 }
2077 }
2078 }
2079 if is_section {
2080 group.push(node(0xb8, None, &[])?);
2081 let (chunk, _) = self.list(&group)?;
2082 manifest.push(node(0xb0, Some(Reference::NodeList(chunk)), &payload)?);
2083 let mut overrides = vec![0; 8];
2084 overrides.extend_from_slice(&(!override_crc).to_le_bytes());
2085 manifest.push(node(
2086 0x84,
2087 Some(Reference::Data(Chunk {
2088 offset: u64::MAX,
2089 length: 0,
2090 })),
2091 &overrides,
2092 )?);
2093 } else {
2094 manifest.extend(group);
2095 }
2096 }
2097 for (role, id) in &commit.roots {
2098 let mut payload = Vec::new();
2099 if is_section {
2100 id.encode(&mut payload);
2101 } else {
2102 payload.extend_from_slice(&compact(*id, toc_table.as_ref().unwrap())?);
2103 }
2104 payload.extend_from_slice(&role.to_le_bytes());
2105 manifest.push(node(if is_section { 0x5a } else { 0x59 }, None, &payload)?);
2106 }
2107 manifest.push(node(0x1c, None, &[])?);
2108 Ok((new_rid, stored))
2109 }
2110
2111 /// Adds a space's manifest nodes to its revision list, creating the space when `new`.
2112 pub(crate) fn close(&mut self, space: ExGuid, new: bool, manifest: &[Vec<u8>]) -> Result<()> {
2113 if new {
2114 let (chunk, tail) = self.list(manifest)?;
2115 let mut payload = Vec::new();
2116 space.encode(&mut payload);
2117 let (chunk, _) = self.list(&[
2118 node(0xc, None, &payload)?,
2119 node(0x10, Some(Reference::NodeList(chunk)), &[])?,
2120 ])?;
2121 self.root_nodes
2122 .push(node(8, Some(Reference::NodeList(chunk)), &payload)?);
2123 self.state.spaces.insert(space, tail);
2124 } else {
2125 let mut tail = *self.state.spaces.get(&space).ok_or(Error {
2126 offset: 0,
2127 message: "Object space is absent from the root list",
2128 })?;
2129 self.extend(&mut tail, manifest)?;
2130 self.state.spaces.insert(space, tail);
2131 }
2132 Ok(())
2133 }
2134
2135 /// Embeds payloads as file-data store objects referenced from the root file node list
2136 /// under their identities, as OneNote embeds pictures and attachments.
2137 pub(crate) fn payloads(
2138 &mut self,
2139 payloads: &[([u8; 16], &[u8])],
2140 protection: Option<&dyn Protection>,
2141 ) -> Result<()> {
2142 let mut data_nodes = Vec::new();
2143 for (guid, payload) in payloads {
2144 if self.state.file_type != FileType::Section {
2145 return Err(Error {
2146 offset: 0,
2147 message: "Embedded payloads require a section file",
2148 });
2149 }
2150 let mut blob = vec![
2151 0xe7, 0x16, 0xe3, 0xbd, 0x65, 0x26, 0x11, 0x45, 0xa4, 0xc4, 0x8d, 0x4d, 0x0b, 0x7a,
2152 0x9e, 0xac,
2153 ];
2154 let sealed = protection
2155 .map(|protection| protection.seal_file(payload))
2156 .transpose()?;
2157 let payload = sealed.as_deref().unwrap_or(*payload);
2158 blob.extend_from_slice(&(payload.len() as u64).to_le_bytes());
2159 blob.extend_from_slice(&[0; 12]);
2160 blob.extend_from_slice(payload);
2161 blob.resize(blob.len().next_multiple_of(8), 0);
2162 blob.extend_from_slice(&[
2163 0x22, 0xa7, 0xfb, 0x71, 0x79, 0x0f, 0x0b, 0x4a, 0xbb, 0x13, 0x89, 0x92, 0x56, 0x42,
2164 0x6b, 0x24,
2165 ]);
2166 let chunk = self.append(&blob)?;
2167 data_nodes.push(node(0x94, Some(Reference::Data(chunk)), guid)?);
2168 }
2169 if data_nodes.is_empty() {
2170 return Ok(());
2171 }
2172 // Payload declarations live in the file-data store list the root list references.
2173 match self.state.files {
2174 Some(mut tail) => {
2175 self.extend(&mut tail, &data_nodes)?;
2176 self.state.files = Some(tail);
2177 }
2178 None => {
2179 let (chunk, tail) = self.list(&data_nodes)?;
2180 self.state.files = Some(tail);
2181 self.root_nodes
2182 .push(node(0x90, Some(Reference::NodeList(chunk)), &[])?);
2183 }
2184 }
2185 Ok(())
2186 }
2187
2188 /// The transaction committing what was appended, and the state it leaves; none when
2189 /// nothing was.
2190 pub(crate) fn finish(mut self) -> Result<Option<(crate::Transaction, StoreState)>> {
2191 if !self.root_nodes.is_empty() {
2192 let nodes = std::mem::take(&mut self.root_nodes);
2193 let mut root = self.state.root;
2194 self.extend(&mut root, &nodes)?;
2195 self.state.root = root;
2196 }
2197 if self.counts.is_empty() {
2198 return Ok(None);
2199 }
2200 let file_type = self.state.file_type;
2201 let is_section = file_type == FileType::Section;
2202 let mut header = self.base.header;
2203 let count = u32::from_le_bytes(header[96..100].try_into().unwrap());
2204 let transactions = count.checked_add(1).ok_or(Error {
2205 offset: 96,
2206 message: "Transaction counter is exhausted",
2207 })?;
2208 let changed_bytes = count ^ transactions;
2209 let commit_byte = (31 - changed_bytes.leading_zeros()) / 8;
2210 let ceiling = transactions | ((1_u32 << (commit_byte * 8)) - 1);
2211 let mut entries = Vec::new();
2212 for (id, count) in std::mem::take(&mut self.counts) {
2213 entries.extend_from_slice(&id.to_le_bytes());
2214 entries.extend_from_slice(
2215 &u32::try_from(count)
2216 .map_err(|_| Error {
2217 offset: 0,
2218 message: "File-node count exceeds the format limit",
2219 })?
2220 .to_le_bytes(),
2221 );
2222 }
2223 let (mut log_chunk, mut used) = self.state.log;
2224 let mut checksum = self.state.log_crc;
2225 let mut crc_used = used;
2226 let mut crc_capacity = (log_chunk.length as usize - 12) & !7;
2227 let mut advance_crc = |checksum, entry: &[u8]| {
2228 if crc_used == crc_capacity {
2229 crc_used = 0;
2230 crc_capacity = 1008;
2231 }
2232 crc_used += 8;
2233 transaction_crc(checksum, entry, file_type, crc_used == crc_capacity)
2234 };
2235 for entry in entries.as_chunks::<8>().0 {
2236 checksum = advance_crc(checksum, entry);
2237 }
2238 // Past a counter carry, sentinels up to the ceiling follow the one that commits; the
2239 // next transaction's entries overwrite them, as a reader counting sentinels expects.
2240 let committed = entries.len() + 8;
2241 let mut committed_crc = None;
2242 for _ in transactions..=ceiling {
2243 let sentinel_crc = if is_section { !checksum } else { checksum };
2244 entries.extend_from_slice(&1_u32.to_le_bytes());
2245 entries.extend_from_slice(&sentinel_crc.to_le_bytes());
2246 checksum = advance_crc(checksum, &entries[entries.len() - 8..]);
2247 committed_crc.get_or_insert(checksum);
2248 }
2249 let mut log = self.state.log;
2250 let mut remaining = entries.as_slice();
2251 while !remaining.is_empty() {
2252 let capacity = usize::try_from(log_chunk.length)
2253 .unwrap()
2254 .checked_sub(12)
2255 .map(|size| size & !7)
2256 .filter(|capacity| *capacity >= used)
2257 .ok_or(Error {
2258 offset: usize::try_from(log_chunk.offset).unwrap(),
2259 message: "Transaction fragment has no room for continuation",
2260 })?;
2261 let size = remaining.len().min(capacity - used);
2262 self.write(log_chunk.offset + used as u64, &remaining[..size]);
2263 let written = entries.len() - remaining.len();
2264 if (written + 1..=written + size).contains(&committed) {
2265 log = (log_chunk, used + committed - written);
2266 }
2267 used += size;
2268 remaining = &remaining[size..];
2269 if !remaining.is_empty() {
2270 let mut fragment = vec![0; 1024];
2271 fragment[1008..1016].copy_from_slice(&u64::MAX.to_le_bytes());
2272 let next = self.append(&fragment)?;
2273 let mut reference = next.offset.to_le_bytes().to_vec();
2274 reference.extend_from_slice(&(next.length as u32).to_le_bytes());
2275 self.write(log_chunk.offset + capacity as u64, &reference);
2276 log_chunk = next;
2277 used = 0;
2278 }
2279 }
2280 let length = self.end();
2281 header[96..100].copy_from_slice(&transactions.to_le_bytes());
2282 header[196..204].copy_from_slice(&length.to_le_bytes());
2283 header[212..228].copy_from_slice(&fresh_guid()?);
2284 header[236..252].copy_from_slice(&fresh_guid()?);
2285 let generation = u64::from_le_bytes(header[228..236].try_into().unwrap())
2286 .checked_add(1)
2287 .ok_or(Error {
2288 offset: 228,
2289 message: "File generation counter is exhausted",
2290 })?;
2291 header[228..236].copy_from_slice(&generation.to_le_bytes());
2292 self.state.stamp = crate::Stamp { header, length };
2293 self.state.log = log;
2294 self.state.log_crc = committed_crc.unwrap();
2295 Ok(Some((
2296 crate::Transaction {
2297 base: self.base,
2298 append: self.append,
2299 patches: self.patches,
2300 header,
2301 },
2302 self.state,
2303 )))
2304 }
2305}
2306
2307/// The `required` nodes of fragment `sequence` of list `id`, which `bytes` at `at` hold and
2308/// which ends its list.
2309fn nodes(
2310 bytes: &[u8],
2311 at: Chunk,
2312 id: u32,
2313 sequence: u32,
2314 required: usize,
2315) -> Result<Vec<Node<'_>>> {
2316 let fragment = Fragment::parse(bytes, at.offset as usize)?;
2317 if fragment.id != id || fragment.sequence != sequence || !fragment.next.absent() {
2318 return Err(Error {
2319 offset: at.offset as usize,
2320 message: "An appended fragment has the wrong list, sequence or successor",
2321 });
2322 }
2323 let mut nodes = Vec::new();
2324 fragment.nodes(required, &mut nodes, |_| Ok(()))?;
2325 if nodes.len() != required {
2326 return Err(Error {
2327 offset: at.offset as usize,
2328 message: "An appended fragment lacks logged nodes",
2329 });
2330 }
2331 Ok(nodes)
2332}
2333
2334/// A revision `Appending::revision` wrote, as `check_transaction` expects to read it back.
2335pub(crate) struct Written<'r, 'a> {
2336 pub space: ExGuid,
2337 pub rid: ExGuid,
2338 pub previous: Option<ExGuid>,
2339 pub label: (ExGuid, u32),
2340 pub live: &'r LiveRevision<'a>,
2341 pub commit: &'r Commit,
2342}
2343
2344/// Reads back what `transaction` appends to the section `before` describes, whose bytes
2345/// `segments` hold, and checks it stores `revisions`, then each space's `labels` (space,
2346/// revision, context), and `payloads` as `after` expects:
2347/// fragments link and decode to the logged counts under the logged checksum; each declared
2348/// object parses, names identities its group's table holds and objects reachable after its
2349/// revision, carries its incremental reference count and, if read-only, its MD5; roots stay
2350/// unless a space is new. Validates nothing the transaction leaves as it was.
2351#[allow(clippy::too_many_arguments)]
2352pub(crate) fn check_transaction(
2353 before: &StoreState,
2354 after: &StoreState,
2355 transaction: &crate::Transaction,
2356 segments: &[(u64, &[u8])],
2357 revisions: &[Written<'_, '_>],
2358 labels: &[(ExGuid, ExGuid, ExGuid)],
2359 payloads: &[([u8; 16], &[u8])],
2360 protection: Option<&dyn Protection>,
2361) -> Result<()> {
2362 let wrong = |message| Error { offset: 0, message };
2363 let encoding: [u8; 2] = if protection.is_some() { [2, 0] } else { [0, 0] };
2364 let base = transaction.base.length;
2365 // A chunk's bytes with this transaction's writes: nothing read here is patched earlier.
2366 let read = |chunk: Chunk| -> Result<Vec<u8>> {
2367 let (start, end) = (chunk.offset, chunk.offset + chunk.length);
2368 let mut bytes = if start >= base {
2369 transaction
2370 .append
2371 .get((start - base) as usize..(end - base) as usize)
2372 .ok_or(wrong("An appended chunk lies outside the transaction"))?
2373 .to_vec()
2374 } else {
2375 let (offset, segment) = segments
2376 .iter()
2377 .rfind(|(offset, _)| *offset <= start)
2378 .ok_or(wrong("A chunk lies outside the store"))?;
2379 segment
2380 .get((start - offset) as usize..(end - offset) as usize)
2381 .ok_or(wrong("A chunk lies outside the store"))?
2382 .to_vec()
2383 };
2384 for (offset, patch) in &transaction.patches {
2385 let (from, to) = (start.max(*offset), end.min(offset + patch.len() as u64));
2386 if from < to {
2387 bytes[(from - start) as usize..(to - start) as usize]
2388 .copy_from_slice(&patch[(from - offset) as usize..(to - offset) as usize]);
2389 }
2390 }
2391 Ok(bytes)
2392 };
2393 let header = &transaction.header;
2394 let count = |header: &[u8; 1024]| u32::from_le_bytes(header[96..100].try_into().unwrap());
2395 if *header != after.stamp.header
2396 || count(header) != count(&before.stamp.header) + 1
2397 || u64::from_le_bytes(header[196..204].try_into().unwrap()) != after.stamp.length
2398 || after.stamp.length != base + transaction.append.len() as u64
2399 {
2400 return Err(wrong("The transaction header does not commit its bytes"));
2401 }
2402
2403 // The log: entries from where it ended, through the sentinel that commits them.
2404 let mut counts = BTreeMap::new();
2405 let (mut chunk, mut used) = before.log;
2406 let mut checksum = before.log_crc;
2407 loop {
2408 let capacity = (chunk.length as usize - 12) & !7;
2409 let bytes = read(chunk)?;
2410 if used == capacity {
2411 let next = u64::from_le_bytes(bytes[capacity..capacity + 8].try_into().unwrap());
2412 let length = u32::from_le_bytes(bytes[capacity + 8..capacity + 12].try_into().unwrap());
2413 (chunk, used) = (
2414 Chunk {
2415 offset: next,
2416 length: u64::from(length),
2417 },
2418 0,
2419 );
2420 continue;
2421 }
2422 let entry = &bytes[used..used + 8];
2423 used += 8;
2424 let id = u32::from_le_bytes(entry[..4].try_into().unwrap());
2425 let value = u32::from_le_bytes(entry[4..].try_into().unwrap());
2426 if id == 1 {
2427 if value != !checksum {
2428 return Err(wrong("The transaction log checksum does not match"));
2429 }
2430 checksum = transaction_crc(checksum, entry, FileType::Section, used == capacity);
2431 break;
2432 }
2433 checksum = transaction_crc(checksum, entry, FileType::Section, used == capacity);
2434 counts.insert(id, value as usize);
2435 }
2436 if (chunk, used) != after.log || checksum != after.log_crc {
2437 return Err(wrong("The transaction log ends elsewhere than its state"));
2438 }
2439
2440 // Nodes a list gained: the fragment `after` ends with, linked from where `before` ended.
2441 let gained = |before: Option<&ListTail>, after: &ListTail| -> Result<Vec<u8>> {
2442 if counts.get(&after.id) != Some(&after.nodes) {
2443 return Err(wrong("A list's nodes disagree with the transaction log"));
2444 }
2445 if let Some(before) = before {
2446 let previous = read(before.last)?;
2447 if Fragment::parse(&previous, before.last.offset as usize)?.next != after.last
2448 || after.fragments != before.fragments + 1
2449 || after.id != before.id
2450 {
2451 return Err(wrong("An appended fragment is not linked to its list"));
2452 }
2453 }
2454 read(after.last)
2455 };
2456 // Each space's list gains one fragment: its revisions in order, then its labels.
2457 let mut spaces: Vec<ExGuid> = Vec::new();
2458 for space in revisions
2459 .iter()
2460 .map(|written| written.space)
2461 .chain(labels.iter().map(|(space, ..)| *space))
2462 {
2463 if !spaces.contains(&space) {
2464 spaces.push(space);
2465 }
2466 }
2467 for space in spaces {
2468 let old = before.spaces.get(&space);
2469 let new = after
2470 .spaces
2471 .get(&space)
2472 .ok_or(wrong("A sealed space has no list"))?;
2473 let bytes = gained(old, new)?;
2474 let manifest = nodes(
2475 &bytes,
2476 new.last,
2477 new.id,
2478 old.map_or(0, |old| old.fragments),
2479 new.nodes - old.map_or(0, |old| old.nodes),
2480 )?;
2481 let mut manifest = manifest.iter();
2482 let mut next = || {
2483 manifest
2484 .next()
2485 .ok_or(wrong("A revision manifest is truncated"))
2486 };
2487 let mut fresh = old.is_none();
2488 for written in revisions.iter().filter(|written| written.space == space) {
2489 let Written {
2490 rid,
2491 live,
2492 commit,
2493 label,
2494 ..
2495 } = *written;
2496 let mut node = next()?;
2497 if fresh {
2498 let mut c = Cursor {
2499 bytes: node.payload,
2500 offset: node.offset,
2501 };
2502 if node.id != 0x14 || c.exguid()? != space {
2503 return Err(wrong("A new space's revision list names another space"));
2504 }
2505 node = next()?;
2506 fresh = false;
2507 }
2508 let mut c = Cursor {
2509 bytes: node.payload,
2510 offset: node.offset,
2511 };
2512 let dependency = if commit.checkpoint {
2513 ExGuid::default()
2514 } else {
2515 written
2516 .previous
2517 .ok_or(wrong("A dependent revision follows none"))?
2518 };
2519 let contextual = label.0 != ExGuid::default();
2520 if node.id != if contextual { 0x1f } else { 0x1e }
2521 || c.exguid()? != rid
2522 || c.exguid()? != dependency
2523 || c.read::<4>()? != label.1.to_le_bytes()
2524 || c.read::<2>()? != encoding
2525 || (contextual && c.exguid()? != label.0)
2526 {
2527 return Err(wrong(
2528 "A revision starts with the wrong identity, dependency or label",
2529 ));
2530 }
2531 if let Some(protection) = protection {
2532 let node = next()?;
2533 if node.id != 0x7c || node.reference != Some(Reference::Data(protection.metadata()))
2534 {
2535 return Err(wrong("A protected revision does not name its key"));
2536 }
2537 }
2538 let mut declared = BTreeSet::new();
2539 let mut roots = BTreeMap::new();
2540 loop {
2541 let node = next()?;
2542 match node.id {
2543 0xb0 => {
2544 let Some(Reference::NodeList(at)) = node.reference else {
2545 return Err(wrong("An object group lacks its list"));
2546 };
2547 let bytes = read(at)?;
2548 let id = Fragment::parse(&bytes, at.offset as usize)?.id;
2549 let required = *counts
2550 .get(&id)
2551 .ok_or(wrong("An object group is not logged"))?;
2552 let group = nodes(&bytes, at, id, 0, required)?;
2553 let mut table = BTreeMap::new();
2554 let mut override_crc = u32::MAX;
2555 for item in &group {
2556 let mut c = Cursor {
2557 bytes: item.payload,
2558 offset: item.offset,
2559 };
2560 match item.id {
2561 0xb4 | 0x22 | 0x28 | 0xb8 => {}
2562 0x24 => {
2563 table.insert(u32::from_le_bytes(c.read()?), c.read::<16>()?);
2564 }
2565 0xa5 | 0xc5 | 0x73 => {
2566 let id = c.compact(&table)?;
2567 let object =
2568 live.revision.objects.get(&id).ok_or(wrong(
2569 "A revision declares an unknown object",
2570 ))?;
2571 let jcid = u32::from_le_bytes(c.read()?);
2572 let flags = if item.id == 0x73 {
2573 None
2574 } else {
2575 Some(c.read::<1>()?[0])
2576 };
2577 let count = u32::from_le_bytes(c.read()?);
2578 if !declared.insert(id)
2579 || jcid != object.jcid
2580 || count != object.reference_count
2581 {
2582 return Err(wrong(
2583 "A declaration disagrees with its object or its count",
2584 ));
2585 }
2586 override_crc =
2587 crc(override_crc, &count.to_le_bytes(), FileType::Section);
2588 if item.id == 0x73 {
2589 continue;
2590 }
2591 let Some(Reference::Data(at)) = item.reference else {
2592 return Err(wrong("An object declaration lacks its data"));
2593 };
2594 let data = read(at)?;
2595 let clear = match protection {
2596 Some(protection) => protection.open_property(&data)?,
2597 None => zeroize::Zeroizing::new(data.clone()),
2598 };
2599 let stored = crate::Object {
2600 jcid,
2601 reference_count: count,
2602 data: ObjectData::Properties(&clear),
2603 global_ids: Arc::new(table.clone()),
2604 };
2605 let references = stored.references()?;
2606 let expected = u8::from(!references.objects.is_empty())
2607 | (u8::from(
2608 !references.object_spaces.is_empty()
2609 || !references.contexts.is_empty(),
2610 ) << 1);
2611 let hash = match protection {
2612 Some(_) => crate::protected::digest(&clear),
2613 None => md5::compute(&data).0,
2614 };
2615 if object.data != ObjectData::Properties(&clear)
2616 || flags != Some(expected)
2617 || (item.id == 0xc5) != (jcid & 0x100000 != 0)
2618 || (item.id == 0xc5 && c.read::<16>()? != hash)
2619 {
2620 return Err(wrong(
2621 "An object's stored bytes differ from its revision",
2622 ));
2623 }
2624 if live.is_reachable(id)
2625 && !references
2626 .objects
2627 .iter()
2628 .all(|target| live.is_reachable(*target))
2629 {
2630 return Err(wrong(
2631 "An object references one its revision cannot reach",
2632 ));
2633 }
2634 }
2635 _ => return Err(wrong("Unexpected node in an object group")),
2636 }
2637 }
2638 let node = next()?;
2639 let mut c = Cursor {
2640 bytes: node.payload,
2641 offset: node.offset,
2642 };
2643 if node.id != 0x84
2644 || c.read::<8>()? != [0; 8]
2645 || u32::from_le_bytes(c.read()?) != !override_crc
2646 {
2647 return Err(wrong("An object group's count overrides do not match"));
2648 }
2649 }
2650 0x5a => {
2651 let mut c = Cursor {
2652 bytes: node.payload,
2653 offset: node.offset,
2654 };
2655 let id = c.exguid()?;
2656 roots.insert(u32::from_le_bytes(c.read()?), id);
2657 }
2658 0x1c => break,
2659 _ => return Err(wrong("Unexpected node in a revision manifest")),
2660 }
2661 }
2662 let expected: BTreeSet<ExGuid> = if commit.checkpoint {
2663 live.revision.objects.keys().copied().collect()
2664 } else {
2665 commit.changed.clone()
2666 };
2667 // Checked against the revision itself, not the roots the manifest was written from.
2668 if declared != expected
2669 || roots
2670 .iter()
2671 .any(|(role, id)| live.revision.roots.get(role) != Some(id))
2672 || (commit.checkpoint && roots != live.revision.roots)
2673 {
2674 return Err(wrong(
2675 "A revision declares other objects or roots than it changed",
2676 ));
2677 }
2678 }
2679 for (_, rid, context) in labels.iter().filter(|(labelled, ..)| *labelled == space) {
2680 let node = next()?;
2681 if node.id != 0x5d || node.payload != label_payload(*rid, *context) {
2682 return Err(wrong("A revision label names another revision or context"));
2683 }
2684 }
2685 if next().is_ok() {
2686 return Err(wrong(
2687 "A revision list gains nodes no revision or label declares",
2688 ));
2689 }
2690 }
2691
2692 if after.root.nodes != before.root.nodes {
2693 let bytes = gained(Some(&before.root), &after.root)?;
2694 let added = nodes(
2695 &bytes,
2696 after.root.last,
2697 after.root.id,
2698 before.root.fragments,
2699 after.root.nodes - before.root.nodes,
2700 )?;
2701 let created = revisions
2702 .iter()
2703 .map(|written| written.space)
2704 .filter(|space| !before.spaces.contains_key(space))
2705 .collect::<BTreeSet<_>>()
2706 .len();
2707 let spaces = added.iter().filter(|node| node.id == 8).count();
2708 if spaces != created || added.iter().any(|node| !matches!(node.id, 8 | 0x90)) {
2709 return Err(wrong(
2710 "The root list gains other nodes than new spaces declare",
2711 ));
2712 }
2713 }
2714 if !payloads.is_empty() {
2715 let files = after
2716 .files
2717 .as_ref()
2718 .ok_or(wrong("Embedded payloads have no store"))?;
2719 let bytes = gained(before.files.as_ref(), files)?;
2720 let declared = nodes(
2721 &bytes,
2722 files.last,
2723 files.id,
2724 before.files.map_or(0, |files| files.fragments),
2725 payloads.len(),
2726 )?;
2727 for ((guid, payload), node) in payloads.iter().zip(&declared) {
2728 let Some(Reference::Data(at)) = node.reference else {
2729 return Err(wrong("A payload declaration lacks its data"));
2730 };
2731 let data = read(at)?;
2732 let blob = crate::files::payload(&data, at.offset as usize)?;
2733 let blob = match protection {
2734 Some(protection) => protection.open_file(blob)?,
2735 None => zeroize::Zeroizing::new(blob.to_vec()),
2736 };
2737 if node.id != 0x94 || node.payload != guid || blob.as_slice() != *payload {
2738 return Err(wrong("An embedded payload differs from its declaration"));
2739 }
2740 }
2741 }
2742 Ok(())
2743}