25 #include <boost/algorithm/string.hpp> 26 #include <boost/filesystem.hpp> 40 <<
"Usage rlp <mode> [OPTIONS]" << endl
42 <<
" create <json> Given a simplified JSON string, output the RLP." << endl
43 <<
" render [ <file> | -- ] Render the given RLP. Options:" << endl
44 <<
" --indent <string> Use string as the level indentation (default ' ')." << endl
45 <<
" --hex-ints Render integers in hex." << endl
46 <<
" --string-ints Render integers in the same way as strings." << endl
47 <<
" --ascii-strings Render data as C-style strings or hex depending on content being ASCII." << endl
48 <<
" --force-string Force all data to be rendered as C-style strings." << endl
49 <<
" --force-escape When rendering as C-style strings, force all characters to be escaped." << endl
50 <<
" --force-hex Force all data to be rendered as raw hex." << endl
51 <<
" list [ <file> | -- ] List the items in the RLP list by hash and size." << endl
52 <<
" extract [ <file> | -- ] Extract all items in the RLP list, named by hash." << endl
53 <<
" assemble [ <manifest> | <base path> ] <file> ... Given a manifest & files, output the RLP." << endl
54 <<
" -D,--dapp Dapp-building mode; equivalent to --encrypt --64." << endl
56 <<
"General options:" << endl
57 <<
" -e,--encrypt Encrypt the RLP data prior to output." << endl
58 <<
" -L,--lenience Try not to bomb out early if possible." << endl
59 <<
" -x,--hex,--base-16 Treat input RLP as hex encoded data." << endl
60 <<
" -k,--keccak Output Keccak-256 hash only." << endl
61 <<
" --64,--base-64 Treat input RLP as base-64 encoded data." << endl
62 <<
" -b,--bin,--base-256 Treat input RLP as raw binary data." << endl
63 <<
" -q,--quiet Don't place additional information on stderr." << endl
64 <<
" -h,--help Print this help message and exit." << endl
65 <<
" -V,--version Show the version and exit." << endl
92 if (!std::setlocale(LC_ALL,
""))
94 setenv(
"LC_ALL",
"C", 1);
118 if (_s.size() >= 2 && _s.substr(0, 2) ==
"0x")
133 bool hexInts =
false;
134 bool stringInts =
true;
135 bool hexPrefix =
true;
136 bool forceString =
false;
137 bool escapeAll =
false;
138 bool forceHex =
true;
147 else if (_d.
isInt() && !m_prefs.stringInts)
152 else if (_d.
isData() || (_d.
isInt() && m_prefs.stringInts))
153 if (m_prefs.forceString || (!m_prefs.forceHex &&
isAscii(_d.
toString())))
156 m_out <<
"\"" << (m_prefs.hexPrefix ?
"0x" :
"") <<
toHex(_d.
toBytes()) <<
"\"";
160 string newline =
"\n";
161 for (
unsigned i = 0; i < _level + 1; ++i)
162 newline += m_prefs.indent;
167 (m_prefs.indent.empty() ?
", " : (
"," + newline)) :
168 (m_prefs.indent.empty() ?
" " : newline));
169 output(i, _level + 1);
171 newline = newline.substr(0, newline.size() - m_prefs.indent.size());
172 m_out << (m_prefs.indent.empty() ? (j ?
" ]" :
"]") : (j ? newline +
"]" :
"]"));
187 cerr <<
"Keccak of RLP: " << h.
hex() << endl;
192 cout <<
toHex(_out) << endl;
198 cout.write((
char const*)_out.data(), _out.size());
201 cout <<
sha3(_out).hex() << endl;
206 int main(
int argc,
char** argv)
213 bool lenience =
false;
218 for (
int i = 1; i < argc; ++i)
220 string arg = argv[i];
221 if (arg ==
"-h" || arg ==
"--help")
223 else if (arg ==
"render")
225 else if (arg ==
"create")
227 else if ((arg ==
"-i" || arg ==
"--indent") && i + 1 < argc)
229 else if (arg ==
"--hex-ints")
231 else if (arg ==
"--string-ints")
233 else if (arg ==
"--ascii-strings")
235 else if (arg ==
"--force-string")
237 else if (arg ==
"--force-hex")
239 else if (arg ==
"--force-escape")
241 else if (arg ==
"-n" || arg ==
"--nice")
243 else if (arg ==
"list")
245 else if (arg ==
"extract")
247 else if (arg ==
"assemble")
249 else if (arg ==
"-L" || arg ==
"--lenience")
251 else if (arg ==
"-D" || arg ==
"--dapp")
253 else if (arg ==
"-V" || arg ==
"--version")
255 else if (arg ==
"-q" || arg ==
"--quiet")
257 else if (arg ==
"-x" || arg ==
"--hex" || arg ==
"--base-16")
259 else if (arg ==
"-k" || arg ==
"--keccak")
261 else if (arg ==
"--64" || arg ==
"--base-64")
263 else if (arg ==
"-b" || arg ==
"--bin" || arg ==
"--base-256")
265 else if (arg ==
"-e" || arg ==
"--encrypt")
267 else if (inputFile.empty())
270 otherInputs.push_back(arg);
274 if (inputFile ==
"--")
275 for (
int i = cin.get(); i != -1; i = cin.get())
276 in.push_back((
byte)i);
277 else if (boost::filesystem::is_regular_file(inputFile))
290 if (b !=
'\n' && b !=
' ' && b !=
'\t')
292 if (encoding ==
Encoding::Hex && (b < '0' || b >
'9' ) && (b < 'a' || b >
'f' ) && (b < 'A' || b >
'F' ))
297 if (encoding ==
Encoding::Base64 && (b < '0' || b >
'9' ) && (b < 'a' || b >
'z' ) && (b < 'A' || b >
'Z' ) && b !=
'+' && b !=
'/')
309 boost::algorithm::replace_all(s,
" ",
"");
310 boost::algorithm::replace_all(s,
"\n",
"");
311 boost::algorithm::replace_all(s,
"\t",
"");
318 boost::algorithm::replace_all(s,
" ",
"");
319 boost::algorithm::replace_all(s,
"\n",
"");
320 boost::algorithm::replace_all(s,
"\t",
"");
339 cout <<
"Error: Invalid format; RLP data is not a list." << endl;
342 cout << rlp.
itemCount() <<
" items:" << endl;
347 cout <<
"Error: Invalid format; RLP list item is not data." << endl;
351 cout <<
" " << i.size() <<
" bytes: " <<
sha3(i.data()) << endl;
359 cout <<
"Error: Invalid format; RLP data is not a list." << endl;
362 cout << rlp.
itemCount() <<
" items:" << endl;
367 cout <<
"Error: Invalid format; RLP list item is not data." << endl;
373 fout.write(reinterpret_cast<char const*>(i.data().data()), i.data().size());
379 if (boost::filesystem::is_directory(inputFile))
382 auto basePath = boost::filesystem::canonical(boost::filesystem::path(inputFile)).string();
383 for (
string& i: otherInputs)
387 boost::algorithm::split(parsed, i, boost::is_any_of(
","));
389 for (
unsigned j = 1; j < parsed.size(); ++j)
392 boost::algorithm::split(nv, parsed[j], boost::is_any_of(
":"));
394 entry[nv[0]] = nv[1];
397 if (!entry.count(
"path"))
399 std::string path = boost::filesystem::canonical(boost::filesystem::path(parsed[0])).string().substr(basePath.size());
400 if (path ==
"/index.html")
402 entry[
"path"] = path;
405 entries.push_back(entry);
408 o[
"entries"] = entries;
414 for (
auto i: otherInputs)
415 if (!boost::filesystem::is_regular_file(i))
416 cerr <<
"Skipped " << i << std::endl;
418 addedInputs.push_back(i);
422 for (
auto i: addedInputs)
436 vector<js::mValue> v(1);
442 cerr <<
"Error: Invalid format; bad JSON." << endl;
456 for (
int i = a.size() - 1; i >= 0; --i)
462 string const& s = vb.get_str();
463 if (s.size() >= 2 && s.substr(0, 2) ==
"0x")
469 ss.reserve(s.size());
470 for (
unsigned i = 0; i < s.size(); ++i)
471 if (s[i] ==
'\\' && i + 1 < s.size())
473 if (s[++i] ==
'x' && i + 2 < s.size())
474 ss.push_back(
fromHex(s.substr(i, 2))[0]);
476 else if (s[i] !=
'\\')
477 ss.push_back((
byte)s[i]);
486 cerr <<
"ERROR: Unsupported type in JSON." << endl;
499 cerr <<
"Error: Invalid format; bad RLP." << endl;
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
bool isNull() const
No value.
vector_ref< _T const > ref(_T const &_t)
bool isInt() const
Integer value. Must not have a leading zero.
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
bool isList() const
List value.
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
RLPStream & append(unsigned _s)
Append given datum to the byte stream.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
bytes const & out() const
Read the byte stream.
SecureFixedHash< 32 > Secret
for(size_t i=trim;i< len;i++) hash[i-trim]
std::vector< std::string > strings
std::hash for asio::adress
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
bool isData() const
String value.
std::string escaped(std::string const &_s, bool _all=true)
Escapes a string into the C-string representation.
void putOut(bytes _out, Encoding _encoding, bool _encrypt, bool _quiet)
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
bool read_string(const String_type &s, Value_type &value)
void setDefaultOrCLocale()
mConfig::Array_type mArray
int main(int argc, char **argv)
RLPStreamer(ostream &_out, Prefs _p)
std::vector< byte > bytes
bytes toCompactBigEndian(T _val, unsigned _min=0)
Convenience function for toBigEndian.
bool isAscii(string const &_s)
RLPStream & appendList(size_t _items)
Appends a list.
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
Keccak message digest base class.
std::string toBase64(bytesConstRef _in)
mConfig::Object_type mObject
Value_type::String_type write_string(const Value_type &value, bool pretty)
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
bytes fromBase64(std::string const &_in)
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
std::string toString(int _flags=LaissezFaire) const
Converts to string.
_T toInt(int _flags=Strict) const
Converts to int of type given; if isString(), decodes as big-endian bytestream.
Class for writing to an RLP bytestream.
bytes contents(std::string const &_file)
Retrieve and returns the contents of the given file.
Class for interpreting Recursive Linear-Prefix Data.
std::string asString(bytes const &_b)
Converts byte array to a string containing the same (binary) data.
bytes toBytes(int _flags=LaissezFaire) const
Converts to bytearray.
void output(RLP const &_d, unsigned _level=0)