Fabcoin Core  0.16.2
P2P Digital Currency
CommonJS.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #include "CommonJS.h"
25 
26 using namespace std;
27 
28 namespace dev
29 {
30 
31 bytes jsToBytes(string const& _s, OnFailed _f)
32 {
33  try
34  {
35  return fromHex(_s, WhenError::Throw);
36  }
37  catch (...)
38  {
39  if (_f == OnFailed::InterpretRaw)
40  return asBytes(_s);
41  else if (_f == OnFailed::Throw)
42  throw invalid_argument("Cannot intepret '" + _s + "' as bytes; must be 0x-prefixed hex or decimal.");
43  }
44  return bytes();
45 }
46 
47 bytes padded(bytes _b, unsigned _l)
48 {
49  while (_b.size() < _l)
50  _b.insert(_b.begin(), 0);
51  return asBytes(asString(_b).substr(_b.size() - max(_l, _l)));
52 }
53 
54 bytes paddedRight(bytes _b, unsigned _l)
55 {
56  _b.resize(_l);
57  return _b;
58 }
59 
61 {
62  auto p = asString(_b).find_last_not_of((char)0);
63  _b.resize(p == string::npos ? 0 : (p + 1));
64  return _b;
65 }
66 
68 {
69  unsigned int i = 0;
70  if (_b.size() == 0)
71  return _b;
72 
73  while (i < _b.size() && _b[i] == byte(0))
74  i++;
75 
76  if (i != 0)
77  _b.erase(_b.begin(), _b.begin() + i);
78  return _b;
79 }
80 
81 string fromRaw(h256 _n)
82 {
83  if (_n)
84  {
85  string s((char const*)_n.data(), 32);
86  auto l = s.find_first_of('\0');
87  if (!l)
88  return "";
89  if (l != string::npos)
90  s.resize(l);
91  for (auto i: s)
92  if (i < 32)
93  return "";
94  return s;
95  }
96  return "";
97 }
98 
99 }
100 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
byte * data()
Definition: FixedHash.h:139
OnFailed
Definition: CommonJS.h:68
std::hash for asio::adress
Definition: Common.h:323
bytes unpadded(bytes _b)
Removing all trailing &#39;0&#39;. Returns empty array if input contains only &#39;0&#39; char.
Definition: CommonJS.cpp:60
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
string fromRaw(h256 _n)
Convert h256 into user-readable string (by directly using std::string constructor). If it can&#39;t be interpreted as an ASCII string, empty string is returned.
Definition: CommonJS.cpp:81
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string&#39;s (byte) data.
Definition: CommonData.h:92
bytes paddedRight(bytes _b, unsigned _l)
Add &#39;0&#39; on, or remove items from, the back of _b until it is of length _l.
Definition: CommonJS.cpp:54
bytes jsToBytes(string const &_s, OnFailed _f)
Definition: CommonJS.cpp:31
uint8_t byte
Definition: Common.h:10
bytes padded(bytes _b, unsigned _l)
Add &#39;0&#39; on, or remove items from, the front of _b until it is of length _l.
Definition: CommonJS.cpp:47
dev::WithExisting max(dev::WithExisting _a, dev::WithExisting _b)
Definition: Common.h:326
bytes unpadLeft(bytes _b)
Remove all 0 byte on the head of _s.
Definition: CommonJS.cpp:67
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
Definition: CommonData.h:79