serde_json/
read.rs

1use crate::error::{Error, ErrorCode, Result};
2use alloc::vec::Vec;
3use core::char;
4use core::cmp;
5use core::ops::Deref;
6use core::str;
7
8#[cfg(feature = "std")]
9use crate::io;
10#[cfg(feature = "std")]
11use crate::iter::LineColIterator;
12
13#[cfg(feature = "raw_value")]
14use crate::raw::BorrowedRawDeserializer;
15#[cfg(all(feature = "raw_value", feature = "std"))]
16use crate::raw::OwnedRawDeserializer;
17#[cfg(all(feature = "raw_value", feature = "std"))]
18use alloc::string::String;
19#[cfg(feature = "raw_value")]
20use serde::de::Visitor;
21
22/// Trait used by the deserializer for iterating over input. This is manually
23/// "specialized" for iterating over `&[u8]`. Once feature(specialization) is
24/// stable we can use actual specialization.
25///
26/// This trait is sealed and cannot be implemented for types outside of
27/// `serde_json`.
28pub trait Read<'de>: private::Sealed {
29    #[doc(hidden)]
30    fn next(&mut self) -> Result<Option<u8>>;
31    #[doc(hidden)]
32    fn peek(&mut self) -> Result<Option<u8>>;
33
34    /// Only valid after a call to peek(). Discards the peeked byte.
35    #[doc(hidden)]
36    fn discard(&mut self);
37
38    /// Position of the most recent call to next().
39    ///
40    /// The most recent call was probably next() and not peek(), but this method
41    /// should try to return a sensible result if the most recent call was
42    /// actually peek() because we don't always know.
43    ///
44    /// Only called in case of an error, so performance is not important.
45    #[doc(hidden)]
46    fn position(&self) -> Position;
47
48    /// Position of the most recent call to peek().
49    ///
50    /// The most recent call was probably peek() and not next(), but this method
51    /// should try to return a sensible result if the most recent call was
52    /// actually next() because we don't always know.
53    ///
54    /// Only called in case of an error, so performance is not important.
55    #[doc(hidden)]
56    fn peek_position(&self) -> Position;
57
58    /// Offset from the beginning of the input to the next byte that would be
59    /// returned by next() or peek().
60    #[doc(hidden)]
61    fn byte_offset(&self) -> usize;
62
63    /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
64    /// string until the next quotation mark using the given scratch space if
65    /// necessary. The scratch space is initially empty.
66    #[doc(hidden)]
67    fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>;
68
69    /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
70    /// string until the next quotation mark using the given scratch space if
71    /// necessary. The scratch space is initially empty.
72    ///
73    /// This function returns the raw bytes in the string with escape sequences
74    /// expanded but without performing unicode validation.
75    #[doc(hidden)]
76    fn parse_str_raw<'s>(
77        &'s mut self,
78        scratch: &'s mut Vec<u8>,
79    ) -> Result<Reference<'de, 's, [u8]>>;
80
81    /// Assumes the previous byte was a quotation mark. Parses a JSON-escaped
82    /// string until the next quotation mark but discards the data.
83    #[doc(hidden)]
84    fn ignore_str(&mut self) -> Result<()>;
85
86    /// Assumes the previous byte was a hex escape sequence ('\u') in a string.
87    /// Parses next hexadecimal sequence.
88    #[doc(hidden)]
89    fn decode_hex_escape(&mut self) -> Result<u16>;
90
91    /// Switch raw buffering mode on.
92    ///
93    /// This is used when deserializing `RawValue`.
94    #[cfg(feature = "raw_value")]
95    #[doc(hidden)]
96    fn begin_raw_buffering(&mut self);
97
98    /// Switch raw buffering mode off and provides the raw buffered data to the
99    /// given visitor.
100    #[cfg(feature = "raw_value")]
101    #[doc(hidden)]
102    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
103    where
104        V: Visitor<'de>;
105
106    /// Whether StreamDeserializer::next needs to check the failed flag. True
107    /// for IoRead, false for StrRead and SliceRead which can track failure by
108    /// truncating their input slice to avoid the extra check on every next
109    /// call.
110    #[doc(hidden)]
111    const should_early_return_if_failed: bool;
112
113    /// Mark a persistent failure of StreamDeserializer, either by setting the
114    /// flag or by truncating the input data.
115    #[doc(hidden)]
116    fn set_failed(&mut self, failed: &mut bool);
117}
118
119pub struct Position {
120    pub line: usize,
121    pub column: usize,
122}
123
124pub enum Reference<'b, 'c, T>
125where
126    T: ?Sized + 'static,
127{
128    Borrowed(&'b T),
129    Copied(&'c T),
130}
131
132impl<'b, 'c, T> Deref for Reference<'b, 'c, T>
133where
134    T: ?Sized + 'static,
135{
136    type Target = T;
137
138    fn deref(&self) -> &Self::Target {
139        match *self {
140            Reference::Borrowed(b) => b,
141            Reference::Copied(c) => c,
142        }
143    }
144}
145
146/// JSON input source that reads from a std::io input stream.
147#[cfg(feature = "std")]
148#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
149pub struct IoRead<R>
150where
151    R: io::Read,
152{
153    iter: LineColIterator<io::Bytes<R>>,
154    /// Temporary storage of peeked byte.
155    ch: Option<u8>,
156    #[cfg(feature = "raw_value")]
157    raw_buffer: Option<Vec<u8>>,
158}
159
160/// JSON input source that reads from a slice of bytes.
161//
162// This is more efficient than other iterators because peek() can be read-only
163// and we can compute line/col position only if an error happens.
164pub struct SliceRead<'a> {
165    slice: &'a [u8],
166    /// Index of the *next* byte that will be returned by next() or peek().
167    index: usize,
168    #[cfg(feature = "raw_value")]
169    raw_buffering_start_index: usize,
170}
171
172/// JSON input source that reads from a UTF-8 string.
173//
174// Able to elide UTF-8 checks by assuming that the input is valid UTF-8.
175pub struct StrRead<'a> {
176    delegate: SliceRead<'a>,
177    #[cfg(feature = "raw_value")]
178    data: &'a str,
179}
180
181// Prevent users from implementing the Read trait.
182mod private {
183    pub trait Sealed {}
184}
185
186//////////////////////////////////////////////////////////////////////////////
187
188#[cfg(feature = "std")]
189impl<R> IoRead<R>
190where
191    R: io::Read,
192{
193    /// Create a JSON input source to read from a std::io input stream.
194    pub fn new(reader: R) -> Self {
195        IoRead {
196            iter: LineColIterator::new(reader.bytes()),
197            ch: None,
198            #[cfg(feature = "raw_value")]
199            raw_buffer: None,
200        }
201    }
202}
203
204#[cfg(feature = "std")]
205impl<R> private::Sealed for IoRead<R> where R: io::Read {}
206
207#[cfg(feature = "std")]
208impl<R> IoRead<R>
209where
210    R: io::Read,
211{
212    fn parse_str_bytes<'s, T, F>(
213        &'s mut self,
214        scratch: &'s mut Vec<u8>,
215        validate: bool,
216        result: F,
217    ) -> Result<T>
218    where
219        T: 's,
220        F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
221    {
222        loop {
223            let ch = tri!(next_or_eof(self));
224            if !ESCAPE[ch as usize] {
225                scratch.push(ch);
226                continue;
227            }
228            match ch {
229                b'"' => {
230                    return result(self, scratch);
231                }
232                b'\\' => {
233                    tri!(parse_escape(self, validate, scratch));
234                }
235                _ => {
236                    if validate {
237                        return error(self, ErrorCode::ControlCharacterWhileParsingString);
238                    }
239                    scratch.push(ch);
240                }
241            }
242        }
243    }
244}
245
246#[cfg(feature = "std")]
247impl<'de, R> Read<'de> for IoRead<R>
248where
249    R: io::Read,
250{
251    #[inline]
252    fn next(&mut self) -> Result<Option<u8>> {
253        match self.ch.take() {
254            Some(ch) => {
255                #[cfg(feature = "raw_value")]
256                {
257                    if let Some(buf) = &mut self.raw_buffer {
258                        buf.push(ch);
259                    }
260                }
261                Ok(Some(ch))
262            }
263            None => match self.iter.next() {
264                Some(Err(err)) => Err(Error::io(err)),
265                Some(Ok(ch)) => {
266                    #[cfg(feature = "raw_value")]
267                    {
268                        if let Some(buf) = &mut self.raw_buffer {
269                            buf.push(ch);
270                        }
271                    }
272                    Ok(Some(ch))
273                }
274                None => Ok(None),
275            },
276        }
277    }
278
279    #[inline]
280    fn peek(&mut self) -> Result<Option<u8>> {
281        match self.ch {
282            Some(ch) => Ok(Some(ch)),
283            None => match self.iter.next() {
284                Some(Err(err)) => Err(Error::io(err)),
285                Some(Ok(ch)) => {
286                    self.ch = Some(ch);
287                    Ok(self.ch)
288                }
289                None => Ok(None),
290            },
291        }
292    }
293
294    #[cfg(not(feature = "raw_value"))]
295    #[inline]
296    fn discard(&mut self) {
297        self.ch = None;
298    }
299
300    #[cfg(feature = "raw_value")]
301    fn discard(&mut self) {
302        if let Some(ch) = self.ch.take() {
303            if let Some(buf) = &mut self.raw_buffer {
304                buf.push(ch);
305            }
306        }
307    }
308
309    fn position(&self) -> Position {
310        Position {
311            line: self.iter.line(),
312            column: self.iter.col(),
313        }
314    }
315
316    fn peek_position(&self) -> Position {
317        // The LineColIterator updates its position during peek() so it has the
318        // right one here.
319        self.position()
320    }
321
322    fn byte_offset(&self) -> usize {
323        match self.ch {
324            Some(_) => self.iter.byte_offset() - 1,
325            None => self.iter.byte_offset(),
326        }
327    }
328
329    fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
330        self.parse_str_bytes(scratch, true, as_str)
331            .map(Reference::Copied)
332    }
333
334    fn parse_str_raw<'s>(
335        &'s mut self,
336        scratch: &'s mut Vec<u8>,
337    ) -> Result<Reference<'de, 's, [u8]>> {
338        self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
339            .map(Reference::Copied)
340    }
341
342    fn ignore_str(&mut self) -> Result<()> {
343        loop {
344            let ch = tri!(next_or_eof(self));
345            if !ESCAPE[ch as usize] {
346                continue;
347            }
348            match ch {
349                b'"' => {
350                    return Ok(());
351                }
352                b'\\' => {
353                    tri!(ignore_escape(self));
354                }
355                _ => {
356                    return error(self, ErrorCode::ControlCharacterWhileParsingString);
357                }
358            }
359        }
360    }
361
362    fn decode_hex_escape(&mut self) -> Result<u16> {
363        let mut n = 0;
364        for _ in 0..4 {
365            match decode_hex_val(tri!(next_or_eof(self))) {
366                None => return error(self, ErrorCode::InvalidEscape),
367                Some(val) => {
368                    n = (n << 4) + val;
369                }
370            }
371        }
372        Ok(n)
373    }
374
375    #[cfg(feature = "raw_value")]
376    fn begin_raw_buffering(&mut self) {
377        self.raw_buffer = Some(Vec::new());
378    }
379
380    #[cfg(feature = "raw_value")]
381    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
382    where
383        V: Visitor<'de>,
384    {
385        let raw = self.raw_buffer.take().unwrap();
386        let raw = match String::from_utf8(raw) {
387            Ok(raw) => raw,
388            Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
389        };
390        visitor.visit_map(OwnedRawDeserializer {
391            raw_value: Some(raw),
392        })
393    }
394
395    const should_early_return_if_failed: bool = true;
396
397    #[inline]
398    #[cold]
399    fn set_failed(&mut self, failed: &mut bool) {
400        *failed = true;
401    }
402}
403
404//////////////////////////////////////////////////////////////////////////////
405
406impl<'a> SliceRead<'a> {
407    /// Create a JSON input source to read from a slice of bytes.
408    pub fn new(slice: &'a [u8]) -> Self {
409        SliceRead {
410            slice,
411            index: 0,
412            #[cfg(feature = "raw_value")]
413            raw_buffering_start_index: 0,
414        }
415    }
416
417    fn position_of_index(&self, i: usize) -> Position {
418        let start_of_line = match memchr::memrchr(b'\n', &self.slice[..i]) {
419            Some(position) => position + 1,
420            None => 0,
421        };
422        Position {
423            line: 1 + memchr::memchr_iter(b'\n', &self.slice[..start_of_line]).count(),
424            column: i - start_of_line,
425        }
426    }
427
428    /// The big optimization here over IoRead is that if the string contains no
429    /// backslash escape sequences, the returned &str is a slice of the raw JSON
430    /// data so we avoid copying into the scratch space.
431    fn parse_str_bytes<'s, T, F>(
432        &'s mut self,
433        scratch: &'s mut Vec<u8>,
434        validate: bool,
435        result: F,
436    ) -> Result<Reference<'a, 's, T>>
437    where
438        T: ?Sized + 's,
439        F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,
440    {
441        // Index of the first byte not yet copied into the scratch space.
442        let mut start = self.index;
443
444        loop {
445            while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
446                self.index += 1;
447            }
448            if self.index == self.slice.len() {
449                return error(self, ErrorCode::EofWhileParsingString);
450            }
451            match self.slice[self.index] {
452                b'"' => {
453                    if scratch.is_empty() {
454                        // Fast path: return a slice of the raw JSON without any
455                        // copying.
456                        let borrowed = &self.slice[start..self.index];
457                        self.index += 1;
458                        return result(self, borrowed).map(Reference::Borrowed);
459                    } else {
460                        scratch.extend_from_slice(&self.slice[start..self.index]);
461                        self.index += 1;
462                        return result(self, scratch).map(Reference::Copied);
463                    }
464                }
465                b'\\' => {
466                    scratch.extend_from_slice(&self.slice[start..self.index]);
467                    self.index += 1;
468                    tri!(parse_escape(self, validate, scratch));
469                    start = self.index;
470                }
471                _ => {
472                    self.index += 1;
473                    if validate {
474                        return error(self, ErrorCode::ControlCharacterWhileParsingString);
475                    }
476                }
477            }
478        }
479    }
480}
481
482impl<'a> private::Sealed for SliceRead<'a> {}
483
484impl<'a> Read<'a> for SliceRead<'a> {
485    #[inline]
486    fn next(&mut self) -> Result<Option<u8>> {
487        // `Ok(self.slice.get(self.index).map(|ch| { self.index += 1; *ch }))`
488        // is about 10% slower.
489        Ok(if self.index < self.slice.len() {
490            let ch = self.slice[self.index];
491            self.index += 1;
492            Some(ch)
493        } else {
494            None
495        })
496    }
497
498    #[inline]
499    fn peek(&mut self) -> Result<Option<u8>> {
500        // `Ok(self.slice.get(self.index).map(|ch| *ch))` is about 10% slower
501        // for some reason.
502        Ok(if self.index < self.slice.len() {
503            Some(self.slice[self.index])
504        } else {
505            None
506        })
507    }
508
509    #[inline]
510    fn discard(&mut self) {
511        self.index += 1;
512    }
513
514    fn position(&self) -> Position {
515        self.position_of_index(self.index)
516    }
517
518    fn peek_position(&self) -> Position {
519        // Cap it at slice.len() just in case the most recent call was next()
520        // and it returned the last byte.
521        self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
522    }
523
524    fn byte_offset(&self) -> usize {
525        self.index
526    }
527
528    fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
529        self.parse_str_bytes(scratch, true, as_str)
530    }
531
532    fn parse_str_raw<'s>(
533        &'s mut self,
534        scratch: &'s mut Vec<u8>,
535    ) -> Result<Reference<'a, 's, [u8]>> {
536        self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
537    }
538
539    fn ignore_str(&mut self) -> Result<()> {
540        loop {
541            while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
542                self.index += 1;
543            }
544            if self.index == self.slice.len() {
545                return error(self, ErrorCode::EofWhileParsingString);
546            }
547            match self.slice[self.index] {
548                b'"' => {
549                    self.index += 1;
550                    return Ok(());
551                }
552                b'\\' => {
553                    self.index += 1;
554                    tri!(ignore_escape(self));
555                }
556                _ => {
557                    return error(self, ErrorCode::ControlCharacterWhileParsingString);
558                }
559            }
560        }
561    }
562
563    fn decode_hex_escape(&mut self) -> Result<u16> {
564        if self.index + 4 > self.slice.len() {
565            self.index = self.slice.len();
566            return error(self, ErrorCode::EofWhileParsingString);
567        }
568
569        let mut n = 0;
570        for _ in 0..4 {
571            let ch = decode_hex_val(self.slice[self.index]);
572            self.index += 1;
573            match ch {
574                None => return error(self, ErrorCode::InvalidEscape),
575                Some(val) => {
576                    n = (n << 4) + val;
577                }
578            }
579        }
580        Ok(n)
581    }
582
583    #[cfg(feature = "raw_value")]
584    fn begin_raw_buffering(&mut self) {
585        self.raw_buffering_start_index = self.index;
586    }
587
588    #[cfg(feature = "raw_value")]
589    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
590    where
591        V: Visitor<'a>,
592    {
593        let raw = &self.slice[self.raw_buffering_start_index..self.index];
594        let raw = match str::from_utf8(raw) {
595            Ok(raw) => raw,
596            Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
597        };
598        visitor.visit_map(BorrowedRawDeserializer {
599            raw_value: Some(raw),
600        })
601    }
602
603    const should_early_return_if_failed: bool = false;
604
605    #[inline]
606    #[cold]
607    fn set_failed(&mut self, _failed: &mut bool) {
608        self.slice = &self.slice[..self.index];
609    }
610}
611
612//////////////////////////////////////////////////////////////////////////////
613
614impl<'a> StrRead<'a> {
615    /// Create a JSON input source to read from a UTF-8 string.
616    pub fn new(s: &'a str) -> Self {
617        StrRead {
618            delegate: SliceRead::new(s.as_bytes()),
619            #[cfg(feature = "raw_value")]
620            data: s,
621        }
622    }
623}
624
625impl<'a> private::Sealed for StrRead<'a> {}
626
627impl<'a> Read<'a> for StrRead<'a> {
628    #[inline]
629    fn next(&mut self) -> Result<Option<u8>> {
630        self.delegate.next()
631    }
632
633    #[inline]
634    fn peek(&mut self) -> Result<Option<u8>> {
635        self.delegate.peek()
636    }
637
638    #[inline]
639    fn discard(&mut self) {
640        self.delegate.discard();
641    }
642
643    fn position(&self) -> Position {
644        self.delegate.position()
645    }
646
647    fn peek_position(&self) -> Position {
648        self.delegate.peek_position()
649    }
650
651    fn byte_offset(&self) -> usize {
652        self.delegate.byte_offset()
653    }
654
655    fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
656        self.delegate.parse_str_bytes(scratch, true, |_, bytes| {
657            // The deserialization input came in as &str with a UTF-8 guarantee,
658            // and the \u-escapes are checked along the way, so don't need to
659            // check here.
660            Ok(unsafe { str::from_utf8_unchecked(bytes) })
661        })
662    }
663
664    fn parse_str_raw<'s>(
665        &'s mut self,
666        scratch: &'s mut Vec<u8>,
667    ) -> Result<Reference<'a, 's, [u8]>> {
668        self.delegate.parse_str_raw(scratch)
669    }
670
671    fn ignore_str(&mut self) -> Result<()> {
672        self.delegate.ignore_str()
673    }
674
675    fn decode_hex_escape(&mut self) -> Result<u16> {
676        self.delegate.decode_hex_escape()
677    }
678
679    #[cfg(feature = "raw_value")]
680    fn begin_raw_buffering(&mut self) {
681        self.delegate.begin_raw_buffering();
682    }
683
684    #[cfg(feature = "raw_value")]
685    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
686    where
687        V: Visitor<'a>,
688    {
689        let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
690        visitor.visit_map(BorrowedRawDeserializer {
691            raw_value: Some(raw),
692        })
693    }
694
695    const should_early_return_if_failed: bool = false;
696
697    #[inline]
698    #[cold]
699    fn set_failed(&mut self, failed: &mut bool) {
700        self.delegate.set_failed(failed);
701    }
702}
703
704//////////////////////////////////////////////////////////////////////////////
705
706impl<'a, 'de, R> private::Sealed for &'a mut R where R: Read<'de> {}
707
708impl<'a, 'de, R> Read<'de> for &'a mut R
709where
710    R: Read<'de>,
711{
712    fn next(&mut self) -> Result<Option<u8>> {
713        R::next(self)
714    }
715
716    fn peek(&mut self) -> Result<Option<u8>> {
717        R::peek(self)
718    }
719
720    fn discard(&mut self) {
721        R::discard(self);
722    }
723
724    fn position(&self) -> Position {
725        R::position(self)
726    }
727
728    fn peek_position(&self) -> Position {
729        R::peek_position(self)
730    }
731
732    fn byte_offset(&self) -> usize {
733        R::byte_offset(self)
734    }
735
736    fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
737        R::parse_str(self, scratch)
738    }
739
740    fn parse_str_raw<'s>(
741        &'s mut self,
742        scratch: &'s mut Vec<u8>,
743    ) -> Result<Reference<'de, 's, [u8]>> {
744        R::parse_str_raw(self, scratch)
745    }
746
747    fn ignore_str(&mut self) -> Result<()> {
748        R::ignore_str(self)
749    }
750
751    fn decode_hex_escape(&mut self) -> Result<u16> {
752        R::decode_hex_escape(self)
753    }
754
755    #[cfg(feature = "raw_value")]
756    fn begin_raw_buffering(&mut self) {
757        R::begin_raw_buffering(self);
758    }
759
760    #[cfg(feature = "raw_value")]
761    fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
762    where
763        V: Visitor<'de>,
764    {
765        R::end_raw_buffering(self, visitor)
766    }
767
768    const should_early_return_if_failed: bool = R::should_early_return_if_failed;
769
770    fn set_failed(&mut self, failed: &mut bool) {
771        R::set_failed(self, failed);
772    }
773}
774
775//////////////////////////////////////////////////////////////////////////////
776
777/// Marker for whether StreamDeserializer can implement FusedIterator.
778pub trait Fused: private::Sealed {}
779impl<'a> Fused for SliceRead<'a> {}
780impl<'a> Fused for StrRead<'a> {}
781
782// Lookup table of bytes that must be escaped. A value of true at index i means
783// that byte i requires an escape sequence in the input.
784static ESCAPE: [bool; 256] = {
785    const CT: bool = true; // control character \x00..=\x1F
786    const QU: bool = true; // quote \x22
787    const BS: bool = true; // backslash \x5C
788    const __: bool = false; // allow unescaped
789    [
790        //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
791        CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0
792        CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1
793        __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
794        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3
795        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4
796        __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5
797        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6
798        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
799        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
800        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
801        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
802        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
803        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
804        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
805        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
806        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
807    ]
808};
809
810fn next_or_eof<'de, R>(read: &mut R) -> Result<u8>
811where
812    R: ?Sized + Read<'de>,
813{
814    match tri!(read.next()) {
815        Some(b) => Ok(b),
816        None => error(read, ErrorCode::EofWhileParsingString),
817    }
818}
819
820fn peek_or_eof<'de, R>(read: &mut R) -> Result<u8>
821where
822    R: ?Sized + Read<'de>,
823{
824    match tri!(read.peek()) {
825        Some(b) => Ok(b),
826        None => error(read, ErrorCode::EofWhileParsingString),
827    }
828}
829
830fn error<'de, R, T>(read: &R, reason: ErrorCode) -> Result<T>
831where
832    R: ?Sized + Read<'de>,
833{
834    let position = read.position();
835    Err(Error::syntax(reason, position.line, position.column))
836}
837
838fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
839    str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
840}
841
842/// Parses a JSON escape sequence and appends it into the scratch space. Assumes
843/// the previous byte read was a backslash.
844fn parse_escape<'de, R: Read<'de>>(
845    read: &mut R,
846    validate: bool,
847    scratch: &mut Vec<u8>,
848) -> Result<()> {
849    let ch = tri!(next_or_eof(read));
850
851    match ch {
852        b'"' => scratch.push(b'"'),
853        b'\\' => scratch.push(b'\\'),
854        b'/' => scratch.push(b'/'),
855        b'b' => scratch.push(b'\x08'),
856        b'f' => scratch.push(b'\x0c'),
857        b'n' => scratch.push(b'\n'),
858        b'r' => scratch.push(b'\r'),
859        b't' => scratch.push(b'\t'),
860        b'u' => {
861            fn encode_surrogate(scratch: &mut Vec<u8>, n: u16) {
862                scratch.extend_from_slice(&[
863                    (n >> 12 & 0b0000_1111) as u8 | 0b1110_0000,
864                    (n >> 6 & 0b0011_1111) as u8 | 0b1000_0000,
865                    (n & 0b0011_1111) as u8 | 0b1000_0000,
866                ]);
867            }
868
869            let c = match tri!(read.decode_hex_escape()) {
870                n @ 0xDC00..=0xDFFF => {
871                    return if validate {
872                        error(read, ErrorCode::LoneLeadingSurrogateInHexEscape)
873                    } else {
874                        encode_surrogate(scratch, n);
875                        Ok(())
876                    };
877                }
878
879                // Non-BMP characters are encoded as a sequence of two hex
880                // escapes, representing UTF-16 surrogates. If deserializing a
881                // utf-8 string the surrogates are required to be paired,
882                // whereas deserializing a byte string accepts lone surrogates.
883                n1 @ 0xD800..=0xDBFF => {
884                    if tri!(peek_or_eof(read)) == b'\\' {
885                        read.discard();
886                    } else {
887                        return if validate {
888                            read.discard();
889                            error(read, ErrorCode::UnexpectedEndOfHexEscape)
890                        } else {
891                            encode_surrogate(scratch, n1);
892                            Ok(())
893                        };
894                    }
895
896                    if tri!(peek_or_eof(read)) == b'u' {
897                        read.discard();
898                    } else {
899                        return if validate {
900                            read.discard();
901                            error(read, ErrorCode::UnexpectedEndOfHexEscape)
902                        } else {
903                            encode_surrogate(scratch, n1);
904                            // The \ prior to this byte started an escape sequence,
905                            // so we need to parse that now. This recursive call
906                            // does not blow the stack on malicious input because
907                            // the escape is not \u, so it will be handled by one
908                            // of the easy nonrecursive cases.
909                            parse_escape(read, validate, scratch)
910                        };
911                    }
912
913                    let n2 = tri!(read.decode_hex_escape());
914
915                    if n2 < 0xDC00 || n2 > 0xDFFF {
916                        return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
917                    }
918
919                    let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
920
921                    match char::from_u32(n) {
922                        Some(c) => c,
923                        None => {
924                            return error(read, ErrorCode::InvalidUnicodeCodePoint);
925                        }
926                    }
927                }
928
929                // Every u16 outside of the surrogate ranges above is guaranteed
930                // to be a legal char.
931                n => char::from_u32(n as u32).unwrap(),
932            };
933
934            scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
935        }
936        _ => {
937            return error(read, ErrorCode::InvalidEscape);
938        }
939    }
940
941    Ok(())
942}
943
944/// Parses a JSON escape sequence and discards the value. Assumes the previous
945/// byte read was a backslash.
946fn ignore_escape<'de, R>(read: &mut R) -> Result<()>
947where
948    R: ?Sized + Read<'de>,
949{
950    let ch = tri!(next_or_eof(read));
951
952    match ch {
953        b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
954        b'u' => {
955            // At this point we don't care if the codepoint is valid. We just
956            // want to consume it. We don't actually know what is valid or not
957            // at this point, because that depends on if this string will
958            // ultimately be parsed into a string or a byte buffer in the "real"
959            // parse.
960
961            tri!(read.decode_hex_escape());
962        }
963        _ => {
964            return error(read, ErrorCode::InvalidEscape);
965        }
966    }
967
968    Ok(())
969}
970
971static HEX: [u8; 256] = {
972    const __: u8 = 255; // not a hex digit
973    [
974        //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
975        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 0
976        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 1
977        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2
978        00, 01, 02, 03, 04, 05, 06, 07, 08, 09, __, __, __, __, __, __, // 3
979        __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 4
980        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 5
981        __, 10, 11, 12, 13, 14, 15, __, __, __, __, __, __, __, __, __, // 6
982        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7
983        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8
984        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9
985        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A
986        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B
987        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C
988        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D
989        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E
990        __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F
991    ]
992};
993
994fn decode_hex_val(val: u8) -> Option<u16> {
995    let n = HEX[val as usize] as u16;
996    if n == 255 {
997        None
998    } else {
999        Some(n)
1000    }
1001}