1 // Copyright 2017 Serde Developers
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8 
9 use std::ops::Deref;
10 use std::{char, cmp, io, str};
11 
12 #[cfg(feature = "raw_value")]
13 use serde::de::Visitor;
14 
15 use iter::LineColIterator;
16 
17 use error::{Error, ErrorCode, Result};
18 
19 #[cfg(feature = "raw_value")]
20 use raw::{BorrowedRawDeserializer, OwnedRawDeserializer};
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)]
next(&mut self) -> Result<Option<u8>>30     fn next(&mut self) -> Result<Option<u8>>;
31     #[doc(hidden)]
peek(&mut self) -> Result<Option<u8>>32     fn peek(&mut self) -> Result<Option<u8>>;
33 
34     /// Only valid after a call to peek(). Discards the peeked byte.
35     #[doc(hidden)]
discard(&mut self)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)]
position(&self) -> Position46     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)]
peek_position(&self) -> Position56     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)]
byte_offset(&self) -> usize61     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)]
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>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)]
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'de, 's, [u8]>>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)]
ignore_str(&mut self) -> Result<()>84     fn ignore_str(&mut self) -> Result<()>;
85 
86     /// Assumes the previous byte was a hex escape sequnce ('\u') in a string.
87     /// Parses next hexadecimal sequence.
88     #[doc(hidden)]
decode_hex_escape(&mut self) -> Result<u16>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)]
begin_raw_buffering(&mut self)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)]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'de>102     fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
103     where
104         V: Visitor<'de>;
105 }
106 
107 pub struct Position {
108     pub line: usize,
109     pub column: usize,
110 }
111 
112 pub enum Reference<'b, 'c, T: ?Sized + 'static> {
113     Borrowed(&'b T),
114     Copied(&'c T),
115 }
116 
117 impl<'b, 'c, T: ?Sized + 'static> Deref for Reference<'b, 'c, T> {
118     type Target = T;
119 
deref(&self) -> &Self::Target120     fn deref(&self) -> &Self::Target {
121         match *self {
122             Reference::Borrowed(b) => b,
123             Reference::Copied(c) => c,
124         }
125     }
126 }
127 
128 /// JSON input source that reads from a std::io input stream.
129 pub struct IoRead<R>
130 where
131     R: io::Read,
132 {
133     iter: LineColIterator<io::Bytes<R>>,
134     /// Temporary storage of peeked byte.
135     ch: Option<u8>,
136     #[cfg(feature = "raw_value")]
137     raw_buffer: Option<Vec<u8>>,
138 }
139 
140 /// JSON input source that reads from a slice of bytes.
141 //
142 // This is more efficient than other iterators because peek() can be read-only
143 // and we can compute line/col position only if an error happens.
144 pub struct SliceRead<'a> {
145     slice: &'a [u8],
146     /// Index of the *next* byte that will be returned by next() or peek().
147     index: usize,
148     #[cfg(feature = "raw_value")]
149     raw_buffering_start_index: usize,
150 }
151 
152 /// JSON input source that reads from a UTF-8 string.
153 //
154 // Able to elide UTF-8 checks by assuming that the input is valid UTF-8.
155 pub struct StrRead<'a> {
156     delegate: SliceRead<'a>,
157     #[cfg(feature = "raw_value")]
158     data: &'a str,
159 }
160 
161 // Prevent users from implementing the Read trait.
162 mod private {
163     pub trait Sealed {}
164 }
165 
166 //////////////////////////////////////////////////////////////////////////////
167 
168 impl<R> IoRead<R>
169 where
170     R: io::Read,
171 {
172     /// Create a JSON input source to read from a std::io input stream.
new(reader: R) -> Self173     pub fn new(reader: R) -> Self {
174         #[cfg(not(feature = "raw_value"))]
175         {
176             IoRead {
177                 iter: LineColIterator::new(reader.bytes()),
178                 ch: None,
179             }
180         }
181         #[cfg(feature = "raw_value")]
182         {
183             IoRead {
184                 iter: LineColIterator::new(reader.bytes()),
185                 ch: None,
186                 raw_buffer: None,
187             }
188         }
189     }
190 }
191 
192 impl<R> private::Sealed for IoRead<R> where R: io::Read {}
193 
194 impl<R> IoRead<R>
195 where
196     R: io::Read,
197 {
parse_str_bytes<'s, T, F>( &'s mut self, scratch: &'s mut Vec<u8>, validate: bool, result: F, ) -> Result<T> where T: 's, F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,198     fn parse_str_bytes<'s, T, F>(
199         &'s mut self,
200         scratch: &'s mut Vec<u8>,
201         validate: bool,
202         result: F,
203     ) -> Result<T>
204     where
205         T: 's,
206         F: FnOnce(&'s Self, &'s [u8]) -> Result<T>,
207     {
208         loop {
209             let ch = try!(next_or_eof(self));
210             if !ESCAPE[ch as usize] {
211                 scratch.push(ch);
212                 continue;
213             }
214             match ch {
215                 b'"' => {
216                     return result(self, scratch);
217                 }
218                 b'\\' => {
219                     try!(parse_escape(self, scratch));
220                 }
221                 _ => {
222                     if validate {
223                         return error(self, ErrorCode::ControlCharacterWhileParsingString);
224                     }
225                     scratch.push(ch);
226                 }
227             }
228         }
229     }
230 }
231 
232 impl<'de, R> Read<'de> for IoRead<R>
233 where
234     R: io::Read,
235 {
236     #[inline]
next(&mut self) -> Result<Option<u8>>237     fn next(&mut self) -> Result<Option<u8>> {
238         match self.ch.take() {
239             Some(ch) => {
240                 #[cfg(feature = "raw_value")]
241                 {
242                     if let Some(ref mut buf) = self.raw_buffer {
243                         buf.push(ch);
244                     }
245                 }
246                 Ok(Some(ch))
247             }
248             None => match self.iter.next() {
249                 Some(Err(err)) => Err(Error::io(err)),
250                 Some(Ok(ch)) => {
251                     #[cfg(feature = "raw_value")]
252                     {
253                         if let Some(ref mut buf) = self.raw_buffer {
254                             buf.push(ch);
255                         }
256                     }
257                     Ok(Some(ch))
258                 }
259                 None => Ok(None),
260             },
261         }
262     }
263 
264     #[inline]
peek(&mut self) -> Result<Option<u8>>265     fn peek(&mut self) -> Result<Option<u8>> {
266         match self.ch {
267             Some(ch) => Ok(Some(ch)),
268             None => match self.iter.next() {
269                 Some(Err(err)) => Err(Error::io(err)),
270                 Some(Ok(ch)) => {
271                     self.ch = Some(ch);
272                     Ok(self.ch)
273                 }
274                 None => Ok(None),
275             },
276         }
277     }
278 
279     #[cfg(not(feature = "raw_value"))]
280     #[inline]
discard(&mut self)281     fn discard(&mut self) {
282         self.ch = None;
283     }
284 
285     #[cfg(feature = "raw_value")]
discard(&mut self)286     fn discard(&mut self) {
287         if let Some(ch) = self.ch.take() {
288             if let Some(ref mut buf) = self.raw_buffer {
289                 buf.push(ch);
290             }
291         }
292     }
293 
position(&self) -> Position294     fn position(&self) -> Position {
295         Position {
296             line: self.iter.line(),
297             column: self.iter.col(),
298         }
299     }
300 
peek_position(&self) -> Position301     fn peek_position(&self) -> Position {
302         // The LineColIterator updates its position during peek() so it has the
303         // right one here.
304         self.position()
305     }
306 
byte_offset(&self) -> usize307     fn byte_offset(&self) -> usize {
308         match self.ch {
309             Some(_) => self.iter.byte_offset() - 1,
310             None => self.iter.byte_offset(),
311         }
312     }
313 
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>>314     fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
315         self.parse_str_bytes(scratch, true, as_str)
316             .map(Reference::Copied)
317     }
318 
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'de, 's, [u8]>>319     fn parse_str_raw<'s>(
320         &'s mut self,
321         scratch: &'s mut Vec<u8>,
322     ) -> Result<Reference<'de, 's, [u8]>> {
323         self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
324             .map(Reference::Copied)
325     }
326 
ignore_str(&mut self) -> Result<()>327     fn ignore_str(&mut self) -> Result<()> {
328         loop {
329             let ch = try!(next_or_eof(self));
330             if !ESCAPE[ch as usize] {
331                 continue;
332             }
333             match ch {
334                 b'"' => {
335                     return Ok(());
336                 }
337                 b'\\' => {
338                     try!(ignore_escape(self));
339                 }
340                 _ => {
341                     return error(self, ErrorCode::ControlCharacterWhileParsingString);
342                 }
343             }
344         }
345     }
346 
decode_hex_escape(&mut self) -> Result<u16>347     fn decode_hex_escape(&mut self) -> Result<u16> {
348         let mut n = 0;
349         for _ in 0..4 {
350             match decode_hex_val(try!(next_or_eof(self))) {
351                 None => return error(self, ErrorCode::InvalidEscape),
352                 Some(val) => {
353                     n = (n << 4) + val;
354                 }
355             }
356         }
357         Ok(n)
358     }
359 
360     #[cfg(feature = "raw_value")]
begin_raw_buffering(&mut self)361     fn begin_raw_buffering(&mut self) {
362         self.raw_buffer = Some(Vec::new());
363     }
364 
365     #[cfg(feature = "raw_value")]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'de>,366     fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
367     where
368         V: Visitor<'de>,
369     {
370         let raw = self.raw_buffer.take().unwrap();
371         let raw = String::from_utf8(raw).unwrap();
372         visitor.visit_map(OwnedRawDeserializer {
373             raw_value: Some(raw),
374         })
375     }
376 }
377 
378 //////////////////////////////////////////////////////////////////////////////
379 
380 impl<'a> SliceRead<'a> {
381     /// Create a JSON input source to read from a slice of bytes.
new(slice: &'a [u8]) -> Self382     pub fn new(slice: &'a [u8]) -> Self {
383         #[cfg(not(feature = "raw_value"))]
384         {
385             SliceRead {
386                 slice: slice,
387                 index: 0,
388             }
389         }
390         #[cfg(feature = "raw_value")]
391         {
392             SliceRead {
393                 slice: slice,
394                 index: 0,
395                 raw_buffering_start_index: 0,
396             }
397         }
398     }
399 
position_of_index(&self, i: usize) -> Position400     fn position_of_index(&self, i: usize) -> Position {
401         let mut position = Position { line: 1, column: 0 };
402         for ch in &self.slice[..i] {
403             match *ch {
404                 b'\n' => {
405                     position.line += 1;
406                     position.column = 0;
407                 }
408                 _ => {
409                     position.column += 1;
410                 }
411             }
412         }
413         position
414     }
415 
416     /// The big optimization here over IoRead is that if the string contains no
417     /// backslash escape sequences, the returned &str is a slice of the raw JSON
418     /// data so we avoid copying into the scratch space.
parse_str_bytes<'s, T: ?Sized, F>( &'s mut self, scratch: &'s mut Vec<u8>, validate: bool, result: F, ) -> Result<Reference<'a, 's, T>> where T: 's, F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,419     fn parse_str_bytes<'s, T: ?Sized, F>(
420         &'s mut self,
421         scratch: &'s mut Vec<u8>,
422         validate: bool,
423         result: F,
424     ) -> Result<Reference<'a, 's, T>>
425     where
426         T: 's,
427         F: for<'f> FnOnce(&'s Self, &'f [u8]) -> Result<&'f T>,
428     {
429         // Index of the first byte not yet copied into the scratch space.
430         let mut start = self.index;
431 
432         loop {
433             while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
434                 self.index += 1;
435             }
436             if self.index == self.slice.len() {
437                 return error(self, ErrorCode::EofWhileParsingString);
438             }
439             match self.slice[self.index] {
440                 b'"' => {
441                     if scratch.is_empty() {
442                         // Fast path: return a slice of the raw JSON without any
443                         // copying.
444                         let borrowed = &self.slice[start..self.index];
445                         self.index += 1;
446                         return result(self, borrowed).map(Reference::Borrowed);
447                     } else {
448                         scratch.extend_from_slice(&self.slice[start..self.index]);
449                         self.index += 1;
450                         return result(self, scratch).map(Reference::Copied);
451                     }
452                 }
453                 b'\\' => {
454                     scratch.extend_from_slice(&self.slice[start..self.index]);
455                     self.index += 1;
456                     try!(parse_escape(self, scratch));
457                     start = self.index;
458                 }
459                 _ => {
460                     self.index += 1;
461                     if validate {
462                         return error(self, ErrorCode::ControlCharacterWhileParsingString);
463                     }
464                 }
465             }
466         }
467     }
468 }
469 
470 impl<'a> private::Sealed for SliceRead<'a> {}
471 
472 impl<'a> Read<'a> for SliceRead<'a> {
473     #[inline]
next(&mut self) -> Result<Option<u8>>474     fn next(&mut self) -> Result<Option<u8>> {
475         // `Ok(self.slice.get(self.index).map(|ch| { self.index += 1; *ch }))`
476         // is about 10% slower.
477         Ok(if self.index < self.slice.len() {
478             let ch = self.slice[self.index];
479             self.index += 1;
480             Some(ch)
481         } else {
482             None
483         })
484     }
485 
486     #[inline]
peek(&mut self) -> Result<Option<u8>>487     fn peek(&mut self) -> Result<Option<u8>> {
488         // `Ok(self.slice.get(self.index).map(|ch| *ch))` is about 10% slower
489         // for some reason.
490         Ok(if self.index < self.slice.len() {
491             Some(self.slice[self.index])
492         } else {
493             None
494         })
495     }
496 
497     #[inline]
discard(&mut self)498     fn discard(&mut self) {
499         self.index += 1;
500     }
501 
position(&self) -> Position502     fn position(&self) -> Position {
503         self.position_of_index(self.index)
504     }
505 
peek_position(&self) -> Position506     fn peek_position(&self) -> Position {
507         // Cap it at slice.len() just in case the most recent call was next()
508         // and it returned the last byte.
509         self.position_of_index(cmp::min(self.slice.len(), self.index + 1))
510     }
511 
byte_offset(&self) -> usize512     fn byte_offset(&self) -> usize {
513         self.index
514     }
515 
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>>516     fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
517         self.parse_str_bytes(scratch, true, as_str)
518     }
519 
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'a, 's, [u8]>>520     fn parse_str_raw<'s>(
521         &'s mut self,
522         scratch: &'s mut Vec<u8>,
523     ) -> Result<Reference<'a, 's, [u8]>> {
524         self.parse_str_bytes(scratch, false, |_, bytes| Ok(bytes))
525     }
526 
ignore_str(&mut self) -> Result<()>527     fn ignore_str(&mut self) -> Result<()> {
528         loop {
529             while self.index < self.slice.len() && !ESCAPE[self.slice[self.index] as usize] {
530                 self.index += 1;
531             }
532             if self.index == self.slice.len() {
533                 return error(self, ErrorCode::EofWhileParsingString);
534             }
535             match self.slice[self.index] {
536                 b'"' => {
537                     self.index += 1;
538                     return Ok(());
539                 }
540                 b'\\' => {
541                     self.index += 1;
542                     try!(ignore_escape(self));
543                 }
544                 _ => {
545                     return error(self, ErrorCode::ControlCharacterWhileParsingString);
546                 }
547             }
548         }
549     }
550 
decode_hex_escape(&mut self) -> Result<u16>551     fn decode_hex_escape(&mut self) -> Result<u16> {
552         if self.index + 4 > self.slice.len() {
553             self.index = self.slice.len();
554             return error(self, ErrorCode::EofWhileParsingString);
555         }
556 
557         let mut n = 0;
558         for _ in 0..4 {
559             let ch = decode_hex_val(self.slice[self.index]);
560             self.index += 1;
561             match ch {
562                 None => return error(self, ErrorCode::InvalidEscape),
563                 Some(val) => {
564                     n = (n << 4) + val;
565                 }
566             }
567         }
568         Ok(n)
569     }
570 
571     #[cfg(feature = "raw_value")]
begin_raw_buffering(&mut self)572     fn begin_raw_buffering(&mut self) {
573         self.raw_buffering_start_index = self.index;
574     }
575 
576     #[cfg(feature = "raw_value")]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'a>,577     fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
578     where
579         V: Visitor<'a>,
580     {
581         let raw = &self.slice[self.raw_buffering_start_index..self.index];
582         let raw = str::from_utf8(raw).unwrap();
583         visitor.visit_map(BorrowedRawDeserializer {
584             raw_value: Some(raw),
585         })
586     }
587 }
588 
589 //////////////////////////////////////////////////////////////////////////////
590 
591 impl<'a> StrRead<'a> {
592     /// Create a JSON input source to read from a UTF-8 string.
new(s: &'a str) -> Self593     pub fn new(s: &'a str) -> Self {
594         #[cfg(not(feature = "raw_value"))]
595         {
596             StrRead {
597                 delegate: SliceRead::new(s.as_bytes()),
598             }
599         }
600         #[cfg(feature = "raw_value")]
601         {
602             StrRead {
603                 delegate: SliceRead::new(s.as_bytes()),
604                 data: s,
605             }
606         }
607     }
608 }
609 
610 impl<'a> private::Sealed for StrRead<'a> {}
611 
612 impl<'a> Read<'a> for StrRead<'a> {
613     #[inline]
next(&mut self) -> Result<Option<u8>>614     fn next(&mut self) -> Result<Option<u8>> {
615         self.delegate.next()
616     }
617 
618     #[inline]
peek(&mut self) -> Result<Option<u8>>619     fn peek(&mut self) -> Result<Option<u8>> {
620         self.delegate.peek()
621     }
622 
623     #[inline]
discard(&mut self)624     fn discard(&mut self) {
625         self.delegate.discard();
626     }
627 
position(&self) -> Position628     fn position(&self) -> Position {
629         self.delegate.position()
630     }
631 
peek_position(&self) -> Position632     fn peek_position(&self) -> Position {
633         self.delegate.peek_position()
634     }
635 
byte_offset(&self) -> usize636     fn byte_offset(&self) -> usize {
637         self.delegate.byte_offset()
638     }
639 
parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>>640     fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'a, 's, str>> {
641         self.delegate.parse_str_bytes(scratch, true, |_, bytes| {
642             // The input is assumed to be valid UTF-8 and the \u-escapes are
643             // checked along the way, so don't need to check here.
644             Ok(unsafe { str::from_utf8_unchecked(bytes) })
645         })
646     }
647 
parse_str_raw<'s>( &'s mut self, scratch: &'s mut Vec<u8>, ) -> Result<Reference<'a, 's, [u8]>>648     fn parse_str_raw<'s>(
649         &'s mut self,
650         scratch: &'s mut Vec<u8>,
651     ) -> Result<Reference<'a, 's, [u8]>> {
652         self.delegate.parse_str_raw(scratch)
653     }
654 
ignore_str(&mut self) -> Result<()>655     fn ignore_str(&mut self) -> Result<()> {
656         self.delegate.ignore_str()
657     }
658 
decode_hex_escape(&mut self) -> Result<u16>659     fn decode_hex_escape(&mut self) -> Result<u16> {
660         self.delegate.decode_hex_escape()
661     }
662 
663     #[cfg(feature = "raw_value")]
begin_raw_buffering(&mut self)664     fn begin_raw_buffering(&mut self) {
665         self.delegate.begin_raw_buffering()
666     }
667 
668     #[cfg(feature = "raw_value")]
end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value> where V: Visitor<'a>,669     fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
670     where
671         V: Visitor<'a>,
672     {
673         let raw = &self.data[self.delegate.raw_buffering_start_index..self.delegate.index];
674         visitor.visit_map(BorrowedRawDeserializer {
675             raw_value: Some(raw),
676         })
677     }
678 }
679 
680 //////////////////////////////////////////////////////////////////////////////
681 
682 const CT: bool = true; // control character \x00...\x1F
683 const QU: bool = true; // quote \x22
684 const BS: bool = true; // backslash \x5C
685 const O: bool = false; // allow unescaped
686 
687 // Lookup table of bytes that must be escaped. A value of true at index i means
688 // that byte i requires an escape sequence in the input.
689 #[cfg_attr(rustfmt, rustfmt_skip)]
690 static ESCAPE: [bool; 256] = [
691     //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
692     CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 0
693     CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, CT, // 1
694      O,  O, QU,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // 2
695      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // 3
696      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // 4
697      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, BS,  O,  O,  O, // 5
698      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // 6
699      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // 7
700      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // 8
701      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // 9
702      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // A
703      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // B
704      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // C
705      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // D
706      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // E
707      O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O,  O, // F
708 ];
709 
next_or_eof<'de, R: ?Sized + Read<'de>>(read: &mut R) -> Result<u8>710 fn next_or_eof<'de, R: ?Sized + Read<'de>>(read: &mut R) -> Result<u8> {
711     match try!(read.next()) {
712         Some(b) => Ok(b),
713         None => error(read, ErrorCode::EofWhileParsingString),
714     }
715 }
716 
error<'de, R: ?Sized + Read<'de>, T>(read: &R, reason: ErrorCode) -> Result<T>717 fn error<'de, R: ?Sized + Read<'de>, T>(read: &R, reason: ErrorCode) -> Result<T> {
718     let position = read.position();
719     Err(Error::syntax(reason, position.line, position.column))
720 }
721 
as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str>722 fn as_str<'de, 's, R: Read<'de>>(read: &R, slice: &'s [u8]) -> Result<&'s str> {
723     str::from_utf8(slice).or_else(|_| error(read, ErrorCode::InvalidUnicodeCodePoint))
724 }
725 
726 /// Parses a JSON escape sequence and appends it into the scratch space. Assumes
727 /// the previous byte read was a backslash.
parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Result<()>728 fn parse_escape<'de, R: Read<'de>>(read: &mut R, scratch: &mut Vec<u8>) -> Result<()> {
729     let ch = try!(next_or_eof(read));
730 
731     match ch {
732         b'"' => scratch.push(b'"'),
733         b'\\' => scratch.push(b'\\'),
734         b'/' => scratch.push(b'/'),
735         b'b' => scratch.push(b'\x08'),
736         b'f' => scratch.push(b'\x0c'),
737         b'n' => scratch.push(b'\n'),
738         b'r' => scratch.push(b'\r'),
739         b't' => scratch.push(b'\t'),
740         b'u' => {
741             let c = match try!(read.decode_hex_escape()) {
742                 0xDC00...0xDFFF => {
743                     return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
744                 }
745 
746                 // Non-BMP characters are encoded as a sequence of
747                 // two hex escapes, representing UTF-16 surrogates.
748                 n1 @ 0xD800...0xDBFF => {
749                     if try!(next_or_eof(read)) != b'\\' {
750                         return error(read, ErrorCode::UnexpectedEndOfHexEscape);
751                     }
752                     if try!(next_or_eof(read)) != b'u' {
753                         return error(read, ErrorCode::UnexpectedEndOfHexEscape);
754                     }
755 
756                     let n2 = try!(read.decode_hex_escape());
757 
758                     if n2 < 0xDC00 || n2 > 0xDFFF {
759                         return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
760                     }
761 
762                     let n = (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000;
763 
764                     match char::from_u32(n) {
765                         Some(c) => c,
766                         None => {
767                             return error(read, ErrorCode::InvalidUnicodeCodePoint);
768                         }
769                     }
770                 }
771 
772                 n => match char::from_u32(n as u32) {
773                     Some(c) => c,
774                     None => {
775                         return error(read, ErrorCode::InvalidUnicodeCodePoint);
776                     }
777                 },
778             };
779 
780             scratch.extend_from_slice(c.encode_utf8(&mut [0_u8; 4]).as_bytes());
781         }
782         _ => {
783             return error(read, ErrorCode::InvalidEscape);
784         }
785     }
786 
787     Ok(())
788 }
789 
790 /// Parses a JSON escape sequence and discards the value. Assumes the previous
791 /// byte read was a backslash.
ignore_escape<'de, R: ?Sized + Read<'de>>(read: &mut R) -> Result<()>792 fn ignore_escape<'de, R: ?Sized + Read<'de>>(read: &mut R) -> Result<()> {
793     let ch = try!(next_or_eof(read));
794 
795     match ch {
796         b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {}
797         b'u' => {
798             let n = match try!(read.decode_hex_escape()) {
799                 0xDC00...0xDFFF => {
800                     return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
801                 }
802 
803                 // Non-BMP characters are encoded as a sequence of
804                 // two hex escapes, representing UTF-16 surrogates.
805                 n1 @ 0xD800...0xDBFF => {
806                     if try!(next_or_eof(read)) != b'\\' {
807                         return error(read, ErrorCode::UnexpectedEndOfHexEscape);
808                     }
809                     if try!(next_or_eof(read)) != b'u' {
810                         return error(read, ErrorCode::UnexpectedEndOfHexEscape);
811                     }
812 
813                     let n2 = try!(read.decode_hex_escape());
814 
815                     if n2 < 0xDC00 || n2 > 0xDFFF {
816                         return error(read, ErrorCode::LoneLeadingSurrogateInHexEscape);
817                     }
818 
819                     (((n1 - 0xD800) as u32) << 10 | (n2 - 0xDC00) as u32) + 0x1_0000
820                 }
821 
822                 n => n as u32,
823             };
824 
825             if char::from_u32(n).is_none() {
826                 return error(read, ErrorCode::InvalidUnicodeCodePoint);
827             }
828         }
829         _ => {
830             return error(read, ErrorCode::InvalidEscape);
831         }
832     }
833 
834     Ok(())
835 }
836 
837 #[cfg_attr(rustfmt, rustfmt_skip)]
838 static HEX: [u8; 256] = [
839     //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
840    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 0
841    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 1
842    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 2
843      0,  1,  2,  3,  4,  5,  6,  7,  8,  9,255,255,255,255,255,255, // 3
844    255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 4
845    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 5
846    255, 10, 11, 12, 13, 14, 15,255,255,255,255,255,255,255,255,255, // 6
847    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 7
848    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 8
849    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // 9
850    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // A
851    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // B
852    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // C
853    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // D
854    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // E
855    255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255, // F
856 ];
857 
decode_hex_val(val: u8) -> Option<u16>858 fn decode_hex_val(val: u8) -> Option<u16> {
859     let n = HEX[val as usize] as u16;
860     if n == 255 {
861         None
862     } else {
863         Some(n)
864     }
865 }
866