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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::collections::BTreeMap;
use bytes::{Buf, Bytes};
use crate::{
metadata::Manifest,
v2::{
read::{
dir_entry::DirEntryError, scanner::InvalidSize, sections::SectionConversionError,
AtomsSection, ManifestSection, Section, SectionError, VolumeSection,
},
Index, Span, Tag,
},
DetectError, Magic, Version,
};
#[derive(Debug, Clone, PartialEq)]
pub struct OwnedReader {
buffer: Bytes,
index: Index,
manifest: Manifest,
atoms: BTreeMap<String, Bytes>,
}
impl OwnedReader {
pub fn parse(webc: impl Into<Bytes>) -> Result<Self, OwnedReaderError> {
let webc: Bytes = webc.into();
let version = crate::detect(webc.clone().reader())?;
if version != Version::V2 {
return Err(OwnedReaderError::UnsupportedVersion(version));
}
let index = read_index(webc.clone())?;
let manifest =
parse_section(&webc, index.manifest.span).and_then(|section: ManifestSection| {
section.manifest().map_err(OwnedReaderError::Manifest)
})?;
let atoms_section: AtomsSection = parse_section(&webc, index.atoms.span)?;
let atoms = atoms_section
.iter()
.map(|result| result.map(|(s, b)| (s.to_string(), b)))
.collect::<Result<BTreeMap<String, Bytes>, DirEntryError>>()
.map_err(OwnedReaderError::Atoms)?;
Ok(OwnedReader {
buffer: webc,
index,
atoms,
manifest,
})
}
pub fn manifest(&self) -> &Manifest {
&self.manifest
}
pub fn index(&self) -> &Index {
&self.index
}
pub fn atom_names(&self) -> impl Iterator<Item = &str> + '_ {
self.atoms.keys().map(|s| s.as_str())
}
pub fn iter_atoms(&self) -> impl Iterator<Item = (&str, &Bytes)> + '_ {
self.atoms.iter().map(|(s, b)| (s.as_str(), b))
}
pub fn get_atom(&self, name: &str) -> Option<&Bytes> {
self.atoms.get(name)
}
pub fn volume_names(&self) -> impl Iterator<Item = &str> + '_ {
self.index.volumes.keys().map(|s| s.as_str())
}
pub fn iter_volumes(
&self,
) -> impl Iterator<Item = Result<(&str, VolumeSection), OwnedReaderError>> {
self.index.volumes.iter().map(|(name, entry)| {
let volume: VolumeSection = parse_section(&self.buffer, entry.span)?;
Ok((name.as_str(), volume))
})
}
pub fn get_volume(&self, name: &str) -> Result<VolumeSection, OwnedReaderError> {
let entry = self
.index
.volumes
.get(name)
.ok_or_else(|| OwnedReaderError::NoSuchVolume {
name: name.to_string(),
})?;
parse_section(&self.buffer, entry.span)
}
}
fn parse_section<T>(buffer: &Bytes, span: Span) -> Result<T, OwnedReaderError>
where
T: TryFrom<Section, Error = SectionConversionError>,
{
let (tag, data) = get_section(buffer, span)?;
let section = Section::parse(tag, data.clone()).map_err(|error| OwnedReaderError::Section {
error,
tag,
data,
})?;
T::try_from(section).map_err(OwnedReaderError::from)
}
fn get_section(buffer: &Bytes, span: Span) -> Result<(u8, Bytes), OwnedReaderError> {
get(buffer, span).and_then(read_raw_section)
}
fn get(buffer: &Bytes, span: Span) -> Result<Bytes, OwnedReaderError> {
if buffer.len() < span.end() {
Err(OwnedReaderError::IndexOutOfBounds {
offset: span.end(),
bytes_available: buffer.len(),
})
} else {
Ok(buffer.slice(span.start..span.end()))
}
}
fn read_raw_section(mut buffer: Bytes) -> Result<(u8, Bytes), OwnedReaderError> {
if buffer.len() < std::mem::size_of::<u8>() + std::mem::size_of::<u64>() {
todo!();
}
let tag = buffer.get_u8();
let length: usize = buffer.get_u64_le().try_into().unwrap();
if buffer.len() < length {
todo!();
}
let data = buffer.copy_to_bytes(length);
Ok((tag, data))
}
fn read_index(mut webc: Bytes) -> Result<Index, OwnedReaderError> {
let header_length = std::mem::size_of::<Magic>() + std::mem::size_of::<Version>();
webc.advance(header_length);
let (tag, data) = read_raw_section(webc)?;
match Section::parse(tag, data.clone()) {
Ok(Section::Index(index_reader)) => {
let index = index_reader.index().map_err(OwnedReaderError::Index)?;
Ok(index)
}
Ok(_) => Err(OwnedReaderError::UnexpectedSection {
expected_tag: Tag::Index,
actual_tag: tag,
offset: header_length,
}),
Err(error) => Err(OwnedReaderError::Section { error, tag, data }),
}
}
#[derive(Debug, thiserror::Error)]
pub enum OwnedReaderError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("Invalid magic bytes, {}", _0.escape_ascii())]
InvalidMagic(Magic),
#[error("The version, {_0}, isn't supported")]
UnsupportedVersion(Version),
#[error("Expected to find a {expected_tag} at offset {offset:#x}, but found a \"{}\"", Tag::display(*actual_tag))]
UnexpectedSection {
expected_tag: Tag,
actual_tag: u8,
offset: usize,
},
#[error(
"Tried to access memory at offset {offset}, but only {bytes_available} bytes are available"
)]
IndexOutOfBounds {
offset: usize,
bytes_available: usize,
},
#[error("Unable to parse the index as CBOR")]
Index(serde_cbor::Error),
#[error("Unable to parse the manifest as CBOR")]
Manifest(serde_cbor::Error),
#[error("Unable to decode a section")]
Section {
#[source]
error: SectionError,
tag: u8,
data: Bytes,
},
#[error("Found the wrong section")]
IncorrectSection(#[from] SectionConversionError),
#[error("Volume not found: \"{name}\"")]
NoSuchVolume { name: String },
#[error("Unable to determine the atoms")]
Atoms(DirEntryError),
#[error("Unable to detect the WEBC file's version number")]
Detect(#[from] DetectError),
}
impl From<InvalidSize> for OwnedReaderError {
fn from(value: InvalidSize) -> Self {
let InvalidSize { expected, actual } = value;
OwnedReaderError::IndexOutOfBounds {
offset: expected,
bytes_available: actual,
}
}
}