/* * Copyright (c) 2010 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #include #include std::ostream& operator<<(std::ostream& os, const Swift::ByteArray& s) { return operator<<(os, s.getDataVector()); } std::ostream& operator<<(std::ostream& os, const std::vector& s) { std::ios::fmtflags oldFlags = os.flags(); os << std::hex; for (Swift::ByteArray::const_iterator i = s.begin(); i != s.end(); ++i) { os << "0x" << static_cast(static_cast(*i)); if (i + 1 < s.end()) { os << " "; } } os << std::endl; os.flags(oldFlags); return os; } namespace Swift { static const int BUFFER_SIZE = 4096; void ByteArray::readFromFile(const std::string& file) { std::ifstream input(file.c_str(), std::ios_base::in|std::ios_base::binary); while (input.good()) { size_t oldSize = data_.size(); data_.resize(oldSize + BUFFER_SIZE); input.read(reinterpret_cast(&data_[oldSize]), BUFFER_SIZE); data_.resize(oldSize + input.gcount()); } input.close(); } std::vector ByteArray::create(const std::string& s) { return std::vector(s.begin(), s.end()); } std::vector ByteArray::create(const char* c) { std::vector data; while (*c) { data.push_back(static_cast(*c)); ++c; } return data; } std::vector ByteArray::create(const char* c, size_t n) { std::vector data; if (n > 0) { data.resize(n); memcpy(&data[0], c, n); } return data; } std::vector ByteArray::create(const unsigned char* c, size_t n) { std::vector data; if (n > 0) { data.resize(n); memcpy(&data[0], c, n); } return data; } std::string ByteArray::toString() const { size_t i; for (i = data_.size(); i > 0; --i) { if (data_[i - 1] != 0) { break; } } return i > 0 ? std::string(reinterpret_cast(getData()), i) : ""; } }