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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use std::{
    borrow::Cow,
    collections::BTreeMap,
    convert::TryFrom,
    fmt::{Display, Formatter},
    iter,
};

use crate::{
    constants::*,
    reader::{integer, tagged_value, Iter},
    visit::visit,
    Cbor,
};

/// Lifted navigation structure for a CborValue.
///
/// You can obtain this using [`CborValue::as_object()`](struct.CborValue#method.as_object).
#[derive(Debug, Clone, PartialEq)]
pub enum CborObject<'a> {
    Array(Vec<CborObject<'a>>),
    Dict(BTreeMap<&'a str, CborObject<'a>>),
    Value(Option<u64>, ValueKind<'a>),
}

impl<'a> CborObject<'a> {
    pub fn depth(&self) -> usize {
        match self {
            CborObject::Array(v) => 1 + v.iter().map(|o| o.depth()).max().unwrap_or(1),
            CborObject::Dict(d) => 1 + d.values().map(|o| o.depth()).max().unwrap_or(1),
            CborObject::Value(_, _) => 1,
        }
    }
}

/// Low-level decoded form of a CBOR item. Use CborValue for inspecting values.
///
/// Beware of the `Neg` variant, which carries `-1 - x`.
///
/// The Owned variants are only generated when decoding indefinite size (byte) strings in order
/// to present a contiguous slice of memory. You will never see these if you used
/// [`Cbor::canonical()`](struct.Cbor#method.canonical).
#[derive(Debug, PartialEq, Clone)]
pub enum ValueKind<'a> {
    Pos(u64),
    Neg(u64),
    Float(f64),
    Str(&'a str),
    Bytes(&'a [u8]),
    Bool(bool),
    Null,
    Undefined,
    Simple(u8),
    Array,
    Dict,
}
use ValueKind::*;

impl<'a> Display for ValueKind<'a> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Pos(x) => write!(f, "{}", x),
            Neg(x) => write!(f, "{}", -1 - (*x as i128)),
            Float(x) => {
                if *x == 0f64 && x.is_sign_negative() {
                    write!(f, "-0.0")
                } else {
                    write!(f, "{:?}", x)
                }
            }
            Str(s) => write!(f, "\"{}\"", s.escape_debug()),
            Bytes(b) => write!(
                f,
                "0x{}",
                b.iter()
                    .map(|b| format!("{:02x}", b))
                    .collect::<Vec<_>>()
                    .join("")
            ),
            Bool(b) => write!(f, "{}", b),
            Null => write!(f, "null"),
            Undefined => write!(f, "undefined"),
            Simple(b) => write!(f, "simple({})", b),
            Array => write!(f, "array"),
            Dict => write!(f, "dict"),
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct Tag<'a> {
    pub tag: u64,
    pub bytes: &'a [u8],
}

/// Representation of a possibly tagged CBOR data item.
#[derive(Debug, Clone)]
pub struct CborValue<'a> {
    pub tag: Option<Tag<'a>>,
    pub kind: ValueKind<'a>,
    pub bytes: &'a [u8],
}

impl<'a> PartialEq<CborValue<'_>> for CborValue<'a> {
    fn eq(&self, other: &CborValue<'_>) -> bool {
        self.tag() == other.tag() && self.kind == other.kind
    }
}

impl<'a> Display for CborValue<'a> {
    fn fmt(&self, mut f: &mut Formatter<'_>) -> std::fmt::Result {
        type Res<T> = Result<T, std::fmt::Error>;
        impl crate::visit::Visitor<std::fmt::Error> for &mut Formatter<'_> {
            fn visit_simple(&mut self, item: CborValue) -> Res<()> {
                if let Some(t) = item.tag() {
                    write!(*self, "{}|", t)?;
                }
                write!(*self, "{}", item.kind)
            }
            fn visit_array_begin(&mut self, size: Option<u64>, tag: Option<u64>) -> Res<bool> {
                if let Some(t) = tag {
                    write!(*self, "{}|", t)?;
                }
                write!(*self, "[")?;
                if size.is_none() {
                    write!(*self, "_ ")?;
                }
                Ok(true)
            }
            fn visit_array_index(&mut self, idx: u64) -> Res<bool> {
                if idx > 0 {
                    write!(*self, ", ")?;
                }
                Ok(true)
            }
            fn visit_array_end(&mut self) -> Res<()> {
                write!(*self, "]")
            }
            fn visit_dict_begin(&mut self, size: Option<u64>, tag: Option<u64>) -> Res<bool> {
                if let Some(t) = tag {
                    write!(*self, "{}|", t)?;
                }
                write!(*self, "{{")?;
                if size.is_none() {
                    write!(*self, "_ ")?;
                }
                Ok(true)
            }
            fn visit_dict_key(&mut self, key: &str, is_first: bool) -> Res<bool> {
                if !is_first {
                    write!(*self, ", ")?;
                }
                write!(*self, "\"{}\": ", key.escape_debug())?;
                Ok(true)
            }
            fn visit_dict_end(&mut self) -> Res<()> {
                write!(*self, "}}")
            }
        }
        visit(&mut f, self.clone())
    }
}

