| 1 |
use crate::fsshttpb::data::cell_id::CellId; |
| 2 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 3 |
use crate::onestore::types::compact_id::CompactId; |
| 4 |
use std::collections::HashMap; |
| 5 |
|
| 6 |
/// The ID mapping table for an object. |
| 7 |
/// |
| 8 |
/// The specification isn't really clear on how the mapping table works. According to the spec, |
| 9 |
/// the mapping table maps from `CompactId`s to `ExGuid`s for objects and `CellId`s for object |
| 10 |
/// spaces. BUT while it specifies how to build the mapping table, it doesn't mention how it |
| 11 |
/// is used. From testing it looks like there cases where a single `CompactId` maps to *multiple* |
| 12 |
/// `ExGuid`s/`CellId`s. In this case we will use the table _index_ as a fallback. |
| 13 |
/// |
| 14 |
/// See [\[MS-ONESTORE\] 2.7.8]. |
| 15 |
/// |
| 16 |
/// [\[MS-ONESTORE\] 2.7.8]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-onestore/c2e58ac6-7a86-4009-a1e4-4a84cd21508f |
| 17 |
#[derive(Debug, Clone)] |
| 18 |
pub(crate) struct MappingTable { |
| 19 |
objects: HashMap<CompactId, Vec<(usize, ExGuid)>>, |
| 20 |
object_spaces: HashMap<CompactId, Vec<(usize, CellId)>>, |
| 21 |
} |
| 22 |
|
| 23 |
impl MappingTable { |
| 24 |
pub(crate) fn from_entries< |
| 25 |
I: Iterator<Item = (CompactId, ExGuid)>, |
| 26 |
J: Iterator<Item = (CompactId, CellId)>, |
| 27 |
>( |
| 28 |
objects: I, |
| 29 |
object_spaces: J, |
| 30 |
) -> MappingTable { |
| 31 |
let mut objects_map: HashMap<CompactId, Vec<(usize, ExGuid)>> = HashMap::new(); |
| 32 |
for (i, (cid, id)) in objects.enumerate() { |
| 33 |
objects_map.entry(cid).or_default().push((i, id)); |
| 34 |
} |
| 35 |
|
| 36 |
let mut object_spaces_map: HashMap<CompactId, Vec<(usize, CellId)>> = HashMap::new(); |
| 37 |
for (i, (cid, id)) in object_spaces.enumerate() { |
| 38 |
object_spaces_map.entry(cid).or_default().push((i, id)); |
| 39 |
} |
| 40 |
|
| 41 |
MappingTable { |
| 42 |
objects: objects_map, |
| 43 |
object_spaces: object_spaces_map, |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
pub(crate) fn get_object(&self, index: usize, cid: CompactId) -> Option<ExGuid> { |
| 48 |
self.get(index, cid, &self.objects) |
| 49 |
} |
| 50 |
|
| 51 |
pub(crate) fn get_object_space(&self, index: usize, cid: CompactId) -> Option<CellId> { |
| 52 |
self.get(index, cid, &self.object_spaces) |
| 53 |
} |
| 54 |
|
| 55 |
fn get<T: Copy>( |
| 56 |
&self, |
| 57 |
index: usize, |
| 58 |
cid: CompactId, |
| 59 |
table: &HashMap<CompactId, Vec<(usize, T)>>, |
| 60 |
) -> Option<T> { |
| 61 |
if let Some(entries) = table.get(&cid) { |
| 62 |
// Only one entry: return it! |
| 63 |
if let [(_, id)] = &**entries { |
| 64 |
return Some(*id); |
| 65 |
} |
| 66 |
|
| 67 |
// Find entry with matching table index |
| 68 |
if let Some((_, id)) = entries.iter().find(|(i, _)| *i == index) { |
| 69 |
return Some(*id); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
None |
| 74 |
} |
| 75 |
} |