-
Notifications
You must be signed in to change notification settings - Fork 235
/
mod.rs
1914 lines (1730 loc) · 76.5 KB
/
mod.rs
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! A module to handle `Reader`
use std::borrow::Cow;
use std::ops::{Deref, DerefMut};
use std::str::from_utf8;
#[cfg(feature = "encoding")]
use encoding_rs::{Encoding, UTF_16BE, UTF_16LE, UTF_8};
use crate::errors::{Error, Result};
use crate::events::{BytesCData, BytesDecl, BytesEnd, BytesStart, BytesText, Event};
use crate::name::{LocalName, NamespaceResolver, QName, ResolveResult};
use memchr;
mod buffered_reader;
mod slice_reader;
pub use self::buffered_reader::BufferedReader;
pub use self::slice_reader::SliceReader;
/// Possible reader states. The state transition diagram (`true` and `false` shows
/// value of [`Reader::expand_empty_elements()`] option):
///
/// ```mermaid
/// flowchart LR
/// subgraph _
/// direction LR
///
/// Init -- "(no event)"\nStartText --> Opened
/// Opened -- Decl, DocType, PI\nComment, CData\nStart, Empty, End --> Closed
/// Closed -- "#lt;false#gt;\n(no event)"\nText --> Opened
/// end
/// Closed -- "#lt;true#gt;"\nStart --> Empty
/// Empty -- End --> Closed
/// _ -. Eof .-> Exit
/// ```
#[derive(Clone)]
enum TagState {
/// Initial state in which reader stay after creation. Transition from that
/// state could produce a `StartText`, `Decl`, `Comment` or `Start` event.
/// The next state is always `Opened`. The reader will never return to this
/// state. The event emitted during transition to `Opened` is a `StartEvent`
/// if the first symbol not `<`, otherwise no event are emitted.
Init,
/// State after seeing the `<` symbol. Depending on the next symbol all other
/// events (except `StartText`) could be generated.
///
/// After generating ane event the reader moves to the `Closed` state.
Opened,
/// State in which reader searches the `<` symbol of a markup. All bytes before
/// that symbol will be returned in the [`Event::Text`] event. After that
/// the reader moves to the `Opened` state.
Closed,
/// This state is used only if option `expand_empty_elements` is set to `true`.
/// Reader enters to this state when it is in a `Closed` state and emits an
/// [`Event::Start`] event. The next event emitted will be an [`Event::End`],
/// after which reader returned to the `Closed` state.
Empty,
/// Reader enters this state when `Eof` event generated or an error occurred.
/// This is the last state, the reader stay in it forever.
Exit,
}
/// A reference to an encoding together with information about how it was retrieved.
///
/// The state transition diagram:
///
/// ```mermaid
/// flowchart LR
/// Implicit -- from_str --> Explicit
/// Implicit -- BOM --> BomDetected
/// Implicit -- "encoding=..." --> XmlDetected
/// BomDetected -- "encoding=..." --> XmlDetected
/// ```
#[cfg(feature = "encoding")]
#[derive(Clone, Copy)]
enum EncodingRef {
/// Encoding was implicitly assumed to have a specified value. It can be refined
/// using BOM or by the XML declaration event (`<?xml encoding=... ?>`)
Implicit(&'static Encoding),
/// Encoding was explicitly set to the desired value. It cannot be changed
/// nor by BOM, nor by parsing XML declaration (`<?xml encoding=... ?>`)
Explicit(&'static Encoding),
/// Encoding was detected from a byte order mark (BOM) or by the first bytes
/// of the content. It can be refined by the XML declaration event (`<?xml encoding=... ?>`)
BomDetected(&'static Encoding),
/// Encoding was detected using XML declaration event (`<?xml encoding=... ?>`).
/// It can no longer change
XmlDetected(&'static Encoding),
}
#[cfg(feature = "encoding")]
impl EncodingRef {
#[inline]
fn encoding(&self) -> &'static Encoding {
match self {
Self::Implicit(e) => e,
Self::Explicit(e) => e,
Self::BomDetected(e) => e,
Self::XmlDetected(e) => e,
}
}
#[inline]
fn can_be_refined(&self) -> bool {
match self {
Self::Implicit(_) | Self::BomDetected(_) => true,
Self::Explicit(_) | Self::XmlDetected(_) => false,
}
}
}
/// A trait for the underlying abstracion handling the actual reading part for the [`Reader`].
pub trait InnerReader: Deref<Target = Self::Reader> + DerefMut {
/// The real type of the inner reader.
type Reader;
/// Consumes this abstration returning the underlying reader.
fn into_inner(self) -> Self::Reader;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// A low level encoding-agnostic XML event reader.
///
/// Consumes bytes and streams XML [`Event`]s.
///
/// # Examples
///
/// ```
/// use quick_xml::Reader;
/// use quick_xml::events::Event;
///
/// let xml = r#"<tag1 att1 = "test">
/// <tag2><!--Test comment-->Test</tag2>
/// <tag2>Test 2</tag2>
/// </tag1>"#;
/// let mut reader = Reader::from_reader(xml.as_bytes());
/// reader.trim_text(true);
///
/// let mut count = 0;
/// let mut txt = Vec::new();
/// let mut buf = Vec::new();
///
/// // The `Reader` does not implement `Iterator` because it outputs borrowed data (`Cow`s)
/// loop {
/// // NOTE: this is the generic case when we don't know about the input BufRead.
/// // when the input is a &str or a &[u8], we don't actually need to use another
/// // buffer, we could directly call `reader.read_event()`
/// match reader.read_event_into(&mut buf) {
/// Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
/// // exits the loop when reaching end of file
/// Ok(Event::Eof) => break,
///
/// Ok(Event::Start(e)) => {
/// match e.name().as_ref() {
/// b"tag1" => println!("attributes values: {:?}",
/// e.attributes().map(|a| a.unwrap().value)
/// .collect::<Vec<_>>()),
/// b"tag2" => count += 1,
/// _ => (),
/// }
/// }
/// Ok(Event::Text(e)) => txt.push(e.decode_and_unescape(&reader).unwrap().into_owned()),
///
/// // There are several other `Event`s we do not consider here
/// _ => (),
/// }
/// // if we don't keep a borrow elsewhere, we can clear the buffer to keep memory usage low
/// buf.clear();
/// }
/// ```
#[derive(Clone)]
pub struct Reader<R> {
/// reader
pub(crate) reader: R,
/// current buffer position, useful for debugging errors
buf_position: usize,
/// current state Open/Close
tag_state: TagState,
/// expand empty element into an opening and closing element
expand_empty_elements: bool,
/// trims leading whitespace in Text events, skip the element if text is empty
trim_text_start: bool,
/// trims trailing whitespace in Text events.
trim_text_end: bool,
/// trims trailing whitespaces from markup names in closing tags `</a >`
trim_markup_names_in_closing_tags: bool,
/// check if End nodes match last Start node
check_end_names: bool,
/// check if comments contains `--` (false per default)
check_comments: bool,
/// All currently Started elements which didn't have a matching
/// End element yet.
///
/// For an XML
///
/// ```xml
/// <root><one/><inner attr="value">|<tag></inner></root>
/// ```
/// when cursor at the `|` position buffer contains:
///
/// ```text
/// rootinner
/// ^ ^
/// ```
///
/// The `^` symbols shows which positions stored in the [`Self::opened_starts`]
/// (0 and 4 in that case).
opened_buffer: Vec<u8>,
/// Opened name start indexes into [`Self::opened_buffer`]. See documentation
/// for that field for details
opened_starts: Vec<usize>,
/// A buffer to manage namespaces
ns_resolver: NamespaceResolver,
/// For `Empty` events keep the 'scope' of the namespace on the stack artificially. That way, the
/// consumer has a chance to use `resolve` in the context of the empty element. We perform the
/// pop as the first operation in the next `next()` call.
pending_pop: bool,
#[cfg(feature = "encoding")]
/// Reference to the encoding used to read an XML
encoding: EncodingRef,
}
/// Builder methods
impl<R> Reader<R> {
/// Creates a `Reader` that reads from a given reader.
fn from_reader_internal(reader: R) -> Self {
Self {
reader,
opened_buffer: Vec::new(),
opened_starts: Vec::new(),
tag_state: TagState::Init,
expand_empty_elements: false,
trim_text_start: false,
trim_text_end: false,
trim_markup_names_in_closing_tags: true,
check_end_names: true,
buf_position: 0,
check_comments: false,
ns_resolver: NamespaceResolver::default(),
pending_pop: false,
#[cfg(feature = "encoding")]
encoding: EncodingRef::Implicit(UTF_8),
}
}
/// Changes whether empty elements should be split into an `Open` and a `Close` event.
///
/// When set to `true`, all [`Empty`] events produced by a self-closing tag like `<tag/>` are
/// expanded into a [`Start`] event followed by an [`End`] event. When set to `false` (the
/// default), those tags are represented by an [`Empty`] event instead.
///
/// Note, that setting this to `true` will lead to additional allocates that
/// needed to store tag name for an [`End`] event. There is no additional
/// allocation, however, if [`Self::check_end_names()`] is also set.
///
/// (`false` by default)
///
/// [`Empty`]: Event::Empty
/// [`Start`]: Event::Start
/// [`End`]: Event::End
pub fn expand_empty_elements(&mut self, val: bool) -> &mut Self {
self.expand_empty_elements = val;
self
}
/// Changes whether whitespace before and after character data should be removed.
///
/// When set to `true`, all [`Text`] events are trimmed. If they are empty, no event will be
/// pushed.
///
/// (`false` by default)
///
/// [`Text`]: Event::Text
pub fn trim_text(&mut self, val: bool) -> &mut Self {
self.trim_text_start = val;
self.trim_text_end = val;
self
}
/// Changes whether whitespace after character data should be removed.
///
/// When set to `true`, trailing whitespace is trimmed in [`Text`] events.
///
/// (`false` by default)
///
/// [`Text`]: Event::Text
pub fn trim_text_end(&mut self, val: bool) -> &mut Self {
self.trim_text_end = val;
self
}
/// Changes whether trailing whitespaces after the markup name are trimmed in closing tags
/// `</a >`.
///
/// If true the emitted [`End`] event is stripped of trailing whitespace after the markup name.
///
/// Note that if set to `false` and `check_end_names` is true the comparison of markup names is
/// going to fail erronously if a closing tag contains trailing whitespaces.
///
/// (`true` by default)
///
/// [`End`]: Event::End
pub fn trim_markup_names_in_closing_tags(&mut self, val: bool) -> &mut Self {
self.trim_markup_names_in_closing_tags = val;
self
}
/// Changes whether mismatched closing tag names should be detected.
///
/// When set to `false`, it won't check if a closing tag matches the corresponding opening tag.
/// For example, `<mytag></different_tag>` will be permitted.
///
/// If the XML is known to be sane (already processed, etc.) this saves extra time.
///
/// Note that the emitted [`End`] event will not be modified if this is disabled, ie. it will
/// contain the data of the mismatched end tag.
///
/// Note, that setting this to `true` will lead to additional allocates that
/// needed to store tag name for an [`End`] event. There is no additional
/// allocation, however, if [`Self::expand_empty_elements()`] is also set.
///
/// (`true` by default)
///
/// [`End`]: Event::End
pub fn check_end_names(&mut self, val: bool) -> &mut Self {
self.check_end_names = val;
self
}
/// Changes whether comments should be validated.
///
/// When set to `true`, every [`Comment`] event will be checked for not containing `--`, which
/// is not allowed in XML comments. Most of the time we don't want comments at all so we don't
/// really care about comment correctness, thus the default value is `false` to improve
/// performance.
///
/// (`false` by default)
///
/// [`Comment`]: Event::Comment
pub fn check_comments(&mut self, val: bool) -> &mut Self {
self.check_comments = val;
self
}
}
/// Getters
impl<R, I: InnerReader<Reader = R>> Reader<I> {
/// Consumes `Reader` returning the underlying reader
///
/// Can be used to compute line and column of a parsing error position
///
/// # Examples
///
/// ```
/// # use pretty_assertions::assert_eq;
/// use std::{str, io::Cursor};
/// use quick_xml::{BufferedReader, Reader};
/// use quick_xml::events::Event;
///
/// let xml = r#"<tag1 att1 = "test">
/// <tag2><!--Test comment-->Test</tag2>
/// <tag3>Test 2</tag3>
/// </tag1>"#;
/// let mut reader = Reader::from_reader(Cursor::new(xml.as_bytes()));
/// let mut buf = Vec::new();
///
/// fn into_line_and_column(reader: Reader<BufferedReader<Cursor<&[u8]>>>) -> (usize, usize) {
/// let end_pos = reader.buffer_position();
/// let mut cursor = reader.into_inner();
/// let s = String::from_utf8(cursor.into_inner()[0..end_pos].to_owned())
/// .expect("can't make a string");
/// let mut line = 1;
/// let mut column = 0;
/// for c in s.chars() {
/// if c == '\n' {
/// line += 1;
/// column = 0;
/// } else {
/// column += 1;
/// }
/// }
/// (line, column)
/// }
///
/// loop {
/// match reader.read_event_into(&mut buf) {
/// Ok(Event::Start(ref e)) => match e.name().as_ref() {
/// b"tag1" | b"tag2" => (),
/// tag => {
/// assert_eq!(b"tag3", tag);
/// assert_eq!((3, 22), into_line_and_column(reader));
/// break;
/// }
/// },
/// Ok(Event::Eof) => unreachable!(),
/// _ => (),
/// }
/// buf.clear();
/// }
/// ```
pub fn into_inner(self) -> R {
self.reader.into_inner()
}
/// Gets a reference to the underlying reader.
pub fn get_ref(&self) -> &R {
&self.reader
}
/// Gets a mutable reference to the underlying reader.
pub fn get_mut(&mut self) -> &mut R {
&mut self.reader
}
}
/// Getters that are not specific to any inner reader implementation
impl<R> Reader<R> {
/// Gets the current byte position in the input data.
///
/// Useful when debugging errors.
pub fn buffer_position(&self) -> usize {
// when internal state is Opened, we have actually read until '<',
// which we don't want to show
if let TagState::Opened = self.tag_state {
self.buf_position - 1
} else {
self.buf_position
}
}
/// Resolves a potentially qualified **event name** into (namespace name, local name).
///
/// *Qualified* attribute names have the form `prefix:local-name` where the`prefix` is defined
/// on any containing XML element via `xmlns:prefix="the:namespace:uri"`. The namespace prefix
/// can be defined on the same element as the attribute in question.
///
/// *Unqualified* event inherits the current *default namespace*.
///
/// # Lifetimes
///
/// - `'n`: lifetime of an element name
/// - `'ns`: lifetime of a namespaces buffer, where all found namespaces are stored
#[inline]
pub fn event_namespace<'n, 'ns>(
&self,
name: QName<'n>,
namespace_buffer: &'ns [u8],
) -> (ResolveResult<'ns>, LocalName<'n>) {
self.ns_resolver.resolve(name, namespace_buffer, true)
}
/// Resolves a potentially qualified **attribute name** into (namespace name, local name).
///
/// *Qualified* attribute names have the form `prefix:local-name` where the`prefix` is defined
/// on any containing XML element via `xmlns:prefix="the:namespace:uri"`. The namespace prefix
/// can be defined on the same element as the attribute in question.
///
/// *Unqualified* attribute names do *not* inherit the current *default namespace*.
///
/// # Lifetimes
///
/// - `'n`: lifetime of an attribute
/// - `'ns`: lifetime of a namespaces buffer, where all found namespaces are stored
#[inline]
pub fn attribute_namespace<'n, 'ns>(
&self,
name: QName<'n>,
namespace_buffer: &'ns [u8],
) -> (ResolveResult<'ns>, LocalName<'n>) {
self.ns_resolver.resolve(name, namespace_buffer, false)
}
/// Get the decoder, used to decode bytes, read by this reader, to the strings.
///
/// If `encoding` feature is enabled, the used encoding may change after
/// parsing the XML declaration, otherwise encoding is fixed to UTF-8.
///
/// If `encoding` feature is enabled and no encoding is specified in declaration,
/// defaults to UTF-8.
pub fn decoder(&self) -> Decoder {
Decoder {
#[cfg(feature = "encoding")]
encoding: self.encoding.encoding(),
}
}
}
/// Common parsing code for all reader implementations.
impl<R> Reader<R> {
/// reads `BytesElement` starting with a `!`,
/// return `Comment`, `CData` or `DocType` event
fn read_bang<'b>(&mut self, bang_type: BangType, buf: &'b [u8]) -> Result<Event<'b>> {
let uncased_starts_with = |string: &[u8], prefix: &[u8]| {
string.len() >= prefix.len() && string[..prefix.len()].eq_ignore_ascii_case(prefix)
};
let len = buf.len();
match bang_type {
BangType::Comment if buf.starts_with(b"!--") => {
if self.check_comments {
// search if '--' not in comments
if let Some(p) = memchr::memchr_iter(b'-', &buf[3..len - 2])
.position(|p| buf[3 + p + 1] == b'-')
{
self.buf_position += len - p;
return Err(Error::UnexpectedToken("--".to_string()));
}
}
Ok(Event::Comment(BytesText::from_escaped(&buf[3..len - 2])))
}
BangType::CData if uncased_starts_with(buf, b"![CDATA[") => {
Ok(Event::CData(BytesCData::new(&buf[8..])))
}
BangType::DocType if uncased_starts_with(buf, b"!DOCTYPE") => {
let start = buf[8..]
.iter()
.position(|b| !is_whitespace(*b))
.unwrap_or_else(|| len - 8);
debug_assert!(start < len - 8, "DocType must have a name");
Ok(Event::DocType(BytesText::from_escaped(&buf[8 + start..])))
}
_ => Err(bang_type.to_err()),
}
}
/// reads `BytesElement` starting with a `/`,
/// if `self.check_end_names`, checks that element matches last opened element
/// return `End` event
fn read_end<'b>(&mut self, buf: &'b [u8]) -> Result<Event<'b>> {
// XML standard permits whitespaces after the markup name in closing tags.
// Let's strip them from the buffer before comparing tag names.
let name = if self.trim_markup_names_in_closing_tags {
if let Some(pos_end_name) = buf[1..].iter().rposition(|&b| !b.is_ascii_whitespace()) {
let (name, _) = buf[1..].split_at(pos_end_name + 1);
name
} else {
&buf[1..]
}
} else {
&buf[1..]
};
if self.check_end_names {
let mismatch_err = |expected: &[u8], found: &[u8], buf_position: &mut usize| {
*buf_position -= buf.len();
Err(Error::EndEventMismatch {
expected: from_utf8(expected).unwrap_or("").to_owned(),
found: from_utf8(found).unwrap_or("").to_owned(),
})
};
match self.opened_starts.pop() {
Some(start) => {
let expected = &self.opened_buffer[start..];
if name != expected {
mismatch_err(expected, name, &mut self.buf_position)
} else {
self.opened_buffer.truncate(start);
Ok(Event::End(BytesEnd::borrowed(name)))
}
}
None => mismatch_err(b"", &buf[1..], &mut self.buf_position),
}
} else {
Ok(Event::End(BytesEnd::borrowed(name)))
}
}
/// reads `BytesElement` starting with a `?`,
/// return `Decl` or `PI` event
fn read_question_mark<'b>(&mut self, buf: &'b [u8]) -> Result<Event<'b>> {
let len = buf.len();
if len > 2 && buf[len - 1] == b'?' {
if len > 5 && &buf[1..4] == b"xml" && is_whitespace(buf[4]) {
let event = BytesDecl::from_start(BytesStart::borrowed(&buf[1..len - 1], 3));
// Try getting encoding from the declaration event
#[cfg(feature = "encoding")]
if self.encoding.can_be_refined() {
if let Some(encoding) = event.encoder() {
self.encoding = EncodingRef::XmlDetected(encoding);
}
}
Ok(Event::Decl(event))
} else {
Ok(Event::PI(BytesText::from_escaped(&buf[1..len - 1])))
}
} else {
self.buf_position -= len;
Err(Error::UnexpectedEof("XmlDecl".to_string()))
}
}
#[inline]
fn close_expanded_empty(&mut self) -> Result<Event<'static>> {
self.tag_state = TagState::Closed;
let name = self
.opened_buffer
.split_off(self.opened_starts.pop().unwrap());
Ok(Event::End(BytesEnd::owned(name)))
}
/// reads `BytesElement` starting with any character except `/`, `!` or ``?`
/// return `Start` or `Empty` event
fn read_start<'b>(&mut self, buf: &'b [u8]) -> Result<Event<'b>> {
// TODO: do this directly when reading bufreader ...
let len = buf.len();
let name_end = buf.iter().position(|&b| is_whitespace(b)).unwrap_or(len);
if let Some(&b'/') = buf.last() {
let end = if name_end < len { name_end } else { len - 1 };
if self.expand_empty_elements {
self.tag_state = TagState::Empty;
self.opened_starts.push(self.opened_buffer.len());
self.opened_buffer.extend(&buf[..end]);
Ok(Event::Start(BytesStart::borrowed(&buf[..len - 1], end)))
} else {
Ok(Event::Empty(BytesStart::borrowed(&buf[..len - 1], end)))
}
} else {
if self.check_end_names {
self.opened_starts.push(self.opened_buffer.len());
self.opened_buffer.extend(&buf[..name_end]);
}
Ok(Event::Start(BytesStart::borrowed(buf, name_end)))
}
}
fn resolve_namespaced_event_inner<'b, 'ns>(
&mut self,
event: Result<Event<'b>>,
namespace_buffer: &'ns mut Vec<u8>,
) -> Result<(ResolveResult<'ns>, Event<'b>)> {
match event {
Ok(Event::Eof) => Ok((ResolveResult::Unbound, Event::Eof)),
Ok(Event::Start(e)) => {
self.ns_resolver.push(&e, namespace_buffer);
Ok((
self.ns_resolver.find(e.name(), namespace_buffer),
Event::Start(e),
))
}
Ok(Event::Empty(e)) => {
// For empty elements we need to 'artificially' keep the namespace scope on the
// stack until the next `next()` call occurs.
// Otherwise the caller has no chance to use `resolve` in the context of the
// namespace declarations that are 'in scope' for the empty element alone.
// Ex: <img rdf:nodeID="abc" xmlns:rdf="urn:the-rdf-uri" />
self.ns_resolver.push(&e, namespace_buffer);
// notify next `read_namespaced_event()` invocation that it needs to pop this
// namespace scope
self.pending_pop = true;
Ok((
self.ns_resolver.find(e.name(), namespace_buffer),
Event::Empty(e),
))
}
Ok(Event::End(e)) => {
// notify next `read_namespaced_event()` invocation that it needs to pop this
// namespace scope
self.pending_pop = true;
Ok((
self.ns_resolver.find(e.name(), namespace_buffer),
Event::End(e),
))
}
Ok(e) => Ok((ResolveResult::Unbound, e)),
Err(e) => Err(e),
}
}
}
/// Possible elements started with `<!`
#[derive(Debug, PartialEq)]
enum BangType {
/// <![CDATA[...]]>
CData,
/// <!--...-->
Comment,
/// <!DOCTYPE...>
DocType,
}
impl BangType {
#[inline(always)]
fn new(byte: Option<u8>) -> Result<Self> {
Ok(match byte {
Some(b'[') => Self::CData,
Some(b'-') => Self::Comment,
Some(b'D') | Some(b'd') => Self::DocType,
Some(b) => return Err(Error::UnexpectedBang(b)),
None => return Err(Error::UnexpectedEof("Bang".to_string())),
})
}
/// If element is finished, returns its content up to `>` symbol and
/// an index of this symbol, otherwise returns `None`
#[inline(always)]
fn parse<'b>(&self, chunk: &'b [u8], offset: usize) -> Option<(&'b [u8], usize)> {
for i in memchr::memchr_iter(b'>', chunk) {
match self {
// Need to read at least 6 symbols (`!---->`) for properly finished comment
// <!----> - XML comment
// 012345 - i
Self::Comment => {
if offset + i > 4 && chunk[..i].ends_with(b"--") {
// We cannot strip last `--` from the buffer because we need it in case of
// check_comments enabled option. XML standard requires that comment
// will not end with `--->` sequence because this is a special case of
// `--` in the comment (https://www.w3.org/TR/xml11/#sec-comments)
return Some((&chunk[..i], i + 1)); // +1 for `>`
}
}
Self::CData => {
if chunk[..i].ends_with(b"]]") {
return Some((&chunk[..i - 2], i + 1)); // +1 for `>`
}
}
Self::DocType => {
let content = &chunk[..i];
let balance = memchr::memchr2_iter(b'<', b'>', content)
.map(|p| if content[p] == b'<' { 1i32 } else { -1 })
.sum::<i32>();
if balance == 0 {
return Some((content, i + 1)); // +1 for `>`
}
}
}
}
None
}
#[inline]
fn to_err(self) -> Error {
let bang_str = match self {
Self::CData => "CData",
Self::Comment => "Comment",
Self::DocType => "DOCTYPE",
};
Error::UnexpectedEof(bang_str.to_string())
}
}
/// State machine for the [`XmlSource::read_element`]
#[derive(Clone, Copy)]
enum ReadElementState {
/// The initial state (inside element, but outside of attribute value)
Elem,
/// Inside a single-quoted attribute value
SingleQ,
/// Inside a double-quoted attribute value
DoubleQ,
}
impl ReadElementState {
/// Changes state by analyzing part of input.
/// Returns a tuple with part of chunk up to element closing symbol `>`
/// and a position after that symbol or `None` if such symbol was not found
#[inline(always)]
fn change<'b>(&mut self, chunk: &'b [u8]) -> Option<(&'b [u8], usize)> {
for i in memchr::memchr3_iter(b'>', b'\'', b'"', chunk) {
*self = match (*self, chunk[i]) {
// only allowed to match `>` while we are in state `Elem`
(Self::Elem, b'>') => return Some((&chunk[..i], i + 1)),
(Self::Elem, b'\'') => Self::SingleQ,
(Self::Elem, b'\"') => Self::DoubleQ,
// the only end_byte that gets us out if the same character
(Self::SingleQ, b'\'') | (Self::DoubleQ, b'"') => Self::Elem,
// all other bytes: no state change
_ => *self,
};
}
None
}
}
/// A function to check whether the byte is a whitespace (blank, new line, carriage return or tab)
#[inline]
pub(crate) fn is_whitespace(b: u8) -> bool {
match b {
b' ' | b'\r' | b'\n' | b'\t' => true,
_ => false,
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
/// Decoder of byte slices to the strings. This is lightweight object that can be copied.
///
/// If feature `encoding` is enabled, this encoding taken from the `"encoding"`
/// XML declaration or assumes UTF-8, if XML has no <?xml ?> declaration, encoding
/// key is not defined or contains unknown encoding.
///
/// The library supports any UTF-8 compatible encodings that crate `encoding_rs`
/// is supported. [*UTF-16 is not supported at the present*][utf16].
///
/// If feature `encoding` is disabled, the decoder is always UTF-8 decoder:
/// any XML declarations are ignored.
///
/// [utf16]: https://github.com/tafia/quick-xml/issues/158
#[derive(Clone, Copy, Debug)]
pub struct Decoder {
#[cfg(feature = "encoding")]
encoding: &'static Encoding,
}
#[cfg(not(feature = "encoding"))]
impl Decoder {
/// Decodes a UTF8 slice regardless of XML declaration and ignoring BOM if
/// it is present in the `bytes`.
///
/// Returns an error in case of malformed sequences in the `bytes`.
///
/// If you instead want to use XML declared encoding, use the `encoding` feature
#[inline]
pub fn decode<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
Ok(Cow::Borrowed(from_utf8(bytes)?))
}
/// Decodes a slice regardless of XML declaration with BOM removal if
/// it is present in the `bytes`.
///
/// Returns an error in case of malformed sequences in the `bytes`.
///
/// If you instead want to use XML declared encoding, use the `encoding` feature
pub fn decode_with_bom_removal<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
let bytes = if bytes.starts_with(b"\xEF\xBB\xBF") {
&bytes[3..]
} else {
bytes
};
self.decode(bytes)
}
}
#[cfg(feature = "encoding")]
impl Decoder {
/// Returns the `Reader`s encoding.
///
/// This encoding will be used by [`decode`].
///
/// [`decode`]: Self::decode
pub fn encoding(&self) -> &'static Encoding {
self.encoding
}
/// Decodes specified bytes using encoding, declared in the XML, if it was
/// declared there, or UTF-8 otherwise, and ignoring BOM if it is present
/// in the `bytes`.
///
/// Returns an error in case of malformed sequences in the `bytes`.
pub fn decode<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
match self
.encoding
.decode_without_bom_handling_and_without_replacement(bytes)
{
None => Err(Error::NonDecodable(None)),
Some(s) => Ok(s),
}
}
/// Decodes a slice with BOM removal if it is present in the `bytes` using
/// the reader encoding.
///
/// If this method called after reading XML declaration with the `"encoding"`
/// key, then this encoding is used, otherwise UTF-8 is used.
///
/// If XML declaration is absent in the XML, UTF-8 is used.
///
/// Returns an error in case of malformed sequences in the `bytes`.
pub fn decode_with_bom_removal<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
self.decode(self.remove_bom(bytes))
}
/// Copied from [`Encoding::decode_with_bom_removal`]
#[inline]
fn remove_bom<'b>(&self, bytes: &'b [u8]) -> &'b [u8] {
if self.encoding == UTF_8 && bytes.starts_with(b"\xEF\xBB\xBF") {
return &bytes[3..];
}
if self.encoding == UTF_16LE && bytes.starts_with(b"\xFF\xFE") {
return &bytes[2..];
}
if self.encoding == UTF_16BE && bytes.starts_with(b"\xFE\xFF") {
return &bytes[2..];
}
bytes
}
}
/// This implementation is required for tests of other parts of the library
#[cfg(test)]
#[cfg(feature = "serialize")]
impl Decoder {
pub(crate) fn utf8() -> Self {
Decoder {
#[cfg(feature = "encoding")]
encoding: UTF_8,
}
}
#[cfg(feature = "encoding")]
pub(crate) fn utf16() -> Self {
Decoder { encoding: UTF_16LE }
}
}
/// Automatic encoding detection of XML files based using the [recommended algorithm]
/// (https://www.w3.org/TR/xml11/#sec-guessing)
///
/// The algorithm suggests examine up to the first 4 bytes to determine encoding
/// according to the following table:
///
/// | Bytes |Detected encoding
/// |-------------|------------------------------------------
/// |`00 00 FE FF`|UCS-4, big-endian machine (1234 order)
/// |`FF FE 00 00`|UCS-4, little-endian machine (4321 order)
/// |`00 00 FF FE`|UCS-4, unusual octet order (2143)
/// |`FE FF 00 00`|UCS-4, unusual octet order (3412)
/// |`FE FF ## ##`|UTF-16, big-endian
/// |`FF FE ## ##`|UTF-16, little-endian
/// |`EF BB BF` |UTF-8
/// |-------------|------------------------------------------
/// |`00 00 00 3C`|UCS-4 or similar (use declared encoding to find the exact one), in big-endian (1234)
/// |`3C 00 00 00`|UCS-4 or similar (use declared encoding to find the exact one), in little-endian (4321)
/// |`00 00 3C 00`|UCS-4 or similar (use declared encoding to find the exact one), in unusual byte orders (2143)
/// |`00 3C 00 00`|UCS-4 or similar (use declared encoding to find the exact one), in unusual byte orders (3412)
/// |`00 3C 00 3F`|UTF-16 BE or ISO-10646-UCS-2 BE or similar 16-bit BE (use declared encoding to find the exact one)
/// |`3C 00 3F 00`|UTF-16 LE or ISO-10646-UCS-2 LE or similar 16-bit LE (use declared encoding to find the exact one)
/// |`3C 3F 78 6D`|UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, or mixed-width encoding which ensures that the characters of ASCII have their normal positions, width, and values; the actual encoding declaration must be read to detect which of these applies, but since all of these encodings use the same bit patterns for the relevant ASCII characters, the encoding declaration itself may be read reliably
/// |`4C 6F A7 94`|EBCDIC (in some flavor; the full encoding declaration must be read to tell which code page is in use)
/// |_Other_ |UTF-8 without an encoding declaration, or else the data stream is mislabeled (lacking a required encoding declaration), corrupt, fragmentary, or enclosed in a wrapper of some kind
///
/// Because [`encoding_rs`] crate supported only subset of those encodings, only
/// supported subset are detected, which is UTF-8, UTF-16 BE and UTF-16 LE.
///
/// If encoding is detected, `Some` is returned, otherwise `None` is returned.
#[cfg(feature = "encoding")]
fn detect_encoding(bytes: &[u8]) -> Option<&'static Encoding> {
match bytes {
// with BOM
_ if bytes.starts_with(&[0xFE, 0xFF]) => Some(UTF_16BE),
_ if bytes.starts_with(&[0xFF, 0xFE]) => Some(UTF_16LE),
_ if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) => Some(UTF_8),
// without BOM
_ if bytes.starts_with(&[0x00, b'<', 0x00, b'?']) => Some(UTF_16BE), // Some BE encoding, for example, UTF-16 or ISO-10646-UCS-2
_ if bytes.starts_with(&[b'<', 0x00, b'?', 0x00]) => Some(UTF_16LE), // Some LE encoding, for example, UTF-16 or ISO-10646-UCS-2
_ if bytes.starts_with(&[b'<', b'?', b'x', b'm']) => Some(UTF_8), // Some ASCII compatible
_ => None,
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#[cfg(test)]
mod test {
macro_rules! check {
($(let mut $buf:ident = $init:expr;)?) => {
mod read_bytes_until {
use super::input_from_bytes;
// Use Bytes for printing bytes as strings for ASCII range
use crate::utils::Bytes;
use pretty_assertions::assert_eq;
/// Checks that search in the empty buffer returns `None`
#[test]
fn empty() {
$(let mut $buf = $init;)?
let mut position = 0;
let mut input = input_from_bytes(b"".as_ref());
// ^= 0
assert_eq!(
input
.read_bytes_until(b'*', $(&mut $buf, )? &mut position)
.unwrap()
.map(Bytes),
None
);
assert_eq!(position, 0);
}
/// Checks that search in the buffer non-existent value returns entire buffer
/// as a result and set `position` to `len()`
#[test]
fn non_existent() {
$(let mut $buf = $init;)?
let mut position = 0;
let mut input = input_from_bytes(b"abcdef".as_ref());
// ^= 6
assert_eq!(
input
.read_bytes_until(b'*', $(&mut $buf, )? &mut position)
.unwrap()
.map(Bytes),