// TODO flesh out and extract data more thoroughly
impl<'a> CborValue<'a> {
    #[cfg(test)]
    pub fn fake(tag: Option<u64>, kind: ValueKind<'a>) -> Self {
        Self {
            tag: tag.map(|tag| Tag { tag, bytes: b"" }),
            kind,
            bytes: b"",
        }
    }

    /// strip off wrappers of CBOR encoded item
    pub fn decoded(&self) -> Option<Self> {
        if let (Some(TAG_CBOR_ITEM), Bytes(b)) = (self.tag(), &self.kind) {
            tagged_value(b)?.decoded()
        } else {
            Some(self.clone())
        }
    }

    /// Get value of the innermost tag if one was provided.
    pub fn tag(&self) -> Option<u64> {
        self.tag.as_ref().map(|t| t.tag)
    }

    /// Try to interpret this value as a 64bit unsigned integer.
    ///
    /// Currently does not check floats or big integers.
    pub fn as_u64(&self) -> Option<u64> {
        // TODO should also check for bigint
        match self.decoded()?.kind {
            Pos(x) => Some(x),
            _ => None,
        }
    }

    /// Try to interpret this value as a signed integer.
    ///
    /// Currently does not check floats or big integers.
    pub fn as_i64(&self) -> Option<i64> {
        match self.decoded()?.kind {
            Pos(x) => i64::try_from(x).ok(),
            Neg(x) => i64::try_from(x).ok().map(|x| -1 - x),
            _ => None,
        }
    }

    /// Try to interpret this value as a signed integer.
    ///
    /// Currently does not check floats or big integers.
    pub fn as_i32(&self) -> Option<i32> {
        match self.decoded()?.kind {
            Pos(x) => i32::try_from(x).ok(),
            Neg(x) => i32::try_from(x).ok().map(|x| -1 - x),
            _ => None,
        }
    }

    /// Try to interpret this value as a floating-point number.
    ///
    /// TODO: add proper representations for the big number types supported by CBOR
    pub fn as_f64(&self) -> Option<f64> {
        let decoded = self.decoded()?;
        match decoded.kind {
            Pos(x) => Some(x as f64),
            Neg(x) => Some(-1.0 - (x as f64)),
            Float(f) => Some(f),
            Bytes(b) if decoded.tag() == Some(TAG_BIGNUM_POS) => Some(bytes_to_float(b)),
            Bytes(b) if decoded.tag() == Some(TAG_BIGNUM_NEG) => Some(-bytes_to_float(b)),
            Array if decoded.tag() == Some(TAG_BIGDECIMAL) => {
                let cbor = Cbor::trusting(decoded.bytes);
                let exponent = cbor.index_iter(iter::once("0"))?.as_i32()?;
                let mantissa = cbor.index_iter(iter::once("1"))?.as_f64()?;
                Some(mantissa * 10f64.powi(exponent))
            }
            Array if decoded.tag() == Some(TAG_BIGFLOAT) => {
                let cbor = Cbor::trusting(decoded.bytes);
                let exponent = cbor.index_iter(iter::once("0"))?.as_i32()?;
                let mantissa = cbor.index_iter(iter::once("1"))?.as_f64()?;
                Some(mantissa * 2f64.powi(exponent))
            }
            _ => None,
        }
    }

