Add name-to-comment support to GPAs

This commit is contained in:
elasota
2021-09-01 22:06:43 -04:00
parent 82fe38dfc7
commit 376fdf16c4
4 changed files with 56 additions and 7 deletions

View File

@@ -227,6 +227,8 @@ int toolMain(int argc, const char **argv)
std::vector<PortabilityLayer::ZipCentralDirectoryFileHeader> resCentralDir; std::vector<PortabilityLayer::ZipCentralDirectoryFileHeader> resCentralDir;
std::vector<uint8_t> fileNameBytes; std::vector<uint8_t> fileNameBytes;
std::vector<size_t> fileNameSizes; std::vector<size_t> fileNameSizes;
std::vector<uint8_t> commentBytes;
std::vector<size_t> commentSizes;
FILE *resF = fopen_utf8(resName.c_str(), "rb"); FILE *resF = fopen_utf8(resName.c_str(), "rb");
if (resF) if (resF)
@@ -266,7 +268,20 @@ int toolMain(int argc, const char **argv)
} }
} }
if (!resStream.SeekCurrent(cdirFile.m_extraFieldLength + cdirFile.m_commentLength)) size_t commentLength = cdirFile.m_commentLength;
commentSizes.push_back(commentLength);
if (commentLength > 0)
{
commentBytes.resize(commentBytes.size() + commentLength);
if (!resStream.Read(&commentBytes[commentBytes.size() - commentLength], commentLength))
{
fprintf(stderr, "Error reading cdir entry");
return -1;
}
}
if (!resStream.SeekCurrent(cdirFile.m_extraFieldLength))
{ {
fprintf(stderr, "Error reading cdir entry"); fprintf(stderr, "Error reading cdir entry");
return -1; return -1;
@@ -275,7 +290,7 @@ int toolMain(int argc, const char **argv)
resCentralDir.push_back(cdirFile); resCentralDir.push_back(cdirFile);
numFiles++; numFiles++;
cdirSize += sizeof(PortabilityLayer::ZipCentralDirectoryFileHeader) + fileNameLength; cdirSize += sizeof(PortabilityLayer::ZipCentralDirectoryFileHeader) + fileNameLength + commentLength;
} }
for (size_t i = 0; i < resCentralDir.size(); i++) for (size_t i = 0; i < resCentralDir.size(); i++)
@@ -296,7 +311,6 @@ int toolMain(int argc, const char **argv)
cdirHeader.m_localHeaderOffset = mergedStream.Tell(); cdirHeader.m_localHeaderOffset = mergedStream.Tell();
cdirHeader.m_extraFieldLength = 0; cdirHeader.m_extraFieldLength = 0;
cdirHeader.m_commentLength = 0;
if (!mergedStream.WriteExact(&resLH, sizeof(resLH))) if (!mergedStream.WriteExact(&resLH, sizeof(resLH)))
{ {
@@ -357,19 +371,22 @@ int toolMain(int argc, const char **argv)
} }
} }
size_t commentBytesOffset = 0;
size_t fnameBytesOffset = 0; size_t fnameBytesOffset = 0;
for (size_t i = 0; i < resCentralDir.size(); i++) for (size_t i = 0; i < resCentralDir.size(); i++)
{ {
size_t fnameSize = fileNameSizes[i]; size_t fnameSize = fileNameSizes[i];
size_t commentSize = commentSizes[i];
const PortabilityLayer::ZipCentralDirectoryFileHeader &cdir = resCentralDir[i]; const PortabilityLayer::ZipCentralDirectoryFileHeader &cdir = resCentralDir[i];
if (!mergedStream.WriteExact(&cdir, sizeof(cdir)) || (fnameSize > 0 && !mergedStream.WriteExact(&fileNameBytes[fnameBytesOffset], fnameSize))) if (!mergedStream.WriteExact(&cdir, sizeof(cdir)) || (fnameSize > 0 && !mergedStream.WriteExact(&fileNameBytes[fnameBytesOffset], fnameSize)) || (commentSize > 0 && !mergedStream.WriteExact(&commentBytes[commentBytesOffset], commentSize)))
{ {
fprintf(stderr, "Error writing directory"); fprintf(stderr, "Error writing directory");
return -1; return -1;
} }
fnameBytesOffset += fnameSize; fnameBytesOffset += fnameSize;
commentBytesOffset += commentSize;
} }
PortabilityLayer::ZipEndOfCentralDirectoryRecord eocd; PortabilityLayer::ZipEndOfCentralDirectoryRecord eocd;

View File

