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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! CBOR encoder.

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::io::Write;
use std::iter::FromIterator;
use std::ops::Deref;
use std::sync::Arc;

use byteorder::{BigEndian, ByteOrder};
use libipld_core::cid::Cid;
use libipld_core::codec::Encode;
use libipld_core::error::Result;
use libipld_core::ipld::Ipld;

use crate::cbor::{MajorKind, FALSE, TRUE};
use crate::error::NumberOutOfRange;
use crate::DagCborCodec as DagCbor;

/// Writes a null byte to a cbor encoded byte stream.
pub fn write_null<W: Write>(w: &mut W) -> Result<()> {
    w.write_all(&[0xf6])?;
    Ok(())
}

/// Writes a u8 to a cbor encoded byte stream.
pub fn write_u8<W: Write>(w: &mut W, major: MajorKind, value: u8) -> Result<()> {
    let major = major as u8;
    if value <= 0x17 {
        let buf = [major << 5 | value];
        w.write_all(&buf)?;
    } else {
        let buf = [major << 5 | 24, value];
        w.write_all(&buf)?;
    }
    Ok(())
}

/// Writes a u16 to a cbor encoded byte stream.
pub fn write_u16<W: Write>(w: &mut W, major: MajorKind, value: u16) -> Result<()> {
    if value <= u16::from(u8::max_value()) {
        write_u8(w, major, value as u8)?;
    } else {
        let mut buf = [(major as u8) << 5 | 25, 0, 0];
        BigEndian::write_u16(&mut buf[1..], value);
        w.write_all(&buf)?;
    }
    Ok(())
}

/// Writes a u32 to a cbor encoded byte stream.
pub fn write_u32<W: Write>(w: &mut W, major: MajorKind, value: u32) -> Result<()> {
    if value <= u32::from(u16::max_value()) {
        write_u16(w, major, value as u16)?;
    } else {
        let mut buf = [(major as u8) << 5 | 26, 0, 0, 0, 0];
        BigEndian::write_u32(&mut buf[1..], value);
        w.write_all(&buf)?;
    }
    Ok(())
}

/// Writes a u64 to a cbor encoded byte stream.
pub fn write_u64<W: Write>(w: &mut W, major: MajorKind, value: u64) -> Result<()> {
    if value <= u64::from(u32::max_value()) {
        write_u32(w, major, value as u32)?;
    } else {
        let mut buf = [(major as u8) << 5 | 27, 0, 0, 0, 0, 0, 0, 0, 0];
        BigEndian::write_u64(&mut buf[1..], value);
        w.write_all(&buf)?;
    }
    Ok(())
}

/// Writes a tag to a cbor encoded byte stream.
pub fn write_tag<W: Write>(w: &mut W, tag: u64) -> Result<()> {
    write_u64(w, MajorKind::Tag, tag)
}

impl Encode<DagCbor> for bool {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        let buf = if *self { [TRUE.into()] } else { [FALSE.into()] };
        w.write_all(&buf)?;
        Ok(())
    }
}

impl Encode<DagCbor> for u8 {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        write_u8(w, MajorKind::UnsignedInt, *self)
    }
}

impl Encode<DagCbor> for u16 {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        write_u16(w, MajorKind::UnsignedInt, *self)
    }
}

impl Encode<DagCbor> for u32 {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        write_u32(w, MajorKind::UnsignedInt, *self)
    }
}

impl Encode<DagCbor> for u64 {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        write_u64(w, MajorKind::UnsignedInt, *self)
    }
}

impl Encode<DagCbor> for i8 {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        if self.is_negative() {
            write_u8(w, MajorKind::NegativeInt, -(*self + 1) as u8)
        } else {
            (*self as u8).encode(c, w)
        }
    }
}

impl Encode<DagCbor> for i16 {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        if self.is_negative() {
            write_u16(w, MajorKind::NegativeInt, -(*self + 1) as u16)
        } else {
            (*self as u16).encode(c, w)
        }
    }
}

impl Encode<DagCbor> for i32 {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        if self.is_negative() {
            write_u32(w, MajorKind::NegativeInt, -(*self + 1) as u32)
        } else {
            (*self as u32).encode(c, w)
        }
    }
}

impl Encode<DagCbor> for i64 {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        if self.is_negative() {
            write_u64(w, MajorKind::NegativeInt, -(*self + 1) as u64)
        } else {
            (*self as u64).encode(c, w)
        }
    }
}

impl Encode<DagCbor> for f32 {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        // IPLD maximally encodes floats.
        f64::from(*self).encode(c, w)
    }
}

impl Encode<DagCbor> for f64 {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        // IPLD forbids nan, infinities, etc.
        if !self.is_finite() {
            return Err(NumberOutOfRange::new::<f64>().into());
        }
        let mut buf = [0xfb, 0, 0, 0, 0, 0, 0, 0, 0];
        BigEndian::write_f64(&mut buf[1..], *self);
        w.write_all(&buf)?;
        Ok(())
    }
}

impl Encode<DagCbor> for [u8] {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        write_u64(w, MajorKind::ByteString, self.len() as u64)?;
        w.write_all(self)?;
        Ok(())
    }
}

