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