Decode a BOLT #1 `tu64`, enforcing minimal form. Empty slice -> 0. Leading 0x00 or >8 bytes is invalid.
(raw: &[u8])
| 313 | /// Decode a BOLT #1 `tu64`, enforcing minimal form. |
| 314 | /// Empty slice -> 0. Leading 0x00 or >8 bytes is invalid. |
| 315 | fn decode_tu64(raw: &[u8]) -> Result<u64> { |
| 316 | if raw.is_empty() { |
| 317 | return Ok(0); |
| 318 | } |
| 319 | if raw.len() > 8 { |
| 320 | return Err(TlvError::Overflow); |
| 321 | } |
| 322 | if raw[0] == 0 { |
| 323 | return Err(TlvError::LeadingZero); |
| 324 | } |
| 325 | let mut buf = [0u8; 8]; |
| 326 | buf[8 - raw.len()..].copy_from_slice(raw); |
| 327 | Ok(u64::from_be_bytes(buf)) |
| 328 | } |
| 329 | |
| 330 | #[cfg(test)] |
| 331 | mod tests { |