From 5b22fe07f25ba765310adfd5a892c5f29f4d8c12 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Wed, 14 Oct 2020 14:58:33 -0400 Subject: [PATCH] More code cleanup. --- source/cert.c | 5 +++-- source/cnmt.c | 22 ++++++++++++---------- source/gamecard.c | 22 ++++++++++++---------- source/keys.c | 16 +++++++++------- source/nacp.c | 18 +++++++++++------- source/nca.h | 9 ++++----- source/title.c | 35 ++++++++++++++++++++++------------- source/title.h | 3 ++- 8 files changed, 75 insertions(+), 55 deletions(-) diff --git a/source/cert.c b/source/cert.c index 3250ada..23613f3 100644 --- a/source/cert.c +++ b/source/cert.c @@ -385,7 +385,8 @@ static void certCopyCertificateChainDataToMemoryBuffer(void *dst, const Certific u8 *dst_u8 = (u8*)dst; for(u32 i = 0; i < chain->count; i++) { - memcpy(dst_u8, chain->certs[i].data, chain->certs[i].size); - dst_u8 += chain->certs[i].size; + Certificate *cert = &(chain->certs[i]); + memcpy(dst_u8, cert->data, cert->size); + dst_u8 += cert->size; } } diff --git a/source/cnmt.c b/source/cnmt.c index 59b353a..661ce31 100644 --- a/source/cnmt.c +++ b/source/cnmt.c @@ -290,24 +290,26 @@ bool cnmtGenerateAuthoringToolXml(ContentMetaContext *cnmt_ctx, NcaContext *nca_ for(i = 0; i < nca_ctx_count; i++) { + NcaContext *cur_nca_ctx = &(nca_ctx[i]); + /* Check if this NCA is really referenced by our CNMT. */ - if (nca_ctx[i].content_type != NcmContentType_Meta) + if (cur_nca_ctx->content_type != NcmContentType_Meta) { /* Non-Meta NCAs: check if their content IDs are part of the packaged content info entries from the CNMT. */ for(j = 0; j < cnmt_ctx->packaged_header->content_count; j++) { - if (!memcmp(cnmt_ctx->packaged_content_info[j].info.content_id.c, nca_ctx[i].content_id.c, 0x10)) break; + if (!memcmp(cnmt_ctx->packaged_content_info[j].info.content_id.c, cur_nca_ctx->content_id.c, 0x10)) break; } invalid_nca = (j >= cnmt_ctx->packaged_header->content_count); } else { /* Meta NCAs: quick and dirty pointer comparison because why not. */ - invalid_nca = (cnmt_ctx->nca_ctx != &(nca_ctx[i])); + invalid_nca = (cnmt_ctx->nca_ctx != cur_nca_ctx); } if (invalid_nca) { - LOGFILE("NCA \"%s\" isn't referenced by this CNMT!", nca_ctx[i].content_id_str); + LOGFILE("NCA \"%s\" isn't referenced by this CNMT!", cur_nca_ctx->content_id_str); goto end; } @@ -320,12 +322,12 @@ bool cnmtGenerateAuthoringToolXml(ContentMetaContext *cnmt_ctx, NcaContext *nca_ " %u\n" \ " %u\n" \ " \n", \ - titleGetNcmContentTypeName(nca_ctx[i].content_type), \ - nca_ctx[i].content_id_str, \ - nca_ctx[i].content_size, \ - nca_ctx[i].hash_str, \ - nca_ctx[i].key_generation, \ - nca_ctx[i].id_offset)) goto end; + titleGetNcmContentTypeName(cur_nca_ctx->content_type), \ + cur_nca_ctx->content_id_str, \ + cur_nca_ctx->content_size, \ + cur_nca_ctx->hash_str, \ + cur_nca_ctx->key_generation, \ + cur_nca_ctx->id_offset)) goto end; } utilsGenerateHexStringFromData(digest_str, sizeof(digest_str), cnmt_ctx->digest, CNMT_DIGEST_SIZE); diff --git a/source/gamecard.c b/source/gamecard.c index a03e79d..d73790e 100644 --- a/source/gamecard.c +++ b/source/gamecard.c @@ -682,6 +682,8 @@ static void gamecardLoadInfo(void) /* Read hash FS partitions. */ for(u32 i = 0; i < fs_header->entry_count; i++) { + GameCardHashFileSystemPartitionInfo *hfs_partition = &(g_gameCardHfsPartitions[i]); + fs_entry = gamecardGetHashFileSystemEntryByIndex(g_gameCardHfsRootHeader, i); if (!fs_entry || !fs_entry->size) { @@ -689,14 +691,14 @@ static void gamecardLoadInfo(void) goto end; } - g_gameCardHfsPartitions[i].offset = (g_gameCardHeader.partition_fs_header_address + g_gameCardHeader.partition_fs_header_size + fs_entry->offset); - g_gameCardHfsPartitions[i].size = fs_entry->size; + hfs_partition->offset = (g_gameCardHeader.partition_fs_header_address + g_gameCardHeader.partition_fs_header_size + fs_entry->offset); + hfs_partition->size = fs_entry->size; /* Partially read the current hash FS partition header. */ GameCardHashFileSystemHeader partition_header = {0}; - if (!gamecardReadStorageArea(&partition_header, sizeof(GameCardHashFileSystemHeader), g_gameCardHfsPartitions[i].offset, false)) + if (!gamecardReadStorageArea(&partition_header, sizeof(GameCardHashFileSystemHeader), hfs_partition->offset, false)) { - LOGFILE("Failed to partially read hash FS partition #%u header from offset 0x%lX!", i, g_gameCardHfsPartitions[i].offset); + LOGFILE("Failed to partially read hash FS partition #%u header from offset 0x%lX!", i, hfs_partition->offset); goto end; } @@ -713,21 +715,21 @@ static void gamecardLoadInfo(void) } /* Calculate the full header size for the current hash FS partition and round it to a GAMECARD_MEDIA_UNIT_SIZE bytes boundary. */ - g_gameCardHfsPartitions[i].header_size = (sizeof(GameCardHashFileSystemHeader) + (partition_header.entry_count * sizeof(GameCardHashFileSystemEntry)) + partition_header.name_table_size); - g_gameCardHfsPartitions[i].header_size = ALIGN_UP(g_gameCardHfsPartitions[i].header_size, GAMECARD_MEDIA_UNIT_SIZE); + hfs_partition->header_size = (sizeof(GameCardHashFileSystemHeader) + (partition_header.entry_count * sizeof(GameCardHashFileSystemEntry)) + partition_header.name_table_size); + hfs_partition->header_size = ALIGN_UP(hfs_partition->header_size, GAMECARD_MEDIA_UNIT_SIZE); /* Allocate memory for the hash FS partition header. */ - g_gameCardHfsPartitions[i].header = calloc(g_gameCardHfsPartitions[i].header_size, sizeof(u8)); - if (!g_gameCardHfsPartitions[i].header) + hfs_partition->header = calloc(hfs_partition->header_size, sizeof(u8)); + if (!hfs_partition->header) { LOGFILE("Unable to allocate memory for the hash FS partition #%u header!", i); goto end; } /* Finally, read the full hash FS partition header. */ - if (!gamecardReadStorageArea(g_gameCardHfsPartitions[i].header, g_gameCardHfsPartitions[i].header_size, g_gameCardHfsPartitions[i].offset, false)) + if (!gamecardReadStorageArea(hfs_partition->header, hfs_partition->header_size, hfs_partition->offset, false)) { - LOGFILE("Failed to read full hash FS partition #%u header from offset 0x%lX!", i, g_gameCardHfsPartitions[i].offset); + LOGFILE("Failed to read full hash FS partition #%u header from offset 0x%lX!", i, hfs_partition->offset); goto end; } } diff --git a/source/keys.c b/source/keys.c index e6d07e4..42fc13c 100644 --- a/source/keys.c +++ b/source/keys.c @@ -265,23 +265,25 @@ static bool keysRetrieveKeysFromProgramMemory(KeysMemoryInfo *info) { found = false; - if (!info->keys[i].dst) + KeysMemoryKey *key = &(info->keys[i]); + + if (!key->dst) { - LOGFILE("Invalid destination pointer for key \"%s\" in program %016lX!", info->keys[i].name, info->location.program_id); + LOGFILE("Invalid destination pointer for key \"%s\" in program %016lX!", key->name, info->location.program_id); goto end; } /* Hash every key length-sized byte chunk in the process memory buffer until a match is found. */ for(u64 j = 0; j < info->location.data_size; j++) { - if ((info->location.data_size - j) < info->keys[i].size) break; + if ((info->location.data_size - j) < key->size) break; - sha256CalculateHash(tmp_hash, info->location.data + j, info->keys[i].size); + sha256CalculateHash(tmp_hash, info->location.data + j, key->size); - if (!memcmp(tmp_hash, info->keys[i].hash, SHA256_HASH_SIZE)) + if (!memcmp(tmp_hash, key->hash, SHA256_HASH_SIZE)) { /* Jackpot. */ - memcpy(info->keys[i].dst, info->location.data + j, info->keys[i].size); + memcpy(key->dst, info->location.data + j, key->size); found = true; break; } @@ -289,7 +291,7 @@ static bool keysRetrieveKeysFromProgramMemory(KeysMemoryInfo *info) if (!found) { - LOGFILE("Unable to locate key \"%s\" in process memory from program %016lX!", info->keys[i].name, info->location.program_id); + LOGFILE("Unable to locate key \"%s\" in process memory from program %016lX!", key->name, info->location.program_id); goto end; } } diff --git a/source/nacp.c b/source/nacp.c index 40e76de..77b348f 100644 --- a/source/nacp.c +++ b/source/nacp.c @@ -363,7 +363,8 @@ bool nacpGenerateAuthoringToolXml(NacpContext *nacp_ctx, u32 version, u32 requir /* Title. */ for(i = 0, count = 0; i < NacpLanguage_Count; i++) { - if (!strlen(nacp->title[i].name) || !strlen(nacp->title[i].publisher)) continue; + NacpTitle *title = &(nacp->title[i]); + if (!strlen(title->name) || !strlen(title->publisher)) continue; if (!utilsAppendFormattedStringToBuffer(&xml_buf, &xml_buf_size, \ " \n" \ @@ -372,8 +373,8 @@ bool nacpGenerateAuthoringToolXml(NacpContext *nacp_ctx, u32 version, u32 requir " <Publisher>%s</Publisher>\n" \ " \n", \ nacpGetLanguageString(i), - nacp->title[i].name, - nacp->title[i].publisher)) goto end; + title->name, + title->publisher)) goto end; count++; } @@ -621,14 +622,16 @@ bool nacpGenerateAuthoringToolXml(NacpContext *nacp_ctx, u32 version, u32 requir /* ReceivableGroupConfiguration. */ for(i = 0; i < 0x10; i++) { - utilsGenerateHexStringFromData(key_str, sizeof(key_str), ndcc->receivable_group_configurations[i].key, sizeof(ndcc->receivable_group_configurations[i].key)); + NacpApplicationNeighborDetectionGroupConfiguration *rgc = &(ndcc->receivable_group_configurations[i]); + + utilsGenerateHexStringFromData(key_str, sizeof(key_str), rgc->key, sizeof(rgc->key)); if (!utilsAppendFormattedStringToBuffer(&xml_buf, &xml_buf_size, \ " \n" \ " 0x%016lx\n" \ " %s\n" \ " \n", \ - ndcc->receivable_group_configurations[i].group_id, + rgc->group_id, key_str)) goto end; } @@ -649,13 +652,14 @@ bool nacpGenerateAuthoringToolXml(NacpContext *nacp_ctx, u32 version, u32 requir /* RequiredAddOnContentsSet. */ for(i = 0, count = 0; i < 0x20; i++) { - if (!raocsbd->descriptors[i].index || !raocsbd->descriptors[i].continue_set) continue; + NacpDescriptors *descriptor = &(raocsbd->descriptors[i]); + if (!descriptor->index || !descriptor->continue_set) continue; if (!utilsAppendFormattedStringToBuffer(&xml_buf, &xml_buf_size, \ " \n" \ " %u\n" \ " \n", - raocsbd->descriptors[i].index)) goto end; + descriptor->index)) goto end; count++; } diff --git a/source/nca.h b/source/nca.h index 82ff6bd..2322d6e 100644 --- a/source/nca.h +++ b/source/nca.h @@ -441,8 +441,8 @@ NX_INLINE bool ncaValidateHierarchicalSha256Offsets(NcaHierarchicalSha256Data *h for(u32 i = 0; i < hierarchical_sha256_data->hash_region_count; i++) { - if (hierarchical_sha256_data->hash_region[i].offset >= section_size || !hierarchical_sha256_data->hash_region[i].size || \ - (hierarchical_sha256_data->hash_region[i].offset + hierarchical_sha256_data->hash_region[i].size) > section_size) return false; + NcaRegion *hash_region = &(hierarchical_sha256_data->hash_region[i]); + if (hash_region->offset >= section_size || !hash_region->size || (hash_region->offset + hash_region->size) > section_size) return false; } return true; @@ -455,9 +455,8 @@ NX_INLINE bool ncaValidateHierarchicalIntegrityOffsets(NcaIntegrityMetaInfo *int for(u32 i = 0; i < NCA_IVFC_LEVEL_COUNT; i++) { - if (integrity_meta_info->info_level_hash.level_information[i].offset >= section_size || !integrity_meta_info->info_level_hash.level_information[i].size || \ - !integrity_meta_info->info_level_hash.level_information[i].block_order || \ - (integrity_meta_info->info_level_hash.level_information[i].offset + integrity_meta_info->info_level_hash.level_information[i].size) > section_size) return false; + NcaHierarchicalIntegrityVerificationLevelInformation *level_information = &(integrity_meta_info->info_level_hash.level_information[i]); + if (level_information->offset >= section_size || !level_information->size || !level_information->block_order || (level_information->offset + level_information->size) > section_size) return false; } return true; diff --git a/source/title.c b/source/title.c index bb78246..f41f738 100644 --- a/source/title.c +++ b/source/title.c @@ -583,9 +583,11 @@ TitleApplicationMetadata **titleGetApplicationMetadataEntries(bool is_system, u3 for(u32 i = start_idx; i < max_val; i++) { + TitleApplicationMetadata *cur_app_metadata = &(g_appMetadata[i]); + /* Skip current metadata entry if content data for this title isn't available. */ - if ((is_system && !_titleGetInfoFromStorageByTitleId(NcmStorageId_BuiltInSystem, g_appMetadata[i].title_id, false)) || \ - (!is_system && !titleIsUserApplicationContentAvailable(g_appMetadata[i].title_id))) continue; + if ((is_system && !_titleGetInfoFromStorageByTitleId(NcmStorageId_BuiltInSystem, cur_app_metadata->title_id, false)) || \ + (!is_system && !titleIsUserApplicationContentAvailable(cur_app_metadata->title_id))) continue; /* Reallocate pointer buffer. */ tmp_app_metadata = realloc(app_metadata, (app_count + 1) * sizeof(TitleApplicationMetadata*)); @@ -601,7 +603,7 @@ TitleApplicationMetadata **titleGetApplicationMetadataEntries(bool is_system, u3 tmp_app_metadata = NULL; /* Set current pointer and increase counter. */ - app_metadata[app_count++] = &(g_appMetadata[i]); + app_metadata[app_count++] = cur_app_metadata; } if (app_metadata && app_count) @@ -647,9 +649,10 @@ bool titleGetUserApplicationData(u64 app_id, TitleUserApplicationData *out) /* Get first add-on content title info. */ for(u32 i = 0; i < g_titleInfoCount; i++) { - if (g_titleInfo[i].meta_key.type == NcmContentMetaType_AddOnContent && titleCheckIfAddOnContentIdBelongsToApplicationId(app_id, g_titleInfo[i].meta_key.id)) + TitleInfo *title_info = &(g_titleInfo[i]); + if (title_info->meta_key.type == NcmContentMetaType_AddOnContent && titleCheckIfAddOnContentIdBelongsToApplicationId(app_id, title_info->meta_key.id)) { - out->aoc_info = &(g_titleInfo[i]); + out->aoc_info = title_info; break; } } @@ -699,7 +702,8 @@ TitleInfo **titleGetInfoFromOrphanTitles(u32 *out_count) /* Get pointers to orphan title info entries. */ for(u32 i = 0, j = 0; i < g_titleInfoCount && j < g_titleInfoOrphanCount; i++) { - if ((g_titleInfo[i].meta_key.type == NcmContentMetaType_Patch || g_titleInfo[i].meta_key.type == NcmContentMetaType_AddOnContent) && !g_titleInfo[i].parent) orphan_info[j++] = &(g_titleInfo[i]); + TitleInfo *title_info = &(g_titleInfo[i]); + if ((title_info->meta_key.type == NcmContentMetaType_Patch || title_info->meta_key.type == NcmContentMetaType_AddOnContent) && !title_info->parent) orphan_info[j++] = title_info; } /* Sort orphan title info entries by title ID. */ @@ -936,8 +940,11 @@ static bool titleGenerateMetadataEntriesFromSystemTitles(void) /* Fill new application metadata entries. */ for(u32 i = 0; i < g_systemTitlesCount; i++) { - g_appMetadata[g_appMetadataCount + i].title_id = g_systemTitles[i].title_id; - sprintf(g_appMetadata[g_appMetadataCount + i].lang_entry.name, g_systemTitles[i].name); + TitleApplicationMetadata *app_metadata = &(g_appMetadata[g_appMetadataCount + i]); + const SystemTitleName *system_title = &(g_systemTitles[i]); + + app_metadata->title_id = system_title->title_id; + sprintf(app_metadata->lang_entry.name, system_title->name); } /* Sort metadata entries by title ID. */ @@ -1358,12 +1365,12 @@ static bool titleRetrieveContentMetaKeysFromDatabase(u8 storage_id) /* Fill information. */ cur_title_info->storage_id = storage_id; memcpy(&(cur_title_info->meta_key), &(meta_keys[i]), sizeof(NcmContentMetaKey)); - cur_title_info->version.value = meta_keys[i].version; + cur_title_info->version.value = cur_title_info->meta_key.version; - if (cur_title_info->meta_key.type <= NcmContentMetaType_Application) cur_title_info->app_metadata = titleFindApplicationMetadataByTitleId(meta_keys[i].id); + if (cur_title_info->meta_key.type <= NcmContentMetaType_Application) cur_title_info->app_metadata = titleFindApplicationMetadataByTitleId(cur_title_info->meta_key.id); /* Retrieve content infos. */ - if (titleGetContentInfosFromTitle(storage_id, &(meta_keys[i]), &(cur_title_info->content_infos), &(cur_title_info->content_count))) + if (titleGetContentInfosFromTitle(storage_id, &(cur_title_info->meta_key), &(cur_title_info->content_infos), &(cur_title_info->content_count))) { /* Calculate title size. */ u64 tmp_size = 0; @@ -1779,9 +1786,11 @@ static TitleInfo *_titleGetInfoFromStorageByTitleId(u8 storage_id, u64 title_id, for(u32 i = start_idx; i < max_val; i++) { - if (g_titleInfo[i].meta_key.id == title_id && (storage_id == NcmStorageId_Any || (storage_id != NcmStorageId_Any && g_titleInfo[i].storage_id == storage_id))) + TitleInfo *title_info = &(g_titleInfo[i]); + + if (title_info->meta_key.id == title_id && (storage_id == NcmStorageId_Any || (storage_id != NcmStorageId_Any && title_info->storage_id == storage_id))) { - info = &(g_titleInfo[i]); + info = title_info; break; } } diff --git a/source/title.h b/source/title.h index 7704c2d..99b41ae 100644 --- a/source/title.h +++ b/source/title.h @@ -215,7 +215,8 @@ NX_INLINE NcmContentInfo *titleGetContentInfoByTypeAndIdOffset(TitleInfo *info, for(u32 i = 0; i < info->content_count; i++) { - if (info->content_infos[i].content_type == content_type && info->content_infos[i].id_offset == id_offset) return &(info->content_infos[i]); + NcmContentInfo *cur_content_info = &(info->content_infos[i]); + if (cur_content_info->content_type == content_type && cur_content_info->id_offset == id_offset) return cur_content_info; } return NULL;