remove some obviated const_casts

This commit is contained in:
Martin Michelsen
2022-02-05 19:23:08 -08:00
parent 04b21f34d4
commit 64d6b69a8b
9 changed files with 32 additions and 35 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ bool Client::send(string&& data) {
}
if (this->crypt_out.get()) {
this->crypt_out->encrypt(const_cast<char*>(data.data()), data.size());
this->crypt_out->encrypt(data.data(), data.size());
}
struct evbuffer* buf = bufferevent_get_output(this->bev);
+6 -8
View File
@@ -67,7 +67,7 @@ void ProxyServer::send_to_end(const std::string& data, bool to_server) {
PSOEncryption* crypt = to_server ? this->server_output_crypt.get() : this->client_output_crypt.get();
if (crypt) {
string crypted_data = data;
crypt->encrypt(const_cast<char*>(crypted_data.data()), crypted_data.size());
crypt->encrypt(crypted_data.data(), crypted_data.size());
evbuffer_add(buf, crypted_data.data(), crypted_data.size());
} else {
evbuffer_add(buf, data.data(), data.size());
@@ -274,21 +274,19 @@ void ProxyServer::receive_and_process_commands(bool from_server) {
}
string command(command_size, '\0');
ssize_t bytes = evbuffer_remove(source_buf,
const_cast<char*>(command.data()), command_size);
ssize_t bytes = evbuffer_remove(source_buf, command.data(), command_size);
if (bytes < static_cast<ssize_t>(command_size)) {
throw logic_error("enough bytes available, but could not remove them");
}
//log(INFO, "[ProxyServer-debug] read command (%zX bytes)", bytes);
// overwrite the header with the already-decrypted header
memcpy(const_cast<char*>(command.data()), input_header, this->header_size);
memcpy(command.data(), input_header, this->header_size);
//log(INFO, "[ProxyServer-debug] received encrypted command with pre-decrypted header");
//print_data(stderr, command);
if (source_crypt) {
source_crypt->decrypt(
const_cast<char*>(command.data() + this->header_size),
source_crypt->decrypt(command.data() + this->header_size,
command_size - this->header_size);
}
@@ -346,7 +344,7 @@ void ProxyServer::receive_and_process_commands(bool from_server) {
}
ReconnectCommandArgs* args = reinterpret_cast<ReconnectCommandArgs*>(
const_cast<char*>(command.data() + this->header_size));
command.data() + this->header_size);
memset(&this->next_destination, 0, sizeof(this->next_destination));
struct sockaddr_in* sin = reinterpret_cast<struct sockaddr_in*>(
&this->next_destination);
@@ -378,7 +376,7 @@ void ProxyServer::receive_and_process_commands(bool from_server) {
// reencrypt and forward the command
if (dest_buf) {
if (dest_crypt) {
dest_crypt->encrypt(const_cast<char*>(command.data()), command.size());
dest_crypt->encrypt(command.data(), command.size());
}
//log(INFO, "[ProxyServer-debug] sending encrypted command");
//print_data(stderr, command);
+2 -2
View File
@@ -54,7 +54,7 @@ commands:\n\
if (data.size() == 0) {
throw invalid_argument("no data given");
}
uint16_t* size_field = reinterpret_cast<uint16_t*>(const_cast<char*>(data.data() + 2));
uint16_t* size_field = reinterpret_cast<uint16_t*>(data.data() + 2);
*size_field = data.size();
log(INFO, "%s (from proxy):", to_client ? "server" : "client");
@@ -78,7 +78,7 @@ commands:\n\
}
data.push_back('\0');
data.resize((data.size() + 3) & (~3));
uint16_t* size_field = reinterpret_cast<uint16_t*>(const_cast<char*>(data.data() + 2));
uint16_t* size_field = reinterpret_cast<uint16_t*>(data.data() + 2);
*size_field = data.size();
log(INFO, "client (from proxy):");
+6 -7
View File
@@ -366,7 +366,7 @@ string Quest::decode_gci(const string& filename) {
"GCI file is truncated before download quest header (have 0x%zX bytes)", data.size()));
}
PSODownloadQuestHeader* h = reinterpret_cast<PSODownloadQuestHeader*>(
const_cast<char*>(data.data() + 0x2080));
data.data() + 0x2080);
h->byteswap();
string compressed_data_with_header = data.substr(0x2088, h->size);
@@ -385,8 +385,8 @@ string Quest::decode_gci(const string& filename) {
if (compressed_data_with_header.size() < sizeof(DecryptedHeader)) {
throw runtime_error("GCI file compressed data truncated during header");
}
DecryptedHeader* dh = reinterpret_cast<DecryptedHeader*>(const_cast<char*>(
compressed_data_with_header.data()));
DecryptedHeader* dh = reinterpret_cast<DecryptedHeader*>(
compressed_data_with_header.data());
if (dh->unknown1 || dh->unknown2 || dh->unknown4) {
throw runtime_error("GCI file appears to be encrypted");
}
@@ -476,8 +476,7 @@ vector<shared_ptr<const Quest>> QuestIndex::filter(GameVersion version,
static string create_download_quest_file(const string& compressed_data,
size_t decompressed_size) {
string data(8, '\0');
auto* header = reinterpret_cast<PSODownloadQuestHeader*>(const_cast<char*>(
compressed_data.data()));
auto* header = reinterpret_cast<PSODownloadQuestHeader*>(data.data());
header->size = decompressed_size + sizeof(PSODownloadQuestHeader);
header->encryption_seed = random_object<uint32_t>();
data += compressed_data;
@@ -487,7 +486,7 @@ static string create_download_quest_file(const string& compressed_data,
// TODO: for DC quests, do we use DC encryption?
PSOPCEncryption encr(header->encryption_seed);
encr.encrypt(const_cast<char*>(data.data() + sizeof(PSODownloadQuestHeader)),
encr.encrypt(data.data() + sizeof(PSODownloadQuestHeader),
data.size() - sizeof(PSODownloadQuestHeader));
return data;
}
@@ -498,7 +497,7 @@ shared_ptr<Quest> Quest::create_download_quest(const string& file_basename) cons
}
string decompressed_bin = prs_decompress(*this->bin_contents());
void* data_ptr = const_cast<char*>(decompressed_bin.data());
void* data_ptr = decompressed_bin.data();
switch (this->version) {
case GameVersion::DC:
reinterpret_cast<PSOQuestHeaderDC*>(data_ptr)->is_download = 0x01;
+6 -6
View File
@@ -2220,27 +2220,27 @@ void process_command(shared_ptr<ServerState> s, shared_ptr<Client> c,
if (c->version == GameVersion::BB) {
data_to_print.resize(size + 8);
PSOCommandHeaderBB* header = reinterpret_cast<PSOCommandHeaderBB*>(
const_cast<char*>(data_to_print.data()));
data_to_print.data());
header->command = command;
header->flag = flag;
header->size = size + 8;
memcpy(const_cast<char*>(data_to_print.data() + 8), data, size);
memcpy(data_to_print.data() + 8, data, size);
} else if (c->version == GameVersion::PC) {
data_to_print.resize(size + 4);
PSOCommandHeaderPC* header = reinterpret_cast<PSOCommandHeaderPC*>(
const_cast<char*>(data_to_print.data()));
data_to_print.data());
header->command = command;
header->flag = flag;
header->size = size + 4;
memcpy(const_cast<char*>(data_to_print.data() + 4), data, size);
memcpy(data_to_print.data() + 4, data, size);
} else { // DC/GC
data_to_print.resize(size + 4);
PSOCommandHeaderDCGC* header = reinterpret_cast<PSOCommandHeaderDCGC*>(
const_cast<char*>(data_to_print.data()));
data_to_print.data());
header->command = command;
header->flag = flag;
header->size = size + 4;
memcpy(const_cast<char*>(data_to_print.data() + 4), data, size);
memcpy(data_to_print.data() + 4, data, size);
}
print_data(stderr, data_to_print);
}
+8 -8
View File
@@ -360,8 +360,8 @@ void send_guild_card_chunk_bb(shared_ptr<Client> c, size_t chunk_index) {
}
string contents(8, '\0');
*reinterpret_cast<uint32_t*>(const_cast<char*>(contents.data())) = 0;
*reinterpret_cast<uint32_t*>(const_cast<char*>(contents.data() + 4)) = chunk_index;
*reinterpret_cast<uint32_t*>(contents.data()) = 0;
*reinterpret_cast<uint32_t*>(contents.data() + 4) = chunk_index;
contents.append(reinterpret_cast<char*>(&c->player.guild_cards + chunk_offset),
data_size);
@@ -469,7 +469,7 @@ static void send_large_message_pc_patch_bb(shared_ptr<Client> c, uint8_t command
u16string data;
if (include_header) {
data.resize(sizeof(LargeMessageOptionalHeader) / sizeof(char16_t));
*reinterpret_cast<LargeMessageOptionalHeader*>(const_cast<char16_t*>(data.data())) =
*reinterpret_cast<LargeMessageOptionalHeader*>(data.data()) =
{0, from_serial_number};
}
data += text;
@@ -483,7 +483,7 @@ static void send_large_message_dc_gc(shared_ptr<Client> c, uint8_t command,
string data;
if (include_header) {
data.resize(sizeof(LargeMessageOptionalHeader) / sizeof(char));
*reinterpret_cast<LargeMessageOptionalHeader*>(const_cast<char*>(data.data())) =
*reinterpret_cast<LargeMessageOptionalHeader*>(data.data()) =
{0, from_serial_number};
}
data += encode_sjis(text);
@@ -2025,7 +2025,7 @@ void send_ep3_card_list_update(shared_ptr<Client> c) {
auto file_data = file_cache.get("system/ep3/cardupdate.mnr");
string data("\0\0\0\0", 4);
*reinterpret_cast<uint32_t*>(const_cast<char*>(data.data())) = file_data->size();
*reinterpret_cast<uint32_t*>(data.data()) = file_data->size();
data += *file_data;
data.resize((data.size() + 3) & ~3);
@@ -2049,7 +2049,7 @@ void send_ep3_map_list(shared_ptr<Lobby> l) {
auto file_data = file_cache.get("system/ep3/maplist.mnr");
string data(16, '\0');
PSOSubcommand* subs = reinterpret_cast<PSOSubcommand*>(const_cast<char*>(data.data()));
PSOSubcommand* subs = reinterpret_cast<PSOSubcommand*>(data.data());
subs[0].dword = 0x000000B6;
subs[1].dword = (23 + file_data->size()) & 0xFFFFFFFC;
subs[2].dword = 0x00000040;
@@ -2065,7 +2065,7 @@ void send_ep3_map_data(shared_ptr<Lobby> l, uint32_t map_id) {
auto file_data = file_cache.get(filename);
string data(12, '\0');
PSOSubcommand* subs = reinterpret_cast<PSOSubcommand*>(const_cast<char*>(data.data()));
PSOSubcommand* subs = reinterpret_cast<PSOSubcommand*>(data.data());
subs[0].dword = 0x000000B6;
subs[1].dword = (19 + file_data->size()) & 0xFFFFFFFC;
subs[2].dword = 0x00000041;
@@ -2168,7 +2168,7 @@ void send_server_time(shared_ptr<Client> c) {
gmtime_r(&t_secs, &t_parsed);
string time_str(128, 0);
size_t len = strftime(const_cast<char*>(time_str.data()), time_str.size(),
size_t len = strftime(time_str.data(), time_str.size(),
"%Y:%m:%d: %H:%M:%S.000", &t_parsed);
if (len == 0) {
throw runtime_error("format_time buffer too short");
+1 -1
View File
@@ -184,7 +184,7 @@ void Server::receive_and_process_commands(shared_ptr<Client> c) {
size_t new_bytes = evbuffer_get_length(buf);
new_bytes &= ~(header_size - 1); // only read in multiples of header_size
c->recv_buffer.resize(existing_bytes + new_bytes);
void* recv_ptr = const_cast<char*>(c->recv_buffer.data() + existing_bytes);
void* recv_ptr = c->recv_buffer.data() + existing_bytes;
if (evbuffer_remove(buf, recv_ptr, new_bytes) != static_cast<ssize_t>(new_bytes)) {
throw runtime_error("some bytes could not be read from the receive buffer");
}
+1 -1
View File
@@ -57,7 +57,7 @@ void Shell::read_stdin() {
}
string command(2048, '\0');
if (!fgets(const_cast<char*>(command.data()), command.size(), stdin)) {
if (!fgets(command.data(), command.size(), stdin)) {
if (!any_command_read) {
// ctrl+d probably; we should exit
fputc('\n', stderr);
+1 -1
View File
@@ -72,6 +72,6 @@ size_t add_color_inplace(T* a) {
template <typename T>
void add_color_inplace(std::basic_string<T>& a, size_t header_bytes) {
size_t count = add_color_inplace(const_cast<T*>(a.data() + header_bytes));
size_t count = add_color_inplace(a.data() + header_bytes);
a.resize(count + header_bytes);
}