| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 3 |
use crate::one::property::references::References; |
| 4 |
use crate::one::property::PropertyType; |
| 5 |
use crate::onestore::object::Object; |
| 6 |
use crate::onestore::types::compact_id::CompactId; |
| 7 |
use crate::onestore::types::property::PropertyValue; |
| 8 |
|
| 9 |
/// A generic object reference. |
| 10 |
/// |
| 11 |
/// This allows for all sorts of object references (e.g. pages referencing their content). |
| 12 |
/// It implements parsing these references from the OneStore mapping table. |
| 13 |
pub(crate) struct ObjectReference; |
| 14 |
|
| 15 |
impl ObjectReference { |
| 16 |
pub(crate) fn parse(prop_type: PropertyType, object: &Object) -> Result<Option<ExGuid>> { |
| 17 |
// Validate the value of the property |
| 18 |
match object.props().get(prop_type) { |
| 19 |
Some(property) => property.to_object_id().ok_or_else(|| { |
| 20 |
ErrorKind::MalformedOneNoteFileData("object reference is not a object id".into()) |
| 21 |
})?, |
| 22 |
None => return Ok(None), |
| 23 |
}; |
| 24 |
|
| 25 |
// Find the correct object reference |
| 26 |
let index = Self::get_offset(prop_type, object)?; |
| 27 |
|
| 28 |
let id = object |
| 29 |
.props() |
| 30 |
.object_ids() |
| 31 |
.iter() |
| 32 |
.nth(index) |
| 33 |
.ok_or_else(|| ErrorKind::MalformedOneNoteFileData("object id index corrupt".into()))?; |
| 34 |
|
| 35 |
Ok(Self::resolve_id(index, id, object)) |
| 36 |
} |
| 37 |
|
| 38 |
pub(crate) fn parse_vec( |
| 39 |
prop_type: PropertyType, |
| 40 |
object: &Object, |
| 41 |
) -> Result<Option<Vec<ExGuid>>> { |
| 42 |
// Determine the number of object references |
| 43 |
let count = match object.props().get(prop_type) { |
| 44 |
Some(prop) => prop.to_object_ids().ok_or_else(|| { |
| 45 |
ErrorKind::MalformedOneNoteFileData( |
| 46 |
"object reference array is not a object id array".into(), |
| 47 |
) |
| 48 |
})?, |
| 49 |
None => return Ok(None), |
| 50 |
}; |
| 51 |
|
| 52 |
// Determine offset for the property for which we want to look up the object reference |
| 53 |
let offset = Self::get_offset(prop_type, object)?; |
| 54 |
|
| 55 |
let references = object.props().object_ids(); |
| 56 |
|
| 57 |
// Look up the object references by offset/count and resolve them |
| 58 |
let object_ids = references |
| 59 |
.iter() |
| 60 |
.skip(offset) |
| 61 |
.take(count as usize) |
| 62 |
.enumerate() |
| 63 |
.flat_map(|(index, id)| Self::resolve_id(index + offset, id, object)) |
| 64 |
.collect(); |
| 65 |
|
| 66 |
Ok(Some(object_ids)) |
| 67 |
} |
| 68 |
|
| 69 |
pub(crate) fn get_offset(prop_type: PropertyType, object: &Object) -> Result<usize> { |
| 70 |
let predecessors = References::get_predecessors(prop_type, object)?; |
| 71 |
let offset = Self::count_references(predecessors); |
| 72 |
|
| 73 |
Ok(offset) |
| 74 |
} |
| 75 |
|
| 76 |
pub(crate) fn count_references<'a>(props: impl Iterator<Item = &'a PropertyValue>) -> usize { |
| 77 |
props |
| 78 |
.map(|v| match v { |
| 79 |
PropertyValue::ObjectId => 1, |
| 80 |
PropertyValue::ObjectIds(c) => *c as usize, |
| 81 |
PropertyValue::PropertyValues(_, sets) => sets |
| 82 |
.iter() |
| 83 |
.map(|set| Self::count_references(set.values())) |
| 84 |
.sum(), |
| 85 |
_ => 0, |
| 86 |
}) |
| 87 |
.sum() |
| 88 |
} |
| 89 |
|
| 90 |
fn resolve_id(index: usize, id: &CompactId, object: &Object) -> Option<ExGuid> { |
| 91 |
object.mapping().get_object(index, *id) |
| 92 |
} |
| 93 |
} |