void Cdlg_login::add_game(const string& reg_key, ::t_game game, int gsku) { Creg_key key; string serial; if (ERROR_SUCCESS == key.open(HKEY_LOCAL_MACHINE, reg_key, KEY_READ)) key.query_value("Serial", serial); if (serial.size() == 22) { Cvirtual_binary s; s.load(xcc_dirs::get_dir(game) + "woldata.key"); for (int i = 0, j = 0; i < s.size(); i++, j++) { if (j == serial.length()) j = 0; serial[j] = (262 - s.data()[i] + serial[j]) % 10 + '0'; } } else serial.clear(); t_game e; e.gsku = gsku; e.serial = serial; m_game.SetItemData(m_game.AddString(game_name[game]), m_games.size()); m_games.push_back(e); }
int Caud_file::extract_as_wav(const string& name) { Cvirtual_binary d = decode(); if (!d.data()) return 1; Cfile32 f; int error = f.open(name, GENERIC_WRITE); if (error) return error; int cb_sample = get_cb_sample(); int cs_remaining = get_c_samples(); t_wav_header header; memset(&header, 0, sizeof(t_wav_header)); header.file_header.id = wav_file_id; header.file_header.size = sizeof(header) - sizeof(header.file_header) + (cs_remaining << 1); header.form_type = wav_form_id; header.format_chunk.header.id = wav_format_id; header.format_chunk.header.size = sizeof(header.format_chunk) - sizeof(header.format_chunk.header); header.format_chunk.formattag = 1; header.format_chunk.c_channels = 1; header.format_chunk.samplerate = get_samplerate(); header.format_chunk.byterate = cb_sample * get_samplerate(); header.format_chunk.blockalign = cb_sample; header.format_chunk.cbits_sample = cb_sample << 3; header.data_chunk_header.id = wav_data_id; header.data_chunk_header.size = cb_sample * cs_remaining; error = f.write(&header, sizeof(t_wav_header)); return error ? error : f.write(d); }
Cvirtual_binary Caud_file::decode() { Cvirtual_binary d; int cb_audio = get_cb_sample() * get_c_samples(); switch (header().compression) { case 1: { byte* w = d.write_start(cb_audio); for (int chunk_i = 0; w != d.data_end(); chunk_i++) { const t_aud_chunk_header& header = *get_chunk_header(chunk_i); aud_decode_ws_chunk(get_chunk_data(chunk_i), reinterpret_cast<char*>(w), header.size_in, header.size_out); w += header.size_out; } } break; case 0x63: { aud_decode decode; decode.init(); byte* w = d.write_start(cb_audio); for (int chunk_i = 0; w != d.data_end(); chunk_i++) { const t_aud_chunk_header* header = get_chunk_header(chunk_i); if (!header) break; decode.decode_chunk(get_chunk_data(chunk_i), reinterpret_cast<short*>(w), header->size_out / get_cb_sample()); w += header->size_out; } } break; } return d; }
Cvirtual_binary Cbvalue::read() const { Cvirtual_binary d; int cb_d = read(d.write_start(pre_read())); assert(cb_d == d.size()); return d; }
int Cogg_file::decode(Cvirtual_audio& audio) { int error = 0; OggVorbis_File vf; seek(0); if (ov_open_callbacks(this, &vf, NULL, 0, cbs) < 0) error = 0x100; else { vorbis_info* vi = ov_info(&vf, -1); Cvirtual_binary b; char* w = reinterpret_cast<char*>(b.write_start(vi->channels * ov_pcm_total(&vf, -1) << 1)); while (!error) { int current_section; int cb_read = ov_read(&vf, w, b.size(), 0, 2, 1, ¤t_section); if (!cb_read) break; else if (cb_read < 0) error = 1; else w += cb_read; } if (!error) audio.load(b, b.size() / (vi->channels << 1), vi->rate, 2, vi->channels); ov_clear(&vf); } return error; }
static void scan_dir(string in_dir, string out_dir, string postfix, ofstream& f) { WIN32_FIND_DATA fd; HANDLE findhandle = FindFirstFile((in_dir + '*' + postfix).c_str(), &fd); if (findhandle != INVALID_HANDLE_VALUE) { do { if (~fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { string title = boost::to_lower_copy(Cfname(fd.cFileName).get_ftitle()); Cvirtual_binary fdata; fdata.load(in_dir + fd.cFileName); Cxif_key k; Cxif_key& l = k.open_key_write(0); l.set_value_string(vi_fname, boost::to_lower_copy(string(fd.cFileName))); l.set_value_binary(vi_fdata, fdata); k.vdata().save(out_dir + title + ".xmuf"); f << title << "=,http://xccu.sourceforge.net/ra2_maps/" << title << ".xmuf" << endl; } } while (FindNextFile(findhandle, &fd)); FindClose(findhandle); } }
Cvirtual_binary Cbig_file_write::write() { int cb_header = sizeof(t_big_header); int cb_d = sizeof(t_big_header); for (auto& i : m_index) { cb_header += sizeof(t_big_index_entry) + i.first.length() + 1; cb_d += sizeof(t_big_index_entry) + i.first.length() + 1 + i.second.size(); } Cvirtual_binary d; byte* w = d.write_start(cb_d); t_big_header& header = *reinterpret_cast<t_big_header*>(w); header.id = big_id; header.size = d.size(); header.mc_files = reverse(m_index.size()); header.mcb_header = reverse(cb_header); byte* w2 = w + cb_header; w += sizeof(t_big_header); for (auto& i : m_index) { t_big_index_entry& e = *reinterpret_cast<t_big_index_entry*>(w); e.offset = reverse(w2 - d.data()); e.size = reverse(i.second.size()); w += sizeof(t_big_index_entry); memcpy(w, i.first.c_str(), i.first.length() + 1); w += i.first.length() + 1; w2 += i.second.read(w2); } assert(w2 == d.data_end()); return d; }
int CXCCRA2MapUpdaterApp::download_update(string link, string fname) { int error = 0; CInternetSession is; CHttpFile* f = reinterpret_cast<CHttpFile*>(is.OpenURL(link.c_str(), INTERNET_FLAG_TRANSFER_BINARY)); if (!f) error = 1; else { Cvirtual_file h; DWORD status; if (!f->QueryInfoStatusCode(status)) error = 2; else if (status != 200) error = 3; else { int total_size = f->Seek(0, CFile::end); f->Seek(0, CFile::begin); Cdownload_dlg dlg; dlg.set(link, fname, total_size); dlg.Create(Cdownload_dlg::IDD, NULL); dlg.EnableWindow(false); Cvirtual_binary t; while (!error) { int cb_p = min<int>(f->GetLength(), 1 << 10); if (!cb_p) break; f->Read(t.write_start(cb_p), cb_p); h.write(t); dlg.set_size(h.size()); dlg.UpdateWindow(); } h.compact(); Cxif_key k; if (k.load_key(h.data(), h.size())) error = 5; else { for (t_xif_key_map::const_iterator ki = k.m_keys.begin(); ki != k.m_keys.end(); ki++) { if (error) break; const Cxif_key& l = ki->second; string fext = boost::to_lower_copy(Cfname(l.get_value_string(vi_fname)).get_fext()); if (fext != ".mmx" && (fext != ".yro") || !Cfname(xcc_dirs::get_exe(game_ra2_yr)).exists()) continue; if (file32_write(Cfname(fname).get_path() + l.get_value_string(vi_fname), l.get_value(vi_fdata).get_data(), l.get_value(vi_fdata).get_size())) error = 6; } } dlg.DestroyWindow(); } f->Close(); } return error; }
Cvirtual_binary Cvirtual_file::read() const { if (m_data.size() == 1) return *m_data.begin(); Cvirtual_binary r; read(r.write_start(size())); return r; }
int Cvirtual_file::load(const string& fname) { clear(); Cvirtual_binary t; int error = t.load(fname); if (!error) write(t); return error; }
int Cmix_edit::insert(const string& name, Cvirtual_binary d) { mix_database::add_name(m_game, name, "-"); int offset = new_block(d.size()); m_f.seek(offset); int error = m_f.write(d); if (!error) m_index[id(name)] = t_mix_index_entry(id(name), offset, d.size()); return error; }
int Cmerkle_tree::load(const Cvirtual_binary& v) { if (v.size() % 21) return 1; m_size = v.size() / 21; for (int i = 1; i < m_size; i <<= 1) m_size -= i; if (m_size) m_d = v; return 0; }
Cvirtual_binary Cbt_tracker_accounts::dump() const { int cb_d = 4; for (const_iterator i = begin(); i != end(); i++) cb_d += i->pre_dump(); Cvirtual_binary d; Cstream_writer w(d.write_start(cb_d)); w.write_int(4, size()); for (const_iterator i = begin(); i != end(); i++) i->dump(w); assert(w.w() == d.end()); return d; }
int mix_database::load() { if (!td_list.empty() || !ra_list.empty() || !ts_list.empty()) return 0; Cvirtual_binary f; if (f.load(xcc_dirs::get_data_dir() + "global mix database.dat") || f.size() < 16) return 1; const char* data = reinterpret_cast<const char*>(f.data()); read_list(game_td, data); read_list(game_ra, data); read_list(game_ts, data); read_list(game_ra2, data); if (0) { ofstream log_f("c:\\log.txt"); for (auto& i : ts_list) log_f << i.second.name << '\t' << i.second.description << endl; } return 0; char name[12] = "scg00ea.bin"; const char char1[] = "bgjm"; const char char2[] = "ew"; const char char3[] = "abc"; for (int i = 0; i < 2; i++) { if (i) strcpy(name + 8, "ini"); for (int j = 0; j < 4; j++) { name[2] = char1[j]; for (int k = 0; k < 100; k++) { memcpy(name + 3, nwzl(2, k).c_str(), 2); for (int l = 0; l < 2; l++) { name[5] = char2[l]; for (int m = 0; m < 3; m++) { name[6] = char3[m]; mix_database::add_name(game_td, name, ""); mix_database::add_name(game_ra, name, ""); mix_database::add_name(game_ts, name, ""); } } } } } return 0; }
int Cvxl_file::extract_as_pcx(const Cfname& name, t_file_type ft, const t_palet _palet) const { t_palet palet; memcpy(palet, _palet, sizeof(t_palet)); convert_palet_18_to_24(palet); const t_vxl_section_tailer& section_tailer = *get_section_tailer(0); const int cx = section_tailer.cx; const int cy = section_tailer.cy; const int cz = section_tailer.cz; Cvirtual_binary s; for (int i = 0; i < cz; i++) { memset(s.write_start(cx * cy), 0, cx * cy); int j = 0; for (int y = 0; y < cy; y++) { for (int x = 0; x < cx; x++) { const byte* r = get_span_data(0, j); if (r) { int z = 0; while (z < cz) { z += *r++; int c = *r++; while (c--) { if (i == z) s.data_edit()[x + cx * y] = *r; r += 2; z++; } r++; } } j++; } } Cfname t = name; t.set_title(name.get_ftitle() + " " + nwzl(4, i)); int error = image_file_write(t, ft, s.data(), palet, cx, cy); if (error) return error; } return 0; }
static int copy_block(Cfile32& s, int s_p, Cfile32& d, int d_p, int size) { Cvirtual_binary buffer; while (size) { int cb_buffer = min(size, 4 << 20); s.seek(s_p); if (int e = s.read(buffer.write_start(cb_buffer), cb_buffer)) return e; d.seek(d_p); if (int e = d.write(buffer.data(), cb_buffer)) return e; s_p += cb_buffer; d_p += cb_buffer; size -= cb_buffer; } return 0; }
void Cdlg_make_torrent::OnLoadTrackers() { UpdateData(true); CFileDialog dlg(true, "torrent", NULL, OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, "Torrents|*.torrent|", this); if (IDOK != dlg.DoModal()) return; Cvirtual_binary d; d.load(static_cast<std::string>(dlg.GetPathName())); Cbt_torrent torrent(d.range()); if (!torrent.valid()) return; m_tracker = torrent.announce().c_str(); m_trackers.Empty(); const Cbt_torrent::t_announces& announces = torrent.announces(); for (Cbt_torrent::t_announces::const_iterator i = announces.begin(); i != announces.end(); i++) { m_trackers += i->c_str(); m_trackers += "\r\n"; } UpdateData(false); }
Cvirtual_binary shp_file_write(const byte* s, int cx, int cy, int c_images) { Cvirtual_binary d; const byte* r = s; byte* w = d.write_start(sizeof(t_shp_ts_header) + (sizeof(t_shp_ts_image_header) + cx * cy) * c_images); t_shp_header& header = *reinterpret_cast<t_shp_header*>(w); header.c_images = c_images; header.unknown1 = 0; header.unknown2 = 0; header.cx = cx; header.cy = cy; header.unknown3 = 0; w += sizeof(t_shp_header); int* index = reinterpret_cast<int*>(w); w += 8 * (c_images + 2); for (int i = 0; i < c_images; i++) { *index++ = 0x80000000 | w - d.data(); *index++ = 0; w += encode80(r, w, cx * cy); r += cx * cy; } *index++ = w - d.data(); *index++ = 0; *index++ = 0; *index++ = 0; d.size(w - d.data()); return d; }
void Cbt_tracker_accounts::load(const Cvirtual_binary& s) { clear(); if (s.size() < 4) return; Cstream_reader r(s); for (int count = r.read_int(4); count--; ) { std::string tracker = r.read_string(); std::string name = r.read_string(); std::string pass = r.read_string(); push_back(Cbt_tracker_account(tracker, name, pass)); } }
Cvirtual_binary Cxif_key::export_bz() const { Cvirtual_binary d; int size = get_size(); int external_size = get_external_size(); Cvirtual_binary s; byte* w = s.write_start(size); save(w); unsigned int cb_d = s.size() + (s.size() + 99) / 100 + 600; t_xif_header_fast& header = *reinterpret_cast<t_xif_header_fast*>(d.write_start(sizeof(t_xif_header_fast) + cb_d + external_size)); if (BZ_OK != BZ2_bzBuffToBuffCompress(reinterpret_cast<char*>(d.data_edit() + sizeof(t_xif_header_fast)), &cb_d, const_cast<char*>(reinterpret_cast<const char*>(s.data())), s.size(), 9, 0, 0)) return Cvirtual_binary(); w = d.data_edit() + sizeof(t_xif_header_fast) + cb_d; external_save(w); header.id = file_id; header.version = file_version_fast; header.size_uncompressed = size; header.size_compressed = cb_d; header.size_external = external_size; d.size(sizeof(t_xif_header_fast) + cb_d + external_size); return d; }
int Cxif_key::load_key(const byte* data, int size) { const byte* read_p = data; const t_xif_header_fast& header = *reinterpret_cast<const t_xif_header_fast*>(read_p); if (size < sizeof(t_xif_header_old) || header.id != file_id || header.version != file_version_old && header.version != file_version_new && header.version != file_version_fast) return 1; int error = 0; if (header.version == file_version_old) { read_p += sizeof(t_xif_header_old) - 4; load_old(read_p); error = size != read_p - data; } else { unsigned long cb_d = header.size_uncompressed; if (cb_d) { Cvirtual_binary d; if (header.version == file_version_new) error = Z_OK != uncompress(d.write_start(cb_d), &cb_d, data + sizeof(t_xif_header_old), size - sizeof(t_xif_header_old)); else { if (memcmp(data + sizeof(t_xif_header_fast), "BZh", 3)) error = Z_OK != uncompress(d.write_start(cb_d), &cb_d, data + sizeof(t_xif_header_fast), header.size_compressed); else error = BZ_OK != BZ2_bzBuffToBuffDecompress(reinterpret_cast<char*>(d.write_start(cb_d)), reinterpret_cast<unsigned int*>(&cb_d), const_cast<char*>(reinterpret_cast<const char*>(data + sizeof(t_xif_header_fast))), header.size_compressed, 0, 0); } // d.export("c:/temp/xif data.bin"); if (!error) { read_p = d.data(); load_new(read_p); error = read_p != d.data_end(); if (header.version == file_version_fast && !error) { read_p = data + sizeof(t_xif_header_fast) + header.size_compressed; load_external(read_p); error = size != read_p - data; } } } else { read_p = data + (header.version == file_version_fast ? sizeof(t_xif_header_fast) : sizeof(t_xif_header_old)); load_new(read_p); load_external(read_p); error = size != read_p - data; } } return error; }
int Cmix_edit::write_index() { { Cxcc_lmd_file_write lmd_fw; for (auto& i : m_index) { string name = mix_database::get_name(m_game, i.second.id); if (!name.empty()) lmd_fw.add_fname(name); } insert("local mix database.dat", lmd_fw.write(m_game)); } Cvirtual_binary d; byte* w = d.write_start(cb_header(m_index.size())); *reinterpret_cast<__int32*>(w) = 0; w += 4; t_mix_header& header = *reinterpret_cast<t_mix_header*>(w); header.c_files = m_index.size(); w += sizeof(t_mix_header); { t_block_map block_map = Cmix_edit::block_map(); for (auto& i : block_map) { if (i.second->offset < d.size()) { int offset = new_block(i.second->size); int error = copy_block(m_f, i.second->offset, m_f, offset, i.second->size); if (error) return error; i.second->offset = offset; } } } int total_size = d.size(); t_mix_index_entry* index = reinterpret_cast<t_mix_index_entry*>(w); for (auto& i : m_index) { index->id = i.first; index->offset = i.second.offset - d.size(); index->size = i.second.size; index++; total_size = max(total_size, i.second.offset + i.second.size); } m_f.seek(total_size); m_f.set_eof(); header.size = m_f.size() - d.size(); m_f.seek(0); return m_f.write(d); }
int Cbvalue::write(const Cvirtual_binary& v) { return write(v, v.size()); }
Cvirtual_binary Cxif_key::vdata(bool fast) const { Cvirtual_binary d; int size = get_size(); int external_size = get_external_size(); if (fast) { t_xif_header_fast& header = *reinterpret_cast<t_xif_header_fast*>(d.write_start(sizeof(t_xif_header_fast) + size + external_size)); header.id = file_id; header.version = file_version_fast; header.size_uncompressed = 0; header.size_compressed = size; header.size_external = external_size; byte* w = d.data_edit() + sizeof(t_xif_header_fast); save(w); external_save(w); assert(d.data_end() == w); return d; } Cvirtual_binary s; byte* w = s.write_start(size); save(w); unsigned long cb_d = s.size() + (s.size() + 999) / 1000 + 12; t_xif_header_fast& header = *reinterpret_cast<t_xif_header_fast*>(d.write_start(sizeof(t_xif_header_fast) + cb_d + external_size)); compress(d.data_edit() + sizeof(t_xif_header_fast), &cb_d, s.data(), s.size()); w = d.data_edit() + sizeof(t_xif_header_fast) + cb_d; external_save(w); header.id = file_id; header.version = file_version_fast; header.size_uncompressed = size; header.size_compressed = cb_d; header.size_external = external_size; d.size(sizeof(t_xif_header_fast) + cb_d + external_size); return d; }
static Cvirtual_binary gzip(const Cvirtual_binary& s) { // gzip input if it results in a smaller size Cvirtual_binary d = xcc_z::gzip(s); return d.size() < s.size() ? d : s; }
int main(int argc, char* argv[]) { time_t t = time(NULL); if (argc < 2) { std::cerr << "Usage: " << argv[0] << " <file> <tracker> [--v1]" << std::endl; return 1; } std::string tracker = argc >= 3 ? argv[2] : "udp://localhost:2710"; bool use_merkle = argc >= 4 ? strcmp(argv[3], "--v1") : true; // set to false for a non-merkle torrent insert(argv[1]); // use 1 mbyte pieces by default int cb_piece = 1 << 20; if (!use_merkle) { // find optimal piece size for a non-merkle torrent long long cb_total = 0; for (t_map::const_iterator i = g_map.begin(); i != g_map.end(); i++) cb_total += i->second.size; cb_piece = 256 << 10; while (cb_total / cb_piece > 4 << 10) cb_piece <<= 1; } Cbvalue files; std::string pieces; Cvirtual_binary d; byte* w = d.write_start(cb_piece); for (t_map::const_iterator i = g_map.begin(); i != g_map.end(); i++) { int f = open(i->second.name.c_str(), O_BINARY | O_RDONLY); if (!f) continue; long long cb_f = 0; std::string merkle_hash; int cb_d; if (use_merkle) { // calculate merkle root hash as explained in XBT Make Merkle Tree.cpp typedef std::map<int, std::string> t_map; t_map map; char d[1025]; while (cb_d = read(f, d + 1, 1024)) { if (cb_d < 0) break; *d = 0; std::string h = Csha1(const_memory_range(d, cb_d + 1)).read(); *d = 1; int i; for (i = 0; map.find(i) != map.end(); i++) { memcpy(d + 1, map.find(i)->second.c_str(), 20); memcpy(d + 21, h.c_str(), 20); h = Csha1(const_memory_range(d, 41)).read(); map.erase(i); } map[i] = h; cb_f += cb_d; } *d = 1; while (map.size() > 1) { memcpy(d + 21, map.begin()->second.c_str(), 20); map.erase(map.begin()); memcpy(d + 1, map.begin()->second.c_str(), 20); map.erase(map.begin()); map[0] = Csha1(const_memory_range(d, 41)).read(); } if (!map.empty()) merkle_hash = map.begin()->second; } else { // calculate piece hashes while (cb_d = read(f, w, d.data_end() - w)) { if (cb_d < 0) break; w += cb_d; if (w == d.data_end()) { pieces += Csha1(const_memory_range(d, w - d)).read(); w = d.data_edit(); } cb_f += cb_d; } } close(f); // add file to files key files.l(merkle_hash.empty() ? Cbvalue().d(bts_length, cb_f).d(bts_path, Cbvalue().l(base_name(i->second.name))) : Cbvalue().d(bts_merkle_hash, merkle_hash).d(bts_length, cb_f).d(bts_path, Cbvalue().l(base_name(i->second.name)))); } if (w != d) pieces += Csha1(const_memory_range(d, w - d)).read(); Cbvalue info; info.d(bts_piece_length, cb_piece); if (!pieces.empty()) info.d(bts_pieces, pieces); if (g_map.size() == 1) { // single-file torrent if (use_merkle) info.d(bts_merkle_hash, files.l().front().d(bts_merkle_hash)); info.d(bts_length, files.l().front().d(bts_length)); info.d(bts_name, files.l().front().d(bts_path).l().front()); } else { // multi-file torrent info.d(bts_files, files); info.d(bts_name, g_name); } Cbvalue torrent; torrent.d(bts_announce, tracker); torrent.d(bts_info, info); Cvirtual_binary s = torrent.read(); if (use_merkle) s = gzip(s); s.save(g_name + ".torrent"); std::cout << time(NULL) - t << " s" << std::endl; return 0; }
void Cconnection::read(const std::string& v) { #ifndef NDEBUG std::cout << v << std::endl; #endif if (m_server->config().m_log_access) { static std::ofstream f("xbt_tracker_raw.log"); f << m_server->time() << '\t' << inet_ntoa(m_a.sin_addr) << '\t' << ntohs(m_a.sin_port) << '\t' << v << std::endl; } Ctracker_input ti; size_t e = v.find('?'); if (e == std::string::npos) e = v.size(); else { size_t a = e + 1; size_t b = v.find(' ', a); if (b == std::string::npos) return; while (a < b) { size_t c = v.find('=', a); if (c++ == std::string::npos) break; size_t d = v.find_first_of(" &", c); if (d == std::string::npos) break; ti.set(v.substr(a, c - a - 1), uri_decode(v.substr(c, d - c))); a = d + 1; } } if (!ti.m_ipa || !is_private_ipa(m_a.sin_addr.s_addr)) ti.m_ipa = m_a.sin_addr.s_addr; std::string torrent_pass0; size_t a = 4; if (a < e && v[a] == '/') { a++; if (a + 1 < e && v[a + 1] == '/') a += 2; if (a + 32 < e && v[a + 32] == '/') { torrent_pass0 = v.substr(a, 32); a += 33; if (a + 40 < e && v[a + 40] == '/') a += 41; } } std::string h = "HTTP/1.0 200 OK\r\n"; Cvirtual_binary s; bool gzip = true; switch (a < v.size() ? v[a] : 0) { case 'a': if (!ti.valid()) break; gzip = false; if (0) s = Cbvalue().d(bts_failure_reason, bts_banned_client).read(); else { std::string error = m_server->insert_peer(ti, false, m_server->find_user_by_torrent_pass(torrent_pass0, ti.m_info_hash)); s = error.empty() ? m_server->select_peers(ti) : Cbvalue().d(bts_failure_reason, error).read(); } break; case 'd': if (m_server->config().m_debug) { gzip = m_server->config().m_gzip_debug; h += "Content-Type: text/html; charset=us-ascii\r\n"; s = Cvirtual_binary(m_server->debug(ti)); } break; case 's': if (v.size() >= 7 && v[6] == 't') { gzip = m_server->config().m_gzip_debug; h += "Content-Type: text/html; charset=us-ascii\r\n"; s = Cvirtual_binary(m_server->statistics()); } else if (m_server->config().m_full_scrape || ti.m_compact || !ti.m_info_hash.empty()) { gzip = m_server->config().m_gzip_scrape && !ti.m_compact && ti.m_info_hash.empty(); s = m_server->scrape(ti); } break; } if (s.empty()) { if (!ti.m_peer_id.empty() || m_server->config().m_redirect_url.empty()) h = "HTTP/1.0 404 Not Found\r\n"; else { h = "HTTP/1.0 302 Found\r\n" "Location: " + m_server->config().m_redirect_url + (ti.m_info_hash.empty() ? "" : "?info_hash=" + uri_encode(ti.m_info_hash)) + "\r\n"; } } else if (gzip) { Cvirtual_binary s2 = xcc_z::gzip(s); #ifndef NDEBUG static std::ofstream f("xbt_tracker_gzip.log"); f << m_server->time() << '\t' << v[5] << '\t' << s.size() << '\t' << s2.size() << std::endl; #endif if (s2.size() + 24 < s.size()) { h += "Content-Encoding: gzip\r\n"; s = s2; } } h += "\r\n"; #ifdef WIN32 m_write_b.resize(h.size() + s.size()); memcpy(m_write_b.data_edit(), h.data(), h.size()); s.read(m_write_b.data_edit() + h.size()); int r = m_s.send(m_write_b); #else boost::array<iovec, 2> d; d[0].iov_base = const_cast<char*>(h.data()); d[0].iov_len = h.size(); d[1].iov_base = const_cast<unsigned char*>(s.data()); d[1].iov_len = s.size(); msghdr m; m.msg_name = NULL; m.msg_namelen = 0; m.msg_iov = const_cast<iovec*>(d.data()); m.msg_iovlen = d.size(); m.msg_control = NULL; m.msg_controllen = 0; m.msg_flags = 0; int r = sendmsg(m_s, &m, MSG_NOSIGNAL); #endif if (r == SOCKET_ERROR) { if (WSAGetLastError() != WSAECONNRESET) std::cerr << "send failed: " << Csocket::error2a(WSAGetLastError()) << std::endl; } else if (r != h.size() + s.size()) { #ifndef WIN32 if (r < h.size()) { m_write_b.resize(h.size() + s.size()); memcpy(m_write_b.data_edit(), h.data(), h.size()); s.read(m_write_b.data_edit() + h.size()); } else { m_write_b = s; r -= h.size(); } #endif m_r = m_write_b; m_r += r; } if (m_r.empty()) m_write_b.clear(); }
int Cxse::open() { t_game game = Cfname(xcc_dirs::get_main_mix(game_ra2_yr)).exists() ? game_ra2_yr : game_ra2; int error = m_bag_f.open_edit(xcc_dirs::get_dir(game) + "audio.bag"); if (!error) { error = m_idx_f.open_edit(xcc_dirs::get_dir(game) + "audio.idx"); if (!error) { if (m_bag_f.size() && m_idx_f.size()) { int cb_s = m_idx_f.size(); Cvirtual_binary s; error = m_idx_f.read(s.write_start(cb_s), cb_s); if (!error) { Caudio_idx_file g; g.load(s, cb_s); read_idx_file(g); } } else { Cmix_file language; error = language.open(xcc_dirs::get_language_mix(game)); if (!error) { language.set_game(game_ra2); Cmix_file audio; error = audio.open(xcc_dirs::get_audio_mix(game), language); if (!error) { audio.set_game(game_ra2); Ccc_file f(true); error = f.open("audio.bag", audio); if (!error) { error = m_bag_f.write(f); if (!error) { Caudio_idx_file g; error = g.open("audio.idx", audio); if (!error) { read_idx_file(g); if (!error) error = write_idx_file(); } } } } } } if (error) m_idx_f.close(); } if (error) m_bag_f.close(); } return error; }
static Cvirtual_binary gzip(const Cvirtual_binary& s) { Cvirtual_binary d = xcc_z::gzip(s); return d.size() < s.size() ? d : s; }
void Cdlg_make_torrent::OnSave() { if (!UpdateData()) return; m_name.TrimRight(" "); m_tracker.TrimRight(" "); CFileDialog dlg(false, "torrent", m_name + ".torrent", OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST, "Torrents|*.torrent|", this); if (IDOK != dlg.DoModal()) return; CWaitCursor wc; AfxGetApp()->WriteProfileString(m_strRegStore, "tracker", m_tracker); AfxGetApp()->WriteProfileString(m_strRegStore, "trackers", m_trackers); AfxGetApp()->WriteProfileInt(m_strRegStore, "use_merkle", m_use_merkle); int cb_piece = 1 << 20; if (!m_use_merkle) { long long cb_total = 0; for (t_map::const_iterator i = m_map.begin(); i != m_map.end(); i++) cb_total += i->second.size; cb_piece = 256 << 10; while (cb_total / cb_piece > 4 << 10) cb_piece <<= 1; } typedef std::set<std::string> t_set; t_set set; for (t_map::const_iterator i = m_map.begin(); i != m_map.end(); i++) set.insert(i->second.name); Cbvalue files; std::string pieces; Cvirtual_binary d; byte* w = d.write_start(cb_piece); for (t_set::const_iterator i = set.begin(); i != set.end(); i++) { int f = open(i->c_str(), _O_BINARY | _O_RDONLY); if (!f) continue; long long cb_f = 0; std::string merkle_hash; int cb_d; if (m_use_merkle) { typedef std::map<int, std::string> t_map; t_map map; char d[1025]; while (cb_d = read(f, d + 1, 1024)) { if (cb_d < 0) break; *d = 0; std::string h = Csha1(const_memory_range(d, cb_d + 1)).read(); *d = 1; int i; for (i = 0; map.find(i) != map.end(); i++) { memcpy(d + 1, map.find(i)->second.c_str(), 20); memcpy(d + 21, h.c_str(), 20); h = Csha1(const_memory_range(d, 41)).read(); map.erase(i); } map[i] = h; cb_f += cb_d; } *d = 1; while (map.size() > 1) { memcpy(d + 21, map.begin()->second.c_str(), 20); map.erase(map.begin()); memcpy(d + 1, map.begin()->second.c_str(), 20); map.erase(map.begin()); map[0] = Csha1(const_memory_range(d, 41)).read(); } if (!map.empty()) merkle_hash = map.begin()->second; } else { while (cb_d = read(f, w, d.end() - w)) { if (cb_d < 0) break; w += cb_d; if (w == d.end()) { pieces += Csha1(const_memory_range(d, w - d)).read(); w = d.data_edit(); } cb_f += cb_d; } } close(f); files.l(merkle_hash.empty() ? Cbvalue().d(bts_length, cb_f).d(bts_path, Cbvalue().l(base_name(*i))) : Cbvalue().d(bts_merkle_hash, merkle_hash).d(bts_length, cb_f).d(bts_path, Cbvalue().l(base_name(*i)))); } if (w != d) pieces += Csha1(const_memory_range(d, w - d)).read(); Cbvalue info; info.d(bts_piece_length, cb_piece); if (!pieces.empty()) info.d(bts_pieces, pieces); if (m_map.size() == 1) { if (m_use_merkle) info.d(bts_merkle_hash, files.l().front()[bts_merkle_hash]); info.d(bts_length, files.l().front()[bts_length]); info.d(bts_name, files.l().front()[bts_path].l().front()); } else { info.d(bts_files, files); info.d(bts_name, static_cast<std::string>(m_name)); } Cbvalue torrent; torrent.d(bts_announce, static_cast<std::string>(m_tracker)); if (!m_trackers.IsEmpty()) torrent.d(bts_announce_list, parse_trackers(static_cast<std::string>(m_trackers))); torrent.d(bts_info, info); Cvirtual_binary s = torrent.read(); if (m_use_merkle) s = gzip(s); s.save(static_cast<std::string>(dlg.GetPathName())); m_torrent_fname = dlg.GetPathName(); EndDialog(IDOK); }