1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394:
| // parts of this file are based on work from pan One (http://home-3.tiscali.nl/~meost/pms/)
//this file is part of eMule //Copyright (C)2002 Merkur ( merkur-@users.sourceforge.net / http://www.emule-project.net ) // //This program is free software; you can redistribute it and/or //modify it under the terms of the GNU General Public License //as published by the Free Software Foundation; either //version 2 of the License, or (at your option) any later version. // //This program is distributed in the hope that it will be useful, //but WITHOUT ANY WARRANTY; without even the implied warranty of //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //GNU General Public License for more details. // //You should have received a copy of the GNU General Public License //along with this program; if not, write to the Free Software //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#include "StdAfx.h" #include "knownfile.h" #include "opcodes.h" #include <io.h> #include <sys/types.h> #include <sys/stat.h> #include "emule.h" void CFileStatistic::AddRequest(){ requested++; alltimerequested++; theApp.knownfiles->requested++; theApp.sharedfiles->UpdateItem(fileParent); } void CFileStatistic::AddAccepted(){ accepted++; alltimeaccepted++; theApp.knownfiles->accepted++; theApp.sharedfiles->UpdateItem(fileParent); } void CFileStatistic::AddTransferd(uint32 bytes){ transfered += bytes; alltimetransfered += bytes; theApp.knownfiles->transfered += bytes; theApp.sharedfiles->UpdateItem(fileParent); }
CKnownFile::CKnownFile(){ filetype = 2; directory = 0; filename = 0; filesize = 0; date = 0; m_iPriority = PR_NORMAL; m_iPermissions = PERM_ALL; statistic.fileParent=this; }
CKnownFile::~CKnownFile(){ for (int i = 0; i != hashlist.GetSize(); i++) if (hashlist[i]) delete[] hashlist[i]; for (int i = 0; i != taglist.GetSize(); i++) safe_delete(taglist[i]); // if (filename) // done by CAbstractFile destructor // delete[] filename; if (directory) delete[] directory; }
void CKnownFile::SetPath(char* path){ if (directory) delete[] directory; directory = nstrdup(path); }
bool CKnownFile::CreateFromFile(char* in_directory,char* in_filename){ // TODO Errorhandling //first create the filehashset // open file directory = nstrdup(in_directory); char* namebuffer = new char[strlen(in_directory)+strlen(in_filename)+2]; sprintf(namebuffer,"%s\\%s",in_directory,in_filename); FILE* file = fopen(namebuffer,"rbS"); delete namebuffer; if (!file) return false; // set filesize + name filename = nstrdup(in_filename); filesize =_filelength(file->_file); // create hashset uint32 togo = filesize; for (uint16 hashcount = 0;togo >= PARTSIZE;) { uchar* newhash = new uchar[16]; CreateHashFromFile(file,PARTSIZE,newhash); hashlist.Add(newhash); togo -= PARTSIZE; hashcount++; } uchar* lasthash = new uchar[16]; memset(lasthash,0,16); CreateHashFromFile(file,togo,lasthash); if (!hashcount) memcpy(filehash,lasthash,16); else { hashlist.Add(lasthash); uchar* buffer = new uchar[hashlist.GetCount()*16]; for (int i = 0;i != hashlist.GetCount();i++) memcpy(buffer+(i*16),hashlist[i],16); CreateHashFromString(buffer,hashlist.GetCount()*16,filehash); delete[] buffer; } // TODO: Add filetags
// set lastwrite date struct _stat fileinfo; _fstat(file->_file,&fileinfo); date = fileinfo.st_mtime; //finished fclose(file); return true; }
// needed for memfiles. its probably better to switch everything to CFile... bool CKnownFile::LoadHashsetFromFile(CFile* file, bool checkhash){ uchar checkid[16]; file->Read(&checkid,16); uint16 parts; file->Read(&parts,2); for (int i = 0; i != parts; i++){ uchar* cur_hash = new uchar[16]; file->Read(cur_hash,16); hashlist.Add(cur_hash); } if (!checkhash){ memcpy(filehash,checkid,16); return true; } // trust noone ;-)
if (!hashlist.IsEmpty()){ uchar* buffer = new uchar[hashlist.GetCount()*16]; for (int i = 0;i != hashlist.GetCount();i++) memcpy(buffer+(i*16),hashlist[i],16); CreateHashFromString(buffer,hashlist.GetCount()*16,checkid); delete[] buffer; } if (!memcmp(filehash,checkid,16)) return true; else{ for (int i = 0; i != hashlist.GetSize(); i++) delete[] hashlist[i]; hashlist.RemoveAll(); return false; } }
bool CKnownFile::LoadTagsFromFile(CFile* file){ uint32 tagcount; file->Read(&tagcount,4); for (uint32 j = 0; j != tagcount;j++){ CTag* newtag = new CTag(file); switch(newtag->tag->specialtag){ case FT_FILENAME:{ filename = nstrdup(newtag->tag->stringvalue); delete newtag; break; } case FT_FILESIZE:{ filesize = newtag->tag->intvalue; delete newtag; break; } case FT_ATTRANSFERED:{ statistic.alltimetransfered = newtag->tag->intvalue; delete newtag; break; } case FT_ATREQUESTED:{ statistic.alltimerequested = newtag->tag->intvalue; delete newtag; break; } case FT_ATACCEPTED:{ statistic.alltimeaccepted = newtag->tag->intvalue; delete newtag; break; } case FT_PRIORITY:{ m_iPriority = newtag->tag->intvalue; delete newtag; break; } case FT_PERMISSIONS:{ m_iPermissions = newtag->tag->intvalue; delete newtag; break; } default: taglist.Add(newtag); } } return true; }
bool CKnownFile::LoadDateFromFile(CFile* file){ file->Read(&date,4); return true; }
bool CKnownFile::LoadFromFile(CFile* file){ return (LoadDateFromFile(file) && LoadHashsetFromFile(file,false) && LoadTagsFromFile(file)); }
bool CKnownFile::WriteToFile(FILE* file){ // date fwrite(&date,4,1,file); // hashset fwrite(&filehash,16,1,file); uint16 parts = hashlist.GetCount(); fwrite(&parts,2,1,file); for (int i = 0; i != parts; i++) fwrite(hashlist[i],16,1,file); //tags uint32 tagcount = taglist.GetCount()+7; // standard tags fwrite(&tagcount,4,1,file); CTag* nametag = new CTag(FT_FILENAME,filename); nametag->WriteTagToFile(file); delete nametag; CTag* sizetag = new CTag(FT_FILESIZE,filesize); sizetag->WriteTagToFile(file); delete sizetag; // statistic CTag attag1(FT_ATTRANSFERED,statistic.GetAllTimeTransfered()); attag1.WriteTagToFile(file); CTag attag2(FT_ATREQUESTED,statistic.GetAllTimeRequests()); attag2.WriteTagToFile(file); CTag attag3(FT_ATACCEPTED,statistic.GetAllTimeAccepts()); attag3.WriteTagToFile(file); // priority N permission CTag priotag(FT_PRIORITY, m_iPriority); priotag.WriteTagToFile(file); CTag permtag(FT_PERMISSIONS, m_iPermissions); permtag.WriteTagToFile(file); //other tags for (uint32 j = 0; j != tagcount-7;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]; while (Required >= 64){ if (in_string) data->Read(&X,64); else if (file) fread(&X,64,1,file); else if (file2) file2->Read(&X,64); MD4Transform(Hash, (uint32*)X); Required -= 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 (filesize/PARTSIZE) + ((filesize % PARTSIZE)? 1:0); }
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 (filename != NULL) { delete[] filename; filename = new char[strlen(NewName) + 1]; sprintf(filename, "%s", NewName); } } //End by Tarod |