| 1 | //! Decodes a bencoded struct |
| 2 | //! |
| 3 | //! # Basic decoding |
| 4 | //! For any decoding process, first we need to create a decoder: |
| 5 | //! |
| 6 | //! ``` |
| 7 | //! # use bendy::decoding::{Decoder}; |
| 8 | //! # |
| 9 | //! # let buf: &[u8] = b"d3:fooi1ee"; |
| 10 | //! let _decoder = Decoder::new(buf); |
| 11 | //! ``` |
| 12 | //! |
| 13 | //! Decoders have a depth limit to prevent resource exhaustion from hostile inputs. By default, it's |
| 14 | //! set high enough for most structures that you'd encounter when prototyping, but for production |
| 15 | //! use, not only may it not be enough, but the higher the depth limit, the more stack space an |
| 16 | //! attacker can cause your program to use, so we recommend setting the bounds tightly: |
| 17 | //! |
| 18 | //! ``` |
| 19 | //! # use bendy::decoding::{Decoder}; |
| 20 | //! # |
| 21 | //! # let buf: &[u8] = b"d3:fooi1ee"; |
| 22 | //! let _decoder = Decoder::new(buf).with_max_depth(3); |
| 23 | //! ``` |
| 24 | //! |
| 25 | //! Atoms (integers and strings) have depth zero, and lists and dicts have a depth equal to the |
| 26 | //! depth of their deepest member plus one. As an special case, an empty list or dict has depth 1. |
| 27 | //! |
| 28 | //! Now, you can start reading objects: |
| 29 | //! |
| 30 | //! ``` |
| 31 | //! # use bendy::decoding::{Decoder,Object}; |
| 32 | //! # |
| 33 | //! # fn decode_list(_: bendy::decoding::ListDecoder) {} |
| 34 | //! # fn decode_dict(_: bendy::decoding::DictDecoder) {} |
| 35 | //! # |
| 36 | //! # let buf: &[u8] = b"d3:fooi1ee"; |
| 37 | //! # let mut decoder = Decoder::new(buf); |
| 38 | //! # |
| 39 | //! match decoder.next_object().unwrap() { |
| 40 | //! None => (), // EOF |
| 41 | //! Some(Object::List(d)) => decode_list(d), |
| 42 | //! Some(Object::Dict(d)) => decode_dict(d), |
| 43 | //! Some(Object::Integer(_)) => (), // integer, as a string |
| 44 | //! Some(Object::Bytes(_)) => (), // A raw bytestring |
| 45 | //! }; |
| 46 | //! ``` |
| 47 | //! |
| 48 | //! # Error handling |
| 49 | //! |
| 50 | //! Once an error is encountered, the decoder won't try to muddle through it; instead, every future |
| 51 | //! call to the decoder will return the same error. This behaviour can be used to check the syntax |
| 52 | //! of an input object without fully decoding it: |
| 53 | //! |
| 54 | //! ``` |
| 55 | //! # use bendy::decoding::Decoder; |
| 56 | //! # |
| 57 | //! fn syntax_check(buf: &[u8]) -> bool { |
| 58 | //! let mut decoder = Decoder::new(buf); |
| 59 | //! decoder.next_object().ok(); // ignore the return value of this |
| 60 | //! return decoder.next_object().is_ok(); |
| 61 | //! } |
| 62 | //! # |
| 63 | //! # assert!(syntax_check(b"i18e")); |
| 64 | //! ``` |
| 65 | |
| 66 | mod decoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/; |
| 67 | mod errorrust-analyzer cargo bendy 0.6.1 decoding/error/; |
| 68 | mod from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/; |
| 69 | mod objectrust-analyzer cargo bendy 0.6.1 decoding/object/; |
| 70 | |
| 71 | pub use selfrust-analyzer cargo bendy 0.6.1 decoding/::{ |
| 72 | decoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/::{Decoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/Decoder#, DictDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/DictDecoder#, ListDecoderrust-analyzer cargo bendy 0.6.1 decoding/decoder/ListDecoder#, Tokensrust-analyzer cargo bendy 0.6.1 decoding/decoder/Tokens#}, |
| 73 | errorrust-analyzer cargo bendy 0.6.1 decoding/error/::{Errorrust-analyzer cargo bendy 0.6.1 decoding/error/Error#, ErrorKindrust-analyzer cargo bendy 0.6.1 decoding/error/ErrorKind#, ResultExtrust-analyzer cargo bendy 0.6.1 decoding/error/ResultExt#}, |
| 74 | from_bencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/::FromBencoderust-analyzer cargo bendy 0.6.1 decoding/from_bencode/FromBencode#, |
| 75 | objectrust-analyzer cargo bendy 0.6.1 decoding/object/::Objectrust-analyzer cargo bendy 0.6.1 decoding/object/Object#, |
| 76 | }; |