| 1 |
use std::collections::BTreeMap; |
| 2 |
use CborType; |
| 3 |
|
| 4 |
/// Given a vector of bytes to append to, a tag to use, and an unsigned value to encode, uses the |
| 5 |
/// CBOR unsigned integer encoding to represent the given value. |
| 6 |
fn common_encode_unsigned(output: &mut Vec<u8>, tag: u8, value: u64) { |
| 7 |
assert!(tag < 8); |
| 8 |
let shifted_tag = tag << 5; |
| 9 |
match value { |
| 10 |
0..=23 => { |
| 11 |
output.push(shifted_tag | (value as u8)); |
| 12 |
} |
| 13 |
24..=255 => { |
| 14 |
output.push(shifted_tag | 24); |
| 15 |
output.push(value as u8); |
| 16 |
} |
| 17 |
256..=65_535 => { |
| 18 |
output.push(shifted_tag | 25); |
| 19 |
output.push((value >> 8) as u8); |
| 20 |
output.push((value & 255) as u8); |
| 21 |
} |
| 22 |
65_536..=4_294_967_295 => { |
| 23 |
output.push(shifted_tag | 26); |
| 24 |
output.push((value >> 24) as u8); |
| 25 |
output.push(((value >> 16) & 255) as u8); |
| 26 |
output.push(((value >> 8) & 255) as u8); |
| 27 |
output.push((value & 255) as u8); |
| 28 |
} |
| 29 |
_ => { |
| 30 |
output.push(shifted_tag | 27); |
| 31 |
output.push((value >> 56) as u8); |
| 32 |
output.push(((value >> 48) & 255) as u8); |
| 33 |
output.push(((value >> 40) & 255) as u8); |
| 34 |
output.push(((value >> 32) & 255) as u8); |
| 35 |
output.push(((value >> 24) & 255) as u8); |
| 36 |
output.push(((value >> 16) & 255) as u8); |
| 37 |
output.push(((value >> 8) & 255) as u8); |
| 38 |
output.push((value & 255) as u8); |
| 39 |
} |
| 40 |
}; |
| 41 |
} |
| 42 |
|
| 43 |
/// The major type is 0. For values 0 through 23, the 5 bits of additional information is just the |
| 44 |
/// value of the unsigned number. For values representable in one byte, the additional information |
| 45 |
/// has the value 24. If two bytes are necessary, the value is 25. If four bytes are necessary, the |
| 46 |
/// value is 26. If 8 bytes are necessary, the value is 27. The following bytes are the value of the |
| 47 |
/// unsigned number in as many bytes were indicated in network byte order (big endian). |
| 48 |
fn encode_unsigned(output: &mut Vec<u8>, unsigned: u64) { |
| 49 |
common_encode_unsigned(output, 0, unsigned); |
| 50 |
} |
| 51 |
|
| 52 |
/// The major type is 1. The encoding is the same as for positive (i.e. unsigned) integers, except |
| 53 |
/// the value encoded is -1 minus the value of the negative number. |
| 54 |
fn encode_negative(output: &mut Vec<u8>, negative: i64) { |
| 55 |
assert!(negative < 0); |
| 56 |
let value_to_encode: u64 = (-1 - negative) as u64; |
| 57 |
common_encode_unsigned(output, 1, value_to_encode); |
| 58 |
} |
| 59 |
|
| 60 |
/// The major type is 2. The length of the data is encoded as with positive integers, followed by |
| 61 |
/// the actual data. |
| 62 |
fn encode_bytes(output: &mut Vec<u8>, bstr: &[u8]) { |
| 63 |
common_encode_unsigned(output, 2, bstr.len() as u64); |
| 64 |
output.extend_from_slice(bstr); |
| 65 |
} |
| 66 |
|
| 67 |
/// The major type is 3. The length is as with bstr. The UTF-8-encoded bytes of the string follow. |
| 68 |
fn encode_string(output: &mut Vec<u8>, tstr: &str) { |
| 69 |
let utf8_bytes = tstr.as_bytes(); |
| 70 |
common_encode_unsigned(output, 3, utf8_bytes.len() as u64); |
| 71 |
output.extend_from_slice(utf8_bytes); |
| 72 |
} |
| 73 |
|
| 74 |
/// The major type is 4. The number of items is encoded as with positive integers. Then follows the |
| 75 |
/// encodings of the items themselves. |
| 76 |
fn encode_array(output: &mut Vec<u8>, array: &[CborType]) { |
| 77 |
common_encode_unsigned(output, 4, array.len() as u64); |
| 78 |
for element in array { |
| 79 |
output.append(&mut element.serialize()); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/// The major type is 5. The number of pairs is encoded as with positive integers. Then follows the |
| 84 |
/// encodings of each key, value pair. In Canonical CBOR, the keys must be sorted lowest value to |
| 85 |
/// highest. |
| 86 |
fn encode_map(output: &mut Vec<u8>, map: &BTreeMap<CborType, CborType>) { |
| 87 |
common_encode_unsigned(output, 5, map.len() as u64); |
| 88 |
for (key, value) in map { |
| 89 |
output.append(&mut key.serialize()); |
| 90 |
output.append(&mut value.serialize()); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
fn encode_tag(output: &mut Vec<u8>, tag: &u64, val: &CborType) { |
| 95 |
common_encode_unsigned(output, 6, *tag); |
| 96 |
output.append(&mut val.serialize()); |
| 97 |
} |
| 98 |
|
| 99 |
/// The major type is 7. The only supported value for this type is 22, which is Null. |
| 100 |
/// This makes the encoded value 246, or 0xf6. |
| 101 |
fn encode_null(output: &mut Vec<u8>) { |
| 102 |
output.push(0xf6); |
| 103 |
} |
| 104 |
|
| 105 |
impl CborType { |
| 106 |
/// Serialize a Cbor object. NB: if the object to be serialized consists of too many nested |
| 107 |
/// Cbor objects (for example, Array(Array(Array(...(Array(0))))), it is possible that calling |
| 108 |
/// serialize will exhaust the stack. It is considered the caller's responsibility to prevent |
| 109 |
/// this. |
| 110 |
pub fn serialize(&self) -> Vec<u8> { |
| 111 |
let mut bytes: Vec<u8> = Vec::new(); |
| 112 |
match *self { |
| 113 |
CborType::Integer(ref unsigned) => encode_unsigned(&mut bytes, *unsigned), |
| 114 |
CborType::SignedInteger(ref negative) => encode_negative(&mut bytes, *negative), |
| 115 |
CborType::Bytes(ref bstr) => encode_bytes(&mut bytes, bstr), |
| 116 |
CborType::String(ref tstr) => encode_string(&mut bytes, tstr), |
| 117 |
CborType::Array(ref arr) => encode_array(&mut bytes, arr), |
| 118 |
CborType::Map(ref map) => encode_map(&mut bytes, map), |
| 119 |
CborType::Tag(ref t, ref val) => encode_tag(&mut bytes, t, val), |
| 120 |
CborType::Null => encode_null(&mut bytes), |
| 121 |
}; |
| 122 |
bytes |
| 123 |
} |
| 124 |
} |