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
14namespace ccf::cbor
15{
16 struct ValueImpl;
17 using Value = std::unique_ptr<ValueImpl>;
18
19 using Unsigned = uint64_t;
20 using Signed = int64_t;
21 using Bytes = std::span<const uint8_t>;
22 using String = std::string_view;
23 using Simple = uint8_t;
24
25 struct Array
26 {
27 std::vector<Value> items;
28 };
29
30 struct Map
31 {
32 std::vector<std::pair<Value, Value>> items;
33 };
34
35 struct Tagged
36 {
37 uint64_t tag{0};
38 Value item{nullptr};
39 };
40
41 using Type =
42 std::variant<Unsigned, Signed, Bytes, String, Array, Map, Tagged, Simple>;
43
44 using CBORDecodeError = std::runtime_error;
45
46 struct ValueImpl
47 {
48 ValueImpl(Type value_) : value(std::move(value_)) {}
50
51 [[nodiscard]] const Value& array_at(
52 size_t index, std::string_view context = {}) const;
53 [[nodiscard]] const Value& map_at(
54 const Value& key, std::string_view context = {}) const;
55 [[nodiscard]] const Value& tag_at(
56 uint64_t tag, std::string_view context = {}) const;
57 [[nodiscard]] Unsigned as_unsigned(std::string_view context = {}) const;
58 [[nodiscard]] Signed as_signed(std::string_view context = {}) const;
59 [[nodiscard]] Bytes as_bytes(std::string_view context = {}) const;
60 [[nodiscard]] String as_string(std::string_view context = {}) const;
61 [[nodiscard]] Simple as_simple(std::string_view context = {}) const;
62 [[nodiscard]] size_t size() const;
63 };
64
65 Value make_unsigned(uint64_t value);
66 Value make_signed(int64_t value);
67 Value make_string(std::string_view data);
68
70 std::span<const uint8_t> raw, std::string_view context = {});
71 std::string print_value(const Value& value, size_t indent = 0);
72} // namespace ccf::cbor
Definition cbor.cpp:278
std::variant< Unsigned, Signed, Bytes, String, Array, Map, Tagged, Simple > Type
Definition cbor.h:42
int64_t Signed
Definition cbor.h:20
Value make_signed(int64_t value)
Definition cbor.cpp:283
Value make_unsigned(uint64_t value)
Definition cbor.cpp:279
std::string_view String
Definition cbor.h:22
std::string print_value(const Value &value, size_t indent)
Definition cbor.cpp:314
std::runtime_error CBORDecodeError
Definition cbor.h:44
Value make_string(std::string_view data)
Definition cbor.cpp:287
uint64_t Unsigned
Definition cbor.h:19
std::span< const uint8_t > Bytes
Definition cbor.h:21
Value parse_value(std::span< const uint8_t > raw, std::string_view context)
Definition cbor.cpp:292
std::unique_ptr< ValueImpl > Value
Definition cbor.h:17
uint8_t Simple
Definition cbor.h:23
STL namespace.
Definition cbor.h:26
std::vector< Value > items
Definition cbor.h:27
Definition cbor.h:31
std::vector< std::pair< Value, Value > > items
Definition cbor.h:32
Definition cbor.h:36
Value item
Definition cbor.h:38
uint64_t tag
Definition cbor.h:37
Definition cbor.h:47
const Value & map_at(const Value &key, std::string_view context={}) const
Definition cbor.cpp:339
Simple as_simple(std::string_view context={}) const
Definition cbor.cpp:476
Type value
Definition cbor.h:49
Bytes as_bytes(std::string_view context={}) const
Definition cbor.cpp:456
ValueImpl(Type value_)
Definition cbor.h:48
Unsigned as_unsigned(std::string_view context={}) const
Definition cbor.cpp:436
Signed as_signed(std::string_view context={}) const
Definition cbor.cpp:446
const Value & array_at(size_t index, std::string_view context={}) const
Definition cbor.cpp:321
const Value & tag_at(uint64_t tag, std::string_view context={}) const
Definition cbor.cpp:418
String as_string(std::string_view context={}) const
Definition cbor.cpp:466
size_t size() const
Definition cbor.cpp:403