void random_bytes(span<char> buffer) { #if TORRENT_USE_CRYPTOAPI HCRYPTPROV prov; if (!CryptAcquireContext(&prov, NULL, NULL , PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } if (!CryptGenRandom(prov, int(buffer.size()) , reinterpret_cast<BYTE*>(buffer.data()))) { CryptReleaseContext(prov, 0); #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } CryptReleaseContext(prov, 0); #elif defined TORRENT_USE_LIBCRYPTO #ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" #endif int r = RAND_bytes(reinterpret_cast<unsigned char*>(buffer.data()) , int(buffer.size())); if (r != 1) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(int(::ERR_get_error()), system_category())); #else std::terminate(); #endif } #ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO #pragma clang diagnostic pop #endif #else for (auto& b : buffer) b = char(random(0xff)); #endif }
void update(span<char const> data) { if (CryptHashData(m_hash, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false) { throw_ex<system_error>(error_code(GetLastError(), system_category())); } }
const error_category& posix_category() { #ifdef LLVM_ON_WIN32 return generic_category(); #else return system_category(); #endif }
inline void crypt_gen_random(span<char> buffer) { static HCRYPTPROV provider = crypt_acquire_provider(PROV_RSA_FULL); if (!CryptGenRandom(provider, int(buffer.size()) , reinterpret_cast<BYTE*>(buffer.data()))) { throw_ex<system_error>(error_code(GetLastError(), system_category())); } }
HCRYPTHASH duplicate(crypt_hash const& h) { HCRYPTHASH ret; if (CryptDuplicateHash(h.m_hash, 0, 0, &ret) == false) { throw_ex<system_error>(error_code(GetLastError(), system_category())); } return ret; }
HCRYPTHASH create() { HCRYPTHASH ret; if (CryptCreateHash(get_provider(), AlgId, 0, 0, &ret) == false) { throw_ex<system_error>(error_code(GetLastError(), system_category())); } return ret; }
void get_hash(char *digest, std::size_t digest_size) { DWORD size = DWORD(digest_size); if (CryptGetHashParam(m_hash, HP_HASHVAL , reinterpret_cast<BYTE*>(digest), &size, 0) == false) { throw_ex<system_error>(error_code(GetLastError(), system_category())); } TORRENT_ASSERT(size == DWORD(digest_size)); }
inline HCRYPTPROV crypt_acquire_provider(DWORD provider_type) { HCRYPTPROV ret; if (CryptAcquireContext(&ret, nullptr, nullptr, provider_type , CRYPT_VERIFYCONTEXT) == false) { throw_ex<system_error>(error_code(GetLastError(), system_category())); } return ret; }
void init(error_code& ec) noexcept { file_ = fopen(path_.c_str(), "rb"); if(! file_) ec = error_code{errno, system_category()}; else size_ = boost::filesystem::file_size(path_); }
void dht_tracker::incoming_error(error_code const& ec, udp::endpoint const& ep) { if (ec == boost::asio::error::connection_refused || ec == boost::asio::error::connection_reset || ec == boost::asio::error::connection_aborted #ifdef _WIN32 || ec == error_code(ERROR_HOST_UNREACHABLE, system_category()) || ec == error_code(ERROR_PORT_UNREACHABLE, system_category()) || ec == error_code(ERROR_CONNECTION_REFUSED, system_category()) || ec == error_code(ERROR_CONNECTION_ABORTED, system_category()) #endif ) { m_dht.unreachable(ep); #if TORRENT_USE_IPV6 m_dht6.unreachable(ep); #endif } }
void thread::join() { int ec = pthread_join(__t_, 0); #ifndef _LIBCPP_NO_EXCEPTIONS if (ec) throw system_error(error_code(ec, system_category()), "thread::join failed"); #endif // _LIBCPP_NO_EXCEPTIONS __t_ = 0; }
std::int64_t file::get_size(error_code& ec) const { #ifdef TORRENT_WINDOWS LARGE_INTEGER file_size; if (!GetFileSizeEx(native_handle(), &file_size)) { ec.assign(GetLastError(), system_category()); return -1; } return file_size.QuadPart; #else struct stat fs; if (::fstat(native_handle(), &fs) != 0) { ec.assign(errno, system_category()); return -1; } return fs.st_size; #endif }
void update(span<char const> data) { if (CryptHashData(m_hash, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } }
hasher512::hasher512(hasher512 const& h) { if (CryptDuplicateHash(h.m_context, 0, 0, &m_context) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } }
void thread::join() { int ec = __libcpp_thread_join(&__t_); #ifndef _LIBCPP_NO_EXCEPTIONS if (ec) throw system_error(error_code(ec, system_category()), "thread::join failed"); #else (void)ec; #endif // _LIBCPP_NO_EXCEPTIONS __t_ = 0; }
HCRYPTHASH duplicate(crypt_hash const& h) { HCRYPTHASH ret; if (CryptDuplicateHash(h.m_hash, 0, 0, &ret) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } return ret; }
HCRYPTHASH create() { HCRYPTHASH ret; if (CryptCreateHash(get_provider(), AlgId, 0, 0, &ret) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } return ret; }
inline void crypt_gen_random(span<char> buffer) { static HCRYPTPROV provider = crypt_acquire_provider(PROV_RSA_FULL); if (!CryptGenRandom(provider, int(buffer.size()) , reinterpret_cast<BYTE*>(buffer.data()))) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } }
// this has to be thread safe and atomic. i.e. on posix systems it has to be // turned into a series of pread() calls std::int64_t file::readv(std::int64_t file_offset, span<iovec_t const> bufs , error_code& ec, open_mode_t flags) { if (m_file_handle == INVALID_HANDLE_VALUE) { #ifdef TORRENT_WINDOWS ec = error_code(ERROR_INVALID_HANDLE, system_category()); #else ec = error_code(boost::system::errc::bad_file_descriptor, generic_category()); #endif return -1; } TORRENT_ASSERT((m_open_mode & open_mode::rw_mask) == open_mode::read_only || (m_open_mode & open_mode::rw_mask) == open_mode::read_write); TORRENT_ASSERT(!bufs.empty()); TORRENT_ASSERT(is_open()); #if TORRENT_USE_PREADV TORRENT_UNUSED(flags); std::int64_t ret = iov(&::preadv, native_handle(), file_offset, bufs, ec); #else // there's no point in coalescing single buffer writes if (bufs.size() == 1) { flags &= ~open_mode::coalesce_buffers; } iovec_t tmp; span<iovec_t const> tmp_bufs = bufs; if (flags & open_mode::coalesce_buffers) { if (!coalesce_read_buffers(tmp_bufs, tmp)) // ok, that failed, don't coalesce this read flags &= ~open_mode::coalesce_buffers; } #if TORRENT_USE_PREAD std::int64_t ret = iov(&::pread, native_handle(), file_offset, tmp_bufs, ec); #else std::int64_t ret = iov(&::read, native_handle(), file_offset, tmp_bufs, ec); #endif if (flags & open_mode::coalesce_buffers) coalesce_read_buffers_end(bufs , tmp.data(), !ec); #endif return ret; }
hasher512& hasher512::operator=(hasher512 const& h) { if (this == &h) return *this; CryptDestroyHash(m_context); if (CryptDuplicateHash(h.m_context, 0, 0, &m_context) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } return *this; }
void get_hash(char *digest, size_t digest_size) { DWORD size = DWORD(digest_size); if (CryptGetHashParam(m_hash, HP_HASHVAL , reinterpret_cast<BYTE*>(digest), &size, 0) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } TORRENT_ASSERT(size == DWORD(digest_size)); }
inline HCRYPTPROV crypt_acquire_provider(DWORD provider_type) { HCRYPTPROV ret; if (CryptAcquireContext(&ret, nullptr, nullptr, provider_type , CRYPT_VERIFYCONTEXT) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } return ret; }
int wait(HANDLE file, error_code& ec) { if (ol.hEvent != INVALID_HANDLE_VALUE && WaitForSingleObject(ol.hEvent, INFINITE) == WAIT_FAILED) { ec.assign(GetLastError(), system_category()); return -1; } DWORD ret; if (GetOverlappedResult(file, &ol, &ret, false) == 0) { DWORD last_error = GetLastError(); if (last_error != ERROR_HANDLE_EOF) { #ifdef ERROR_CANT_WAIT TORRENT_ASSERT(last_error != ERROR_CANT_WAIT); #endif ec.assign(last_error, system_category()); return -1; } } return ret; }
void thread::detach() { int ec = EINVAL; if (__t_ != 0) { ec = pthread_detach(__t_); if (ec == 0) __t_ = 0; } #ifndef _LIBCPP_NO_EXCEPTIONS if (ec) throw system_error(error_code(ec, system_category()), "thread::detach failed"); #endif // _LIBCPP_NO_EXCEPTIONS }
FilterContext FilterGraph::allocFilter(const Filter &filter, const std::string &name, error_code &ec) { clear_if(ec); if (!m_raw) { throws_if(ec, Errors::Unallocated); return FilterContext(); } AVFilterContext *ctx = avfilter_graph_alloc_filter(m_raw, filter.raw(), name.c_str()); if (!ctx) { throws_if(ec, ENOMEM, system_category()); return FilterContext(); } return FilterContext(ctx); }
hasher512::hasher512() { #ifdef TORRENT_USE_LIBGCRYPT gcry_md_open(&m_context, GCRY_MD_SHA512, 0); #elif TORRENT_USE_COMMONCRYPTO CC_SHA512_Init(&m_context); #elif TORRENT_USE_CRYPTOAPI if (CryptCreateHash(get_crypt_provider(), CALG_SHA_512, 0, 0, &m_context) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } #elif defined TORRENT_USE_LIBCRYPTO SHA512_Init(&m_context); #else SHA512_init(&m_context); #endif }
bool write(error_code& ec, WriteFunction&& wf) noexcept { if(size_ - offset_ < sizeof(buf_)) buf_len_ = static_cast<std::size_t>( size_ - offset_); else buf_len_ = sizeof(buf_); auto const nread = fread( buf_, 1, sizeof(buf_), file_); if(ferror(file_)) { ec = error_code(errno, system_category()); return true; } BOOST_ASSERT(nread != 0); offset_ += nread; wf(boost::asio::buffer(buf_, nread)); return offset_ >= size_; }
hasher512& hasher512::update(span<char const> data) { TORRENT_ASSERT(!data.empty()); #ifdef TORRENT_USE_LIBGCRYPT gcry_md_write(m_context, data.data(), data.size()); #elif TORRENT_USE_COMMONCRYPTO CC_SHA512_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), data.size()); #elif TORRENT_USE_CRYPTOAPI if (CryptHashData(m_context, reinterpret_cast<BYTE const*>(data.data()), int(data.size()), 0) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } #elif defined TORRENT_USE_LIBCRYPTO SHA512_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), data.size()); #else SHA512_update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), data.size()); #endif return *this; }
hasher& hasher::update(const char* data, int len) { TORRENT_ASSERT(data != nullptr); TORRENT_ASSERT(len > 0); #ifdef TORRENT_USE_LIBGCRYPT gcry_md_write(m_context, data, len); #elif TORRENT_USE_COMMONCRYPTO CC_SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len); #elif TORRENT_USE_CRYPTOAPI if (CryptHashData(m_context, reinterpret_cast<BYTE const*>(data), len, 0) == false) { #ifndef BOOST_NO_EXCEPTIONS throw system_error(error_code(GetLastError(), system_category())); #else std::terminate(); #endif } #elif defined TORRENT_USE_LIBCRYPTO SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len); #else SHA1_update(&m_context, reinterpret_cast<unsigned char const*>(data), len); #endif return *this; }
inline error_code make_error_code( cygwin_errno e ) { return error_code( e, system_category() ); }