| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::compact_u64::CompactU64; |
| 3 |
use crate::shared::guid::Guid; |
| 4 |
use crate::Reader; |
| 5 |
use std::fmt; |
| 6 |
|
| 7 |
/// A variable-width encoding of an extended GUID (GUID + 32 bit value) |
| 8 |
/// |
| 9 |
/// See [\[MS-FSSHTTPB\] 2.2.1.7]. |
| 10 |
/// |
| 11 |
/// [\[MS-FSSHTTPB\] 2.2.1.7]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/bff58e9f-8222-4fbb-b112-5826d5febedd |
| 12 |
#[derive(Clone, Copy, PartialEq, Hash, Eq)] |
| 13 |
pub struct ExGuid { |
| 14 |
pub guid: Guid, |
| 15 |
pub value: u32, |
| 16 |
} |
| 17 |
|
| 18 |
impl ExGuid { |
| 19 |
pub(crate) fn is_nil(&self) -> bool { |
| 20 |
self.guid.is_nil() && self.value == 0 |
| 21 |
} |
| 22 |
|
| 23 |
pub(crate) fn as_option(&self) -> Option<ExGuid> { |
| 24 |
if self.is_nil() { |
| 25 |
None |
| 26 |
} else { |
| 27 |
Some(*self) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
pub(crate) fn from_guid(guid: Guid, value: u32) -> ExGuid { |
| 32 |
ExGuid { guid, value } |
| 33 |
} |
| 34 |
|
| 35 |
pub(crate) fn parse(reader: Reader) -> Result<ExGuid> { |
| 36 |
let data = reader.get_u8()?; |
| 37 |
|
| 38 |
// A null ExGuid ([FSSHTTPB] 2.2.1.7.1) |
| 39 |
if data == 0 { |
| 40 |
return Ok(ExGuid { |
| 41 |
guid: Guid::nil(), |
| 42 |
value: 0, |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
// A ExGuid with a 5 bit value ([FSSHTTPB] 2.2.1.7.2) |
| 47 |
if data & 0b111 == 4 { |
| 48 |
return Ok(ExGuid { |
| 49 |
guid: Guid::parse(reader)?, |
| 50 |
value: (data >> 3) as u32, |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
// A ExGuid with a 10 bit value ([FSSHTTPB] 2.2.1.7.3) |
| 55 |
if data & 0b111111 == 32 { |
| 56 |
let value = (reader.get_u8()? as u16) << 2 | (data >> 6) as u16; |
| 57 |
|
| 58 |
return Ok(ExGuid { |
| 59 |
guid: Guid::parse(reader)?, |
| 60 |
value: value as u32, |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
// A ExGuid with a 17 bit value ([FSSHTTPB] 2.2.1.7.4) |
| 65 |
if data & 0b1111111 == 64 { |
| 66 |
let value = (reader.get_u16()? as u32) << 1 | (data >> 7) as u32; |
| 67 |
|
| 68 |
return Ok(ExGuid { |
| 69 |
guid: Guid::parse(reader)?, |
| 70 |
value, |
| 71 |
}); |
| 72 |
} |
| 73 |
|
| 74 |
// A ExGuid with a 32 bit value ([FSSHTTPB] 2.2.1.7.5) |
| 75 |
if data == 128 { |
| 76 |
let value = reader.get_u32()?; |
| 77 |
|
| 78 |
return Ok(ExGuid { |
| 79 |
guid: Guid::parse(reader)?, |
| 80 |
value, |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
Err( |
| 85 |
ErrorKind::MalformedData(format!("unexpected ExGuid first byte: {:b}", data).into()) |
| 86 |
.into(), |
| 87 |
) |
| 88 |
} |
| 89 |
|
| 90 |
/// Parse an array of `ExGuid` values. |
| 91 |
/// |
| 92 |
/// See [\[MS-FSSHTTPB\] 2.2.1.8] |
| 93 |
/// |
| 94 |
/// [\[MS-FSSHTTPB\] 2.2.1.8]: https://docs.microsoft.com/en-us/openspecs/sharepoint_protocols/ms-fsshttpb/10d6fb35-d630-4ae3-b530-b9e877fc27d3 |
| 95 |
pub(crate) fn parse_array(reader: Reader) -> Result<Vec<ExGuid>> { |
| 96 |
let mut values = vec![]; |
| 97 |
|
| 98 |
let count = CompactU64::parse(reader)?.value(); |
| 99 |
for _ in 0..count { |
| 100 |
values.push(ExGuid::parse(reader)?); |
| 101 |
} |
| 102 |
|
| 103 |
Ok(values) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl fmt::Debug for ExGuid { |
| 108 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 109 |
write!(f, "ExGuid {{{}, {}}}", self.guid, self.value) |
| 110 |
} |
| 111 |
} |