1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::err::DeserializationResult as Result;
use crate::ChunkOffset;
pub use byteorder::{LittleEndian, ReadBytesExt};
use crate::utils::read_len_prefixed_utf16_string;
use std::io::{Cursor, Seek, SeekFrom};
use quick_xml::events::{BytesEnd, BytesStart};
use serde::export::Formatter;
use std::fmt;
#[derive(Debug, PartialEq, PartialOrd, Clone, Hash)]
pub struct BinXmlName {
str: String,
}
#[derive(Debug, PartialOrd, PartialEq, Clone, Hash)]
pub struct BinXmlNameRef {
pub offset: ChunkOffset,
}
impl fmt::Display for BinXmlName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.str)
}
}
#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub(crate) struct BinXmlNameLink {
pub next_string: Option<ChunkOffset>,
pub hash: u16,
}
impl BinXmlNameLink {
pub fn from_stream(stream: &mut Cursor<&[u8]>) -> Result<Self> {
let next_string = try_read!(stream, u32)?;
let name_hash = try_read!(stream, u16, "name_hash")?;
Ok(BinXmlNameLink {
next_string: if next_string > 0 {
Some(next_string)
} else {
None
},
hash: name_hash,
})
}
pub fn data_size() -> u32 {
6
}
}
impl BinXmlNameRef {
pub fn from_stream(cursor: &mut Cursor<&[u8]>) -> Result<Self> {
let name_offset = try_read!(cursor, u32, "name_offset")?;
let position_before_string = cursor.position();
let need_to_seek = position_before_string == u64::from(name_offset);
if need_to_seek {
let _ = BinXmlNameLink::from_stream(cursor)?;
let len = cursor.read_u16::<LittleEndian>()?;
let nul_terminator_len = 4;
let data_size = BinXmlNameLink::data_size() + u32::from(len * 2) + nul_terminator_len;
try_seek!(
cursor,
position_before_string + u64::from(data_size),
"Skip string"
)?;
}
Ok(BinXmlNameRef {
offset: name_offset,
})
}
}
impl BinXmlName {
#[cfg(test)]
pub(crate) fn from_str(s: &str) -> Self {
BinXmlName { str: s.to_string() }
}
#[cfg(test)]
pub(crate) fn from_string(s: String) -> Self {
BinXmlName { str: s }
}
pub fn from_stream(cursor: &mut Cursor<&[u8]>) -> Result<Self> {
let name = try_read!(cursor, len_prefixed_utf_16_str_nul_terminated, "name")?
.unwrap_or_else(|| "".to_string());
Ok(BinXmlName { str: name })
}
pub fn as_str(&self) -> &str {
&self.str
}
}
impl<'a> Into<quick_xml::events::BytesStart<'a>> for &'a BinXmlName {
fn into(self) -> BytesStart<'a> {
BytesStart::borrowed_name(self.as_str().as_bytes())
}
}
impl<'a> Into<quick_xml::events::BytesEnd<'a>> for BinXmlName {
fn into(self) -> BytesEnd<'a> {
let inner = self.as_str().as_bytes();
BytesEnd::owned(inner.to_vec())
}
}