got rid of magic numbers

This commit is contained in:
applestash
2014-07-15 15:16:43 +10:00
parent 1c8f323a1a
commit 0d65ce286e
5 changed files with 70 additions and 41 deletions
+15 -15
View File
@@ -199,7 +199,7 @@ int GetSettingsFromUsrset(cia_settings *ciaset, user_settings *usrset)
if(result) return result;
// Tmd Stuff
if(usrset->cia.contentId[0] > 0xffffffff)
if(usrset->cia.contentId[0] > MAX_U32)
ciaset->content.id[0] = u32GetRand();
else
ciaset->content.id[0] = usrset->cia.contentId[0];
@@ -282,10 +282,10 @@ finish:
int GetCIADataFromNcch(cia_settings *ciaset, u8 *ncch, ncch_struct *ncch_ctx, u8 *key)
{
extended_hdr *exhdr = malloc(0x400);
memcpy(exhdr,ncch+ncch_ctx->exhdrOffset,0x400);
extended_hdr *exhdr = malloc(sizeof(extended_hdr));
memcpy(exhdr,ncch+ncch_ctx->exhdrOffset,sizeof(extended_hdr));
if(key != NULL)
CryptNCCHSection((u8*)exhdr,0x400,0,ncch_ctx,key,ncch_exhdr);
CryptNCCHSection((u8*)exhdr,sizeof(extended_hdr),0,ncch_ctx,key,ncch_exhdr);
u16 Category = u8_to_u16((ciaset->common.titleId+2),BE);
if(IsPatch(Category)||ciaset->content.IsCfa||ciaset->content.keyNotFound)
@@ -300,25 +300,25 @@ int GetCIADataFromNcch(cia_settings *ciaset, u8 *ncch, ncch_struct *ncch_ctx, u8
}
if(ciaset->content.IsCfa||ciaset->content.keyNotFound){
if(ciaset->common.titleVersion[0] == 0xffff){ // '-major' wasn't set
if(ciaset->common.titleVersion[VER_MAJOR] == MAX_U16){ // '-major' wasn't set
if(ciaset->content.IsCfa){ // Is a CFA and can be decrypted
fprintf(stderr,"[CIA ERROR] Invalid major version. Use \"-major\" option.\n");
return CIA_BAD_VERSION;
}
else // CXI which cannot be decrypted
ciaset->common.titleVersion[0] = 0;
ciaset->common.titleVersion[VER_MAJOR] = 0;
}
}
else{ // Is a CXI and can be decrypted
if(ciaset->common.titleVersion[0] != 0xffff){ // '-major' was set
if(ciaset->common.titleVersion[VER_MAJOR] != MAX_U16){ // '-major' was set
fprintf(stderr,"[CIA ERROR] Option \"-major\" cannot be applied for cxi.\n");
return CIA_BAD_VERSION;
}
// Setting remaster ver
ciaset->common.titleVersion[0] = GetRemasterVersion_frm_exhdr(exhdr);
ciaset->common.titleVersion[VER_MAJOR] = GetRemasterVersion_frm_exhdr(exhdr);
}
u16 version = SetupVersion(ciaset->common.titleVersion[0],ciaset->common.titleVersion[1],ciaset->common.titleVersion[2]);
u16 version = SetupVersion(ciaset->common.titleVersion[VER_MAJOR],ciaset->common.titleVersion[VER_MINOR],ciaset->common.titleVersion[VER_MICRO]);
ciaset->tik.version = version;
ciaset->tmd.version = version;
@@ -331,10 +331,10 @@ int GetMetaRegion(cia_settings *ciaset, u8 *ncch, ncch_struct *ncch_ctx, u8 *key
if(ciaset->content.IsCfa || ciaset->content.keyNotFound)
return 0;
extended_hdr *exhdr = malloc(0x400);
memcpy(exhdr,ncch+ncch_ctx->exhdrOffset,0x400);
extended_hdr *exhdr = malloc(sizeof(extended_hdr));
memcpy(exhdr,ncch+ncch_ctx->exhdrOffset,sizeof(extended_hdr));
if(key != NULL)
CryptNCCHSection((u8*)exhdr,0x400,0,ncch_ctx,key,ncch_exhdr);
CryptNCCHSection((u8*)exhdr,sizeof(extended_hdr),0,ncch_ctx,key,ncch_exhdr);
exefs_hdr *exefsHdr = malloc(sizeof(exefs_hdr));
memcpy(exefsHdr,ncch+ncch_ctx->exefsOffset,sizeof(exefs_hdr));
@@ -346,7 +346,7 @@ int GetMetaRegion(cia_settings *ciaset, u8 *ncch, ncch_struct *ncch_ctx, u8 *key
for(int i = 0; i < MAX_EXEFS_SECTIONS; i++){
if(strncmp(exefsHdr->fileHdr[i].name,"icon",8) == 0){
icon_size = u8_to_u32(exefsHdr->fileHdr[i].size,LE);
icon_offset = u8_to_u32(exefsHdr->fileHdr[i].offset,LE) + 0x200;
icon_offset = u8_to_u32(exefsHdr->fileHdr[i].offset,LE) + sizeof(exefs_hdr);
}
}
@@ -392,7 +392,7 @@ int GetContentFilePtrs(cia_settings *ciaset, user_settings *usrset)
ciaset->content.fileSize[j] = GetFileSize_u64(usrset->common.contentPath[i]);
ciaset->content.filePtrs[j] = fopen(usrset->common.contentPath[i],"rb");
if(usrset->cia.contentId[i] == 0x100000000)
if(usrset->cia.contentId[i] > MAX_U32)
ciaset->content.id[j] = u32GetRand();
else
ciaset->content.id[j] = (u32)usrset->cia.contentId[i];
@@ -409,7 +409,7 @@ int GetContentFilePtrs(cia_settings *ciaset, user_settings *usrset)
return FAILED_TO_OPEN_FILE;
}
ciaset->content.size[j] = align(calcSize,0x10);
ciaset->content.size[j] = align(calcSize,CIA_CONTENT_ALIGN);
ciaset->content.offset[j] = ciaset->content.totalSize;
ciaset->content.totalSize += ciaset->content.size[j];
+1
View File
@@ -1,6 +1,7 @@
#pragma once
static const int CIA_ALIGN_SIZE = 0x40;
static const int CIA_CONTENT_ALIGN = 0x10;
// Enums
typedef enum
+8
View File
@@ -28,6 +28,14 @@ typedef enum
GB = 1073741824
} file_unit_size;
typedef enum
{
MAX_U8 = 0xff,
MAX_U16 = 0xffff,
MAX_U32 = 0xffffffff,
MAX_U64 = 0xffffffffffffffff,
} data_type_max;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
+27 -24
View File
@@ -76,9 +76,12 @@ int ParseArgs(int argc, char *argv[], user_settings *usr_settings)
if(!usr_settings->common.outFileName){
char *source_path = NULL;
if(usr_settings->ncch.buildNcch0) source_path = usr_settings->common.rsfPath;
else if(usr_settings->common.workingFileType == infile_ncsd || usr_settings->common.workingFileType == infile_srl) source_path = usr_settings->common.workingFilePath;
else source_path = usr_settings->common.contentPath[0];
if(usr_settings->ncch.buildNcch0)
source_path = usr_settings->common.rsfPath;
else if(usr_settings->common.workingFileType == infile_ncsd || usr_settings->common.workingFileType == infile_srl)
source_path = usr_settings->common.workingFilePath;
else
source_path = usr_settings->common.contentPath[0];
u16 outfile_len = strlen(source_path) + 3;
usr_settings->common.outFileName = calloc(outfile_len,sizeof(char));
if(!usr_settings->common.outFileName){
@@ -117,11 +120,11 @@ void SetDefaults(user_settings *set)
// CIA Info
set->cia.useDataTitleVer = false;
set->cia.titleVersion[0] = 0xffff; // invalid for detection
set->cia.titleVersion[VER_MAJOR] = MAX_U16 // invalid so changes can be detected
set->cia.randomTitleKey = false;
set->common.keys.aes.currentCommonKey = 0x100; // invalid for detection
set->common.keys.aes.currentCommonKey = MAX_U8 + 1; // invalid so changes can be detected
for(int i = 0; i < CIA_MAX_CONTENT; i++){
set->cia.contentId[i] = 0x100000000;
set->cia.contentId[i] = MAX_U32 + 1; // invalid so changes can be detected
}
}
@@ -276,7 +279,7 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set)
else if(strcasecmp(app_type,"ECApp") == 0) set->common.keys.accessDescSign.presetType = desc_preset_EC_APP;
else if(strcasecmp(app_type,"Demo") == 0) set->common.keys.accessDescSign.presetType = desc_preset_DEMO;
else if(strcasecmp(app_type,"DlpChild") == 0 || strcasecmp(app_type,"Dlp") == 0) set->common.keys.accessDescSign.presetType = desc_preset_DLP;
else if(strcasecmp(app_type,"FIRM") == 0) set->common.keys.accessDescSign.presetType = desc_preset_FIRM;
//else if(strcasecmp(app_type,"FIRM") == 0) set->common.keys.accessDescSign.presetType = desc_preset_FIRM;
else{
fprintf(stderr,"[SETTING ERROR] Accessdesc AppType preset '%s' not valid, please manually configure RSF\n",app_type);
return USR_BAD_ARG;
@@ -404,12 +407,12 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set)
return USR_ARG_REQ_PARAM;
}
set->cia.useNormTitleVer = true;
u32 tmp = strtoul(argv[i+1],NULL,10);
if(tmp > 63){
fprintf(stderr,"[SETTING ERROR] Major version: '%d' is too large, max: '63'\n",tmp);
u32 ver = strtoul(argv[i+1],NULL,10);
if(ver > VER_MAJOR_MAX){
fprintf(stderr,"[SETTING ERROR] Major version: '%d' is too large, max: '%d'\n",ver,VER_MAJOR_MAX);
return USR_BAD_ARG;
}
set->cia.titleVersion[0] = tmp;
set->cia.titleVersion[VER_MAJOR] = ver;
return 2;
}
else if(strcmp(argv[i],"-minor") == 0){
@@ -418,12 +421,12 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set)
return USR_ARG_REQ_PARAM;
}
set->cia.useNormTitleVer = true;
u32 tmp = strtoul(argv[i+1],NULL,10);
if(tmp > 63){
fprintf(stderr,"[SETTING ERROR] Minor version: '%d' is too large, max: '63'\n",tmp);
u32 ver = strtoul(argv[i+1],NULL,10);
if(ver > VER_MINOR_MAX){
fprintf(stderr,"[SETTING ERROR] Minor version: '%d' is too large, max: '%d'\n",ver,VER_MINOR_MAX);
return USR_BAD_ARG;
}
set->cia.titleVersion[1] = tmp;
set->cia.titleVersion[VER_MINOR] = ver;
return 2;
}
else if(strcmp(argv[i],"-micro") == 0){
@@ -431,12 +434,12 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set)
PrintArgReqParam("-micro",1);
return USR_ARG_REQ_PARAM;
}
u32 tmp = strtoul(argv[i+1],NULL,10);
if(tmp > 15){
fprintf(stderr,"[SETTING ERROR] Micro version: '%d' is too large, max: '15'\n",tmp);
u32 ver = strtoul(argv[i+1],NULL,10);
if(ver > VER_MICRO_MAX){
fprintf(stderr,"[SETTING ERROR] Micro version: '%d' is too large, max: '%d'\n",ver,VER_MICRO_MAX);
return USR_BAD_ARG;
}
set->cia.titleVersion[2] = tmp;
set->cia.titleVersion[VER_MICRO] = ver;
return 2;
}
else if(strcmp(argv[i],"-dver") == 0){
@@ -445,13 +448,13 @@ int SetArgument(int argc, int i, char *argv[], user_settings *set)
return USR_ARG_REQ_PARAM;
}
set->cia.useDataTitleVer = true;
u32 tmp = strtoul(argv[i+1],NULL,10);
if(tmp > 4095){
fprintf(stderr,"[SETTING ERROR] Data version: '%d' is too large, max: '4095'\n",tmp);
u32 ver = strtoul(argv[i+1],NULL,10);
if(ver > VER_DVER_MAX){
fprintf(stderr,"[SETTING ERROR] Data version: '%d' is too large, max: '%d'\n",ver,VER_DVER_MAX);
return USR_BAD_ARG;
}
set->cia.titleVersion[0] = (tmp >> 6) & 63;
set->cia.titleVersion[1] = tmp & 63;
set->cia.titleVersion[VER_MAJOR] = (ver >> 6) & VER_MAJOR_MAX;
set->cia.titleVersion[VER_MINOR] = ver & VER_MAJOR_MAX;
return 2;
}
else if(strcmp(argv[i],"-rand") == 0){
+19 -2
View File
@@ -1,8 +1,25 @@
#pragma once
#define CCI_MAX_CONTENT 8
#define CIA_MAX_CONTENT 65536
typedef enum
{
CCI_MAX_CONTENT = 8,
CIA_MAX_CONTENT = MAX_U16,
} content_limits;
typedef enum
{
VER_MAJOR,
VER_MINOR,
VER_MICRO
} title_ver_index;
typedef enum
{
VER_MAJOR_MAX = 63,
VER_MINOR_MAX = 63,
VER_MICRO_MAX = 15,
VER_DVER_MAX = 4095,
} title_ver_max;
typedef enum
{