NicheC commited on
Commit
8522c10
·
verified ·
1 Parent(s): 7234cc9

Upload CTID.h

Browse files
Files changed (1) hide show
  1. CTID.h +88 -0
CTID.h ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ //------------------------------------------------------------------------------
2
+ /*
3
+ This file is part of rippled: https://github.com/ripple/rippled
4
+ Copyright (c) 2019 Ripple Labs Inc.
5
+
6
+ Permission to use, copy, modify, and/or distribute this software for any
7
+ purpose with or without fee is hereby granted, provided that the above
8
+ copyright notice and this permission notice appear in all copies.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
+ ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
+ */
18
+ //==============================================================================
19
+
20
+ #ifndef RIPPLE_RPC_CTID_H_INCLUDED
21
+ #define RIPPLE_RPC_CTID_H_INCLUDED
22
+
23
+ #include <boost/algorithm/string/predicate.hpp>
24
+ #include <boost/regex.hpp>
25
+ #include <optional>
26
+ #include <regex>
27
+ #include <sstream>
28
+
29
+ namespace ripple {
30
+
31
+ namespace RPC {
32
+
33
+ inline std::optional<std::string>
34
+ encodeCTID(
35
+ uint32_t ledger_seq,
36
+ uint16_t txn_index,
37
+ uint16_t network_id) noexcept
38
+ {
39
+ if (ledger_seq > 0x0FFF'FFFF)
40
+ return {};
41
+
42
+ uint64_t ctidValue =
43
+ ((0xC000'0000ULL + static_cast<uint64_t>(ledger_seq)) << 32) +
44
+ (static_cast<uint64_t>(txn_index) << 16) + network_id;
45
+
46
+ std::stringstream buffer;
47
+ buffer << std::hex << std::uppercase << std::setfill('0') << std::setw(16)
48
+ << ctidValue;
49
+ return {buffer.str()};
50
+ }
51
+
52
+ template <typename T>
53
+ inline std::optional<std::tuple<uint32_t, uint16_t, uint16_t>>
54
+ decodeCTID(const T ctid) noexcept
55
+ {
56
+ uint64_t ctidValue{0};
57
+ if constexpr (
58
+ std::is_same_v<T, std::string> || std::is_same_v<T, char*> ||
59
+ std::is_same_v<T, const char*> || std::is_same_v<T, std::string_view>)
60
+ {
61
+ std::string const ctidString(ctid);
62
+
63
+ if (ctidString.length() != 16)
64
+ return {};
65
+
66
+ if (!boost::regex_match(ctidString, boost::regex("^[0-9A-F]+$")))
67
+ return {};
68
+
69
+ ctidValue = std::stoull(ctidString, nullptr, 16);
70
+ }
71
+ else if constexpr (std::is_integral_v<T>)
72
+ ctidValue = ctid;
73
+ else
74
+ return {};
75
+
76
+ if ((ctidValue & 0xF000'0000'0000'0000ULL) != 0xC000'0000'0000'0000ULL)
77
+ return {};
78
+
79
+ uint32_t ledger_seq = (ctidValue >> 32) & 0xFFFF'FFFUL;
80
+ uint16_t txn_index = (ctidValue >> 16) & 0xFFFFU;
81
+ uint16_t network_id = ctidValue & 0xFFFFU;
82
+ return {{ledger_seq, txn_index, network_id}};
83
+ }
84
+
85
+ } // namespace RPC
86
+ } // namespace ripple
87
+
88
+ #endif