2 // This file is part of the aMule Project.
4 // Copyright (c) 2008 Dévai Tamás ( gonosztopi@amule.org )
5 // Copyright (c) 2004-2008 Angel Vidal ( kry@amule.org )
6 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
7 // Copyright (c) 2003 Barry Dunne (http://www.emule-project.net)
9 // Any parts of this program derived from the xMule, lMule or eMule project,
10 // or contributed by third-party developers are copyrighted by their
11 // respective authors.
13 // This program is free software; you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation; either version 2 of the License, or
16 // (at your option) any later version.
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 Please do not change anything here and release it..
31 There is going to be a new forum created just for the Kademlia side of the client..
32 If you feel there is an error or a way to improve something, please
33 post it in the forum first and let us look at it.. If it is a real improvement,
34 it will be added to the offical client.. Changing something without knowing
35 what all it does can cause great harm to the network if released in mass form..
36 Any mod that changes anything within the Kademlia side will not be allowed to advertise
37 there client on the eMule forum..
43 #include "../../Types.h"
45 ////////////////////////////////////////
47 ////////////////////////////////////////
52 CUInt128(const CUInt128
& value
) throw()
57 explicit CUInt128(bool fill
= false) throw()
59 m_data
[0] = m_data
[1] = m_data
[2] = m_data
[3] = (fill
? (uint32_t)-1 : 0);
62 explicit CUInt128(uint32_t value
) throw()
67 explicit CUInt128(const uint8_t *valueBE
) throw()
73 * Generates a new number, copying the most significant 'numBits' bits from 'value'.
74 * The remaining bits are randomly generated.
76 CUInt128(const CUInt128
& value
, uint32_t numBits
) throw();
78 /** Bit at level 0 being most significant. */
79 unsigned GetBitNumber(unsigned bit
) const throw()
81 return bit
<= 127 ? (m_data
[bit
/ 32] >> (31 - (bit
% 32))) & 1 : 0;
84 int CompareTo(const CUInt128
& other
) const throw();
85 int CompareTo(uint32_t value
) const throw();
87 wxString
ToHexString() const;
88 wxString
ToBinaryString(bool trim
= false) const;
89 void ToByteArray(uint8_t *b
) const throw();
91 uint32_t Get32BitChunk(unsigned val
) const throw()
98 void Set32BitChunk(unsigned chunk
, uint32_t value
) throw()
102 m_data
[chunk
] = value
;
105 CUInt128
& SetValue(const CUInt128
& value
) throw()
107 m_data
[0] = value
.m_data
[0];
108 m_data
[1] = value
.m_data
[1];
109 m_data
[2] = value
.m_data
[2];
110 m_data
[3] = value
.m_data
[3];
114 CUInt128
& SetValue(uint32_t value
) throw()
116 m_data
[0] = m_data
[1] = m_data
[2] = 0;
121 CUInt128
& SetValueBE(const uint8_t *valueBE
) throw();
123 CUInt128
& SetValueRandom();
126 * Stores value used by the crypt functions.
128 * Since eMule started to use the value as-is (four little-endian 32-bit integers in big-endian order),
129 * we have to reproduce that same representation on every platform.
131 * @param buf Buffer to hold the value. Must be large enough to hold the data (16 bytes at least),
132 * and must not be NULL.
134 void StoreCryptValue(uint8_t *buf
) const throw();
136 /** Bit at level 0 being most significant. */
137 CUInt128
& SetBitNumber(unsigned bit
, unsigned value
) throw()
139 wxASSERT(bit
<= 127);
142 m_data
[bit
/ 32] |= 1 << (31 - (bit
% 32));
144 m_data
[bit
/ 32] &= ~(1 << (31 - (bit
% 32)));
149 CUInt128
& ShiftLeft(unsigned bits
) throw();
151 CUInt128
& Add(const CUInt128
& value
) throw();
152 CUInt128
& Add(uint32_t value
) throw()
154 return value
? Add(CUInt128(value
)) : *this;
157 CUInt128
& Subtract(const CUInt128
& value
) throw();
158 CUInt128
& Subtract(uint32_t value
) throw()
160 return value
? Subtract(CUInt128(value
)) : *this;
163 CUInt128
& XOR(const CUInt128
& value
) throw()
165 m_data
[0] ^= value
.m_data
[0];
166 m_data
[1] ^= value
.m_data
[1];
167 m_data
[2] ^= value
.m_data
[2];
168 m_data
[3] ^= value
.m_data
[3];
172 CUInt128
& XORBE(const uint8_t *valueBE
) throw() { return XOR(CUInt128(valueBE
)); }
174 bool operator< (const CUInt128
& value
) const throw() {return (CompareTo(value
) < 0);}
175 bool operator> (const CUInt128
& value
) const throw() {return (CompareTo(value
) > 0);}
176 bool operator<= (const CUInt128
& value
) const throw() {return (CompareTo(value
) <= 0);}
177 bool operator>= (const CUInt128
& value
) const throw() {return (CompareTo(value
) >= 0);}
178 bool operator== (const CUInt128
& value
) const throw() {return (CompareTo(value
) == 0);}
179 bool operator!= (const CUInt128
& value
) const throw() {return (CompareTo(value
) != 0);}
181 CUInt128
& operator= (const CUInt128
& value
) throw() { return SetValue(value
); }
182 CUInt128
& operator+=(const CUInt128
& value
) throw() { return Add(value
); }
183 CUInt128
& operator-=(const CUInt128
& value
) throw() { return Subtract(value
); }
184 CUInt128
& operator^=(const CUInt128
& value
) throw() { return XOR(value
); }
185 CUInt128
operator+ (const CUInt128
& value
) const throw() { return CUInt128(*this).Add(value
); }
186 CUInt128
operator- (const CUInt128
& value
) const throw() { return CUInt128(*this).Subtract(value
); }
187 CUInt128
operator^ (const CUInt128
& value
) const throw() { return CUInt128(*this).XOR(value
); }
189 bool operator< (uint32_t value
) const throw() {return (CompareTo(value
) < 0);}
190 bool operator> (uint32_t value
) const throw() {return (CompareTo(value
) > 0);}
191 bool operator<= (uint32_t value
) const throw() {return (CompareTo(value
) <= 0);}
192 bool operator>= (uint32_t value
) const throw() {return (CompareTo(value
) >= 0);}
193 bool operator== (uint32_t value
) const throw() {return (CompareTo(value
) == 0);}
194 bool operator!= (uint32_t value
) const throw() {return (CompareTo(value
) != 0);}
196 CUInt128
& operator= (uint32_t value
) throw() { return SetValue(value
); }
197 CUInt128
& operator+=(uint32_t value
) throw() { return Add(value
); }
198 CUInt128
& operator-=(uint32_t value
) throw() { return Subtract(value
); }
199 CUInt128
& operator^=(uint32_t value
) throw() { return value
? XOR(CUInt128(value
)) : *this; }
200 CUInt128
operator+ (uint32_t value
) const throw() { return CUInt128(*this).Add(value
); }
201 CUInt128
operator- (uint32_t value
) const throw() { return CUInt128(*this).Subtract(value
); }
202 CUInt128
operator^ (uint32_t value
) const throw() { return value
? CUInt128(*this).XOR(CUInt128(value
)) : *this; }
204 CUInt128
operator<< (uint32_t bits
) const throw() { return CUInt128(*this).ShiftLeft(bits
); }
205 CUInt128
& operator<<=(uint32_t bits
) throw() { return ShiftLeft(bits
); }
208 bool IsZero() const throw()
210 return (m_data
[0] | m_data
[1] | m_data
[2] | m_data
[3]) == 0;
216 inline bool operator==(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator==(x
); }
217 inline bool operator!=(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator!=(x
); }
218 inline bool operator<(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator>(x
); }
219 inline bool operator>(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator<(x
); }
220 inline bool operator<=(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator>=(x
); }
221 inline bool operator>=(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator<=(x
); }
222 inline CUInt128
operator+(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator+(x
); }
223 inline CUInt128
operator-(uint32_t x
, const CUInt128
& y
) throw() { return CUInt128(x
).Subtract(y
); }
224 inline CUInt128
operator^(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator^(x
); }
229 // File_checked_for_headers