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 
25 namespace ripple {
26 
28 to_string(Currency const& currency)
29 {
30  // Characters we are willing to allow in the ASCII representation of a
31  // three-letter currency code.
32  static std::string const allowed_characters =
33  "abcdefghijklmnopqrstuvwxyz"
34  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35  "0123456789"
36  "<>(){}[]|?!@#$%^&*";
37 
38  if (currency == beast::zero)
39  return systemCurrencyCode();
40 
41  if (currency == noCurrency())
42  return "1";
43 
44  static Currency const sIsoBits(
45  from_hex_text<Currency>("FFFFFFFFFFFFFFFFFFFFFFFF000000FFFFFFFFFF"));
46 
47  if ((currency & sIsoBits).isZero())
48  {
49  // The offset of the 3 character ISO code in the currency descriptor
50  int const isoOffset = 12;
51 
52  std::string const iso(
53  currency.data() + isoOffset, currency.data() + isoOffset + 3);
54 
55  // Specifying the system currency code using ISO-style representation
56  // is not allowed.
57  if ((iso != systemCurrencyCode()) &&
58  (iso.find_first_not_of(allowed_characters) == std::string::npos))
59  {
60  return iso;
61  }
62  }
63 
64  return strHex(currency);
65 }
66 
67 bool
68 to_currency(Currency& currency, std::string const& code)
69 {
70  if (code.empty() || !code.compare(systemCurrencyCode()))
71  {
72  currency = beast::zero;
73  return true;
74  }
75 
76  static const int CURRENCY_CODE_LENGTH = 3;
77  if (code.size() == CURRENCY_CODE_LENGTH)
78  {
79  Blob codeBlob(CURRENCY_CODE_LENGTH);
80 
81  std::transform(code.begin(), code.end(), codeBlob.begin(), [](auto c) {
82  return ::toupper(static_cast<unsigned char>(c));
83  });
84 
85  Serializer s;
86 
87  s.addZeros(96 / 8);
88  s.addRaw(codeBlob);
89  s.addZeros(16 / 8);
90  s.addZeros(24 / 8);
91 
92  s.get160(currency, 0);
93  return true;
94  }
95 
96  if (40 == code.size())
97  return currency.SetHex(code);
98 
99  return false;
100 }
101 
102 Currency
104 {
105  Currency currency;
106  if (!to_currency(currency, code))
107  currency = noCurrency();
108  return currency;
109 }
110 
111 Currency const&
113 {
114  static Currency const currency(beast::zero);
115  return currency;
116 }
117 
118 Currency const&
120 {
121  static Currency const currency(1);
122  return currency;
123 }
124 
125 Currency const&
127 {
128  static Currency const currency(0x5852500000000000);
129  return currency;
130 }
131 
132 } // 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:126
ripple::systemCurrencyCode
static std::string const & systemCurrencyCode()
Definition: SystemParameters.h:53
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:68
std::string
STL class.
ripple::noCurrency
Currency const & noCurrency()
A placeholder for empty currencies.
Definition: UintTypes.cpp:119
std::vector< unsigned char >
std::string::size
T size(T... args)
ripple::to_string
std::string to_string(ListDisposition disposition)
Definition: ValidatorList.cpp:41
ripple::base_uint< 160, detail::CurrencyTag >
std::string::compare
T compare(T... args)
ripple::Serializer::addRaw
int addRaw(Blob const &vector)
Definition: Serializer.cpp:127
ripple::base_uint::SetHex
bool SetHex(const char *psz, bool bStrict=false)
Parse a hex string into a base_uint The input can be:
Definition: base_uint.h:406
std::transform
T transform(T... args)
ripple::Serializer::get160
bool get160(base_uint< 160, Tag > &o, int offset) const
Definition: Serializer.h:173
ripple::Serializer
Definition: Serializer.h:43
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::Serializer::addZeros
int addZeros(size_t uBytes)
Definition: Serializer.cpp:29
std::string::begin
T begin(T... args)
std::string::empty
T empty(T... args)
ripple::strHex
std::string strHex(FwdIt begin, FwdIt end)
Definition: strHex.h:67
std::string::end
T end(T... args)
ripple::xrpCurrency
Currency const & xrpCurrency()
XRP currency.
Definition: UintTypes.cpp:112