CCF
Loading...
Searching...
No Matches
cbor.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3
4#pragma once
5
6#include <cstdint>
7#include <exception>
8#include <memory>
9#include <span>
10#include <string>
11#include <variant>
12#include <vector>
13
14#define FMT_HEADER_ONLY
15#include <fmt/format.h>
16
17namespace ccf::cbor
18{
19 namespace tag
20 {
21 // https://www.rfc-editor.org/rfc/rfc8152.html#section-2
22 static constexpr int64_t COSE_SIGN_1 = 18;
23 }
24
25 struct ValueImpl;
26 using Value = std::shared_ptr<ValueImpl>;
27
28 using Signed = int64_t;
29 using Bytes = std::span<const uint8_t>;
30 using String = std::string_view;
31 using Simple = uint8_t;
32
33 // https://www.iana.org/assignments/cbor-simple-values/cbor-simple-values.xhtml.
34 //
35 // To be filled further on demand, currently only those to be (likely) used.
36 enum SimpleValue : uint8_t
37 {
38 False = 20,
39 True = 21,
40 Null = 22,
42 };
43
44 struct Array
45 {
46 std::vector<Value> items;
47 };
48
49 using MapItem = std::pair<Value, Value>;
50 struct Map
51 {
52 std::vector<MapItem> items;
53 };
54
55 struct Tagged
56 {
57 uint64_t tag{0};
58 Value item{nullptr};
59 };
60
61 using Type = std::variant<Signed, Bytes, String, Array, Map, Tagged, Simple>;
62
63 enum class Error : uint8_t
64 {
65 UNDEFINED = 0,
66 DECODE_FAILED = 1,
67 KEY_NOT_FOUND = 2,
68 OUT_OF_BOUND = 3,
69 TYPE_MISMATCH = 4,
70 ENCODE_FAILED = 5,
71 };
72
73 class CBOREncodeError : public std::runtime_error
74 {
75 public:
76 explicit CBOREncodeError(Error err, const std::string& what);
77 [[nodiscard]] Error error_code() const;
78
79 private:
81 };
82
83 class CBORDecodeError : public std::runtime_error
84 {
85 public:
86 explicit CBORDecodeError(Error err, const std::string& what);
87 [[nodiscard]] Error error_code() const;
88
89 private:
91 };
92
93 struct ValueImpl
94 {
95 ValueImpl(Type value_) : value(std::move(value_)) {}
97
98 [[nodiscard]] const Value& array_at(size_t index) const;
99 [[nodiscard]] const Value& map_at(const Value& key) const;
100 [[nodiscard]] const Value& tag_at(uint64_t tag) const;
101 [[nodiscard]] Signed as_signed() const;
102 [[nodiscard]] Bytes as_bytes() const;
103 [[nodiscard]] String as_string() const;
104 [[nodiscard]] Simple as_simple() const;
105 [[nodiscard]] size_t size() const;
106 };
107
108 Value make_signed(int64_t value);
109 Value make_simple(SimpleValue value);
110 Value make_string(std::string_view data);
111 Value make_bytes(std::span<const uint8_t> data);
112 Value make_tagged(uint64_t tag, Value&& value);
113 Value make_array(std::vector<Value>&& data);
114 Value make_map(std::vector<MapItem>&& data);
115
116 Value parse(std::span<const uint8_t> raw, size_t max_depth = 16);
117 std::vector<uint8_t> serialize(const Value& value, size_t max_depth = 16);
118
119 std::string to_string(const Value& value);
120 bool simple_to_boolean(const Simple& value);
121 SimpleValue boolean_to_simple(bool value);
122
123 decltype(auto) rethrow_with_msg(auto&& f, std::string_view msg = {})
124 {
125 try
126 {
127 return f();
128 }
129 catch (const CBORDecodeError& err)
130 {
131 if (!msg.empty())
132 {
133 throw CBORDecodeError(
134 err.error_code(), fmt::format("{}: {}", msg, err.what()));
135 }
136 throw err;
137 }
138 }
139} // namespace ccf::cbor
Definition cbor.h:84
Error error_code() const
Definition cbor.cpp:519
Definition cbor.h:74
Error error_code() const
Definition cbor.cpp:509
Definition cbor.cpp:503
int64_t Signed
Definition cbor.h:28
std::string_view String
Definition cbor.h:30
Error
Definition cbor.h:64
decltype(auto) rethrow_with_msg(auto &&f, std::string_view msg={})
Definition cbor.h:123
std::shared_ptr< ValueImpl > Value
Definition cbor.h:26
std::variant< Signed, Bytes, String, Array, Map, Tagged, Simple > Type
Definition cbor.h:61
std::span< const uint8_t > Bytes
Definition cbor.h:29
SimpleValue
Definition cbor.h:37
@ Undefined
Definition cbor.h:41
@ False
Definition cbor.h:38
@ True
Definition cbor.h:39
@ Null
Definition cbor.h:40
std::pair< Value, Value > MapItem
Definition cbor.h:49
uint8_t Simple
Definition cbor.h:31
STL namespace.
Definition cbor.h:45
std::vector< Value > items
Definition cbor.h:46
Definition cbor.h:51
std::vector< MapItem > items
Definition cbor.h:52
Definition cbor.h:56
Value item
Definition cbor.h:58
uint64_t tag
Definition cbor.h:57
Definition cbor.h:94
Type value
Definition cbor.h:96
const Value & array_at(size_t index) const
Definition cbor.cpp:644
Bytes as_bytes() const
Definition cbor.cpp:761
ValueImpl(Type value_)
Definition cbor.h:95
String as_string() const
Definition cbor.cpp:770
Simple as_simple() const
Definition cbor.cpp:779
const Value & tag_at(uint64_t tag) const
Definition cbor.cpp:736
size_t size() const
Definition cbor.cpp:721
const Value & map_at(const Value &key) const
Definition cbor.cpp:660
Signed as_signed() const
Definition cbor.cpp:752