@@ -20,6 +20,7 @@ namespace PortabilityLayer
, m_numResources(0) , m_numResources(0)
, m_compiledTypeListBlob(nullptr) , m_compiledTypeListBlob(nullptr)
, m_numResourceTypes(0) , m_numResourceTypes(0)
, m_nameListOffset(0)
{ {
} }
@@ -343,6 +344,16 @@ namespace PortabilityLayer
outCount = m_numResourceTypes; outCount = m_numResourceTypes;
} }
const uint8_t *ResourceFile::GetResNames() const
{
return m_resNameBlob;
}
const size_t ResourceFile::GetResNamesSize() const
{
return m_resNameBlobSize;
}
const ResourceCompiledTypeList *ResourceFile::GetResourceTypeList(const ResTypeID &resType) const ResourceCompiledTypeList *ResourceFile::GetResourceTypeList(const ResTypeID &resType)
{ {
const ResourceCompiledTypeList *tlStart = m_compiledTypeListBlob; const ResourceCompiledTypeList *tlStart = m_compiledTypeListBlob;

View File

@@ -21,6 +21,9 @@ namespace PortabilityLayer
void GetAllResourceTypeLists(ResourceCompiledTypeList *&outTypeLists, size_t &outCount) const; void GetAllResourceTypeLists(ResourceCompiledTypeList *&outTypeLists, size_t &outCount) const;
const uint8_t *GetResNames() const;
const size_t GetResNamesSize() const;
const ResourceCompiledTypeList *GetResourceTypeList(const ResTypeID &resType); const ResourceCompiledTypeList *GetResourceTypeList(const ResTypeID &resType);
THandle<void> LoadResource(const ResTypeID &resType, int id); THandle<void> LoadResource(const ResTypeID &resType, int id);
@@ -43,6 +46,8 @@ namespace PortabilityLayer
ResourceCompiledTypeList *m_compiledTypeListBlob; ResourceCompiledTypeList *m_compiledTypeListBlob;
size_t m_numResourceTypes; size_t m_numResourceTypes;
uint32_t m_nameListOffset;
static bool CompiledRefSortPredicate(const ResourceCompiledRef &a, const ResourceCompiledRef &b); static bool CompiledRefSortPredicate(const ResourceCompiledRef &a, const ResourceCompiledRef &b);
static bool CompiledTypeListSortPredicate(const ResourceCompiledTypeList &a, const ResourceCompiledTypeList &b); static bool CompiledTypeListSortPredicate(const ResourceCompiledTypeList &a, const ResourceCompiledTypeList &b);

View File

@@ -52,6 +52,7 @@ struct PlannedEntry
std::vector<uint8_t> m_compressedContents; std::vector<uint8_t> m_compressedContents;
std::string m_name; std::string m_name;
std::string m_comment;
bool m_isDirectory; bool m_isDirectory;
PlannedEntry() PlannedEntry()
@@ -252,7 +253,7 @@ void ExportZipFile(const char *path, std::vector<PlannedEntry> &entries, const P
cdirHeader.m_uncompressedSize = static_cast<uint32_t>(entry.m_uncompressedContents.size()); cdirHeader.m_uncompressedSize = static_cast<uint32_t>(entry.m_uncompressedContents.size());
cdirHeader.m_fileNameLength = static_cast<uint32_t>(entry.m_name.size()); cdirHeader.m_fileNameLength = static_cast<uint32_t>(entry.m_name.size());
cdirHeader.m_extraFieldLength = 0; cdirHeader.m_extraFieldLength = 0;
cdirHeader.m_commentLength = 0; cdirHeader.m_commentLength = static_cast<uint32_t>(entry.m_comment.size());
cdirHeader.m_diskNumber = 0; cdirHeader.m_diskNumber = 0;
cdirHeader.m_internalAttributes = 0; cdirHeader.m_internalAttributes = 0;
cdirHeader.m_externalAttributes = entry.m_isDirectory ? PortabilityLayer::ZipConstants::kDirectoryAttributes : PortabilityLayer::ZipConstants::kArchivedAttributes; cdirHeader.m_externalAttributes = entry.m_isDirectory ? PortabilityLayer::ZipConstants::kDirectoryAttributes : PortabilityLayer::ZipConstants::kArchivedAttributes;
@@ -290,6 +291,7 @@ void ExportZipFile(const char *path, std::vector<PlannedEntry> &entries, const P
{ {
fwrite(&cdirRecords[i], 1, sizeof(PortabilityLayer::ZipCentralDirectoryFileHeader), outF); fwrite(&cdirRecords[i], 1, sizeof(PortabilityLayer::ZipCentralDirectoryFileHeader), outF);
fwrite(entries[i].m_name.c_str(), 1, entries[i].m_name.size(), outF); fwrite(entries[i].m_name.c_str(), 1, entries[i].m_name.size(), outF);
fwrite(entries[i].m_comment.c_str(), 1, entries[i].m_comment.size(), outF);
} }
long cdirEndPos = ftell(outF); long cdirEndPos = ftell(outF);
@@ -2363,7 +2365,6 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
PortabilityLayer::ResourceFile *resFile = PortabilityLayer::ResourceFile::Create(); PortabilityLayer::ResourceFile *resFile = PortabilityLayer::ResourceFile::Create();
resFile->Load(&cfs); resFile->Load(&cfs);
cfs.Close();
PortabilityLayer::ResourceCompiledTypeList *typeLists = nullptr; PortabilityLayer::ResourceCompiledTypeList *typeLists = nullptr;
size_t typeListCount = 0; size_t typeListCount = 0;
@@ -2428,6 +2429,14 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
const void *resData = res.m_resData; const void *resData = res.m_resData;
const size_t resSize = res.GetSize(); const size_t resSize = res.GetSize();
std::string resComment;
if (res.m_resNameOffset >= 0)
{
const uint8_t *pstrStart = resFile->GetResNames() + res.m_resNameOffset;
resComment = std::string(reinterpret_cast<const char*>(pstrStart + 1), pstrStart[0]);
}
if (typeList.m_resType == pictTypeID || typeList.m_resType == dateTypeID) if (typeList.m_resType == pictTypeID || typeList.m_resType == dateTypeID)
{ {
char resName[256]; char resName[256];
@@ -2438,6 +2447,7 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
PlannedEntry entry; PlannedEntry entry;
entry.m_name = resName; entry.m_name = resName;
entry.m_comment = resComment;
if (ImportPICT(entry.m_uncompressedContents, resData, resSize, dumpqtDir, res.m_resID)) if (ImportPICT(entry.m_uncompressedContents, resData, resSize, dumpqtDir, res.m_resID))
contents.push_back(entry); contents.push_back(entry);
@@ -2454,6 +2464,7 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
PlannedEntry entry; PlannedEntry entry;
entry.m_name = resName; entry.m_name = resName;
entry.m_comment = resComment;
if (ImportSound(entry.m_uncompressedContents, resData, resSize, res.m_resID)) if (ImportSound(entry.m_uncompressedContents, resData, resSize, res.m_resID))
contents.push_back(entry); contents.push_back(entry);
@@ -2470,6 +2481,7 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
PlannedEntry entry; PlannedEntry entry;
entry.m_name = resName; entry.m_name = resName;
entry.m_comment = resComment;
if (ImportIndexedString(entry.m_uncompressedContents, resData, resSize)) if (ImportIndexedString(entry.m_uncompressedContents, resData, resSize))
contents.push_back(entry); contents.push_back(entry);
@@ -2484,6 +2496,7 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
PlannedEntry entry; PlannedEntry entry;
entry.m_name = resName; entry.m_name = resName;
entry.m_comment = resComment;
if (ImportDialogItemTemplate(entry.m_uncompressedContents, resData, resSize)) if (ImportDialogItemTemplate(entry.m_uncompressedContents, resData, resSize))
contents.push_back(entry); contents.push_back(entry);
@@ -2505,8 +2518,8 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
isIcon = true; isIcon = true;
PlannedEntry entry; PlannedEntry entry;
entry.m_name = resName; entry.m_name = resName;
entry.m_comment = resComment;
if (ImportIcon(entry.m_uncompressedContents, resData, resSize, iconSpec.m_width, iconSpec.m_height, iconSpec.m_bpp)) if (ImportIcon(entry.m_uncompressedContents, resData, resSize, iconSpec.m_width, iconSpec.m_height, iconSpec.m_bpp))
contents.push_back(entry); contents.push_back(entry);
@@ -2527,6 +2540,7 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
PlannedEntry entry; PlannedEntry entry;
entry.m_name = resName; entry.m_name = resName;
entry.m_comment = resComment;
entry.m_uncompressedContents.resize(res.GetSize()); entry.m_uncompressedContents.resize(res.GetSize());
memcpy(&entry.m_uncompressedContents[0], resData, resSize); memcpy(&entry.m_uncompressedContents[0], resData, resSize);
@@ -2537,6 +2551,8 @@ int ConvertSingleFile(const char *resPath, const PortabilityLayer::CombinedTimes
} }
} }
cfs.Close();
if (havePatchFile) if (havePatchFile)
{ {
if (!ApplyPatch(patchFileContents, contents)) if (!ApplyPatch(patchFileContents, contents))