| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::compact_u64::CompactU64; |
| 3 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 4 |
use crate::fsshttpb::data::object_types::ObjectType; |
| 5 |
use crate::fsshttpb::data::serial_number::SerialNumber; |
| 6 |
use crate::fsshttpb::data::stream_object::ObjectHeader; |
| 7 |
use crate::fsshttpb::data_element::data_element_fragment::DataElementFragment; |
| 8 |
use crate::fsshttpb::data_element::object_data_blob::ObjectDataBlob; |
| 9 |
use crate::fsshttpb::data_element::object_group::ObjectGroup; |
| 10 |
use crate::fsshttpb::data_element::revision_manifest::RevisionManifest; |
| 11 |
use crate::fsshttpb::data_element::storage_index::StorageIndex; |
| 12 |
use crate::fsshttpb::data_element::storage_manifest::StorageManifest; |
| 13 |
use crate::Reader; |
| 14 |
use std::collections::HashMap; |
| 15 |
use std::fmt::Debug; |
| 16 |
|
| 17 |
pub(crate) mod cell_manifest; |
| 18 |
pub(crate) mod data_element_fragment; |
| 19 |
pub(crate) mod object_data_blob; |
| 20 |
pub(crate) mod object_group; |
| 21 |
pub(crate) mod revision_manifest; |
| 22 |
pub(crate) mod storage_index; |
| 23 |
pub(crate) mod storage_manifest; |
| 24 |
|
| 25 |
/// A FSSHTTPB data element package. |
| 26 |
/// |
| 27 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12]. |
| 28 |
/// |
| 29 |
/// [\[MS-FSSHTTPB\] 2.2.1.12]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/99a25464-99b5-4262-a964-baabed2170eb |
| 30 |
#[derive(Debug)] |
| 31 |
pub(crate) struct DataElementPackage { |
| 32 |
pub(crate) storage_indexes: HashMap<ExGuid, StorageIndex>, |
| 33 |
pub(crate) storage_manifests: HashMap<ExGuid, StorageManifest>, |
| 34 |
pub(crate) cell_manifests: HashMap<ExGuid, ExGuid>, |
| 35 |
pub(crate) revision_manifests: HashMap<ExGuid, RevisionManifest>, |
| 36 |
pub(crate) object_groups: HashMap<ExGuid, ObjectGroup>, |
| 37 |
pub(crate) data_element_fragments: HashMap<ExGuid, DataElementFragment>, |
| 38 |
pub(crate) object_data_blobs: HashMap<ExGuid, ObjectDataBlob>, |
| 39 |
} |
| 40 |
|
| 41 |
impl DataElementPackage { |
| 42 |
pub(crate) fn parse(reader: Reader) -> Result<DataElementPackage> { |
| 43 |
ObjectHeader::try_parse_16(reader, ObjectType::DataElementPackage)?; |
| 44 |
|
| 45 |
if reader.get_u8()? != 0 { |
| 46 |
return Err(ErrorKind::MalformedFssHttpBData("invalid padding byte".into()).into()); |
| 47 |
} |
| 48 |
|
| 49 |
let mut package = DataElementPackage { |
| 50 |
storage_indexes: Default::default(), |
| 51 |
storage_manifests: Default::default(), |
| 52 |
cell_manifests: Default::default(), |
| 53 |
revision_manifests: Default::default(), |
| 54 |
object_groups: Default::default(), |
| 55 |
data_element_fragments: Default::default(), |
| 56 |
object_data_blobs: Default::default(), |
| 57 |
}; |
| 58 |
|
| 59 |
loop { |
| 60 |
if ObjectHeader::has_end_8(reader, ObjectType::DataElementPackage)? { |
| 61 |
break; |
| 62 |
} |
| 63 |
|
| 64 |
DataElement::parse(reader, &mut package)? |
| 65 |
} |
| 66 |
|
| 67 |
ObjectHeader::try_parse_end_8(reader, ObjectType::DataElementPackage)?; |
| 68 |
|
| 69 |
Ok(package) |
| 70 |
} |
| 71 |
|
| 72 |
/// Look up the object groups referenced by a cell. |
| 73 |
pub(crate) fn find_objects( |
| 74 |
&self, |
| 75 |
cell: ExGuid, |
| 76 |
storage_index: &StorageIndex, |
| 77 |
) -> Result<Vec<&ObjectGroup>> { |
| 78 |
let revision_id = self |
| 79 |
.find_cell_revision_id(cell) |
| 80 |
.ok_or_else(|| ErrorKind::MalformedFssHttpBData("cell revision id not found".into()))?; |
| 81 |
let revision_mapping_id = storage_index |
| 82 |
.find_revision_mapping_id(revision_id) |
| 83 |
.ok_or_else(|| { |
| 84 |
ErrorKind::MalformedFssHttpBData("revision mapping id not found".into()) |
| 85 |
})?; |
| 86 |
let revision_manifest = self |
| 87 |
.find_revision_manifest(revision_mapping_id) |
| 88 |
.ok_or_else(|| { |
| 89 |
ErrorKind::MalformedFssHttpBData("revision manifest not found".into()) |
| 90 |
})?; |
| 91 |
|
| 92 |
revision_manifest |
| 93 |
.group_references |
| 94 |
.iter() |
| 95 |
.map(|reference| { |
| 96 |
self.find_object_group(*reference).ok_or_else(|| { |
| 97 |
ErrorKind::MalformedFssHttpBData("object group not found".into()).into() |
| 98 |
}) |
| 99 |
}) |
| 100 |
.collect::<Result<_>>() |
| 101 |
} |
| 102 |
|
| 103 |
/// Look up a blob by its ID. |
| 104 |
pub(crate) fn find_blob(&self, id: ExGuid) -> Option<&[u8]> { |
| 105 |
self.object_data_blobs.get(&id).map(|blob| blob.value()) |
| 106 |
} |
| 107 |
|
| 108 |
/// Find the first storage index. |
| 109 |
pub(crate) fn find_storage_index(&self) -> Option<&StorageIndex> { |
| 110 |
self.storage_indexes.values().next() |
| 111 |
} |
| 112 |
|
| 113 |
/// Find the first storage manifest. |
| 114 |
pub(crate) fn find_storage_manifest(&self) -> Option<&StorageManifest> { |
| 115 |
self.storage_manifests.values().next() |
| 116 |
} |
| 117 |
|
| 118 |
/// Look up a cell revision ID by the cell's manifest ID. |
| 119 |
pub(crate) fn find_cell_revision_id(&self, id: ExGuid) -> Option<ExGuid> { |
| 120 |
self.cell_manifests.get(&id).copied() |
| 121 |
} |
| 122 |
|
| 123 |
/// Look up a revision manifest by its ID. |
| 124 |
pub(crate) fn find_revision_manifest(&self, id: ExGuid) -> Option<&RevisionManifest> { |
| 125 |
self.revision_manifests.get(&id) |
| 126 |
} |
| 127 |
|
| 128 |
/// Look up an object group by its ID. |
| 129 |
pub(crate) fn find_object_group(&self, id: ExGuid) -> Option<&ObjectGroup> { |
| 130 |
self.object_groups.get(&id) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/// A parser for a single data element. |
| 135 |
/// |
| 136 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12.1] |
| 137 |
/// |
| 138 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.1]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/f0901ac0-4f26-413f-805b-a6830781f64c |
| 139 |
#[derive(Debug)] |
| 140 |
pub(crate) struct DataElement; |
| 141 |
|
| 142 |
impl DataElement { |
| 143 |
pub(crate) fn parse(reader: Reader, package: &mut DataElementPackage) -> Result<()> { |
| 144 |
ObjectHeader::try_parse_16(reader, ObjectType::DataElement)?; |
| 145 |
|
| 146 |
let id = ExGuid::parse(reader)?; |
| 147 |
let _serial = SerialNumber::parse(reader)?; |
| 148 |
let element_type = CompactU64::parse(reader)?; |
| 149 |
|
| 150 |
match element_type.value() { |
| 151 |
0x01 => { |
| 152 |
package |
| 153 |
.storage_indexes |
| 154 |
.insert(id, Self::parse_storage_index(reader)?); |
| 155 |
} |
| 156 |
0x02 => { |
| 157 |
package |
| 158 |
.storage_manifests |
| 159 |
.insert(id, Self::parse_storage_manifest(reader)?); |
| 160 |
} |
| 161 |
0x03 => { |
| 162 |
package |
| 163 |
.cell_manifests |
| 164 |
.insert(id, Self::parse_cell_manifest(reader)?); |
| 165 |
} |
| 166 |
0x04 => { |
| 167 |
package |
| 168 |
.revision_manifests |
| 169 |
.insert(id, Self::parse_revision_manifest(reader)?); |
| 170 |
} |
| 171 |
0x05 => { |
| 172 |
package |
| 173 |
.object_groups |
| 174 |
.insert(id, Self::parse_object_group(reader)?); |
| 175 |
} |
| 176 |
0x06 => { |
| 177 |
package |
| 178 |
.data_element_fragments |
| 179 |
.insert(id, Self::parse_data_element_fragment(reader)?); |
| 180 |
} |
| 181 |
0x0A => { |
| 182 |
package |
| 183 |
.object_data_blobs |
| 184 |
.insert(id, Self::parse_object_data_blob(reader)?); |
| 185 |
} |
| 186 |
x => { |
| 187 |
return Err(ErrorKind::MalformedFssHttpBData( |
| 188 |
format!("invalid element type: 0x{:X}", x).into(), |
| 189 |
) |
| 190 |
.into()) |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
Ok(()) |
| 195 |
} |
| 196 |
} |