document-svg 2.0.2

Convert PDF, Word, Excel, PowerPoint, diagram and CAD files into one SVG per page, locally — a Rust library and the docsvg command
Documentation
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
//! Bounded BIM Collaboration Format (BCFZIP/BCF-XML) issue previews.
//!
//! BCF packages exchange coordination topics, markups and viewpoints alongside
//! IFC models. This adapter reads only small XML metadata entries; snapshots,
//! referenced models, document URLs and collaboration actions remain inert.

use std::fs::File;
use std::io::{Cursor, Read};
use std::path::Path;

use zip::ZipArchive;

use crate::convert::{ConvertOptions, PageConsumer, read_limited_file};
use crate::document::html::{HtmlBlock, render_blocks_to_pages};
use crate::error::{Error, Result};
use crate::geospatial::xml_tree::{XmlElement, XmlLimits, parse_xml_tree};
use crate::table::{TableAlign, TableData};

const MAX_BCF_BYTES: u64 = 128 * 1024 * 1024;
const MAX_BCF_EXPANDED_BYTES: u64 = 128 * 1024 * 1024;
const MAX_BCF_ENTRY_BYTES: u64 = 16 * 1024 * 1024;
const MAX_BCF_ENTRIES: usize = 100_000;
const MAX_BCF_XML_EVENTS: usize = 500_000;
const MAX_BCF_XML_NODES: usize = 300_000;
const MAX_BCF_XML_DEPTH: usize = 96;
const MAX_BCF_TEXT_BYTES: usize = 24 * 1024 * 1024;
const MAX_BCF_ROWS: usize = 100_000;
const MAX_BCF_DISPLAY_BYTES: usize = 512;

pub(crate) fn looks_like_archive(path: &Path) -> bool {
    let Ok(metadata) = std::fs::metadata(path) else {
        return false;
    };
    if metadata.len() > MAX_BCF_BYTES {
        return false;
    }
    let Ok(mut file) = File::open(path) else {
        return false;
    };
    let mut signature = [0_u8; 4];
    if file.read_exact(&mut signature).is_err() || signature != [0x50, 0x4b, 0x03, 0x04] {
        return false;
    }
    let Ok(bytes) = read_limited_file(path, MAX_BCF_BYTES, "BCFZIP sniff") else {
        return false;
    };
    let Ok(mut archive) = ZipArchive::new(Cursor::new(bytes)) else {
        return false;
    };
    if archive.len() > MAX_BCF_ENTRIES {
        return false;
    }
    let mut has_version = false;
    let mut has_markup = false;
    for index in 0..archive.len() {
        let Ok(entry) = archive.by_index(index) else {
            return false;
        };
        let name = entry.name().to_ascii_lowercase();
        has_version |= name == "bcf.version";
        has_markup |= name.ends_with("markup.bcf");
        if has_version && has_markup {
            return true;
        }
    }
    false
}

struct BcfPageSink<'a> {
    inner: &'a mut dyn PageConsumer,
    warnings: &'a [String],
}
impl PageConsumer for BcfPageSink<'_> {
    fn consume(&mut self, mut page: crate::ir::Page) -> Result<()> {
        page.source_format = "bcfzip".into();
        if page.title.is_empty() {
            page.title = "BCF issue package".into();
        }
        page.description = "BCF topics and markup metadata are rendered as a bounded inert summary; snapshots, models and external references are not opened".into();
        for warning in self.warnings {
            page.warn(warning.clone());
        }
        self.inner.consume(page)
    }
}

#[derive(Default)]
struct Summary {
    version: String,
    project: String,
    topics: usize,
    comments: usize,
    viewpoints: usize,
    document_refs: usize,
    components: usize,
    snapshots: usize,
    rows: Vec<Vec<String>>,
}

