Cvirtual_binary Cfile32::get_mm() { if (!size()) return Cvirtual_binary(); Cwin_handle mh(CreateFileMapping(h(), NULL, PAGE_READONLY, 0, 0, NULL)); void* d = mh ? MapViewOfFile(mh, FILE_MAP_READ, 0, 0, 0) : NULL; return d ? Cvirtual_binary(d, size(), std::shared_ptr<void>(d, UnmapViewOfFile)) : Cvirtual_binary(); }
int xcc_units::load_data() { Cxif_key units_key; if (units_key.load_key(Cvirtual_binary(xcc_dirs::get_data_dir() + units_xif_fname))) return 1; int unit_i = 0; for (t_xif_key_map::iterator i = units_key.m_keys.begin(); i != units_key.m_keys.end(); i++) { t_unit_data_entry& ud = unit_data[unit_i]; Cxif_key& uk = i->second; ud.long_name = uk.get_value_string(vi_ud_long_name); ud.short_name = uk.get_value_string(vi_ud_short_name); ud.cx = uk.get_value_int(vi_ud_cx); ud.cy = uk.get_value_int(vi_ud_cy); ud.base_ox = 0; ud.base_oy = 0; ud.flags = uk.get_value_int(vi_ud_flags); ud.c_rotations = uk.get_value_int(vi_ud_c_rotations); for (t_xif_value_map::const_iterator i = uk.m_values.begin(); i != uk.m_values.end(); i++) { switch (i->first) { case vi_ud_base_ox: ud.base_ox = i->second.get_int(); break; case vi_ud_base_oy: ud.base_oy = i->second.get_int(); break; } } unit_i++; } return 0; }
void CXCCRA2RadarCustomizerDlg::load_image(int id, Cvirtual_image& image) { HINSTANCE hin = AfxGetInstanceHandle(); HRSRC rc = FindResource(hin, MAKEINTRESOURCE(id), "PNG"); HGLOBAL hgl = LoadResource(NULL, rc); image.load(Cvirtual_binary(static_cast<const byte*>(LockResource(hgl)), SizeofResource(NULL, rc))); FreeResource(hgl); }
int CXCCRA2MapUpdaterApp::update() { int error = 0; try { CWaitCursor wait; CInternetSession is; CHttpFile* f = reinterpret_cast<CHttpFile*>(is.OpenURL("http://xccu.sourceforge.net/ra2_maps/official.ucf")); if (!f) error = 1; else { string s; while (1) { int cb_p = f->GetLength(); if (!cb_p) break; char* p = new char[cb_p + 1]; f->Read(p, cb_p); p[cb_p] = 0; s += p; delete[] p; } f->Close(); Cvirtual_tfile f; f.load_data(Cvirtual_binary(s.c_str(), s.length())); while (!f.eof()) { Cmulti_line l = f.read_line(); Cfname fname = xcc_dirs::get_dir(game_ra2) + l.get_next_line('=') + ".mmx"; if (!fname.exists()) { string version = l.get_next_line(','); string link = l.get_next_line(','); error = download_update(link, fname); if (error) { delete_file(fname); MessageBox(NULL, "Error retrieving update.", NULL, MB_ICONERROR); error = 0; } } } } } catch (CInternetException*) { error = 1; } if (error) MessageBox(NULL, "Error querying for update.", NULL, MB_ICONERROR); return error; }
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 file32_read(const string& name) { Cfile32 f; return f.open_read(name) ? Cvirtual_binary() : f.get_mm(); }
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(); }
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 Cvirtual_file::write(const void* d, int cb_d) { write(Cvirtual_binary(d, cb_d)); }