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