/* * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include #include #include namespace Swift { static const int BUFFER_SIZE = 4096; void readByteArrayFromFile(ByteArray& data, const boost::filesystem::path& file) { boost::filesystem::ifstream input(file, 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 + boost::numeric_cast(input.gcount())); } input.close(); } std::vector createByteArray(const std::string& s) { return std::vector(s.begin(), s.end()); } std::vector createByteArray(const char* c) { std::vector data; while (*c) { data.push_back(static_cast(*c)); ++c; } return data; } std::string byteArrayToString(const ByteArray& b) { size_t i; for (i = b.size(); i > 0; --i) { if (b[i - 1] != 0) { break; } } return i > 0 ? std::string(reinterpret_cast(vecptr(b)), i) : ""; } }