impl Encode<DagCbor> for Box<[u8]> {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        self[..].encode(c, w)
    }
}

impl Encode<DagCbor> for str {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        write_u64(w, MajorKind::TextString, self.len() as u64)?;
        w.write_all(self.as_bytes())?;
        Ok(())
    }
}

impl Encode<DagCbor> for String {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        self.as_str().encode(c, w)
    }
}

impl Encode<DagCbor> for i128 {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        if *self < 0 {
            if -(*self + 1) > u64::max_value() as i128 {
                return Err(NumberOutOfRange::new::<i128>().into());
            }
            write_u64(w, MajorKind::NegativeInt, -(*self + 1) as u64)?;
        } else {
            if *self > u64::max_value() as i128 {
                return Err(NumberOutOfRange::new::<i128>().into());
            }
            write_u64(w, MajorKind::UnsignedInt, *self as u64)?;
        }
        Ok(())
    }
}

impl Encode<DagCbor> for Cid {
    fn encode<W: Write>(&self, _: DagCbor, w: &mut W) -> Result<()> {
        write_tag(w, 42)?;
        // insert zero byte per https://github.com/ipld/specs/blob/master/block-layer/codecs/dag-cbor.md#links
        // TODO: don't allocate
        let buf = self.to_bytes();
        let len = buf.len();
        write_u64(w, MajorKind::ByteString, len as u64 + 1)?;
        w.write_all(&[0])?;
        w.write_all(&buf[..len])?;
        Ok(())
    }
}

impl<T: Encode<DagCbor>> Encode<DagCbor> for Option<T> {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        if let Some(value) = self {
            value.encode(c, w)?;
        } else {
            write_null(w)?;
        }
        Ok(())
    }
}

impl<T: Encode<DagCbor>> Encode<DagCbor> for Vec<T> {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        write_u64(w, MajorKind::Array, self.len() as u64)?;
        for value in self {
            value.encode(c, w)?;
        }
        Ok(())
    }
}

impl<T: Encode<DagCbor> + 'static> Encode<DagCbor> for BTreeMap<String, T> {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        write_u64(w, MajorKind::Map, self.len() as u64)?;
        // CBOR RFC-7049 specifies a canonical sort order, where keys are sorted by length first.
        // This was later revised with RFC-8949, but we need to stick to the original order to stay
        // compatible with existing data.
        let mut cbor_order = Vec::from_iter(self);
        cbor_order.sort_unstable_by(|&(key_a, _), &(key_b, _)| {
            match key_a.len().cmp(&key_b.len()) {
                Ordering::Greater => Ordering::Greater,
                Ordering::Less => Ordering::Less,
                Ordering::Equal => key_a.cmp(key_b),
            }
        });
        for (k, v) in cbor_order {
            k.encode(c, w)?;
            v.encode(c, w)?;
        }
        Ok(())
    }
}

impl Encode<DagCbor> for Ipld {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        match self {
            Self::Null => write_null(w),
            Self::Bool(b) => b.encode(c, w),
            Self::Integer(i) => i.encode(c, w),
            Self::Float(f) => f.encode(c, w),
            Self::Bytes(b) => b.as_slice().encode(c, w),
            Self::String(s) => s.encode(c, w),
            Self::List(l) => l.encode(c, w),
            Self::Map(m) => m.encode(c, w),
            Self::Link(cid) => cid.encode(c, w),
        }
    }
}

impl<T: Encode<DagCbor>> Encode<DagCbor> for Arc<T> {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        self.deref().encode(c, w)
    }
}

impl Encode<DagCbor> for () {
    fn encode<W: Write>(&self, _c: DagCbor, w: &mut W) -> Result<()> {
        write_u8(w, MajorKind::Array, 0)?;
        Ok(())
    }
}

impl<A: Encode<DagCbor>> Encode<DagCbor> for (A,) {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        write_u8(w, MajorKind::Array, 1)?;
        self.0.encode(c, w)?;
        Ok(())
    }
}

impl<A: Encode<DagCbor>, B: Encode<DagCbor>> Encode<DagCbor> for (A, B) {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        write_u8(w, MajorKind::Array, 2)?;
        self.0.encode(c, w)?;
        self.1.encode(c, w)?;
        Ok(())
    }
}

impl<A: Encode<DagCbor>, B: Encode<DagCbor>, C: Encode<DagCbor>> Encode<DagCbor> for (A, B, C) {
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        write_u8(w, MajorKind::Array, 3)?;
        self.0.encode(c, w)?;
        self.1.encode(c, w)?;
        self.2.encode(c, w)?;
        Ok(())
    }
}

impl<A: Encode<DagCbor>, B: Encode<DagCbor>, C: Encode<DagCbor>, D: Encode<DagCbor>> Encode<DagCbor>
    for (A, B, C, D)
{
    fn encode<W: Write>(&self, c: DagCbor, w: &mut W) -> Result<()> {
        write_u8(w, MajorKind::Array, 4)?;
        self.0.encode(c, w)?;
        self.1.encode(c, w)?;
        self.2.encode(c, w)?;
        self.3.encode(c, w)?;
        Ok(())
    }
}