quick_xml/encoding.rs
1//! A module for wrappers that encode / decode data.
2
3use std::borrow::Cow;
4use std::str::Utf8Error;
5
6#[cfg(feature = "encoding")]
7use encoding_rs::{DecoderResult, Encoding, UTF_16BE, UTF_16LE, UTF_8};
8
9/// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-8.
10/// See <https://unicode.org/faq/utf_bom.html#bom1>
11pub(crate) const UTF8_BOM: &[u8] = &[0xEF, 0xBB, 0xBF];
12/// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-16 with little-endian byte order.
13/// See <https://unicode.org/faq/utf_bom.html#bom1>
14#[cfg(feature = "encoding")]
15pub(crate) const UTF16_LE_BOM: &[u8] = &[0xFF, 0xFE];
16/// Unicode "byte order mark" (\u{FEFF}) encoded as UTF-16 with big-endian byte order.
17/// See <https://unicode.org/faq/utf_bom.html#bom1>
18#[cfg(feature = "encoding")]
19pub(crate) const UTF16_BE_BOM: &[u8] = &[0xFE, 0xFF];
20
21/// An error when decoding or encoding
22///
23/// If feature [`encoding`] is disabled, the [`EncodingError`] is always [`EncodingError::Utf8`]
24///
25/// [`encoding`]: ../index.html#encoding
26#[derive(Clone, Debug, PartialEq, Eq)]
27#[non_exhaustive]
28pub enum EncodingError {
29 /// Input was not valid UTF-8
30 Utf8(Utf8Error),
31 /// Input did not adhere to the given encoding
32 #[cfg(feature = "encoding")]
33 Other(&'static Encoding),
34}
35
36impl From<Utf8Error> for EncodingError {
37 #[inline]
38 fn from(e: Utf8Error) -> Self {
39 Self::Utf8(e)
40 }
41}
42
43impl std::error::Error for EncodingError {
44 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
45 match self {
46 Self::Utf8(e) => Some(e),
47 #[cfg(feature = "encoding")]
48 Self::Other(_) => None,
49 }
50 }
51}
52
53impl std::fmt::Display for EncodingError {
54 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55 match self {
56 Self::Utf8(e) => write!(f, "cannot decode input using UTF-8: {}", e),
57 #[cfg(feature = "encoding")]
58 Self::Other(encoding) => write!(f, "cannot decode input using {}", encoding.name()),
59 }
60 }
61}
62
63/// Decoder of byte slices into strings.
64///
65/// If feature [`encoding`] is enabled, this encoding taken from the `"encoding"`
66/// XML declaration or assumes UTF-8, if XML has no <?xml ?> declaration, encoding
67/// key is not defined or contains unknown encoding.
68///
69/// The library supports any UTF-8 compatible encodings that crate `encoding_rs`
70/// is supported. [*UTF-16 and ISO-2022-JP are not supported at the present*][utf16].
71///
72/// If feature [`encoding`] is disabled, the decoder is always UTF-8 decoder:
73/// any XML declarations are ignored.
74///
75/// [utf16]: https://github.com/tafia/quick-xml/issues/158
76/// [`encoding`]: ../index.html#encoding
77#[derive(Clone, Copy, Debug, Eq, PartialEq)]
78pub struct Decoder {
79 #[cfg(feature = "encoding")]
80 pub(crate) encoding: &'static Encoding,
81}
82
83impl Decoder {
84 pub(crate) fn utf8() -> Self {
85 Decoder {
86 #[cfg(feature = "encoding")]
87 encoding: UTF_8,
88 }
89 }
90
91 #[cfg(all(test, feature = "encoding", feature = "serialize"))]
92 pub(crate) fn utf16() -> Self {
93 Decoder { encoding: UTF_16LE }
94 }
95}
96
97impl Decoder {
98 /// Returns the `Reader`s encoding.
99 ///
100 /// This encoding will be used by [`decode`].
101 ///
102 /// [`decode`]: Self::decode
103 #[cfg(feature = "encoding")]
104 pub const fn encoding(&self) -> &'static Encoding {
105 self.encoding
106 }
107
108 /// ## Without `encoding` feature
109 ///
110 /// Decodes an UTF-8 slice regardless of XML declaration and ignoring BOM
111 /// if it is present in the `bytes`.
112 ///
113 /// ## With `encoding` feature
114 ///
115 /// Decodes specified bytes using encoding, declared in the XML, if it was
116 /// declared there, or UTF-8 otherwise, and ignoring BOM if it is present
117 /// in the `bytes`.
118 ///
119 /// ----
120 /// Returns an error in case of malformed sequences in the `bytes`.
121 pub fn decode<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>, EncodingError> {
122 #[cfg(not(feature = "encoding"))]
123 let decoded = Ok(Cow::Borrowed(std::str::from_utf8(bytes)?));
124
125 #[cfg(feature = "encoding")]
126 let decoded = decode(bytes, self.encoding);
127
128 decoded
129 }
130
131 /// Like [`decode`][Self::decode] but using a pre-allocated buffer.
132 pub fn decode_into(&self, bytes: &[u8], buf: &mut String) -> Result<(), EncodingError> {
133 #[cfg(not(feature = "encoding"))]
134 buf.push_str(std::str::from_utf8(bytes)?);
135
136 #[cfg(feature = "encoding")]
137 decode_into(bytes, self.encoding, buf)?;
138
139 Ok(())
140 }
141
142 /// Decodes the `Cow` buffer, preserves the lifetime
143 pub(crate) fn decode_cow<'b>(
144 &self,
145 bytes: &Cow<'b, [u8]>,
146 ) -> Result<Cow<'b, str>, EncodingError> {
147 match bytes {
148 Cow::Borrowed(bytes) => self.decode(bytes),
149 // Convert to owned, because otherwise Cow will be bound with wrong lifetime
150 Cow::Owned(bytes) => Ok(self.decode(bytes)?.into_owned().into()),
151 }
152 }
153}
154
155/// Decodes the provided bytes using the specified encoding.
156///
157/// Returns an error in case of malformed or non-representable sequences in the `bytes`.
158#[cfg(feature = "encoding")]
159pub fn decode<'b>(
160 bytes: &'b [u8],
161 encoding: &'static Encoding,
162) -> Result<Cow<'b, str>, EncodingError> {
163 encoding
164 .decode_without_bom_handling_and_without_replacement(bytes)
165 .ok_or(EncodingError::Other(encoding))
166}
167
168/// Like [`decode`] but using a pre-allocated buffer.
169#[cfg(feature = "encoding")]
170pub fn decode_into(
171 bytes: &[u8],
172 encoding: &'static Encoding,
173 buf: &mut String,
174) -> Result<(), EncodingError> {
175 if encoding == UTF_8 {
176 buf.push_str(std::str::from_utf8(bytes)?);
177 return Ok(());
178 }
179
180 let mut decoder = encoding.new_decoder_without_bom_handling();
181 buf.reserve(
182 decoder
183 .max_utf8_buffer_length_without_replacement(bytes.len())
184 // SAFETY: None can be returned only if required size will overflow usize,
185 // but in that case String::reserve also panics
186 .unwrap(),
187 );
188 let (result, read) = decoder.decode_to_string_without_replacement(bytes, buf, true);
189 match result {
190 DecoderResult::InputEmpty => {
191 debug_assert_eq!(read, bytes.len());
192 Ok(())
193 }
194 DecoderResult::Malformed(_, _) => Err(EncodingError::Other(encoding)),
195 // SAFETY: We allocate enough space above
196 DecoderResult::OutputFull => unreachable!(),
197 }
198}
199
200/// Automatic encoding detection of XML files based using the
201/// [recommended algorithm](https://www.w3.org/TR/xml11/#sec-guessing).
202///
203/// If encoding is detected, `Some` is returned with an encoding and size of BOM
204/// in bytes, if detection was performed using BOM, or zero, if detection was
205/// performed without BOM.
206///
207/// IF encoding was not recognized, `None` is returned.
208///
209/// Because the [`encoding_rs`] crate supports only subset of those encodings, only
210/// the supported subset are detected, which is UTF-8, UTF-16 BE and UTF-16 LE.
211///
212/// The algorithm suggests examine up to the first 4 bytes to determine encoding
213/// according to the following table:
214///
215/// | Bytes |Detected encoding
216/// |-------------|------------------------------------------
217/// | **BOM**
218/// |`FE_FF_##_##`|UTF-16, big-endian
219/// |`FF FE ## ##`|UTF-16, little-endian
220/// |`EF BB BF` |UTF-8
221/// | **No BOM**
222/// |`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)
223/// |`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)
224/// |`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
225#[cfg(feature = "encoding")]
226pub fn detect_encoding(bytes: &[u8]) -> Option<(&'static Encoding, usize)> {
227 match bytes {
228 // with BOM
229 _ if bytes.starts_with(UTF16_BE_BOM) => Some((UTF_16BE, 2)),
230 _ if bytes.starts_with(UTF16_LE_BOM) => Some((UTF_16LE, 2)),
231 _ if bytes.starts_with(UTF8_BOM) => Some((UTF_8, 3)),
232
233 // without BOM
234 _ if bytes.starts_with(&[0x00, b'<', 0x00, b'?']) => Some((UTF_16BE, 0)), // Some BE encoding, for example, UTF-16 or ISO-10646-UCS-2
235 _ if bytes.starts_with(&[b'<', 0x00, b'?', 0x00]) => Some((UTF_16LE, 0)), // Some LE encoding, for example, UTF-16 or ISO-10646-UCS-2
236 _ if bytes.starts_with(&[b'<', b'?', b'x', b'm']) => Some((UTF_8, 0)), // Some ASCII compatible
237
238 _ => None,
239 }
240}