diff options
Diffstat (limited to 'libdevcore')
-rw-r--r-- | libdevcore/Common.h | 8 | ||||
-rw-r--r-- | libdevcore/CommonData.cpp | 25 | ||||
-rw-r--r-- | libdevcore/CommonData.h | 9 | ||||
-rw-r--r-- | libdevcore/CommonIO.cpp | 39 | ||||
-rw-r--r-- | libdevcore/CommonIO.h | 8 | ||||
-rw-r--r-- | libdevcore/FixedHash.h | 16 |
6 files changed, 41 insertions, 64 deletions
diff --git a/libdevcore/Common.h b/libdevcore/Common.h index 0363d9a2..6208424e 100644 --- a/libdevcore/Common.h +++ b/libdevcore/Common.h @@ -64,15 +64,13 @@ #include <functional> #include <string> -using byte = uint8_t; - namespace dev { // Binary data types. -using bytes = std::vector<byte>; -using bytesRef = vector_ref<byte>; -using bytesConstRef = vector_ref<byte const>; +using bytes = std::vector<uint8_t>; +using bytesRef = vector_ref<uint8_t>; +using bytesConstRef = vector_ref<uint8_t const>; // Numeric types. using bigint = boost::multiprecision::number<boost::multiprecision::cpp_int_backend<>>; diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp index cb79fa98..8d2639c9 100644 --- a/libdevcore/CommonData.cpp +++ b/libdevcore/CommonData.cpp @@ -64,7 +64,7 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw) int h = fromHex(_s[i], WhenError::DontThrow); int l = fromHex(_s[i + 1], WhenError::DontThrow); if (h != -1 && l != -1) - ret.push_back((byte)(h * 16 + l)); + ret.push_back((uint8_t)(h * 16 + l)); else if (_throw == WhenError::Throw) BOOST_THROW_EXCEPTION(BadHexCharacter()); else @@ -110,3 +110,26 @@ string dev::getChecksummedAddress(string const& _addr) } return ret; } + +bool dev::isValidHex(string const& _string) +{ + if (_string.substr(0, 2) != "0x") + return false; + if (_string.find_first_not_of("0123456789abcdefABCDEF", 2) != string::npos) + return false; + return true; +} + +bool dev::isValidDecimal(string const& _string) +{ + if (_string.empty()) + return false; + if (_string == "0") + return true; + // No leading zeros + if (_string.front() == '0') + return false; + if (_string.find_first_not_of("0123456789") != string::npos) + return false; + return true; +} diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index f208c425..fedd3af2 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -88,7 +88,7 @@ inline std::string asString(bytesConstRef _b) /// Converts a string to a byte array containing the string's (byte) data. inline bytes asBytes(std::string const& _b) { - return bytes((byte const*)_b.data(), (byte const*)(_b.data() + _b.size())); + return bytes((uint8_t const*)_b.data(), (uint8_t const*)(_b.data() + _b.size())); } // Big-endian to/from host endian conversion functions. @@ -117,7 +117,7 @@ inline T fromBigEndian(_In const& _bytes) { T ret = (T)0; for (auto i: _bytes) - ret = (T)((ret << 8) | (byte)(typename std::make_unsigned<typename _In::value_type>::type)i); + ret = (T)((ret << 8) | (uint8_t)(typename std::make_unsigned<typename _In::value_type>::type)i); return ret; } inline bytes toBigEndian(u256 _val) { bytes ret(32); toBigEndian(_val, ret); return ret; } @@ -135,7 +135,7 @@ inline bytes toCompactBigEndian(T _val, unsigned _min = 0) toBigEndian(_val, ret); return ret; } -inline bytes toCompactBigEndian(byte _val, unsigned _min = 0) +inline bytes toCompactBigEndian(uint8_t _val, unsigned _min = 0) { return (_min || _val) ? bytes{ _val } : bytes{}; } @@ -272,4 +272,7 @@ bool passesAddressChecksum(std::string const& _str, bool _strict); /// @param hex strings that look like an address std::string getChecksummedAddress(std::string const& _addr); +bool isValidHex(std::string const& _string); +bool isValidDecimal(std::string const& _string); + } diff --git a/libdevcore/CommonIO.cpp b/libdevcore/CommonIO.cpp index 1aa3504c..cc730575 100644 --- a/libdevcore/CommonIO.cpp +++ b/libdevcore/CommonIO.cpp @@ -80,45 +80,6 @@ string dev::readStandardInput() return ret; } -void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename) -{ - namespace fs = boost::filesystem; - if (_writeDeleteRename) - { - fs::path tempPath = fs::unique_path(_file + "-%%%%%%"); - writeFile(tempPath.string(), _data, false); - // will delete _file if it exists - fs::rename(tempPath, _file); - } - else - { - // create directory if not existent - fs::path p(_file); - if (!p.parent_path().empty() && !fs::exists(p.parent_path())) - { - fs::create_directories(p.parent_path()); - try - { - fs::permissions(p.parent_path(), fs::owner_all); - } - catch (...) - { - } - } - - ofstream s(_file, ios::trunc | ios::binary); - s.write(reinterpret_cast<char const*>(_data.data()), _data.size()); - assertThrow(s, FileError, "Could not write to file: " + _file); - try - { - fs::permissions(_file, fs::owner_read|fs::owner_write); - } - catch (...) - { - } - } -} - #if defined(_WIN32) class DisableConsoleBuffering { diff --git a/libdevcore/CommonIO.h b/libdevcore/CommonIO.h index 928b6d15..b9f941ea 100644 --- a/libdevcore/CommonIO.h +++ b/libdevcore/CommonIO.h @@ -41,14 +41,6 @@ std::string readStandardInput(); /// Retrieve and returns a character from standard input (without waiting for EOL). int readStandardInputChar(); -/// Write the given binary data into the given file, replacing the file if it pre-exists. -/// Throws exception on error. -/// @param _writeDeleteRename useful not to lose any data: If set, first writes to another file in -/// the same directory and then moves that file. -void writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename = false); -/// Write the given binary data into the given file, replacing the file if it pre-exists. -inline void writeFile(std::string const& _file, bytes const& _data, bool _writeDeleteRename = false) { writeFile(_file, bytesConstRef(&_data), _writeDeleteRename); } -inline void writeFile(std::string const& _file, std::string const& _data, bool _writeDeleteRename = false) { writeFile(_file, bytesConstRef(_data), _writeDeleteRename); } /// Converts arbitrary value to string representation using std::stringstream. template <class _T> std::string toString(_T const& _t) diff --git a/libdevcore/FixedHash.h b/libdevcore/FixedHash.h index cd6e1da1..24b89840 100644 --- a/libdevcore/FixedHash.h +++ b/libdevcore/FixedHash.h @@ -79,7 +79,7 @@ public: operator Arith() const { return fromBigEndian<Arith>(m_data); } /// @returns true iff this is the empty hash. - explicit operator bool() const { return std::any_of(m_data.begin(), m_data.end(), [](byte _b) { return _b != 0; }); } + explicit operator bool() const { return std::any_of(m_data.begin(), m_data.end(), [](uint8_t _b) { return _b != 0; }); } // The obvious comparison operators. bool operator==(FixedHash const& _c) const { return m_data == _c.m_data; } @@ -90,9 +90,9 @@ public: FixedHash operator~() const { FixedHash ret; for (unsigned i = 0; i < N; ++i) ret[i] = ~m_data[i]; return ret; } /// @returns a particular byte from the hash. - byte& operator[](unsigned _i) { return m_data[_i]; } + uint8_t& operator[](unsigned _i) { return m_data[_i]; } /// @returns a particular byte from the hash. - byte operator[](unsigned _i) const { return m_data[_i]; } + uint8_t operator[](unsigned _i) const { return m_data[_i]; } /// @returns the hash as a user-readable hex string. std::string hex() const { return toHex(ref()); } @@ -104,19 +104,19 @@ public: bytesConstRef ref() const { return bytesConstRef(m_data.data(), N); } /// @returns a mutable byte pointer to the object's data. - byte* data() { return m_data.data(); } + uint8_t* data() { return m_data.data(); } /// @returns a constant byte pointer to the object's data. - byte const* data() const { return m_data.data(); } + uint8_t const* data() const { return m_data.data(); } /// @returns a copy of the object's data as a byte vector. bytes asBytes() const { return bytes(data(), data() + N); } /// @returns a mutable reference to the object's data as an STL array. - std::array<byte, N>& asArray() { return m_data; } + std::array<uint8_t, N>& asArray() { return m_data; } /// @returns a constant reference to the object's data as an STL array. - std::array<byte, N> const& asArray() const { return m_data; } + std::array<uint8_t, N> const& asArray() const { return m_data; } /// Returns the index of the first bit set to one, or size() * 8 if no bits are set. inline unsigned firstBitSet() const @@ -137,7 +137,7 @@ public: void clear() { m_data.fill(0); } private: - std::array<byte, N> m_data; ///< The binary data. + std::array<uint8_t, N> m_data; ///< The binary data. }; /// Stream I/O for the FixedHash class. |