2003-10-23 01:00:57 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2003, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_PEER_ID_HPP_INCLUDED
|
|
|
|
#define TORRENT_PEER_ID_HPP_INCLUDED
|
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2004-06-14 01:30:42 +02:00
|
|
|
#include <cctype>
|
2004-03-24 23:50:07 +01:00
|
|
|
#include <algorithm>
|
2007-03-16 02:24:40 +01:00
|
|
|
#include <string>
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-09-01 06:08:39 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
2005-11-01 19:30:39 +01:00
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2005-11-01 19:30:39 +01:00
|
|
|
class TORRENT_EXPORT big_number
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2004-06-30 09:53:42 +02:00
|
|
|
// private type
|
2004-06-14 01:30:42 +02:00
|
|
|
struct private_pointer {};
|
2004-06-30 09:53:42 +02:00
|
|
|
// the number of bytes of the number
|
|
|
|
enum { number_size = 20 };
|
2003-10-23 01:00:57 +02:00
|
|
|
public:
|
2006-05-15 00:30:05 +02:00
|
|
|
enum { size = number_size };
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
big_number() {}
|
|
|
|
|
2007-03-16 02:24:40 +01:00
|
|
|
big_number(std::string const& s)
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(s.size() >= 20);
|
2007-03-16 02:24:40 +01:00
|
|
|
int sl = int(s.size()) < size ? int(s.size()) : size;
|
|
|
|
std::memcpy(m_number, &s[0], sl);
|
|
|
|
}
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
// when initialized with 0
|
|
|
|
big_number(private_pointer*) { clear(); }
|
|
|
|
|
2004-01-21 14:16:11 +01:00
|
|
|
void clear()
|
2004-01-15 02:29:43 +01:00
|
|
|
{
|
|
|
|
std::fill(m_number,m_number+number_size,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_all_zeros() const
|
|
|
|
{
|
|
|
|
return std::count(m_number,m_number+number_size,0) == number_size;
|
|
|
|
}
|
|
|
|
|
2004-12-21 13:30:09 +01:00
|
|
|
bool operator==(big_number const& n) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
return std::equal(n.m_number, n.m_number+number_size, m_number);
|
|
|
|
}
|
|
|
|
|
2004-12-21 13:30:09 +01:00
|
|
|
bool operator!=(big_number const& n) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
return !std::equal(n.m_number, n.m_number+number_size, m_number);
|
|
|
|
}
|
|
|
|
|
2004-12-21 13:30:09 +01:00
|
|
|
bool operator<(big_number const& n) const
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
2006-06-29 01:27:44 +02:00
|
|
|
for (int i = 0; i < number_size; ++i)
|
2003-10-23 01:00:57 +02:00
|
|
|
{
|
|
|
|
if (m_number[i] < n.m_number[i]) return true;
|
|
|
|
if (m_number[i] > n.m_number[i]) return false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-29 01:27:44 +02:00
|
|
|
|
|
|
|
big_number operator~()
|
|
|
|
{
|
|
|
|
big_number ret;
|
|
|
|
for (int i = 0; i< number_size; ++i)
|
|
|
|
ret.m_number[i] = ~m_number[i];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
big_number& operator &= (big_number const& n)
|
|
|
|
{
|
|
|
|
for (int i = 0; i< number_size; ++i)
|
|
|
|
m_number[i] &= n.m_number[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
big_number& operator |= (big_number const& n)
|
|
|
|
{
|
|
|
|
for (int i = 0; i< number_size; ++i)
|
|
|
|
m_number[i] |= n.m_number[i];
|
|
|
|
return *this;
|
|
|
|
}
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2007-06-06 02:41:20 +02:00
|
|
|
big_number& operator ^= (big_number const& n)
|
|
|
|
{
|
|
|
|
for (int i = 0; i< number_size; ++i)
|
|
|
|
m_number[i] ^= n.m_number[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2003-12-21 18:28:27 +01:00
|
|
|
unsigned char& operator[](int i)
|
2007-10-05 02:30:00 +02:00
|
|
|
{ TORRENT_ASSERT(i >= 0 && i < number_size); return m_number[i]; }
|
2003-12-21 18:28:27 +01:00
|
|
|
|
2004-10-03 13:39:34 +02:00
|
|
|
unsigned char const& operator[](int i) const
|
2007-10-05 02:30:00 +02:00
|
|
|
{ TORRENT_ASSERT(i >= 0 && i < number_size); return m_number[i]; }
|
2003-12-21 18:28:27 +01:00
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
typedef const unsigned char* const_iterator;
|
|
|
|
typedef unsigned char* iterator;
|
2003-10-23 01:00:57 +02:00
|
|
|
|
2003-11-02 22:06:50 +01:00
|
|
|
const_iterator begin() const { return m_number; }
|
|
|
|
const_iterator end() const { return m_number+number_size; }
|
|
|
|
|
|
|
|
iterator begin() { return m_number; }
|
|
|
|
iterator end() { return m_number+number_size; }
|
2003-10-23 01:00:57 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
unsigned char m_number[number_size];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef big_number peer_id;
|
|
|
|
typedef big_number sha1_hash;
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
inline std::ostream& operator<<(std::ostream& os, big_number const& peer)
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
|
|
|
for (big_number::const_iterator i = peer.begin();
|
2005-08-11 13:06:52 +02:00
|
|
|
i != peer.end(); ++i)
|
2003-11-02 22:06:50 +01:00
|
|
|
{
|
|
|
|
os << std::hex << std::setw(2) << std::setfill('0')
|
|
|
|
<< static_cast<unsigned int>(*i);
|
|
|
|
}
|
2003-11-05 00:27:06 +01:00
|
|
|
os << std::dec << std::setfill(' ');
|
2003-11-02 22:06:50 +01:00
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
inline std::istream& operator>>(std::istream& is, big_number& peer)
|
|
|
|
{
|
2004-08-16 23:55:31 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2004-06-14 01:30:42 +02:00
|
|
|
for (big_number::iterator i = peer.begin();
|
|
|
|
i != peer.end(); ++i)
|
|
|
|
{
|
|
|
|
char c[2];
|
|
|
|
is >> c[0] >> c[1];
|
2005-08-11 13:06:52 +02:00
|
|
|
c[0] = tolower(c[0]);
|
|
|
|
c[1] = tolower(c[1]);
|
|
|
|
if (
|
|
|
|
((c[0] < '0' || c[0] > '9') && (c[0] < 'a' || c[0] > 'f'))
|
|
|
|
|| ((c[1] < '0' || c[1] > '9') && (c[1] < 'a' || c[1] > 'f'))
|
2004-10-10 02:42:48 +02:00
|
|
|
|| is.fail())
|
|
|
|
{
|
|
|
|
is.setstate(ios_base::failbit);
|
|
|
|
return is;
|
|
|
|
}
|
2004-08-16 23:55:31 +02:00
|
|
|
*i = ((isdigit(c[0])?c[0]-'0':c[0]-'a'+10) << 4)
|
|
|
|
+ (isdigit(c[1])?c[1]-'0':c[1]-'a'+10);
|
2004-06-14 01:30:42 +02:00
|
|
|
}
|
|
|
|
return is;
|
|
|
|
}
|
|
|
|
|
2003-10-23 01:00:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // TORRENT_PEER_ID_HPP_INCLUDED
|
2005-08-11 13:06:52 +02:00
|
|
|
|