Coverage Report

Created: 2024-07-06 06:44

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