pub(crate) fn convert(
    path: &Path,
    options: &ConvertOptions,
    sink: &mut dyn PageConsumer,
) -> Result<Vec<String>> {
    let bytes = read_limited_file(
        path,
        options.max_input_bytes.min(MAX_BCF_BYTES),
        "BCFZIP input",
    )?;
    let mut archive = ZipArchive::new(Cursor::new(bytes))
        .map_err(|error| Error::InvalidInput(format!("invalid BCFZIP archive: {error}")))?;
    if archive.len() > MAX_BCF_ENTRIES {
        return Err(Error::LimitExceeded(format!(
            "BCFZIP entries exceed {MAX_BCF_ENTRIES}"
        )));
    }
    let mut summary = Summary::default();
    let mut markups = Vec::new();
    let mut has_version = false;
    let mut expanded_bytes = 0_u64;
    for index in 0..archive.len() {
        let mut entry = archive.by_index(index).map_err(|error| {
            Error::InvalidInput(format!("invalid BCFZIP entry {index}: {error}"))
        })?;
        let name = entry.name().to_owned();
        let lower = name.to_ascii_lowercase();
        if lower == "bcf.version" {
            has_version = true;
            if entry.size() > MAX_BCF_ENTRY_BYTES {
                return Err(Error::LimitExceeded(
                    "BCF.version exceeds entry limit".into(),
                ));
            }
            reserve_expanded(&mut expanded_bytes, entry.size())?;
            let mut data = Vec::new();
            entry
                .take(MAX_BCF_ENTRY_BYTES.saturating_add(1))
                .read_to_end(&mut data)?;
            if data.len() as u64 > MAX_BCF_ENTRY_BYTES {
                return Err(Error::LimitExceeded(
                    "BCF.version exceeds entry limit".into(),
                ));
            }
            summary.version = truncate(&String::from_utf8_lossy(&data));
        } else if lower.ends_with("project.bcfp") {
            reserve_expanded(&mut expanded_bytes, entry.size())?;
            let data = read_entry(&mut entry, &name)?;
            if let Ok(root) = parse_bcf_xml(&data, options) {
                summary.project = first_text(&root, "Name");
            }
        } else if lower.ends_with("markup.bcf") {
            reserve_expanded(&mut expanded_bytes, entry.size())?;
            let data = read_entry(&mut entry, &name)?;
            markups.push((name, data));
        } else if lower.ends_with("snapshot.png")
            || lower.ends_with("snapshot.jpg")
            || lower.ends_with("snapshot.jpeg")
        {
            summary.snapshots = summary.snapshots.saturating_add(1);
        }
    }
    if !has_version {
        return Err(Error::InvalidInput(
            "BCFZIP is missing the root bcf.version entry".into(),
        ));
    }
    for (name, data) in markups {
        let root = parse_bcf_xml(&data, options).map_err(|error| {
            Error::InvalidInput(format!("BCF markup {name} is invalid: {error}"))
        })?;
        if !root.name.eq_ignore_ascii_case("Markup") {
            return Err(Error::InvalidInput(format!(
                "BCF markup {name} must have a Markup root"
            )));
        }
        summary.topics = summary.topics.saturating_add(count_named(&root, "Topic"));
        summary.comments = summary
            .comments
            .saturating_add(count_named_with_attr(&root, "Comment", "Guid"));
        summary.viewpoints = summary
            .viewpoints
            .saturating_add(count_named(&root, "Viewpoint"));
        summary.document_refs = summary
            .document_refs
            .saturating_add(count_named(&root, "DocumentReference"));
        summary.components = summary
            .components
            .saturating_add(count_named(&root, "Component"));
        let topic = descendants_named(&root, "Topic").first().copied();
        let title = topic
            .map(|node| first_text(node, "Title"))
            .unwrap_or_default();
        let topic_type = topic
            .and_then(|node| attr_local(node, "TopicType"))
            .map(str::to_owned)
            .unwrap_or_default();
        let status = topic
            .and_then(|node| attr_local(node, "TopicStatus"))
            .map(str::to_owned)
            .unwrap_or_default();
        let priority = topic
            .and_then(|node| attr_local(node, "Priority"))
            .map(str::to_owned)
            .unwrap_or_default();
        push_row(
            &mut summary.rows,
            "Topic",
            &title,
            &format!(
                "type={} status={} priority={}",
                display_or_dash(&topic_type),
                display_or_dash(&status),
                display_or_dash(&priority)
            ),
        )?;
    }
    if summary.topics == 0 {
        return Err(Error::InvalidInput(
            "BCFZIP contains no markup topics".into(),
        ));
    }
    push_row(
        &mut summary.rows,
        "Package",
        "BCFZIP",
        &format!(
            "version={} project={}",
            display_or_dash(&summary.version),
            display_or_dash(&summary.project)
        ),
    )?;
    push_row(
        &mut summary.rows,
        "Topics",
        &summary.topics.to_string(),
        &format!(
            "comments={} viewpoints={}",
            summary.comments, summary.viewpoints
        ),
    )?;
    push_row(
        &mut summary.rows,
        "References",
        &summary.document_refs.to_string(),
        &format!(
            "components={} snapshots={}",
            summary.components, summary.snapshots
        ),
    )?;
    let metadata = format!(
        "BCF version: {}\nProject: {}\nTopics: {}\nComments: {}\nViewpoints: {}\nDocument references: {}\nComponents: {}\nSnapshots skipped: {}",
        display_or_dash(&summary.version),
        display_or_dash(&summary.project),
        summary.topics,
        summary.comments,
        summary.viewpoints,
        summary.document_refs,
        summary.components,
        summary.snapshots
    );
    let blocks = vec![
        HtmlBlock::Heading {
            level: 1,
            text: "BCF issue package".into(),
        },
        HtmlBlock::Paragraph { text: metadata },
        HtmlBlock::Table(TableData {
            headers: vec!["Kind".into(), "Value".into(), "Detail".into()],
            rows: summary.rows,
            alignments: vec![TableAlign::Left; 3],
            raw_source: String::new(),
        }),
    ];
    let warnings = vec![
        "BCF version/project/topic status/title/priority and comment/viewpoint/reference/component counts are shown; snapshot images, IFC/model references, document URLs, GUID payloads and markup extensions are omitted or redacted".into(),
        "BCFZIP entry and XML traversal are bounded; external models, URI dereferencing, collaboration API calls, scripts and issue mutations never run".into(),
    ];
    let mut page_sink = BcfPageSink {
        inner: sink,
        warnings: &warnings,
    };
    render_blocks_to_pages(&blocks, &mut page_sink, options)?;
    Ok(warnings)
}

