pith-zip 0.1.0

ZIP container reading: stored and deflated entries, plus a minimal XML reader
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
//! A minimal XML tokenizer, sized for the documents the suite actually
//! meets: `word/document.xml` inside DOCX, `content.xml` inside ODF —
//! well-formed XML produced by real office software, not hostile DTDs.
//!
//! What it emits: start tags with attributes, end tags (self-closing
//! elements surface as `Start` followed by `End`), text with entities
//! decoded, and CDATA content as text. What it skips silently: the XML
//! declaration, other processing instructions, comments, and `<!DOCTYPE>`
//! declarations (internal subset included). What it refuses: unknown
//! entities, malformed names, mismatched end tags, a second root element.
//!
//! Namespaces are *not* resolved: `w:document` arrives as the literal
//! name `w:document` and `xmlns:w="…"` as an ordinary attribute, because
//! the consumers match element names verbatim and a half-resolution would
//! be worse than none.
//!
//! The reader is an [`Iterator`] of [`Result`]`<`[`XmlEvent`]`>`. Once it
//! returns `Err` or `None` it is done; a poisoned reader keeps returning
//! `None` rather than resuming a corrupt position.

use pith_digest::{Error, Result};

/// One token of the document.
#[derive(Clone, Debug, PartialEq)]
pub enum XmlEvent<'a> {
    /// `<name …>` — attributes in document order, values with entities
    /// already decoded.
    Start {
        /// The element name, verbatim (`prefix:local` included).
        name: &'a str,
        /// `(name, decoded value)` pairs, document order preserved.
        attrs: Vec<(&'a str, String)>,
    },
    /// `</name>` or the implied close of a self-closing `<name/>`.
    End {
        /// The element name being closed.
        name: &'a str,
    },
    /// Character data or CDATA content. Entities are already decoded in
    /// character data; CDATA content arrives verbatim.
    Text(String),
}

/// A pull tokenizer over an XML document.
///
/// `XmlReader::new("<w:t>hi</w:t>")` hands out `Start(w:t)`,
/// `Text("hi")`, `End(w:t)` and then `None`.
pub struct XmlReader<'a> {
    /// Unconsumed input.
    input: &'a str,
    /// Open element names, innermost last.
    stack: Vec<&'a str>,
    /// A `Start` from a self-closing tag produces its `End` on the next
    /// call; at most one can be outstanding because `End` carries no
    /// fields that would need a queue.
    deferred_end: Option<&'a str>,
    /// Whether the (single) root element has been seen already.
    root_seen: bool,
    /// Set after an error or a completed document: the stream is over.
    done: bool,
}

impl<'a> XmlReader<'a> {
    /// Starts reading a document. Nothing is consumed yet; the first
    /// event appears on [`next_event`](Self::next_event).
    pub fn new(input: &'a str) -> Self {
        XmlReader {
            input,
            stack: Vec::new(),
            deferred_end: None,
            root_seen: false,
            done: false,
        }
    }

