[makerom] fixed romfs (for linux)

This commit is contained in:
jakcron
2015-11-15 03:19:53 +08:00
parent 7c3b5faed6
commit 0854cbe5d1
8 changed files with 299 additions and 374 deletions
+136 -235
View File
@@ -2,35 +2,36 @@
#include "dir.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 */
/* Tested working on Windows/Linux/OSX */
int fs_InitDir(const fs_entry *entry, fs_dir *dir);
int fs_ManageDirSlot(fs_dir *dir);
int fs_ManageFileSlot(fs_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;
}
int OpenDir(romfs_dir *dir);
int InitDir(romfs_dir *dir);
int ManageDir(romfs_dir *dir);
u32 fs_strlen(const fs_char *str)
{
u32 i;
for (i = 0; str[i] != 0x0; i++);
return i;
#ifdef _WIN32
return strlen_char16(str);
#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)
{
#ifdef _WIN32
@@ -67,264 +68,164 @@ fs_char* fs_AppendToPath(const fs_char *src, const fs_char *add)
return new_path;
}
fs_char* fs_CopyPath(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)
fs_char* fs_CopyStr(const fs_char *src)
{
#ifdef _WIN32
wprintf(L"%s", str);
return strcopy_16to16(src);
#else
printf("%s", str);
return strcopy_8to8(src);
#endif
}
void fs_romfs_fputs(FILE *out, const fs_romfs_char *str)
romfs_char* romfs_CopyConvertStr(const fs_char *src)
{
#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
const char *name = (const char*)str;
for (u32 i = 0; i < fs_romfs_strlen(str)*2; i += 2)
putchar(name[i]);
for (u32 i = 0; i < romfs_strlen(str)*2; i += 2)
fputc(name[i],out);
#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->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->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;
}
int fs_ManageDirSlot(fs_dir *dir)
int ManageDir(romfs_dir *dir)
{
if(dir->u_child >= dir->m_child)
{
dir->m_child *= 2;
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;
if (dir->u_child >= dir->m_child) {
dir->m_child = 2 * dir->u_child;
dir->child = realloc(dir->child, dir->m_child*sizeof(romfs_dir));
}
return 0;
}
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->u_file >= dir->m_file) {
dir->m_file = 2 * dir->u_file;
dir->file = realloc(dir->file, dir->m_file*sizeof(romfs_file));
}
if (dir->child == NULL || dir->file == NULL)
return MEM_ERROR;
return 0;
}
fs_entry* fs_GetEntry(const fs_char *parent_path, fs_DIR *dp)
int OpenRootDir(const char *path, romfs_dir *dir)
{
// Directory structs
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;
// Create native FS path
#ifdef _WIN32
namlen = tmp_entry->d_namlen;
dir->path = strcopy_8to16(path);
#else
namlen = strlen(tmp_entry->d_name);
dir->path = strcopy_8to8(path);
#endif
//printf("allocate memory for entry\n");
fs_entry *entry = malloc(sizeof(fs_entry));
memset(entry,0,sizeof(fs_entry));
// Copy romfs name (empty string)
dir->name = romfs_CopyStr(ROMFS_EMPTY_PATH);
dir->namesize = 0;
//Copy FS compatible Entry name
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);
// Convert Entry name into RomFS u16 char (windows wchar_t, thanks Nintendo)
#if _WIN32
str_u16_to_u16(&entry->name,&entry->name_len,tmp_entry->d_name,namlen);
#else
str_utf8_to_u16(&entry->name,&entry->name_len,(u8*)tmp_entry->d_name,namlen);
#endif
//printf("get dir entry from dir ptr to check if dir\n");
tmp_dptr = fs_opendir(entry->fs_path);
if(tmp_dptr)
return OpenDir(dir);
}
int OpenDir(romfs_dir *dir)
{
fs_DIR *dp, *tmp_dp;
struct fs_dirent *entry;
if (InitDir(dir))
return MEM_ERROR;
// Open Directory
if((dp = fs_opendir(dir->path)) == NULL)
{
//printf("is dir\n");
fs_closedir(tmp_dptr);
entry->IsDir = true;
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);
printf("[ROMFS] Failed to open directory: \"");
fs_fputs(dir->path, stdout);
printf("\"\n");
return -1;
}
//printf("read entries\n");
while((entry = fs_GetEntry(dir->fs_path, dp)) != NULL)
{
ret = entry->IsDir? fs_AddDir(entry, dir) : fs_AddFile(entry, dir);
// Process Entries
while ((entry = fs_readdir(dp)) != NULL)
{
// 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");
fs_FreeEntry(entry);
// Ensures that there is always memory for child directory and file structs
if (ManageDir(dir))
return MEM_ERROR;
// Get native FS path
fs_char *path = fs_AppendToPath(dir->path, entry->d_name);
if(ret)
{
//printf("error parsing entry\n");
break;
// Opening directory with fs path to test if directory
if ((tmp_dp = fs_opendir(path)) != NULL) {
fs_closedir(tmp_dp);
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);
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++)
printf(" ");
if (depth > 0)
fs_romfs_fputs(stdout, dir->name);
romfs_fputs(dir->name, stdout);
else
printf("romfs:");
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++)
printf(" ");
fs_romfs_fputs(stdout, dir->file[i].name);
romfs_fputs(dir->file[i].name, stdout);
printf(" (0x%"PRIx64")\n", dir->file[i].size);
}
}
if(dir->u_child)
{
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");
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);
}
//printf("free file struct\n");
@@ -361,9 +262,9 @@ void fs_FreeDir(fs_dir *dir)
//printf("free dir names and\n");
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);
fs_FreeDir(&dir->child[i]);
FreeDir(&dir->child[i]);
}
//printf("free dir struct\n");
free(dir->child);
+26 -35
View File
@@ -1,7 +1,7 @@
#pragma once
#ifdef _WIN32
#define fs_romfs_char u16
#define romfs_char u16
#define fs_char wchar_t
#define fs_dirent _wdirent
#define fs_DIR _WDIR
@@ -11,7 +11,7 @@
#define fs_closedir _wclosedir
#define FS_PATH_SEPARATOR '\\'
#else
#define fs_romfs_char u16
#define romfs_char u16
#define fs_char char
#define fs_dirent dirent
#define fs_DIR DIR
@@ -21,59 +21,50 @@
#define fs_closedir closedir
#define FS_PATH_SEPARATOR '/'
#endif
struct fs_entry
struct romfs_file
{
bool IsDir;
fs_char *fs_path;
fs_romfs_char *name;
u32 name_len;
fs_char *path;
romfs_char *name;
u32 namesize;
u64 size;
};
struct fs_file
struct romfs_dir
{
fs_char *fs_path;
fs_romfs_char *name;
u32 name_len;
u64 size;
};
struct fs_dir
{
fs_char *fs_path;
fs_romfs_char *name;
u32 name_len;
fs_char *path;
romfs_char *name;
u32 namesize;
struct fs_dir *child;
struct romfs_dir *child;
u32 m_child;
u32 u_child;
struct fs_file *file;
struct romfs_file *file;
u32 m_file;
u32 u_file;
};
typedef struct fs_entry fs_entry;
typedef struct fs_file fs_file;
typedef struct fs_dir fs_dir;
typedef struct romfs_file romfs_file;
typedef struct romfs_dir romfs_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_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);
int fs_strcmp(const fs_char *str1, const fs_char *str2);
FILE* fs_fopen(fs_char *path);
u64 fs_fsize(fs_char *path);
fs_char* fs_AppendToPath(const fs_char *src, const fs_char *add);
fs_char* fs_CopyPath(const fs_char *src);
fs_romfs_char* fs_CopyRomfsName(const fs_romfs_char *src);
void fs_fputs(FILE *out, const fs_char *str);
void fs_romfs_fputs(FILE *out, const fs_romfs_char *str);
fs_char* fs_CopyStr(const fs_char *src);
romfs_char* romfs_CopyStr(const romfs_char *src);
void fs_fputs(const fs_char *str, FILE *out);
void romfs_fputs(const romfs_char *str, FILE *out);
int fs_OpenRootDir(const char *path, fs_dir *dir);
int fs_OpenDir(fs_entry *entry, fs_dir *dir);
void fs_PrintDir(fs_dir *dir, u32 depth);
void fs_FreeDir(fs_dir *dir);
int OpenRootDir(const char *path, romfs_dir *dir);
void PrintDir(romfs_dir *dir, u32 depth);
void FreeDir(romfs_dir *dir);
+6 -6
View File
@@ -66,9 +66,6 @@
<ClInclude Include="ctr_utils.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="dir.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="elf.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -339,6 +336,9 @@
<ClInclude Include="code.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="dir.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="accessdesc.c">
@@ -362,9 +362,6 @@
<ClCompile Include="ctr_utils.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="dir.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="elf.c">
<Filter>Source Files</Filter>
</ClCompile>
@@ -473,6 +470,9 @@
<ClCompile Include="code.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="dir.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="Makefile">
+1 -1
View File
@@ -47,7 +47,7 @@ int BuildRomFs(romfs_buildctx *ctx)
void FreeRomFsCtx(romfs_buildctx *ctx)
{
if(ctx->fs){
fs_FreeDir(ctx->fs);
FreeDir(ctx->fs);
free(ctx->fs);
}
}
+1 -1
View File
@@ -86,7 +86,7 @@ typedef struct
ivfc_hdr *ivfcHdr;
romfs_infoheader *romfsHdr;
fs_dir *fs;
romfs_dir *fs;
u8 *dirHashTable;
u32 m_dirHashTable;
+48 -47
View File
@@ -7,35 +7,36 @@ const int ROMFS_BLOCK_SIZE = 0x1000;
const unsigned int ROMFS_UNUSED_ENTRY = 0xffffffff;
// Build
bool IsFileWanted(fs_file *file, void *filter_criteria);
bool IsDirWanted(fs_dir *dir, void *filter_criteria);
void CalcDirSize(romfs_buildctx *ctx, fs_dir *fs);
bool IsFileWanted(romfs_file *file, void *filter_criteria);
bool IsDirWanted(romfs_dir *dir, void *filter_criteria);
void CalcDirSize(romfs_buildctx *ctx, romfs_dir *fs);
void CalcRomfsSize(romfs_buildctx *ctx);
int FilterRomFS(fs_dir *fs_raw, fs_dir *fs_filtered, void *filter_criteria);
void AddFileToRomfs(romfs_buildctx *ctx, fs_file *file, u32 parent, u32 sibling);
void AddDirToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 sibling);
void AddDirChildrenToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 dir);
int FilterRomFS(romfs_dir *fs_raw, romfs_dir *fs_filtered, void *filter_criteria);
void AddFileToRomfs(romfs_buildctx *ctx, romfs_file *file, u32 parent, u32 sibling);
void AddDirToRomfs(romfs_buildctx *ctx, romfs_dir *fs, u32 parent, u32 sibling);
void AddDirChildrenToRomfs(romfs_buildctx *ctx, romfs_dir *fs, u32 parent, u32 dir);
void PopulateHashTable(romfs_buildctx *ctx);
void PopulateRomfs(romfs_buildctx *ctx);
void BuildRomfsHeader(romfs_buildctx *ctx);
void BuildIvfcHeader(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)
{
/* FS Structures */
void *filter_criteria = NULL;
fs_dir *fs_raw = calloc(1,sizeof(fs_dir));
ctx->fs = calloc(1,sizeof(fs_dir));
romfs_dir *fs_raw = calloc(1,sizeof(romfs_dir));
ctx->fs = calloc(1,sizeof(romfs_dir));
/* 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);
/* free unfiltered FS */
fs_FreeDir(fs_raw);
FreeDir(fs_raw);
free(fs_raw);
/* 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;
}
bool IsDirWanted(fs_dir *dir, void *filter_criteria)
bool IsDirWanted(romfs_dir *dir, void *filter_criteria)
{
bool ret = false;
for(u32 i = 0; i < dir->u_file; i++)
@@ -121,16 +122,16 @@ bool IsDirWanted(fs_dir *dir, void *filter_criteria)
return ret;
}
void CalcDirSize(romfs_buildctx *ctx, fs_dir *fs)
void CalcDirSize(romfs_buildctx *ctx, romfs_dir *fs)
{
if(ctx->m_dirTableLen == 0)
ctx->m_dirTableLen = sizeof(romfs_direntry);
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++)
{
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)
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);
}
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))
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->name = fs_CopyRomfsName(fs_raw->name);
fs_filtered->namesize = fs_raw->namesize;
fs_filtered->name = romfs_CopyStr(fs_raw->name);
fs_filtered->u_child = 0;
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->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++)
{
@@ -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))
{
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].name = fs_CopyRomfsName(fs_raw->file[i].name);
fs_filtered->file[fs_filtered->u_file].namesize = fs_raw->file[i].namesize;
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;
@@ -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;
}
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;
}
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);
@@ -297,10 +298,10 @@ void AddFileToRomfs(romfs_buildctx *ctx, fs_file *file, u32 parent, u32 sibling)
u32_to_u8(entry->siblingoffset,sibling,LE);
/* 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));
memset(name_pos,0,align(file->name_len,4));
memcpy(name_pos,(u8*)file->name,file->name_len);
memset(name_pos,0,align(file->namesize,4));
memcpy(name_pos,(u8*)file->name,file->namesize);
/* Set hash data */
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) {
printf("[ROMFS] Reading \"");
fs_fputs(stdout, file->fs_path);
fs_fputs(file->path, stdout);
printf("\"... ");
}
FILE *fp = fs_fopen(file->fs_path);
FILE *fp = fs_fopen(file->path);
fread(data_pos, file->size, 1, 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);
/* 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;
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);
/* 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));
memset(name_pos,0,(u32)align(fs->name_len,4));
memcpy(name_pos,(u8*)fs->name,fs->name_len);
memset(name_pos,0,(u32)align(fs->namesize,4));
memcpy(name_pos,(u8*)fs->name,fs->namesize);
/* Set hash data */
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);
/* 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);
@@ -380,7 +381,7 @@ void AddDirChildrenToRomfs(romfs_buildctx *ctx, fs_dir *fs, u32 parent, u32 dir)
if (i >= fs->u_file - 1)
file_sibling = ROMFS_UNUSED_ENTRY;
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 */
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)
dir_sibling = ROMFS_UNUSED_ENTRY;
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 */
AddDirToRomfs(ctx, &fs->child[i], dir, dir_sibling);
@@ -459,7 +460,7 @@ void GenIvfcHashTree(romfs_buildctx *ctx)
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;
for( u32 index = 0; index < length; index++ )
+76 -45
View File
@@ -68,8 +68,8 @@ char* replace_filextention(const char *input, const char *new_ext)
}
else {
u32 size = ext - input;
new_name = calloc(size + strlen(new_ext), 1);
snprintf(new_name, size, "%s", input);
new_name = calloc(size + strlen(new_ext) + 1, 1);
strncpy(new_name, input, size);
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);
*dst = malloc((*dst_len)+sizeof(u16));
if(*dst == NULL)
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;
u32 i;
for (i = 0; str[i] != 0x0; i++);
return i;
}
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);
*dst = malloc((*dst_len)+sizeof(u16));
if(*dst == NULL)
return -1;
memset(*dst,0,(*dst_len)+sizeof(u16));
u16 *tmp = *dst;
for(int i=0; i<src_len; i++)
tmp[i] = src[i];
return 0;
if (!src)
return NULL;
u32 src_len = strlen(src);
// Allocate memory for expanded string
char *dst = calloc(src_len + 1, sizeof(u8));
if (!dst)
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);
*dst = malloc((*dst_len)+sizeof(u16));
if(*dst == NULL)
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;
if (!src)
return NULL;
u32 src_len = strlen(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;
}
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
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);
*dst = malloc((*dst_len)+sizeof(u16));
if(*dst == NULL)
return -1;
memset(*dst,0,(*dst_len)+sizeof(u16));
UTF16 *target_start = *dst;
UTF16 *target_end = (target_start + *dst_len);
if (!src)
return NULL;
u32 src_len = strlen(src);
// Allocate memory for expanded string
u16 *dst = calloc(src_len + 1, sizeof(u16));
if (!dst)
return NULL;
UTF16 *target_start = dst;
UTF16 *target_end = (target_start + (src_len*sizeof(u16)));
UTF8 *src_start = (UTF8*)src;
UTF8 *src_end = (UTF8*)(src+src_len*sizeof(u8));
return ConvertUTF8toUTF16 ((const UTF8 **)&src_start, src_end, &target_start, target_end, strictConversion);
UTF8 *src_end = (UTF8*)(src + src_len*sizeof(char));
ConvertUTF8toUTF16((const UTF8 **)&src_start, src_end, &target_start, target_end, strictConversion);
return dst;
}
#endif
+5 -4
View File
@@ -20,11 +20,12 @@ u64 max64(u64 a, u64 b);
// Strings
void memdump(FILE* fout, const char* prefix, const const u8* data, u32 size);
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);
int str_u16_to_u16(u16 **dst, u32 *dst_len, const u16 *src, u32 src_len);
int str_u32_to_u16(u16 **dst, u32 *dst_len, const u32 *src, u32 src_len);
u32 strlen_char16(const u16 *str);
char* strcopy_8to8(const char *src);
u16* strcopy_8to16(const char *src);
u16* strcopy_16to16(const u16 *src);
#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
// Base64