Skip to content

Commit

Permalink
nax0: Invert conditionals within nax0_save()
Browse files Browse the repository at this point in the history
Allows unindenting most of the code within the function to be nicer to
follow.
  • Loading branch information
lioncash committed Aug 12, 2018
1 parent a97bdb8 commit 17ada97
Showing 1 changed file with 36 additions and 33 deletions.
69 changes: 36 additions & 33 deletions nax0.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 (dec_path->valid != VALIDITY_VALID) {
return
}

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);
}
printf("Saving Decrypted NAX0 Content to %s...\n", dec_path->char_path);
FILE *f_dec = os_fopen(dec_path->os_path, OS_MODE_WRITE);

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 (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) {
Expand Down

0 comments on commit 17ada97

Please sign in to comment.