MCPcopy Create free account
hub / github.com/ElementsProject/lightning / decode_bigsize

Function decode_bigsize

plugins/lsps-plugin/src/core/tlv.rs:261–300  ·  view source on GitHub ↗
(input: &[u8])

Source from the content-addressed store, hash-verified

259}
260
261fn decode_bigsize(input: &[u8]) -> Result<(u64, usize)> {
262 if input.is_empty() {
263 return Err(TlvError::Truncated.into());
264 }
265 match input[0] {
266 n @ 0x00..=0xfc => Ok((n as u64, 1)),
267 0xfd => {
268 if input.len() < 3 {
269 return Err(TlvError::Truncated.into());
270 }
271 let v = u16::from_be_bytes([input[1], input[2]]) as u64;
272 if v < 0xfd {
273 return Err(TlvError::NonCanonicalBigSize.into());
274 }
275 Ok((v, 3))
276 }
277 0xfe => {
278 if input.len() < 5 {
279 return Err(TlvError::Truncated.into());
280 }
281 let v = u32::from_be_bytes([input[1], input[2], input[3], input[4]]) as u64;
282 if v <= 0xffff {
283 return Err(TlvError::NonCanonicalBigSize.into());
284 }
285 Ok((v, 5))
286 }
287 0xff => {
288 if input.len() < 9 {
289 return Err(TlvError::Truncated.into());
290 }
291 let v = u64::from_be_bytes([
292 input[1], input[2], input[3], input[4], input[5], input[6], input[7], input[8],
293 ]);
294 if v <= 0xffff_ffff {
295 return Err(TlvError::NonCanonicalBigSize.into());
296 }
297 Ok((v, 9))
298 }
299 }
300}
301
302/// Encode a BOLT #1 `tu64`: big-endian, minimal length (no leading 0x00).
303/// Value 0 is encoded as zero-length.

Callers 2

from_bytesMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected