-
Notifications
You must be signed in to change notification settings - Fork 51
/
romfs.c
145 lines (130 loc) · 4.92 KB
/
romfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <string.h>
#include "romfs.h"
#include "nacp.h"
static void romfs_visit_file(romfs_ctx_t *ctx, uint32_t file_offset, filepath_t *dir_path, nsp_ctx_t *nsp_ctx)
{
romfs_fentry_t *entry = romfs_get_fentry(ctx->files, file_offset);
filepath_t *cur_path = calloc(1, sizeof(filepath_t));
if (cur_path == NULL)
{
fprintf(stderr, "Failed to allocate filepath!\n");
exit(EXIT_FAILURE);
}
filepath_copy(cur_path, dir_path);
if (entry->name_size)
{
filepath_append_n(cur_path, entry->name_size, "%s", entry->name);
}
if ((strcmp(cur_path->char_path, "/control.nacp") == 0) || (strcmp(cur_path->char_path, "\\control.nacp") == 0))
{
// Read control.nacp
uint64_t current_pos = ftello64(ctx->file);
nacp_t nacp;
memset(&nacp, 0, sizeof(nacp_t));
fseeko64(ctx->file, ctx->romfs_offset + ctx->header.data_offset + entry->offset, SEEK_SET);
if (fread(&nacp, 1, sizeof(nacp_t), ctx->file) != sizeof(nacp_t))
{
fprintf(stderr, "Failed to read control.nacp from RomFS!\n");
exit(EXIT_FAILURE);
}
// Fill nsp_ctx titlename and version
char *forbidden_chars[10] = {"/", "\\", "?", "%%", "*", ":", "|", "\"", ">", "<"};
for (int i = 0; i < 16; i++)
{
if (nacp.Title[i].Name[0] != 0x00)
{
for (unsigned int j = 0; j < strlen(nacp.Title[i].Name); j++)
{
int found_forbidden_char = 0;
for (int k = 0; k < 10; k++)
{
if (strncmp(&nacp.Title[i].Name[j], forbidden_chars[k], 1) == 0)
found_forbidden_char = 1;
}
if (found_forbidden_char == 0)
strncat(nsp_ctx->title_name, &nacp.Title[i].Name[j], 1);
}
break;
}
}
strcpy(nsp_ctx->title_display_version, nacp.DisplayVersion);
fseeko64(ctx->file, current_pos, SEEK_SET);
return;
}
free(cur_path);
if (entry->sibling != ROMFS_ENTRY_EMPTY)
{
romfs_visit_file(ctx, entry->sibling, dir_path, nsp_ctx);
}
}
static void romfs_visit_dir(romfs_ctx_t *ctx, uint32_t dir_offset, filepath_t *parent_path, nsp_ctx_t *nsp_ctx)
{
romfs_direntry_t *entry = romfs_get_direntry(ctx->directories, dir_offset);
filepath_t *cur_path = calloc(1, sizeof(filepath_t));
if (cur_path == NULL)
{
fprintf(stderr, "Failed to allocate filepath!\n");
exit(EXIT_FAILURE);
}
filepath_copy(cur_path, parent_path);
if (entry->name_size)
{
filepath_append_n(cur_path, entry->name_size, "%s", entry->name);
}
if (entry->file != ROMFS_ENTRY_EMPTY)
{
romfs_visit_file(ctx, entry->file, cur_path, nsp_ctx);
}
if (entry->child != ROMFS_ENTRY_EMPTY)
{
romfs_visit_dir(ctx, entry->child, cur_path, nsp_ctx);
}
if (entry->sibling != ROMFS_ENTRY_EMPTY)
{
romfs_visit_dir(ctx, entry->sibling, parent_path, nsp_ctx);
}
free(cur_path);
}
void romfs_process(romfs_ctx_t *ctx, nsp_ctx_t *nsp_ctx)
{
ctx->romfs_offset = 0;
fseeko64(ctx->file, ctx->romfs_offset, SEEK_SET);
if (fread(&ctx->header, 1, sizeof(romfs_hdr_t), ctx->file) != sizeof(romfs_hdr_t))
{
fprintf(stderr, "Failed to read RomFS header!\n");
return;
}
if (ctx->header.header_size == ROMFS_HEADER_SIZE)
{
/* Pre-load the file/data entry caches. */
ctx->directories = calloc(1, ctx->header.dir_meta_table_size);
if (ctx->directories == NULL)
{
fprintf(stderr, "Failed to allocate RomFS directory cache!\n");
exit(EXIT_FAILURE);
}
fseeko64(ctx->file, ctx->romfs_offset + ctx->header.dir_meta_table_offset, SEEK_SET);
if (fread(ctx->directories, 1, ctx->header.dir_meta_table_size, ctx->file) != ctx->header.dir_meta_table_size)
{
fprintf(stderr, "Failed to read RomFS directory cache!\n");
exit(EXIT_FAILURE);
}
ctx->files = calloc(1, ctx->header.file_meta_table_size);
if (ctx->files == NULL)
{
fprintf(stderr, "Failed to allocate RomFS file cache!\n");
exit(EXIT_FAILURE);
}
fseeko64(ctx->file, ctx->romfs_offset + ctx->header.file_meta_table_offset, SEEK_SET);
if (fread(ctx->files, 1, ctx->header.file_meta_table_size, ctx->file) != ctx->header.file_meta_table_size)
{
fprintf(stderr, "Failed to read RomFS file cache!\n");
exit(EXIT_FAILURE);
}
}
filepath_t fakepath;
filepath_init(&fakepath);
filepath_set(&fakepath, "");
romfs_visit_dir(ctx, 0, &fakepath, nsp_ctx);
}