CCF
Loading...
Searching...
No Matches
byte_vector.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#pragma once
4
5#include "ccf/ds/siphash.h"
6
7#define FMT_HEADER_ONLY
8#include <climits>
9#include <fmt/format.h>
10#include <fmt/ranges.h>
11#include <small_vector/SmallVector.h>
12
13namespace ccf
14{
15 using ByteVector = llvm_vecsmall::SmallVector<uint8_t, CHAR_BIT>;
16}
17
18// NOLINTBEGIN(cert-dcl58-cpp)
19namespace std
20{
21 template <typename T, unsigned N>
22 struct hash<llvm_vecsmall::SmallVector<T, N>>
23 {
24 size_t operator()(const llvm_vecsmall::SmallVector<T, N>& v) const
25 {
26 static constexpr ccf::siphash::SipKey k{
27 0x7720796f726c694b, 0x2165726568207361};
28 return ccf::siphash::siphash<2, 4>(v.data(), v.size(), k);
29 }
30 };
31}
32// NOLINTEND(cert-dcl58-cpp)
33
34FMT_BEGIN_NAMESPACE
35template <>
36struct formatter<ccf::ByteVector>
37{
38 template <typename ParseContext>
39 constexpr auto parse(ParseContext& ctx)
40 {
41 return ctx.begin();
42 }
43
44 template <typename FormatContext>
45 auto format(const ccf::ByteVector& e, FormatContext& ctx) const
46 {
47 // This is the same as std::isprint, but independent of the current locale.
48 constexpr auto first_printable = 0x20;
49 constexpr auto last_printable = 0x7e;
50 auto printable = [](uint8_t b) {
51 return b >= first_printable && b <= last_printable;
52 };
53 if (std::all_of(e.begin(), e.end(), printable))
54 {
55 return format_to(
56 ctx.out(),
57 "<uint8[{}]: ascii={}>",
58 e.size(),
59 std::string(e.begin(), e.end()));
60 }
61 return format_to(
62 ctx.out(), "<uint8[{}]: hex={:02x}>", e.size(), fmt::join(e, " "));
63 }
64};
65FMT_END_NAMESPACE
uint64_t[2] SipKey
Definition siphash.h:15
Definition app_interface.h:14
llvm_vecsmall::SmallVector< uint8_t, CHAR_BIT > ByteVector
Definition byte_vector.h:15
STL namespace.
constexpr auto parse(ParseContext &ctx)
Definition byte_vector.h:39
auto format(const ccf::ByteVector &e, FormatContext &ctx) const
Definition byte_vector.h:45
size_t operator()(const llvm_vecsmall::SmallVector< T, N > &v) const
Definition byte_vector.h:24