rippled
Loading...
Searching...
No Matches
UintTypes.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2014 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#include <xrpl/basics/strHex.h>
21#include <xrpl/beast/utility/Zero.h>
22#include <xrpl/protocol/SystemParameters.h>
23#include <xrpl/protocol/UintTypes.h>
24
25#include <algorithm>
26#include <cstddef>
27#include <string>
28#include <string_view>
29
30namespace ripple {
31
32// For details on the protocol-level serialization please visit
33// https://xrpl.org/serialization.html#currency-codes
34
35namespace detail {
36
37// Characters we are willing to allow in the ASCII representation of a
38// three-letter currency code.
40 "abcdefghijklmnopqrstuvwxyz"
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "0123456789"
43 "<>(){}[]|?!@#$%^&*";
44
45// The location (in bytes) of the 3 digit currency inside a 160-bit value
47
48// The length of an ISO-4217 like code
50
51} // namespace detail
52
54to_string(Currency const& currency)
55{
56 if (currency == beast::zero)
57 return systemCurrencyCode();
58
59 if (currency == noCurrency())
60 return "1";
61
62 static constexpr Currency sIsoBits(
63 "FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF");
64
65 if ((currency & sIsoBits).isZero())
66 {
67 std::string const iso(
68 currency.data() + detail::isoCodeOffset,
70
71 // Specifying the system currency code using ISO-style representation
72 // is not allowed.
73 if ((iso != systemCurrencyCode()) &&
74 (iso.find_first_not_of(detail::isoCharSet) == std::string::npos))
75 {
76 return iso;
77 }
78 }
79
80 return strHex(currency);
81}
82
83bool
84to_currency(Currency& currency, std::string const& code)
85{
86 if (code.empty() || !code.compare(systemCurrencyCode()))
87 {
88 currency = beast::zero;
89 return true;
90 }
91
92 // Handle ISO-4217-like 3-digit character codes.
93 if (code.size() == detail::isoCodeLength)
94 {
95 if (code.find_first_not_of(detail::isoCharSet) != std::string::npos)
96 return false;
97
98 currency = beast::zero;
99
100 std::copy(
101 code.begin(), code.end(), currency.begin() + detail::isoCodeOffset);
102
103 return true;
104 }
105
106 return currency.parseHex(code);
107}
108
111{
112 Currency currency;
113 if (!to_currency(currency, code))
114 currency = noCurrency();
115 return currency;
116}
117
118Currency const&
120{
121 static Currency const currency(beast::zero);
122 return currency;
123}
124
125Currency const&
127{
128 static Currency const currency(1);
129 return currency;
130}
131
132Currency const&
134{
135 static Currency const currency(0x5852500000000000);
136 return currency;
137}
138
139} // namespace ripple
T begin(T... args)
iterator begin()
Definition base_uint.h:136
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition base_uint.h:503
T compare(T... args)
T copy(T... args)
T empty(T... args)
T end(T... args)
T find_first_not_of(T... args)
constexpr std::string_view isoCharSet
Definition UintTypes.cpp:39
constexpr std::size_t isoCodeOffset
Definition UintTypes.cpp:46
constexpr std::size_t isoCodeLength
Definition UintTypes.cpp:49
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Currency const & badCurrency()
We deliberately disallow the currency that looks like "XRP" because too many people were using it ins...
Currency const & noCurrency()
A placeholder for empty currencies.
static std::string const & systemCurrencyCode()
std::string strHex(FwdIt begin, FwdIt end)
Definition strHex.h:30
base_uint< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:56
Currency const & xrpCurrency()
XRP currency.
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:630
bool to_currency(Currency &, std::string const &)
Tries to convert a string to a Currency, returns true on success.
Definition UintTypes.cpp:84
T size(T... args)