mirror of
https://github.com/DarkStore-3DS/Project_CTR.git
synced 2026-07-03 00:39:14 +00:00
[makerom] fixed romfs (for linux)
This commit is contained in:
+136
-235
@@ -2,35 +2,36 @@
|
|||||||
#include "dir.h"
|
#include "dir.h"
|
||||||
#include "utf.h"
|
#include "utf.h"
|
||||||
|
|
||||||
const fs_romfs_char FS_CURRENT_DIR_PATH = 0x2E;
|
|
||||||
const fs_romfs_char FS_PARENT_DIR_PATH[2] = { 0x2E,0x2E };
|
|
||||||
|
|
||||||
/* This is the FS interface for ROMFS generation */
|
/* This is the FS interface for ROMFS generation */
|
||||||
/* Tested working on Windows/Linux/OSX */
|
/* Tested working on Windows/Linux/OSX */
|
||||||
int fs_InitDir(const fs_entry *entry, fs_dir *dir);
|
int OpenDir(romfs_dir *dir);
|
||||||
int fs_ManageDirSlot(fs_dir *dir);
|
int InitDir(romfs_dir *dir);
|
||||||
int fs_ManageFileSlot(fs_dir *dir);
|
int ManageDir(romfs_dir *dir);
|
||||||
void fs_chdirUp(void);
|
|
||||||
fs_entry* fs_GetEntry(const fs_char *parent_path, fs_DIR *dp);
|
|
||||||
void fs_FreeEntry(fs_entry *entry);
|
|
||||||
bool fs_EntryIsDirNav(fs_entry *entry);
|
|
||||||
int fs_AddDir(fs_entry *entry, fs_dir *dir);
|
|
||||||
int fs_AddFile(fs_entry *entry, fs_dir *dir);
|
|
||||||
|
|
||||||
u32 fs_romfs_strlen(const fs_romfs_char *str)
|
|
||||||
{
|
|
||||||
u32 i;
|
|
||||||
for( i = 0; str[i] != 0x0; i++ );
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 fs_strlen(const fs_char *str)
|
u32 fs_strlen(const fs_char *str)
|
||||||
{
|
{
|
||||||
u32 i;
|
#ifdef _WIN32
|
||||||
for (i = 0; str[i] != 0x0; i++);
|
return strlen_char16(str);
|
||||||
return i;
|
#else
|
||||||
|
return strlen(str);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u32 romfs_strlen(const romfs_char *str)
|
||||||
|
{
|
||||||
|
return strlen_char16(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
int fs_strcmp(const fs_char *str1, const fs_char *str2)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
return wcscmp(str1, str2);
|
||||||
|
#else
|
||||||
|
return strcmp(str1, str2);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
FILE* fs_fopen(fs_char *path)
|
FILE* fs_fopen(fs_char *path)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@@ -67,264 +68,164 @@ fs_char* fs_AppendToPath(const fs_char *src, const fs_char *add)
|
|||||||
return new_path;
|
return new_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs_char* fs_CopyPath(const fs_char *src)
|
fs_char* fs_CopyStr(const fs_char *src)
|
||||||
{
|
|
||||||
u32 src_len;
|
|
||||||
fs_char *new_path;
|
|
||||||
|
|
||||||
src_len = fs_strlen(src);
|
|
||||||
new_path = calloc(src_len + 0x10, sizeof(fs_char));
|
|
||||||
|
|
||||||
for (u32 i = 0; i < src_len; i++)
|
|
||||||
new_path[i] = src[i];
|
|
||||||
|
|
||||||
return new_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
fs_romfs_char* fs_CopyRomfsName(const fs_romfs_char *src)
|
|
||||||
{
|
|
||||||
u32 src_len;
|
|
||||||
fs_romfs_char *new_path;
|
|
||||||
|
|
||||||
src_len = fs_strlen(src);
|
|
||||||
new_path = calloc(src_len + 0x10, sizeof(fs_romfs_char));
|
|
||||||
|
|
||||||
for (u32 i = 0; i < src_len; i++)
|
|
||||||
new_path[i] = src[i];
|
|
||||||
|
|
||||||
return new_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fs_fputs(FILE *out, const fs_char *str)
|
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
wprintf(L"%s", str);
|
return strcopy_16to16(src);
|
||||||
#else
|
#else
|
||||||
printf("%s", str);
|
return strcopy_8to8(src);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void fs_romfs_fputs(FILE *out, const fs_romfs_char *str)
|
romfs_char* romfs_CopyConvertStr(const fs_char *src)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
wprintf(L"%s", str);
|
return strcopy_16to16(src);
|
||||||
|
#else
|
||||||
|
return strcopy_utf8to16(src);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
romfs_char* romfs_CopyStr(const romfs_char *src)
|
||||||
|
{
|
||||||
|
return strcopy_16to16(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
void fs_fputs(const fs_char *str, FILE *out)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
fwprintf(out,L"%s", str);
|
||||||
|
#else
|
||||||
|
fprintf(out,"%s", str);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void romfs_fputs(const romfs_char *str, FILE *out)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
fwprintf(out,L"%s", str);
|
||||||
#else
|
#else
|
||||||
const char *name = (const char*)str;
|
const char *name = (const char*)str;
|
||||||
for (u32 i = 0; i < fs_romfs_strlen(str)*2; i += 2)
|
for (u32 i = 0; i < romfs_strlen(str)*2; i += 2)
|
||||||
putchar(name[i]);
|
fputc(name[i],out);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int fs_InitDir(const fs_entry *entry, fs_dir *dir)
|
int InitDir(romfs_dir *dir)
|
||||||
{
|
{
|
||||||
dir->fs_path = fs_CopyPath(entry->fs_path);
|
|
||||||
|
|
||||||
dir->name_len = entry->name_len;
|
|
||||||
dir->name = fs_CopyRomfsName(entry->name);
|
|
||||||
|
|
||||||
dir->m_child = 10;
|
dir->m_child = 10;
|
||||||
dir->u_child = 0;
|
dir->u_child = 0;
|
||||||
dir->child = calloc(dir->m_child,sizeof(fs_dir));
|
dir->child = calloc(dir->m_child,sizeof(romfs_dir));
|
||||||
|
|
||||||
dir->m_file = 10;
|
dir->m_file = 10;
|
||||||
dir->u_file = 0;
|
dir->u_file = 0;
|
||||||
dir->file = calloc(dir->m_file,sizeof(fs_file));
|
dir->file = calloc(dir->m_file,sizeof(romfs_file));
|
||||||
|
|
||||||
|
if (dir->child == NULL || dir->file == NULL)
|
||||||
|
return MEM_ERROR;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fs_ManageDirSlot(fs_dir *dir)
|
int ManageDir(romfs_dir *dir)
|
||||||
{
|
{
|
||||||
if(dir->u_child >= dir->m_child)
|
if (dir->u_child >= dir->m_child) {
|
||||||
{
|
dir->m_child = 2 * dir->u_child;
|
||||||
dir->m_child *= 2;
|
dir->child = realloc(dir->child, dir->m_child*sizeof(romfs_dir));
|
||||||
fs_dir *tmp = calloc(dir->m_child,sizeof(fs_dir));
|
|
||||||
memcpy(tmp,dir->child,sizeof(fs_dir)*dir->u_child);
|
|
||||||
free(dir->child);
|
|
||||||
dir->child = tmp;
|
|
||||||
}
|
}
|
||||||
return 0;
|
if (dir->u_file >= dir->m_file) {
|
||||||
}
|
dir->m_file = 2 * dir->u_file;
|
||||||
|
dir->file = realloc(dir->file, dir->m_file*sizeof(romfs_file));
|
||||||
int fs_ManageFileSlot(fs_dir *dir)
|
|
||||||
{
|
|
||||||
if(dir->u_file >= dir->m_file)
|
|
||||||
{
|
|
||||||
dir->m_file *= 2;
|
|
||||||
fs_file *tmp = calloc(dir->m_file,sizeof(fs_file));
|
|
||||||
memcpy(tmp,dir->file,sizeof(fs_file)*dir->u_file);
|
|
||||||
free(dir->file);
|
|
||||||
dir->file = tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dir->child == NULL || dir->file == NULL)
|
||||||
|
return MEM_ERROR;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs_entry* fs_GetEntry(const fs_char *parent_path, fs_DIR *dp)
|
int OpenRootDir(const char *path, romfs_dir *dir)
|
||||||
{
|
{
|
||||||
// Directory structs
|
// Create native FS path
|
||||||
struct fs_dirent *tmp_entry;
|
|
||||||
fs_DIR *tmp_dptr;
|
|
||||||
u32 namlen = 0;
|
|
||||||
|
|
||||||
//printf("get api dir entry from dir ptr\n");
|
|
||||||
tmp_entry = fs_readdir(dp);
|
|
||||||
|
|
||||||
//printf("if null, return\n");
|
|
||||||
if(!tmp_entry)
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
namlen = tmp_entry->d_namlen;
|
dir->path = strcopy_8to16(path);
|
||||||
#else
|
#else
|
||||||
namlen = strlen(tmp_entry->d_name);
|
dir->path = strcopy_8to8(path);
|
||||||
#endif
|
#endif
|
||||||
|
// Copy romfs name (empty string)
|
||||||
//printf("allocate memory for entry\n");
|
dir->name = romfs_CopyStr(ROMFS_EMPTY_PATH);
|
||||||
fs_entry *entry = malloc(sizeof(fs_entry));
|
dir->namesize = 0;
|
||||||
memset(entry,0,sizeof(fs_entry));
|
|
||||||
|
|
||||||
//Copy FS compatible Entry name
|
return OpenDir(dir);
|
||||||
fs_char *fs_name = calloc(sizeof(fs_char)*(namlen+1),1);
|
}
|
||||||
memcpy(fs_name,tmp_entry->d_name,sizeof(fs_char)*namlen);
|
|
||||||
entry->fs_path = fs_AppendToPath(parent_path, fs_name);
|
int OpenDir(romfs_dir *dir)
|
||||||
|
{
|
||||||
// Convert Entry name into RomFS u16 char (windows wchar_t, thanks Nintendo)
|
fs_DIR *dp, *tmp_dp;
|
||||||
#if _WIN32
|
struct fs_dirent *entry;
|
||||||
str_u16_to_u16(&entry->name,&entry->name_len,tmp_entry->d_name,namlen);
|
|
||||||
#else
|
if (InitDir(dir))
|
||||||
str_utf8_to_u16(&entry->name,&entry->name_len,(u8*)tmp_entry->d_name,namlen);
|
return MEM_ERROR;
|
||||||
#endif
|
|
||||||
|
// Open Directory
|
||||||
//printf("get dir entry from dir ptr to check if dir\n");
|
if((dp = fs_opendir(dir->path)) == NULL)
|
||||||
tmp_dptr = fs_opendir(entry->fs_path);
|
|
||||||
if(tmp_dptr)
|
|
||||||
{
|
{
|
||||||
//printf("is dir\n");
|
printf("[ROMFS] Failed to open directory: \"");
|
||||||
fs_closedir(tmp_dptr);
|
fs_fputs(dir->path, stdout);
|
||||||
entry->IsDir = true;
|
printf("\"\n");
|
||||||
entry->size = 0;
|
|
||||||
}
|
|
||||||
else // Open file if it is a file
|
|
||||||
{
|
|
||||||
entry->IsDir = false;
|
|
||||||
entry->size = fs_fsize(entry->fs_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't bother returning current entry, if it is useless
|
|
||||||
if (fs_EntryIsDirNav(entry)) {
|
|
||||||
fs_FreeEntry(entry);
|
|
||||||
return fs_GetEntry(parent_path, dp);
|
|
||||||
}
|
|
||||||
|
|
||||||
return entry;
|
|
||||||
}
|
|
||||||
|
|
||||||
void fs_FreeEntry(fs_entry *entry)
|
|
||||||
{
|
|
||||||
free(entry->fs_path);
|
|
||||||
free(entry->name);
|
|
||||||
free(entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool fs_EntryIsDirNav(fs_entry *entry)
|
|
||||||
{
|
|
||||||
if(entry->name_len == sizeof(fs_romfs_char)*1 && memcmp(entry->name,&FS_CURRENT_DIR_PATH,sizeof(fs_romfs_char)*1) == 0)
|
|
||||||
return true;
|
|
||||||
if(entry->name_len == sizeof(fs_romfs_char)*2 && memcmp(entry->name,FS_PARENT_DIR_PATH,sizeof(fs_romfs_char)*2) == 0)
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int fs_AddDir(fs_entry *entry, fs_dir *dir)
|
|
||||||
{
|
|
||||||
fs_ManageDirSlot(dir);
|
|
||||||
u32 current_slot = dir->u_child;
|
|
||||||
|
|
||||||
dir->u_child++;
|
|
||||||
return fs_OpenDir(entry,&dir->child[current_slot]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int fs_AddFile(fs_entry *entry, fs_dir *dir)
|
|
||||||
{
|
|
||||||
fs_ManageFileSlot(dir);
|
|
||||||
dir->file[dir->u_file].fs_path = fs_CopyPath(entry->fs_path);
|
|
||||||
dir->file[dir->u_file].name_len = entry->name_len;
|
|
||||||
dir->file[dir->u_file].name = fs_CopyRomfsName(entry->name);
|
|
||||||
dir->file[dir->u_file].size = entry->size;
|
|
||||||
|
|
||||||
dir->u_file++;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fs_OpenRootDir(const char *path, fs_dir *dir)
|
|
||||||
{
|
|
||||||
fs_entry *root = calloc(1, sizeof(fs_entry));
|
|
||||||
u32 nul;
|
|
||||||
|
|
||||||
root->IsDir = true;
|
|
||||||
root->size = 0;
|
|
||||||
|
|
||||||
str_u16_to_u16(&root->name, &root->name_len, ROMFS_EMPTY_PATH, 0);
|
|
||||||
#ifdef _WIN32
|
|
||||||
str_u8_to_u16(&root->fs_path, &nul, (u8*)path, strlen(path));
|
|
||||||
#else
|
|
||||||
str_u8_to_u8(&root->fs_path, &nul, (u8*)path, strlen(path));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
int ret = fs_OpenDir(root, dir);
|
|
||||||
|
|
||||||
fs_FreeEntry(root);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fs_OpenDir(fs_entry *curr_dir_entry, fs_dir *dir)
|
|
||||||
{
|
|
||||||
//printf("init open dir\n");
|
|
||||||
int ret = 0;
|
|
||||||
fs_DIR *dp;
|
|
||||||
fs_entry *entry;
|
|
||||||
|
|
||||||
//printf("do some more init\n");
|
|
||||||
fs_InitDir(curr_dir_entry, dir);
|
|
||||||
//wprintf(L" rec: \"%s\" (%d)\n",dir->name,dir->name_len);
|
|
||||||
|
|
||||||
//printf("check if path exists\n");
|
|
||||||
dp = fs_opendir(dir->fs_path);
|
|
||||||
if(!dp)
|
|
||||||
{
|
|
||||||
wprintf(L"[!] Failed to open directory: \"%s\"\n",dir->fs_path);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//printf("read entries\n");
|
// Process Entries
|
||||||
while((entry = fs_GetEntry(dir->fs_path, dp)) != NULL)
|
while ((entry = fs_readdir(dp)) != NULL)
|
||||||
{
|
{
|
||||||
ret = entry->IsDir? fs_AddDir(entry, dir) : fs_AddFile(entry, dir);
|
// Skip if "." or ".."
|
||||||
|
if (fs_strcmp(entry->d_name, FS_CURRENT_DIR_PATH) == 0 || fs_strcmp(entry->d_name, FS_PARENT_DIR_PATH) == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
//printf("free entry\n");
|
// Ensures that there is always memory for child directory and file structs
|
||||||
fs_FreeEntry(entry);
|
if (ManageDir(dir))
|
||||||
|
return MEM_ERROR;
|
||||||
|
|
||||||
|
// Get native FS path
|
||||||
|
fs_char *path = fs_AppendToPath(dir->path, entry->d_name);
|
||||||
|
|
||||||
if(ret)
|
// Opening directory with fs path to test if directory
|
||||||
{
|
if ((tmp_dp = fs_opendir(path)) != NULL) {
|
||||||
//printf("error parsing entry\n");
|
fs_closedir(tmp_dp);
|
||||||
break;
|
|
||||||
|
dir->child[dir->u_child].path = path;
|
||||||
|
dir->child[dir->u_child].name = romfs_CopyConvertStr(entry->d_name);
|
||||||
|
dir->child[dir->u_child].namesize = fs_strlen(entry->d_name)*sizeof(romfs_char);
|
||||||
|
dir->u_child++;
|
||||||
|
|
||||||
|
// Populate directory
|
||||||
|
OpenDir(&dir->child[dir->u_child-1]);
|
||||||
|
}
|
||||||
|
// Otherwise this is a file
|
||||||
|
else {
|
||||||
|
dir->file[dir->u_file].path = path;
|
||||||
|
dir->file[dir->u_file].name = romfs_CopyConvertStr(entry->d_name);
|
||||||
|
dir->file[dir->u_file].namesize = fs_strlen(entry->d_name)*sizeof(romfs_char);
|
||||||
|
dir->file[dir->u_file].size = fs_fsize(path);
|
||||||
|
dir->u_file++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs_closedir(dp);
|
fs_closedir(dp);
|
||||||
return ret;
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void fs_PrintDir(fs_dir *dir, u32 depth) // This is just for simple debugging, please don't shoot me
|
void PrintDir(romfs_dir *dir, u32 depth)
|
||||||
{
|
{
|
||||||
for(u32 i = 0; i < depth; i++)
|
for(u32 i = 0; i < depth; i++)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
|
|
||||||
if (depth > 0)
|
if (depth > 0)
|
||||||
fs_romfs_fputs(stdout, dir->name);
|
romfs_fputs(dir->name, stdout);
|
||||||
else
|
else
|
||||||
printf("romfs:");
|
printf("romfs:");
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
@@ -335,23 +236,23 @@ void fs_PrintDir(fs_dir *dir, u32 depth) // This is just for simple debugging, p
|
|||||||
{
|
{
|
||||||
for(u32 j = 0; j < depth+1; j++)
|
for(u32 j = 0; j < depth+1; j++)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
fs_romfs_fputs(stdout, dir->file[i].name);
|
romfs_fputs(dir->file[i].name, stdout);
|
||||||
printf(" (0x%"PRIx64")\n", dir->file[i].size);
|
printf(" (0x%"PRIx64")\n", dir->file[i].size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(dir->u_child)
|
if(dir->u_child)
|
||||||
{
|
{
|
||||||
for(u32 i = 0; i < dir->u_child; i++)
|
for(u32 i = 0; i < dir->u_child; i++)
|
||||||
fs_PrintDir(&dir->child[i],depth+1);
|
PrintDir(&dir->child[i],depth+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void fs_FreeDir(fs_dir *dir)
|
void FreeDir(romfs_dir *dir)
|
||||||
{
|
{
|
||||||
//printf("DIR!! free file names\n");
|
//printf("DIR!! free file names\n");
|
||||||
for(u32 i = 0; i < dir->u_file; i++)
|
for(u32 i = 0; i < dir->u_file; i++)
|
||||||
{
|
{
|
||||||
free(dir->file[i].fs_path);
|
free(dir->file[i].path);
|
||||||
free(dir->file[i].name);
|
free(dir->file[i].name);
|
||||||
}
|
}
|
||||||
//printf("free file struct\n");
|
//printf("free file struct\n");
|
||||||
@@ -361,9 +262,9 @@ void fs_FreeDir(fs_dir *dir)
|
|||||||
//printf("free dir names and\n");
|
//printf("free dir names and\n");
|
||||||
for(u32 i = 0; i < dir->u_child; i++)
|
for(u32 i = 0; i < dir->u_child; i++)
|
||||||
{
|
{
|
||||||
free(dir->child[i].fs_path);
|
free(dir->child[i].path);
|
||||||
free(dir->child[i].name);
|
free(dir->child[i].name);
|
||||||
fs_FreeDir(&dir->child[i]);
|
FreeDir(&dir->child[i]);
|
||||||
}
|
}
|
||||||
//printf("free dir struct\n");
|
//printf("free dir struct\n");
|
||||||
free(dir->child);
|
free(dir->child);
|
||||||
|
|||||||
+26
-35
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define fs_romfs_char u16
|
#define romfs_char u16
|
||||||
#define fs_char wchar_t
|
#define fs_char wchar_t
|
||||||
#define fs_dirent _wdirent
|
#define fs_dirent _wdirent
|
||||||
#define fs_DIR _WDIR
|
#define fs_DIR _WDIR
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
#define fs_closedir _wclosedir
|
#define fs_closedir _wclosedir
|
||||||
#define FS_PATH_SEPARATOR '\\'
|
#define FS_PATH_SEPARATOR '\\'
|
||||||
#else
|
#else
|
||||||
#define fs_romfs_char u16
|
#define romfs_char u16
|
||||||
#define fs_char char
|
#define fs_char char
|
||||||
#define fs_dirent dirent
|
#define fs_dirent dirent
|
||||||
#define fs_DIR DIR
|
#define fs_DIR DIR
|
||||||
@@ -21,59 +21,50 @@
|
|||||||
#define fs_closedir closedir
|
#define fs_closedir closedir
|
||||||
#define FS_PATH_SEPARATOR '/'
|
#define FS_PATH_SEPARATOR '/'
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
struct fs_entry
|
struct romfs_file
|
||||||
{
|
{
|
||||||
bool IsDir;
|
fs_char *path;
|
||||||
fs_char *fs_path;
|
romfs_char *name;
|
||||||
fs_romfs_char *name;
|
u32 namesize;
|
||||||
u32 name_len;
|
|
||||||
u64 size;
|
u64 size;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct fs_file
|
struct romfs_dir
|
||||||
{
|
{
|
||||||
fs_char *fs_path;
|
fs_char *path;
|
||||||
fs_romfs_char *name;
|
romfs_char *name;
|
||||||
u32 name_len;
|
u32 namesize;
|
||||||
u64 size;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct fs_dir
|
|
||||||
{
|
|
||||||
fs_char *fs_path;
|
|
||||||
fs_romfs_char *name;
|
|
||||||
u32 name_len;
|
|
||||||
|
|
||||||
struct fs_dir *child;
|
struct romfs_dir *child;
|
||||||
u32 m_child;
|
u32 m_child;
|
||||||
u32 u_child;
|
u32 u_child;
|
||||||
|
|
||||||
struct fs_file *file;
|
struct romfs_file *file;
|
||||||
u32 m_file;
|
u32 m_file;
|
||||||
u32 u_file;
|
u32 u_file;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct fs_entry fs_entry;
|
typedef struct romfs_file romfs_file;
|
||||||
typedef struct fs_file fs_file;
|
typedef struct romfs_dir romfs_dir;
|
||||||
typedef struct fs_dir fs_dir;
|
|
||||||
|
|
||||||
static const fs_romfs_char ROMFS_EMPTY_PATH[2] = { 0 };
|
static const romfs_char ROMFS_EMPTY_PATH[2] = { 0 };
|
||||||
static const fs_char FS_EMPTY_PATH[2] = { 0 };
|
static const fs_char FS_EMPTY_PATH[2] = { 0 };
|
||||||
|
static const fs_char FS_CURRENT_DIR_PATH[2] = { '.' };
|
||||||
|
static const fs_char FS_PARENT_DIR_PATH[3] = { '.', '.' };
|
||||||
|
|
||||||
u32 fs_romfs_strlen(const fs_romfs_char *str);
|
u32 romfs_strlen(const romfs_char *str);
|
||||||
u32 fs_strlen(const fs_char *str);
|
u32 fs_strlen(const fs_char *str);
|
||||||
|
int fs_strcmp(const fs_char *str1, const fs_char *str2);
|
||||||
FILE* fs_fopen(fs_char *path);
|
FILE* fs_fopen(fs_char *path);
|
||||||
u64 fs_fsize(fs_char *path);
|
u64 fs_fsize(fs_char *path);
|
||||||
|
|
||||||
fs_char* fs_AppendToPath(const fs_char *src, const fs_char *add);
|
fs_char* fs_AppendToPath(const fs_char *src, const fs_char *add);
|
||||||
fs_char* fs_CopyPath(const fs_char *src);
|
fs_char* fs_CopyStr(const fs_char *src);
|
||||||
fs_romfs_char* fs_CopyRomfsName(const fs_romfs_char *src);
|
romfs_char* romfs_CopyStr(const romfs_char *src);
|
||||||
void fs_fputs(FILE *out, const fs_char *str);
|
void fs_fputs(const fs_char *str, FILE *out);
|
||||||
void fs_romfs_fputs(FILE *out, const fs_romfs_char *str);
|
void romfs_fputs(const romfs_char *str, FILE *out);
|
||||||
|
|
||||||
int fs_OpenRootDir(const char *path, fs_dir *dir);
|
int OpenRootDir(const char *path, romfs_dir *dir);
|
||||||
int fs_OpenDir(fs_entry *entry, fs_dir *dir);
|
void PrintDir(romfs_dir *dir, u32 depth);
|
||||||
void fs_PrintDir(fs_dir *dir, u32 depth);
|
void FreeDir(romfs_dir *dir);
|
||||||
void fs_FreeDir(fs_dir *dir);
|
|
||||||
@@ -66,9 +66,6 @@
|
|||||||
<ClInclude Include="ctr_utils.h">
|
<ClInclude Include="ctr_utils.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="dir.h">
|
|
||||||
<Filter>Header Files</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="elf.h">
|
<ClInclude Include="elf.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
@@ -339,6 +336,9 @@
|
|||||||
<ClInclude Include="code.h">
|
<ClInclude Include="code.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="dir.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="accessdesc.c">
|
<ClCompile Include="accessdesc.c">
|
||||||
@@ -362,9 +362,6 @@
|
|||||||
<ClCompile Include="ctr_utils.c">
|
<ClCompile Include="ctr_utils.c">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="dir.c">
|
|
||||||
<Filter>Source Files</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="elf.c">
|
<ClCompile Include="elf.c">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@@ -473,6 +470,9 @@
|
|||||||
<ClCompile Include="code.c">
|
<ClCompile Include="code.c">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="dir.c">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="Makefile">
|
<None Include="Makefile">
|
||||||
|
|||||||
+1
-1
@@ -47,7 +47,7 @@ int BuildRomFs(romfs_buildctx *ctx)
|
|||||||
void FreeRomFsCtx(romfs_buildctx *ctx)
|
void FreeRomFsCtx(romfs_buildctx *ctx)
|
||||||
{
|
{
|
||||||
if(ctx->fs){
|
if(ctx->fs){
|
||||||
fs_FreeDir(ctx->fs);
|
FreeDir(ctx->fs);
|
||||||
free(ctx->fs);
|
free(ctx->fs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+1
-1
@@ -86,7 +86,7 @@ typedef struct
|
|||||||
ivfc_hdr *ivfcHdr;
|
ivfc_hdr *ivfcHdr;
|
||||||
romfs_infoheader *romfsHdr;
|
romfs_infoheader *romfsHdr;
|
||||||
|
|
||||||
fs_dir *fs;
|
romfs_dir *fs;
|
||||||
|
|
||||||
u8 *dirHashTable;
|
u8 *dirHashTable;
|
||||||
u32 m_dirHashTable;
|
u32 m_dirHashTable;
|
||||||
|
|||||||
+48
-47
@@ -7,35 +7,36 @@ const int ROMFS_BLOCK_SIZE = 0x1000;
|
|||||||
const unsigned int ROMFS_UNUSED_ENTRY = 0xffffffff;
|
const unsigned int ROMFS_UNUSED_ENTRY = 0xffffffff;
|
||||||
|
|
||||||
// Build
|
// Build
|
||||||
bool IsFileWanted(fs_file *file, void *filter_criteria);
|
bool IsFileWanted(romfs_file *file, void *filter_criteria);
|
||||||
bool IsDirWanted(fs_dir *dir, void *filter_criteria);
|
bool IsDirWanted(romfs_dir *dir, void *filter_criteria);
|
||||||
void CalcDirSize(romfs_buildctx *ctx, fs_dir *fs);
|
void CalcDirSize(romfs_buildctx *ctx, romfs_dir *fs);
|
||||||
void CalcRomfsSize(romfs_buildctx *ctx);
|
void CalcRomfsSize(romfs_buildctx *ctx);
|
||||||
int FilterRomFS(fs_dir *fs_raw, fs_dir *fs_filtered, void *filter_criteria);
|
int FilterRomFS(romfs_dir *fs_raw, romfs_dir *fs_filtered, void *filter_criteria);
|
||||||
void AddFileToRomfs(romfs_buildctx *ctx, fs_file *file, u32 parent, u32 sibling);
|
void AddFileToRomfs(romfs_buildctx *ctx, romfs_file *file, u32 parent, u32 sibling);
|
||||||
void AddDirToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 sibling);
|
void AddDirToRomfs(romfs_buildctx *ctx, romfs_dir *fs, u32 parent, u32 sibling);
|
||||||
void AddDirChildrenToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 dir);
|
void AddDirChildrenToRomfs(romfs_buildctx *ctx, romfs_dir *fs, u32 parent, u32 dir);
|
||||||
void PopulateHashTable(romfs_buildctx *ctx);
|
void PopulateHashTable(romfs_buildctx *ctx);
|
||||||
void PopulateRomfs(romfs_buildctx *ctx);
|
void PopulateRomfs(romfs_buildctx *ctx);
|
||||||
void BuildRomfsHeader(romfs_buildctx *ctx);
|
void BuildRomfsHeader(romfs_buildctx *ctx);
|
||||||
void BuildIvfcHeader(romfs_buildctx *ctx);
|
void BuildIvfcHeader(romfs_buildctx *ctx);
|
||||||
void GenIvfcHashTree(romfs_buildctx *ctx);
|
void GenIvfcHashTree(romfs_buildctx *ctx);
|
||||||
u32 CalcPathHash(u32 parent, const fs_romfs_char* path, u32 start, u32 length);
|
u32 CalcPathHash(u32 parent, const romfs_char* path, u32 start, u32 length);
|
||||||
|
|
||||||
|
|
||||||
int PrepareBuildRomFsBinary(ncch_settings *ncchset, romfs_buildctx *ctx)
|
int PrepareBuildRomFsBinary(ncch_settings *ncchset, romfs_buildctx *ctx)
|
||||||
{
|
{
|
||||||
/* FS Structures */
|
/* FS Structures */
|
||||||
void *filter_criteria = NULL;
|
void *filter_criteria = NULL;
|
||||||
fs_dir *fs_raw = calloc(1,sizeof(fs_dir));
|
romfs_dir *fs_raw = calloc(1,sizeof(romfs_dir));
|
||||||
ctx->fs = calloc(1,sizeof(fs_dir));
|
ctx->fs = calloc(1,sizeof(romfs_dir));
|
||||||
|
|
||||||
/* Import FS and process */
|
/* Import FS and process */
|
||||||
fs_OpenRootDir(ncchset->rsfSet->RomFs.RootPath,fs_raw);
|
OpenRootDir(ncchset->rsfSet->RomFs.RootPath,fs_raw);
|
||||||
FilterRomFS(fs_raw,ctx->fs,filter_criteria);
|
FilterRomFS(fs_raw,ctx->fs,filter_criteria);
|
||||||
|
|
||||||
|
|
||||||
/* free unfiltered FS */
|
/* free unfiltered FS */
|
||||||
fs_FreeDir(fs_raw);
|
FreeDir(fs_raw);
|
||||||
free(fs_raw);
|
free(fs_raw);
|
||||||
|
|
||||||
/* Abort romfs making, if no wanted files/directories were found */
|
/* Abort romfs making, if no wanted files/directories were found */
|
||||||
@@ -94,12 +95,12 @@ int BuildRomFsBinary(romfs_buildctx *ctx)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool IsFileWanted(fs_file *file, void *filter_criteria)
|
bool IsFileWanted(romfs_file *file, void *filter_criteria)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsDirWanted(fs_dir *dir, void *filter_criteria)
|
bool IsDirWanted(romfs_dir *dir, void *filter_criteria)
|
||||||
{
|
{
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
for(u32 i = 0; i < dir->u_file; i++)
|
for(u32 i = 0; i < dir->u_file; i++)
|
||||||
@@ -121,16 +122,16 @@ bool IsDirWanted(fs_dir *dir, void *filter_criteria)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CalcDirSize(romfs_buildctx *ctx, fs_dir *fs)
|
void CalcDirSize(romfs_buildctx *ctx, romfs_dir *fs)
|
||||||
{
|
{
|
||||||
if(ctx->m_dirTableLen == 0)
|
if(ctx->m_dirTableLen == 0)
|
||||||
ctx->m_dirTableLen = sizeof(romfs_direntry);
|
ctx->m_dirTableLen = sizeof(romfs_direntry);
|
||||||
else
|
else
|
||||||
ctx->m_dirTableLen += sizeof(romfs_direntry) + align(fs->name_len,4);
|
ctx->m_dirTableLen += sizeof(romfs_direntry) + align(fs->namesize,4);
|
||||||
|
|
||||||
for(u32 i = 0; i < fs->u_file; i++)
|
for(u32 i = 0; i < fs->u_file; i++)
|
||||||
{
|
{
|
||||||
ctx->m_fileTableLen += sizeof(romfs_fileentry) + align(fs->file[i].name_len,4);
|
ctx->m_fileTableLen += sizeof(romfs_fileentry) + align(fs->file[i].namesize,4);
|
||||||
if(fs->file[i].size)
|
if(fs->file[i].size)
|
||||||
ctx->m_dataLen = align(ctx->m_dataLen,0x10) + fs->file[i].size;
|
ctx->m_dataLen = align(ctx->m_dataLen,0x10) + fs->file[i].size;
|
||||||
}
|
}
|
||||||
@@ -180,24 +181,24 @@ void CalcRomfsSize(romfs_buildctx *ctx)
|
|||||||
ctx->romfsSize += align(ctx->level[i].size,ROMFS_BLOCK_SIZE);
|
ctx->romfsSize += align(ctx->level[i].size,ROMFS_BLOCK_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
int FilterRomFS(fs_dir *fs_raw, fs_dir *fs_filtered, void *filter_criteria)
|
int FilterRomFS(romfs_dir *fs_raw, romfs_dir *fs_filtered, void *filter_criteria)
|
||||||
{
|
{
|
||||||
memset(fs_filtered,0,sizeof(fs_dir));
|
memset(fs_filtered,0,sizeof(romfs_dir));
|
||||||
if(!IsDirWanted(fs_raw,filter_criteria))
|
if(!IsDirWanted(fs_raw,filter_criteria))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fs_filtered->fs_path = fs_CopyPath(fs_raw->fs_path);
|
fs_filtered->path = fs_CopyStr(fs_raw->path);
|
||||||
|
|
||||||
fs_filtered->name_len = fs_raw->name_len;
|
fs_filtered->namesize = fs_raw->namesize;
|
||||||
fs_filtered->name = fs_CopyRomfsName(fs_raw->name);
|
fs_filtered->name = romfs_CopyStr(fs_raw->name);
|
||||||
|
|
||||||
fs_filtered->u_child = 0;
|
fs_filtered->u_child = 0;
|
||||||
fs_filtered->m_child = fs_raw->u_child;
|
fs_filtered->m_child = fs_raw->u_child;
|
||||||
fs_filtered->child = calloc(fs_filtered->m_child,sizeof(fs_dir));
|
fs_filtered->child = calloc(fs_filtered->m_child,sizeof(romfs_dir));
|
||||||
|
|
||||||
fs_filtered->u_file = 0;
|
fs_filtered->u_file = 0;
|
||||||
fs_filtered->m_file = fs_raw->u_file;
|
fs_filtered->m_file = fs_raw->u_file;
|
||||||
fs_filtered->file = calloc(fs_filtered->m_file,sizeof(fs_file));
|
fs_filtered->file = calloc(fs_filtered->m_file,sizeof(romfs_file));
|
||||||
|
|
||||||
for(u32 i = 0; i < fs_raw->u_child; i++)
|
for(u32 i = 0; i < fs_raw->u_child; i++)
|
||||||
{
|
{
|
||||||
@@ -212,10 +213,10 @@ int FilterRomFS(fs_dir *fs_raw, fs_dir *fs_filtered, void *filter_criteria)
|
|||||||
{
|
{
|
||||||
if(IsFileWanted(&fs_raw->file[i],filter_criteria))
|
if(IsFileWanted(&fs_raw->file[i],filter_criteria))
|
||||||
{
|
{
|
||||||
fs_filtered->file[fs_filtered->u_file].fs_path = fs_CopyPath(fs_raw->file[i].fs_path);
|
fs_filtered->file[fs_filtered->u_file].path = fs_CopyStr(fs_raw->file[i].path);
|
||||||
|
|
||||||
fs_filtered->file[fs_filtered->u_file].name_len = fs_raw->file[i].name_len;
|
fs_filtered->file[fs_filtered->u_file].namesize = fs_raw->file[i].namesize;
|
||||||
fs_filtered->file[fs_filtered->u_file].name = fs_CopyRomfsName(fs_raw->file[i].name);
|
fs_filtered->file[fs_filtered->u_file].name = romfs_CopyStr(fs_raw->file[i].name);
|
||||||
|
|
||||||
fs_filtered->file[fs_filtered->u_file].size = fs_raw->file[i].size;
|
fs_filtered->file[fs_filtered->u_file].size = fs_raw->file[i].size;
|
||||||
|
|
||||||
@@ -277,19 +278,19 @@ void BuildRomfsHeader(romfs_buildctx *ctx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 GetFileHashTableIndex(romfs_buildctx *ctx, u32 parent, const fs_romfs_char *path)
|
u32 GetFileHashTableIndex(romfs_buildctx *ctx, u32 parent, const romfs_char *path)
|
||||||
{
|
{
|
||||||
u32 hash = CalcPathHash(parent, path, 0, fs_romfs_strlen(path));
|
u32 hash = CalcPathHash(parent, path, 0, romfs_strlen(path));
|
||||||
return hash % ctx->m_fileHashTable;
|
return hash % ctx->m_fileHashTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 GetDirHashTableIndex(romfs_buildctx *ctx, u32 parent, const fs_romfs_char* path)
|
u32 GetDirHashTableIndex(romfs_buildctx *ctx, u32 parent, const romfs_char* path)
|
||||||
{
|
{
|
||||||
u32 hash = CalcPathHash(parent, path, 0, fs_romfs_strlen(path));
|
u32 hash = CalcPathHash(parent, path, 0, romfs_strlen(path));
|
||||||
return hash % ctx->m_dirHashTable;
|
return hash % ctx->m_dirHashTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddFileToRomfs(romfs_buildctx *ctx, fs_file *file, u32 parent, u32 sibling)
|
void AddFileToRomfs(romfs_buildctx *ctx, romfs_file *file, u32 parent, u32 sibling)
|
||||||
{
|
{
|
||||||
romfs_fileentry *entry = (romfs_fileentry*)(ctx->fileTable + ctx->u_fileTableLen);
|
romfs_fileentry *entry = (romfs_fileentry*)(ctx->fileTable + ctx->u_fileTableLen);
|
||||||
|
|
||||||
@@ -297,10 +298,10 @@ void AddFileToRomfs(romfs_buildctx *ctx, fs_file *file, u32 parent, u32 sibling)
|
|||||||
u32_to_u8(entry->siblingoffset,sibling,LE);
|
u32_to_u8(entry->siblingoffset,sibling,LE);
|
||||||
|
|
||||||
/* Import name */
|
/* Import name */
|
||||||
u32_to_u8(entry->namesize,file->name_len,LE);
|
u32_to_u8(entry->namesize,file->namesize,LE);
|
||||||
u8 *name_pos = (u8*)(ctx->fileTable + ctx->u_fileTableLen + sizeof(romfs_fileentry));
|
u8 *name_pos = (u8*)(ctx->fileTable + ctx->u_fileTableLen + sizeof(romfs_fileentry));
|
||||||
memset(name_pos,0,align(file->name_len,4));
|
memset(name_pos,0,align(file->namesize,4));
|
||||||
memcpy(name_pos,(u8*)file->name,file->name_len);
|
memcpy(name_pos,(u8*)file->name,file->namesize);
|
||||||
|
|
||||||
/* Set hash data */
|
/* Set hash data */
|
||||||
u32 hashindex = GetFileHashTableIndex(ctx, parent, file->name);
|
u32 hashindex = GetFileHashTableIndex(ctx, parent, file->name);
|
||||||
@@ -317,11 +318,11 @@ void AddFileToRomfs(romfs_buildctx *ctx, fs_file *file, u32 parent, u32 sibling)
|
|||||||
|
|
||||||
if (ctx->verbose) {
|
if (ctx->verbose) {
|
||||||
printf("[ROMFS] Reading \"");
|
printf("[ROMFS] Reading \"");
|
||||||
fs_fputs(stdout, file->fs_path);
|
fs_fputs(file->path, stdout);
|
||||||
printf("\"... ");
|
printf("\"... ");
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *fp = fs_fopen(file->fs_path);
|
FILE *fp = fs_fopen(file->path);
|
||||||
fread(data_pos, file->size, 1, fp);
|
fread(data_pos, file->size, 1, fp);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
@@ -335,10 +336,10 @@ void AddFileToRomfs(romfs_buildctx *ctx, fs_file *file, u32 parent, u32 sibling)
|
|||||||
u64_to_u8(entry->dataoffset,0x00,LE);
|
u64_to_u8(entry->dataoffset,0x00,LE);
|
||||||
|
|
||||||
/* Increment used file table length */
|
/* Increment used file table length */
|
||||||
ctx->u_fileTableLen += sizeof(romfs_fileentry) + align(file->name_len,4);
|
ctx->u_fileTableLen += sizeof(romfs_fileentry) + align(file->namesize,4);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddDirToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 sibling)
|
void AddDirToRomfs(romfs_buildctx *ctx, romfs_dir *fs, u32 parent, u32 sibling)
|
||||||
{
|
{
|
||||||
u32 offset = ctx->u_dirTableLen;
|
u32 offset = ctx->u_dirTableLen;
|
||||||
romfs_direntry *entry = (romfs_direntry*)(ctx->dirTable + offset);
|
romfs_direntry *entry = (romfs_direntry*)(ctx->dirTable + offset);
|
||||||
@@ -350,10 +351,10 @@ void AddDirToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 sibling)
|
|||||||
u32_to_u8(entry->fileoffset, ROMFS_UNUSED_ENTRY, LE);
|
u32_to_u8(entry->fileoffset, ROMFS_UNUSED_ENTRY, LE);
|
||||||
|
|
||||||
/* Import name */
|
/* Import name */
|
||||||
u32_to_u8(entry->namesize,fs->name_len,LE);
|
u32_to_u8(entry->namesize,fs->namesize,LE);
|
||||||
u8 *name_pos = (u8*)(ctx->dirTable + ctx->u_dirTableLen + sizeof(romfs_direntry));
|
u8 *name_pos = (u8*)(ctx->dirTable + ctx->u_dirTableLen + sizeof(romfs_direntry));
|
||||||
memset(name_pos,0,(u32)align(fs->name_len,4));
|
memset(name_pos,0,(u32)align(fs->namesize,4));
|
||||||
memcpy(name_pos,(u8*)fs->name,fs->name_len);
|
memcpy(name_pos,(u8*)fs->name,fs->namesize);
|
||||||
|
|
||||||
/* Set hash data */
|
/* Set hash data */
|
||||||
u32 hashindex = GetDirHashTableIndex(ctx, parent, fs->name);
|
u32 hashindex = GetDirHashTableIndex(ctx, parent, fs->name);
|
||||||
@@ -361,10 +362,10 @@ void AddDirToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 sibling)
|
|||||||
u32_to_u8(ctx->dirHashTable + hashindex * 4, offset, LE);
|
u32_to_u8(ctx->dirHashTable + hashindex * 4, offset, LE);
|
||||||
|
|
||||||
/* Increment used dir table length */
|
/* Increment used dir table length */
|
||||||
ctx->u_dirTableLen += sizeof(romfs_direntry) + (u32)align(fs->name_len,4);
|
ctx->u_dirTableLen += sizeof(romfs_direntry) + (u32)align(fs->namesize,4);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddDirChildrenToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 dir)
|
void AddDirChildrenToRomfs(romfs_buildctx *ctx, romfs_dir *fs, u32 parent, u32 dir)
|
||||||
{
|
{
|
||||||
romfs_direntry *entry = (romfs_direntry*)(ctx->dirTable + dir);
|
romfs_direntry *entry = (romfs_direntry*)(ctx->dirTable + dir);
|
||||||
|
|
||||||
@@ -380,7 +381,7 @@ void AddDirChildrenToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 dir)
|
|||||||
if (i >= fs->u_file - 1)
|
if (i >= fs->u_file - 1)
|
||||||
file_sibling = ROMFS_UNUSED_ENTRY;
|
file_sibling = ROMFS_UNUSED_ENTRY;
|
||||||
else
|
else
|
||||||
file_sibling = ctx->u_fileTableLen + sizeof(romfs_fileentry) + (u32)align(fs->file[i].name_len, 4);
|
file_sibling = ctx->u_fileTableLen + sizeof(romfs_fileentry) + (u32)align(fs->file[i].namesize, 4);
|
||||||
|
|
||||||
/* Create file entry */
|
/* Create file entry */
|
||||||
AddFileToRomfs(ctx, &fs->file[i], dir, file_sibling);
|
AddFileToRomfs(ctx, &fs->file[i], dir, file_sibling);
|
||||||
@@ -404,7 +405,7 @@ void AddDirChildrenToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 dir)
|
|||||||
if (i >= fs->u_child - 1)
|
if (i >= fs->u_child - 1)
|
||||||
dir_sibling = ROMFS_UNUSED_ENTRY;
|
dir_sibling = ROMFS_UNUSED_ENTRY;
|
||||||
else
|
else
|
||||||
dir_sibling = ctx->u_dirTableLen + sizeof(romfs_direntry) + (u32)align(fs->child[i].name_len, 4);
|
dir_sibling = ctx->u_dirTableLen + sizeof(romfs_direntry) + (u32)align(fs->child[i].namesize, 4);
|
||||||
|
|
||||||
/* Create child directory entry */
|
/* Create child directory entry */
|
||||||
AddDirToRomfs(ctx, &fs->child[i], dir, dir_sibling);
|
AddDirToRomfs(ctx, &fs->child[i], dir, dir_sibling);
|
||||||
@@ -459,7 +460,7 @@ void GenIvfcHashTree(romfs_buildctx *ctx)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 CalcPathHash(u32 parent, const fs_romfs_char* path, u32 start, u32 length)
|
u32 CalcPathHash(u32 parent, const romfs_char* path, u32 start, u32 length)
|
||||||
{
|
{
|
||||||
u32 hash = parent ^ 123456789;
|
u32 hash = parent ^ 123456789;
|
||||||
for( u32 index = 0; index < length; index++ )
|
for( u32 index = 0; index < length; index++ )
|
||||||
|
|||||||
+76
-45
@@ -68,8 +68,8 @@ char* replace_filextention(const char *input, const char *new_ext)
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
u32 size = ext - input;
|
u32 size = ext - input;
|
||||||
new_name = calloc(size + strlen(new_ext), 1);
|
new_name = calloc(size + strlen(new_ext) + 1, 1);
|
||||||
snprintf(new_name, size, "%s", input);
|
strncpy(new_name, input, size);
|
||||||
sprintf(new_name, "%s%s", new_name, new_ext);
|
sprintf(new_name, "%s%s", new_name, new_ext);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,61 +104,92 @@ void memdump(FILE* fout, const char* prefix, const u8* data, u32 size)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int str_u8_to_u16(u16 **dst, u32 *dst_len, const u8 *src, u32 src_len)
|
u32 strlen_char16(const u16 *str)
|
||||||
{
|
{
|
||||||
*dst_len = src_len*sizeof(u16);
|
u32 i;
|
||||||
*dst = malloc((*dst_len)+sizeof(u16));
|
for (i = 0; str[i] != 0x0; i++);
|
||||||
if(*dst == NULL)
|
return i;
|
||||||
return -1;
|
|
||||||
memset(*dst,0,(*dst_len)+sizeof(u16));
|
|
||||||
u16 *tmp = *dst;
|
|
||||||
for(int i=0; i<src_len; i++)
|
|
||||||
tmp[i] = (u16)src[i];
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int str_u16_to_u16(u16 **dst, u32 *dst_len, const u16 *src, u32 src_len)
|
char* strcopy_8to8(const char *src)
|
||||||
{
|
{
|
||||||
*dst_len = src_len*sizeof(u16);
|
if (!src)
|
||||||
*dst = malloc((*dst_len)+sizeof(u16));
|
return NULL;
|
||||||
if(*dst == NULL)
|
|
||||||
return -1;
|
u32 src_len = strlen(src);
|
||||||
memset(*dst,0,(*dst_len)+sizeof(u16));
|
|
||||||
u16 *tmp = *dst;
|
// Allocate memory for expanded string
|
||||||
for(int i=0; i<src_len; i++)
|
char *dst = calloc(src_len + 1, sizeof(u8));
|
||||||
tmp[i] = src[i];
|
if (!dst)
|
||||||
return 0;
|
return NULL;
|
||||||
|
|
||||||
|
// Copy elements from src into dst
|
||||||
|
strncpy(dst, src, src_len);
|
||||||
|
|
||||||
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
int str_u32_to_u16(u16 **dst, u32 *dst_len, const u32 *src, u32 src_len)
|
u16* strcopy_8to16(const char *src)
|
||||||
{
|
{
|
||||||
*dst_len = src_len*sizeof(u16);
|
if (!src)
|
||||||
*dst = malloc((*dst_len)+sizeof(u16));
|
return NULL;
|
||||||
if(*dst == NULL)
|
|
||||||
return -1;
|
u32 src_len = strlen(src);
|
||||||
memset(*dst,0,(*dst_len)+sizeof(u16));
|
|
||||||
u16 *tmp = *dst;
|
// Allocate memory for expanded string
|
||||||
for(int i=0; i<src_len; i++)
|
u16 *dst = calloc(src_len+1, sizeof(u16));
|
||||||
tmp[i] = (u16)src[i];
|
if (!dst)
|
||||||
return 0;
|
return NULL;
|
||||||
|
|
||||||
|
// Copy elements from src into dst
|
||||||
|
for (u32 i = 0; i < src_len; i++)
|
||||||
|
dst[i] = src[i];
|
||||||
|
|
||||||
|
return dst;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
u16* strcopy_16to16(const u16 *src)
|
||||||
|
{
|
||||||
|
if (!src)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
u32 src_len = strlen_char16(src);
|
||||||
|
|
||||||
|
// Allocate memory for expanded string
|
||||||
|
u16 *dst = calloc(src_len + 1, sizeof(u16));
|
||||||
|
if (!dst)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
// Copy elements from src into dst
|
||||||
|
for (u32 i = 0; i < src_len; i++)
|
||||||
|
dst[i] = src[i];
|
||||||
|
|
||||||
|
return dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
int str_utf8_to_u16(u16 **dst, u32 *dst_len, const u8 *src, u32 src_len)
|
u16* strcopy_utf8to16(const char *src)
|
||||||
{
|
{
|
||||||
*dst_len = src_len*sizeof(u16);
|
if (!src)
|
||||||
*dst = malloc((*dst_len)+sizeof(u16));
|
return NULL;
|
||||||
if(*dst == NULL)
|
|
||||||
return -1;
|
u32 src_len = strlen(src);
|
||||||
memset(*dst,0,(*dst_len)+sizeof(u16));
|
|
||||||
|
// Allocate memory for expanded string
|
||||||
UTF16 *target_start = *dst;
|
u16 *dst = calloc(src_len + 1, sizeof(u16));
|
||||||
UTF16 *target_end = (target_start + *dst_len);
|
if (!dst)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
UTF16 *target_start = dst;
|
||||||
|
UTF16 *target_end = (target_start + (src_len*sizeof(u16)));
|
||||||
|
|
||||||
UTF8 *src_start = (UTF8*)src;
|
UTF8 *src_start = (UTF8*)src;
|
||||||
UTF8 *src_end = (UTF8*)(src+src_len*sizeof(u8));
|
UTF8 *src_end = (UTF8*)(src + src_len*sizeof(char));
|
||||||
|
|
||||||
return ConvertUTF8toUTF16 ((const UTF8 **)&src_start, src_end, &target_start, target_end, strictConversion);
|
ConvertUTF8toUTF16((const UTF8 **)&src_start, src_end, &target_start, target_end, strictConversion);
|
||||||
|
|
||||||
|
return dst;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
+5
-4
@@ -20,11 +20,12 @@ u64 max64(u64 a, u64 b);
|
|||||||
// Strings
|
// Strings
|
||||||
void memdump(FILE* fout, const char* prefix, const const u8* data, u32 size);
|
void memdump(FILE* fout, const char* prefix, const const u8* data, u32 size);
|
||||||
char* replace_filextention(const char *input, const char *extention);
|
char* replace_filextention(const char *input, const char *extention);
|
||||||
int str_u8_to_u16(u16 **dst, u32 *dst_len, const u8 *src, u32 src_len);
|
u32 strlen_char16(const u16 *str);
|
||||||
int str_u16_to_u16(u16 **dst, u32 *dst_len, const u16 *src, u32 src_len);
|
char* strcopy_8to8(const char *src);
|
||||||
int str_u32_to_u16(u16 **dst, u32 *dst_len, const u32 *src, u32 src_len);
|
u16* strcopy_8to16(const char *src);
|
||||||
|
u16* strcopy_16to16(const u16 *src);
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
int str_utf8_to_u16(u16 **dst, u32 *dst_len, const u8 *src, u32 src_len);
|
u16* strcopy_utf8to16(const char *src);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Base64
|
// Base64
|
||||||
|
|||||||
Reference in New Issue
Block a user