~ubuntu-branches/ubuntu/natty/kdenetwork/natty-proposed

« back to all changes in this revision

Viewing changes to kget/transfer-plugins/bittorrent/libbtcore/dht/key.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2011-01-21 16:28:50 UTC
  • mfrom: (1.1.55 upstream)
  • Revision ID: james.westby@ubuntu.com-20110121162850-5okl235t3l91cwx0
Tags: 4:4.6.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2005 by Joris Guisson                                   *
3
 
 *   joris.guisson@gmail.com                                               *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
19
 
 ***************************************************************************/
20
 
#include "key.h"
21
 
#include <time.h>
22
 
#include <stdlib.h>
23
 
#include <util/constants.h>
24
 
 
25
 
using namespace bt;
26
 
 
27
 
namespace dht
28
 
{
29
 
 
30
 
        Key::Key()
31
 
        {}
32
 
 
33
 
        Key::Key(const bt::SHA1Hash & k) : bt::SHA1Hash(k)
34
 
        {
35
 
        }
36
 
        
37
 
        Key::Key(const Uint8* d) : bt::SHA1Hash(d)
38
 
        {
39
 
        }
40
 
        
41
 
        Key::Key(const QByteArray & ba)
42
 
        {
43
 
                for (int i = 0;i < 20 && i < ba.size();i++)
44
 
                        hash[i] = ba[i];
45
 
        }
46
 
 
47
 
        Key::~Key()
48
 
        {}
49
 
 
50
 
        bool Key::operator == (const Key & other) const
51
 
        {
52
 
                return bt::SHA1Hash::operator ==(other);
53
 
        }
54
 
        
55
 
        bool Key::operator != (const Key & other) const
56
 
        {
57
 
                return !operator == (other);
58
 
        }
59
 
        
60
 
        bool Key::operator < (const Key & other) const
61
 
        {
62
 
                for (int i = 0;i < 20;i++)
63
 
                {
64
 
                        if (hash[i] < other.hash[i])
65
 
                                return true;
66
 
                        else if (hash[i] > other.hash[i])
67
 
                                return false;
68
 
                }
69
 
                return false;
70
 
        }
71
 
        
72
 
        bool Key::operator <= (const Key & other) const
73
 
        {
74
 
                return operator < (other) || operator == (other);
75
 
        }
76
 
        
77
 
        bool Key::operator > (const Key & other) const
78
 
        {
79
 
                for (int i = 0;i < 20;i++)
80
 
                {
81
 
                        if (hash[i] < other.hash[i])
82
 
                                return false;
83
 
                        else if (hash[i] > other.hash[i])
84
 
                                return true;
85
 
                }
86
 
                return false;
87
 
        }
88
 
        
89
 
        bool Key::operator >= (const Key & other) const
90
 
        {
91
 
                return operator > (other) || operator == (other);
92
 
        }
93
 
 
94
 
        Key Key::distance(const Key & a,const Key & b)
95
 
        {
96
 
                return a ^ b;
97
 
        }
98
 
        
99
 
        Key Key::random()
100
 
        {
101
 
                srand(time(0));
102
 
                Key k;
103
 
                for (int i = 0;i < 20;i++)
104
 
                {
105
 
                        k.hash[i] = (Uint8)rand() % 0xFF;
106
 
                }
107
 
                return k;
108
 
        }
109
 
}