From 17ada97b051a5ab9f187a659b384db6b2c081782 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 11 Aug 2018 21:39:07 -0400 Subject: [PATCH] nax0: Invert conditionals within nax0_save() Allows unindenting most of the code within the function to be nicer to follow. --- nax0.c | 75 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/nax0.c b/nax0.c index 0d9f63a..6784201 100644 --- a/nax0.c +++ b/nax0.c @@ -108,43 +108,46 @@ void nax0_save(nax0_ctx_t *ctx) { /* Save Decrypted Contents. */ filepath_t *dec_path = &ctx->tool_ctx->settings.plaintext_path; - if (dec_path->valid == VALIDITY_VALID) { - printf("Saving Decrypted NAX0 Content to %s...\n", dec_path->char_path); - FILE *f_dec = os_fopen(dec_path->os_path, OS_MODE_WRITE); - - if (f_dec != NULL) { - uint64_t ofs = 0x4000; - uint64_t end_ofs = ofs + ctx->header.size; - unsigned char *buf = malloc(0x400000); - if (buf == NULL) { - fprintf(stderr, "Failed to allocate file-save buffer!\n"); - exit(EXIT_FAILURE); - } - - uint64_t read_size = 0x400000; /* 4 MB buffer. */ - memset(buf, 0xCC, read_size); /* Debug in case I fuck this up somehow... */ - while (ofs < end_ofs) { - if (ofs + read_size >= end_ofs) read_size = end_ofs - ofs; - if (nax0_read(ctx, ofs, buf, read_size) != read_size) { - fprintf(stderr, "Failed to read file!\n"); - exit(EXIT_FAILURE); - } - - uint64_t dec_size = (read_size + 0x3FFF) & ~0x3FFF; - aes_xts_decrypt(ctx->aes_ctx, buf, buf, dec_size, (ofs - 0x4000) >> 14, 0x4000); - - if (fwrite(buf, 1, read_size, f_dec) != read_size) { - fprintf(stderr, "Failed to write file!\n"); - exit(EXIT_FAILURE); - } - ofs += read_size; - } - - free(buf); - } else { - fprintf(stderr, "Failed to open %s!\n", dec_path->char_path); - } + if (dec_path->valid != VALIDITY_VALID) { + return } + + printf("Saving Decrypted NAX0 Content to %s...\n", dec_path->char_path); + FILE *f_dec = os_fopen(dec_path->os_path, OS_MODE_WRITE); + + if (f_dec == NULL) { + fprintf(stderr, "Failed to open %s!\n", dec_path->char_path); + return; + } + + uint64_t ofs = 0x4000; + uint64_t end_ofs = ofs + ctx->header.size; + unsigned char *buf = malloc(0x400000); + if (buf == NULL) { + fprintf(stderr, "Failed to allocate file-save buffer!\n"); + exit(EXIT_FAILURE); + } + + uint64_t read_size = 0x400000; /* 4 MB buffer. */ + memset(buf, 0xCC, read_size); /* Debug in case I fuck this up somehow... */ + while (ofs < end_ofs) { + if (ofs + read_size >= end_ofs) read_size = end_ofs - ofs; + if (nax0_read(ctx, ofs, buf, read_size) != read_size) { + fprintf(stderr, "Failed to read file!\n"); + exit(EXIT_FAILURE); + } + + uint64_t dec_size = (read_size + 0x3FFF) & ~0x3FFF; + aes_xts_decrypt(ctx->aes_ctx, buf, buf, dec_size, (ofs - 0x4000) >> 14, 0x4000); + + if (fwrite(buf, 1, read_size, f_dec) != read_size) { + fprintf(stderr, "Failed to write file!\n"); + exit(EXIT_FAILURE); + } + ofs += read_size; + } + + free(buf); } const char *nax0_get_key_summary(unsigned int k) {