Skip to content

Commit

Permalink
Use std::wstring
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Nov 3, 2024
1 parent 2734bf6 commit 7ddafe1
Showing 1 changed file with 11 additions and 32 deletions.
43 changes: 11 additions & 32 deletions src/patch_java_manifest_for_utf8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,19 @@ int wmain(int argc, wchar_t *argv[]) {
std::wstring manifest((wchar_t *) manifestData, manifestLen / sizeof(wchar_t));
fwprintf(stderr, L"Manifest data: %S\n", manifest.c_str());

// Modify the manifest data to prepend the activeCodePage element
wchar_t *pInsert =
L"<activeCodePage xmlns=\"http://schemas.microsoft.com/SMI/2019/WindowsSettings\">UTF-8</activeCodePage>";
wchar_t *pEndTag = L"</asmv3:windowsSettings>";
wchar_t *pEndTagPos = wcsstr((wchar_t *) manifestData, pEndTag);
if (!pEndTagPos) {
fwprintf(stderr, L"End tag not found.\n");
return 1;
}

size_t insertLen = wcslen(pInsert) * sizeof(wchar_t);
size_t endTagLen = wcslen(pEndTag) * sizeof(wchar_t);
size_t newSize = manifestLen + insertLen;

fwprintf(stderr, L"Read manifest data of size %d bytes\n", manifestLen);
UnlockResource(manifestHandle);
FreeResource(manifestHandle);

// Allocate new buffer for modified data
auto *pNewData = (wchar_t *) malloc(newSize);
if (!pNewData) {
fwprintf(stderr, L"Memory allocation failed.\n");
std::size_t insertPos = manifest.find(L"</asmv3:windowsSettings>");
if (insertPos == std::wstring::npos) {
fwprintf(stderr, L"End tag not found.\n");
return 1;
}
std::wstring newManifest = manifest.substr(0, insertPos) +
L"<activeCodePage xmlns=\"http://schemas.microsoft.com/SMI/2019/WindowsSettings\">UTF-8</activeCodePage>" +
manifest.substr(insertPos);

// Copy data up to the end tag
size_t prefixLen = (pEndTagPos - (wchar_t *) manifestData) * sizeof(wchar_t);
memcpy(pNewData, manifestData, prefixLen);

// Insert the new XML content
memcpy((char *) pNewData + prefixLen, pInsert, insertLen);

// Copy the end tag and the rest of the data
memcpy((char *) pNewData + prefixLen + insertLen, pEndTagPos, endTagLen);

fwprintf(stderr, L"Modified manifest data of size %zd bytes\n", newSize);
fwprintf(stderr, L"Modified manifest data: %S\n", newManifest.c_str());

HANDLE updateHandle = BeginUpdateResourceW(argv[1], false);
if (!updateHandle) {
Expand All @@ -99,8 +78,8 @@ int wmain(int argc, wchar_t *argv[]) {
MAKEINTRESOURCEW(1),
MAKEINTRESOURCEW(24),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
pNewData,
newSize)) {
newManifest.c_str(),
newManifest.size() * sizeof(wchar_t))) {
fwprintf(stderr, L"Error updating resource.\n");
return 1;
}
Expand Down

0 comments on commit 7ddafe1

Please sign in to comment.