mirror of
https://github.com/SciresM/hactool
synced 2024-11-22 20:13:08 +00:00
nax0: Invert conditionals within nax0_save()
Allows unindenting most of the code within the function to be nicer to follow.
This commit is contained in:
parent
a97bdb8a24
commit
17ada97b05
1 changed files with 39 additions and 36 deletions
75
nax0.c
75
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) {
|
||||
|
|
Loading…
Reference in a new issue