exml 0.7.2

Pure Rust XML library based on libxml2
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright of the original code is the following.
// --------
// Summary: the core parser module
// Description: Interfaces, constants and types related to the XML parser
//
// Copy: See Copyright for the status of this software.
//
// Author: Daniel Veillard
// --------
// parser.c : an XML 1.0 parser, namespaces and validity support are mostly
//            implemented on top of the SAX interfaces
//
// References:
//   The XML specification:
//     http://www.w3.org/TR/REC-xml
//   Original 1.0 version:
//     http://www.w3.org/TR/1998/REC-xml-19980210
//   XML second edition working draft
//     http://www.w3.org/TR/2000/WD-xml-2e-20000814
//
// Okay this is a big file, the parser core is around 7000 lines, then it
// is followed by the progressive parser top routines, then the various
// high level APIs to call the parser and a few miscellaneous functions.
// A number of helper functions and deprecated ones have been moved to
// parserInternals.c to reduce this file size.
// As much as possible the functions are associated with their relative
// production in the XML specification. A few productions defining the
// different ranges of character are actually implanted either in
// parserInternals.h or parserInternals.c
// The DOM tree build is realized from the default SAX callbacks in
// the module SAX.c.
// The routines doing the validation checks are in valid.c and called either
// from the SAX callbacks or as standalone functions using a preparsed
// document.
//
// See Copyright for the status of this software.
//
// daniel@veillard.com

use std::str::{from_utf8, from_utf8_mut};

use crate::{
    chvalid::XmlCharValid,
    encoding::{
        XmlCharEncoding, XmlCharEncodingHandler, find_encoding_handler, get_encoding_handler,
    },
    generic_error,
    globals::get_parser_debug_entities,
    io::{__xml_loader_err, XmlParserInputBuffer, xml_check_http_input, xml_parser_get_directory},
    parser::xml_err_internal,
    tree::{XmlEntityPtr, XmlEntityType},
    uri::canonic_path,
};

use super::{
    INPUT_CHUNK, LINE_LEN, XmlParserCtxt, XmlParserCtxtPtr, xml_err_memory,
    xml_load_external_entity,
};

/// The parser is now working also as a state based parser.
/// The recursive one use the state info for entities processing.
#[doc(alias = "xmlParserInputState")]
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum XmlParserInputState {
    XmlParserEOF = -1, // nothing is to be parsed
    #[default]
    XmlParserStart = 0, // nothing has been parsed
    XmlParserMisc,     // Misc* before int subset
    XmlParserPI,       // Within a processing instruction
    XmlParserDTD,      // within some DTD content
    XmlParserProlog,   // Misc* after internal subset
    XmlParserComment,  // within a comment
    XmlParserStartTag, // within a start tag
    XmlParserContent,  // within the content
    XmlParserCDATASection, // within a CDATA section
    XmlParserEndTag,   // within a closing tag
    XmlParserEntityDecl, // within an entity declaration
    XmlParserEntityValue, // within an entity value in a decl
    XmlParserAttributeValue, // within an attribute value
    XmlParserSystemLiteral, // within a SYSTEM value
    XmlParserEpilog,   // the Misc* after the last end tag
    XmlParserIgnore,   // within an IGNORED section
    XmlParserPublicLiteral, // within a PUBLIC value
}

/// An xmlParserInput is an input flow for the XML processor.
/// Each entity parsed is associated an xmlParserInput (except the
/// few predefined ones). This is the case both for internal entities
/// - in which case the flow is already completely in memory  
/// - or external entities  
/// - in which case we use the buf structure for
///   progressive reading and I18N conversions to the internal UTF-8 format.
#[doc(alias = "xmlParserInput")]
#[repr(C)]
#[derive(Default)]
pub struct XmlParserInput<'a> {
    // UTF-8 encoded buffer
    pub buf: Option<XmlParserInputBuffer<'a>>,
    // The file analyzed, if any
    pub filename: Option<String>,
    // the directory/base of the file
    pub(crate) directory: Option<String>,
    // Base of the array to parse
    pub base: usize,
    // Current char being parsed
    pub cur: usize,
    // length if known
    pub(crate) length: i32,
    // Current line
    pub line: i32,
    // Current column
    pub(crate) col: i32,
    // How many xmlChars already consumed
    pub consumed: u64,
    // the encoding string for entity
    pub(crate) encoding: Option<String>,
    // the version string for entity
    pub(crate) version: Option<String>,
    // Was that entity marked standalone
    pub(crate) standalone: i32,
    // an unique identifier for the entity
    pub(crate) id: i32,
    // consumed bytes from parents
    pub(crate) parent_consumed: u64,
    // entity, if any
    pub(crate) entity: Option<XmlEntityPtr>,
    // Indicates how far `buf.buffer` is valid as UTF-8.
    // If `buf.encoder` is `Some`, `buf.buffer` is always valid, so this is always 0.
    pub(crate) valid_up_to: usize,
}

