Skip to content

Commit

Permalink
makerom: Fix too much file handle cause memory leak (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
R-YaTian authored Dec 29, 2024
1 parent 06ac1b8 commit 745fc4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
21 changes: 10 additions & 11 deletions makerom/src/cia.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int GetSettingsFromNcch0(cia_settings *ciaset, u32 ncch0_offset);
int GetTmdDataFromNcch(cia_settings *ciaset, u8 *ncch, ncch_info *ncch_ctx, u8 *key);
int GetMetaRegion(cia_settings *ciaset, u8 *ncch, ncch_info *ncch_ctx, u8 *key);
int GetContentFilePtrs(cia_settings *ciaset, user_settings *usrset);
int ImportNcchContent(cia_settings *ciaset);
int ImportNcchContent(cia_settings *ciaset, user_settings *usrset);
int GetSettingsFromSrl(cia_settings *ciaset);
int GetSettingsFromCci(cia_settings *ciaset);

Expand Down Expand Up @@ -143,7 +143,7 @@ int GetCiaSettings(cia_settings *ciaset, user_settings *usrset)
return result;
if((result = GetContentFilePtrs(ciaset,usrset)) != 0)
return result;
if((result = ImportNcchContent(ciaset)) != 0)
if((result = ImportNcchContent(ciaset,usrset)) != 0)
return result;
}

Expand Down Expand Up @@ -418,7 +418,7 @@ int GetContentFilePtrs(cia_settings *ciaset, user_settings *usrset)
}
ciaset->content.fileSize[j] = GetFileSize64(usrset->common.contentPath[i]);
ciaset->content.filePtrs[j] = fopen(usrset->common.contentPath[i],"rb");

if(usrset->cia.contentId[i] > MAX_U32)
ciaset->content.id[j] = u32GetRand();
else
Expand All @@ -428,7 +428,7 @@ int GetContentFilePtrs(cia_settings *ciaset, user_settings *usrset)

// Get Data from ncch HDR
ReadNcchHdr(hdr,ciaset->content.filePtrs[j]);

// Get Size
u64 calcSize = GetNcchSize(hdr);
if(calcSize != ciaset->content.fileSize[j]){
Expand All @@ -438,10 +438,10 @@ int GetContentFilePtrs(cia_settings *ciaset, user_settings *usrset)

ciaset->content.size[j] = align(calcSize,CIA_CONTENT_ALIGN);
ciaset->content.offset[j] = ciaset->content.totalSize;

ciaset->content.totalSize += ciaset->content.size[j];


fclose(ciaset->content.filePtrs[j]);
// Finish get next content
j++;
}
Expand All @@ -461,7 +461,7 @@ int GetContentFilePtrs(cia_settings *ciaset, user_settings *usrset)
return 0;
}

int ImportNcchContent(cia_settings *ciaset)
int ImportNcchContent(cia_settings *ciaset, user_settings *usrset)
{
ciaset->ciaSections.content.buffer = realloc(ciaset->ciaSections.content.buffer,ciaset->content.totalSize);
if(!ciaset->ciaSections.content.buffer){
Expand All @@ -473,17 +473,16 @@ int ImportNcchContent(cia_settings *ciaset)
for(int i = 1; i < ciaset->content.count; i++){
// Import
u8 *ncchpos = (u8*)(ciaset->ciaSections.content.buffer+ciaset->content.offset[i]);

ciaset->content.filePtrs[i] = fopen(usrset->common.contentPath[i], "rb");
ReadFile64(ncchpos, ciaset->content.fileSize[i], 0, ciaset->content.filePtrs[i]);
if(ModifyNcchIds(ncchpos, NULL, ncch0hdr->programId, ciaset->keys) != 0)
return -1;

// Set Additional Flags
if(ciaset->content.IsDlc)
ciaset->content.flags[i] |= content_Optional;

//if(unknown condition)
// ciaset->content.flags[i] |= content_Shared;
fclose(ciaset->content.filePtrs[i]);
}

ciaset->ciaSections.content.size = ciaset->content.totalSize;
Expand Down
13 changes: 8 additions & 5 deletions makerom/src/ncch.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,10 +973,13 @@ int VerifyNcch(u8 *ncch, keys_struct *keys, bool CheckHash, bool SuppressOutput)
int ModifyNcchIds(u8 *ncch, u8 *titleId, u8 *programId, keys_struct *keys)
{
if(!IsNcch(NULL,ncch))
{
fprintf(stderr, "[NCCH ERROR] Content not a valid ncch\n");
return -1;

}

ncch_hdr *hdr = (ncch_hdr*)ncch;

bool titleIdMatches = titleId == NULL? true : memcmp(titleId,hdr->titleId,8) == 0;
bool programIdMatches = programId == NULL? true : memcmp(programId,hdr->programId,8) == 0;

Expand All @@ -990,7 +993,7 @@ int ModifyNcchIds(u8 *ncch, u8 *titleId, u8 *programId, keys_struct *keys)

ncch_info ncchInfo;
u8 *romfs = NULL;

//Decrypting if necessary
if(IsNcchEncrypted(hdr)){
GetNcchInfo(&ncchInfo,hdr);
Expand All @@ -1001,14 +1004,14 @@ int ModifyNcchIds(u8 *ncch, u8 *titleId, u8 *programId, keys_struct *keys)
}
CryptNcchRegion(romfs,ncchInfo.romfsSize,0,ncchInfo.titleId,keys->aes.ncchKey1,ncch_romfs);
}

// Editing data and resigning
if(titleId)
memcpy(hdr->titleId,titleId,8);
if(programId)
memcpy(hdr->programId,programId,8);
SignCFA(hdr,keys);

// Re-encrypting if necessary
if(IsNcchEncrypted(hdr)){
GetNcchInfo(&ncchInfo,hdr);
Expand Down

0 comments on commit 745fc4f

Please sign in to comment.