unsigned int TorrentFile::GetSize() { unsigned int size = 0; unsigned int len = (unsigned int)v_announce_list.size(); // announce list vector <string>::iterator v1_Iter; for(v1_Iter = v_announce_list.begin(); v1_Iter != v_announce_list.end( ) ;v1_Iter++) { size+= (unsigned int)(*v1_Iter).size() + 1; } //size += size += 20*sizeof(m_info_hash); // m_info_hash; size += 256; // m_url[256]; size += 256; // char m_name[256]; size += (unsigned int)m_info_hash_string.size() + 1; // m_info_hash_string size += (unsigned int)v_pieces.size()*20*sizeof(unsigned short); PeerList * curr = m_peers; while(curr != NULL) { size += curr->GetSize()+sizeof(int); curr = curr->GetNext(); } size += sizeof(bool); return size+sizeof(TorrentFileHeader); }
char * TorrentFile::GetBuffer() { int size = GetSize(); int cur_pos = 0; char * buf = new char[size]; memset(buf,0,size); char * ptr = buf; TorrentFileHeader header; header.SetAnnounceLength((int)GetAnnounceList().size()); header.SetNumPieces((int)GetNumPieces()); header.m_creationdate = m_creationdate; header.m_length = m_length; header.m_piecelength = m_piecelength; header.m_numpieces = m_numpieces; header.info_len = info_len; header.info_start = info_start; header.m_size = 0;//m_size; memcpy(ptr, &header, sizeof(TorrentFileHeader)); ptr += sizeof(TorrentFileHeader); vector <string>::iterator v1_Iter; for(v1_Iter = v_announce_list.begin(); v1_Iter != v_announce_list.end( ) ;v1_Iter++) { strcpy(ptr, (*v1_Iter).c_str()); ptr += (*v1_Iter).size() + 1; } memcpy(ptr, m_info_hash, 20*sizeof(unsigned short)); ptr += 20*sizeof(unsigned short); // m_info_hash; memcpy(ptr, m_url, 256); ptr += 256; // m_url[256]; memcpy(ptr, m_name, 256); ptr += 256; // char m_name[256]; strcpy(ptr, m_info_hash_string.c_str()); ptr += (int)m_info_hash_string.size() + 1; // m_info_hash_string *((bool *)ptr) = b_is_valid; ptr+=sizeof(bool); vector <unsigned short*>::iterator v2_Iter; for(v2_Iter = v_pieces.begin(); v2_Iter != v_pieces.end( ) ;v2_Iter++) { memcpy(ptr, (*v2_Iter), 20); ptr += 20*sizeof(unsigned short); } PeerList * curr = m_peers; while(curr != NULL) { curr->GetBuffer(ptr); ptr += curr->GetSize(); curr = curr->GetNext(); } return buf; }