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; }
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; }
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 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; }
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; }
Cvirtual_binary Cbvalue::read() const { Cvirtual_binary d; int cb_d = read(d.write_start(pre_read())); assert(cb_d == d.size()); return d; }
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; }
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; }
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; }
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 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 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; }
Cvirtual_binary Ccsf_file::write() const { Cvirtual_binary d; write(d.write_start(get_write_size())); return d; }
bool Cbt_hasher::run(Cbt_file& f) { if (m_i >= f.m_pieces.size()) return false; if (!m_i) m_sub_file = f.m_sub_files.begin(); Cbt_piece& piece = f.m_pieces[m_i]; Cvirtual_binary d; if (m_validate) { if (f.m_merkle) { bool root_valid = m_sub_file->merkle_tree().root().string() == m_sub_file->merkle_hash(); d.write_start(piece.size()); piece.valid(!f.read_data(f.mcb_piece * m_i, d)); for (const byte* r = d; r < d.end(); r += 0x8000) { std::string h = Cmerkle_tree::compute_root(const_memory_range(r, std::min(r + 0x8000, d.end()))); if (root_valid) { if (piece.valid() && (!m_sub_file->merkle_tree().has(m_j) || h != m_sub_file->merkle_tree().get(m_j).string())) piece.valid(false); } else m_sub_file->merkle_tree().set(m_j, h); m_j++; } } else { d.write_start(piece.size()); piece.valid(!f.read_data(f.mcb_piece * m_i, d) && !memcmp(Csha1(d).read().data(), piece.m_hash, 20)); } } if (!piece.valid()) f.m_left += piece.size(); int cb0 = piece.size(); while (cb0) { int cb1 = min(cb0, m_sub_file->size() - m_offset); if (!piece.valid()) m_sub_file->left(m_sub_file->left() + cb1); cb0 -= cb1; m_offset += cb1; if (m_offset == m_sub_file->size()) { if (f.m_merkle && m_sub_file->merkle_tree().root().string() != m_sub_file->merkle_hash()) { Cbt_piece* piece = &f.m_pieces.front() + m_sub_file->offset() / f.mcb_piece; for (int i = 0; i < m_sub_file->c_pieces(f.mcb_piece); i++) { if (piece->valid()) f.m_left += piece->size(); piece->valid(false); piece++; } m_sub_file->left(m_sub_file->size()); m_sub_file->merkle_tree().invalidate(); m_sub_file->merkle_tree().root(m_sub_file->merkle_hash()); } if (!m_sub_file->left()) { m_sub_file->close(); m_sub_file->open(f.m_name, O_RDONLY); } m_j = 0; m_offset = 0; m_sub_file++; } } m_i++; return true; }
Cvirtual_binary vxl_file_write(const Cxif_key& s) { Cvirtual_binary d; byte* w = d.write_start(1 << 20); const Cxif_key& header_key = s.open_key_read(vi_header); const Cxif_key& body_key = s.open_key_read(vi_body); int c_sections = body_key.c_keys(); int* span_start_list_ofs = new int[c_sections]; int* span_end_list_ofs = new int[c_sections]; int* span_data_ofs = new int[c_sections]; t_vxl_header& header = *reinterpret_cast<t_vxl_header*>(w); strcpy(header.id, vxl_id); header.one = 1; header.c_section_headers = c_sections; header.c_section_tailers = c_sections; header.unknown = 0x1f10; const Cxif_key& palet_key = header_key.open_key_read(vi_palet); int i; for (i = 0; i < 256; i++) { const Cxif_key& palet_entry_key = palet_key.open_key_read(i); header.palet[i].r = palet_entry_key.get_value_int(vi_red); header.palet[i].g = palet_entry_key.get_value_int(vi_green); header.palet[i].b = palet_entry_key.get_value_int(vi_blue); } w += sizeof(t_vxl_header); for (i = 0; i < c_sections; i++) { const Cxif_key& section_key = body_key.open_key_read(i); const Cxif_key& section_header_key = section_key.open_key_read(vi_header); t_vxl_section_header& section_header = *reinterpret_cast<t_vxl_section_header*>(w); strcpy(section_header.id, section_header_key.get_value_string(vi_id).c_str()); section_header.section_i = i; section_header.one = 1; section_header.zero = 0; w += sizeof(t_vxl_section_header); } byte* body_start = w; for (i = 0; i < c_sections; i++) { const Cxif_key& section_key = body_key.open_key_read(i); const Cxif_key& section_body_key = section_key.open_key_read(vi_body); const Cxif_key& section_tailer_key = section_key.open_key_read(vi_tailer); const int cx = section_tailer_key.get_value_int(vi_cx); const int cy = section_tailer_key.get_value_int(vi_cy); const int cz = section_tailer_key.get_value_int(vi_cz); __int32* span_start_list = reinterpret_cast<__int32*>(w); span_start_list_ofs[i] = w - body_start; w += 4 * cx * cy; __int32* span_end_list = reinterpret_cast<__int32*>(w); span_end_list_ofs[i] = w - body_start; w += 4 * cx * cy; byte* span_data_start = w; span_data_ofs[i] = w - body_start; int span_i = 0; for (int y = 0; y < cy; y++) { const Cxif_key& yi = section_body_key.open_key_read(y); for (int x = 0; x < cx; x++) { byte* span_start = w; const Cxif_key& xi = yi.open_key_read(x); int z = 0; for (auto& i : xi.m_keys) { int z_inc = i.first - z; z = i.first + 1; *w++ = z_inc; *w++ = 1; *w++ = i.second.get_value_int(vi_color); *w++ = i.second.get_value_int(vi_surface_normal); *w++ = 1; } if (span_start == w) { span_start_list[span_i] = -1; span_end_list[span_i] = -1; } else { if (z != cz) { *w++ = cz - z; *w++ = 0; *w++ = 0; } span_start_list[span_i] = span_start - span_data_start; span_end_list[span_i] = w - span_data_start - 1; } span_i++; } } } header.size = w - body_start; for (i = 0; i < c_sections; i++) { const Cxif_key& section_key = body_key.open_key_read(i); const Cxif_key& section_tailer_key = section_key.open_key_read(vi_tailer); t_vxl_section_tailer& section_tailer = *reinterpret_cast<t_vxl_section_tailer*>(w); section_tailer.span_start_ofs = span_start_list_ofs[i]; section_tailer.span_end_ofs = span_end_list_ofs[i]; section_tailer.span_data_ofs = span_data_ofs[i]; section_tailer.scale = 0; for (int ty = 0; ty < 3; ty++) { for (int tx = 0; tx < 4; tx++) section_tailer.transform[ty][tx] = 0; } section_tailer.x_min_scale = section_tailer_key.get_value_int(vi_x_min) / 1000000.0; section_tailer.y_min_scale = section_tailer_key.get_value_int(vi_y_min) / 1000000.0; section_tailer.z_min_scale = section_tailer_key.get_value_int(vi_z_min) / 1000000.0; section_tailer.x_max_scale = section_tailer_key.get_value_int(vi_x_max) / 1000000.0; section_tailer.y_max_scale = section_tailer_key.get_value_int(vi_y_max) / 1000000.0; section_tailer.z_max_scale = section_tailer_key.get_value_int(vi_z_max) / 1000000.0; section_tailer.cx = section_tailer_key.get_value_int(vi_cx); section_tailer.cy = section_tailer_key.get_value_int(vi_cy); section_tailer.cz = section_tailer_key.get_value_int(vi_cz); section_tailer.unknown = 2; w += sizeof(t_vxl_section_tailer); } delete[] span_data_ofs; delete[] span_end_list_ofs; delete[] span_start_list_ofs; d.size(w - d.data()); return d; }
Cvirtual_binary vxl_decode4(const byte* s, int cb_d) { Cvirtual_binary d; const byte* r = s; const t_vxl4_header& s_header = *reinterpret_cast<const t_vxl4_header*>(r); const int c_sections = s_header.c_sections; r += sizeof(t_vxl4_header); byte* w = d.write_start(cb_d ? cb_d : 1 << 20); t_vxl_header& header = *reinterpret_cast<t_vxl_header*>(w); strcpy(header.id, vxl_id); header.one = 1; header.c_section_headers = c_sections; header.c_section_tailers = c_sections; header.unknown = 0x1f10; w += sizeof(t_vxl_header); t_vxl_section_header* d_section_header = reinterpret_cast<t_vxl_section_header*>(w); t_vxl_section_tailer* section_tailer = new t_vxl_section_tailer[c_sections]; w += sizeof(t_vxl_section_header) * c_sections; byte* body_start = w; for (int i = 0; i < c_sections; i++) { const t_vxl4_section_header& section_header = *reinterpret_cast<const t_vxl4_section_header*>(r); const int cx = section_header.cx; const int cy = section_header.cy; const int cz = section_header.cz; strcpy(d_section_header->id, section_header.id); d_section_header->section_i = section_header.section_i; d_section_header->one = 1; d_section_header->zero = 0; d_section_header++; __int32* span_start_list = reinterpret_cast<__int32*>(w); section_tailer[i].span_start_ofs = w - body_start; w += 4 * cx * cy; __int32* span_end_list = reinterpret_cast<__int32*>(w); section_tailer[i].span_end_ofs = w - body_start; w += 4 * cx * cy; section_tailer[i].span_data_ofs = w - body_start; section_tailer[i].scale = section_header.scale; section_tailer[i].x_min_scale = section_header.x_min_scale; section_tailer[i].y_min_scale = section_header.y_min_scale; section_tailer[i].z_min_scale = section_header.z_min_scale; section_tailer[i].x_max_scale = section_header.x_max_scale; section_tailer[i].y_max_scale = section_header.y_max_scale; section_tailer[i].z_max_scale = section_header.z_max_scale; section_tailer[i].cx = section_header.cx; section_tailer[i].cy = section_header.cy; section_tailer[i].cz = section_header.cz; section_tailer[i].unknown = section_header.unknown; r += sizeof(t_vxl4_section_header); byte* span_data_start = w; for (int j = 0; j < cx * cy; j++) { byte* span_start = w; int z = 0; while (z < cz) { int z_inc = *r++; if (z_inc == cz) break; z += *w++ = z_inc; int count = *w++ = *r++; int c = count; while (c--) { *w++ = *r++; *w++ = *r++; z++; } *w++ = count; } if (span_start == w) { span_start_list[j] = -1; span_end_list[j] = -1; } else { span_start_list[j] = span_start - span_data_start; span_end_list[j] = w - span_data_start - 1; } } } header.size = w - body_start; memcpy(w, section_tailer, sizeof(t_vxl_section_tailer) * c_sections); delete[] section_tailer; w += sizeof(t_vxl_section_tailer) * c_sections; assert(!cb_d || d.size() == w - d.data()); return cb_d ? d : Cvirtual_binary(d.data(), w - d.data()); }
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); }
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; }
Cvirtual_image Cshp_file::vimage() const { Cvirtual_binary image; decode(image.write_start(cb_video())); return Cvirtual_image(image, cx(), cf() * cy(), cb_pixel()); }
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; }