    /// The next token, `None` at end of document, or a named [`Error`].
    /// Errors are sticky: every call after the first `Err` returns `None`.
    pub fn next_event(&mut self) -> Result<Option<XmlEvent<'a>>> {
        if self.done {
            return Ok(None);
        }
        match self.step() {
            Ok(e) => Ok(e),
            Err(e) => {
                self.done = true;
                Err(e)
            }
        }
    }

    /// The walking state machine behind [`next_event`](Self::next_event).
    fn step(&mut self) -> Result<Option<XmlEvent<'a>>> {
        if let Some(name) = self.deferred_end.take() {
            return Ok(Some(XmlEvent::End { name }));
        }
        if self.input.is_empty() {
            if self.stack.is_empty() {
                return Ok(None);
            }
            return Err(Error::truncated("xml document", 1, 0));
        }
        if let Some(rest) = self.input.strip_prefix('<') {
            self.input = rest;
            self.markup()
        } else {
            self.take_text()
        }
    }

    /// Consumes whatever follows the `<` already stripped: a tag, a
    /// comment/PI/doctype to skip and recurse over, or a CDATA section.
    fn markup(&mut self) -> Result<Option<XmlEvent<'a>>> {
        if self.input.starts_with("!--") {
            self.input = &self.input[3..];
            self.skip_past("-->", "xml comment")?;
            return self.step();
        }
        if self.input.starts_with('?') {
            self.input = &self.input[1..];
            self.skip_past("?>", "xml processing instruction")?;
            return self.step();
        }
        if self.input.starts_with("![CDATA[") {
            self.input = &self.input[8..];
            let end = self.input.find("]]>").ok_or_else(|| {
                Error::truncated("xml cdata", self.input.len() + 3, self.input.len())
            })?;
            let text = self.input[..end].to_string();
            self.input = &self.input[end + 3..];
            return Ok(Some(XmlEvent::Text(text)));
        }
        if self.input.starts_with('!') {
            // <!DOCTYPE …> and friends: skip to the `>` that closes the
            // declaration, tracking an internal subset's […] and quotes.
            self.input = &self.input[1..];
            self.skip_declaration()?;
            return self.step();
        }
        if self.input.starts_with('/') {
            self.input = &self.input[1..];
            return self.end_tag();
        }
        self.start_tag()
    }

    /// Consumes an end tag after `</` and checks it against the innermost
    /// open element.
    fn end_tag(&mut self) -> Result<Option<XmlEvent<'a>>> {
        let name = self.scan_name("xml end tag name")?;
        self.skip_ws();
        match self.take_char() {
            Some('>') => {}
            Some(_) => return Err(Error::BadValue("xml end tag")),
            None => return Err(Error::truncated("xml end tag", 1, 0)),
        }
        match self.stack.last() {
            Some(&open) if open == name => {
                self.stack.pop();
                Ok(Some(XmlEvent::End { name }))
            }
            _ => Err(Error::BadValue("xml end tag mismatch")),
        }
    }

    /// Consumes a start tag: name, attributes, the `>` or `/>`.
    fn start_tag(&mut self) -> Result<Option<XmlEvent<'a>>> {
        if self.stack.is_empty() && self.root_seen {
            return Err(Error::BadValue("xml second root element"));
        }
        let name = self.scan_name("xml tag name")?;
        let mut attrs: Vec<(&'a str, String)> = Vec::new();
        let self_closing;
        loop {
            self.skip_ws();
            match self.input.as_bytes().first() {
                Some(b'>') => {
                    self.input = &self.input[1..];
                    self_closing = false;
                    break;
                }
                Some(b'/') => {
                    self.input = &self.input[1..];
                    match self.take_char() {
                        Some('>') => {
                            self_closing = true;
                            break;
                        }
                        Some(_) => return Err(Error::BadValue("xml self-closing tag")),
                        None => return Err(Error::truncated("xml tag", 1, 0)),
                    }
                }
                Some(_) => {
                    let attr_name = self.scan_name("xml attribute name")?;
                    self.skip_ws();
                    match self.take_char() {
                        Some('=') => {}
                        Some(_) => return Err(Error::BadValue("xml attribute")),
                        None => return Err(Error::truncated("xml attribute", 1, 0)),
                    }
                    self.skip_ws();
                    let quote = match self.take_char() {
                        Some('"') => '"',
                        Some('\'') => '\'',
                        Some(_) => return Err(Error::BadValue("xml attribute quote")),
                        None => return Err(Error::truncated("xml attribute", 1, 0)),
                    };
                    let value = self.take_attr_value(quote)?;
                    if attrs.iter().any(|(n, _)| *n == attr_name) {
                        return Err(Error::BadValue("xml duplicate attribute"));
                    }
                    attrs.push((attr_name, value));
                }
                None => return Err(Error::truncated("xml tag", 1, 0)),
            }
        }
        if self_closing {
            self.deferred_end = Some(name);
        } else {
            self.stack.push(name);
        }
        if self.stack.len() == 1 && !self_closing {
            self.root_seen = true;
        }
        if self_closing && self.stack.is_empty() {
            // A self-closing document element is still the root.
            self.root_seen = true;
        }
        Ok(Some(XmlEvent::Start { name, attrs }))
    }

    /// Consumes a run of character data up to the next markup.
    fn take_text(&mut self) -> Result<Option<XmlEvent<'a>>> {
        if self.stack.is_empty() {
            // Outside the root only whitespace may appear; anything else
            // is a malformed document, not a text node.
            if self.input[..self.text_run_end()].trim().is_empty() {
                let run = self.text_run_end();
                self.input = &self.input[run..];
                return self.step();
            }
            return Err(Error::BadValue("xml text outside root element"));
        }
        let run = self.text_run_end();
        let chunk = &self.input[..run];
        let mut out = String::with_capacity(chunk.len());
        let mut rest = chunk;
        while let Some(amp) = rest.find('&') {
            out.push_str(&rest[..amp]);
            rest = &rest[amp..];
            self.decode_entity(&mut rest, &mut out)?;
        }
        out.push_str(rest);
        self.input = &self.input[run..];
        Ok(Some(XmlEvent::Text(out)))
    }

    /// How many bytes before the next markup boundary (`<`) or EOF. Text
    /// may contain `&`, so the run stops at `<` alone.
    fn text_run_end(&self) -> usize {
        self.input.find('<').unwrap_or(self.input.len())
    }

    /// Consumes an attribute value after its opening quote, decoding
    /// entities. `<` inside a value is malformed XML.
    fn take_attr_value(&mut self, quote: char) -> Result<String> {
        let mut out = String::new();
        let mut rest = self.input;
        loop {
            let next = rest.find([quote, '<', '&']);
            match next {
                None => {
                    return Err(Error::truncated(
                        "xml attribute value",
                        rest.len() + 1,
                        rest.len(),
                    ));
                }
                Some(i) => {
                    out.push_str(&rest[..i]);
                    match rest.as_bytes()[i] {
                        b'<' => return Err(Error::BadValue("xml '<' in attribute value")),
                        b'&' => {
                            rest = &rest[i..];
                            self.decode_entity(&mut rest, &mut out)?;
                        }
                        _ => {
                            self.input = &rest[i + 1..];
                            return Ok(out);
                        }
                    }
                }
            }
        }
    }

    /// Decodes `&…;` at the head of `*rest` (which starts at the `&`)
    /// into `out` and advances `*rest` past the `;`.
    fn decode_entity(&self, rest: &mut &'a str, out: &mut String) -> Result<()> {
        let semi = rest
            .find(';')
            .ok_or(Error::BadValue("xml unterminated entity"))?;
        let body = &rest[1..semi];
        if body.is_empty() || body.contains(|c: char| c.is_whitespace() || c == '<' || c == '&') {
            return Err(Error::BadValue("xml entity"));
        }
        let decoded: char = match body {
            "amp" => '&',
            "lt" => '<',
            "gt" => '>',
            "quot" => '"',
            "apos" => '\'',
            _ if body.starts_with("#x") || body.starts_with("#X") => {
                let digits = &body[2..];
                let v = u32::from_str_radix(digits, 16)
                    .map_err(|_| Error::BadValue("xml numeric character reference"))?;
                char::from_u32(v).ok_or(Error::BadValue("xml numeric character reference"))?
            }
            _ if body.starts_with('#') => {
                let digits = &body[1..];
                let v = digits
                    .parse::<u32>()
                    .map_err(|_| Error::BadValue("xml numeric character reference"))?;
                char::from_u32(v).ok_or(Error::BadValue("xml numeric character reference"))?
            }
            _ => return Err(Error::BadValue("xml unknown entity")),
        };
        out.push(decoded);
        *rest = &rest[semi + 1..];
        Ok(())
    }

    /// Scans an XML Name: a non-empty run of name characters, terminated
    /// by anything that is not one.
    fn scan_name(&mut self, what: &'static str) -> Result<&'a str> {
        let end = self
            .input
            .find(|c: char| !(c.is_alphanumeric() || c == '_' || c == '-' || c == '.' || c == ':'))
            .unwrap_or(self.input.len());
        if end == 0 {
            if self.input.is_empty() {
                return Err(Error::truncated(what, 1, 0));
            }
            return Err(Error::BadValue(what));
        }
        let (name, rest) = self.input.split_at(end);
        self.input = rest;
        Ok(name)
    }

    /// Skips whitespace in place.
    fn skip_ws(&mut self) {
        self.input = self.input.trim_start();
    }

    /// Takes the first char of the input, or `None` at EOF. Char-based,
    /// not byte-based: slicing off a fixed byte width would panic on a
    /// multi-byte character.
    fn take_char(&mut self) -> Option<char> {
        let c = self.input.chars().next()?;
        self.input = &self.input[c.len_utf8()..];
        Some(c)
    }

    /// Skips to just past the first `needle`, or [`Error::Truncated`].
    fn skip_past(&mut self, needle: &str, what: &'static str) -> Result<()> {
        match self.input.find(needle) {
            Some(i) => {
                self.input = &self.input[i + needle.len()..];
                Ok(())
            }
            None => Err(Error::truncated(
                what,
                self.input.len() + needle.len(),
                self.input.len(),
            )),
        }
    }

    /// Skips a `<!…>` declaration: a `<!DOCTYPE` internal subset can hide
    /// `>` inside `[…]` or quoted strings, so the scan tracks bracket
    /// depth and quoting rather than stopping at the first `>`.
    fn skip_declaration(&mut self) -> Result<()> {
        let mut depth = 0usize;
        let mut quote: Option<char> = None;
        for (i, c) in self.input.char_indices() {
            match quote {
                Some(q) => {
                    if c == q {
                        quote = None;
                    }
                }
                None => match c {
                    '"' | '\'' => quote = Some(c),
                    '[' => depth += 1,
                    ']' => depth = depth.saturating_sub(1),
                    '>' if depth == 0 => {
                        self.input = &self.input[i + 1..];
                        return Ok(());
                    }
                    _ => {}
                },
            }
        }
        Err(Error::truncated(
            "xml declaration",
            self.input.len() + 1,
            self.input.len(),
        ))
    }
}

impl<'a> Iterator for XmlReader<'a> {
    type Item = Result<XmlEvent<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.next_event().transpose()
    }
}