| 63 | } |
| 64 | |
| 65 | pub fn from_bytes(mut bytes: &[u8]) -> Result<Self> { |
| 66 | let mut recs = Vec::new(); |
| 67 | let mut last_type: Option<u64> = None; |
| 68 | |
| 69 | while !bytes.is_empty() { |
| 70 | let (t, n1) = decode_bigsize(bytes)?; |
| 71 | bytes = &bytes[n1..]; |
| 72 | let (len, n2) = decode_bigsize(bytes)?; |
| 73 | bytes = &bytes[n2..]; |
| 74 | |
| 75 | let l = usize::try_from(len).map_err(|_| TlvError::Overflow)?; |
| 76 | if bytes.len() < l { |
| 77 | return Err(TlvError::Truncated.into()); |
| 78 | } |
| 79 | let v = bytes[..l].to_vec(); |
| 80 | bytes = &bytes[l..]; |
| 81 | |
| 82 | if let Some(prev) = last_type { |
| 83 | if t == prev { |
| 84 | return Err(TlvError::DuplicateType(t).into()); |
| 85 | } |
| 86 | if t < prev { |
| 87 | return Err(TlvError::NotSorted.into()); |
| 88 | } |
| 89 | } |
| 90 | last_type = Some(t); |
| 91 | recs.push(TlvRecord { type_: t, value: v }); |
| 92 | } |
| 93 | Ok(TlvStream(recs)) |
| 94 | } |
| 95 | |
| 96 | pub fn from_bytes_with_length_prefix(bytes: &[u8]) -> Result<Self> { |
| 97 | if bytes.is_empty() { |