1use std::fmt;
23
24use anyhow::{anyhow, bail, Result};
25
26pub const CODEC_DAG_CBOR: u64 = 0x71;
28pub const CODEC_RAW: u64 = 0x55;
30
31const MH_SHA2_256: u64 = 0x12;
33const SHA256_LEN: usize = 32;
34
35#[derive(Clone, Copy, Eq, PartialEq, Hash)]
45pub struct Cid {
46 bytes: [u8; Self::CAP],
48 len: u8,
50}
51
52impl Cid {
53 const CAP: usize = 40;
56
57 pub fn from_dag_cbor(block_bytes: &[u8]) -> Self {
63 let mut bytes = Vec::with_capacity(38);
64 bytes.push(0x01); write_uvarint(CODEC_DAG_CBOR, &mut bytes);
66 write_uvarint(MH_SHA2_256, &mut bytes);
68 write_uvarint(SHA256_LEN as u64, &mut bytes);
69 use sha2::{Digest, Sha256};
70 let digest = Sha256::digest(block_bytes);
71 bytes.extend_from_slice(&digest);
72 Cid::from_vec(bytes)
73 }
74
75 pub fn from_raw(blob_bytes: &[u8]) -> Self {
78 let mut bytes = Vec::with_capacity(38);
79 bytes.push(0x01);
80 write_uvarint(CODEC_RAW, &mut bytes);
81 write_uvarint(MH_SHA2_256, &mut bytes);
82 write_uvarint(SHA256_LEN as u64, &mut bytes);
83 use sha2::{Digest, Sha256};
84 let digest = Sha256::digest(blob_bytes);
85 bytes.extend_from_slice(&digest);
86 Cid::from_vec(bytes)
87 }
88
89 fn from_vec(bytes: Vec<u8>) -> Self {
91 let len = bytes.len();
92 assert!(
93 len <= Self::CAP,
94 "CID exceeds inline buffer: {len} > {}",
95 Self::CAP
96 );
97 let mut buf = [0u8; Self::CAP];
98 buf[..len].copy_from_slice(&bytes);
99 Cid {
100 bytes: buf,
101 len: len as u8,
102 }
103 }
104
105 pub fn as_bytes(&self) -> &[u8] {
107 &self.bytes[..self.len as usize]
108 }
109
110 pub fn to_link_bytes(&self) -> Vec<u8> {
116 let bytes = self.as_bytes();
117 let mut out = Vec::with_capacity(1 + bytes.len());
118 out.push(0x00);
119 out.extend_from_slice(bytes);
120 out
121 }
122
123 pub fn from_link_bytes(link: &[u8]) -> Result<Self> {
125 if link.is_empty() {
126 bail!("empty DAG-CBOR link payload");
127 }
128 if link[0] != 0x00 {
130 bail!(
131 "DAG-CBOR link payload must start with 0x00 identity marker, got 0x{:02x}",
132 link[0]
133 );
134 }
135 Self::from_bytes(&link[1..])
136 }
137
138 pub fn from_bytes(raw: &[u8]) -> Result<Self> {
140 if raw.is_empty() {
141 bail!("empty CID bytes");
142 }
143 if raw[0] != 0x01 {
144 bail!(
145 "only CIDv1 is supported (got version byte 0x{:02x})",
146 raw[0]
147 );
148 }
149 let rest = &raw[1..];
151 let (_codec, rest) = read_uvarint(rest).ok_or_else(|| anyhow!("truncated codec varint"))?;
152 let (code, rest) = read_uvarint(rest).ok_or_else(|| anyhow!("truncated multihash code"))?;
153 let (len, rest) =
154 read_uvarint(rest).ok_or_else(|| anyhow!("truncated multihash length"))?;
155 let len = usize::try_from(len).map_err(|_| anyhow!("multihash length overflow"))?;
156 if rest.len() != len {
157 bail!(
158 "multihash length {len} does not match remaining {} bytes",
159 rest.len()
160 );
161 }
162 if code != MH_SHA2_256 {
163 bail!("only sha2-256 multihash is supported (got code {code})");
164 }
165 if raw.len() > Self::CAP {
166 bail!("CID exceeds inline buffer: {} > {}", raw.len(), Self::CAP);
167 }
168 Ok(Cid::from_vec(raw.to_vec()))
169 }
170
171 pub fn encode(&self) -> String {
181 let bytes = self.as_bytes();
182 let mut out = String::with_capacity(1 + bytes.len() * 2);
183 out.push('b');
184 out.push_str(&base32_nopad_lower(bytes));
185 out
186 }
187}
188
189impl fmt::Debug for Cid {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 write!(f, "Cid({})", self.encode())
192 }
193}
194
195impl fmt::Display for Cid {
196 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
197 f.write_str(&self.encode())
198 }
199}
200
201impl PartialOrd for Cid {
202 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
203 Some(self.cmp(other))
204 }
205}
206impl Ord for Cid {
207 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
208 self.bytes.cmp(&other.bytes)
209 }
210}
211
212pub(crate) fn write_uvarint(mut val: u64, out: &mut Vec<u8>) {
216 while val >= 0x80 {
217 out.push(((val & 0x7f) as u8) | 0x80);
218 val >>= 7;
219 }
220 out.push(val as u8);
221}
222
223pub(crate) fn read_uvarint(input: &[u8]) -> Option<(u64, &[u8])> {
225 let mut val: u64 = 0;
226 let mut shift = 0u32;
227 for (i, &b) in input.iter().enumerate() {
228 if shift >= 64 {
229 return None; }
231 if shift == 63 && b & 0x7e != 0 {
234 return None;
235 }
236 val |= ((b & 0x7f) as u64) << shift;
237 if b & 0x80 == 0 {
238 if i > 0 && b == 0 {
241 return None;
242 }
243 return Some((val, &input[i + 1..]));
244 }
245 shift += 7;
246 }
247 None }
249
250fn base32_nopad_lower(bytes: &[u8]) -> String {
252 const ALPHA: &[u8; 32] = b"abcdefghijklmnopqrstuvwxyz234567";
253 let mut out = String::with_capacity((bytes.len() * 8).div_ceil(5));
254 let mut buffer: u64 = 0;
255 let mut bits: u32 = 0;
256 for &b in bytes {
257 buffer = (buffer << 8) | (b as u64);
258 bits += 8;
259 while bits >= 5 {
260 bits -= 5;
261 let idx = ((buffer >> bits) & 0x1f) as usize;
262 out.push(ALPHA[idx] as char);
263 }
264 }
265 if bits > 0 {
266 let idx = ((buffer << (5 - bits)) & 0x1f) as usize;
267 out.push(ALPHA[idx] as char);
268 }
269 out
270}
271
272#[cfg(test)]
273mod tests {
274 #![allow(
275 clippy::unwrap_used,
276 clippy::expect_used,
277 clippy::indexing_slicing,
278 clippy::panic
279 )]
280 use super::*;
281
282 #[test]
283 fn cid_is_deterministic() {
284 let a = Cid::from_dag_cbor(b"hello world");
286 let b = Cid::from_dag_cbor(b"hello world");
287 assert_eq!(a, b);
288 assert_eq!(a.as_bytes(), b.as_bytes());
289 assert_ne!(
290 Cid::from_dag_cbor(b"hello world"),
291 Cid::from_dag_cbor(b"hello earth")
292 );
293 }
294
295 #[test]
296 fn cid_round_trip_link_bytes() {
297 let cid = Cid::from_dag_cbor(b"some dag-cbor block");
298 let link = cid.to_link_bytes();
299 assert_eq!(link[0], 0x00);
300 let parsed = Cid::from_link_bytes(&link).expect("round-trip");
301 assert_eq!(cid, parsed);
302 }
303
304 #[test]
305 fn cid_round_trip_from_bytes() {
306 let cid = Cid::from_dag_cbor(b"x");
307 let parsed = Cid::from_bytes(cid.as_bytes()).expect("round-trip");
308 assert_eq!(cid, parsed);
309 }
310
311 #[test]
312 fn cid_string_starts_with_b() {
313 let cid = Cid::from_dag_cbor(b"");
314 let s = cid.encode();
315 assert!(s.starts_with('b'), "CIDv1 base32 must start with 'b': {s}");
316 }
317
318 #[test]
319 fn varint_round_trip() {
320 for val in [
321 0u64,
322 1,
323 0x7f,
324 0x80,
325 0xff,
326 0x71,
327 0x12,
328 0x4000,
329 u32::MAX as u64,
330 ] {
331 let mut buf = Vec::new();
332 write_uvarint(val, &mut buf);
333 let (parsed, rest) = read_uvarint(&buf).expect("round-trip");
334 assert_eq!(parsed, val, "varint {val}");
335 assert!(rest.is_empty(), "varint {val} had trailing bytes");
336 }
337 }
338
339 #[test]
340 fn varint_rejects_overlong_and_overflow_encodings() {
341 assert!(read_uvarint(&[0xf1, 0x00]).is_none());
342 assert!(read_uvarint(&[0x80, 0x00]).is_none());
343
344 let mut overflow = vec![0xff; 9];
345 overflow.push(0x02);
346 assert!(read_uvarint(&overflow).is_none());
347 }
348}