Formatting

This commit is contained in:
Maschell 2018-03-11 16:44:04 +01:00
parent ab456c08c9
commit 94d64db894
24 changed files with 1273 additions and 1300 deletions

View File

@ -3,39 +3,33 @@
#include <stdio.h>
#include "CFile.hpp"
CFile::CFile()
{
iFd = -1;
mem_file = NULL;
filesize = 0;
pos = 0;
CFile::CFile() {
iFd = -1;
mem_file = NULL;
filesize = 0;
pos = 0;
}
CFile::CFile(const std::string & filepath, eOpenTypes mode)
{
iFd = -1;
this->open(filepath, mode);
CFile::CFile(const std::string & filepath, eOpenTypes mode) {
iFd = -1;
this->open(filepath, mode);
}
CFile::CFile(const u8 * mem, s32 size)
{
iFd = -1;
this->open(mem, size);
CFile::CFile(const u8 * mem, s32 size) {
iFd = -1;
this->open(mem, size);
}
CFile::~CFile()
{
this->close();
CFile::~CFile() {
this->close();
}
s32 CFile::open(const std::string & filepath, eOpenTypes mode)
{
this->close();
s32 CFile::open(const std::string & filepath, eOpenTypes mode) {
this->close();
s32 openMode = 0;
s32 openMode = 0;
switch(mode)
{
switch(mode) {
default:
case ReadOnly:
openMode = O_RDONLY;
@ -49,79 +43,71 @@ s32 CFile::open(const std::string & filepath, eOpenTypes mode)
case Append:
openMode = O_APPEND | O_WRONLY;
break;
}
}
//! Using fopen works only on the first launch as expected
//! on the second launch it causes issues because we don't overwrite
//! the .data sections which is needed for a normal application to re-init
//! this will be added with launching as RPX
iFd = ::open(filepath.c_str(), openMode);
if(iFd < 0)
return iFd;
iFd = ::open(filepath.c_str(), openMode);
if(iFd < 0)
return iFd;
filesize = ::lseek(iFd, 0, SEEK_END);
::lseek(iFd, 0, SEEK_SET);
filesize = ::lseek(iFd, 0, SEEK_END);
::lseek(iFd, 0, SEEK_SET);
return 0;
return 0;
}
s32 CFile::open(const u8 * mem, s32 size)
{
this->close();
s32 CFile::open(const u8 * mem, s32 size) {
this->close();
mem_file = mem;
filesize = size;
mem_file = mem;
filesize = size;
return 0;
return 0;
}
void CFile::close()
{
if(iFd >= 0)
::close(iFd);
void CFile::close() {
if(iFd >= 0)
::close(iFd);
iFd = -1;
mem_file = NULL;
filesize = 0;
pos = 0;
iFd = -1;
mem_file = NULL;
filesize = 0;
pos = 0;
}
s32 CFile::read(u8 * ptr, size_t size)
{
if(iFd >= 0)
{
s32 ret = ::read(iFd, ptr,size);
if(ret > 0)
pos += ret;
return ret;
}
s32 CFile::read(u8 * ptr, size_t size) {
if(iFd >= 0) {
s32 ret = ::read(iFd, ptr,size);
if(ret > 0)
pos += ret;
return ret;
}
s32 readsize = size;
s32 readsize = size;
if(readsize > (s64) (filesize-pos))
readsize = filesize-pos;
if(readsize > (s64) (filesize-pos))
readsize = filesize-pos;
if(readsize <= 0)
return readsize;
if(readsize <= 0)
return readsize;
if(mem_file != NULL)
{
memcpy(ptr, mem_file+pos, readsize);
pos += readsize;
return readsize;
}
if(mem_file != NULL) {
memcpy(ptr, mem_file+pos, readsize);
pos += readsize;
return readsize;
}
return -1;
return -1;
}
s32 CFile::write(const u8 * ptr, size_t size)
{
if(iFd >= 0)
{
size_t done = 0;
while(done < size)
{
s32 CFile::write(const u8 * ptr, size_t size) {
if(iFd >= 0) {
size_t done = 0;
while(done < size) {
s32 ret = ::write(iFd, ptr, size - done);
if(ret <= 0)
return ret;
@ -130,68 +116,56 @@ s32 CFile::write(const u8 * ptr, size_t size)
done += ret;
pos += ret;
}
return done;
}
return done;
}
return -1;
return -1;
}
s32 CFile::seek(long int offset, s32 origin)
{
s32 ret = 0;
s64 newPos = pos;
s32 CFile::seek(long int offset, s32 origin) {
s32 ret = 0;
s64 newPos = pos;
if(origin == SEEK_SET)
{
newPos = offset;
}
else if(origin == SEEK_CUR)
{
newPos += offset;
}
else if(origin == SEEK_END)
{
newPos = filesize+offset;
}
if(origin == SEEK_SET) {
newPos = offset;
} else if(origin == SEEK_CUR) {
newPos += offset;
} else if(origin == SEEK_END) {
newPos = filesize+offset;
}
if(newPos < 0)
{
pos = 0;
}
else {
if(newPos < 0) {
pos = 0;
} else {
pos = newPos;
}
}
if(iFd >= 0)
ret = ::lseek(iFd, pos, SEEK_SET);
if(iFd >= 0)
ret = ::lseek(iFd, pos, SEEK_SET);
if(mem_file != NULL)
{
if(pos > filesize)
{
pos = filesize;
}
}
if(mem_file != NULL) {
if(pos > filesize) {
pos = filesize;
}
}
return ret;
return ret;
}
s32 CFile::fwrite(const char *format, ...)
{
s32 CFile::fwrite(const char *format, ...) {
s32 result = -1;
char * tmp = NULL;
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) {
result = this->write((u8 *)tmp, strlen(tmp));
}
va_end(va);
}
va_end(va);
if(tmp){
free(tmp);
tmp = NULL;
if(tmp) {
free(tmp);
tmp = NULL;
}
return result;

View File

@ -8,50 +8,54 @@
#include <dynamic_libs/os_types.h>
#include <unistd.h>
class CFile
{
public:
enum eOpenTypes
{
ReadOnly,
WriteOnly,
ReadWrite,
Append
};
class CFile {
public:
enum eOpenTypes {
ReadOnly,
WriteOnly,
ReadWrite,
Append
};
CFile();
CFile(const std::string & filepath, eOpenTypes mode);
CFile(const u8 * memory, s32 memsize);
virtual ~CFile();
CFile();
CFile(const std::string & filepath, eOpenTypes mode);
CFile(const u8 * memory, s32 memsize);
virtual ~CFile();
s32 open(const std::string & filepath, eOpenTypes mode);
s32 open(const u8 * memory, s32 memsize);
s32 open(const std::string & filepath, eOpenTypes mode);
s32 open(const u8 * memory, s32 memsize);
bool isOpen() const {
if(iFd >= 0)
return true;
bool isOpen() const {
if(iFd >= 0)
return true;
if(mem_file)
return true;
if(mem_file)
return true;
return false;
}
return false;
}
void close();
void close();
s32 read(u8 * ptr, size_t size);
s32 write(const u8 * ptr, size_t size);
s32 fwrite(const char *format, ...);
s32 seek(long int offset, s32 origin);
u64 tell() { return pos; };
u64 size() { return filesize; };
void rewind() { this->seek(0, SEEK_SET); };
s32 read(u8 * ptr, size_t size);
s32 write(const u8 * ptr, size_t size);
s32 fwrite(const char *format, ...);
s32 seek(long int offset, s32 origin);
u64 tell() {
return pos;
};
u64 size() {
return filesize;
};
void rewind() {
this->seek(0, SEEK_SET);
};
protected:
s32 iFd;
const u8 * mem_file;
u64 filesize;
u64 pos;
protected:
s32 iFd;
const u8 * mem_file;
u64 filesize;
u64 pos;
};
#endif

View File

@ -35,76 +35,68 @@
#include "DirList.h"
#include "utils/StringTools.h"
DirList::DirList()
{
Flags = 0;
Filter = 0;
Depth = 0;
DirList::DirList() {
Flags = 0;
Filter = 0;
Depth = 0;
}
DirList::DirList(const std::string & path, const char *filter, u32 flags, u32 maxDepth)
{
this->LoadPath(path, filter, flags, maxDepth);
this->SortList();
DirList::DirList(const std::string & path, const char *filter, u32 flags, u32 maxDepth) {
this->LoadPath(path, filter, flags, maxDepth);
this->SortList();
}
DirList::~DirList()
{
ClearList();
DirList::~DirList() {
ClearList();
}
bool DirList::LoadPath(const std::string & folder, const char *filter, u32 flags, u32 maxDepth)
{
if(folder.empty()) return false;
bool DirList::LoadPath(const std::string & folder, const char *filter, u32 flags, u32 maxDepth) {
if(folder.empty()) return false;
Flags = flags;
Filter = filter;
Depth = maxDepth;
Flags = flags;
Filter = filter;
Depth = maxDepth;
std::string folderpath(folder);
u32 length = folderpath.size();
std::string folderpath(folder);
u32 length = folderpath.size();
//! clear path of double slashes
StringTools::RemoveDoubleSlashs(folderpath);
//! clear path of double slashes
StringTools::RemoveDoubleSlashs(folderpath);
//! remove last slash if exists
if(length > 0 && folderpath[length-1] == '/')
folderpath.erase(length-1);
//! remove last slash if exists
if(length > 0 && folderpath[length-1] == '/')
folderpath.erase(length-1);
//! add root slash if missing
if(folderpath.find('/') == std::string::npos){
if(folderpath.find('/') == std::string::npos) {
folderpath += '/';
}
return InternalLoadPath(folderpath);
return InternalLoadPath(folderpath);
}
bool DirList::InternalLoadPath(std::string &folderpath)
{
if(folderpath.size() < 3)
return false;
bool DirList::InternalLoadPath(std::string &folderpath) {
if(folderpath.size() < 3)
return false;
struct dirent *dirent = NULL;
DIR *dir = NULL;
struct dirent *dirent = NULL;
DIR *dir = NULL;
dir = opendir(folderpath.c_str());
if (dir == NULL)
return false;
dir = opendir(folderpath.c_str());
if (dir == NULL)
return false;
while ((dirent = readdir(dir)) != 0)
{
bool isDir = dirent->d_type & DT_DIR;
const char *filename = dirent->d_name;
while ((dirent = readdir(dir)) != 0) {
bool isDir = dirent->d_type & DT_DIR;
const char *filename = dirent->d_name;
if(isDir)
{
if(strcmp(filename,".") == 0 || strcmp(filename,"..") == 0)
continue;
if(isDir) {
if(strcmp(filename,".") == 0 || strcmp(filename,"..") == 0)
continue;
if((Flags & CheckSubfolders) && (Depth > 0))
{
if((Flags & CheckSubfolders) && (Depth > 0)) {
s32 length = folderpath.size();
if(length > 2 && folderpath[length-1] != '/'){
if(length > 2 && folderpath[length-1] != '/') {
folderpath += '/';
}
folderpath += filename;
@ -115,122 +107,106 @@ bool DirList::InternalLoadPath(std::string &folderpath)
Depth++;
}
if(!(Flags & Dirs))
continue;
}
else if(!(Flags & Files))
{
continue;
}
if(!(Flags & Dirs))
continue;
} else if(!(Flags & Files)) {
continue;
}
if(Filter)
{
char * fileext = strrchr(filename, '.');
if(!fileext)
continue;
if(Filter) {
char * fileext = strrchr(filename, '.');
if(!fileext)
continue;
if(StringTools::strtokcmp(fileext, Filter, ",") == 0)
AddEntrie(folderpath, filename, isDir);
}
else
{
AddEntrie(folderpath, filename, isDir);
}
}
closedir(dir);
if(StringTools::strtokcmp(fileext, Filter, ",") == 0)
AddEntrie(folderpath, filename, isDir);
} else {
AddEntrie(folderpath, filename, isDir);
}
}
closedir(dir);
return true;
return true;
}
void DirList::AddEntrie(const std::string &filepath, const char * filename, bool isDir)
{
if(!filename)
return;
void DirList::AddEntrie(const std::string &filepath, const char * filename, bool isDir) {
if(!filename)
return;
s32 pos = FileInfo.size();
s32 pos = FileInfo.size();
FileInfo.resize(pos+1);
FileInfo.resize(pos+1);
FileInfo[pos].FilePath = (char *) malloc(filepath.size()+strlen(filename)+2);
if(!FileInfo[pos].FilePath)
{
FileInfo.resize(pos);
return;
}
FileInfo[pos].FilePath = (char *) malloc(filepath.size()+strlen(filename)+2);
if(!FileInfo[pos].FilePath) {
FileInfo.resize(pos);
return;
}
sprintf(FileInfo[pos].FilePath, "%s/%s", filepath.c_str(), filename);
FileInfo[pos].isDir = isDir;
sprintf(FileInfo[pos].FilePath, "%s/%s", filepath.c_str(), filename);
FileInfo[pos].isDir = isDir;
}
void DirList::ClearList()
{
for(u32 i = 0; i < FileInfo.size(); ++i)
{
if(FileInfo[i].FilePath){
free(FileInfo[i].FilePath);
FileInfo[i].FilePath = NULL;
}
}
void DirList::ClearList() {
for(u32 i = 0; i < FileInfo.size(); ++i) {
if(FileInfo[i].FilePath) {
free(FileInfo[i].FilePath);
FileInfo[i].FilePath = NULL;
}
}
FileInfo.clear();
std::vector<DirEntry>().swap(FileInfo);
FileInfo.clear();
std::vector<DirEntry>().swap(FileInfo);
}
const char * DirList::GetFilename(s32 ind) const
{
if (!valid(ind))
return "";
const char * DirList::GetFilename(s32 ind) const {
if (!valid(ind))
return "";
return StringTools::FullpathToFilename(FileInfo[ind].FilePath);
return StringTools::FullpathToFilename(FileInfo[ind].FilePath);
}
static bool SortCallback(const DirEntry & f1, const DirEntry & f2)
{
if(f1.isDir && !(f2.isDir)) return true;
if(!(f1.isDir) && f2.isDir) return false;
static bool SortCallback(const DirEntry & f1, const DirEntry & f2) {
if(f1.isDir && !(f2.isDir)) return true;
if(!(f1.isDir) && f2.isDir) return false;
if(f1.FilePath && !f2.FilePath) return true;
if(!f1.FilePath) return false;
if(f1.FilePath && !f2.FilePath) return true;
if(!f1.FilePath) return false;
if(strcasecmp(f1.FilePath, f2.FilePath) > 0)
return false;
if(strcasecmp(f1.FilePath, f2.FilePath) > 0)
return false;
return true;
return true;
}
void DirList::SortList()
{
if(FileInfo.size() > 1)
std::sort(FileInfo.begin(), FileInfo.end(), SortCallback);
void DirList::SortList() {
if(FileInfo.size() > 1)
std::sort(FileInfo.begin(), FileInfo.end(), SortCallback);
}
void DirList::SortList(bool (*SortFunc)(const DirEntry &a, const DirEntry &b))
{
if(FileInfo.size() > 1)
std::sort(FileInfo.begin(), FileInfo.end(), SortFunc);
void DirList::SortList(bool (*SortFunc)(const DirEntry &a, const DirEntry &b)) {
if(FileInfo.size() > 1)
std::sort(FileInfo.begin(), FileInfo.end(), SortFunc);
}
u64 DirList::GetFilesize(s32 index) const
{
struct stat st;
const char *path = GetFilepath(index);
u64 DirList::GetFilesize(s32 index) const {
struct stat st;
const char *path = GetFilepath(index);
if(!path || stat(path, &st) != 0)
return 0;
if(!path || stat(path, &st) != 0)
return 0;
return st.st_size;
return st.st_size;
}
s32 DirList::GetFileIndex(const char *filename) const
{
if(!filename)
return -1;
s32 DirList::GetFileIndex(const char *filename) const {
if(!filename)
return -1;
for (u32 i = 0; i < FileInfo.size(); ++i)
{
if (strcasecmp(GetFilename(i), filename) == 0)
return i;
}
for (u32 i = 0; i < FileInfo.size(); ++i) {
if (strcasecmp(GetFilename(i), filename) == 0)
return i;
}
return -1;
return -1;
}

View File

@ -31,66 +31,73 @@
#include <string>
#include <dynamic_libs/os_types.h>
typedef struct
{
char * FilePath;
bool isDir;
typedef struct {
char * FilePath;
bool isDir;
} DirEntry;
class DirList
{
class DirList {
public:
//!Constructor
DirList(void);
//!\param path Path from where to load the filelist of all files
//!\param filter A fileext that needs to be filtered
//!\param flags search/filter flags from the enum
DirList(const std::string & path, const char *filter = NULL, u32 flags = Files | Dirs, u32 maxDepth = 0xffffffff);
//!Destructor
virtual ~DirList();
//! Load all the files from a directory
bool LoadPath(const std::string & path, const char *filter = NULL, u32 flags = Files | Dirs, u32 maxDepth = 0xffffffff);
//! Get a filename of the list
//!\param list index
const char * GetFilename(s32 index) const;
//! Get the a filepath of the list
//!\param list index
const char *GetFilepath(s32 index) const { if (!valid(index)) return ""; else return FileInfo[index].FilePath; }
//! Get the a filesize of the list
//!\param list index
u64 GetFilesize(s32 index) const;
//! Is index a dir or a file
//!\param list index
bool IsDir(s32 index) const { if(!valid(index)) return false; return FileInfo[index].isDir; };
//! Get the filecount of the whole list
s32 GetFilecount() const { return FileInfo.size(); };
//! Sort list by filepath
void SortList();
//! Custom sort command for custom sort functions definitions
void SortList(bool (*SortFunc)(const DirEntry &a, const DirEntry &b));
//! Get the index of the specified filename
s32 GetFileIndex(const char *filename) const;
//! Enum for search/filter flags
enum
{
Files = 0x01,
Dirs = 0x02,
CheckSubfolders = 0x08,
};
//!Constructor
DirList(void);
//!\param path Path from where to load the filelist of all files
//!\param filter A fileext that needs to be filtered
//!\param flags search/filter flags from the enum
DirList(const std::string & path, const char *filter = NULL, u32 flags = Files | Dirs, u32 maxDepth = 0xffffffff);
//!Destructor
virtual ~DirList();
//! Load all the files from a directory
bool LoadPath(const std::string & path, const char *filter = NULL, u32 flags = Files | Dirs, u32 maxDepth = 0xffffffff);
//! Get a filename of the list
//!\param list index
const char * GetFilename(s32 index) const;
//! Get the a filepath of the list
//!\param list index
const char *GetFilepath(s32 index) const {
if (!valid(index)) return "";
else return FileInfo[index].FilePath;
}
//! Get the a filesize of the list
//!\param list index
u64 GetFilesize(s32 index) const;
//! Is index a dir or a file
//!\param list index
bool IsDir(s32 index) const {
if(!valid(index)) return false;
return FileInfo[index].isDir;
};
//! Get the filecount of the whole list
s32 GetFilecount() const {
return FileInfo.size();
};
//! Sort list by filepath
void SortList();
//! Custom sort command for custom sort functions definitions
void SortList(bool (*SortFunc)(const DirEntry &a, const DirEntry &b));
//! Get the index of the specified filename
s32 GetFileIndex(const char *filename) const;
//! Enum for search/filter flags
enum {
Files = 0x01,
Dirs = 0x02,
CheckSubfolders = 0x08,
};
protected:
// Internal parser
bool InternalLoadPath(std::string &path);
//!Add a list entrie
void AddEntrie(const std::string &filepath, const char * filename, bool isDir);
//! Clear the list
void ClearList();
//! Check if valid pos is requested
inline bool valid(u32 pos) const { return (pos < FileInfo.size()); };
// Internal parser
bool InternalLoadPath(std::string &path);
//!Add a list entrie
void AddEntrie(const std::string &filepath, const char * filename, bool isDir);
//! Clear the list
void ClearList();
//! Check if valid pos is requested
inline bool valid(u32 pos) const {
return (pos < FileInfo.size());
};
u32 Flags;
u32 Depth;
const char *Filter;
std::vector<DirEntry> FileInfo;
u32 Flags;
u32 Depth;
const char *Filter;
std::vector<DirEntry> FileInfo;
};
#endif

View File

@ -8,7 +8,7 @@
#include <dynamic_libs/os_functions.h>
#include <dynamic_libs/fs_functions.h>
s32 FSOSUtils::MountFS(void *pClient, void *pCmd, char **mount_path){
s32 FSOSUtils::MountFS(void *pClient, void *pCmd, char **mount_path) {
InitOSFunctionPointers();
InitFSFunctionPointers();
s32 result = -1;
@ -28,8 +28,7 @@ s32 FSOSUtils::MountFS(void *pClient, void *pCmd, char **mount_path){
memset(mountPath, 0, FS_MAX_MOUNTPATH_SIZE);
// Mount sdcard
if (FSGetMountSource(pClient, pCmd, FS_SOURCETYPE_EXTERNAL, mountSrc, -1) == 0)
{
if (FSGetMountSource(pClient, pCmd, FS_SOURCETYPE_EXTERNAL, mountSrc, -1) == 0) {
result = FSMount(pClient, pCmd, mountSrc, mountPath, FS_MAX_MOUNTPATH_SIZE, -1);
if((result == 0) && mount_path) {
*mount_path = (char*)malloc(strlen(mountPath) + 1);
@ -47,7 +46,7 @@ s32 FSOSUtils::MountFS(void *pClient, void *pCmd, char **mount_path){
return result;
}
s32 FSOSUtils::UmountFS(void *pClient, void *pCmd, const char *mountPath){
s32 FSOSUtils::UmountFS(void *pClient, void *pCmd, const char *mountPath) {
s32 result = -1;
result = FSUnmount(pClient, pCmd, mountPath, -1);

View File

@ -3,8 +3,8 @@
#include <dynamic_libs/os_types.h>
class FSOSUtils{
public:
class FSOSUtils {
public:
static s32 MountFS(void *pClient, void *pCmd, char **mount_path);
static s32 UmountFS(void *pClient, void *pCmd, const char *mountPath);

View File

@ -7,32 +7,30 @@
#include "CFile.hpp"
#include "utils/logger.h"
s32 FSUtils::LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size){
s32 FSUtils::LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size) {
//! always initialze input
*inbuffer = NULL;
*inbuffer = NULL;
if(size)
*size = 0;
s32 iFd = open(filepath, O_RDONLY);
if (iFd < 0)
return -1;
if (iFd < 0)
return -1;
u32 filesize = lseek(iFd, 0, SEEK_END);
u32 filesize = lseek(iFd, 0, SEEK_END);
lseek(iFd, 0, SEEK_SET);
u8 *buffer = (u8 *) malloc(filesize);
if (buffer == NULL)
{
u8 *buffer = (u8 *) malloc(filesize);
if (buffer == NULL) {
close(iFd);
return -2;
}
return -2;
}
u32 blocksize = 0x4000;
u32 done = 0;
s32 readBytes = 0;
while(done < filesize)
{
while(done < filesize) {
if(done + blocksize > filesize) {
blocksize = filesize - done;
}
@ -44,106 +42,98 @@ s32 FSUtils::LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size){
close(iFd);
if (done != filesize)
{
free(buffer);
buffer = NULL;
return -3;
}
if (done != filesize) {
free(buffer);
buffer = NULL;
return -3;
}
*inbuffer = buffer;
*inbuffer = buffer;
//! sign is optional input
if(size){
if(size) {
*size = filesize;
}
return filesize;
return filesize;
}
s32 FSUtils::CheckFile(const char * filepath){
if(!filepath)
return 0;
s32 FSUtils::CheckFile(const char * filepath) {
if(!filepath)
return 0;
struct stat filestat;
struct stat filestat;
char dirnoslash[strlen(filepath)+2];
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);
char dirnoslash[strlen(filepath)+2];
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);
while(dirnoslash[strlen(dirnoslash)-1] == '/')
dirnoslash[strlen(dirnoslash)-1] = '\0';
while(dirnoslash[strlen(dirnoslash)-1] == '/')
dirnoslash[strlen(dirnoslash)-1] = '\0';
char * notRoot = strrchr(dirnoslash, '/');
if(!notRoot)
{
strcat(dirnoslash, "/");
}
char * notRoot = strrchr(dirnoslash, '/');
if(!notRoot) {
strcat(dirnoslash, "/");
}
if (stat(dirnoslash, &filestat) == 0)
return 1;
if (stat(dirnoslash, &filestat) == 0)
return 1;
return 0;
return 0;
}
s32 FSUtils::CreateSubfolder(const char * fullpath){
if(!fullpath)
return 0;
s32 FSUtils::CreateSubfolder(const char * fullpath) {
if(!fullpath)
return 0;
s32 result = 0;
s32 result = 0;
char dirnoslash[strlen(fullpath)+1];
strcpy(dirnoslash, fullpath);
char dirnoslash[strlen(fullpath)+1];
strcpy(dirnoslash, fullpath);
s32 pos = strlen(dirnoslash)-1;
while(dirnoslash[pos] == '/')
{
dirnoslash[pos] = '\0';
pos--;
}
s32 pos = strlen(dirnoslash)-1;
while(dirnoslash[pos] == '/') {
dirnoslash[pos] = '\0';
pos--;
}
if(CheckFile(dirnoslash))
{
return 1;
}
else
{
char parentpath[strlen(dirnoslash)+2];
strcpy(parentpath, dirnoslash);
char * ptr = strrchr(parentpath, '/');
if(CheckFile(dirnoslash)) {
return 1;
} else {
char parentpath[strlen(dirnoslash)+2];
strcpy(parentpath, dirnoslash);
char * ptr = strrchr(parentpath, '/');
if(!ptr)
{
//!Device root directory (must be with '/')
strcat(parentpath, "/");
struct stat filestat;
if (stat(parentpath, &filestat) == 0)
return 1;
if(!ptr) {
//!Device root directory (must be with '/')
strcat(parentpath, "/");
struct stat filestat;
if (stat(parentpath, &filestat) == 0)
return 1;
return 0;
}
return 0;
}
ptr++;
ptr[0] = '\0';
ptr++;
ptr[0] = '\0';
result = CreateSubfolder(parentpath);
}
result = CreateSubfolder(parentpath);
}
if(!result)
return 0;
if(!result)
return 0;
if (mkdir(dirnoslash, 0777) == -1)
{
return 0;
}
if (mkdir(dirnoslash, 0777) == -1) {
return 0;
}
return 1;
return 1;
}
bool FSUtils::saveBufferToFile(const char * path, void * buffer, u32 size){
bool FSUtils::saveBufferToFile(const char * path, void * buffer, u32 size) {
s32 res = open(path, O_CREAT | O_TRUNC | O_WRONLY);
close(res);
CFile file(path, CFile::WriteOnly);
if (!file.isOpen()){
if (!file.isOpen()) {
DEBUG_FUNCTION_LINE("Failed to open %s\n",path);
return false;
}

View File

@ -2,14 +2,14 @@
#define __FS_UTILS_H_
#include <dynamic_libs/os_types.h>
class FSUtils{
public:
static s32 LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size);
class FSUtils {
public:
static s32 LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size);
//! todo: C++ class
static s32 CreateSubfolder(const char * fullpath);
static s32 CheckFile(const char * filepath);
static bool saveBufferToFile(const char * path, void * buffer, u32 size);
//! todo: C++ class
static s32 CreateSubfolder(const char * fullpath);
static s32 CheckFile(const char * filepath);
static bool saveBufferToFile(const char * path, void * buffer, u32 size);
};
#endif // __FS_UTILS_H_

View File

@ -53,14 +53,14 @@ typedef bool (* FN_MEDIUM_CLEARSTATUS)(void) ;
typedef bool (* FN_MEDIUM_SHUTDOWN)(void) ;
struct DISC_INTERFACE_STRUCT {
unsigned long ioType ;
unsigned long features ;
FN_MEDIUM_STARTUP startup ;
FN_MEDIUM_ISINSERTED isInserted ;
FN_MEDIUM_READSECTORS readSectors ;
FN_MEDIUM_WRITESECTORS writeSectors ;
FN_MEDIUM_CLEARSTATUS clearStatus ;
FN_MEDIUM_SHUTDOWN shutdown ;
unsigned long ioType ;
unsigned long features ;
FN_MEDIUM_STARTUP startup ;
FN_MEDIUM_ISINSERTED isInserted ;
FN_MEDIUM_READSECTORS readSectors ;
FN_MEDIUM_WRITESECTORS writeSectors ;
FN_MEDIUM_CLEARSTATUS clearStatus ;
FN_MEDIUM_SHUTDOWN shutdown ;
} ;
typedef struct DISC_INTERFACE_STRUCT DISC_INTERFACE ;

View File

@ -65,8 +65,7 @@ typedef struct _sd_fat_dir_entry_t {
int dirHandle;
} sd_fat_dir_entry_t;
static sd_fat_private_t *sd_fat_get_device_data(const char *path)
{
static sd_fat_private_t *sd_fat_get_device_data(const char *path) {
const devoptab_t *devoptab = NULL;
char name[128] = {0};
int i;
@ -91,8 +90,7 @@ static sd_fat_private_t *sd_fat_get_device_data(const char *path)
return NULL;
}
static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev)
{
static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev) {
// Sanity check
if (!path)
return NULL;
@ -113,8 +111,7 @@ static char *sd_fat_real_path (const char *path, sd_fat_private_t *dev)
return new_name;
}
static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode)
{
static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path, int flags, int mode) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if(!dev) {
r->_errno = ENODEV;
@ -164,8 +161,7 @@ static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path,
free(real_path);
if(result == 0)
{
if(result == 0) {
FSStat stats;
result = FSGetStatFile(dev->pClient, dev->pCmd, fd, &stats, -1);
if(result != 0) {
@ -187,8 +183,7 @@ static int sd_fat_open_r (struct _reent *r, void *fileStruct, const char *path,
}
static int sd_fat_close_r (struct _reent *r, void *fd)
{
static int sd_fat_close_r (struct _reent *r, void *fd) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
r->_errno = ENODEV;
@ -201,16 +196,14 @@ static int sd_fat_close_r (struct _reent *r, void *fd)
OSUnlockMutex(file->dev->pMutex);
if(result < 0)
{
if(result < 0) {
r->_errno = result;
return -1;
}
return 0;
}
static off_t sd_fat_seek_r (struct _reent *r, void* fd, off_t pos, int dir)
{
static off_t sd_fat_seek_r (struct _reent *r, void* fd, off_t pos, int dir) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
r->_errno = ENODEV;
@ -219,8 +212,7 @@ static off_t sd_fat_seek_r (struct _reent *r, void* fd, off_t pos, int dir)
OSLockMutex(file->dev->pMutex);
switch(dir)
{
switch(dir) {
case SEEK_SET:
file->pos = pos;
break;
@ -239,24 +231,21 @@ static off_t sd_fat_seek_r (struct _reent *r, void* fd, off_t pos, int dir)
OSUnlockMutex(file->dev->pMutex);
if(result == 0)
{
if(result == 0) {
return file->pos;
}
return result;
}
static ssize_t sd_fat_write_r (struct _reent *r, void *fd, const char *ptr, size_t len)
{
static ssize_t sd_fat_write_r (struct _reent *r, void *fd, const char *ptr, size_t len) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
r->_errno = ENODEV;
return 0;
}
if(!file->write)
{
if(!file->write) {
r->_errno = EACCES;
return 0;
}
@ -276,25 +265,19 @@ static ssize_t sd_fat_write_r (struct _reent *r, void *fd, const char *ptr, size
size_t done = 0;
while(done < len)
{
while(done < len) {
size_t write_size = (len_aligned < (len - done)) ? len_aligned : (len - done);
memcpy(tmpBuf, ptr + done, write_size);
int result = FSWriteFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, write_size, file->fd, 0, -1);
if(result < 0)
{
if(result < 0) {
r->_errno = result;
break;
}
else if(result == 0)
{
} else if(result == 0) {
if(write_size > 0)
done = 0;
break;
}
else
{
} else {
done += result;
file->pos += result;
}
@ -305,16 +288,14 @@ static ssize_t sd_fat_write_r (struct _reent *r, void *fd, const char *ptr, size
return done;
}
static ssize_t sd_fat_read_r (struct _reent *r, void* fd, char *ptr, size_t len)
{
static ssize_t sd_fat_read_r (struct _reent *r, void* fd, char *ptr, size_t len) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
r->_errno = ENODEV;
return 0;
}
if(!file->read)
{
if(!file->read) {
r->_errno = EACCES;
return 0;
}
@ -334,24 +315,18 @@ static ssize_t sd_fat_read_r (struct _reent *r, void* fd, char *ptr, size_t len)
size_t done = 0;
while(done < len)
{
while(done < len) {
size_t read_size = (len_aligned < (len - done)) ? len_aligned : (len - done);
int result = FSReadFile(file->dev->pClient, file->dev->pCmd, tmpBuf, 0x01, read_size, file->fd, 0, -1);
if(result < 0)
{
if(result < 0) {
r->_errno = result;
done = 0;
break;
}
else if(result == 0)
{
} else if(result == 0) {
//! TODO: error on read_size > 0
break;
}
else
{
} else {
memcpy(ptr + done, tmpBuf, read_size);
done += result;
file->pos += result;
@ -364,8 +339,7 @@ static ssize_t sd_fat_read_r (struct _reent *r, void* fd, char *ptr, size_t len)
}
static int sd_fat_fstat_r (struct _reent *r, void* fd, struct stat *st)
{
static int sd_fat_fstat_r (struct _reent *r, void* fd, struct stat *st) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
r->_errno = ENODEV;
@ -402,8 +376,7 @@ static int sd_fat_fstat_r (struct _reent *r, void* fd, struct stat *st)
return 0;
}
static int sd_fat_ftruncate_r (struct _reent *r, void* fd, off_t len)
{
static int sd_fat_ftruncate_r (struct _reent *r, void* fd, off_t len) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
r->_errno = ENODEV;
@ -424,8 +397,7 @@ static int sd_fat_ftruncate_r (struct _reent *r, void* fd, off_t len)
return 0;
}
static int sd_fat_fsync_r (struct _reent *r, void* fd)
{
static int sd_fat_fsync_r (struct _reent *r, void* fd) {
sd_fat_file_state_t *file = (sd_fat_file_state_t *)fd;
if(!file->dev) {
r->_errno = ENODEV;
@ -446,8 +418,7 @@ static int sd_fat_fsync_r (struct _reent *r, void* fd)
return 0;
}
static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st)
{
static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if(!dev) {
r->_errno = ENODEV;
@ -497,14 +468,12 @@ static int sd_fat_stat_r (struct _reent *r, const char *path, struct stat *st)
return 0;
}
static int sd_fat_link_r (struct _reent *r, const char *existing, const char *newLink)
{
static int sd_fat_link_r (struct _reent *r, const char *existing, const char *newLink) {
r->_errno = ENOTSUP;
return -1;
}
static int sd_fat_unlink_r (struct _reent *r, const char *name)
{
static int sd_fat_unlink_r (struct _reent *r, const char *name) {
sd_fat_private_t *dev = sd_fat_get_device_data(name);
if(!dev) {
r->_errno = ENODEV;
@ -535,8 +504,7 @@ static int sd_fat_unlink_r (struct _reent *r, const char *name)
return 0;
}
static int sd_fat_chdir_r (struct _reent *r, const char *name)
{
static int sd_fat_chdir_r (struct _reent *r, const char *name) {
sd_fat_private_t *dev = sd_fat_get_device_data(name);
if(!dev) {
r->_errno = ENODEV;
@ -566,8 +534,7 @@ static int sd_fat_chdir_r (struct _reent *r, const char *name)
return 0;
}
static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *newName)
{
static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *newName) {
sd_fat_private_t *dev = sd_fat_get_device_data(oldName);
if(!dev) {
r->_errno = ENODEV;
@ -606,8 +573,7 @@ static int sd_fat_rename_r (struct _reent *r, const char *oldName, const char *n
}
static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode)
{
static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if(!dev) {
r->_errno = ENODEV;
@ -637,8 +603,7 @@ static int sd_fat_mkdir_r (struct _reent *r, const char *path, int mode)
return 0;
}
static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf)
{
static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs *buf) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if(!dev) {
r->_errno = ENODEV;
@ -701,8 +666,7 @@ static int sd_fat_statvfs_r (struct _reent *r, const char *path, struct statvfs
return 0;
}
static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const char *path)
{
static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const char *path) {
sd_fat_private_t *dev = sd_fat_get_device_data(path);
if(!dev) {
r->_errno = ENODEV;
@ -728,8 +692,7 @@ static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const c
OSUnlockMutex(dev->pMutex);
if(result < 0)
{
if(result < 0) {
r->_errno = result;
return NULL;
}
@ -740,8 +703,7 @@ static DIR_ITER *sd_fat_diropen_r (struct _reent *r, DIR_ITER *dirState, const c
return dirState;
}
static int sd_fat_dirclose_r (struct _reent *r, DIR_ITER *dirState)
{
static int sd_fat_dirclose_r (struct _reent *r, DIR_ITER *dirState) {
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct;
if(!dirIter->dev) {
r->_errno = ENODEV;
@ -754,16 +716,14 @@ static int sd_fat_dirclose_r (struct _reent *r, DIR_ITER *dirState)
OSUnlockMutex(dirIter->dev->pMutex);
if(result < 0)
{
if(result < 0) {
r->_errno = result;
return -1;
}
return 0;
}
static int sd_fat_dirreset_r (struct _reent *r, DIR_ITER *dirState)
{
static int sd_fat_dirreset_r (struct _reent *r, DIR_ITER *dirState) {
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct;
if(!dirIter->dev) {
r->_errno = ENODEV;
@ -776,16 +736,14 @@ static int sd_fat_dirreset_r (struct _reent *r, DIR_ITER *dirState)
OSUnlockMutex(dirIter->dev->pMutex);
if(result < 0)
{
if(result < 0) {
r->_errno = result;
return -1;
}
return 0;
}
static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *st)
{
static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *st) {
sd_fat_dir_entry_t *dirIter = (sd_fat_dir_entry_t *)dirState->dirStruct;
if(!dirIter->dev) {
r->_errno = ENODEV;
@ -797,8 +755,7 @@ static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filenam
FSDirEntry * dir_entry = (FSDirEntry *)malloc(sizeof(FSDirEntry));
int result = FSReadDir(dirIter->dev->pClient, dirIter->dev->pCmd, dirIter->dirHandle, dir_entry, -1);
if(result < 0)
{
if(result < 0) {
free(dir_entry);
r->_errno = result;
OSUnlockMutex(dirIter->dev->pMutex);
@ -808,8 +765,7 @@ static int sd_fat_dirnext_r (struct _reent *r, DIR_ITER *dirState, char *filenam
// Fetch the current entry
strcpy(filename, dir_entry->name);
if(st)
{
if(st) {
memset(st, 0, sizeof(struct stat));
st->st_mode = (dir_entry->stat.flag & 0x80000000) ? S_IFDIR : S_IFREG;
st->st_nlink = 1;
@ -860,8 +816,7 @@ static const devoptab_t devops_sd_fat = {
};
static int sd_fat_add_device (const char *name, const char *mount_path, void *pClient, void *pCmd)
{
static int sd_fat_add_device (const char *name, const char *mount_path, void *pClient, void *pCmd) {
devoptab_t *dev = NULL;
char *devname = NULL;
char *devpath = NULL;
@ -886,7 +841,7 @@ static int sd_fat_add_device (const char *name, const char *mount_path, void *pC
// create private data
int mount_path_len = 0;
if(mount_path != NULL){
if(mount_path != NULL) {
mount_path_len = strlen(mount_path);
}
sd_fat_private_t *priv = (sd_fat_private_t *)malloc(sizeof(sd_fat_private_t) + mount_path_len + 1);
@ -897,7 +852,7 @@ static int sd_fat_add_device (const char *name, const char *mount_path, void *pC
}
devpath = (char*)(priv+1);
if(mount_path != NULL){
if(mount_path != NULL) {
strcpy(devpath, mount_path);
}
@ -945,7 +900,7 @@ It resets the devoptab_list, otherwise mounting again would throw an exception (
No memory is free'd here. Maybe a problem?!?!?
*/
void deleteDevTabsNames(){
void deleteDevTabsNames() {
const devoptab_t * devoptab = NULL;
u32 last_entry = (u32) devoptab_list[STD_MAX-1];
for (int i = 3; i < STD_MAX; i++) {
@ -953,7 +908,7 @@ void deleteDevTabsNames(){
if (devoptab) {
//log_printf("check devotab %d %08X\n",i,devoptab);
if((u32) devoptab != last_entry){
if((u32) devoptab != last_entry) {
devoptab_list[i] = (const devoptab_t *)last_entry;
//log_printf("Removed devotab %d %08X %08X %08X\n",i,devoptab,devoptab->name,devoptab->deviceData);
}
@ -961,8 +916,7 @@ void deleteDevTabsNames(){
}
}
static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd, char **mountPath)
{
static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd, char **mountPath) {
const devoptab_t *devoptab = NULL;
char name[128] = {0};
int i;
@ -981,12 +935,11 @@ static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd,
if (strcmp(name, devoptab->name) == 0) {
devoptab_list[i] = devoptab_list[0];
if(devoptab->deviceData)
{
if(devoptab->deviceData) {
sd_fat_private_t *priv = (sd_fat_private_t *)devoptab->deviceData;
if(pClient != NULL) *pClient = priv->pClient;
if(pCmd != NULL) *pCmd = priv->pCmd;
if(mountPath != NULL){
if(mountPath != NULL) {
*mountPath = (char*) malloc(strlen(priv->mount_path)+1);
if(*mountPath)
strcpy(*mountPath, priv->mount_path);
@ -1005,7 +958,7 @@ static int sd_fat_remove_device (const char *path, void **pClient, void **pCmd,
return -1;
}
s32 mount_sd_fat(const char *path){
s32 mount_sd_fat(const char *path) {
int result = -1;
// get command and client
@ -1035,14 +988,13 @@ s32 mount_sd_fat(const char *path){
return result;
}
s32 unmount_sd_fat(const char *path){
s32 unmount_sd_fat(const char *path) {
void *pClient = 0;
void *pCmd = 0;
char *mountPath = 0;
int result = sd_fat_remove_device(path, &pClient, &pCmd, &mountPath);
if(result == 0)
{
if(result == 0) {
FSOSUtils::UmountFS(pClient, pCmd, mountPath);
FSDelClient(pClient);
free(pClient);
@ -1053,10 +1005,10 @@ s32 unmount_sd_fat(const char *path){
return result;
}
s32 mount_fake(){
s32 mount_fake() {
return sd_fat_add_device("fake", NULL, NULL, NULL);
}
s32 unmount_fake(){
s32 unmount_fake() {
return sd_fat_remove_device ("fake", NULL,NULL,NULL);
}

View File

@ -9,8 +9,7 @@ extern "C" {
#endif
// original structure in the kernel that is originally 0x1270 long
typedef struct
{
typedef struct {
uint32_t version_cos_xml; // version tag from cos.xml
uint64_t os_version; // os_version from app.xml
uint64_t title_id; // title_id tag from app.xml
@ -71,8 +70,7 @@ typedef struct
// Our own cos/app.xml struct which uses only uses as much memory as really needed, since many things are just zeros in the above structure
// This structure is only 0x64 bytes long + RPX name length (dynamic up to 0x1000 theoretically)
typedef struct
{
typedef struct {
uint32_t version_cos_xml; // version tag from cos.xml
uint64_t os_version; // os_version from app.xml
uint64_t title_id; // title_id tag from app.xml

View File

@ -24,11 +24,10 @@
#include "fs/CFile.hpp"
#include "utils/StringTools.h"
typedef struct _MSG
{
u32 id;
char* msgstr;
struct _MSG *next;
typedef struct _MSG {
u32 id;
char* msgstr;
struct _MSG *next;
} MSG;
static MSG *baseMSG = 0;
@ -37,175 +36,158 @@ static MSG *baseMSG = 0;
/* Defines the so called `hashpjw' function by P.J. Weinberger
[see Aho/Sethi/Ullman, COMPILERS: Principles, Techniques and Tools,
1986, 1987 Bell Telephone Laboratories, Inc.] */
static inline u32 hash_string(const char *str_param)
{
u32 hval, g;
const char *str = str_param;
static inline u32 hash_string(const char *str_param) {
u32 hval, g;
const char *str = str_param;
/* Compute the hash value for the given string. */
hval = 0;
while (*str != '\0')
{
hval <<= 4;
hval += (u8) *str++;
g = hval & ((u32) 0xf << (HASHWORDBITS - 4));
if (g != 0)
{
hval ^= g >> (HASHWORDBITS - 8);
hval ^= g;
}
}
return hval;
/* Compute the hash value for the given string. */
hval = 0;
while (*str != '\0') {
hval <<= 4;
hval += (u8) *str++;
g = hval & ((u32) 0xf << (HASHWORDBITS - 4));
if (g != 0) {
hval ^= g >> (HASHWORDBITS - 8);
hval ^= g;
}
}
return hval;
}
/* Expand some escape sequences found in the argument string. */
static char *
expand_escape(const char *str)
{
char *retval, *rp;
const char *cp = str;
expand_escape(const char *str) {
char *retval, *rp;
const char *cp = str;
retval = (char *) malloc(strlen(str) + 1);
if (retval == NULL) return NULL;
rp = retval;
retval = (char *) malloc(strlen(str) + 1);
if (retval == NULL) return NULL;
rp = retval;
while (cp[0] != '\0' && cp[0] != '\\')
*rp++ = *cp++;
if (cp[0] == '\0') goto terminate;
do
{
while (cp[0] != '\0' && cp[0] != '\\')
*rp++ = *cp++;
if (cp[0] == '\0') goto terminate;
do {
/* Here cp[0] == '\\'. */
switch (*++cp)
{
case '\"': /* " */
*rp++ = '\"';
++cp;
break;
case 'a': /* alert */
*rp++ = '\a';
++cp;
break;
case 'b': /* backspace */
*rp++ = '\b';
++cp;
break;
case 'f': /* form feed */
*rp++ = '\f';
++cp;
break;
case 'n': /* new line */
*rp++ = '\n';
++cp;
break;
case 'r': /* carriage return */
*rp++ = '\r';
++cp;
break;
case 't': /* horizontal tab */
*rp++ = '\t';
++cp;
break;
case 'v': /* vertical tab */
*rp++ = '\v';
++cp;
break;
case '\\':
*rp = '\\';
++cp;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
int ch = *cp++ - '0';
/* Here cp[0] == '\\'. */
switch (*++cp) {
case '\"': /* " */
*rp++ = '\"';
++cp;
break;
case 'a': /* alert */
*rp++ = '\a';
++cp;
break;
case 'b': /* backspace */
*rp++ = '\b';
++cp;
break;
case 'f': /* form feed */
*rp++ = '\f';
++cp;
break;
case 'n': /* new line */
*rp++ = '\n';
++cp;
break;
case 'r': /* carriage return */
*rp++ = '\r';
++cp;
break;
case 't': /* horizontal tab */
*rp++ = '\t';
++cp;
break;
case 'v': /* vertical tab */
*rp++ = '\v';
++cp;
break;
case '\\':
*rp = '\\';
++cp;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7': {
int ch = *cp++ - '0';
if (*cp >= '0' && *cp <= '7')
{
ch *= 8;
ch += *cp++ - '0';
if (*cp >= '0' && *cp <= '7') {
ch *= 8;
ch += *cp++ - '0';
if (*cp >= '0' && *cp <= '7')
{
ch *= 8;
ch += *cp++ - '0';
}
}
*rp = ch;
}
break;
default:
*rp = '\\';
break;
}
if (*cp >= '0' && *cp <= '7') {
ch *= 8;
ch += *cp++ - '0';
}
}
*rp = ch;
}
break;
default:
*rp = '\\';
break;
}
while (cp[0] != '\0' && cp[0] != '\\')
*rp++ = *cp++;
} while (cp[0] != '\0');
while (cp[0] != '\0' && cp[0] != '\\')
*rp++ = *cp++;
} while (cp[0] != '\0');
/* Terminate string. */
terminate: *rp = '\0';
return retval;
/* Terminate string. */
terminate:
*rp = '\0';
return retval;
}
static MSG *findMSG(u32 id)
{
MSG *msg;
for (msg = baseMSG; msg; msg = msg->next)
{
if (msg->id == id) return msg;
}
return NULL;
static MSG *findMSG(u32 id) {
MSG *msg;
for (msg = baseMSG; msg; msg = msg->next) {
if (msg->id == id) return msg;
}
return NULL;
}
static MSG *setMSG(const char *msgid, const char *msgstr)
{
u32 id = hash_string(msgid);
MSG *msg = findMSG(id);
if (!msg)
{
msg = (MSG *) malloc(sizeof(MSG));
msg->id = id;
msg->msgstr = NULL;
msg->next = baseMSG;
baseMSG = msg;
}
if (msg)
{
if (msgstr)
{
if (msg->msgstr) free(msg->msgstr);
//msg->msgstr = strdup(msgstr);
msg->msgstr = expand_escape(msgstr);
}
return msg;
}
return NULL;
static MSG *setMSG(const char *msgid, const char *msgstr) {
u32 id = hash_string(msgid);
MSG *msg = findMSG(id);
if (!msg) {
msg = (MSG *) malloc(sizeof(MSG));
msg->id = id;
msg->msgstr = NULL;
msg->next = baseMSG;
baseMSG = msg;
}
if (msg) {
if (msgstr) {
if (msg->msgstr) free(msg->msgstr);
//msg->msgstr = strdup(msgstr);
msg->msgstr = expand_escape(msgstr);
}
return msg;
}
return NULL;
}
extern "C" void gettextCleanUp(void)
{
while (baseMSG)
{
MSG *nextMsg = baseMSG->next;
free(baseMSG->msgstr);
free(baseMSG);
baseMSG = nextMsg;
}
extern "C" void gettextCleanUp(void) {
while (baseMSG) {
MSG *nextMsg = baseMSG->next;
free(baseMSG->msgstr);
free(baseMSG);
baseMSG = nextMsg;
}
}
extern "C" bool gettextLoadLanguage(const char* langFile)
{
char *lastID = NULL;
gettextCleanUp();
extern "C" bool gettextLoadLanguage(const char* langFile) {
char *lastID = NULL;
gettextCleanUp();
CFile file(langFile, CFile::ReadOnly);
if (!file.isOpen())
CFile file(langFile, CFile::ReadOnly);
if (!file.isOpen())
return false;
std::string strBuffer;
@ -215,8 +197,7 @@ extern "C" bool gettextLoadLanguage(const char* langFile)
//! remove all windows crap signs
size_t position;
while(1)
{
while(1) {
position = strBuffer.find('\r');
if(position == std::string::npos)
break;
@ -224,60 +205,52 @@ extern "C" bool gettextLoadLanguage(const char* langFile)
strBuffer.erase(position, 1);
}
std::vector<std::string> lines = StringTools::stringSplit(strBuffer, "\n");
std::vector<std::string> lines = StringTools::stringSplit(strBuffer, "\n");
if(lines.empty())
return false;
if(lines.empty())
return false;
for(unsigned int i = 0; i < lines.size(); i++)
{
std::string & line = lines[i];
// lines starting with # are comments
if (line[0] == '#')
continue;
else if (strncmp(line.c_str(), "msgid \"", 7) == 0)
{
char *msgid, *end;
if (lastID)
{
free(lastID);
lastID = NULL;
}
msgid = &line[7];
end = strrchr(msgid, '"');
if (end && end - msgid > 1)
{
*end = 0;
lastID = strdup(msgid);
}
}
else if (strncmp(line.c_str(), "msgstr \"", 8) == 0)
{
char *msgstr, *end;
for(unsigned int i = 0; i < lines.size(); i++) {
std::string & line = lines[i];
// lines starting with # are comments
if (line[0] == '#')
continue;
else if (strncmp(line.c_str(), "msgid \"", 7) == 0) {
char *msgid, *end;
if (lastID) {
free(lastID);
lastID = NULL;
}
msgid = &line[7];
end = strrchr(msgid, '"');
if (end && end - msgid > 1) {
*end = 0;
lastID = strdup(msgid);
}
} else if (strncmp(line.c_str(), "msgstr \"", 8) == 0) {
char *msgstr, *end;
if (lastID == NULL) continue;
if (lastID == NULL) continue;
msgstr = &line[8];
end = strrchr(msgstr, '"');
if (end && end - msgstr > 1)
{
*end = 0;
setMSG(lastID, msgstr);
}
free(lastID);
lastID = NULL;
}
msgstr = &line[8];
end = strrchr(msgstr, '"');
if (end && end - msgstr > 1) {
*end = 0;
setMSG(lastID, msgstr);
}
free(lastID);
lastID = NULL;
}
}
return true;
}
return true;
}
extern "C" const char *gettext(const char *msgid)
{
if(!msgid) return NULL;
MSG *msg = findMSG(hash_string(msgid));
if (msg && msg->msgstr) return msg->msgstr;
return msgid;
extern "C" const char *gettext(const char *msgid) {
if(!msgid) return NULL;
MSG *msg = findMSG(hash_string(msgid));
if (msg && msg->msgstr) return msg->msgstr;
return msgid;
}

View File

@ -22,16 +22,16 @@ extern "C"
{
#endif
#define tr(s) gettext(s)
#define trNOOP(s) s
#define tr(s) gettext(s)
#define trNOOP(s) s
bool gettextLoadLanguage(const char* langFile);
void gettextCleanUp(void);
/*
* input msg = a text in ASCII
* output = the translated msg in utf-8
*/
const char *gettext(const char *msg);
bool gettextLoadLanguage(const char* langFile);
void gettextCleanUp(void);
/*
* input msg = a text in ASCII
* output = the translated msg in utf-8
*/
const char *gettext(const char *msg);
#define tr(s) gettext(s)
#define trNOOP(s) s

View File

@ -19,28 +19,23 @@
AsyncDeleter * AsyncDeleter::deleterInstance = NULL;
AsyncDeleter::AsyncDeleter()
: CThread(CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff)
, exitApplication(false)
{
: CThread(CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff)
, exitApplication(false) {
}
AsyncDeleter::~AsyncDeleter()
{
AsyncDeleter::~AsyncDeleter() {
exitApplication = true;
}
void AsyncDeleter::triggerDeleteProcess(void)
{
void AsyncDeleter::triggerDeleteProcess(void) {
if(!deleterInstance)
deleterInstance = new AsyncDeleter;
//! to trigger the event after GUI process is finished execution
//! this function is used to swap elements from one to next array
if(!deleterInstance->deleteElements.empty())
{
if(!deleterInstance->deleteElements.empty()) {
deleterInstance->deleteMutex.lock();
while(!deleterInstance->deleteElements.empty())
{
while(!deleterInstance->deleteElements.empty()) {
deleterInstance->realDeleteElements.push(deleterInstance->deleteElements.front());
deleterInstance->deleteElements.pop();
}
@ -49,15 +44,12 @@ void AsyncDeleter::triggerDeleteProcess(void)
}
}
void AsyncDeleter::executeThread(void)
{
while(!exitApplication || !realDeleteElements.empty())
{
void AsyncDeleter::executeThread(void) {
while(!exitApplication || !realDeleteElements.empty()) {
if(realDeleteElements.empty()) suspendThread();
//! delete elements that require post process deleting
//! because otherwise they would block or do invalid access on GUI thread
while(!realDeleteElements.empty())
{
while(!realDeleteElements.empty()) {
deleteMutex.lock();
AsyncDeleter::Element *element = realDeleteElements.front();
realDeleteElements.pop();

View File

@ -21,47 +21,42 @@
#include "CThread.h"
#include "CMutex.h"
class AsyncDeleter : public CThread
{
class AsyncDeleter : public CThread {
public:
static void destroyInstance()
{
if(deleterInstance != NULL){
static void destroyInstance() {
if(deleterInstance != NULL) {
delete deleterInstance;
deleterInstance = NULL;
}
}
class Element
{
class Element {
public:
Element() {}
virtual ~Element() {}
};
static void pushForDelete(AsyncDeleter::Element *e)
{
if(!deleterInstance)
static void pushForDelete(AsyncDeleter::Element *e) {
if(!deleterInstance) {
deleterInstance = new AsyncDeleter;
}
deleterInstance->deleteElements.push(e);
}
static bool deleteListEmpty()
{
if(!deleterInstance)
static bool deleteListEmpty() {
if(!deleterInstance) {
return true;
}
return deleterInstance->deleteElements.empty();
}
static bool realListEmpty()
{
if(!deleterInstance)
static bool realListEmpty() {
if(!deleterInstance) {
return true;
}
return deleterInstance->realDeleteElements.empty();
}
static void triggerDeleteProcess(void);
private:

View File

@ -20,8 +20,7 @@
#include <malloc.h>
#include <dynamic_libs/os_functions.h>
class CMutex
{
class CMutex {
public:
CMutex() {
pMutex = malloc(OS_MUTEX_SIZE);
@ -53,8 +52,7 @@ private:
void *pMutex;
};
class CMutexLock
{
class CMutexLock {
public:
CMutexLock() {
mutex.lock();

View File

@ -23,99 +23,113 @@
#include <dynamic_libs/os_functions.h>
#include "utils/logger.h"
class CThread
{
class CThread {
public:
typedef void (* Callback)(CThread *thread, void *arg);
typedef void (* Callback)(CThread *thread, void *arg);
//! constructor
CThread(s32 iAttr, s32 iPriority = 16, s32 iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL)
: pThread(NULL)
, pThreadStack(NULL)
, pCallback(callback)
, pCallbackArg(callbackArg)
{
//! save attribute assignment
iAttributes = iAttr;
//! allocate the thread
pThread = (OSThread*) memalign(8, 0x1000);
//! allocate the stack
pThreadStack = (u8 *) memalign(0x20, iStackSize);
//! constructor
CThread(s32 iAttr, s32 iPriority = 16, s32 iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL)
: pThread(NULL)
, pThreadStack(NULL)
, pCallback(callback)
, pCallbackArg(callbackArg) {
//! save attribute assignment
iAttributes = iAttr;
//! allocate the thread
pThread = (OSThread*) memalign(8, 0x1000);
//! allocate the stack
pThreadStack = (u8 *) memalign(0x20, iStackSize);
//! create the thread
if(pThread && pThreadStack)
if(pThread && pThreadStack) {
OSCreateThread(pThread, &CThread::threadCallback, 1, this, (u32)pThreadStack+iStackSize, iStackSize, iPriority, iAttributes);
}
}
}
//! destructor
virtual ~CThread() { shutdownThread(); }
//! destructor
virtual ~CThread() {
shutdownThread();
}
static CThread *create(CThread::Callback callback, void *callbackArg, s32 iAttr = eAttributeNone, s32 iPriority = 16, s32 iStackSize = 0x8000)
{
return ( new CThread(iAttr, iPriority, iStackSize, callback, callbackArg) );
}
static CThread *create(CThread::Callback callback, void *callbackArg, s32 iAttr = eAttributeNone, s32 iPriority = 16, s32 iStackSize = 0x8000) {
return ( new CThread(iAttr, iPriority, iStackSize, callback, callbackArg) );
}
//! Get thread ID
virtual void* getThread() const { return pThread; }
//! Thread entry function
virtual void executeThread(void)
{
if(pCallback)
//! Get thread ID
virtual void* getThread() const {
return pThread;
}
//! Thread entry function
virtual void executeThread(void) {
if(pCallback)
pCallback(this, pCallbackArg);
}
//! Suspend thread
virtual void suspendThread(void) { if(isThreadSuspended()) return; if(pThread) OSSuspendThread(pThread); }
//! Resume thread
virtual void resumeThread(void) { if(!isThreadSuspended()) return; if(pThread) OSResumeThread(pThread); }
//! Set thread priority
virtual void setThreadPriority(s32 prio) { if(pThread) OSSetThreadPriority(pThread, prio); }
//! Check if thread is suspended
virtual bool isThreadSuspended(void) const { if(pThread) return OSIsThreadSuspended(pThread); return false; }
//! Check if thread is terminated
virtual bool isThreadTerminated(void) const { if(pThread) return OSIsThreadTerminated(pThread); return false; }
//! Check if thread is running
virtual bool isThreadRunning(void) const { return !isThreadSuspended() && !isThreadRunning(); }
//! Shutdown thread
virtual void shutdownThread(void)
{
//! wait for thread to finish
if(pThread && !(iAttributes & eAttributeDetach))
{
while(isThreadSuspended()){
}
//! Suspend thread
virtual void suspendThread(void) {
if(isThreadSuspended()) return;
if(pThread) OSSuspendThread(pThread);
}
//! Resume thread
virtual void resumeThread(void) {
if(!isThreadSuspended()) return;
if(pThread) OSResumeThread(pThread);
}
//! Set thread priority
virtual void setThreadPriority(s32 prio) {
if(pThread) OSSetThreadPriority(pThread, prio);
}
//! Check if thread is suspended
virtual bool isThreadSuspended(void) const {
if(pThread) return OSIsThreadSuspended(pThread);
return false;
}
//! Check if thread is terminated
virtual bool isThreadTerminated(void) const {
if(pThread) return OSIsThreadTerminated(pThread);
return false;
}
//! Check if thread is running
virtual bool isThreadRunning(void) const {
return !isThreadSuspended() && !isThreadRunning();
}
//! Shutdown thread
virtual void shutdownThread(void) {
//! wait for thread to finish
if(pThread && !(iAttributes & eAttributeDetach)) {
while(isThreadSuspended()) {
resumeThread();
}
OSJoinThread(pThread, NULL);
}
//! free the thread stack buffer
if(pThreadStack){
free(pThreadStack);
OSJoinThread(pThread, NULL);
}
if(pThread)
free(pThread);
pThread = NULL;
pThreadStack = NULL;
}
//! free the thread stack buffer
if(pThreadStack) {
free(pThreadStack);
}
if(pThread) {
free(pThread);
}
pThread = NULL;
pThreadStack = NULL;
}
//! Thread attributes
enum eCThreadAttributes
{
eAttributeNone = 0x07,
eAttributeAffCore0 = 0x01,
eAttributeAffCore1 = 0x02,
eAttributeAffCore2 = 0x04,
eAttributeDetach = 0x08,
eAttributePinnedAff = 0x10
};
enum eCThreadAttributes {
eAttributeNone = 0x07,
eAttributeAffCore0 = 0x01,
eAttributeAffCore1 = 0x02,
eAttributeAffCore2 = 0x04,
eAttributeDetach = 0x08,
eAttributePinnedAff = 0x10
};
private:
static s32 threadCallback(s32 argc, void *arg)
{
//! After call to start() continue with the internal function
((CThread *) arg)->executeThread();
return 0;
}
static s32 threadCallback(s32 argc, void *arg) {
//! After call to start() continue with the internal function
((CThread *) arg)->executeThread();
return 0;
}
s32 iAttributes;
OSThread *pThread;
u8 *pThreadStack;
Callback pCallback;
void *pCallbackArg;
OSThread *pThread;
u8 *pThreadStack;
Callback pCallback;
void *pCallbackArg;
};
#endif

View File

@ -21,8 +21,8 @@
})
typedef struct _framerec {
struct _framerec *up;
void *lr;
struct _framerec *up;
void *lr;
} frame_rec, *frame_rec_t;
static const char *exception_names[] = {
@ -32,24 +32,24 @@ static const char *exception_names[] = {
};
static const char exception_print_formats[18][45] = {
"Exception type %s occurred!\n", // 0
"GPR00 %08X GPR08 %08X GPR16 %08X GPR24 %08X\n", // 1
"GPR01 %08X GPR09 %08X GPR17 %08X GPR25 %08X\n", // 2
"GPR02 %08X GPR10 %08X GPR18 %08X GPR26 %08X\n", // 3
"GPR03 %08X GPR11 %08X GPR19 %08X GPR27 %08X\n", // 4
"GPR04 %08X GPR12 %08X GPR20 %08X GPR28 %08X\n", // 5
"GPR05 %08X GPR13 %08X GPR21 %08X GPR29 %08X\n", // 6
"GPR06 %08X GPR14 %08X GPR22 %08X GPR30 %08X\n", // 7
"GPR07 %08X GPR15 %08X GPR23 %08X GPR31 %08X\n", // 8
"LR %08X SRR0 %08x SRR1 %08x\n", // 9
"DAR %08X DSISR %08X\n", // 10
"\nSTACK DUMP:", // 11
" --> ", // 12
" -->\n", // 13
"\n", // 14
"%p", // 15
"\nCODE DUMP:\n", // 16
"%p: %08X %08X %08X %08X\n", // 17
"Exception type %s occurred!\n", // 0
"GPR00 %08X GPR08 %08X GPR16 %08X GPR24 %08X\n", // 1
"GPR01 %08X GPR09 %08X GPR17 %08X GPR25 %08X\n", // 2
"GPR02 %08X GPR10 %08X GPR18 %08X GPR26 %08X\n", // 3
"GPR03 %08X GPR11 %08X GPR19 %08X GPR27 %08X\n", // 4
"GPR04 %08X GPR12 %08X GPR20 %08X GPR28 %08X\n", // 5
"GPR05 %08X GPR13 %08X GPR21 %08X GPR29 %08X\n", // 6
"GPR06 %08X GPR14 %08X GPR22 %08X GPR30 %08X\n", // 7
"GPR07 %08X GPR15 %08X GPR23 %08X GPR31 %08X\n", // 8
"LR %08X SRR0 %08x SRR1 %08x\n", // 9
"DAR %08X DSISR %08X\n", // 10
"\nSTACK DUMP:", // 11
" --> ", // 12
" -->\n", // 13
"\n", // 14
"%p", // 15
"\nCODE DUMP:\n", // 16
"%p: %08X %08X %08X %08X\n", // 17
};
static unsigned char exception_cb(void * c, unsigned char exception_type) {
@ -60,67 +60,67 @@ static unsigned char exception_cb(void * c, unsigned char exception_type) {
/*
* This part is mostly from libogc. Thanks to the devs over there.
*/
pos += sprintf(buf + pos, exception_print_formats[0], exception_names[exception_type]);
pos += sprintf(buf + pos, exception_print_formats[1], context->gpr[0], context->gpr[8], context->gpr[16], context->gpr[24]);
pos += sprintf(buf + pos, exception_print_formats[2], context->gpr[1], context->gpr[9], context->gpr[17], context->gpr[25]);
pos += sprintf(buf + pos, exception_print_formats[3], context->gpr[2], context->gpr[10], context->gpr[18], context->gpr[26]);
pos += sprintf(buf + pos, exception_print_formats[4], context->gpr[3], context->gpr[11], context->gpr[19], context->gpr[27]);
pos += sprintf(buf + pos, exception_print_formats[5], context->gpr[4], context->gpr[12], context->gpr[20], context->gpr[28]);
pos += sprintf(buf + pos, exception_print_formats[6], context->gpr[5], context->gpr[13], context->gpr[21], context->gpr[29]);
pos += sprintf(buf + pos, exception_print_formats[7], context->gpr[6], context->gpr[14], context->gpr[22], context->gpr[30]);
pos += sprintf(buf + pos, exception_print_formats[8], context->gpr[7], context->gpr[15], context->gpr[23], context->gpr[31]);
pos += sprintf(buf + pos, exception_print_formats[9], context->lr, context->srr0, context->srr1);
pos += sprintf(buf + pos, exception_print_formats[0], exception_names[exception_type]);
pos += sprintf(buf + pos, exception_print_formats[1], context->gpr[0], context->gpr[8], context->gpr[16], context->gpr[24]);
pos += sprintf(buf + pos, exception_print_formats[2], context->gpr[1], context->gpr[9], context->gpr[17], context->gpr[25]);
pos += sprintf(buf + pos, exception_print_formats[3], context->gpr[2], context->gpr[10], context->gpr[18], context->gpr[26]);
pos += sprintf(buf + pos, exception_print_formats[4], context->gpr[3], context->gpr[11], context->gpr[19], context->gpr[27]);
pos += sprintf(buf + pos, exception_print_formats[5], context->gpr[4], context->gpr[12], context->gpr[20], context->gpr[28]);
pos += sprintf(buf + pos, exception_print_formats[6], context->gpr[5], context->gpr[13], context->gpr[21], context->gpr[29]);
pos += sprintf(buf + pos, exception_print_formats[7], context->gpr[6], context->gpr[14], context->gpr[22], context->gpr[30]);
pos += sprintf(buf + pos, exception_print_formats[8], context->gpr[7], context->gpr[15], context->gpr[23], context->gpr[31]);
pos += sprintf(buf + pos, exception_print_formats[9], context->lr, context->srr0, context->srr1);
//if(exception_type == OS_EXCEPTION_DSI) {
pos += sprintf(buf + pos, exception_print_formats[10], context->ex1, context->ex0); // this freezes
//}
//if(exception_type == OS_EXCEPTION_DSI) {
pos += sprintf(buf + pos, exception_print_formats[10], context->ex1, context->ex0); // this freezes
//}
void *pc = (void*)context->srr0;
void *lr = (void*)context->lr;
void *r1 = (void*)context->gpr[1];
register uint32_t i = 0;
register frame_rec_t l,p = (frame_rec_t)lr;
register uint32_t i = 0;
register frame_rec_t l,p = (frame_rec_t)lr;
l = p;
p = r1;
if(!p)
l = p;
p = r1;
if(!p)
asm volatile("mr %0,%%r1" : "=r"(p));
pos += sprintf(buf + pos, exception_print_formats[11]);
pos += sprintf(buf + pos, exception_print_formats[11]);
for(i = 0; i < CPU_STACK_TRACE_DEPTH-1 && p->up; p = p->up, i++) {
if(i % 4)
for(i = 0; i < CPU_STACK_TRACE_DEPTH-1 && p->up; p = p->up, i++) {
if(i % 4)
pos += sprintf(buf + pos, exception_print_formats[12]);
else {
if(i > 0)
else {
if(i > 0)
pos += sprintf(buf + pos, exception_print_formats[13]);
else
else
pos += sprintf(buf + pos, exception_print_formats[14]);
}
}
switch(i) {
case 0:
if(pc)
pos += sprintf(buf + pos, exception_print_formats[15],pc);
break;
case 1:
if(!l)
l = (frame_rec_t)mfspr(8);
pos += sprintf(buf + pos, exception_print_formats[15],(void*)l);
break;
default:
pos += sprintf(buf + pos, exception_print_formats[15],(void*)(p->up->lr));
break;
}
}
switch(i) {
case 0:
if(pc)
pos += sprintf(buf + pos, exception_print_formats[15],pc);
break;
case 1:
if(!l)
l = (frame_rec_t)mfspr(8);
pos += sprintf(buf + pos, exception_print_formats[15],(void*)l);
break;
default:
pos += sprintf(buf + pos, exception_print_formats[15],(void*)(p->up->lr));
break;
}
}
//if(exception_type == OS_EXCEPTION_DSI) {
uint32_t *pAdd = (uint32_t*)context->srr0;
pos += sprintf(buf + pos, exception_print_formats[16]);
// TODO by Dimok: this was actually be 3 instead of 2 lines in libogc .... but there is just no more space anymore on the screen
for (i = 0; i < 8; i += 4)
pos += sprintf(buf + pos, exception_print_formats[17], &(pAdd[i]),pAdd[i], pAdd[i+1], pAdd[i+2], pAdd[i+3]);
//}
//if(exception_type == OS_EXCEPTION_DSI) {
uint32_t *pAdd = (uint32_t*)context->srr0;
pos += sprintf(buf + pos, exception_print_formats[16]);
// TODO by Dimok: this was actually be 3 instead of 2 lines in libogc .... but there is just no more space anymore on the screen
for (i = 0; i < 8; i += 4)
pos += sprintf(buf + pos, exception_print_formats[17], &(pAdd[i]),pAdd[i], pAdd[i+1], pAdd[i+2], pAdd[i+3]);
//}
log_print(buf);
OSFatal(buf);
return 1;

View File

@ -49,8 +49,7 @@ extern void (* MEMFreeToExpHeap)(s32 heap, void* ptr);
static s32 mem1_heap = -1;
static s32 bucket_heap = -1;
void memoryInitialize(void)
{
void memoryInitialize(void) {
s32 mem1_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_1);
u32 mem1_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(mem1_heap_handle, 4);
void *mem1_memory = MEMAllocFromFrmHeapEx(mem1_heap_handle, mem1_allocatable_size, 4);
@ -64,8 +63,7 @@ void memoryInitialize(void)
bucket_heap = MEMCreateExpHeapEx(bucket_memory, bucket_allocatable_size, 0);
}
void memoryRelease(void)
{
void memoryRelease(void) {
MEMDestroyExpHeap(mem1_heap);
MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_1), 3);
mem1_heap = -1;
@ -78,14 +76,12 @@ void memoryRelease(void)
//!-------------------------------------------------------------------------------------------
//! wraps
//!-------------------------------------------------------------------------------------------
void *__wrap_malloc(size_t size)
{
void *__wrap_malloc(size_t size) {
// pointer to a function resolve
return ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
return ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size);
}
void *__wrap_memalign(size_t align, size_t size)
{
void *__wrap_memalign(size_t align, size_t size) {
if (align < 4)
align = 4;
@ -93,38 +89,38 @@ void *__wrap_memalign(size_t align, size_t size)
return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, align);
}
void __wrap_free(void *p)
{
void __wrap_free(void *p) {
// pointer to a function resolve
if(p != 0)
((void (*)(void *))(*pMEMFreeToDefaultHeap))(p);
}
void *__wrap_calloc(size_t n, size_t size)
{
void *__wrap_calloc(size_t n, size_t size) {
void *p = __wrap_malloc(n * size);
if (p != 0) {
memset(p, 0, n * size);
}
return p;
if (p != 0) {
memset(p, 0, n * size);
}
return p;
}
size_t __wrap_malloc_usable_size(void *p)
{
size_t __wrap_malloc_usable_size(void *p) {
//! TODO: this is totally wrong and needs to be addressed
return 0x7FFFFFFF;
return 0x7FFFFFFF;
}
void *__wrap_realloc(void *ptr, size_t size)
{
void *__wrap_realloc(void *ptr, size_t size) {
void *newPtr;
if (!ptr) {
newPtr = __wrap_malloc(size);
if (!newPtr) { goto error; }
if (!newPtr) {
goto error;
}
} else {
newPtr = __wrap_malloc(size);
if (!newPtr) { goto error; }
if (!newPtr) {
goto error;
}
memcpy(newPtr, ptr, size);
@ -135,82 +131,70 @@ void *__wrap_realloc(void *ptr, size_t size)
error:
return NULL;
}
/*
void *new_ptr = __wrap_malloc(size);
if (new_ptr != 0)
{
memcpy(new_ptr, p, __wrap_malloc_usable_size(p) < size ? __wrap_malloc_usable_size(p) : size);
__wrap_free(p);
}
return new_ptr;
/*
void *new_ptr = __wrap_malloc(size);
if (new_ptr != 0)
{
memcpy(new_ptr, p, __wrap_malloc_usable_size(p) < size ? __wrap_malloc_usable_size(p) : size);
__wrap_free(p);
}
return new_ptr;
}*/
//!-------------------------------------------------------------------------------------------
//! reent versions
//!-------------------------------------------------------------------------------------------
void *__wrap__malloc_r(struct _reent *r, size_t size)
{
return __wrap_malloc(size);
void *__wrap__malloc_r(struct _reent *r, size_t size) {
return __wrap_malloc(size);
}
void *__wrap__calloc_r(struct _reent *r, size_t n, size_t size)
{
void *__wrap__calloc_r(struct _reent *r, size_t n, size_t size) {
return __wrap_calloc(n, size);
}
void *__wrap__memalign_r(struct _reent *r, size_t align, size_t size)
{
void *__wrap__memalign_r(struct _reent *r, size_t align, size_t size) {
return __wrap_memalign(align, size);
}
void __wrap__free_r(struct _reent *r, void *p)
{
void __wrap__free_r(struct _reent *r, void *p) {
__wrap_free(p);
}
size_t __wrap__malloc_usable_size_r(struct _reent *r, void *p)
{
size_t __wrap__malloc_usable_size_r(struct _reent *r, void *p) {
return __wrap_malloc_usable_size(p);
}
void *__wrap__realloc_r(struct _reent *r, void *p, size_t size)
{
void *__wrap__realloc_r(struct _reent *r, void *p, size_t size) {
return __wrap_realloc(p, size);
}
//!-------------------------------------------------------------------------------------------
//! some wrappers
//!-------------------------------------------------------------------------------------------
void * MEM2_alloc(u32 size, u32 align)
{
void * MEM2_alloc(u32 size, u32 align) {
return __wrap_memalign(align, size);
}
void MEM2_free(void *ptr)
{
void MEM2_free(void *ptr) {
__wrap_free(ptr);
}
void * MEM1_alloc(u32 size, u32 align)
{
void * MEM1_alloc(u32 size, u32 align) {
if (align < 4)
align = 4;
return MEMAllocFromExpHeapEx(mem1_heap, size, align);
}
void MEM1_free(void *ptr)
{
void MEM1_free(void *ptr) {
MEMFreeToExpHeap(mem1_heap, ptr);
}
void * MEMBucket_alloc(u32 size, u32 align)
{
void * MEMBucket_alloc(u32 size, u32 align) {
if (align < 4)
align = 4;
return MEMAllocFromExpHeapEx(bucket_heap, size, align);
}
void MEMBucket_free(void *ptr)
{
void MEMBucket_free(void *ptr) {
MEMFreeToExpHeap(bucket_heap, ptr);
}

View File

@ -38,23 +38,22 @@ bool StringTools::EndsWith(const std::string& a, const std::string& b) {
return std::equal(a.begin() + a.size() - b.size(), a.end(), b.begin());
}
const char * StringTools::byte_to_binary(s32 x){
const char * StringTools::byte_to_binary(s32 x) {
static char b[9];
b[0] = '\0';
s32 z;
for (z = 128; z > 0; z >>= 1)
{
for (z = 128; z > 0; z >>= 1) {
strcat(b, ((x & z) == z) ? "1" : "0");
}
return b;
}
std::string StringTools::removeCharFromString(std::string& input,char toBeRemoved){
std::string StringTools::removeCharFromString(std::string& input,char toBeRemoved) {
std::string output = input;
size_t position;
while(1){
while(1) {
position = output.find(toBeRemoved);
if(position == std::string::npos)
break;
@ -63,150 +62,143 @@ std::string StringTools::removeCharFromString(std::string& input,char toBeRemove
return output;
}
const char * StringTools::fmt(const char * format, ...){
static char strChar[512];
strChar[0] = 0;
char * tmp = NULL;
const char * StringTools::fmt(const char * format, ...) {
static char strChar[512];
strChar[0] = 0;
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
snprintf(strChar, sizeof(strChar), tmp);
free(tmp);
va_end(va);
return (const char *) strChar;
}
va_end(va);
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) {
snprintf(strChar, sizeof(strChar), tmp);
free(tmp);
va_end(va);
return (const char *) strChar;
}
va_end(va);
if(tmp)
free(tmp);
if(tmp)
free(tmp);
return NULL;
return NULL;
}
const wchar_t * StringTools::wfmt(const char * format, ...){
static wchar_t strWChar[512];
strWChar[0] = 0;
const wchar_t * StringTools::wfmt(const char * format, ...) {
static wchar_t strWChar[512];
strWChar[0] = 0;
if(!format)
return (const wchar_t *) strWChar;
if(!format)
return (const wchar_t *) strWChar;
if(strcmp(format, "") == 0)
return (const wchar_t *) strWChar;
if(strcmp(format, "") == 0)
return (const wchar_t *) strWChar;
char * tmp = NULL;
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
int bt;
s32 strlength = strlen(tmp);
bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512 );
free(tmp);
tmp = 0;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) {
int bt;
s32 strlength = strlen(tmp);
bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512 );
free(tmp);
tmp = 0;
if(bt > 0)
{
strWChar[bt] = 0;
return (const wchar_t *) strWChar;
}
}
va_end(va);
if(bt > 0) {
strWChar[bt] = 0;
return (const wchar_t *) strWChar;
}
}
va_end(va);
if(tmp)
free(tmp);
if(tmp)
free(tmp);
return NULL;
return NULL;
}
s32 StringTools::strprintf(std::string &str, const char * format, ...){
s32 result = 0;
char * tmp = NULL;
s32 StringTools::strprintf(std::string &str, const char * format, ...) {
s32 result = 0;
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
str = tmp;
result = str.size();
}
va_end(va);
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) {
str = tmp;
result = str.size();
}
va_end(va);
if(tmp)
free(tmp);
if(tmp)
free(tmp);
return result;
return result;
}
std::string StringTools::strfmt(const char * format, ...){
std::string str;
char * tmp = NULL;
std::string StringTools::strfmt(const char * format, ...) {
std::string str;
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
str = tmp;
}
va_end(va);
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) {
str = tmp;
}
va_end(va);
if(tmp)
free(tmp);
if(tmp)
free(tmp);
return str;
return str;
}
bool StringTools::char2wchar_t(const char * strChar, wchar_t * dest){
if(!strChar || !dest)
return false;
bool StringTools::char2wchar_t(const char * strChar, wchar_t * dest) {
if(!strChar || !dest)
return false;
int bt;
bt = mbstowcs(dest, strChar, strlen(strChar));
if (bt > 0) {
dest[bt] = 0;
return true;
}
int bt;
bt = mbstowcs(dest, strChar, strlen(strChar));
if (bt > 0) {
dest[bt] = 0;
return true;
}
return false;
return false;
}
s32 StringTools::strtokcmp(const char * string, const char * compare, const char * separator){
if(!string || !compare)
return -1;
s32 StringTools::strtokcmp(const char * string, const char * compare, const char * separator) {
if(!string || !compare)
return -1;
char TokCopy[512];
strncpy(TokCopy, compare, sizeof(TokCopy));
TokCopy[511] = '\0';
char TokCopy[512];
strncpy(TokCopy, compare, sizeof(TokCopy));
TokCopy[511] = '\0';
char * strTok = strtok(TokCopy, separator);
char * strTok = strtok(TokCopy, separator);
while (strTok != NULL)
{
if (strcasecmp(string, strTok) == 0)
{
return 0;
}
strTok = strtok(NULL,separator);
}
while (strTok != NULL) {
if (strcasecmp(string, strTok) == 0) {
return 0;
}
strTok = strtok(NULL,separator);
}
return -1;
return -1;
}
s32 StringTools::strextcmp(const char * string, const char * extension, char seperator){
if(!string || !extension)
return -1;
s32 StringTools::strextcmp(const char * string, const char * extension, char seperator) {
if(!string || !extension)
return -1;
char *ptr = strrchr(string, seperator);
if(!ptr)
return -1;
char *ptr = strrchr(string, seperator);
if(!ptr)
return -1;
return strcasecmp(ptr + 1, extension);
return strcasecmp(ptr + 1, extension);
}
std::vector<std::string> StringTools::stringSplit(const std::string & inValue, const std::string & splitter){
std::vector<std::string> StringTools::stringSplit(const std::string & inValue, const std::string & splitter) {
std::string value = inValue;
std::vector<std::string> result;
while (true) {

View File

@ -21,17 +21,17 @@ TCPServer::TCPServer(s32 port,s32 priority) {
TCPServer::~TCPServer() {
CloseSockets();
DEBUG_FUNCTION_LINE("Thread will be closed\n");
//DEBUG_FUNCTION_LINE("Thread will be closed\n");
exitThread = 1;
ICInvalidateRange((void*)&exitThread, 4);
DCFlushRange((void*)&exitThread, 4);
if(pThread != NULL) {
DEBUG_FUNCTION_LINE("Deleting it!\n");
//DEBUG_FUNCTION_LINE("Deleting it!\n");
delete pThread;
}
DEBUG_FUNCTION_LINE("Thread done\n");
//DEBUG_FUNCTION_LINE("Thread done\n");
pThread = NULL;
}

View File

@ -55,7 +55,7 @@ u32 vpadbase_handle_internal = 0;
* Patches a function that is loaded at the start of each application. Its not required to restore, at least when they are really dynamic.
* "normal" functions should be patch with the normal patcher. Current Code by Maschell with the help of dimok. Orignal code by Chadderz.
*/
void PatchInvidualMethodHooks(hooks_magic_t method_hooks[],s32 hook_information_size, volatile u32 dynamic_method_calls[]){
void PatchInvidualMethodHooks(hooks_magic_t method_hooks[],s32 hook_information_size, volatile u32 dynamic_method_calls[]) {
InitAcquireOS();
resetLibs();
@ -69,14 +69,13 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[],s32 hook_information_
u32 my_instr_len = 6;
u32 instr_len = my_instr_len + skip_instr;
u32 flush_len = 4*instr_len;
for(s32 i = 0; i < method_hooks_count; i++)
{
for(s32 i = 0; i < method_hooks_count; i++) {
DEBUG_FUNCTION_LINE("Patching %s ...",method_hooks[i].functionName);
if(method_hooks[i].functionType == STATIC_FUNCTION && method_hooks[i].alreadyPatched == 1){
if(isDynamicFunction((u32)OSEffectiveToPhysical((void*)method_hooks[i].realAddr))){
if(method_hooks[i].functionType == STATIC_FUNCTION && method_hooks[i].alreadyPatched == 1) {
if(isDynamicFunction((u32)OSEffectiveToPhysical((void*)method_hooks[i].realAddr))) {
log_printf("The function %s is a dynamic function. Please fix that <3\n", method_hooks[i].functionName);
method_hooks[i].functionType = DYNAMIC_FUNCTION;
}else{
} else {
log_printf("Skipping %s, its already patched\n", method_hooks[i].functionName);
space += instr_len;
continue;
@ -89,23 +88,27 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[],s32 hook_information_
u32 real_addr = GetAddressOfFunction(method_hooks[i].functionName,method_hooks[i].library);
if(!real_addr){
if(!real_addr) {
log_printf("\n");
DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s\n", method_hooks[i].functionName);
space += instr_len;
continue;
}
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("%s is located at %08X!\n", method_hooks[i].functionName,real_addr);}
physical = (u32)OSEffectiveToPhysical((void*)real_addr);
if(!physical){
log_printf("Error. Something is wrong with the physical address\n");
space += instr_len;
continue;
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("%s is located at %08X!\n", method_hooks[i].functionName,real_addr);
}
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("%s physical is located at %08X!\n", method_hooks[i].functionName,physical);}
physical = (u32)OSEffectiveToPhysical((void*)real_addr);
if(!physical) {
log_printf("Error. Something is wrong with the physical address\n");
space += instr_len;
continue;
}
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("%s physical is located at %08X!\n", method_hooks[i].functionName,physical);
}
*(volatile u32 *)(call_addr) = (u32)(space) - CODE_RW_BASE_OFFSET;
@ -114,14 +117,17 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[],s32 hook_information_
space++;
//Only works if skip_instr == 1
if(skip_instr == 1){
if(skip_instr == 1) {
// fill the restore instruction section
method_hooks[i].realAddr = real_addr;
method_hooks[i].restoreInstruction = *(space-1);
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("method_hooks[i].realAddr = %08X!\n", method_hooks[i].realAddr);}
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("method_hooks[i].restoreInstruction = %08X!\n",method_hooks[i].restoreInstruction) ;}
}
else{
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("method_hooks[i].realAddr = %08X!\n", method_hooks[i].realAddr);
}
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("method_hooks[i].restoreInstruction = %08X!\n",method_hooks[i].restoreInstruction) ;
}
} else {
log_printf("Error. Can't save %s for restoring!\n", method_hooks[i].functionName);
}
@ -165,42 +171,42 @@ void PatchInvidualMethodHooks(hooks_magic_t method_hooks[],s32 hook_information_
/* ****************************************************************** */
/* RESTORE ORIGINAL INSTRUCTIONS */
/* ****************************************************************** */
void RestoreInvidualInstructions(hooks_magic_t method_hooks[],s32 hook_information_size){
void RestoreInvidualInstructions(hooks_magic_t method_hooks[],s32 hook_information_size) {
InitAcquireOS();
resetLibs();
DEBUG_FUNCTION_LINE("Restoring given functions!\n");
s32 method_hooks_count = hook_information_size;
for(s32 i = 0; i < method_hooks_count; i++)
{
for(s32 i = 0; i < method_hooks_count; i++) {
DEBUG_FUNCTION_LINE("Restoring %s... ",method_hooks[i].functionName);
if(method_hooks[i].restoreInstruction == 0 || method_hooks[i].realAddr == 0){
if(method_hooks[i].restoreInstruction == 0 || method_hooks[i].realAddr == 0) {
log_printf("I dont have the information for the restore =( skip\n");
continue;
}
u32 real_addr = GetAddressOfFunction(method_hooks[i].functionName,method_hooks[i].library);
if(!real_addr){
if(!real_addr) {
log_printf("OSDynLoad_FindExport failed for %s\n", method_hooks[i].functionName);
continue;
}
u32 physical = (u32)OSEffectiveToPhysical((void*)real_addr);
if(!physical){
if(!physical) {
log_printf("Something is wrong with the physical address\n");
continue;
}
if(isDynamicFunction(physical))
{
log_printf("Its a dynamic function. We don't need to restore it!\n",method_hooks[i].functionName);
}
else
{
if(isDynamicFunction(physical)) {
log_printf("Its a dynamic function. We don't need to restore it!\n",method_hooks[i].functionName);
} else {
physical = (u32)OSEffectiveToPhysical((void*)method_hooks[i].realAddr); //When its an static function, we need to use the old location
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("Restoring %08X to %08X\n",(u32)method_hooks[i].restoreInstruction,physical);}
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("Restoring %08X to %08X\n",(u32)method_hooks[i].restoreInstruction,physical);
}
SC0x25_KernelCopyData(physical,(u32)&method_hooks[i].restoreInstruction , 4);
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("ICInvalidateRange %08X\n",(void*)method_hooks[i].realAddr);}
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("ICInvalidateRange %08X\n",(void*)method_hooks[i].realAddr);
}
ICInvalidateRange((void*)method_hooks[i].realAddr, 4);
log_printf("done\n");
}
@ -210,28 +216,23 @@ void RestoreInvidualInstructions(hooks_magic_t method_hooks[],s32 hook_informati
DEBUG_FUNCTION_LINE("Done with restoring given functions!\n");
}
s32 isDynamicFunction(u32 physicalAddress){
if((physicalAddress & 0x80000000) == 0x80000000){
s32 isDynamicFunction(u32 physicalAddress) {
if((physicalAddress & 0x80000000) == 0x80000000) {
return 1;
}
return 0;
}
u32 GetAddressOfFunction(const char * functionName,u32 library){
u32 GetAddressOfFunction(const char * functionName,u32 library) {
u32 real_addr = 0;
if(strcmp(functionName, "OSDynLoad_Acquire") == 0)
{
if(strcmp(functionName, "OSDynLoad_Acquire") == 0) {
memcpy(&real_addr, &OSDynLoad_Acquire, 4);
return real_addr;
}
else if(strcmp(functionName, "LiWaitOneChunk") == 0)
{
} else if(strcmp(functionName, "LiWaitOneChunk") == 0) {
real_addr = (u32)addr_LiWaitOneChunk;
return real_addr;
}
else if(strcmp(functionName, "LiBounceOneChunk") == 0)
{
} else if(strcmp(functionName, "LiBounceOneChunk") == 0) {
//! not required on firmwares above 3.1.0
if(OS_FIRMWARE >= 400)
return 0;
@ -242,156 +243,282 @@ u32 GetAddressOfFunction(const char * functionName,u32 library){
}
u32 rpl_handle = 0;
if(library == LIB_CORE_INIT){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_CORE_INIT\n", functionName);}
if(coreinit_handle_internal == 0){OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle_internal);}
if(coreinit_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_CORE_INIT failed to acquire\n"); return 0;}
if(library == LIB_CORE_INIT) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_CORE_INIT\n", functionName);
}
if(coreinit_handle_internal == 0) {
OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle_internal);
}
if(coreinit_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_CORE_INIT failed to acquire\n");
return 0;
}
rpl_handle = coreinit_handle_internal;
}
else if(library == LIB_NSYSNET){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_NSYSNET\n", functionName);}
if(nsysnet_handle_internal == 0){OSDynLoad_Acquire("nsysnet.rpl", &nsysnet_handle_internal);}
if(nsysnet_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_NSYSNET failed to acquire\n"); return 0;}
} else if(library == LIB_NSYSNET) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_NSYSNET\n", functionName);
}
if(nsysnet_handle_internal == 0) {
OSDynLoad_Acquire("nsysnet.rpl", &nsysnet_handle_internal);
}
if(nsysnet_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_NSYSNET failed to acquire\n");
return 0;
}
rpl_handle = nsysnet_handle_internal;
}
else if(library == LIB_GX2){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_GX2\n", functionName);}
if(gx2_handle_internal == 0){OSDynLoad_Acquire("gx2.rpl", &gx2_handle_internal);}
if(gx2_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_GX2 failed to acquire\n"); return 0;}
} else if(library == LIB_GX2) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_GX2\n", functionName);
}
if(gx2_handle_internal == 0) {
OSDynLoad_Acquire("gx2.rpl", &gx2_handle_internal);
}
if(gx2_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_GX2 failed to acquire\n");
return 0;
}
rpl_handle = gx2_handle_internal;
}
else if(library == LIB_AOC){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_AOC\n", functionName);}
if(aoc_handle_internal == 0){OSDynLoad_Acquire("nn_aoc.rpl", &aoc_handle_internal);}
if(aoc_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_AOC failed to acquire\n"); return 0;}
} else if(library == LIB_AOC) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_AOC\n", functionName);
}
if(aoc_handle_internal == 0) {
OSDynLoad_Acquire("nn_aoc.rpl", &aoc_handle_internal);
}
if(aoc_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_AOC failed to acquire\n");
return 0;
}
rpl_handle = aoc_handle_internal;
}
else if(library == LIB_AX){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_AX\n", functionName);}
if(sound_handle_internal == 0){OSDynLoad_Acquire("sndcore2.rpl", &sound_handle_internal);}
if(sound_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_AX failed to acquire\n"); return 0;}
} else if(library == LIB_AX) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_AX\n", functionName);
}
if(sound_handle_internal == 0) {
OSDynLoad_Acquire("sndcore2.rpl", &sound_handle_internal);
}
if(sound_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_AX failed to acquire\n");
return 0;
}
rpl_handle = sound_handle_internal;
}
else if(library == LIB_AX_OLD){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_AX_OLD\n", functionName);}
if(sound_handle_internal_old == 0){OSDynLoad_Acquire("snd_core.rpl", &sound_handle_internal_old);}
if(sound_handle_internal_old == 0){DEBUG_FUNCTION_LINE("LIB_AX_OLD failed to acquire\n"); return 0;}
} else if(library == LIB_AX_OLD) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_AX_OLD\n", functionName);
}
if(sound_handle_internal_old == 0) {
OSDynLoad_Acquire("snd_core.rpl", &sound_handle_internal_old);
}
if(sound_handle_internal_old == 0) {
DEBUG_FUNCTION_LINE("LIB_AX_OLD failed to acquire\n");
return 0;
}
rpl_handle = sound_handle_internal_old;
}
else if(library == LIB_FS){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_FS\n", functionName);}
if(coreinit_handle_internal == 0){OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle_internal);}
if(coreinit_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_FS failed to acquire\n"); return 0;}
} else if(library == LIB_FS) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_FS\n", functionName);
}
if(coreinit_handle_internal == 0) {
OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle_internal);
}
if(coreinit_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_FS failed to acquire\n");
return 0;
}
rpl_handle = coreinit_handle_internal;
}
else if(library == LIB_OS){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_OS\n", functionName);}
if(coreinit_handle_internal == 0){OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle_internal);}
if(coreinit_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_OS failed to acquire\n"); return 0;}
} else if(library == LIB_OS) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_OS\n", functionName);
}
if(coreinit_handle_internal == 0) {
OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle_internal);
}
if(coreinit_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_OS failed to acquire\n");
return 0;
}
rpl_handle = coreinit_handle_internal;
}
else if(library == LIB_PADSCORE){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_PADSCORE\n", functionName);}
if(padscore_handle_internal == 0){OSDynLoad_Acquire("padscore.rpl", &padscore_handle_internal);}
if(padscore_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_PADSCORE failed to acquire\n"); return 0;}
} else if(library == LIB_PADSCORE) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_PADSCORE\n", functionName);
}
if(padscore_handle_internal == 0) {
OSDynLoad_Acquire("padscore.rpl", &padscore_handle_internal);
}
if(padscore_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_PADSCORE failed to acquire\n");
return 0;
}
rpl_handle = padscore_handle_internal;
}
else if(library == LIB_SOCKET){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_SOCKET\n", functionName);}
if(nsysnet_handle_internal == 0){OSDynLoad_Acquire("nsysnet.rpl", &nsysnet_handle_internal);}
if(nsysnet_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_SOCKET failed to acquire\n"); return 0;}
} else if(library == LIB_SOCKET) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_SOCKET\n", functionName);
}
if(nsysnet_handle_internal == 0) {
OSDynLoad_Acquire("nsysnet.rpl", &nsysnet_handle_internal);
}
if(nsysnet_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_SOCKET failed to acquire\n");
return 0;
}
rpl_handle = nsysnet_handle_internal;
}
else if(library == LIB_SYS){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_SYS\n", functionName);}
if(sysapp_handle_internal == 0){OSDynLoad_Acquire("sysapp.rpl", &sysapp_handle_internal);}
if(sysapp_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_SYS failed to acquire\n"); return 0;}
} else if(library == LIB_SYS) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_SYS\n", functionName);
}
if(sysapp_handle_internal == 0) {
OSDynLoad_Acquire("sysapp.rpl", &sysapp_handle_internal);
}
if(sysapp_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_SYS failed to acquire\n");
return 0;
}
rpl_handle = sysapp_handle_internal;
}
else if(library == LIB_VPAD){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_VPAD\n", functionName);}
if(vpad_handle_internal == 0){ OSDynLoad_Acquire("vpad.rpl", &vpad_handle_internal);}
if(vpad_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_VPAD failed to acquire\n"); return 0;}
} else if(library == LIB_VPAD) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_VPAD\n", functionName);
}
if(vpad_handle_internal == 0) {
OSDynLoad_Acquire("vpad.rpl", &vpad_handle_internal);
}
if(vpad_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_VPAD failed to acquire\n");
return 0;
}
rpl_handle = vpad_handle_internal;
}
else if(library == LIB_NN_ACP){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_NN_ACP\n", functionName);}
if(acp_handle_internal == 0){OSDynLoad_Acquire("nn_acp.rpl", &acp_handle_internal);}
if(acp_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_NN_ACP failed to acquire\n"); return 0;}
} else if(library == LIB_NN_ACP) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_NN_ACP\n", functionName);
}
if(acp_handle_internal == 0) {
OSDynLoad_Acquire("nn_acp.rpl", &acp_handle_internal);
}
if(acp_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_NN_ACP failed to acquire\n");
return 0;
}
rpl_handle = acp_handle_internal;
}
else if(library == LIB_SYSHID){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_SYSHID\n", functionName);}
if(syshid_handle_internal == 0){OSDynLoad_Acquire("nsyshid.rpl", &syshid_handle_internal);}
if(syshid_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_SYSHID failed to acquire\n"); return 0;}
} else if(library == LIB_SYSHID) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_SYSHID\n", functionName);
}
if(syshid_handle_internal == 0) {
OSDynLoad_Acquire("nsyshid.rpl", &syshid_handle_internal);
}
if(syshid_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_SYSHID failed to acquire\n");
return 0;
}
rpl_handle = syshid_handle_internal;
}
else if(library == LIB_VPADBASE){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_VPADBASE\n", functionName);}
if(vpadbase_handle_internal == 0){OSDynLoad_Acquire("vpadbase.rpl", &vpadbase_handle_internal);}
if(vpadbase_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_VPADBASE failed to acquire\n"); return 0;}
} else if(library == LIB_VPADBASE) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_VPADBASE\n", functionName);
}
if(vpadbase_handle_internal == 0) {
OSDynLoad_Acquire("vpadbase.rpl", &vpadbase_handle_internal);
}
if(vpadbase_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_VPADBASE failed to acquire\n");
return 0;
}
rpl_handle = vpadbase_handle_internal;
}
else if(library == LIB_PROC_UI){
if(DEBUG_LOG_DYN){DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_PROC_UI\n", functionName);}
if(proc_ui_handle_internal == 0){OSDynLoad_Acquire("proc_ui.rpl", &proc_ui_handle_internal);}
if(proc_ui_handle_internal == 0){DEBUG_FUNCTION_LINE("LIB_PROC_UI failed to acquire\n"); return 0;}
} else if(library == LIB_PROC_UI) {
if(DEBUG_LOG_DYN) {
DEBUG_FUNCTION_LINE("FindExport of %s! From LIB_PROC_UI\n", functionName);
}
if(proc_ui_handle_internal == 0) {
OSDynLoad_Acquire("proc_ui.rpl", &proc_ui_handle_internal);
}
if(proc_ui_handle_internal == 0) {
DEBUG_FUNCTION_LINE("LIB_PROC_UI failed to acquire\n");
return 0;
}
rpl_handle = proc_ui_handle_internal;
}
else if(library == LIB_NTAG){
if(DEBUG_LOG_DYN){log_printf("FindExport of %s! From LIB_NTAG\n", functionName);}
if(ntag_handle_internal == 0){OSDynLoad_Acquire("ntag.rpl", &ntag_handle_internal);}
if(ntag_handle_internal == 0){log_print("LIB_NTAG failed to acquire\n"); return 0;}
} else if(library == LIB_NTAG) {
if(DEBUG_LOG_DYN) {
log_printf("FindExport of %s! From LIB_NTAG\n", functionName);
}
if(ntag_handle_internal == 0) {
OSDynLoad_Acquire("ntag.rpl", &ntag_handle_internal);
}
if(ntag_handle_internal == 0) {
log_print("LIB_NTAG failed to acquire\n");
return 0;
}
rpl_handle = ntag_handle_internal;
}
else if(library == LIB_NFP){
if(DEBUG_LOG_DYN){log_printf("FindExport of %s! From LIB_NFP\n", functionName);}
if(nfp_handle_internal == 0){OSDynLoad_Acquire("nn_nfp.rpl", &nfp_handle_internal);}
if(nfp_handle_internal == 0){log_print("LIB_NFP failed to acquire\n"); return 0;}
} else if(library == LIB_NFP) {
if(DEBUG_LOG_DYN) {
log_printf("FindExport of %s! From LIB_NFP\n", functionName);
}
if(nfp_handle_internal == 0) {
OSDynLoad_Acquire("nn_nfp.rpl", &nfp_handle_internal);
}
if(nfp_handle_internal == 0) {
log_print("LIB_NFP failed to acquire\n");
return 0;
}
rpl_handle = nfp_handle_internal;
}
else if(library == LIB_SAVE){
if(DEBUG_LOG_DYN){log_printf("FindExport of %s! From LIB_SAVE\n", functionName);}
if(nn_save_handle_internal == 0){OSDynLoad_Acquire("nn_save.rpl", &nn_save_handle_internal);}
if(nn_save_handle_internal == 0){log_print("LIB_SAVE failed to acquire\n"); return 0;}
} else if(library == LIB_SAVE) {
if(DEBUG_LOG_DYN) {
log_printf("FindExport of %s! From LIB_SAVE\n", functionName);
}
if(nn_save_handle_internal == 0) {
OSDynLoad_Acquire("nn_save.rpl", &nn_save_handle_internal);
}
if(nn_save_handle_internal == 0) {
log_print("LIB_SAVE failed to acquire\n");
return 0;
}
rpl_handle = nn_save_handle_internal;
}
else if(library == LIB_ACT){
if(DEBUG_LOG_DYN){log_printf("FindExport of %s! From LIB_ACT\n", functionName);}
if(nn_act_handle_internal == 0){OSDynLoad_Acquire("nn_act.rpl", &nn_act_handle_internal);}
if(nn_act_handle_internal == 0){log_print("LIB_ACT failed to acquire\n"); return 0;}
} else if(library == LIB_ACT) {
if(DEBUG_LOG_DYN) {
log_printf("FindExport of %s! From LIB_ACT\n", functionName);
}
if(nn_act_handle_internal == 0) {
OSDynLoad_Acquire("nn_act.rpl", &nn_act_handle_internal);
}
if(nn_act_handle_internal == 0) {
log_print("LIB_ACT failed to acquire\n");
return 0;
}
rpl_handle = nn_act_handle_internal;
}
else if(library == LIB_NIM){
if(DEBUG_LOG_DYN){log_printf("FindExport of %s! From LIB_NIM\n", functionName);}
if(nn_nim_handle_internal == 0){OSDynLoad_Acquire("nn_nim.rpl", &nn_nim_handle_internal);}
if(nn_nim_handle_internal == 0){log_print("LIB_NIM failed to acquire\n"); return 0;}
} else if(library == LIB_NIM) {
if(DEBUG_LOG_DYN) {
log_printf("FindExport of %s! From LIB_NIM\n", functionName);
}
if(nn_nim_handle_internal == 0) {
OSDynLoad_Acquire("nn_nim.rpl", &nn_nim_handle_internal);
}
if(nn_nim_handle_internal == 0) {
log_print("LIB_NIM failed to acquire\n");
return 0;
}
rpl_handle = nn_nim_handle_internal;
}
if(!rpl_handle){
if(!rpl_handle) {
DEBUG_FUNCTION_LINE("Failed to find the RPL handle for %s\n", functionName);
return 0;
}
OSDynLoad_FindExport(rpl_handle, 0, functionName, &real_addr);
if(!real_addr){
if(!real_addr) {
OSDynLoad_FindExport(rpl_handle, 1, functionName, &real_addr);
if(!real_addr){
if(!real_addr) {
DEBUG_FUNCTION_LINE("OSDynLoad_FindExport failed for %s\n", functionName);
return 0;
}
}
if((library == LIB_NN_ACP) && (u32)(*(volatile u32*)(real_addr) & 0x48000002) == 0x48000000)
{
if((library == LIB_NN_ACP) && (u32)(*(volatile u32*)(real_addr) & 0x48000002) == 0x48000000) {
u32 address_diff = (u32)(*(volatile u32*)(real_addr) & 0x03FFFFFC);
if((address_diff & 0x03000000) == 0x03000000) {
address_diff |= 0xFC000000;
}
real_addr += (s32)address_diff;
if((u32)(*(volatile u32*)(real_addr) & 0x48000002) == 0x48000000){
if((u32)(*(volatile u32*)(real_addr) & 0x48000002) == 0x48000000) {
return 0;
}
}
@ -399,7 +526,7 @@ u32 GetAddressOfFunction(const char * functionName,u32 library){
return real_addr;
}
void resetLibs(){
void resetLibs() {
acp_handle_internal = 0;
aoc_handle_internal = 0;
sound_handle_internal = 0;

View File

@ -11,24 +11,24 @@ static int log_socket __attribute__((section(".data")))= -1;
static struct sockaddr_in connect_addr __attribute__((section(".data")));
static volatile int log_lock __attribute__((section(".data"))) = 0;
void log_init_(){
void log_init_() {
InitOSFunctionPointers();
InitSocketFunctionPointers();
int broadcastEnable = 1;
log_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (log_socket < 0)
return;
log_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (log_socket < 0)
return;
setsockopt(log_socket, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
memset(&connect_addr, 0, sizeof(struct sockaddr_in));
connect_addr.sin_family = AF_INET;
connect_addr.sin_port = 4405;
memset(&connect_addr, 0, sizeof(struct sockaddr_in));
connect_addr.sin_family = AF_INET;
connect_addr.sin_port = 4405;
connect_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
}
void log_print_(const char *str){
void log_print_(const char *str) {
// socket is always 0 initially as it is in the BSS
if(log_socket < 0) {
return;
@ -53,34 +53,32 @@ void log_print_(const char *str){
log_lock = 0;
}
void OSFatal_printf(const char *format, ...){
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp){
void OSFatal_printf(const char *format, ...) {
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) {
OSFatal(tmp);
}
va_end(va);
}
va_end(va);
}
void log_printf_(const char *format, ...)
{
void log_printf_(const char *format, ...) {
if(log_socket < 0) {
return;
}
char * tmp = NULL;
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp) {
log_print_(tmp);
}
va_end(va);
}
va_end(va);
if(tmp)
free(tmp);
if(tmp)
free(tmp);
}

View File

@ -24,7 +24,7 @@ void dumpHex(const void* data, size_t size) {
log_printf(" ");
if ((i+1) % 16 == 0) {
log_printf("| %s \n", ascii);
if(i + 1 < size){
if(i + 1 < size) {
DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1,i+1);
}
} else if (i+1 == size) {