    /// Try to interpret this value as byte string.
    ///
    /// This returns a `Cow` because it may need to allocate a vector when decoding base64 strings.
    pub fn as_bytes(&self) -> Option<Cow<'a, [u8]>> {
        let decoded = self.decoded()?;
        match decoded.kind {
            Bytes(b) => Some(Cow::Borrowed(b)),
            Str(s) if decoded.tag() == Some(TAG_BASE64) => base64::decode(s).ok().map(Cow::Owned),
            Str(s) if decoded.tag() == Some(TAG_BASE64URL) => {
                base64::decode_config(s, base64::URL_SAFE)
                    .ok()
                    .map(Cow::Owned)
            }
            _ => None,
        }
    }

    /// Try to interpret this value as string.
    ///
    /// Returns None if the type is not a (byte) string or the bytes are not valid UTF-8.
    /// base64 encoded strings (TAG_BASE64 or TAG_BASE64URL) are not decoded but returned
    /// as they are (even when their binary decoded form is valid UTF-8).
    pub fn as_str(&self) -> Option<&'a str> {
        let decoded = self.decoded()?;
        let tag = decoded.tag();
        match self.kind {
            Str(s) => Some(s),
            Bytes(b) if tag != Some(TAG_BIGNUM_POS) && tag != Some(TAG_BIGNUM_NEG) => {
                std::str::from_utf8(b).ok()
            }
            _ => None,
        }
    }

    /// Lift a representation from this CBOR item that turns arrays into vectors and
    /// dicts into BTreeMaps.
    ///
    /// This method is mostly useful for diagnostics and tests.
    pub fn as_object(&self) -> Option<CborObject<'a>> {
        let decoded = self.decoded()?;
        match decoded.kind {
            Array => {
                let info = integer(decoded.bytes);
                let rest = info.map(|x| x.2).unwrap_or_else(|| &decoded.bytes[1..]);
                let len = info.map(|x| x.0);
                let iter = Iter::new(rest, len);
                let mut v = Vec::new();
                for i in iter {
                    v.push(i.value()?.as_object()?);
                }
                Some(CborObject::Array(v))
            }
            Dict => {
                let info = integer(decoded.bytes);
                let rest = info.map(|x| x.2).unwrap_or_else(|| &decoded.bytes[1..]);
                let len = info.map(|x| x.0 * 2);
                let mut iter = Iter::new(rest, len);
                let mut m = BTreeMap::new();
                while let Some(c) = iter.next() {
                    if let Str(key) = c.value()?.kind {
                        let value = iter.next()?.value()?.as_object()?;
                        m.insert(key, value);
                    } else {
                        return None;
                    }
                }
                Some(CborObject::Dict(m))
            }
            _ => Some(CborObject::Value(decoded.tag(), decoded.kind)),
        }
    }
}

fn bytes_to_float(bytes: &[u8]) -> f64 {
    let mut ret = 0.0;
    for x in bytes {
        ret = ret * 256.0 + (*x as f64);
    }
    ret
}

#[cfg(test)]
mod tests {
    use crate::CborOwned;

    use super::*;

    #[test]
    fn base64string() {
        fn to_cbor(s: &str, tag: u64) -> CborOwned {
            let mut v = vec![0xd8u8, tag as u8, 0x60 | (s.len() as u8)];
            v.extend_from_slice(s.as_bytes());
            CborOwned::trusting(v)
        }
        fn b(bytes: &CborOwned) -> Vec<u8> {
            bytes
                .value()
                .expect("a")
                .as_bytes()
                .expect("b")
                .into_owned()
        }

        let bytes = to_cbor("a346_-0=", TAG_BASE64URL);
        assert_eq!(b(&bytes), vec![107, 126, 58, 255, 237]);

        let bytes = to_cbor("a346_-0", TAG_BASE64URL);
        assert_eq!(b(&bytes), vec![107, 126, 58, 255, 237]);

        let bytes = to_cbor("a346/+0=", TAG_BASE64);
        assert_eq!(b(&bytes), vec![107, 126, 58, 255, 237]);

        let bytes = to_cbor("a346/+0", TAG_BASE64);
        assert_eq!(b(&bytes), vec![107, 126, 58, 255, 237]);
    }
}