| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | | use std::collections::BTreeMap; |
| 8 | |
|
| 9 | | #[derive(Clone, Debug, PartialEq, Eq)] |
| 10 | | pub enum Value { |
| 11 | | Uint(u64), |
| 12 | | Bytes(Vec<u8>), |
| 13 | | Text(String), |
| 14 | | Array(Vec<Value>), |
| 15 | | Map(BTreeMap<u64, Value>), |
| 16 | | Bool(bool), |
| 17 | | Null, |
| 18 | | } |
| 19 | |
|
| 20 | | impl Value { |
| 21 | 61676× | pub fn map<const N: usize>(entries: [(u64, Value); N]) -> Value { |
| 22 | 61676× | Value::Map(entries.into_iter().collect()) |
| 23 | 61676× | } |
| 24 | 3809× | pub fn as_uint(&self) -> Option<u64> { |
| 25 | 3809× | match self { |
| 26 | 3809× | Value::Uint(u) => Some(*u), |
| 27 | 0× | _ => None, |
| 28 | | } |
| 29 | 3809× | } |
| 30 | 9902× | pub fn as_bytes(&self) -> Option<&[u8]> { |
| 31 | 9902× | match self { |
| 32 | 9902× | Value::Bytes(b) => Some(b), |
| 33 | 0× | _ => None, |
| 34 | | } |
| 35 | 9902× | } |
| 36 | 3450× | pub fn as_text(&self) -> Option<&str> { |
| 37 | 3450× | match self { |
| 38 | 3450× | Value::Text(t) => Some(t), |
| 39 | 0× | _ => None, |
| 40 | | } |
| 41 | 3450× | } |
| 42 | 1321× | pub fn as_array(&self) -> Option<&[Value]> { |
| 43 | 1321× | match self { |
| 44 | 1321× | Value::Array(a) => Some(a), |
| 45 | 0× | _ => None, |
| 46 | | } |
| 47 | 1321× | } |
| 48 | 4404× | pub fn as_map(&self) -> Option<&BTreeMap<u64, Value>> { |
| 49 | 4404× | match self { |
| 50 | 4404× | Value::Map(m) => Some(m), |
| 51 | 0× | _ => None, |
| 52 | | } |
| 53 | 4404× | } |
| 54 | 0× | pub fn is_null(&self) -> bool { |
| 55 | 0× | matches!(self, Value::Null) |
| 56 | 0× | } |
| 57 | | } |
| 58 | |
|
| 59 | 919187× | fn head(out: &mut Vec<u8>, major: u8, n: u64) { |
| 60 | 919187× | let m = major << 5; |
| 61 | 919187× | match n { |
| 62 | 919187× | 0..=23 => out.push(m | n as u8), |
| 63 | 175057× | 24..=0xff => out.extend_from_slice(&[m | 24, n as u8]), |
| 64 | 83743× | 0x100..=0xffff => { |
| 65 | 28856× | out.push(m | 25); |
| 66 | 28856× | out.extend_from_slice(&(n as u16).to_be_bytes()); |
| 67 | 28856× | } |
| 68 | 54887× | 0x1_0000..=0xffff_ffff => { |
| 69 | 0× | out.push(m | 26); |
| 70 | 0× | out.extend_from_slice(&(n as u32).to_be_bytes()); |
| 71 | 0× | } |
| 72 | 54887× | _ => { |
| 73 | 54887× | out.push(m | 27); |
| 74 | 54887× | out.extend_from_slice(&n.to_be_bytes()); |
| 75 | 54887× | } |
| 76 | | } |
| 77 | 919187× | } |
| 78 | |
|
| 79 | 567437× | fn put(out: &mut Vec<u8>, v: &Value) { |
| 80 | 567437× | match v { |
| 81 | 179847× | Value::Uint(n) => head(out, 0, *n), |
| 82 | 75641× | Value::Bytes(b) => { |
| 83 | 75641× | head(out, 2, b.len() as u64); |
| 84 | 75641× | out.extend_from_slice(b); |
| 85 | 75641× | } |
| 86 | 88442× | Value::Text(t) => { |
| 87 | 88442× | head(out, 3, t.len() as u64); |
| 88 | 88442× | out.extend_from_slice(t.as_bytes()); |
| 89 | 88442× | } |
| 90 | 30950× | Value::Array(a) => { |
| 91 | 30950× | head(out, 4, a.len() as u64); |
| 92 | 53109× | for x in a { |
| 93 | 53109× | put(out, x); |
| 94 | 53109× | } |
| 95 | | } |
| 96 | | |
| 97 | | |
| 98 | 61676× | Value::Map(m) => { |
| 99 | 61676× | head(out, 5, m.len() as u64); |
| 100 | 482631× | for (k, x) in m { |
| 101 | 482631× | head(out, 0, *k); |
| 102 | 482631× | put(out, x); |
| 103 | 482631× | } |
| 104 | | } |
| 105 | 0× | Value::Bool(false) => out.push(0xf4), |
| 106 | 1× | Value::Bool(true) => out.push(0xf5), |
| 107 | 130880× | Value::Null => out.push(0xf6), |
| 108 | | } |
| 109 | 567437× | } |
| 110 | |
|
| 111 | 31697× | pub fn encode(v: &Value) -> Vec<u8> { |
| 112 | 31697× | let mut out = Vec::new(); |
| 113 | 31697× | put(&mut out, v); |
| 114 | 31697× | out |
| 115 | 31697× | } |
| 116 | |
|
| 117 | |
|
| 118 | | #[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] |
| 119 | | #[error("malformed CBOR: {0}")] |
| 120 | | pub struct Error(pub String); |
| 121 | |
|
| 122 | | struct Reader<'a> { |
| 123 | | b: &'a [u8], |
| 124 | | at: usize, |
| 125 | | depth: usize, |
| 126 | | } |
| 127 | |
|
| 128 | | const MAX_DEPTH: usize = 32; |
| 129 | |
|
| 130 | | impl<'a> Reader<'a> { |
| 131 | 72723× | fn take(&mut self, n: usize) -> Result<&'a [u8], Error> { |
| 132 | 72723× | let end = self.at.checked_add(n).filter(|e| *e <= self.b.len()).ok_or_else(|| Error("truncated".into()))?; |
| 133 | 72723× | let s = &self.b[self.at..end]; |
| 134 | 72723× | self.at = end; |
| 135 | 72723× | Ok(s) |
| 136 | 72723× | } |
| 137 | |
|
| 138 | 48115× | fn head(&mut self) -> Result<(u8, u64), Error> { |
| 139 | 48115× | let first = self.take(1)?[0]; |
| 140 | 48115× | let (major, info) = (first >> 5, first & 0x1f); |
| 141 | 48114× | let n = match info { |
| 142 | 48115× | 0..=23 => info as u64, |
| 143 | 6742× | 24 => self.take(1)?[0] as u64, |
| 144 | 2357× | 25 => u16::from_be_bytes(self.take(2)?.try_into().unwrap()) as u64, |
| 145 | 1× | 26 => u32::from_be_bytes(self.take(4)?.try_into().unwrap()) as u64, |
| 146 | 2154× | 27 => u64::from_be_bytes(self.take(8)?.try_into().unwrap()), |
| 147 | 1× | _ if major == 7 => return Ok((7, info as u64)), |
| 148 | 1× | _ => return Err(Error("an indefinite length or a reserved form".into())), |
| 149 | | }; |
| 150 | 48114× | let minimal = match info { |
| 151 | 6742× | 24 => n >= 24, |
| 152 | 2357× | 25 => n > 0xff, |
| 153 | 1× | 26 => n > 0xffff, |
| 154 | 2154× | 27 => n > 0xffff_ffff, |
| 155 | 36860× | _ => true, |
| 156 | | }; |
| 157 | 48114× | if !minimal && major != 7 { |
| 158 | 1× | return Err(Error("a length or number not in its shortest form".into())); |
| 159 | 48113× | } |
| 160 | 48113× | Ok((major, n)) |
| 161 | 48115× | } |
| 162 | |
|
| 163 | 19115× | fn len(&mut self, n: u64) -> Result<usize, Error> { |
| 164 | 19115× | let n = usize::try_from(n).map_err(|_| Error("too long".into()))?; |
| 165 | 19115× | if n > self.b.len() - self.at { |
| 166 | 1× | return Err(Error("truncated".into())); |
| 167 | 19114× | } |
| 168 | 19114× | Ok(n) |
| 169 | 19115× | } |
| 170 | |
|
| 171 | 25877× | fn value(&mut self) -> Result<Value, Error> { |
| 172 | 25877× | self.depth += 1; |
| 173 | 25877× | if self.depth > MAX_DEPTH { |
| 174 | 1× | return Err(Error("nested too deeply".into())); |
| 175 | 25876× | } |
| 176 | 25876× | let (major, n) = self.head()?; |
| 177 | 25874× | let v = match major { |
| 178 | 3839× | 0 => Value::Uint(n), |
| 179 | | 2 => { |
| 180 | 9904× | let l = self.len(n)?; |
| 181 | 9903× | Value::Bytes(self.take(l)?.to_vec()) |
| 182 | | } |
| 183 | | 3 => { |
| 184 | 3451× | let l = self.len(n)?; |
| 185 | 3451× | Value::Text(String::from_utf8(self.take(l)?.to_vec()).map_err(|_| Error("text that is not UTF-8".into()))?) |
| 186 | | } |
| 187 | | 4 => { |
| 188 | 1354× | let l = self.len(n)?; |
| 189 | 1354× | let mut a = Vec::with_capacity(l); |
| 190 | 1354× | for _ in 0..l { |
| 191 | 916× | a.push(self.value()?); |
| 192 | | } |
| 193 | 1322× | Value::Array(a) |
| 194 | | } |
| 195 | | 5 => { |
| 196 | 4406× | let l = self.len(n)?; |
| 197 | 4406× | let mut m = BTreeMap::new(); |
| 198 | 4406× | let mut last: Option<u64> = None; |
| 199 | 4406× | for _ in 0..l { |
| 200 | 22239× | let (km, k) = self.head()?; |
| 201 | 22239× | if km != 0 { |
| 202 | 0× | return Err(Error("a map key that is not an unsigned integer".into())); |
| 203 | 22239× | } |
| 204 | 22239× | if last.is_some_and(|p| k <= p) { |
| 205 | 1× | return Err(Error("map keys out of order or repeated".into())); |
| 206 | 22238× | } |
| 207 | 22238× | last = Some(k); |
| 208 | 22238× | m.insert(k, self.value()?); |
| 209 | | } |
| 210 | 4405× | Value::Map(m) |
| 211 | | } |
| 212 | 2919× | 7 => match n { |
| 213 | 0× | 20 => Value::Bool(false), |
| 214 | 1× | 21 => Value::Bool(true), |
| 215 | 2918× | 22 => Value::Null, |
| 216 | 0× | _ => return Err(Error("an unsupported simple value or float".into())), |
| 217 | | }, |
| 218 | 1× | _ => return Err(Error("a negative integer or a tag".into())), |
| 219 | | }; |
| 220 | 25839× | self.depth -= 1; |
| 221 | 25839× | Ok(v) |
| 222 | 25877× | } |
| 223 | | } |
| 224 | |
|
| 225 | |
|
| 226 | 2723× | pub fn decode(b: &[u8]) -> Result<Value, Error> { |
| 227 | 2723× | let mut r = Reader { b, at: 0, depth: 0 }; |
| 228 | 2723× | let v = r.value()?; |
| 229 | 2717× | if r.at != b.len() { |
| 230 | 1× | return Err(Error("bytes after the value".into())); |
| 231 | 2716× | } |
| 232 | 2716× | Ok(v) |
| 233 | 2723× | } |
| 234 | |
|
| 235 | | #[cfg(test)] |
| 236 | | mod tests { |
| 237 | | use super::*; |
| 238 | |
|
| 239 | | #[test] |
| 240 | 1× | fn round_trips_and_known_encodings() { |
| 241 | 1× | let v = Value::map([ |
| 242 | 1× | (0, Value::Uint(1)), |
| 243 | 1× | (1, Value::Text("é".into())), |
| 244 | 1× | (2, Value::Bytes(vec![1, 2])), |
| 245 | 1× | (3, Value::Array(vec![Value::Null, Value::Bool(true), Value::Uint(500)])), |
| 246 | 1× | (24, Value::Uint(u64::MAX)), |
| 247 | 1× | ]); |
| 248 | 1× | let b = encode(&v); |
| 249 | 1× | assert_eq!(decode(&b).unwrap(), v); |
| 250 | 1× | assert_eq!(encode(&Value::Uint(23)), [0x17]); |
| 251 | 1× | assert_eq!(encode(&Value::Uint(24)), [0x18, 24]); |
| 252 | 1× | assert_eq!(encode(&Value::Uint(1000)), [0x19, 0x03, 0xe8]); |
| 253 | 1× | assert_eq!(encode(&Value::Text("a".into())), [0x61, b'a']); |
| 254 | 1× | } |
| 255 | |
|
| 256 | | #[test] |
| 257 | 1× | fn refuses_what_is_not_canonical() { |
| 258 | 1× | assert!(decode(&[0x18, 5]).is_err(), "5 in two bytes"); |
| 259 | 1× | assert!(decode(&[0xa2, 0x01, 0x00, 0x00, 0x00]).is_err(), "keys out of order"); |
| 260 | 1× | assert!(decode(&[0x5f]).is_err(), "indefinite length"); |
| 261 | 1× | assert!(decode(&[0x01, 0x02]).is_err(), "trailing bytes"); |
| 262 | 1× | assert!(decode(&[0x5a, 0xff, 0xff, 0xff, 0xff]).is_err(), "truncated"); |
| 263 | 1× | assert!(decode(&[0x20]).is_err(), "negative"); |
| 264 | 1× | let deep = [0x81u8; 64]; |
| 265 | 1× | assert!(decode(&deep).is_err(), "too deep"); |
| 266 | 1× | } |
| 267 | | } |