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
31 Please do not change anything here and release it..
32 There is going to be a new forum created just for the Kademlia side of the client..
33 If you feel there is an error or a way to improve something, please
34 post it in the forum first and let us look at it.. If it is a real improvement,
35 it will be added to the offical client.. Changing something without knowing
36 what all it does can cause great harm to the network if released in mass form..
37 Any mod that changes anything within the Kademlia side will not be allowed to advertise
38 there client on the eMule forum..
43 #include "../../ArchSpecific.h"
44 #include <common/Format.h> // Needed for CFormat
47 ////////////////////////////////////////
48 using namespace Kademlia
;
49 ////////////////////////////////////////
51 CUInt128::CUInt128(const CUInt128
&value
, uint32_t numBits
)
53 // Copy the whole uint32s
54 uint32_t numULONGs
= numBits
/ 32;
55 for (uint32_t i
= 0; i
< numULONGs
; ++i
) {
56 m_data
[i
] = value
.m_data
[i
];
59 // Copy the remaining bits
60 for (uint32_t i
= (32 * numULONGs
); i
< numBits
; ++i
) {
61 SetBitNumber(i
, value
.GetBitNumber(i
));
64 // Pad with random bytes (Not seeding based on time to allow multiple different ones to be created in quick succession)
65 for (uint32_t i
= numBits
; i
< 128; ++i
) {
66 SetBitNumber(i
, (rand() % 2));
70 CUInt128
& CUInt128::SetValueBE(const uint8_t *valueBE
) throw()
72 m_data
[0] = wxUINT32_SWAP_ON_LE(RawPeekUInt32(valueBE
+0));
73 m_data
[1] = wxUINT32_SWAP_ON_LE(RawPeekUInt32(valueBE
+4));
74 m_data
[2] = wxUINT32_SWAP_ON_LE(RawPeekUInt32(valueBE
+8));
75 m_data
[3] = wxUINT32_SWAP_ON_LE(RawPeekUInt32(valueBE
+12));
79 wxString
CUInt128::ToHexString() const
83 for (int i
= 0; i
< 4; ++i
) {
84 str
.Append(CFormat(wxT("%08X")) % m_data
[i
]);
90 wxString
CUInt128::ToBinaryString(bool trim
) const
95 for (int i
= 0; i
< 128; ++i
) {
97 if ((!trim
) || (b
!= 0)) {
98 str
.Append(b
? wxT("1") : wxT("0"));
102 if (str
.Len() == 0) {
108 void CUInt128::ToByteArray(uint8_t *b
) const
110 wxCHECK_RET(b
!= NULL
, wxT("Destination buffer missing."));
112 RawPokeUInt32(b
, wxUINT32_SWAP_ON_LE(m_data
[0]));
113 RawPokeUInt32(b
+ 4, wxUINT32_SWAP_ON_LE(m_data
[1]));
114 RawPokeUInt32(b
+ 8, wxUINT32_SWAP_ON_LE(m_data
[2]));
115 RawPokeUInt32(b
+ 12, wxUINT32_SWAP_ON_LE(m_data
[3]));
118 void CUInt128::StoreCryptValue(uint8_t *buf
) const
120 wxCHECK_RET(buf
!= NULL
, wxT("Destination buffer missing."));
122 RawPokeUInt32(buf
, wxUINT32_SWAP_ON_BE(m_data
[0]));
123 RawPokeUInt32(buf
+ 4, wxUINT32_SWAP_ON_BE(m_data
[1]));
124 RawPokeUInt32(buf
+ 8, wxUINT32_SWAP_ON_BE(m_data
[2]));
125 RawPokeUInt32(buf
+ 12, wxUINT32_SWAP_ON_BE(m_data
[3]));
128 int CUInt128::CompareTo(const CUInt128
&other
) const throw()
130 for (int i
= 0; i
< 4; ++i
) {
131 if (m_data
[i
] < other
.m_data
[i
])
133 if (m_data
[i
] > other
.m_data
[i
])
139 int CUInt128::CompareTo(uint32_t value
) const throw()
141 if ((m_data
[0] > 0) || (m_data
[1] > 0) || (m_data
[2] > 0) || (m_data
[3] > value
))
143 if (m_data
[3] < value
)
148 CUInt128
& CUInt128::Add(const CUInt128
&value
) throw()
150 if (value
.IsZero()) return *this;
153 for (int i
= 3; i
>= 0; i
--) {
155 sum
+= value
.m_data
[i
];
156 m_data
[i
] = (uint32_t)sum
;
162 CUInt128
& CUInt128::Subtract(const CUInt128
&value
) throw()
164 if (value
.IsZero()) return *this;
167 for (int i
= 3; i
>= 0; i
--) {
169 sum
-= value
.m_data
[i
];
170 m_data
[i
] = (uint32_t)sum
;
176 CUInt128
& CUInt128::ShiftLeft(unsigned bits
) throw()
178 if ((bits
== 0) || IsZero())
182 SetValue((uint32_t)0);
186 uint32_t result
[] = {0,0,0,0};
187 int indexShift
= (int)bits
/ 32;
189 for (int i
= 3; i
>= indexShift
; i
--)
191 shifted
+= ((int64_t)m_data
[i
]) << (bits
% 32);
192 result
[i
-indexShift
] = (uint32_t)shifted
;
193 shifted
= shifted
>> 32;
195 for (int i
= 0; i
< 4; ++i
)
196 m_data
[i
] = result
[i
];
200 // File_checked_for_headers