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-2008 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
);
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;
91 uint32_t Get32BitChunk(unsigned val
) const throw()
93 return val
< 4 ? m_data
[val
] : 0;
96 void Set32BitChunk(unsigned chunk
, uint32_t value
)
98 wxCHECK2(chunk
< 4, return);
100 m_data
[chunk
] = value
;
103 CUInt128
& SetValue(const CUInt128
& value
) throw()
105 m_data
[0] = value
.m_data
[0];
106 m_data
[1] = value
.m_data
[1];
107 m_data
[2] = value
.m_data
[2];
108 m_data
[3] = value
.m_data
[3];
112 CUInt128
& SetValue(uint32_t value
) throw()
114 m_data
[0] = m_data
[1] = m_data
[2] = 0;
119 CUInt128
& SetValueBE(const uint8_t *valueBE
) throw();
122 * Stores value used by the crypt functions.
124 * Since eMule started to use the value as-is (four little-endian 32-bit integers in big-endian order),
125 * we have to reproduce that same representation on every platform.
127 * @param buf Buffer to hold the value. Must be large enough to hold the data (16 bytes at least),
128 * and must not be NULL.
130 void StoreCryptValue(uint8_t *buf
) const;
132 /** Bit at level 0 being most significant. */
133 CUInt128
& SetBitNumber(unsigned bit
, unsigned value
)
135 wxCHECK(bit
<= 127, *this);
138 m_data
[bit
/ 32] |= 1 << (31 - (bit
% 32));
140 m_data
[bit
/ 32] &= ~(1 << (31 - (bit
% 32)));
145 CUInt128
& ShiftLeft(unsigned bits
) throw();
147 CUInt128
& Add(const CUInt128
& value
) throw();
148 CUInt128
& Add(uint32_t value
) throw()
150 return value
? Add(CUInt128(value
)) : *this;
153 CUInt128
& Subtract(const CUInt128
& value
) throw();
154 CUInt128
& Subtract(uint32_t value
) throw()
156 return value
? Subtract(CUInt128(value
)) : *this;
159 CUInt128
& XOR(const CUInt128
& value
) throw()
161 m_data
[0] ^= value
.m_data
[0];
162 m_data
[1] ^= value
.m_data
[1];
163 m_data
[2] ^= value
.m_data
[2];
164 m_data
[3] ^= value
.m_data
[3];
168 CUInt128
& XORBE(const uint8_t *valueBE
) throw() { return XOR(CUInt128(valueBE
)); }
170 bool operator< (const CUInt128
& value
) const throw() {return (CompareTo(value
) < 0);}
171 bool operator> (const CUInt128
& value
) const throw() {return (CompareTo(value
) > 0);}
172 bool operator<= (const CUInt128
& value
) const throw() {return (CompareTo(value
) <= 0);}
173 bool operator>= (const CUInt128
& value
) const throw() {return (CompareTo(value
) >= 0);}
174 bool operator== (const CUInt128
& value
) const throw() {return (CompareTo(value
) == 0);}
175 bool operator!= (const CUInt128
& value
) const throw() {return (CompareTo(value
) != 0);}
177 CUInt128
& operator= (const CUInt128
& value
) throw() { return SetValue(value
); }
178 CUInt128
& operator+=(const CUInt128
& value
) throw() { return Add(value
); }
179 CUInt128
& operator-=(const CUInt128
& value
) throw() { return Subtract(value
); }
180 CUInt128
& operator^=(const CUInt128
& value
) throw() { return XOR(value
); }
181 CUInt128
operator+ (const CUInt128
& value
) const throw() { return CUInt128(*this).Add(value
); }
182 CUInt128
operator- (const CUInt128
& value
) const throw() { return CUInt128(*this).Subtract(value
); }
183 CUInt128
operator^ (const CUInt128
& value
) const throw() { return CUInt128(*this).XOR(value
); }
185 bool operator< (uint32_t value
) const throw() {return (CompareTo(value
) < 0);}
186 bool operator> (uint32_t value
) const throw() {return (CompareTo(value
) > 0);}
187 bool operator<= (uint32_t value
) const throw() {return (CompareTo(value
) <= 0);}
188 bool operator>= (uint32_t value
) const throw() {return (CompareTo(value
) >= 0);}
189 bool operator== (uint32_t value
) const throw() {return (CompareTo(value
) == 0);}
190 bool operator!= (uint32_t value
) const throw() {return (CompareTo(value
) != 0);}
192 CUInt128
& operator= (uint32_t value
) throw() { return SetValue(value
); }
193 CUInt128
& operator+=(uint32_t value
) throw() { return Add(value
); }
194 CUInt128
& operator-=(uint32_t value
) throw() { return Subtract(value
); }
195 CUInt128
& operator^=(uint32_t value
) throw() { return value
? XOR(CUInt128(value
)) : *this; }
196 CUInt128
operator+ (uint32_t value
) const throw() { return CUInt128(*this).Add(value
); }
197 CUInt128
operator- (uint32_t value
) const throw() { return CUInt128(*this).Subtract(value
); }
198 CUInt128
operator^ (uint32_t value
) const throw() { return value
? CUInt128(*this).XOR(CUInt128(value
)) : *this; }
200 CUInt128
operator<< (uint32_t bits
) const throw() { return CUInt128(*this).ShiftLeft(bits
); }
201 CUInt128
& operator<<=(uint32_t bits
) throw() { return ShiftLeft(bits
); }
204 bool IsZero() const throw()
206 return (m_data
[0] | m_data
[1] | m_data
[2] | m_data
[3]) == 0;
212 inline bool operator==(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator==(x
); }
213 inline bool operator!=(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator!=(x
); }
214 inline bool operator<(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator>(x
); }
215 inline bool operator>(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator<(x
); }
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 CUInt128
operator+(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator+(x
); }
219 inline CUInt128
operator-(uint32_t x
, const CUInt128
& y
) throw() { return CUInt128(x
).Subtract(y
); }
220 inline CUInt128
operator^(uint32_t x
, const CUInt128
& y
) throw() { return y
.operator^(x
); }
225 // File_checked_for_headers