1
0
mirror of https://github.com/RPCS3/rpcs3.git synced 2024-11-25 12:12:50 +01:00

pine: simplify writing strings

This commit is contained in:
Megamouse 2023-01-07 13:01:33 +01:00 committed by Ivan
parent b9f733c401
commit 99ed77da8c

View File

@ -152,6 +152,17 @@ namespace pine
return IPCBuffer{ 5, MakeFailIPC(ret_buffer) }; return IPCBuffer{ 5, MakeFailIPC(ret_buffer) };
}; };
const auto write_string = [&](const std::string& str)
{
if (!SafetyChecks(buf_cnt, 0, ret_cnt, str.size() + 1 + sizeof(u32), buf_size))
return false;
ToArray(ret_buffer, ::narrow<u32>(str.size() + 1), ret_cnt);
ret_cnt += sizeof(u32);
memcpy(&ret_buffer[ret_cnt], str.data(), str.size());
ret_cnt += str.size();
ret_buffer[ret_cnt++] = '\0';
return true;
};
while (buf_cnt < buf_size) while (buf_cnt < buf_size)
{ {
@ -270,15 +281,8 @@ namespace pine
} }
case MsgVersion: case MsgVersion:
{ {
char version[256] = {}; if (!write_string("RPCS3 " + Impl::get_version_and_branch()))
snprintf(version, sizeof(version), "RPCS3 %s", Impl::get_version_and_branch().c_str());
const u32 size = strlen(version) + 1;
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
return error(); return error();
ToArray(ret_buffer, size, ret_cnt);
ret_cnt += 4;
memcpy(&ret_buffer[ret_cnt], version, size);
ret_cnt += size;
break; break;
} }
case MsgStatus: case MsgStatus:
@ -293,62 +297,26 @@ namespace pine
} }
case MsgTitle: case MsgTitle:
{ {
const auto title_string = Impl::get_title(); if (!write_string(Impl::get_title()))
const auto size = title_string.size() + 1;
char* title = new char[size];
snprintf(title, size, "%s", title_string.c_str());
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
return error(); return error();
ToArray(ret_buffer, size, ret_cnt);
ret_cnt += 4;
memcpy(&ret_buffer[ret_cnt], title, size);
ret_cnt += size;
delete[] title;
break; break;
} }
case MsgID: case MsgID:
{ {
const auto title_id_string = Impl::get_title_ID(); if (!write_string(Impl::get_title_ID()))
const auto size = title_id_string.size() + 1;
char* title_id = new char[size];
snprintf(title_id, size, "%s", title_id_string.c_str());
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
return error(); return error();
ToArray(ret_buffer, size, ret_cnt);
ret_cnt += 4;
memcpy(&ret_buffer[ret_cnt], title_id, size);
ret_cnt += size;
delete[] title_id;
break; break;
} }
case MsgUUID: case MsgUUID:
{ {
const auto hash_string = Impl::get_executable_hash(); if (!write_string(Impl::get_executable_hash()))
const auto size = hash_string.size() + 1;
char* hash = new char[size];
snprintf(hash, size, "%s", hash_string.c_str());
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size + 4, buf_size))
return error(); return error();
ToArray(ret_buffer, size, ret_cnt);
ret_cnt += 4;
memcpy(&ret_buffer[ret_cnt], hash, size);
ret_cnt += size;
delete[] hash;
break; break;
} }
case MsgGameVersion: case MsgGameVersion:
{ {
const auto game_version_string = Impl::get_app_version(); if (!write_string(Impl::get_app_version()))
const auto size = game_version_string.size() + 1;
char* game_version = new char[size];
snprintf(game_version, size, "%s", game_version_string.c_str());
if (!SafetyChecks(buf_cnt, 0, ret_cnt, size, buf_size))
return error(); return error();
ToArray(ret_buffer, size, ret_cnt);
ret_cnt += 4;
memcpy(&ret_buffer[ret_cnt], game_version, size);
ret_cnt += size;
delete[] game_version;
break; break;
} }
default: default: