Skip to content

Commit 9f36ff9

Browse files
committed
Accept non local directories
1 parent bb02703 commit 9f36ff9

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

cdecrypt.c

+18-20
Original file line numberDiff line numberDiff line change
@@ -400,21 +400,19 @@ int main_utf8(int argc, char** argv)
400400
}
401401
}
402402

403-
// File too small or without expected magic
404-
if ((tmd_path == NULL) || (tik_path == NULL)) {
405-
argv[1][get_trailing_slash(argv[1])] = 0;
406-
if (argv[1][0] == 0) {
407-
argv[1][0] = '.';
408-
argv[1][1] = 0;
409-
}
403+
// We'll need the current path for locating files, which we set in argv[1]
404+
argv[1][get_trailing_slash(argv[1])] = 0;
405+
if (argv[1][0] == 0) {
406+
argv[1][0] = '.';
407+
argv[1][1] = 0;
410408
}
411409
}
412410

413411
// If the condition below is true, argv[1] is a directory
414412
if ((tmd_path == NULL) || (tik_path == NULL)) {
415413
size_t size = strlen(argv[1]);
416-
tmd_path = calloc(size + 10, 1);
417-
tik_path = calloc(size + 10, 1);
414+
tmd_path = calloc(size + 16, 1);
415+
tik_path = calloc(size + 16, 1);
418416
sprintf(tmd_path, "%s%ctitle.tmd", argv[1], PATH_SEP);
419417
sprintf(tik_path, "%s%ctitle.tik", argv[1], PATH_SEP);
420418
}
@@ -455,11 +453,11 @@ int main_utf8(int argc, char** argv)
455453
uint8_t iv[16];
456454
memset(iv, 0, sizeof(iv));
457455

458-
sprintf(str, "%08X.app", getbe32(&tmd->Contents[0].ID));
456+
sprintf(str, "%s%c%08X.app", argv[1], PATH_SEP, getbe32(&tmd->Contents[0].ID));
459457

460458
uint32_t cnt_len = read_file(str, &cnt);
461459
if (cnt_len == 0) {
462-
sprintf(str, "%08X", getbe32(&tmd->Contents[0].ID));
460+
sprintf(str, "%s%c%08X", argv[1], PATH_SEP, getbe32(&tmd->Contents[0].ID));
463461
cnt_len = read_file(str, &cnt);
464462
if (cnt_len == 0) {
465463
fprintf(stderr, "ERROR: Failed to open content: %02X\n", getbe32(&tmd->Contents[0].ID));
@@ -476,7 +474,7 @@ int main_utf8(int argc, char** argv)
476474
aes_crypt_cbc(&ctx, AES_DECRYPT, cnt_len, iv, cnt, cnt);
477475

478476
if (getbe32(cnt) != FST_MAGIC) {
479-
sprintf(str, "%08X.dec", getbe32(&tmd->Contents[0].ID));
477+
sprintf(str, "%s%c%08X.dec", argv[1], PATH_SEP, getbe32(&tmd->Contents[0].ID));
480478
fprintf(stderr, "ERROR: Unexpected content magic. Dumping decrypted file as '%s'.\n", str);
481479
file_dump(str, cnt, cnt_len);
482480
goto out;
@@ -497,7 +495,7 @@ int main_utf8(int argc, char** argv)
497495

498496
printf("FST entries: %u\n", entries);
499497

500-
char path[1024] = { 0 };
498+
char path[MAX_PATH] = { 0 };
501499
uint32_t entry[16];
502500
uint32_t l_entry[16];
503501

@@ -519,16 +517,16 @@ int main_utf8(int argc, char** argv)
519517
} else {
520518
uint32_t offset;
521519
memset(path, 0, sizeof(path));
520+
strcpy(path, argv[1]);
522521

522+
size_t short_path = strlen(path) + 1;
523523
for (uint32_t j = 0; j < level; j++) {
524-
if (j > 0)
525-
path[strlen(path)] = PATH_SEP;
524+
path[strlen(path)] = PATH_SEP;
526525
offset = getbe32(&fe[entry[j]].TypeName) & 0x00FFFFFF;
527526
memcpy(path + strlen(path), cnt + name_offset + offset, strlen((char*)cnt + name_offset + offset));
528527
create_path(path);
529528
}
530-
if (level > 0)
531-
path[strlen(path)] = PATH_SEP;
529+
path[strlen(path)] = PATH_SEP;
532530
offset = getbe32(&fe[i].TypeName) & 0x00FFFFFF;
533531
memcpy(path + strlen(path), cnt + name_offset + offset, strlen((char*)cnt + name_offset + offset));
534532

@@ -537,15 +535,15 @@ int main_utf8(int argc, char** argv)
537535
cnt_offset <<= 5;
538536

539537
printf("Size:%07X Offset:0x%010" PRIx64 " CID:%02X U:%02X %s\n", getbe32(&fe[i].FileLength),
540-
cnt_offset, getbe16(&fe[i].ContentID), getbe16(&fe[i].Flags), path);
538+
cnt_offset, getbe16(&fe[i].ContentID), getbe16(&fe[i].Flags), &path[short_path]);
541539

542540
uint32_t cnt_file_id = getbe32(&tmd->Contents[getbe16(&fe[i].ContentID)].ID);
543-
sprintf(str, "%08X.app", cnt_file_id);
541+
sprintf(str, "%s%c%08X.app", argv[1], PATH_SEP, cnt_file_id);
544542

545543
if (!(fe[i].Type & 0x80)) {
546544
src = fopen_utf8(str, "rb");
547545
if (src == NULL) {
548-
sprintf(str, "%08X", cnt_file_id);
546+
sprintf(str, "%s%c%08X", argv[1], PATH_SEP, cnt_file_id);
549547
src = fopen_utf8(str, "rb");
550548
if (src == NULL) {
551549
fprintf(stderr, "ERROR: Could not open: '%s'\n", str);

0 commit comments

Comments
 (0)