impl<'a> XmlParserInput<'a> {
    /// Create a new input stream structure.
    ///
    /// Returns the new input stream or NULL
    #[doc(alias = "xmlNewInputStream")]
    pub fn new(ctxt: Option<&mut XmlParserCtxt>) -> Option<Self> {
        let mut input = XmlParserInput {
            line: 1,
            col: 1,
            standalone: -1,
            ..Default::default()
        };

        // If the context is NULL the id cannot be initialized, but that
        // should not happen while parsing which is the situation where
        // the id is actually needed.
        if let Some(ctxt) = ctxt {
            if input.id == i32::MAX {
                xml_err_memory(Some(ctxt), Some("Input ID overflow\n"));
                return None;
            }
            input.id = ctxt.input_id;
            ctxt.input_id += 1;
        }

        Some(input)
    }

    /// Create a new input stream based on a memory buffer.
    ///
    /// Returns the new input stream
    #[doc(alias = "xmlNewStringInputStream")]
    pub fn from_str(ctxt: Option<&mut XmlParserCtxt>, buffer: &'a str) -> Option<Self> {
        if get_parser_debug_entities() != 0 {
            generic_error!("new fixed input: {buffer}\n");
        }
        let Some(buf) = XmlParserInputBuffer::from_memory(buffer.as_bytes(), XmlCharEncoding::None)
        else {
            xml_err_memory(ctxt, None);
            return None;
        };
        let Some(mut input) = XmlParserInput::new(ctxt) else {
            xml_err_memory(None, Some("couldn't allocate a new input stream\n"));
            return None;
        };
        input.buf = Some(buf);
        input.reset_base();
        Some(input)
    }

    /// Create a new input stream structure encapsulating the @input into
    /// a stream suitable for the parser.
    ///
    /// Returns the new input stream or NULL
    #[doc(alias = "xmlNewIOInputStream")]
    pub fn from_io(
        ctxt: &mut XmlParserCtxt,
        input: XmlParserInputBuffer<'a>,
        enc: XmlCharEncoding,
    ) -> Option<XmlParserInput<'a>> {
        if get_parser_debug_entities() != 0 {
            generic_error!("new input from I/O\n");
        }
        let mut input_stream = XmlParserInput::new(Some(ctxt))?;
        input_stream.filename = None;
        input_stream.buf = Some(input);
        input_stream.reset_base();

        if !matches!(enc, XmlCharEncoding::None) {
            ctxt.switch_encoding(enc);
        }

