| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::binary_item::BinaryItem; |
| 3 |
use crate::fsshttpb::data::cell_id::CellId; |
| 4 |
use crate::fsshttpb::data::compact_u64::CompactU64; |
| 5 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 6 |
use crate::fsshttpb::data::object_types::ObjectType; |
| 7 |
use crate::fsshttpb::data::stream_object::ObjectHeader; |
| 8 |
use crate::fsshttpb::data_element::DataElement; |
| 9 |
use crate::Reader; |
| 10 |
use std::fmt; |
| 11 |
|
| 12 |
/// An object group. |
| 13 |
/// |
| 14 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12.6] |
| 15 |
/// |
| 16 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.6]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/21404be6-0334-490e-80b5-82fccb9c04af |
| 17 |
#[derive(Debug)] |
| 18 |
pub(crate) struct ObjectGroup { |
| 19 |
pub(crate) declarations: Vec<ObjectGroupDeclaration>, |
| 20 |
pub(crate) metadata: Vec<ObjectGroupMetadata>, |
| 21 |
pub(crate) objects: Vec<ObjectGroupData>, |
| 22 |
} |
| 23 |
|
| 24 |
/// An object group declaration. |
| 25 |
/// |
| 26 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12.6.1] |
| 27 |
/// |
| 28 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.6.1]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/ef660e4b-a099-4e76-81f7-ed5c04a70caa |
| 29 |
#[derive(Debug)] |
| 30 |
pub(crate) enum ObjectGroupDeclaration { |
| 31 |
Object { |
| 32 |
object_id: ExGuid, |
| 33 |
partition_id: u64, |
| 34 |
data_size: u64, |
| 35 |
object_reference_count: u64, |
| 36 |
cell_reference_count: u64, |
| 37 |
}, |
| 38 |
Blob { |
| 39 |
object_id: ExGuid, |
| 40 |
blob_id: ExGuid, |
| 41 |
partition_id: u64, |
| 42 |
object_reference_count: u64, |
| 43 |
cell_reference_count: u64, |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
impl ObjectGroupDeclaration { |
| 48 |
pub(crate) fn partition_id(&self) -> u64 { |
| 49 |
match self { |
| 50 |
ObjectGroupDeclaration::Object { partition_id, .. } => *partition_id, |
| 51 |
ObjectGroupDeclaration::Blob { partition_id, .. } => *partition_id, |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
pub(crate) fn object_id(&self) -> ExGuid { |
| 56 |
match self { |
| 57 |
ObjectGroupDeclaration::Object { object_id, .. } => *object_id, |
| 58 |
ObjectGroupDeclaration::Blob { object_id, .. } => *object_id, |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/// An object group's metadata. |
| 64 |
/// |
| 65 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12.6.3] and [\[MS-FSSHTTPB\] 2.2.1.12.6.3.1] |
| 66 |
/// |
| 67 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.6.3]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/d35a8e21-e139-455c-a20b-3f47a5d9fb89 |
| 68 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.6.3.1]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/507c6b42-2772-4319-b530-8fbbf4d34afd |
| 69 |
#[derive(Debug)] |
| 70 |
pub(crate) struct ObjectGroupMetadata { |
| 71 |
pub(crate) change_frequency: ObjectChangeFrequency, |
| 72 |
} |
| 73 |
|
| 74 |
#[derive(Debug)] |
| 75 |
pub(crate) enum ObjectChangeFrequency { |
| 76 |
Unknown = 0, |
| 77 |
Frequent = 1, |
| 78 |
Infrequent = 2, |
| 79 |
Independent = 3, |
| 80 |
Custom = 4, |
| 81 |
} |
| 82 |
|
| 83 |
impl ObjectChangeFrequency { |
| 84 |
fn parse(value: u64) -> ObjectChangeFrequency { |
| 85 |
match value { |
| 86 |
x if x == ObjectChangeFrequency::Unknown as u64 => ObjectChangeFrequency::Unknown, |
| 87 |
x if x == ObjectChangeFrequency::Frequent as u64 => ObjectChangeFrequency::Frequent, |
| 88 |
x if x == ObjectChangeFrequency::Infrequent as u64 => ObjectChangeFrequency::Infrequent, |
| 89 |
x if x == ObjectChangeFrequency::Independent as u64 => { |
| 90 |
ObjectChangeFrequency::Independent |
| 91 |
} |
| 92 |
x if x == ObjectChangeFrequency::Custom as u64 => ObjectChangeFrequency::Custom, |
| 93 |
x => panic!("unexpected change frequency: {}", x), |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/// An object group's data. |
| 99 |
pub(crate) enum ObjectGroupData { |
| 100 |
/// An object. |
| 101 |
/// |
| 102 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12.6.4] |
| 103 |
/// |
| 104 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.6.4]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/d542b89c-9e81-4af6-885a-47b2f9c1ce53 |
| 105 |
Object { |
| 106 |
group: Vec<ExGuid>, |
| 107 |
cells: Vec<CellId>, |
| 108 |
data: Vec<u8>, |
| 109 |
}, |
| 110 |
/// An excluded object. |
| 111 |
/// |
| 112 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12.6.4] |
| 113 |
/// |
| 114 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.6.4]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/d542b89c-9e81-4af6-885a-47b2f9c1ce53 |
| 115 |
ObjectExcluded { |
| 116 |
group: Vec<ExGuid>, |
| 117 |
cells: Vec<CellId>, |
| 118 |
size: u64, |
| 119 |
}, |
| 120 |
/// A blob reference. |
| 121 |
/// |
| 122 |
/// See [\[MS-FSSHTTPB\] 2.2.1.12.6.5] |
| 123 |
/// |
| 124 |
/// [\[MS-FSSHTTPB\] 2.2.1.12.6.5]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/9f73af5e-bd70-4703-8ec6-1866338f1b91 |
| 125 |
BlobReference { |
| 126 |
objects: Vec<ExGuid>, |
| 127 |
cells: Vec<CellId>, |
| 128 |
blob: ExGuid, |
| 129 |
}, |
| 130 |
} |
| 131 |
|
| 132 |
struct DebugSize(usize); |
| 133 |
|
| 134 |
impl fmt::Debug for ObjectGroupData { |
| 135 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 136 |
match self { |
| 137 |
ObjectGroupData::Object { group, cells, data } => f |
| 138 |
.debug_struct("Object") |
| 139 |
.field("group", group) |
| 140 |
.field("cells", cells) |
| 141 |
.field("data", &DebugSize(data.len())) |
| 142 |
.finish(), |
| 143 |
ObjectGroupData::ObjectExcluded { group, cells, size } => f |
| 144 |
.debug_struct("ObjectExcluded") |
| 145 |
.field("group", group) |
| 146 |
.field("cells", cells) |
| 147 |
.field("size", size) |
| 148 |
.finish(), |
| 149 |
ObjectGroupData::BlobReference { |
| 150 |
objects, |
| 151 |
cells, |
| 152 |
blob, |
| 153 |
} => f |
| 154 |
.debug_struct("ObjectExcluded") |
| 155 |
.field("objects", objects) |
| 156 |
.field("cells", cells) |
| 157 |
.field("blob", blob) |
| 158 |
.finish(), |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
impl fmt::Debug for DebugSize { |
| 164 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 165 |
write!(f, "{} bytes", self.0) |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
impl DataElement { |
| 170 |
pub(crate) fn parse_object_group(reader: Reader) -> Result<ObjectGroup> { |
| 171 |
let declarations = DataElement::parse_object_group_declarations(reader)?; |
| 172 |
|
| 173 |
let mut metadata = vec![]; |
| 174 |
|
| 175 |
let object_header = ObjectHeader::parse(reader)?; |
| 176 |
match object_header.object_type { |
| 177 |
ObjectType::ObjectGroupMetadataBlock => { |
| 178 |
metadata = DataElement::parse_object_group_metadata(reader)?; |
| 179 |
|
| 180 |
// Parse object header for the group data section |
| 181 |
let object_header = ObjectHeader::parse(reader)?; |
| 182 |
if object_header.object_type != ObjectType::ObjectGroupData { |
| 183 |
return Err(ErrorKind::MalformedFssHttpBData( |
| 184 |
format!("unexpected object type: {:x}", object_header.object_type).into(), |
| 185 |
) |
| 186 |
.into()); |
| 187 |
} |
| 188 |
} |
| 189 |
ObjectType::ObjectGroupData => {} // Skip, will be parsed below |
| 190 |
_ => { |
| 191 |
return Err(ErrorKind::MalformedFssHttpBData( |
| 192 |
format!("unexpected object type: {:x}", object_header.object_type).into(), |
| 193 |
) |
| 194 |
.into()) |
| 195 |
} |
| 196 |
} |
| 197 |
let objects = DataElement::parse_object_group_data(reader)?; |
| 198 |
|
| 199 |
ObjectHeader::try_parse_end_8(reader, ObjectType::DataElement)?; |
| 200 |
|
| 201 |
Ok(ObjectGroup { |
| 202 |
declarations, |
| 203 |
metadata, |
| 204 |
objects, |
| 205 |
}) |
| 206 |
} |
| 207 |
|
| 208 |
fn parse_object_group_declarations(reader: Reader) -> Result<Vec<ObjectGroupDeclaration>> { |
| 209 |
ObjectHeader::try_parse(reader, ObjectType::ObjectGroupDeclaration)?; |
| 210 |
|
| 211 |
let mut declarations = vec![]; |
| 212 |
|
| 213 |
loop { |
| 214 |
if ObjectHeader::has_end_8(reader, ObjectType::ObjectGroupDeclaration)? { |
| 215 |
break; |
| 216 |
} |
| 217 |
|
| 218 |
let object_header = ObjectHeader::parse(reader)?; |
| 219 |
match object_header.object_type { |
| 220 |
ObjectType::ObjectGroupObject => { |
| 221 |
let object_id = ExGuid::parse(reader)?; |
| 222 |
let partition_id = CompactU64::parse(reader)?.value(); |
| 223 |
let data_size = CompactU64::parse(reader)?.value(); |
| 224 |
let object_reference_count = CompactU64::parse(reader)?.value(); |
| 225 |
let cell_reference_count = CompactU64::parse(reader)?.value(); |
| 226 |
|
| 227 |
declarations.push(ObjectGroupDeclaration::Object { |
| 228 |
object_id, |
| 229 |
partition_id, |
| 230 |
data_size, |
| 231 |
object_reference_count, |
| 232 |
cell_reference_count, |
| 233 |
}) |
| 234 |
} |
| 235 |
ObjectType::ObjectGroupDataBlob => { |
| 236 |
let object_id = ExGuid::parse(reader)?; |
| 237 |
let blob_id = ExGuid::parse(reader)?; |
| 238 |
let partition_id = CompactU64::parse(reader)?.value(); |
| 239 |
let object_reference_count = CompactU64::parse(reader)?.value(); |
| 240 |
let cell_reference_count = CompactU64::parse(reader)?.value(); |
| 241 |
|
| 242 |
declarations.push(ObjectGroupDeclaration::Blob { |
| 243 |
object_id, |
| 244 |
blob_id, |
| 245 |
partition_id, |
| 246 |
object_reference_count, |
| 247 |
cell_reference_count, |
| 248 |
}) |
| 249 |
} |
| 250 |
_ => { |
| 251 |
return Err(ErrorKind::MalformedFssHttpBData( |
| 252 |
format!("unexpected object type: {:x}", object_header.object_type).into(), |
| 253 |
) |
| 254 |
.into()) |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
ObjectHeader::try_parse_end_8(reader, ObjectType::ObjectGroupDeclaration)?; |
| 260 |
|
| 261 |
Ok(declarations) |
| 262 |
} |
| 263 |
|
| 264 |
fn parse_object_group_metadata(reader: Reader) -> Result<Vec<ObjectGroupMetadata>> { |
| 265 |
let mut declarations = vec![]; |
| 266 |
|
| 267 |
loop { |
| 268 |
if ObjectHeader::has_end_8(reader, ObjectType::ObjectGroupMetadataBlock)? { |
| 269 |
break; |
| 270 |
} |
| 271 |
|
| 272 |
ObjectHeader::try_parse_32(reader, ObjectType::ObjectGroupMetadata)?; |
| 273 |
|
| 274 |
let frequency = CompactU64::parse(reader)?; |
| 275 |
declarations.push(ObjectGroupMetadata { |
| 276 |
change_frequency: ObjectChangeFrequency::parse(frequency.value()), |
| 277 |
}) |
| 278 |
} |
| 279 |
|
| 280 |
ObjectHeader::try_parse_end_8(reader, ObjectType::ObjectGroupMetadataBlock)?; |
| 281 |
|
| 282 |
Ok(declarations) |
| 283 |
} |
| 284 |
|
| 285 |
fn parse_object_group_data(reader: Reader) -> Result<Vec<ObjectGroupData>> { |
| 286 |
let mut objects = vec![]; |
| 287 |
|
| 288 |
loop { |
| 289 |
if ObjectHeader::has_end_8(reader, ObjectType::ObjectGroupData)? { |
| 290 |
break; |
| 291 |
} |
| 292 |
|
| 293 |
let object_header = ObjectHeader::parse(reader)?; |
| 294 |
match object_header.object_type { |
| 295 |
ObjectType::ObjectGroupDataExcluded => { |
| 296 |
let group = ExGuid::parse_array(reader)?; |
| 297 |
let cells = CellId::parse_array(reader)?; |
| 298 |
let size = CompactU64::parse(reader)?.value(); |
| 299 |
|
| 300 |
objects.push(ObjectGroupData::ObjectExcluded { group, cells, size }) |
| 301 |
} |
| 302 |
ObjectType::ObjectGroupDataObject => { |
| 303 |
let group = ExGuid::parse_array(reader)?; |
| 304 |
let cells = CellId::parse_array(reader)?; |
| 305 |
let data = BinaryItem::parse(reader)?.value(); |
| 306 |
|
| 307 |
objects.push(ObjectGroupData::Object { group, cells, data }) |
| 308 |
} |
| 309 |
ObjectType::ObjectGroupBlobReference => { |
| 310 |
let references = ExGuid::parse_array(reader)?; |
| 311 |
let cells = CellId::parse_array(reader)?; |
| 312 |
let blob = ExGuid::parse(reader)?; |
| 313 |
|
| 314 |
objects.push(ObjectGroupData::BlobReference { |
| 315 |
objects: references, |
| 316 |
cells, |
| 317 |
blob, |
| 318 |
}) |
| 319 |
} |
| 320 |
_ => { |
| 321 |
return Err(ErrorKind::MalformedFssHttpBData( |
| 322 |
format!("unexpected object type: {:x}", object_header.object_type).into(), |
| 323 |
) |
| 324 |
.into()) |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
ObjectHeader::try_parse_end_8(reader, ObjectType::ObjectGroupData)?; |
| 330 |
|
| 331 |
Ok(objects) |
| 332 |
} |
| 333 |
} |