1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! # cid
//!
//! Implementation of [cid](https://github.com/ipld/cid) in Rust.

#![cfg_attr(not(feature = "std"), no_std)]

mod cid;
pub mod codec;
mod error;
mod typed_cid;
mod version;

#[cfg(any(test, feature = "arb"))]
mod arb;

pub use self::{
  cid::Cid as CidGeneric,
  codec::Codec,
  error::{
    Error,
    Result,
  },
  version::Version,
};

pub use multibase;
pub use sp_multihash;

extern crate alloc;
use bytecursor::ByteCursor;
use unsigned_varint::{
  decode,
  encode as varint_encode,
};

/// Reader function from unsigned_varint
pub fn varint_read_u64(r: &mut ByteCursor) -> Result<u64> {
  let mut b = varint_encode::u64_buffer();
  for i in 0..b.len() {
    let n = r.read(&mut (b[i..i + 1]));
    if n == 0 {
      return Err(Error::VarIntDecodeError);
    }
    if decode::is_last(b[i]) {
      return Ok(decode::u64(&b[..=i]).unwrap().0);
    }
  }
  Err(Error::VarIntDecodeError)
}

/// A Cid that contains a multihash with an allocated size of 512 bits.
///
/// This is the same digest size the default multihash code table has.
///
/// If you need a CID that is generic over its digest size, use [`CidGeneric`]
/// instead.
pub type Cid = CidGeneric<64>;