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 constexpr Currency sIsoBits(
59  "FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF");
60 
61  if ((currency & sIsoBits).isZero())
62  {
63  std::string const iso(
64  currency.data() + detail::isoCodeOffset,
66 
67  // Specifying the system currency code using ISO-style representation
68  // is not allowed.
69  if ((iso != systemCurrencyCode()) &&
70  (iso.find_first_not_of(detail::isoCharSet) == std::string::npos))
71  {
72  return iso;
73  }
74  }
75 
76  return strHex(currency);
77 }
78 
79 bool
80 to_currency(Currency& currency, std::string const& code)
81 {
82  if (code.empty() || !code.compare(systemCurrencyCode()))
83  {
84  currency = beast::zero;
85  return true;
86  }
87 
88  // Handle ISO-4217-like 3-digit character codes.
89  if (code.size() == detail::isoCodeLength)
90  {
91  if (code.find_first_not_of(detail::isoCharSet) != std::string::npos)
92  return false;
93 
94  currency = beast::zero;
95 
96  std::copy(
97  code.begin(), code.end(), currency.begin() + detail::isoCodeOffset);
98 
99  return true;
100  }
101 
102  return currency.parseHex(code);
103 }
104 
105 Currency
107 {
108  Currency currency;
109  if (!to_currency(currency, code))
110  currency = noCurrency();
111  return currency;
112 }
113 
114 Currency const&
116 {
117  static Currency const currency(beast::zero);
118  return currency;
119 }
120 
121 Currency const&
123 {
124  static Currency const currency(1);
125  return currency;
126 }
127 
128 Currency const&
130 {
131  static Currency const currency(0x5852500000000000);
132  return currency;
133 }
134 
135 } // 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:129
ripple::systemCurrencyCode
static std::string const & systemCurrencyCode()
Definition: SystemParameters.h:62
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:80
std::string
STL class.
ripple::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:122
std::string_view
STL class.
std::string::size
T size(T... args)
ripple::detail::isoCodeLength
constexpr std::size_t isoCodeLength
Definition: UintTypes.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:122
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
std::string::compare
T compare(T... args)
std::copy
T copy(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::begin
iterator begin()
Definition: base_uint.h:133
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::to_string
std::string to_string(Manifest const &m)
Format the specified manifest to a string for debugging purposes.
Definition: app/misc/impl/Manifest.cpp:41
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:30
std::string::end
T end(T... args)
ripple::base_uint::parseHex
constexpr bool parseHex(std::string_view sv)
Parse a hex string into a base_uint.
Definition: base_uint.h:496
string_view
ripple::xrpCurrency
Currency const & xrpCurrency()
XRP currency.
Definition: UintTypes.cpp:115