        Some(input_stream)
    }

    /// Create a new input stream based on a file or an URL.
    ///
    /// Returns the new input stream or NULL in case of error
    #[doc(alias = "xmlNewInputFromFile")]
    pub fn from_filename(ctxt: &mut XmlParserCtxt, filename: &str) -> Option<XmlParserInput<'a>> {
        if get_parser_debug_entities() != 0 {
            generic_error!("new input from file: {filename}\n");
        }

        let Some(buf) = XmlParserInputBuffer::from_uri(filename, XmlCharEncoding::None) else {
            __xml_loader_err!(ctxt, "failed to load external entity \"{}\"\n", filename);
            return None;
        };

        let mut input_stream = XmlParserInput::new(Some(ctxt))?;
        input_stream.buf = Some(buf);
        let mut input_stream = xml_check_http_input(ctxt, Some(input_stream))?;

        let uri = input_stream
            .filename
            .clone()
            .unwrap_or_else(|| filename.to_owned());
        let directory = xml_parser_get_directory(&uri);
        let canonic = canonic_path(&uri);
        input_stream.filename = Some(canonic.into_owned());
        if let Some(directory) = directory.as_deref() {
            input_stream.directory = Some(directory.to_string_lossy().into_owned());
        } else {
            input_stream.directory = None;
        }
        input_stream.reset_base();

        if ctxt.directory.is_none() {
            if let Some(directory) = directory {
                ctxt.directory = Some(directory.to_string_lossy().into_owned());
            }
        }
        Some(input_stream)
    }

    /// Create a new input stream based on an xmlEntityPtr
    ///
    /// Returns the new input stream or NULL
    #[doc(alias = "xmlNewEntityInputStream")]
    pub(crate) fn from_entity(ctxt: &mut XmlParserCtxt, mut entity: XmlEntityPtr) -> Option<Self> {
        if get_parser_debug_entities() != 0 {
            generic_error!("new input from entity: {}\n", entity.name);
        }
        let Some(content) = entity.content.as_deref() else {
            match entity.etype {
                XmlEntityType::XmlExternalGeneralUnparsedEntity => {
                    xml_err_internal!(ctxt, "Cannot parse entity {}\n", entity.name.clone());
                }
                XmlEntityType::XmlExternalGeneralParsedEntity
                | XmlEntityType::XmlExternalParameterEntity => {
                    let mut input = xml_load_external_entity(
                        entity.uri.as_deref(),
                        entity.external_id.as_deref(),
                        ctxt,
                    );
                    if let Some(input) = input.as_mut() {
                        input.entity = Some(entity);
                    }
                    return input;
                }
                XmlEntityType::XmlInternalGeneralEntity => {
                    xml_err_internal!(
                        ctxt,
                        "Internal entity {} without content !\n",
                        entity.name.clone()
                    );
                }
                XmlEntityType::XmlInternalParameterEntity => {
                    xml_err_internal!(
                        ctxt,
                        "Internal parameter entity {} without content !\n",
                        entity.name.clone()
                    );
                }
                XmlEntityType::XmlInternalPredefinedEntity => {
                    xml_err_internal!(
                        ctxt,
                        "Predefined entity {} without content !\n",
                        entity.name.clone()
                    );
                }
            }
            return None;
        };
        let buf = std::io::Cursor::new(content.as_bytes().to_vec());
        let mut buf = XmlParserInputBuffer::from_reader(buf, XmlCharEncoding::None);
        if ctxt.progressive {
            // If PROGRESSIVE is true, the buffer is not automatically filled,
            // so it is forced to be filled at this point.
            while buf.grow(INPUT_CHUNK) != 0 {}
        }
        let mut input = XmlParserInput::from_io(ctxt, buf, XmlCharEncoding::None)?;

        if let Some(uri) = entity.uri.as_deref() {
            input.filename = Some(uri.to_owned());
        }
        input.base = 0;
        if entity.length == 0 {
            entity.length = content.len() as i32;
        }
        input.cur = 0;
        input.length = entity.length;
        input.entity = Some(entity);
        Some(input)
    }

    pub fn base_contents(&self) -> &[u8] {
        if self.base == usize::MAX {
            return &[];
        }
        &self
            .buf
            .as_ref()
            .map(|buf| buf.buffer.as_ref())
            .unwrap_or(&[][..])[self.base..]
    }

    pub(crate) fn current_contents(&self) -> &[u8] {
        &self.base_contents()[self.cur..]
    }

    /// Return the offset of `cur` from `base`.
    pub(crate) fn offset_from_base(&self) -> usize {
        self.cur
    }

    /// Return the length of remainder buffer length.  
    /// In other word, the offset of `end` from `cur`.
    pub(crate) fn remainder_len(&self) -> usize {
        self.current_contents().len()
    }

    /// Update the input to use the current set of pointers from the buffer.
    ///
    /// Returns `-1` in case of error, `0` otherwise
    #[doc(alias = "xmlBufResetInput")]
    pub(crate) fn reset_base(&mut self) -> i32 {
        self.base = 0;
        self.cur = 0;
        self.valid_up_to = 0;
        0
    }

    /// Reduce buffer capacity.  
    /// The `base` and `cur` are automatically aligned according to the number of bytes removed.
    ///
    /// # Panics
    /// - The `buf` must not be `None`.
    pub(crate) fn shrink(&mut self) {
        if self.cur <= INPUT_CHUNK {
            return;
        }

        let buf = self.buf.as_mut().unwrap();
        let diff = self.cur - LINE_LEN;
        self.consumed = self.consumed.saturating_add(diff as u64);
        buf.buffer.drain(..diff);
        if self.cur < self.valid_up_to {
            self.valid_up_to -= diff;
        } else {
            self.valid_up_to = 0;
        }
        self.base = 0;
        self.cur = LINE_LEN;
    }

    #[doc(alias = "xmlDetectEBCDIC")]
    pub(crate) fn detect_ebcdic(&self) -> Option<XmlCharEncodingHandler> {
        let mut out: [u8; 200] = [0; 200];

        // To detect the EBCDIC code page, we convert the first 200 bytes
        // to EBCDIC-US and try to find the encoding declaration.
        let mut handler = get_encoding_handler(XmlCharEncoding::EBCDIC)?;
        let inlen = self.remainder_len();
        let outstr = from_utf8_mut(&mut out).ok()?;
        let Ok((_, outlen)) = handler.decode(&self.current_contents()[..inlen], outstr) else {
            return Some(handler);
        };
        out[outlen] = 0;

        let mut i: usize = 0;
        while i < outlen {
            if out[i] == b'>' {
                break;
            }
            if out[i] == b'e' && out[i..].starts_with(b"encoding") {
                let mut cur: u8;

                i += 8;
                while out[i].is_xml_blank_char() {
                    i += 1;
                }
                i += 1;
                if out[i - 1] != b'=' {
                    break;
                }
                while out[i].is_xml_blank_char() {
                    i += 1;
                }
                let quote: u8 = out[i];
                i += 1;
                if quote != b'\'' && quote != b'"' {
                    break;
                }
                let start: usize = i;
                cur = out[i];
                while cur.is_ascii_lowercase()
                    || cur.is_ascii_uppercase()
                    || cur.is_ascii_digit()
                    || cur == b'.'
                    || cur == b'_'
                    || cur == b'-'
                {
                    i += 1;
                    cur = out[i];
                }
                if cur != quote {
                    break;
                }
                return from_utf8(&out[start..i])
                    .ok()
                    .and_then(find_encoding_handler);
            }

            i += 1;
        }

        Some(handler)
    }
}