-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreader.rs
More file actions
495 lines (457 loc) · 18.6 KB
/
Copy pathreader.rs
File metadata and controls
495 lines (457 loc) · 18.6 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! High-level reader API. Opens a `.rvt` / `.rfa` / `.rte` / `.rft` file
//! and exposes its streams + parsed metadata.
use crate::{
Error, Result,
basic_file_info::BasicFileInfo,
class_index, compression,
part_atom::PartAtom,
streams::{
BASIC_FILE_INFO, CONTENTS, FORMATS_LATEST, GLOBAL_CONTENT_DOCUMENTS,
GLOBAL_DOC_INCREMENT_TABLE, GLOBAL_ELEM_TABLE, GLOBAL_HISTORY, GLOBAL_LATEST,
GLOBAL_PARTITION_TABLE, PART_ATOM, REVIT_PREVIEW_4_0, TRANSMISSION_DATA,
},
};
use cfb::CompoundFile;
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeSet,
fs::File,
io::{Cursor, Read},
path::Path,
};
/// Default maximum file size accepted by [`RevitFile::open`].
///
/// 2 GiB is above any real-world Revit project we've observed
/// (typical: few MB–a few hundred MB; worksharing extreme: ~1 GiB)
/// and well below "pathological or hostile" territory. Callers with
/// specific larger-file needs should use [`RevitFile::open_with_limits`].
pub const DEFAULT_MAX_FILE_BYTES: u64 = 2 * 1024 * 1024 * 1024;
/// Default maximum stream size accepted by [`RevitFile::read_stream`].
///
/// 256 MiB per stream is comfortably above any observed stream. The
/// largest legitimate stream we've seen in the corpus is ~40 MiB
/// (Global/Latest on a large worksharing project). Hostile input
/// with a claimed huge stream size will be rejected before a
/// multi-GB allocation.
pub const DEFAULT_MAX_STREAM_BYTES: u64 = 256 * 1024 * 1024;
/// Limits applied when opening a Revit file. Protects against
/// pathological or hostile input that would otherwise force
/// unbounded memory allocation.
///
/// See audit P0 items 4 and 5 (AUDIT-2026-04-19.md) for the
/// rationale — RVT is a file-upload target, and bounded resource
/// consumption is a DoS-safety requirement, not a nice-to-have.
#[derive(Debug, Clone, Copy)]
pub struct OpenLimits {
/// Maximum file size accepted. File bytes are read into memory
/// entirely on open (CFB requires random access), so this
/// doubles as an upper bound on the initial allocation.
pub max_file_bytes: u64,
/// Maximum per-stream size accepted by [`RevitFile::read_stream`].
/// Streams larger than this cause an error rather than a
/// multi-GB alloc.
pub max_stream_bytes: u64,
/// Inflate limits applied to every `compression::inflate_at`
/// call sourced from this `RevitFile`. Keeps bounded-decompression
/// and open-limits consistent so a file opened under restrictive
/// limits doesn't accidentally re-inflate under permissive ones.
pub inflate_limits: compression::InflateLimits,
}
impl Default for OpenLimits {
fn default() -> Self {
Self {
max_file_bytes: DEFAULT_MAX_FILE_BYTES,
max_stream_bytes: DEFAULT_MAX_STREAM_BYTES,
inflate_limits: compression::InflateLimits::default(),
}
}
}
/// Opened Revit file. Holds the CFB handle + cached stream bytes.
pub struct RevitFile {
cfb: CompoundFile<Cursor<Vec<u8>>>,
/// Limits to apply on subsequent reads. Copied from the
/// `OpenLimits` passed at construction; defaults to
/// `OpenLimits::default()` for back-compat `open`/`open_bytes`.
limits: OpenLimits,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Summary {
pub file_size: u64,
pub streams: Vec<String>,
pub version: u32,
pub build: Option<String>,
pub original_path: Option<String>,
pub guid: Option<String>,
pub locale: Option<String>,
pub partition_stream: Option<String>,
pub partatom: Option<PartAtom>,
pub class_name_count: usize,
pub class_name_sample: Vec<String>,
}
impl RevitFile {
/// Open a Revit file from disk.
///
/// Returns an error if the file doesn't exist, can't be read, or
/// doesn't start with the OLE2 / MS-CFB magic bytes
/// (`D0 CF 11 E0 A1 B1 1A E1`).
///
/// ```no_run
/// use rvt::RevitFile;
///
/// let mut rf = RevitFile::open("your-project.rfa")?;
/// let summary = rf.summarize()?;
/// println!("Revit {}", summary.version);
/// # Ok::<(), rvt::Error>(())
/// ```
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Self::open_with_limits(path, OpenLimits::default())
}
/// Open a Revit file from disk with explicit resource limits.
///
/// Stats the file before reading; refuses if file size exceeds
/// `limits.max_file_bytes` to prevent multi-GB allocations from
/// a hostile input. Back-compat [`Self::open`] calls this with
/// `OpenLimits::default()` (2 GiB file, 256 MiB per stream, 256
/// MiB per inflate).
///
/// ```no_run
/// use rvt::reader::{RevitFile, OpenLimits};
///
/// // Only accept files up to 100 MB.
/// let limits = OpenLimits {
/// max_file_bytes: 100 * 1024 * 1024,
/// ..OpenLimits::default()
/// };
/// let mut rf = RevitFile::open_with_limits("your-project.rfa", limits)?;
/// # Ok::<(), rvt::Error>(())
/// ```
pub fn open_with_limits(path: impl AsRef<Path>, limits: OpenLimits) -> Result<Self> {
let path = path.as_ref();
let metadata = std::fs::metadata(path)?;
if metadata.len() > limits.max_file_bytes {
return Err(Error::Cfb(format!(
"file size {} exceeds limit {}",
metadata.len(),
limits.max_file_bytes
)));
}
let mut f = File::open(path)?;
let mut bytes = Vec::with_capacity(metadata.len() as usize);
f.read_to_end(&mut bytes)?;
Self::open_bytes_with_limits(bytes, limits)
}
/// Open a Revit file from an in-memory byte buffer.
///
/// Useful for callers that have the file bytes already (e.g. streamed
/// over the network). Equivalent to `open` after a `read_to_end`.
///
/// ```
/// use rvt::RevitFile;
/// // Four bytes that are definitely not a valid CFB file.
/// let result = RevitFile::open_bytes(b"nope".to_vec());
/// assert!(matches!(result, Err(rvt::Error::NotACfbFile)));
/// ```
pub fn open_bytes(bytes: Vec<u8>) -> Result<Self> {
Self::open_bytes_with_limits(bytes, OpenLimits::default())
}
/// Open-bytes variant with explicit limits. The byte count check
/// has already been done by the caller if they came through
/// [`Self::open_with_limits`]; here it's repeated for in-memory
/// paths that skip disk stat.
pub fn open_bytes_with_limits(bytes: Vec<u8>, limits: OpenLimits) -> Result<Self> {
if (bytes.len() as u64) > limits.max_file_bytes {
return Err(Error::Cfb(format!(
"in-memory buffer size {} exceeds limit {}",
bytes.len(),
limits.max_file_bytes
)));
}
if bytes.len() < 8 || bytes[..8] != [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1] {
return Err(Error::NotACfbFile);
}
let cfb = CompoundFile::open(Cursor::new(bytes)).map_err(|e| Error::Cfb(e.to_string()))?;
Ok(Self { cfb, limits })
}
/// Resource limits this file was opened under. Use to match
/// the limits when calling bounded-inflate on extracted stream
/// bytes.
pub fn limits(&self) -> OpenLimits {
self.limits
}
/// List all OLE stream paths (sorted). Paths are always returned
/// with forward-slash separators regardless of host OS — on
/// Windows, `Path::display()` emits backslashes, but CFB stream
/// paths are logically `/`-separated.
pub fn stream_names(&self) -> Vec<String> {
let mut streams: Vec<_> = self
.cfb
.walk()
.filter(|e| e.is_stream())
.map(|e| {
e.path()
.display()
.to_string()
.replace('\\', "/")
.trim_start_matches('/')
.to_string()
})
.collect();
streams.sort();
streams
}
/// Read a named stream's raw bytes, capped at the file's
/// configured `max_stream_bytes`.
///
/// For streams larger than the limit, returns an error rather
/// than allocating a potentially multi-GB `Vec`. Use
/// [`Self::read_stream_with_limit`] to override per-call.
pub fn read_stream(&mut self, name: &str) -> Result<Vec<u8>> {
self.read_stream_with_limit(name, self.limits.max_stream_bytes)
}
/// Read a named stream's raw bytes, capped at an explicit
/// byte limit.
///
/// `max_bytes` is the ceiling on output size. A stream whose
/// declared size (or read position) exceeds this returns
/// `Error::Cfb("stream exceeds limit…")`.
pub fn read_stream_with_limit(&mut self, name: &str, max_bytes: u64) -> Result<Vec<u8>> {
let path = if name.starts_with('/') {
name.to_string()
} else {
format!("/{name}")
};
let mut stream = self
.cfb
.open_stream(&path)
.map_err(|_| Error::StreamNotFound(name.to_string()))?;
// Stream size is known up-front from the CFB directory entry.
// Reject before reading.
let stream_size = stream.len();
if stream_size > max_bytes {
return Err(Error::Cfb(format!(
"stream '{name}' size {stream_size} exceeds limit {max_bytes}"
)));
}
let cap = (stream_size as usize).min(max_bytes as usize);
let mut out = Vec::with_capacity(cap);
// Read in bounded chunks so we can catch the case where a
// stream's directory-entry size is a lie (malformed CFB).
let mut buf = [0u8; 8192];
loop {
let n = stream.read(&mut buf)?;
if n == 0 {
break;
}
if (out.len() as u64) + (n as u64) > max_bytes {
return Err(Error::Cfb(format!(
"stream '{name}' exceeded limit {max_bytes} mid-read"
)));
}
out.extend_from_slice(&buf[..n]);
}
Ok(out)
}
/// Parse `BasicFileInfo`.
pub fn basic_file_info(&mut self) -> Result<BasicFileInfo> {
let bytes = self.read_stream(BASIC_FILE_INFO)?;
BasicFileInfo::from_bytes(&bytes)
}
/// Parse `PartAtom` XML.
pub fn part_atom(&mut self) -> Result<PartAtom> {
let bytes = self.read_stream(PART_ATOM)?;
PartAtom::from_bytes(&bytes)
}
/// Extract the PNG thumbnail from `RevitPreview4.0`.
///
/// The raw stream has a ~300-byte Revit-specific header (magic
/// `62 19 22 05` — the same header magic seen at the start of the
/// `Contents` stream). The PNG payload begins at the first occurrence
/// of the standard PNG magic bytes.
pub fn preview_png(&mut self) -> Result<Vec<u8>> {
let bytes = self.read_stream(REVIT_PREVIEW_4_0)?;
const PNG_MAGIC: [u8; 8] = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
let pos = bytes
.windows(8)
.position(|w| w == PNG_MAGIC)
.ok_or_else(|| Error::StreamNotFound("PNG magic inside RevitPreview4.0".into()))?;
Ok(bytes[pos..].to_vec())
}
/// Raw bytes of the `RevitPreview4.0` stream including Revit's
/// custom wrapper. Use `preview_png` for just the PNG.
pub fn preview_raw(&mut self) -> Result<Vec<u8>> {
self.read_stream(REVIT_PREVIEW_4_0)
}
/// Decompress `Formats/Latest` and extract the class/schema inventory.
pub fn class_names(&mut self) -> Result<BTreeSet<String>> {
let bytes = self.read_stream(FORMATS_LATEST)?;
// Formats/Latest has GZIP magic at offset 0 (no custom header).
let decompressed = compression::inflate_at(&bytes, 0)?;
class_index::extract_class_names(&decompressed)
}
/// Decompress `Formats/Latest` and parse it into a full schema table —
/// classes + fields + C++ type signatures. This is the structured
/// version of `class_names()`.
pub fn schema(&mut self) -> Result<crate::formats::SchemaTable> {
let bytes = self.read_stream(FORMATS_LATEST)?;
let decompressed = compression::inflate_at(&bytes, 0)?;
crate::formats::parse_schema(&decompressed)
}
/// Find the version-specific `Partitions/NN` stream name.
pub fn partition_stream_name(&self) -> Option<String> {
self.stream_names()
.into_iter()
.find(|n| n.starts_with("Partitions/"))
}
/// Produce a one-shot summary of everything we can parse.
///
/// Historically this method was mixed — strict on BasicFileInfo
/// (propagated errors) but lossy on PartAtom / class-names
/// (swallowed failures via `.ok()` / `.unwrap_or_default()`).
/// Prefer [`RevitFile::summarize_strict`] or
/// [`RevitFile::summarize_lossy`] — this wrapper calls through
/// to the lossy path and discards the diagnostics for backwards
/// compatibility. It's kept on the stable surface but new
/// callers should pick the strictness level they actually want.
#[deprecated(
since = "0.1.2",
note = "Use `summarize_strict` for errors-on-any-failure or `summarize_lossy` for Decoded<Summary> with accumulated diagnostics."
)]
pub fn summarize(&mut self) -> Result<Summary> {
self.summarize_lossy().map(|d| d.value)
}
/// Strict variant of [`RevitFile::summarize`] (API-04) — returns
/// `Err` on ANY parse failure, including PartAtom absence and
/// class-name enumeration failure. Use when downstream code
/// can't tolerate a partially-populated `Summary`.
pub fn summarize_strict(&mut self) -> Result<Summary> {
let streams = self.stream_names();
let bfi = self.basic_file_info()?;
// PartAtom: absence is a soft concept — not every file has
// one. Missing PartAtom is NOT an error here; a failed
// parse of an existing PartAtom IS.
let partatom = match self.part_atom() {
Ok(p) => Some(p),
Err(Error::StreamNotFound(_)) => None,
Err(e) => return Err(e),
};
let partition_stream = self.partition_stream_name();
let class_names = self.class_names()?;
let class_name_count = class_names.len();
let class_name_sample: Vec<String> = class_names.into_iter().take(30).collect();
let file_size: u64 = streams.iter().filter_map(|n| self.stream_size(n)).sum();
Ok(Summary {
file_size,
streams,
version: bfi.version,
build: bfi.build,
original_path: bfi.original_path,
guid: bfi.guid,
locale: bfi.locale,
partition_stream,
partatom,
class_name_count,
class_name_sample,
})
}
/// Lossy variant (API-05) — returns a [`crate::parse_mode::Decoded<Summary>`]
/// that accumulates partial-parse issues into its
/// `diagnostics` field instead of aborting. Fails only when
/// BasicFileInfo itself is unreadable (without it there's
/// nothing to summarise). Every other failure becomes a
/// `Diagnostic::fail_stream` entry and the corresponding
/// `Summary` field uses its default.
///
/// Use when you're summarising a batch of files and want a
/// best-effort report even for the ones with partial parses.
pub fn summarize_lossy(&mut self) -> Result<crate::parse_mode::Decoded<Summary>> {
use crate::parse_mode::{Decoded, Diagnostics};
let streams = self.stream_names();
// BasicFileInfo is load-bearing — without it we can't
// populate any versioned identity. Propagate its error.
let bfi = self.basic_file_info()?;
let mut diagnostics = Diagnostics::default();
let partatom = match self.part_atom() {
Ok(p) => Some(p),
Err(Error::StreamNotFound(_)) => None,
Err(_) => {
diagnostics.fail_stream("PartAtom");
None
}
};
let partition_stream = self.partition_stream_name();
if partition_stream.is_none() {
diagnostics.fail_stream("Partitions/*");
}
let class_names = match self.class_names() {
Ok(n) => n,
Err(_) => {
diagnostics.fail_stream(FORMATS_LATEST);
Default::default()
}
};
let class_name_count = class_names.len();
let class_name_sample: Vec<String> = class_names.into_iter().take(30).collect();
let file_size: u64 = streams.iter().filter_map(|n| self.stream_size(n)).sum();
let summary = Summary {
file_size,
streams,
version: bfi.version,
build: bfi.build,
original_path: bfi.original_path,
guid: bfi.guid,
locale: bfi.locale,
partition_stream,
partatom,
class_name_count,
class_name_sample,
};
Ok(if diagnostics.is_empty() {
Decoded::complete(summary)
} else {
Decoded::partial(summary, diagnostics)
})
}
/// Get size of a named stream (returns `None` if missing).
pub fn stream_size(&self, name: &str) -> Option<u64> {
let path = if name.starts_with('/') {
name.to_string()
} else {
format!("/{name}")
};
self.cfb.entry(&path).ok().map(|e| e.len())
}
/// Check the common/invariant streams are all present. Useful for triage:
/// if any of these is missing, the file is either corrupt or not a Revit file
/// despite having a valid CFB container.
pub fn has_revit_signature(&self) -> bool {
self.missing_required_streams().is_empty()
}
/// Diagnostic form of `has_revit_signature` — returns the list of
/// required streams that are missing, or an empty vec if all are
/// present. Much more useful than the bool when triaging: "why does
/// this 2016 file work on Linux but not Windows?" gets a concrete
/// answer ("missing Global/DocumentIncrementTable") instead of "yes
/// or no".
pub fn missing_required_streams(&self) -> Vec<&'static str> {
let names: BTreeSet<String> = self.stream_names().into_iter().collect();
let required = [
BASIC_FILE_INFO,
CONTENTS,
FORMATS_LATEST,
GLOBAL_CONTENT_DOCUMENTS,
GLOBAL_DOC_INCREMENT_TABLE,
GLOBAL_ELEM_TABLE,
GLOBAL_HISTORY,
GLOBAL_LATEST,
GLOBAL_PARTITION_TABLE,
PART_ATOM,
REVIT_PREVIEW_4_0,
TRANSMISSION_DATA,
];
required
.iter()
.copied()
.filter(|r| !names.contains(*r))
.collect()
}
}