📄 knownfile.cpp
字号:
//tags
uint32 tagcount = taglist.GetCount()+8;
// standard tags
fwrite(&tagcount, 4, 1, file);
CTag* nametag = new CTag(FT_FILENAME, m_pszFileName);
nametag->WriteTagToFile(file);
delete nametag;
CTag* sizetag = new CTag(FT_FILESIZE, m_nFileSize);
sizetag->WriteTagToFile(file);
delete sizetag;
// statistic
uint32 tran;
tran=statistic.alltimetransferred;
CTag attag1(FT_ATTRANSFERED, tran);
attag1.WriteTagToFile(file);
tran=statistic.alltimetransferred>>32;
CTag attag4(FT_ATTRANSFEREDHI, tran);
attag4.WriteTagToFile(file);
CTag attag2(FT_ATREQUESTED, statistic.GetAllTimeRequests());
attag2.WriteTagToFile(file);
CTag attag3(FT_ATACCEPTED, statistic.GetAllTimeAccepts());
attag3.WriteTagToFile(file);
// priority N permission
if( !IsAutoUpPriority() ){
CTag priotag(FT_ULPRIORITY, m_iUpPriority);
priotag.WriteTagToFile(file);
}
else{
CTag priotag(FT_ULPRIORITY, PR_AUTO);
priotag.WriteTagToFile(file);
}
CTag permtag(FT_PERMISSIONS, m_iPermissions);
permtag.WriteTagToFile(file);
//other tags
for (uint32 j = 0; j != tagcount-8;j++)
taglist[j]->WriteTagToFile(file);
return ferror(file);
}
void CKnownFile::CreateHashFromInput(FILE* file,CFile* file2, int Length, uchar* Output, uchar* in_string) {
// time critial
bool PaddingStarted = false;
uint32 Hash[4];
Hash[0] = 0x67452301;
Hash[1] = 0xEFCDAB89;
Hash[2] = 0x98BADCFE;
Hash[3] = 0x10325476;
CFile* data = 0;
if (in_string)
data = new CMemFile(in_string,Length);
uint32 Required = Length;
uchar X[64*128];
while (Required >= 64){
uint32 len = Required / 64;
if (len > sizeof(X)/(64 * sizeof(X[0])))
len = sizeof(X)/(64 * sizeof(X[0]));
if (in_string)
data->Read(&X,len*64);
else if (file)
fread(&X,len*64,1,file);
else if (file2)
file2->Read(&X,len*64);
for (uint32 i = 0; i < len; i++)
{
MD4Transform(Hash, (uint32*)(X + i*64));
}
Required -= len*64;
}
// bytes to read
Required = Length % 64;
if (Required != 0){
if (in_string)
data->Read(&X,Required);
else if (file)
fread(&X,Required,1,file);
else if (file2)
file2->Read(&X,Required);
}
// in byte scale 512 = 64, 448 = 56
if (Required >= 56){
X[Required] = 0x80;
PaddingStarted = TRUE;
memset(&X[Required + 1], 0, 63 - Required);
MD4Transform(Hash, (uint32*)X);
Required = 0;
}
if (!PaddingStarted)
X[Required++] = 0x80;
memset(&X[Required], 0, 64 - Required);
// add size (convert to bits)
uint32 Length2 = Length >> 29;
Length <<= 3;
memcpy(&X[56], &Length, 4);
memcpy(&X[60], &Length2, 4);
MD4Transform(Hash, (uint32*)X);
memcpy(Output, Hash, 16);
safe_delete(data);
}
uchar* CKnownFile::GetPartHash(uint16 part){
if (part >= hashlist.GetCount())
return 0;
return hashlist[part];
}
/*
uint16 CKnownFile::GetPartCount(){
return m_iPartCount>0 ? m_iPartCount : (m_iPartCount = ((m_nFileSize+ (PARTSIZE - 1)) / PARTSIZE));
}
*/
static void MD4Transform(uint32 Hash[4], uint32 x[16])
{
uint32 a = Hash[0];
uint32 b = Hash[1];
uint32 c = Hash[2];
uint32 d = Hash[3];
/* Round 1 */
MD4_FF(a, b, c, d, x[ 0], S11); // 01
MD4_FF(d, a, b, c, x[ 1], S12); // 02
MD4_FF(c, d, a, b, x[ 2], S13); // 03
MD4_FF(b, c, d, a, x[ 3], S14); // 04
MD4_FF(a, b, c, d, x[ 4], S11); // 05
MD4_FF(d, a, b, c, x[ 5], S12); // 06
MD4_FF(c, d, a, b, x[ 6], S13); // 07
MD4_FF(b, c, d, a, x[ 7], S14); // 08
MD4_FF(a, b, c, d, x[ 8], S11); // 09
MD4_FF(d, a, b, c, x[ 9], S12); // 10
MD4_FF(c, d, a, b, x[10], S13); // 11
MD4_FF(b, c, d, a, x[11], S14); // 12
MD4_FF(a, b, c, d, x[12], S11); // 13
MD4_FF(d, a, b, c, x[13], S12); // 14
MD4_FF(c, d, a, b, x[14], S13); // 15
MD4_FF(b, c, d, a, x[15], S14); // 16
/* Round 2 */
MD4_GG(a, b, c, d, x[ 0], S21); // 17
MD4_GG(d, a, b, c, x[ 4], S22); // 18
MD4_GG(c, d, a, b, x[ 8], S23); // 19
MD4_GG(b, c, d, a, x[12], S24); // 20
MD4_GG(a, b, c, d, x[ 1], S21); // 21
MD4_GG(d, a, b, c, x[ 5], S22); // 22
MD4_GG(c, d, a, b, x[ 9], S23); // 23
MD4_GG(b, c, d, a, x[13], S24); // 24
MD4_GG(a, b, c, d, x[ 2], S21); // 25
MD4_GG(d, a, b, c, x[ 6], S22); // 26
MD4_GG(c, d, a, b, x[10], S23); // 27
MD4_GG(b, c, d, a, x[14], S24); // 28
MD4_GG(a, b, c, d, x[ 3], S21); // 29
MD4_GG(d, a, b, c, x[ 7], S22); // 30
MD4_GG(c, d, a, b, x[11], S23); // 31
MD4_GG(b, c, d, a, x[15], S24); // 32
/* Round 3 */
MD4_HH(a, b, c, d, x[ 0], S31); // 33
MD4_HH(d, a, b, c, x[ 8], S32); // 34
MD4_HH(c, d, a, b, x[ 4], S33); // 35
MD4_HH(b, c, d, a, x[12], S34); // 36
MD4_HH(a, b, c, d, x[ 2], S31); // 37
MD4_HH(d, a, b, c, x[10], S32); // 38
MD4_HH(c, d, a, b, x[ 6], S33); // 39
MD4_HH(b, c, d, a, x[14], S34); // 40
MD4_HH(a, b, c, d, x[ 1], S31); // 41
MD4_HH(d, a, b, c, x[ 9], S32); // 42
MD4_HH(c, d, a, b, x[ 5], S33); // 43
MD4_HH(b, c, d, a, x[13], S34); // 44
MD4_HH(a, b, c, d, x[ 3], S31); // 45
MD4_HH(d, a, b, c, x[11], S32); // 46
MD4_HH(c, d, a, b, x[ 7], S33); // 47
MD4_HH(b, c, d, a, x[15], S34); // 48
Hash[0] += a;
Hash[1] += b;
Hash[2] += c;
Hash[3] += d;
}
// Adde by Tarod [Juanjo]
void CAbstractFile::SetFileName(char* NewName)
{
if (m_pszFileName != NULL) {
delete[] m_pszFileName;
m_pszFileName = new char[strlen(NewName) + 1];
CString filenametemp;
filenametemp=NewName;//.Format("%s",NewName);
filenametemp.Replace('/','-');
filenametemp.Replace('>','-');
filenametemp.Replace('<','-');
filenametemp.Replace('*','-');
filenametemp.Replace(':','-');
filenametemp.Replace('?','-');
sprintf(m_pszFileName, "%s", filenametemp);
}
}
//End by Tarod
Packet* CKnownFile::CreateSrcInfoPacket(CUpDownClient* forClient){
CTypedPtrList<CPtrList, CUpDownClient*> srclist;
theApp.uploadqueue->FindSourcesForFileById(&srclist, forClient->GetUploadFileID()); //should we use "m_abyFileHash"?
if(srclist.IsEmpty())
return 0;
CMemFile data;
uint16 nCount = 0;
data.Write(forClient->GetUploadFileID(), 16);
data.Write(&nCount, 2);
uint32 lastRequest = forClient->GetLastSrcReqTime();
//we are only taking 30 random sources since we can't be sure if they have parts we need
//this is hard coded because its a temp solution until next(?) version
srand(time(NULL));
for(int i = 0; i < 30; i++) {
int victim = ((rand() >> 7) % srclist.GetSize());
POSITION pos = srclist.FindIndex(victim);
CUpDownClient *cur_src = srclist.GetAt(pos);
if(!cur_src->HasLowID() && cur_src != forClient) {
nCount++;
uint32 dwID = cur_src->GetUserID();
uint16 nPort = cur_src->GetUserPort();
uint32 dwServerIP = cur_src->GetServerIP();
uint16 nServerPort = cur_src->GetServerPort();
data.Write(&dwID, 4);
data.Write(&nPort, 2);
data.Write(&dwServerIP, 4);
data.Write(&nServerPort, 2);
if (forClient->GetSourceExchangeVersion() > 1)
data.Write(cur_src->GetUserHash(),16);
}
srclist.RemoveAt(pos);
if(srclist.GetSize() == 0)
break;
}
if (!nCount)
return 0;
data.Seek(16,0);
data.Write(&nCount,2);
Packet* result = new Packet(&data, OP_EMULEPROT);
result->opcode = OP_ANSWERSOURCES;
if (nCount > 28)
result->PackPacket();
theApp.emuledlg->AddDebugLogLine( false, "Send:Source User(%s) File(%s) Count(%i)", forClient->GetUserName(), GetFileName(), nCount );
return result;
}
//For File Comment //
void CKnownFile::LoadComment(){
char buffer[100];
char* fullpath = new char[strlen(theApp.glob_prefs->GetAppDir())+13];
sprintf(fullpath,"%sfileinfo.ini",theApp.glob_prefs->GetAppDir());
buffer[0] = 0;
for (uint16 i = 0;i != 16;i++)
sprintf(buffer,"%s%02X",buffer,m_abyFileHash[i]);
CIni ini( fullpath, buffer);
m_strComment = ini.GetString("Comment");
m_iRate = ini.GetInt("Rate", 0);//For rate
m_bCommentLoaded=true;
delete fullpath;
}
void CKnownFile::SetFileComment(CString strNewComment){
char buffer[100];
char* fullpath = new char[strlen(theApp.glob_prefs->GetAppDir())+13];
sprintf(fullpath,"%sfileinfo.ini",theApp.glob_prefs->GetAppDir());
buffer[0] = 0;
for (uint16 i = 0;i != 16;i++)
sprintf(buffer,"%s%02X",buffer,m_abyFileHash[i]);
CIni ini( fullpath, buffer );
ini.WriteString ("Comment", strNewComment);
m_strComment = strNewComment;
delete fullpath;
CTypedPtrList<CPtrList, CUpDownClient*> srclist;
theApp.uploadqueue->FindSourcesForFileById(&srclist, this->GetFileHash());
for (POSITION pos = srclist.GetHeadPosition();pos != 0;srclist.GetNext(pos)){
CUpDownClient *cur_src = srclist.GetAt(pos);
cur_src->SetCommentDirty();
}
}
// For File rate
void CKnownFile::SetFileRate(int8 iNewRate){
char buffer[100];
char* fullpath = new char[strlen(theApp.glob_prefs->GetAppDir())+13];
sprintf(fullpath,"%sfileinfo.ini",theApp.glob_prefs->GetAppDir());
buffer[0] = 0;
for (uint16 i = 0;i != 16;i++)
sprintf(buffer,"%s%02X",buffer,m_abyFileHash[i]);
CIni ini( fullpath, buffer );
ini.WriteInt ("Rate", iNewRate);
m_iRate = iNewRate;
delete fullpath;
CTypedPtrList<CPtrList, CUpDownClient*> srclist;
theApp.uploadqueue->FindSourcesForFileById(&srclist, this->GetFileHash());
for (POSITION pos = srclist.GetHeadPosition();pos != 0;srclist.GetNext(pos)){
CUpDownClient *cur_src = srclist.GetAt(pos);
cur_src->SetCommentDirty();
}
}
void CKnownFile::UpdateAutoUpPriority(){
if( !IsAutoUpPriority() )
return;
if ( GetQueuedCount() > 20 ){
if( GetUpPriority() != PR_LOW ){
SetUpPriority( PR_LOW );
theApp.emuledlg->sharedfileswnd.sharedfilesctrl.UpdateFile(this);
}
return;
}
if ( GetQueuedCount() > 1 ){
if( GetUpPriority() != PR_NORMAL ){
SetUpPriority( PR_NORMAL );
theApp.emuledlg->sharedfileswnd.sharedfilesctrl.UpdateFile(this);
}
return;
}
if( GetUpPriority() != PR_HIGH){
SetUpPriority( PR_HIGH );
theApp.emuledlg->sharedfileswnd.sharedfilesctrl.UpdateFile(this);
}
}
void CKnownFile::SetUpPriority(uint8 iNewUpPriority, bool m_bsave){
m_iUpPriority = iNewUpPriority;
if( this->IsPartFile() && m_bsave )
((CPartFile*)this)->SavePartFile();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -