diff --git a/rlp/main.cpp b/rlp/main.cpp index a3120246bf9..07e5f3e97bd 100644 --- a/rlp/main.cpp +++ b/rlp/main.cpp @@ -135,8 +135,8 @@ class RLPStreamer for (auto i: _d) { m_out << (j++ ? - (m_prefs.indent.empty() ? ", " : ("," + newline)) : - (m_prefs.indent.empty() ? " " : newline)); + (m_prefs.indent.empty() ? ", " : ("," + newline)) : + (m_prefs.indent.empty() ? " " : newline)); output(i, _level + 1); } newline = newline.substr(0, newline.size() - m_prefs.indent.size()); @@ -159,18 +159,18 @@ void putOut(bytes _out, Encoding _encoding, bool _encrypt, bool _quiet) switch (_encoding) { - case Encoding::Hex: case Encoding::Auto: - cout << toHex(_out) << endl; - break; - case Encoding::Base64: - cout << toBase64(&_out) << endl; - break; - case Encoding::Binary: - cout.write((char const*)_out.data(), _out.size()); - break; - case Encoding::Keccak: - cout << sha3(_out).hex() << endl; - break; + case Encoding::Hex: case Encoding::Auto: + cout << toHex(_out) << endl; + break; + case Encoding::Base64: + cout << toBase64(&_out) << endl; + break; + case Encoding::Binary: + cout.write((char const*)_out.data(), _out.size()); + break; + case Encoding::Keccak: + cout << sha3(_out).hex() << endl; + break; } } @@ -185,30 +185,33 @@ int main(int argc, char** argv) bool quiet = false; bool encrypt = false; RLPStreamer::Prefs prefs; + po::options_description modesOptions("Usage rlp [OPTIONS]\nModes"); modesOptions.add_options() - ("indent,i", po::value (), " Use string as the level indentation (default ' ').") - ("hex-ints", "Render integers in hex.") - ("string-ints", "Render integers in the same way as strings.") - ("ascii-strings", "Render data as C-style strings or hex depending on content being ASCII.") - ("force-string", "Force all data to be rendered as C-style strings.") - ("force-escape", "When rendering as C-style strings, force all characters to be escaped.") - ("force-hex", "Force all data to be rendered as raw hex.") - ("dapp,D", "Dapp-building mode; equivalent to --encrypt --64."); + ("indent,i", po::value (), " Use string as the level indentation (default ' ').") + ("hex-ints", "Render integers in hex.") + ("string-ints", "Render integers in the same way as strings.") + ("ascii-strings", "Render data as C-style strings or hex depending on content being ASCII.") + ("force-string", "Force all data to be rendered as C-style strings.") + ("force-escape", "When rendering as C-style strings, force all characters to be escaped.") + ("force-hex", "Force all data to be rendered as raw hex.") + ("dapp,D", "Dapp-building mode; equivalent to --encrypt --64."); + po::options_description generalOptions("General options"); generalOptions.add_options() - ("encrypt,e", "Encrypt the RLP data prior to output.") - ("lenience,L", "Try not to bomb out early if possible.") - ("hex,x", "Treat input RLP as hex encoded data.") - ("base-16", "Treat input RLP as hex encoded data.") - ("keccak,k", "Output Keccak-256 hash only.") - ("base-64", "Treat input RLP as base-64 encoded data.") - ("64", "Treat input RLP as base-64 encoded data.") - ("bin,b", "Treat input RLP as raw binary data.") - ("base-256", "Treat input RLP as raw binary data.") - ("quiet,q", "Don't place additional information on stderr.") - ("help,h", "Print this help message and exit.") - ("version,V", "Show the version and exit."); + ("encrypt,e", "Encrypt the RLP data prior to output.") + ("lenience,L", "Try not to bomb out early if possible.") + ("hex,x", "Treat input RLP as hex encoded data.") + ("base-16", "Treat input RLP as hex encoded data.") + ("keccak,k", "Output Keccak-256 hash only.") + ("base-64", "Treat input RLP as base-64 encoded data.") + ("64", "Treat input RLP as base-64 encoded data.") + ("bin,b", "Treat input RLP as raw binary data.") + ("base-256", "Treat input RLP as raw binary data.") + ("quiet,q", "Don't place additional information on stderr.") + ("help,h", "Print this help message and exit.") + ("version,V", "Show the version and exit."); + po::options_description allowedOptions("Allowed options"); allowedOptions.add(generalOptions).add(modesOptions); po::parsed_options parsed = po::command_line_parser(argc, argv).options(allowedOptions).allow_unregistered().run(); @@ -330,27 +333,27 @@ int main(int argc, char** argv) } switch (encoding) { - case Encoding::Hex: - { - string s = asString(in); - boost::algorithm::replace_all(s, " ", ""); - boost::algorithm::replace_all(s, "\n", ""); - boost::algorithm::replace_all(s, "\t", ""); - b = fromHex(s); - break; - } - case Encoding::Base64: - { - string s = asString(in); - boost::algorithm::replace_all(s, " ", ""); - boost::algorithm::replace_all(s, "\n", ""); - boost::algorithm::replace_all(s, "\t", ""); - b = fromBase64(s); - break; - } - default: - swap(b, in); - break; + case Encoding::Hex: + { + string s = asString(in); + boost::algorithm::replace_all(s, " ", ""); + boost::algorithm::replace_all(s, "\n", ""); + boost::algorithm::replace_all(s, "\t", ""); + b = fromHex(s); + break; + } + case Encoding::Base64: + { + string s = asString(in); + boost::algorithm::replace_all(s, " ", ""); + boost::algorithm::replace_all(s, "\n", ""); + boost::algorithm::replace_all(s, "\t", ""); + b = fromBase64(s); + break; + } + default: + swap(b, in); + break; } } @@ -359,166 +362,166 @@ int main(int argc, char** argv) RLP rlp(b); switch (mode) { - case Mode::ListArchive: + case Mode::ListArchive: + { + if (!rlp.isList()) { - if (!rlp.isList()) - { - cout << "Error: Invalid format; RLP data is not a list." << endl; - exit(1); - } - cout << rlp.itemCount() << " items:" << endl; - for (auto i: rlp) - { - if (!i.isData()) - { - cout << "Error: Invalid format; RLP list item is not data." << endl; - if (!lenience) - exit(1); - } - cout << " " << i.size() << " bytes: " << sha3(i.data()) << endl; - } - break; + cout << "Error: Invalid format; RLP data is not a list." << endl; + exit(1); } - case Mode::ExtractArchive: + cout << rlp.itemCount() << " items:" << endl; + for (auto i: rlp) { - if (!rlp.isList()) + if (!i.isData()) { - cout << "Error: Invalid format; RLP data is not a list." << endl; - exit(1); + cout << "Error: Invalid format; RLP list item is not data." << endl; + if (!lenience) + exit(1); } - cout << rlp.itemCount() << " items:" << endl; - for (auto i: rlp) + cout << " " << i.size() << " bytes: " << sha3(i.data()) << endl; + } + break; + } + case Mode::ExtractArchive: + { + if (!rlp.isList()) + { + cout << "Error: Invalid format; RLP data is not a list." << endl; + exit(1); + } + cout << rlp.itemCount() << " items:" << endl; + for (auto i: rlp) + { + if (!i.isData()) { - if (!i.isData()) - { - cout << "Error: Invalid format; RLP list item is not data." << endl; - if (!lenience) - exit(1); - } - ofstream fout; - fout.open(toString(sha3(i.data()))); - fout.write(reinterpret_cast(i.data().data()), i.data().size()); + cout << "Error: Invalid format; RLP list item is not data." << endl; + if (!lenience) + exit(1); } - break; + ofstream fout; + fout.open(toString(sha3(i.data()))); + fout.write(reinterpret_cast(i.data().data()), i.data().size()); } - case Mode::AssembleArchive: + break; + } + case Mode::AssembleArchive: + { + if (boost::filesystem::is_directory(inputFile)) { - if (boost::filesystem::is_directory(inputFile)) + js::mArray entries; + auto basePath = boost::filesystem::canonical(boost::filesystem::path(inputFile)).string(); + for (string& i: otherInputs) { - js::mArray entries; - auto basePath = boost::filesystem::canonical(boost::filesystem::path(inputFile)).string(); - for (string& i: otherInputs) + js::mObject entry; + strings parsed; + boost::algorithm::split(parsed, i, boost::is_any_of(",")); + i = parsed[0]; + for (unsigned j = 1; j < parsed.size(); ++j) { - js::mObject entry; - strings parsed; - boost::algorithm::split(parsed, i, boost::is_any_of(",")); - i = parsed[0]; - for (unsigned j = 1; j < parsed.size(); ++j) - { - strings nv; - boost::algorithm::split(nv, parsed[j], boost::is_any_of(":")); - if (nv.size() == 2) - entry[nv[0]] = nv[1]; - else{} // TODO: error - } - if (!entry.count("path")) - { - std::string path = boost::filesystem::canonical(boost::filesystem::path(parsed[0])).string().substr(basePath.size()); - if (path == "/index.html") - path = "/"; - entry["path"] = path; - } - entry["hash"] = toHex(dev::sha3(contents(parsed[0])).ref()); - entries.push_back(entry); + strings nv; + boost::algorithm::split(nv, parsed[j], boost::is_any_of(":")); + if (nv.size() == 2) + entry[nv[0]] = nv[1]; + else{} // TODO: error + } + if (!entry.count("path")) + { + std::string path = boost::filesystem::canonical(boost::filesystem::path(parsed[0])).string().substr(basePath.size()); + if (path == "/index.html") + path = "/"; + entry["path"] = path; } - js::mObject o; - o["entries"] = entries; - auto os = js::write_string(js::mValue(o), false); - in = asBytes(os); + entry["hash"] = toHex(dev::sha3(contents(parsed[0])).ref()); + entries.push_back(entry); } + js::mObject o; + o["entries"] = entries; + auto os = js::write_string(js::mValue(o), false); + in = asBytes(os); + } - strings addedInputs; - for (auto i: otherInputs) - if (!boost::filesystem::is_regular_file(i)) - cerr << "Skipped " << i << std::endl; - else - addedInputs.push_back(i); + strings addedInputs; + for (auto i: otherInputs) + if (!boost::filesystem::is_regular_file(i)) + cerr << "Skipped " << i << std::endl; + else + addedInputs.push_back(i); - RLPStream r(addedInputs.size() + 1); - r.append(in); - for (auto i: addedInputs) - r.append(contents(i)); - putOut(r.out(), encoding, encrypt, quiet); - break; + RLPStream r(addedInputs.size() + 1); + r.append(in); + for (auto i: addedInputs) + r.append(contents(i)); + putOut(r.out(), encoding, encrypt, quiet); + break; + } + case Mode::Render: + { + RLPStreamer s(cout, prefs); + s.output(rlp); + cout << endl; + break; + } + case Mode::Create: + { + vector v(1); + try { + js::read_string(asString(in), v[0]); } - case Mode::Render: + catch (...) { - RLPStreamer s(cout, prefs); - s.output(rlp); - cout << endl; - break; + cerr << "Error: Invalid format; bad JSON." << endl; + exit(1); } - case Mode::Create: + RLPStream out; + while (!v.empty()) { - vector v(1); - try { - js::read_string(asString(in), v[0]); - } - catch (...) + auto vb = v.back(); + v.pop_back(); + switch (vb.type()) { - cerr << "Error: Invalid format; bad JSON." << endl; - exit(1); - } - RLPStream out; - while (!v.empty()) - { - auto vb = v.back(); - v.pop_back(); - switch (vb.type()) + case js::array_type: { - case js::array_type: - { - js::mArray a = vb.get_array(); - out.appendList(a.size()); - for (int i = a.size() - 1; i >= 0; --i) - v.push_back(a[i]); - break; - } - case js::str_type: + js::mArray a = vb.get_array(); + out.appendList(a.size()); + for (int i = a.size() - 1; i >= 0; --i) + v.push_back(a[i]); + break; + } + case js::str_type: + { + string const& s = vb.get_str(); + if (s.size() >= 2 && s.substr(0, 2) == "0x") + out << fromHex(s); + else { - string const& s = vb.get_str(); - if (s.size() >= 2 && s.substr(0, 2) == "0x") - out << fromHex(s); - else - { - // assume it's a normal JS escaped string. - bytes ss; - ss.reserve(s.size()); - for (unsigned i = 0; i < s.size(); ++i) - if (s[i] == '\\' && i + 1 < s.size()) - { - if (s[++i] == 'x' && i + 2 < s.size()) - ss.push_back(fromHex(s.substr(i, 2))[0]); - } - else if (s[i] != '\\') - ss.push_back((byte)s[i]); - out << ss; - } - break; + // assume it's a normal JS escaped string. + bytes ss; + ss.reserve(s.size()); + for (unsigned i = 0; i < s.size(); ++i) + if (s[i] == '\\' && i + 1 < s.size()) + { + if (s[++i] == 'x' && i + 2 < s.size()) + ss.push_back(fromHex(s.substr(i, 2))[0]); + } + else if (s[i] != '\\') + ss.push_back((byte)s[i]); + out << ss; } - case js::int_type: - out << vb.get_int(); - break; - default: - cerr << "ERROR: Unsupported type in JSON." << endl; - if (!lenience) - exit(1); + break; } + case js::int_type: + out << vb.get_int(); + break; + default: + cerr << "ERROR: Unsupported type in JSON." << endl; + if (!lenience) + exit(1); } - putOut(out.out(), encoding, encrypt, quiet); - break; } - default:; + putOut(out.out(), encoding, encrypt, quiet); + break; + } + default:; } } catch (...)