Improve the code by static code analysis [1/3]: Bug fixes
[amule.git] / src / KnownFile.cpp
blobe6e9d78a2e47b3213b1ac0071e6fbfd17cbdbc73
1 //
2 // This file is part of the aMule Project.
3 //
4 // Parts of this file are based on work from pan One (http://home-3.tiscali.nl/~meost/pms/)
5 //
6 // Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
7 // Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
8 //
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
29 #include "KnownFile.h" // Do_not_auto_remove
31 #include <protocol/kad/Constants.h>
32 #include <protocol/ed2k/Client2Client/TCP.h>
33 #include <protocol/ed2k/ClientSoftware.h>
34 #include <protocol/Protocols.h>
35 #include <tags/FileTags.h>
37 #include <wx/config.h>
39 #ifdef CLIENT_GUI
40 #include "UpDownClientEC.h" // Needed for CUpDownClient
41 #else
42 #include "updownclient.h" // Needed for CUpDownClient
43 #endif
45 #include "MemFile.h" // Needed for CMemFile
46 #include "Packet.h" // Needed for CPacket
47 #include "Preferences.h" // Needed for CPreferences
48 #include "KnownFileList.h" // Needed for CKnownFileList
49 #include "amule.h" // Needed for theApp
50 #include "PartFile.h" // Needed for SavePartFile
51 #include "ClientList.h" // Needed for clientlist (buddy support)
52 #include "Logger.h"
53 #include "ScopedPtr.h" // Needed for CScopedArray and CScopedPtr
54 #include "GuiEvents.h" // Needed for Notify_*
55 #include "SearchFile.h" // Needed for CSearchFile
56 #include "FileArea.h" // Needed for CFileArea
57 #include "FileAutoClose.h" // Needed for CFileAutoClose
58 #include "Server.h" // Needed for CServer
60 #include "CryptoPP_Inc.h" // Needed for MD4
62 #include <common/Format.h>
64 CFileStatistic::CFileStatistic() :
65 requested(0),
66 transferred(0),
67 accepted(0),
68 alltimerequested(0),
69 alltimetransferred(0),
70 alltimeaccepted(0)
74 #ifndef CLIENT_GUI
76 void CFileStatistic::AddRequest(){
77 requested++;
78 alltimerequested++;
79 theApp->knownfiles->requested++;
80 theApp->sharedfiles->UpdateItem(fileParent);
83 void CFileStatistic::AddAccepted(){
84 accepted++;
85 alltimeaccepted++;
86 theApp->knownfiles->accepted++;
87 theApp->sharedfiles->UpdateItem(fileParent);
90 void CFileStatistic::AddTransferred(uint64 bytes){
91 transferred += bytes;
92 alltimetransferred += bytes;
93 theApp->knownfiles->transferred += bytes;
94 theApp->sharedfiles->UpdateItem(fileParent);
97 #endif // CLIENT_GUI
100 /* Abstract File (base class)*/
102 CAbstractFile::CAbstractFile()
104 m_iRating(0),
105 m_hasComment(false),
106 m_iUserRating(0),
107 m_nFileSize(0)
112 CAbstractFile::CAbstractFile(const CAbstractFile& other)
114 m_abyFileHash(other.m_abyFileHash),
115 m_strComment(other.m_strComment),
116 m_iRating(other.m_iRating),
117 m_hasComment(other.m_hasComment),
118 m_iUserRating(other.m_iUserRating),
119 m_taglist(other.m_taglist),
120 m_nFileSize(other.m_nFileSize),
121 m_fileName(other.m_fileName)
123 /* // TODO: Currently it's not safe to duplicate the entries, but isn't needed either.
124 CKadEntryPtrList::const_iterator it = other.m_kadNotes.begin();
125 for (; it != other.m_kadNotes.end(); ++it) {
126 m_kadNotes.push_back(new Kademlia::CEntry(**it));
132 void CAbstractFile::SetFileName(const CPath& fileName)
134 m_fileName = fileName;
137 uint32 CAbstractFile::GetIntTagValue(uint8 tagname) const
139 ArrayOfCTag::const_iterator it = m_taglist.begin();
140 for (; it != m_taglist.end(); ++it){
141 if (((*it).GetNameID() == tagname) && (*it).IsInt()) {
142 return (*it).GetInt();
145 return 0;
148 bool CAbstractFile::GetIntTagValue(uint8 tagname, uint32& ruValue) const
150 ArrayOfCTag::const_iterator it = m_taglist.begin();
151 for (; it != m_taglist.end(); ++it){
152 if (((*it).GetNameID() == tagname) && (*it).IsInt()){
153 ruValue = (*it).GetInt();
154 return true;
157 return false;
160 uint32 CAbstractFile::GetIntTagValue(const wxString& tagname) const
162 ArrayOfCTag::const_iterator it = m_taglist.begin();
163 for (; it != m_taglist.end(); ++it){
164 if ((*it).IsInt() && ((*it).GetName() == tagname)) {
165 return (*it).GetInt();
168 return 0;
171 const wxString& CAbstractFile::GetStrTagValue(uint8 tagname) const
173 ArrayOfCTag::const_iterator it = m_taglist.begin();
174 for (; it != m_taglist.end(); ++it){
175 if ((*it).GetNameID() == tagname && (*it).IsStr()) {
176 return (*it).GetStr();
179 return EmptyString;
182 const wxString& CAbstractFile::GetStrTagValue(const wxString& tagname) const
184 ArrayOfCTag::const_iterator it = m_taglist.begin();
185 for (; it != m_taglist.end(); ++it){
186 if ((*it).IsStr() && ((*it).GetName() == tagname)) {
187 return (*it).GetStr();
190 return EmptyString;
193 const CTag *CAbstractFile::GetTag(uint8 tagname, uint8 tagtype) const
195 ArrayOfCTag::const_iterator it = m_taglist.begin();
196 for (; it != m_taglist.end(); ++it){
197 if ((*it).GetNameID() == tagname && (*it).GetType() == tagtype) {
198 return &(*it);
201 return NULL;
204 const CTag *CAbstractFile::GetTag(const wxString& tagname, uint8 tagtype) const
206 ArrayOfCTag::const_iterator it = m_taglist.begin();
207 for (; it != m_taglist.end(); ++it){
208 if ((*it).GetType() == tagtype && (*it).GetName() == tagname) {
209 return &(*it);
212 return NULL;
215 const CTag *CAbstractFile::GetTag(uint8 tagname) const
217 ArrayOfCTag::const_iterator it = m_taglist.begin();
218 for (; it != m_taglist.end(); ++it){
219 if ((*it).GetNameID() == tagname) {
220 return &(*it);
223 return NULL;
226 const CTag *CAbstractFile::GetTag(const wxString& tagname) const
228 ArrayOfCTag::const_iterator it = m_taglist.begin();
229 for (; it != m_taglist.end(); ++it){
230 if ((*it).GetName() == tagname) {
231 return &(*it);
234 return NULL;
237 void CAbstractFile::AddTagUnique(const CTag &rTag)
239 ArrayOfCTag::iterator it = m_taglist.begin();
240 for (; it != m_taglist.end(); ++it) {
241 if ( ( ((*it).GetNameID() != 0 &&
242 (*it).GetNameID() == rTag.GetNameID()) ||
243 (!(*it).GetName().IsEmpty() &&
244 !rTag.GetName().IsEmpty() &&
245 (*it).GetName() == rTag.GetName()) ) &&
246 (*it).GetType() == rTag.GetType())
248 it = m_taglist.erase(it);
249 m_taglist.insert(it, rTag);
250 return;
253 m_taglist.push_back(rTag);
256 #ifndef CLIENT_GUI
257 void CAbstractFile::AddNote(Kademlia::CEntry *pEntry)
259 CKadEntryPtrList::iterator it = m_kadNotes.begin();
260 for (; it != m_kadNotes.end(); ++it) {
261 Kademlia::CEntry* entry = *it;
262 if(entry->m_uIP == pEntry->m_uIP || entry->m_uSourceID == pEntry->m_uSourceID) {
263 delete pEntry;
264 return;
267 m_kadNotes.push_front(pEntry);
269 #else
270 void CAbstractFile::AddNote(Kademlia::CEntry *)
273 #endif
276 /* Known File */
278 CKnownFile::CKnownFile()
280 Init();
283 CKnownFile::CKnownFile(uint32 ecid) : CECID(ecid)
285 Init();
289 //#warning Experimental: Construct a CKnownFile from a CSearchFile
290 CKnownFile::CKnownFile(const CSearchFile &searchFile)
292 // This will copy the file hash
293 CAbstractFile(static_cast<const CAbstractFile &>(searchFile))
295 Init();
297 // Use CKnownFile::SetFileName()
298 SetFileName(searchFile.GetFileName());
300 // Use CKnownFile::SetFileSize()
301 SetFileSize(searchFile.GetFileSize());
305 void CKnownFile::Init()
307 m_showSources = false;
308 m_showPeers = false;
309 m_nCompleteSourcesTime = time(NULL);
310 m_nCompleteSourcesCount = 0;
311 m_nCompleteSourcesCountLo = 0;
312 m_nCompleteSourcesCountHi = 0;
313 m_bCommentLoaded = false;
314 m_iPartCount = 0;
315 m_iED2KPartCount = 0;
316 m_iED2KPartHashCount = 0;
317 m_PublishedED2K = false;
318 kadFileSearchID = 0;
319 m_lastPublishTimeKadSrc = 0;
320 m_lastPublishTimeKadNotes = 0;
321 m_lastBuddyIP = 0;
322 m_lastDateChanged = 0;
323 m_bAutoUpPriority = thePrefs::GetNewAutoUp();
324 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
325 m_hashingProgress = 0;
327 statistic.fileParent = this;
329 #ifndef CLIENT_GUI
330 m_pAICHHashSet = new CAICHHashSet(this);
331 #endif
335 void CKnownFile::SetFileSize(uint64 nFileSize)
337 CAbstractFile::SetFileSize(nFileSize);
338 #ifndef CLIENT_GUI
339 m_pAICHHashSet->SetFileSize(nFileSize);
340 #endif
342 // Examples of parthashs, hashsets and filehashs for different filesizes
343 // according the ed2k protocol
344 //----------------------------------------------------------------------
346 //File size: 3 bytes
347 //File hash: 2D55E87D0E21F49B9AD25F98531F3724
348 //Nr. hashs: 0
351 //File size: 1*PARTSIZE
352 //File hash: A72CA8DF7F07154E217C236C89C17619
353 //Nr. hashs: 2
354 //Hash[ 0]: 4891ED2E5C9C49F442145A3A5F608299
355 //Hash[ 1]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
358 //File size: 1*PARTSIZE + 1 byte
359 //File hash: 2F620AE9D462CBB6A59FE8401D2B3D23
360 //Nr. hashs: 2
361 //Hash[ 0]: 121795F0BEDE02DDC7C5426D0995F53F
362 //Hash[ 1]: C329E527945B8FE75B3C5E8826755747
365 //File size: 2*PARTSIZE
366 //File hash: A54C5E562D5E03CA7D77961EB9A745A4
367 //Nr. hashs: 3
368 //Hash[ 0]: B3F5CE2A06BF403BFB9BFFF68BDDC4D9
369 //Hash[ 1]: 509AA30C9EA8FC136B1159DF2F35B8A9
370 //Hash[ 2]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
373 //File size: 3*PARTSIZE
374 //File hash: 5E249B96F9A46A18FC2489B005BF2667
375 //Nr. hashs: 4
376 //Hash[ 0]: 5319896A2ECAD43BF17E2E3575278E72
377 //Hash[ 1]: D86EF157D5E49C5ED502EDC15BB5F82B
378 //Hash[ 2]: 10F2D5B1FCB95C0840519C58D708480F
379 //Hash[ 3]: 31D6CFE0D16AE931B73C59D7E0C089C0 *special part hash*
382 //File size: 3*PARTSIZE + 1 byte
383 //File hash: 797ED552F34380CAFF8C958207E40355
384 //Nr. hashs: 4
385 //Hash[ 0]: FC7FD02CCD6987DCF1421F4C0AF94FB8
386 //Hash[ 1]: 2FE466AF8A7C06DA3365317B75A5ACFE
387 //Hash[ 2]: 873D3BF52629F7C1527C6E8E473C1C30
388 //Hash[ 3]: BCE50BEE7877BB07BB6FDA56BFE142FB
391 // File size Data parts ED2K parts ED2K part hashs
392 // ---------------------------------------------------------------
393 // 1..PARTSIZE-1 1 1 0(!)
394 // PARTSIZE 1 2(!) 2(!)
395 // PARTSIZE+1 2 2 2
396 // PARTSIZE*2 2 3(!) 3(!)
397 // PARTSIZE*2+1 3 3 3
399 if (nFileSize == 0){
400 //wxFAIL; // Kry - Why commented out by lemonfan? it can never be 0
401 m_iPartCount = 0;
402 m_iED2KPartCount = 0;
403 m_iED2KPartHashCount = 0;
404 m_sizeLastPart = 0;
405 return;
408 // nr. of data parts
409 m_iPartCount = nFileSize / PARTSIZE + 1;
410 // size of last part
411 m_sizeLastPart = nFileSize % PARTSIZE;
412 // file with size of n * PARTSIZE
413 if (m_sizeLastPart == 0) {
414 m_sizeLastPart = PARTSIZE;
415 m_iPartCount--;
418 // nr. of parts to be used with OP_FILESTATUS
419 m_iED2KPartCount = nFileSize / PARTSIZE + 1;
421 // nr. of parts to be used with OP_HASHSETANSWER
422 m_iED2KPartHashCount = nFileSize / PARTSIZE;
423 if (m_iED2KPartHashCount != 0) {
424 m_iED2KPartHashCount += 1;
429 void CKnownFile::AddUploadingClient(CUpDownClient* client)
431 m_ClientUploadList.insert(CCLIENTREF(client, wxT("CKnownFile::AddUploadingClient m_ClientUploadList")));
433 SourceItemType type = UNAVAILABLE_SOURCE;
434 switch (client->GetUploadState()) {
435 case US_UPLOADING:
436 case US_ONUPLOADQUEUE:
437 type = AVAILABLE_SOURCE;
438 break;
439 default: {
440 // Any other state is UNAVAILABLE_SOURCE by default.
444 Notify_SharedCtrlAddClient(this, CCLIENTREF(client, wxT("CKnownFile::AddUploadingClient Notify_SharedCtrlAddClient")), type);
446 UpdateAutoUpPriority();
450 void CKnownFile::RemoveUploadingClient(CUpDownClient* client)
452 if (m_ClientUploadList.erase(CCLIENTREF(client, wxEmptyString))) {
453 Notify_SharedCtrlRemoveClient(client->ECID(), this);
454 UpdateAutoUpPriority();
459 #ifdef CLIENT_GUI
461 CKnownFile::CKnownFile(CEC_SharedFile_Tag *tag) : CECID(tag->ID())
463 Init();
465 m_abyFileHash = tag->FileHash();
466 SetFileSize(tag->SizeFull());
467 m_AvailPartFrequency.insert(m_AvailPartFrequency.end(), m_iPartCount, 0);
468 m_queuedCount = 0;
471 CKnownFile::~CKnownFile()
475 void CKnownFile::UpdateAutoUpPriority()
480 #else // ! CLIENT_GUI
482 CKnownFile::~CKnownFile()
484 SourceSet::iterator it = m_ClientUploadList.begin();
485 for ( ; it != m_ClientUploadList.end(); ++it ) {
486 it->ClearUploadFileID();
489 delete m_pAICHHashSet;
493 void CKnownFile::SetFilePath(const CPath& filePath)
495 m_filePath = filePath;
499 // needed for memfiles. its probably better to switch everything to CFile...
500 bool CKnownFile::LoadHashsetFromFile(const CFileDataIO* file, bool checkhash)
502 CMD4Hash checkid = file->ReadHash();
504 uint16 parts = file->ReadUInt16();
505 m_hashlist.clear();
506 for (uint16 i = 0; i < parts; ++i){
507 CMD4Hash cur_hash = file->ReadHash();
508 m_hashlist.push_back(cur_hash);
511 // SLUGFILLER: SafeHash - always check for valid m_hashlist
512 if (!checkhash){
513 m_abyFileHash = checkid;
514 if (parts <= 1) { // nothing to check
515 return true;
517 } else {
518 if ( m_abyFileHash != checkid ) {
519 return false; // wrong file?
520 } else {
521 if (parts != GetED2KPartHashCount()) {
522 return false;
526 // SLUGFILLER: SafeHash
528 // trust noone ;-)
529 // lol, useless comment but made me lmao
530 // wtf you guys are weird.
532 if (!m_hashlist.empty()) {
533 CreateHashFromHashlist(m_hashlist, &checkid);
536 if ( m_abyFileHash == checkid ) {
537 return true;
538 } else {
539 m_hashlist.clear();
540 return false;
545 bool CKnownFile::LoadTagsFromFile(const CFileDataIO* file)
547 uint32 tagcount = file->ReadUInt32();
548 m_taglist.clear();
549 for (uint32 j = 0; j != tagcount; ++j) {
550 CTag newtag(*file, true);
551 switch(newtag.GetNameID()){
552 case FT_FILENAME:
553 if (GetFileName().IsOk()) {
554 // Unlike eMule, we actually prefer the second
555 // filename tag, since we use it to specify the
556 // 'universial' filename (see CPath::ToUniv).
557 CPath path = CPath::FromUniv(newtag.GetStr());
559 // May be invalid, if from older versions where
560 // unicoded filenames be saved as empty-strings.
561 if (path.IsOk()) {
562 SetFileName(path);
564 } else {
565 SetFileName(CPath(newtag.GetStr()));
567 break;
569 case FT_FILESIZE:
570 SetFileSize(newtag.GetInt());
571 m_AvailPartFrequency.clear();
572 m_AvailPartFrequency.insert(
573 m_AvailPartFrequency.begin(),
574 GetPartCount(), 0);
575 break;
577 case FT_ATTRANSFERRED:
578 statistic.alltimetransferred += newtag.GetInt();
579 break;
581 case FT_ATTRANSFERREDHI:
582 statistic.alltimetransferred =
583 (((uint64)newtag.GetInt()) << 32) +
584 ((uint64)statistic.alltimetransferred);
585 break;
587 case FT_ATREQUESTED:
588 statistic.alltimerequested = newtag.GetInt();
589 break;
591 case FT_ATACCEPTED:
592 statistic.alltimeaccepted = newtag.GetInt();
593 break;
595 case FT_ULPRIORITY:
596 m_iUpPriority = newtag.GetInt();
597 if( m_iUpPriority == PR_AUTO ){
598 m_iUpPriority = PR_HIGH;
599 m_bAutoUpPriority = true;
600 } else {
601 if ( m_iUpPriority != PR_VERYLOW &&
602 m_iUpPriority != PR_LOW &&
603 m_iUpPriority != PR_NORMAL &&
604 m_iUpPriority != PR_HIGH &&
605 m_iUpPriority != PR_VERYHIGH &&
606 m_iUpPriority != PR_POWERSHARE) {
607 m_iUpPriority = PR_NORMAL;
610 m_bAutoUpPriority = false;
612 break;
614 case FT_PERMISSIONS:
615 // Ignore it, it's not used anymore.
616 break;
618 case FT_AICH_HASH: {
619 CAICHHash hash;
620 bool hashSizeOk =
621 hash.DecodeBase32(newtag.GetStr()) == CAICHHash::GetHashSize();
622 wxASSERT(hashSizeOk);
623 if (hashSizeOk) {
624 m_pAICHHashSet->SetMasterHash(hash, AICH_HASHSETCOMPLETE);
626 break;
629 case FT_KADLASTPUBLISHSRC:
630 SetLastPublishTimeKadSrc( newtag.GetInt(), 0 );
632 if(GetLastPublishTimeKadSrc() > (uint32)time(NULL)+KADEMLIAREPUBLISHTIMES) {
633 //There may be a posibility of an older client that saved a random number here.. This will check for that..
634 SetLastPublishTimeKadSrc(0, 0);
636 break;
638 case FT_KADLASTPUBLISHNOTES:
639 SetLastPublishTimeKadNotes( newtag.GetInt() );
640 break;
642 case FT_KADLASTPUBLISHKEY:
643 // Just purge it
644 wxASSERT( newtag.IsInt() );
645 break;
647 default:
648 // Store them here and write them back on saving.
649 m_taglist.push_back(newtag);
653 return true;
657 bool CKnownFile::LoadDateFromFile(const CFileDataIO* file)
659 m_lastDateChanged = file->ReadUInt32();
661 return true;
665 bool CKnownFile::LoadFromFile(const CFileDataIO* file)
667 // SLUGFILLER: SafeHash - load first, verify later
668 bool ret1 = LoadDateFromFile(file);
669 bool ret2 = LoadHashsetFromFile(file,false);
670 bool ret3 = LoadTagsFromFile(file);
671 UpdatePartsInfo();
672 // Final hash-count verification, needs to be done after the tags are loaded.
673 return ret1 && ret2 && ret3 && GetED2KPartHashCount()==GetHashCount();
674 // SLUGFILLER: SafeHash
678 bool CKnownFile::WriteToFile(CFileDataIO* file)
680 wxCHECK(!IsPartFile(), false);
682 // date
683 file->WriteUInt32(m_lastDateChanged);
684 // hashset
685 file->WriteHash(m_abyFileHash);
687 uint16 parts = m_hashlist.size();
688 file->WriteUInt16(parts);
690 for (int i = 0; i < parts; ++i)
691 file->WriteHash(m_hashlist[i]);
693 //tags
694 const int iFixedTags = 8;
695 uint32 tagcount = iFixedTags;
696 if (HasProperAICHHashSet()) {
697 tagcount++;
699 // Float meta tags are currently not written. All older eMule versions < 0.28a have
700 // a bug in the meta tag reading+writing code. To achive maximum backward
701 // compatibility for met files with older eMule versions we just don't write float
702 // tags. This is OK, because we (eMule) do not use float tags. The only float tags
703 // we may have to handle is the '# Sent' tag from the Hybrid, which is pretty
704 // useless but may be received from us via the servers.
706 // The code for writing the float tags SHOULD BE ENABLED in SOME MONTHS (after most
707 // people are using the newer eMule versions which do not write broken float tags).
708 for (size_t j = 0; j < m_taglist.size(); ++j){
709 if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
710 ++tagcount;
714 if (m_lastPublishTimeKadSrc) {
715 ++tagcount;
718 if (m_lastPublishTimeKadNotes){
719 ++tagcount;
722 // standard tags
724 file->WriteUInt32(tagcount);
726 // We still save the unicoded filename, for backwards
727 // compatibility with pre-2.2 and other clients.
728 CTagString nametag_unicode(FT_FILENAME, GetFileName().GetRaw());
729 // We write it with BOM to kep eMule compatibility
730 nametag_unicode.WriteTagToFile(file,utf8strOptBOM);
732 // The non-unicoded filename is written in an 'universial'
733 // format, which allows us to identify files, even if the
734 // system locale changes.
735 CTagString nametag(FT_FILENAME, CPath::ToUniv(GetFileName()));
736 nametag.WriteTagToFile(file);
738 CTagIntSized sizetag(FT_FILESIZE, GetFileSize(), IsLargeFile() ? 64 : 32);
739 sizetag.WriteTagToFile(file);
741 // statistic
742 uint32 tran;
743 tran=statistic.alltimetransferred & 0xFFFFFFFF;
744 CTagInt32 attag1(FT_ATTRANSFERRED, tran);
745 attag1.WriteTagToFile(file);
747 tran=statistic.alltimetransferred>>32;
748 CTagInt32 attag4(FT_ATTRANSFERREDHI, tran);
749 attag4.WriteTagToFile(file);
751 CTagInt32 attag2(FT_ATREQUESTED, statistic.GetAllTimeRequests());
752 attag2.WriteTagToFile(file);
754 CTagInt32 attag3(FT_ATACCEPTED, statistic.GetAllTimeAccepts());
755 attag3.WriteTagToFile(file);
757 // priority N permission
758 CTagInt32 priotag(FT_ULPRIORITY, IsAutoUpPriority() ? PR_AUTO : m_iUpPriority);
759 priotag.WriteTagToFile(file);
761 //AICH Filehash
762 if (HasProperAICHHashSet()) {
763 CTagString aichtag(FT_AICH_HASH, m_pAICHHashSet->GetMasterHash().GetString());
764 aichtag.WriteTagToFile(file);
767 // Kad sources
768 if (m_lastPublishTimeKadSrc){
769 CTagInt32 kadLastPubSrc(FT_KADLASTPUBLISHSRC, m_lastPublishTimeKadSrc);
770 kadLastPubSrc.WriteTagToFile(file);
773 // Kad notes
774 if (m_lastPublishTimeKadNotes){
775 CTagInt32 kadLastPubNotes(FT_KADLASTPUBLISHNOTES, m_lastPublishTimeKadNotes);
776 kadLastPubNotes.WriteTagToFile(file);
779 //other tags
780 for (size_t j = 0; j < m_taglist.size(); ++j){
781 if (m_taglist[j].IsInt() || m_taglist[j].IsStr()) {
782 m_taglist[j].WriteTagToFile(file);
785 return true;
789 void CKnownFile::CreateHashFromHashlist(const ArrayOfCMD4Hash& hashes, CMD4Hash* Output)
791 wxCHECK_RET(hashes.size(), wxT("No input to hash from in CreateHashFromHashlist"));
793 std::vector<byte> buffer(hashes.size() * MD4HASH_LENGTH);
794 std::vector<byte>::iterator it = buffer.begin();
796 for (size_t i = 0; i < hashes.size(); ++i) {
797 it = STLCopy_n(hashes[i].GetHash(), MD4HASH_LENGTH, it);
800 CreateHashFromInput(&buffer[0], buffer.size(), Output, NULL);
804 void CKnownFile::CreateHashFromFile(CFileAutoClose& file, uint64 offset, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut)
806 wxCHECK_RET(Length, wxT("No input to hash from in CreateHashFromFile"));
808 CFileArea area;
809 area.ReadAt(file, offset, Length);
811 CreateHashFromInput(area.GetBuffer(), Length, Output, pShaHashOut);
812 area.CheckError();
816 void CKnownFile::CreateHashFromInput(const byte* input, uint32 Length, CMD4Hash* Output, CAICHHashTree* pShaHashOut )
818 wxASSERT_MSG(Output || pShaHashOut, wxT("Nothing to do in CreateHashFromInput"));
819 { wxCHECK_RET(input, wxT("No input to hash from in CreateHashFromInput")); }
820 wxASSERT(Length <= PARTSIZE); // We never hash more than one PARTSIZE
822 CMemFile data(input, Length);
824 uint32 Required = Length;
825 byte X[64*128];
827 uint32 posCurrentEMBlock = 0;
828 uint32 nIACHPos = 0;
829 CScopedPtr<CAICHHashAlgo> pHashAlg(CAICHHashSet::GetNewHashAlgo());
831 // This is all AICH.
832 while (Required >= 64) {
833 uint32 len = Required / 64;
834 if (len > sizeof(X)/(64 * sizeof(X[0]))) {
835 len = sizeof(X)/(64 * sizeof(X[0]));
838 data.Read(&X, len * 64);
840 // SHA hash needs 180KB blocks
841 if (pShaHashOut) {
842 if (nIACHPos + len*64 >= EMBLOCKSIZE) {
843 uint32 nToComplete = EMBLOCKSIZE - nIACHPos;
844 pHashAlg->Add(X, nToComplete);
845 wxASSERT( nIACHPos + nToComplete == EMBLOCKSIZE );
846 pShaHashOut->SetBlockHash(EMBLOCKSIZE, posCurrentEMBlock, pHashAlg.get());
847 posCurrentEMBlock += EMBLOCKSIZE;
848 pHashAlg->Reset();
849 pHashAlg->Add(X+nToComplete,(len*64) - nToComplete);
850 nIACHPos = (len*64) - nToComplete;
852 else{
853 pHashAlg->Add(X, len*64);
854 nIACHPos += len*64;
858 Required -= len*64;
860 // bytes to read
861 Required = Length % 64;
862 if (Required != 0){
863 data.Read(&X,Required);
865 if (pShaHashOut != NULL){
866 if (nIACHPos + Required >= EMBLOCKSIZE){
867 uint32 nToComplete = EMBLOCKSIZE - nIACHPos;
868 pHashAlg->Add(X, nToComplete);
869 wxASSERT( nIACHPos + nToComplete == EMBLOCKSIZE );
870 pShaHashOut->SetBlockHash(EMBLOCKSIZE, posCurrentEMBlock, pHashAlg.get());
871 posCurrentEMBlock += EMBLOCKSIZE;
872 pHashAlg->Reset();
873 pHashAlg->Add(X+nToComplete, Required - nToComplete);
874 nIACHPos = Required - nToComplete;
876 else{
877 pHashAlg->Add(X, Required);
878 nIACHPos += Required;
882 if (pShaHashOut != NULL){
883 if(nIACHPos > 0){
884 pShaHashOut->SetBlockHash(nIACHPos, posCurrentEMBlock, pHashAlg.get());
885 posCurrentEMBlock += nIACHPos;
887 wxASSERT( posCurrentEMBlock == Length );
888 wxCHECK2( pShaHashOut->ReCalculateHash(pHashAlg.get(), false), );
891 if (Output != NULL){
892 #ifdef __WEAK_CRYPTO__
893 CryptoPP::Weak::MD4 md4_hasher;
894 #else
895 CryptoPP::MD4 md4_hasher;
896 #endif
897 md4_hasher.CalculateDigest(Output->GetHash(), input, Length);
902 const CMD4Hash& CKnownFile::GetPartHash(uint16 part) const {
903 wxASSERT( part < m_hashlist.size() );
905 return m_hashlist[part];
908 CPacket* CKnownFile::CreateSrcInfoPacket(const CUpDownClient* forClient, uint8 byRequestedVersion, uint16 nRequestedOptions)
910 // Kad reviewed
912 if (m_ClientUploadList.empty()) {
913 return NULL;
916 if ((((CKnownFile*)forClient->GetRequestFile() != this)
917 && ((CKnownFile*)forClient->GetUploadFile() != this)) || forClient->GetUploadFileID() != GetFileHash()) {
918 wxString file1 = _("Unknown");
919 if (forClient->GetRequestFile() && forClient->GetRequestFile()->GetFileName().IsOk()) {
920 file1 = forClient->GetRequestFile()->GetFileName().GetPrintable();
921 } else if (forClient->GetUploadFile() && forClient->GetUploadFile()->GetFileName().IsOk()) {
922 file1 = forClient->GetUploadFile()->GetFileName().GetPrintable();
924 wxString file2 = _("Unknown");
925 if (GetFileName().IsOk()) {
926 file2 = GetFileName().GetPrintable();
928 AddDebugLogLineN(logKnownFiles, wxT("File mismatch on source packet (K) Sending: ") + file1 + wxT(" From: ") + file2);
929 return NULL;
932 const BitVector& rcvstatus = forClient->GetUpPartStatus();
933 bool SupportsUploadChunksState = !rcvstatus.empty();
934 //wxASSERT(rcvstatus.size() == GetPartCount()); // Obviously!
935 if (rcvstatus.size() != GetPartCount()) {
936 // Yuck. Same file but different part count? Seriously fucked up.
937 AddDebugLogLineN(logKnownFiles, CFormat(wxT("Impossible situation: different partcounts for the same known file: %i (client) and %i (file)")) % rcvstatus.size() % GetPartCount());
938 return NULL;
941 CMemFile data(1024);
943 uint8 byUsedVersion;
944 bool bIsSX2Packet;
945 if (forClient->SupportsSourceExchange2() && byRequestedVersion > 0){
946 // the client uses SourceExchange2 and requested the highest version he knows
947 // and we send the highest version we know, but of course not higher than his request
948 byUsedVersion = std::min(byRequestedVersion, (uint8)SOURCEEXCHANGE2_VERSION);
949 bIsSX2Packet = true;
950 data.WriteUInt8(byUsedVersion);
952 // we don't support any special SX2 options yet, reserved for later use
953 if (nRequestedOptions != 0) {
954 AddDebugLogLineN(logKnownFiles, CFormat(wxT("Client requested unknown options for SourceExchange2: %u")) % nRequestedOptions);
956 } else {
957 byUsedVersion = forClient->GetSourceExchange1Version();
958 bIsSX2Packet = false;
959 if (forClient->SupportsSourceExchange2()) {
960 AddDebugLogLineN(logKnownFiles, wxT("Client which announced to support SX2 sent SX1 packet instead"));
964 uint16 nCount = 0;
966 data.WriteHash(forClient->GetUploadFileID());
967 data.WriteUInt16(nCount);
968 uint32 cDbgNoSrc = 0;
970 SourceSet::iterator it = m_ClientUploadList.begin();
971 for ( ; it != m_ClientUploadList.end(); it++ ) {
972 const CUpDownClient *cur_src = it->GetClient();
974 if ( cur_src->HasLowID() ||
975 cur_src == forClient ||
976 !( cur_src->GetUploadState() == US_UPLOADING ||
977 cur_src->GetUploadState() == US_ONUPLOADQUEUE)) {
978 continue;
981 bool bNeeded = false;
983 if ( SupportsUploadChunksState ) {
984 const BitVector& srcstatus = cur_src->GetUpPartStatus();
985 if ( !srcstatus.empty() ) {
986 //wxASSERT(srcstatus.size() == GetPartCount()); // Obviously!
987 if (srcstatus.size() != GetPartCount()) {
988 continue;
990 if ( cur_src->GetUpPartCount() == forClient->GetUpPartCount() ) {
991 for (int x = 0; x < GetPartCount(); x++ ) {
992 if ( srcstatus.get(x) && !rcvstatus.get(x) ) {
993 // We know the receiving client needs
994 // a chunk from this client.
995 bNeeded = true;
996 break;
1000 } else {
1001 cDbgNoSrc++;
1002 // This client doesn't support upload chunk status.
1003 // So just send it and hope for the best.
1004 bNeeded = true;
1006 } else {
1007 // remote client does not support upload chunk status,
1008 // search sources which have at least one complete part
1009 // we could even sort the list of sources by available
1010 // chunks to return as much sources as possible which
1011 // have the most available chunks. but this could be
1012 // a noticeable performance problem.
1013 const BitVector& srcstatus = cur_src->GetUpPartStatus();
1014 if ( !srcstatus.empty() ) {
1015 //wxASSERT(srcstatus.size() == GetPartCount());
1016 if (srcstatus.size() != GetPartCount()) {
1017 continue;
1019 for (int x = 0; x < GetPartCount(); x++ ) {
1020 if ( srcstatus.get(x) ) {
1021 // this client has at least one chunk
1022 bNeeded = true;
1023 break;
1026 } else {
1027 // This client doesn't support upload chunk status.
1028 // So just send it and hope for the best.
1029 bNeeded = true;
1033 if ( bNeeded ) {
1034 nCount++;
1035 uint32 dwID;
1036 if(byUsedVersion >= 3) {
1037 dwID = cur_src->GetUserIDHybrid();
1038 } else {
1039 dwID = cur_src->GetIP();
1041 data.WriteUInt32(dwID);
1042 data.WriteUInt16(cur_src->GetUserPort());
1043 data.WriteUInt32(cur_src->GetServerIP());
1044 data.WriteUInt16(cur_src->GetServerPort());
1046 if (byUsedVersion >= 2) {
1047 data.WriteHash(cur_src->GetUserHash());
1050 if (byUsedVersion >= 4){
1051 // CryptSettings - SourceExchange V4
1052 // 5 Reserved (!)
1053 // 1 CryptLayer Required
1054 // 1 CryptLayer Requested
1055 // 1 CryptLayer Supported
1056 const uint8 uSupportsCryptLayer = cur_src->SupportsCryptLayer() ? 1 : 0;
1057 const uint8 uRequestsCryptLayer = cur_src->RequestsCryptLayer() ? 1 : 0;
1058 const uint8 uRequiresCryptLayer = cur_src->RequiresCryptLayer() ? 1 : 0;
1059 const uint8 byCryptOptions = (uRequiresCryptLayer << 2) | (uRequestsCryptLayer << 1) | (uSupportsCryptLayer << 0);
1060 data.WriteUInt8(byCryptOptions);
1063 if (nCount > 500) {
1064 break;
1069 if (!nCount) {
1070 return 0;
1073 data.Seek(bIsSX2Packet ? 17 : 16, wxFromStart);
1074 data.WriteUInt16(nCount);
1076 CPacket* result = new CPacket(data, OP_EMULEPROT, bIsSX2Packet ? OP_ANSWERSOURCES2 : OP_ANSWERSOURCES);
1078 if ( result->GetPacketSize() > 354 ) {
1079 result->PackPacket();
1082 return result;
1086 void CKnownFile::CreateOfferedFilePacket(
1087 CMemFile *files,
1088 CServer *pServer,
1089 CUpDownClient *pClient) {
1091 // This function is used for offering files to the local server and for sending
1092 // shared files to some other client. In each case we send our IP+Port only, if
1093 // we have a HighID.
1095 wxASSERT(!(pClient && pServer));
1097 SetPublishedED2K(true);
1098 files->WriteHash(GetFileHash());
1100 uint32 nClientID = 0;
1101 uint16 nClientPort = 0;
1103 if (pServer) {
1104 if (pServer->GetTCPFlags() & SRV_TCPFLG_COMPRESSION) {
1105 #define FILE_COMPLETE_ID 0xfbfbfbfb
1106 #define FILE_COMPLETE_PORT 0xfbfb
1107 #define FILE_INCOMPLETE_ID 0xfcfcfcfc
1108 #define FILE_INCOMPLETE_PORT 0xfcfc
1109 // complete file: ip 251.251.251 (0xfbfbfbfb) port 0xfbfb
1110 // incomplete file: op 252.252.252 (0xfcfcfcfc) port 0xfcfc
1111 if (GetStatus() == PS_COMPLETE) {
1112 nClientID = FILE_COMPLETE_ID;
1113 nClientPort = FILE_COMPLETE_PORT;
1114 } else {
1115 nClientID = FILE_INCOMPLETE_ID;
1116 nClientPort = FILE_INCOMPLETE_PORT;
1118 } else {
1119 if (theApp->IsConnectedED2K() && !::IsLowID(theApp->GetED2KID())){
1120 nClientID = theApp->GetID();
1121 nClientPort = thePrefs::GetPort();
1124 } else {
1125 // Do not merge this with the above case - this one
1126 // also checks Kad status.
1127 if (theApp->IsConnected() && !theApp->IsFirewalled()) {
1128 nClientID = theApp->GetID();
1129 nClientPort = thePrefs::GetPort();
1133 files->WriteUInt32(nClientID);
1134 files->WriteUInt16(nClientPort);
1136 TagPtrList tags;
1138 // The printable filename is used because it's destined for another user.
1139 tags.push_back(new CTagString(FT_FILENAME, GetFileName().GetPrintable()));
1141 if (pClient && pClient->GetVBTTags()) {
1142 tags.push_back(new CTagVarInt(FT_FILESIZE, GetFileSize()));
1143 } else {
1144 if (!IsLargeFile()){
1145 tags.push_back(new CTagInt32(FT_FILESIZE, GetFileSize()));
1146 } else {
1147 // Large file
1148 // we send 2*32 bit tags to servers, but a real 64 bit tag to other clients.
1149 if (pServer) {
1150 if (!pServer->SupportsLargeFilesTCP()){
1151 wxFAIL;
1152 tags.push_back(new CTagInt32(FT_FILESIZE, 0));
1153 } else {
1154 tags.push_back(new CTagInt32(FT_FILESIZE, (uint32)GetFileSize()));
1155 tags.push_back(new CTagInt32(FT_FILESIZE_HI, (uint32)(GetFileSize() >> 32)));
1157 } else {
1158 if (!pClient->SupportsLargeFiles()) {
1159 wxFAIL;
1160 tags.push_back(new CTagInt32(FT_FILESIZE, 0));
1161 } else {
1162 tags.push_back(new CTagInt64(FT_FILESIZE, GetFileSize()));
1168 if (GetFileRating()) {
1169 tags.push_back(new CTagVarInt(FT_FILERATING, GetFileRating(), (pClient && pClient->GetVBTTags()) ? 0 : 32));
1172 // NOTE: Archives and CD-Images are published+searched with file type "Pro"
1173 bool bAddedFileType = false;
1174 if (pServer && (pServer->GetTCPFlags() & SRV_TCPFLG_TYPETAGINTEGER)) {
1175 // Send integer file type tags to newer servers
1176 EED2KFileType eFileType = GetED2KFileTypeSearchID(GetED2KFileTypeID(GetFileName()));
1177 if (eFileType >= ED2KFT_AUDIO && eFileType <= ED2KFT_CDIMAGE) {
1178 tags.push_back(new CTagInt32(FT_FILETYPE, eFileType));
1179 bAddedFileType = true;
1182 if (!bAddedFileType) {
1183 // Send string file type tags to:
1184 // - newer servers, in case there is no integer type available for the file type (e.g. emulecollection)
1185 // - older servers
1186 // - all clients
1187 wxString strED2KFileType(GetED2KFileTypeSearchTerm(GetED2KFileTypeID(GetFileName())));
1188 if (!strED2KFileType.IsEmpty()) {
1189 tags.push_back(new CTagString(FT_FILETYPE, strED2KFileType));
1193 // There, we could add MetaData info, if we ever get to have that.
1195 EUtf8Str eStrEncode;
1197 bool unicode_support =
1198 // eservers that support UNICODE.
1199 (pServer && (pServer->GetUnicodeSupport()))
1201 // clients that support unicode
1202 (pClient && pClient->GetUnicodeSupport());
1203 eStrEncode = unicode_support ? utf8strRaw : utf8strNone;
1205 files->WriteUInt32(tags.size());
1207 // Sadly, eMule doesn't use a MISCOPTIONS flag on hello packet for this, so we
1208 // have to identify the support for new tags by version.
1209 bool new_ed2k =
1210 // eMule client > 0.42f
1211 (pClient && pClient->IsEmuleClient() && pClient->GetVersion() >= MAKE_CLIENT_VERSION(0,42,7))
1213 // aMule >= 2.0.0rc8. Sadly, there's no way to check the rcN number, so I checked
1214 // the rc8 changelog. On rc8 OSInfo was introduced, so...
1215 (pClient && pClient->GetClientSoft() == SO_AMULE && !pClient->GetClientOSInfo().IsEmpty())
1217 // eservers use a flag for this, at least.
1218 (pServer && (pServer->GetTCPFlags() & SRV_TCPFLG_NEWTAGS));
1220 for (TagPtrList::iterator it = tags.begin(); it != tags.end(); ++it ) {
1221 CTag* pTag = *it;
1222 if (new_ed2k) {
1223 pTag->WriteNewEd2kTag(files, eStrEncode);
1224 } else {
1225 pTag->WriteTagToFile(files, eStrEncode);
1227 delete pTag;
1232 // Updates priority of file if autopriority is activated
1233 void CKnownFile::UpdateAutoUpPriority()
1235 if (IsAutoUpPriority()) {
1236 uint32 queued = GetQueuedCount();
1237 uint8 priority = PR_NORMAL;
1239 if (queued > 20) {
1240 priority = PR_LOW;
1241 } else if (queued > 1) {
1242 priority = PR_NORMAL;
1243 } else {
1244 priority = PR_HIGH;
1247 if (GetUpPriority() != priority) {
1248 SetUpPriority(priority, false);
1249 Notify_SharedFilesUpdateItem(this);
1254 void CKnownFile::SetFileCommentRating(const wxString& strNewComment, int8 iNewRating)
1256 if (m_strComment != strNewComment || m_iRating != iNewRating) {
1257 SetLastPublishTimeKadNotes(0);
1258 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1260 wxConfigBase* cfg = wxConfigBase::Get();
1261 if (strNewComment.IsEmpty() && iNewRating == 0) {
1262 cfg->DeleteGroup(strCfgPath);
1263 } else {
1264 cfg->Write( strCfgPath + wxT("Comment"), strNewComment);
1265 cfg->Write( strCfgPath + wxT("Rate"), (int)iNewRating);
1268 m_strComment = strNewComment;
1269 m_iRating = iNewRating;
1271 SourceSet::iterator it = m_ClientUploadList.begin();
1272 for ( ; it != m_ClientUploadList.end(); it++ ) {
1273 it->SetCommentDirty();
1279 void CKnownFile::SetUpPriority(uint8 iNewUpPriority, bool m_bsave){
1280 m_iUpPriority = iNewUpPriority;
1281 if( IsPartFile() && m_bsave ) {
1282 ((CPartFile*)this)->SavePartFile();
1286 void CKnownFile::SetPublishedED2K(bool val){
1287 m_PublishedED2K = val;
1288 Notify_SharedFilesUpdateItem(this);
1291 bool CKnownFile::PublishNotes()
1293 if(m_lastPublishTimeKadNotes > (uint32)time(NULL)) {
1294 return false;
1297 if(!GetFileComment().IsEmpty()) {
1298 m_lastPublishTimeKadNotes = (uint32)time(NULL)+KADEMLIAREPUBLISHTIMEN;
1299 return true;
1302 if(GetFileRating() != 0) {
1303 m_lastPublishTimeKadNotes = (uint32)time(NULL)+KADEMLIAREPUBLISHTIMEN;
1304 return true;
1307 return false;
1310 bool CKnownFile::PublishSrc()
1312 uint32 lastBuddyIP = 0;
1314 if( theApp->IsFirewalled() ) {
1315 CUpDownClient* buddy = theApp->clientlist->GetBuddy();
1316 if( buddy ) {
1317 lastBuddyIP = theApp->clientlist->GetBuddy()->GetIP();
1318 if( lastBuddyIP != m_lastBuddyIP ) {
1319 SetLastPublishTimeKadSrc( (uint32)time(NULL)+KADEMLIAREPUBLISHTIMES, lastBuddyIP );
1320 return true;
1322 } else {
1323 return false;
1327 if(m_lastPublishTimeKadSrc > (uint32)time(NULL)) {
1328 return false;
1331 SetLastPublishTimeKadSrc((uint32)time(NULL)+KADEMLIAREPUBLISHTIMES,lastBuddyIP);
1332 return true;
1336 void CKnownFile::UpdatePartsInfo()
1338 // Cache part count
1339 uint16 partcount = GetPartCount();
1340 bool flag = (time(NULL) - m_nCompleteSourcesTime > 0);
1342 // Ensure the frequency-list is ready
1343 if ( m_AvailPartFrequency.size() != GetPartCount() ) {
1344 m_AvailPartFrequency.clear();
1345 m_AvailPartFrequency.insert(m_AvailPartFrequency.begin(), GetPartCount(), 0);
1348 if (flag) {
1349 ArrayOfUInts16 count;
1350 count.reserve(m_ClientUploadList.size());
1352 SourceSet::iterator it = m_ClientUploadList.begin();
1353 for ( ; it != m_ClientUploadList.end(); it++ ) {
1354 CUpDownClient* client = it->GetClient();
1355 if ( !client->GetUpPartStatus().empty() && client->GetUpPartCount() == partcount ) {
1356 count.push_back(client->GetUpCompleteSourcesCount());
1360 m_nCompleteSourcesCount = m_nCompleteSourcesCountLo = m_nCompleteSourcesCountHi = 0;
1362 if( partcount > 0) {
1363 m_nCompleteSourcesCount = m_AvailPartFrequency[0];
1365 for (uint16 i = 1; i < partcount; ++i) {
1366 if( m_nCompleteSourcesCount > m_AvailPartFrequency[i]) {
1367 m_nCompleteSourcesCount = m_AvailPartFrequency[i];
1370 count.push_back(m_nCompleteSourcesCount);
1372 int32 n = count.size();
1373 if (n > 0) {
1374 std::sort(count.begin(), count.end(), std::less<uint16>());
1376 // calculate range
1377 int i = n >> 1; // (n / 2)
1378 int j = (n * 3) >> 2; // (n * 3) / 4
1379 int k = (n * 7) >> 3; // (n * 7) / 8
1381 // For complete files, trust the people your uploading to more...
1383 // For low guess and normal guess count
1384 // - If we see more sources then the guessed low and
1385 // normal, use what we see.
1386 // - If we see less sources then the guessed low,
1387 // adjust network accounts for 100%, we account for
1388 // 0% with what we see and make sure we are still
1389 // above the normal.
1390 // For high guess
1391 // Adjust 100% network and 0% what we see.
1392 if (n < 20) {
1393 if ( count[i] < m_nCompleteSourcesCount ) {
1394 m_nCompleteSourcesCountLo = m_nCompleteSourcesCount;
1395 } else {
1396 m_nCompleteSourcesCountLo = count[i];
1398 m_nCompleteSourcesCount= m_nCompleteSourcesCountLo;
1399 m_nCompleteSourcesCountHi = count[j];
1400 if( m_nCompleteSourcesCountHi < m_nCompleteSourcesCount ) {
1401 m_nCompleteSourcesCountHi = m_nCompleteSourcesCount;
1403 } else {
1404 // Many sources..
1405 // For low guess
1406 // Use what we see.
1407 // For normal guess
1408 // Adjust network accounts for 100%, we account for
1409 // 0% with what we see and make sure we are still above the low.
1410 // For high guess
1411 // Adjust network accounts for 100%, we account for 0%
1412 // with what we see and make sure we are still above the normal.
1414 m_nCompleteSourcesCountLo = m_nCompleteSourcesCount;
1415 m_nCompleteSourcesCount = count[j];
1416 if( m_nCompleteSourcesCount < m_nCompleteSourcesCountLo ) {
1417 m_nCompleteSourcesCount = m_nCompleteSourcesCountLo;
1419 m_nCompleteSourcesCountHi= count[k];
1420 if( m_nCompleteSourcesCountHi < m_nCompleteSourcesCount ) {
1421 m_nCompleteSourcesCountHi = m_nCompleteSourcesCount;
1425 m_nCompleteSourcesTime = time(NULL) + (60);
1428 Notify_SharedFilesUpdateItem(this);
1432 void CKnownFile::UpdateUpPartsFrequency( CUpDownClient* client, bool increment )
1434 if ( m_AvailPartFrequency.size() != GetPartCount() ) {
1435 m_AvailPartFrequency.clear();
1436 m_AvailPartFrequency.insert(m_AvailPartFrequency.begin(), GetPartCount(), 0);
1437 if ( !increment ) {
1438 return;
1442 const BitVector& freq = client->GetUpPartStatus();
1443 unsigned int size = freq.size();
1444 if ( size != m_AvailPartFrequency.size() ) {
1445 return;
1448 if ( increment ) {
1449 for ( unsigned int i = 0; i < size; ++i ) {
1450 if ( freq.get(i) ) {
1451 m_AvailPartFrequency[i]++;
1454 } else {
1455 for ( unsigned int i = 0; i < size; ++i ) {
1456 if ( freq.get(i) ) {
1457 m_AvailPartFrequency[i]--;
1463 void CKnownFile::ClearPriority() {
1464 if ( !m_bAutoUpPriority ) return;
1465 m_iUpPriority = ( m_bAutoUpPriority ) ? PR_HIGH : PR_NORMAL;
1466 UpdateAutoUpPriority();
1469 void GuessAndRemoveExt(CPath& name)
1471 wxString ext = name.GetExt();
1473 // Remove common two-part extensions, such as "tar.gz"
1474 if (ext == wxT("gz") || ext == wxT("bz2")) {
1475 name = name.RemoveExt();
1476 if (name.GetExt() == wxT("tar")) {
1477 name = name.RemoveExt();
1479 // might be an extension if length == 3
1480 // and also remove some common non-three-character extensions
1481 } else if (ext.Length() == 3 ||
1482 ext == wxT("7z") ||
1483 ext == wxT("rm") ||
1484 ext == wxT("jpeg") ||
1485 ext == wxT("mpeg")
1487 name = name.RemoveExt();
1491 void CKnownFile::SetFileName(const CPath& filename)
1493 CAbstractFile::SetFileName(filename);
1494 wordlist.clear();
1495 // Don't publish extension. That'd kill the node indexing e.g. "avi".
1496 CPath tmpName = GetFileName();
1497 GuessAndRemoveExt(tmpName);
1498 Kademlia::CSearchManager::GetWords(tmpName.GetPrintable(), &wordlist);
1501 #endif // CLIENT_GUI
1503 //For File Comment //
1504 void CKnownFile::LoadComment() const
1506 #ifndef CLIENT_GUI
1507 wxString strCfgPath = wxT("/") + m_abyFileHash.Encode() + wxT("/");
1509 wxConfigBase* cfg = wxConfigBase::Get();
1511 m_strComment = cfg->Read( strCfgPath + wxT("Comment"), wxEmptyString);
1512 m_iRating = cfg->Read( strCfgPath + wxT("Rate"), 0l);
1513 #endif
1515 m_bCommentLoaded = true;
1519 wxString CKnownFile::GetAICHMasterHash() const
1521 #ifdef CLIENT_GUI
1522 return m_AICHMasterHash;
1523 #else
1524 if (HasProperAICHHashSet()) {
1525 return m_pAICHHashSet->GetMasterHash().GetString();
1528 return wxEmptyString;
1529 #endif
1533 bool CKnownFile::HasProperAICHHashSet() const
1535 #ifdef CLIENT_GUI
1536 return m_AICHMasterHash.Length() != 0;
1537 #else
1538 return m_pAICHHashSet->HasValidMasterHash() &&
1539 (m_pAICHHashSet->GetStatus() == AICH_HASHSETCOMPLETE ||
1540 m_pAICHHashSet->GetStatus() == AICH_VERIFIED);
1541 #endif
1544 wxString CKnownFile::GetFeedback() const
1546 return wxString(_("File name")) + wxT(": ") + GetFileName().GetPrintable() + wxT("\n")
1547 + _("File size") + wxT(": ") + CastItoXBytes(GetFileSize()) + wxT("\n")
1548 + _("Share ratio") + CFormat(wxT(": %.2f%%\n")) % (((double)statistic.GetAllTimeTransferred() / (double)GetFileSize()) * 100.0)
1549 + _("Uploaded") + wxT(": ") + CastItoXBytes(statistic.GetTransferred()) + wxT(" (") + CastItoXBytes(statistic.GetAllTimeTransferred()) + wxT(")\n")
1550 + _("Requested") + CFormat(wxT(": %u (%u)\n")) % statistic.GetRequests() % statistic.GetAllTimeRequests()
1551 + _("Accepted") + CFormat(wxT(": %u (%u)\n")) % statistic.GetAccepts() % statistic.GetAllTimeAccepts()
1552 + _("On Queue") + CFormat(wxT(": %u\n")) % GetQueuedCount()
1553 + _("Complete sources") + CFormat(wxT(": %u\n")) % m_nCompleteSourcesCount;
1556 // File_checked_for_headers