| 1 | [](https://github.com/enarx/ciborium/actions?query=workflow%3A%22test%22) |
| 2 | [](https://isitmaintained.com/project/enarx/ciborium "Average time to resolve an issue") |
| 3 | [](https://isitmaintained.com/project/enarx/ciborium "Percentage of issues still open") |
| 4 |  |
| 5 | |
| 6 | # ciborium-ll |
| 7 | |
| 8 | Low level CBOR parsing tools |
| 9 | |
| 10 | This crate contains low-level types for encoding and decoding items in |
| 11 | CBOR. This crate is usable in both `no_std` and `no_alloc` environments. |
| 12 | To understand how this crate works, first we will look at the structure |
| 13 | of a CBOR item on the wire. |
| 14 | |
| 15 | ## Anatomy of a CBOR Item |
| 16 | |
| 17 | This is a brief anatomy of a CBOR item on the wire. |
| 18 | |
| 19 | ``` |
| 20 | +------------+-----------+ |
| 21 | | | | |
| 22 | | Major | Minor | |
| 23 | | (3bits) | (5bits) | |
| 24 | | | | |
| 25 | +------------+-----------+ |
| 26 | ^ ^ |
| 27 | | | |
| 28 | +-----+ +-----+ |
| 29 | | | |
| 30 | | | |
| 31 | +----------------------------+--------------+ |
| 32 | | | | | |
| 33 | | Prefix | Affix | Suffix | |
| 34 | | (1 byte) | (0-8 bytes) | (0+ bytes) | |
| 35 | | | | | |
| 36 | +------------+---------------+--------------+ |
| 37 | |
| 38 | | | | |
| 39 | +------------+---------------+--------------+ |
| 40 | | | |
| 41 | v v |
| 42 | |
| 43 | Header Body |
| 44 | ``` |
| 45 | |
| 46 | The `ciborium` crate works by providing the `Decoder` and `Encoder` types |
| 47 | which provide input and output for a CBOR header (see: `Header`). From |
| 48 | there, you can either handle the body yourself or use the provided utility |
| 49 | functions. |
| 50 | |
| 51 | For more information on the CBOR format, see |
| 52 | [RFC 7049](https://tools.ietf.org/html/rfc7049). |
| 53 | |
| 54 | ## Decoding |
| 55 | |
| 56 | In order to decode CBOR, you will create a `Decoder` from a reader. The |
| 57 | decoder instance will allow you to `Decoder::pull()` `Header` instances |
| 58 | from the input. |
| 59 | |
| 60 | Most CBOR items are fully contained in their headers and therefore have no |
| 61 | body. These items can be evaluated directly from the `Header` instance. |
| 62 | |
| 63 | Bytes and text items have a body but do not contain child items. Since |
| 64 | both bytes and text values may be segmented, parsing them can be a bit |
| 65 | tricky. Therefore, we provide helper functions to parse these types. See |
| 66 | `Decoder::bytes()` and `Decoder::text()` for more details. |
| 67 | |
| 68 | Array and map items have a body which contains child items. These can be |
| 69 | parsed by simply doing `Decoder::pull()` to parse the child items. |
| 70 | |
| 71 | ### Example |
| 72 | |
| 73 | ```rust |
| 74 | use ciborium_ll::{Decoder, Header}; |
| 75 | use ciborium_io::Read as _; |
| 76 | |
| 77 | let input = b"\x6dHello, World!"; |
| 78 | let mut decoder = Decoder::from(&input[..]); |
| 79 | let mut chunks = 0; |
| 80 | |
| 81 | match decoder.pull().unwrap() { |
| 82 | Header::Text(len) => { |
| 83 | let mut segments = decoder.text(len); |
| 84 | while let Some(mut segment) = segments.pull().unwrap() { |
| 85 | let mut buffer = [0u8; 7]; |
| 86 | while let Some(chunk) = segment.pull(&mut buffer[..]).unwrap() { |
| 87 | match chunk { |
| 88 | "Hello, " if chunks == 0 => chunks = 1, |
| 89 | "World!" if chunks == 1 => chunks = 2, |
| 90 | _ => panic!("received unexpected chunk"), |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | _ => panic!("received unexpected value"), |
| 97 | } |
| 98 | |
| 99 | assert_eq!(chunks, 2); |
| 100 | ``` |
| 101 | |
| 102 | ## Encoding |
| 103 | |
| 104 | To encode values to CBOR, create an `Encoder` from a writer. The encoder |
| 105 | instance provides the `Encoder::push()` method to write a `Header` value |
| 106 | to the wire. CBOR item bodies can be written directly. |
| 107 | |
| 108 | For bytes and text, there are the `Encoder::bytes()` and `Encoder::text()` |
| 109 | utility functions, respectively, which will properly segment the output |
| 110 | on the wire for you. |
| 111 | |
| 112 | ### Example |
| 113 | |
| 114 | ```rust |
| 115 | use ciborium_ll::{Encoder, Header}; |
| 116 | use ciborium_io::Write as _; |
| 117 | |
| 118 | let mut buffer = [0u8; 19]; |
| 119 | let mut encoder = Encoder::from(&mut buffer[..]); |
| 120 | |
| 121 | // Write the structure |
| 122 | encoder.push(Header::Map(Some(1))).unwrap(); |
| 123 | encoder.push(Header::Positive(7)).unwrap(); |
| 124 | encoder.text("Hello, World!", 7).unwrap(); |
| 125 | |
| 126 | // Validate our output |
| 127 | encoder.flush().unwrap(); |
| 128 | assert_eq!(b"\xa1\x07\x7f\x67Hello, \x66World!\xff", &buffer[..]); |
| 129 | ``` |
| 130 | |
| 131 | License: Apache-2.0 |