fn read_entry<R: Read>(entry: &mut zip::read::ZipFile<'_, R>, name: &str) -> Result<Vec<u8>> {
    if entry.size() > MAX_BCF_ENTRY_BYTES {
        return Err(Error::LimitExceeded(format!(
            "BCF entry {name} exceeds {MAX_BCF_ENTRY_BYTES} bytes"
        )));
    }
    let mut data = Vec::new();
    entry
        .take(MAX_BCF_ENTRY_BYTES.saturating_add(1))
        .read_to_end(&mut data)?;
    if data.len() as u64 > MAX_BCF_ENTRY_BYTES {
        return Err(Error::LimitExceeded(format!(
            "BCF entry {name} exceeds {MAX_BCF_ENTRY_BYTES} bytes"
        )));
    }
    Ok(data)
}
fn reserve_expanded(total: &mut u64, entry_size: u64) -> Result<()> {
    let next = total
        .checked_add(entry_size)
        .ok_or_else(|| Error::LimitExceeded("BCFZIP expanded entry size overflow".into()))?;
    if next > MAX_BCF_EXPANDED_BYTES {
        return Err(Error::LimitExceeded(format!(
            "BCFZIP expanded entries exceed {MAX_BCF_EXPANDED_BYTES} bytes"
        )));
    }
    *total = next;
    Ok(())
}
fn parse_bcf_xml(bytes: &[u8], options: &ConvertOptions) -> Result<XmlElement> {
    parse_xml_tree(
        bytes,
        &XmlLimits {
            max_events: options.max_xml_events.min(MAX_BCF_XML_EVENTS),
            max_nodes: MAX_BCF_XML_NODES,
            max_depth: MAX_BCF_XML_DEPTH,
            max_text_bytes: MAX_BCF_TEXT_BYTES,
        },
        "BCF",
    )
}
fn attr_local<'a>(element: &'a XmlElement, name: &str) -> Option<&'a str> {
    element.attributes.iter().find_map(|(key, value)| {
        key.rsplit(':')
            .next()
            .filter(|local| local.eq_ignore_ascii_case(name))
            .map(|_| value.as_str())
    })
}
fn first_text(element: &XmlElement, name: &str) -> String {
    descendants_named(element, name)
        .first()
        .map(|node| truncate(&text_content(node)))
        .unwrap_or_default()
}
fn count_named(element: &XmlElement, name: &str) -> usize {
    element
        .children
        .iter()
        .map(|child| usize::from(child.name.eq_ignore_ascii_case(name)) + count_named(child, name))
        .sum()
}
fn count_named_with_attr(element: &XmlElement, name: &str, attr: &str) -> usize {
    element
        .children
        .iter()
        .map(|child| {
            usize::from(child.name.eq_ignore_ascii_case(name) && attr_local(child, attr).is_some())
                + count_named_with_attr(child, name, attr)
        })
        .sum()
}
fn descendants_named<'a>(element: &'a XmlElement, name: &str) -> Vec<&'a XmlElement> {
    let mut result = Vec::new();
    for child in &element.children {
        if child.name.eq_ignore_ascii_case(name) {
            result.push(child);
        }
        result.extend(descendants_named(child, name));
    }
    result
}
fn text_content(element: &XmlElement) -> String {
    let mut parts = Vec::new();
    if !element.text.trim().is_empty() {
        parts.push(element.text.trim().to_owned());
    }
    for child in &element.children {
        let value = text_content(child);
        if !value.is_empty() {
            parts.push(value);
        }
    }
    parts.join(" ")
}
fn display_or_dash(value: &str) -> String {
    if value.is_empty() {
        "-".into()
    } else {
        truncate(value)
    }
}
fn truncate(value: &str) -> String {
    if value.len() <= MAX_BCF_DISPLAY_BYTES {
        value.to_owned()
    } else {
        let mut end = MAX_BCF_DISPLAY_BYTES;
        while !value.is_char_boundary(end) {
            end -= 1;
        }
        format!("{}", &value[..end])
    }
}
fn push_row(rows: &mut Vec<Vec<String>>, kind: &str, value: &str, detail: &str) -> Result<()> {
    if rows.len() >= MAX_BCF_ROWS {
        return Err(Error::LimitExceeded(format!(
            "BCF rows exceed {MAX_BCF_ROWS}"
        )));
    }
    rows.push(vec![truncate(kind), truncate(value), truncate(detail)]);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::looks_like_archive;
    #[test]
    fn missing_archive_is_not_bcf() {
        assert!(!looks_like_archive(std::path::Path::new(
            "does-not-exist.bcfzip"
        )));
    }
}