Browse Source

enough of stack smashing segfault. openssl is required, period. fix #105. fix #97. fix #27.

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
08cadbb78e
  1. 1
      Makefile.am
  2. 45
      libtorrent/include/libtorrent/hasher.hpp
  3. 66
      libtorrent/src/hasher.cpp

1
Makefile.am

@ -88,7 +88,6 @@ LIBTORRENT_SOURCES = \
libtorrent/src/session.cpp \ libtorrent/src/session.cpp \
libtorrent/src/session_impl.cpp \ libtorrent/src/session_impl.cpp \
libtorrent/src/settings.cpp \ libtorrent/src/settings.cpp \
libtorrent/src/sha1.cpp \
libtorrent/src/smart_ban.cpp \ libtorrent/src/smart_ban.cpp \
libtorrent/src/socket_io.cpp \ libtorrent/src/socket_io.cpp \
libtorrent/src/socket_type.cpp \ libtorrent/src/socket_type.cpp \

45
libtorrent/include/libtorrent/hasher.hpp

@ -39,39 +39,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
#ifdef TORRENT_USE_GCRYPT
#include <gcrypt.h>
#elif TORRENT_USE_COMMONCRYPTO
#include <CommonCrypto/CommonDigest.h>
#elif defined TORRENT_USE_OPENSSL
extern "C" extern "C"
{ {
#include <openssl/sha.h> #include <openssl/sha.h>
} }
#else
// from sha1.cpp
namespace libtorrent
{
struct TORRENT_EXTRA_EXPORT sha_ctx
{
boost::uint32_t state[5];
boost::uint32_t count[2];
boost::uint8_t buffer[64];
};
TORRENT_EXTRA_EXPORT void SHA1_init(sha_ctx* context);
TORRENT_EXTRA_EXPORT void SHA1_update(sha_ctx* context, boost::uint8_t const* data, boost::uint32_t len);
TORRENT_EXTRA_EXPORT void SHA1_final(boost::uint8_t* digest, sha_ctx* context);
} // namespace libtorrent
#endif
namespace libtorrent namespace libtorrent
{ {
class TORRENT_EXTRA_EXPORT hasher class TORRENT_EXTRA_EXPORT hasher
@ -81,32 +53,15 @@ namespace libtorrent
hasher(); hasher();
hasher(const char* data, int len); hasher(const char* data, int len);
#ifdef TORRENT_USE_GCRYPT
hasher(hasher const& h);
hasher& operator=(hasher const& h);
#endif
void update(std::string const& data) { update(data.c_str(), data.size()); } void update(std::string const& data) { update(data.c_str(), data.size()); }
void update(const char* data, int len); void update(const char* data, int len);
sha1_hash final(); sha1_hash final();
void reset(); void reset();
#ifdef TORRENT_USE_GCRYPT
~hasher();
#endif
private: private:
#ifdef TORRENT_USE_GCRYPT
gcry_md_hd_t m_context;
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_CTX m_context;
#elif defined TORRENT_USE_OPENSSL
SHA_CTX m_context; SHA_CTX m_context;
#else
sha_ctx m_context;
#endif
}; };
} }

66
libtorrent/src/hasher.cpp

@ -36,100 +36,34 @@ namespace libtorrent
{ {
hasher::hasher() hasher::hasher()
{ {
#ifdef TORRENT_USE_GCRYPT
gcry_md_open(&m_context, GCRY_MD_SHA1, 0);
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Init(&m_context);
#elif defined TORRENT_USE_OPENSSL
SHA1_Init(&m_context); SHA1_Init(&m_context);
#else
SHA1_init(&m_context);
#endif
} }
hasher::hasher(const char* data, int len) hasher::hasher(const char* data, int len)
{ {
TORRENT_ASSERT(data != 0); TORRENT_ASSERT(data != 0);
TORRENT_ASSERT(len > 0); TORRENT_ASSERT(len > 0);
#ifdef TORRENT_USE_GCRYPT
gcry_md_open(&m_context, GCRY_MD_SHA1, 0);
gcry_md_write(m_context, data, len);
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Init(&m_context);
CC_SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#elif defined TORRENT_USE_OPENSSL
SHA1_Init(&m_context); SHA1_Init(&m_context);
SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len); SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#else
SHA1_init(&m_context);
SHA1_update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#endif
} }
#ifdef TORRENT_USE_GCRYPT
hasher::hasher(hasher const& h)
{
gcry_md_copy(&m_context, h.m_context);
}
hasher& hasher::operator=(hasher const& h)
{
gcry_md_close(m_context);
gcry_md_copy(&m_context, h.m_context);
return *this;
}
#endif
void hasher::update(const char* data, int len) void hasher::update(const char* data, int len)
{ {
TORRENT_ASSERT(data != 0); TORRENT_ASSERT(data != 0);
TORRENT_ASSERT(len > 0); TORRENT_ASSERT(len > 0);
#ifdef TORRENT_USE_GCRYPT
gcry_md_write(m_context, data, len);
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#elif defined TORRENT_USE_OPENSSL
SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len); SHA1_Update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#else
SHA1_update(&m_context, reinterpret_cast<unsigned char const*>(data), len);
#endif
} }
sha1_hash hasher::final() sha1_hash hasher::final()
{ {
sha1_hash digest; sha1_hash digest;
#ifdef TORRENT_USE_GCRYPT
gcry_md_final(m_context);
digest.assign((const char*)gcry_md_read(m_context, 0));
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Final(digest.begin(), &m_context);
#elif defined TORRENT_USE_OPENSSL
SHA1_Final(digest.begin(), &m_context); SHA1_Final(digest.begin(), &m_context);
#else
SHA1_final(digest.begin(), &m_context);
#endif
return digest; return digest;
} }
void hasher::reset() void hasher::reset()
{ {
#ifdef TORRENT_USE_GCRYPT
gcry_md_reset(m_context);
#elif TORRENT_USE_COMMONCRYPTO
CC_SHA1_Init(&m_context);
#elif defined TORRENT_USE_OPENSSL
SHA1_Init(&m_context); SHA1_Init(&m_context);
#else
SHA1_init(&m_context);
#endif
} }
#ifdef TORRENT_USE_GCRYPT
hasher::~hasher()
{
gcry_md_close(m_context);
}
#endif
} }

Loading…
Cancel
Save