diff --git a/src/fs/CFile.cpp b/src/fs/CFile.cpp index f6bef9d..e2f9c4a 100644 --- a/src/fs/CFile.cpp +++ b/src/fs/CFile.cpp @@ -11,12 +11,12 @@ CFile::CFile() { pos = 0; } -CFile::CFile(const std::string & filepath, eOpenTypes mode) { +CFile::CFile(const std::string &filepath, eOpenTypes mode) { iFd = -1; this->open(filepath, mode); } -CFile::CFile(const uint8_t * mem, int32_t size) { +CFile::CFile(const uint8_t *mem, int32_t size) { iFd = -1; this->open(mem, size); } @@ -25,7 +25,7 @@ CFile::~CFile() { this->close(); } -int32_t CFile::open(const std::string & filepath, eOpenTypes mode) { +int32_t CFile::open(const std::string &filepath, eOpenTypes mode) { this->close(); int32_t openMode = 0; @@ -33,20 +33,20 @@ int32_t CFile::open(const std::string & filepath, eOpenTypes mode) { // This depend on the devoptab implementation. // see https://github.com/devkitPro/wut/blob/master/libraries/wutdevoptab/devoptab_fs_open.c#L21 fpr reference - switch(mode) { - default: - case ReadOnly: // file must exist - openMode = O_RDONLY; - break; - case WriteOnly: // file will be created / zerod - openMode = O_TRUNC | O_CREAT | O_WRONLY; - break; - case ReadWrite: // file must exist - openMode = O_RDWR; - break; - case Append: // append to file, file will be created if missing. write only - openMode = O_CREAT | O_APPEND | O_WRONLY; - break; + switch (mode) { + default: + case ReadOnly: // file must exist + openMode = O_RDONLY; + break; + case WriteOnly: // file will be created / zerod + openMode = O_TRUNC | O_CREAT | O_WRONLY; + break; + case ReadWrite: // file must exist + openMode = O_RDWR; + break; + case Append: // append to file, file will be created if missing. write only + openMode = O_CREAT | O_APPEND | O_WRONLY; + break; } //! Using fopen works only on the first launch as expected @@ -54,7 +54,7 @@ int32_t CFile::open(const std::string & filepath, eOpenTypes mode) { //! 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) + if (iFd < 0) return iFd; @@ -64,7 +64,7 @@ int32_t CFile::open(const std::string & filepath, eOpenTypes mode) { return 0; } -int32_t CFile::open(const uint8_t * mem, int32_t size) { +int32_t CFile::open(const uint8_t *mem, int32_t size) { this->close(); mem_file = mem; @@ -74,7 +74,7 @@ int32_t CFile::open(const uint8_t * mem, int32_t size) { } void CFile::close() { - if(iFd >= 0) + if (iFd >= 0) ::close(iFd); iFd = -1; @@ -83,24 +83,24 @@ void CFile::close() { pos = 0; } -int32_t CFile::read(uint8_t * ptr, size_t size) { - if(iFd >= 0) { - int32_t ret = ::read(iFd, ptr,size); - if(ret > 0) +int32_t CFile::read(uint8_t *ptr, size_t size) { + if (iFd >= 0) { + int32_t ret = ::read(iFd, ptr, size); + if (ret > 0) pos += ret; return ret; } int32_t readsize = size; - if(readsize > (int64_t) (filesize-pos)) - readsize = filesize-pos; + if (readsize > (int64_t) (filesize - pos)) + readsize = filesize - pos; - if(readsize <= 0) + if (readsize <= 0) return readsize; - if(mem_file != NULL) { - memcpy(ptr, mem_file+pos, readsize); + if (mem_file != NULL) { + memcpy(ptr, mem_file + pos, readsize); pos += readsize; return readsize; } @@ -108,12 +108,12 @@ int32_t CFile::read(uint8_t * ptr, size_t size) { return -1; } -int32_t CFile::write(const uint8_t * ptr, size_t size) { - if(iFd >= 0) { +int32_t CFile::write(const uint8_t *ptr, size_t size) { + if (iFd >= 0) { size_t done = 0; - while(done < size) { + while (done < size) { int32_t ret = ::write(iFd, ptr, size - done); - if(ret <= 0) + if (ret <= 0) return ret; ptr += ret; @@ -130,25 +130,25 @@ int32_t CFile::seek(long int offset, int32_t origin) { int32_t ret = 0; int64_t newPos = pos; - if(origin == SEEK_SET) { + if (origin == SEEK_SET) { newPos = offset; - } else if(origin == SEEK_CUR) { + } else if (origin == SEEK_CUR) { newPos += offset; - } else if(origin == SEEK_END) { - newPos = filesize+offset; + } else if (origin == SEEK_END) { + newPos = filesize + offset; } - if(newPos < 0) { + if (newPos < 0) { pos = 0; } else { pos = newPos; } - if(iFd >= 0) + if (iFd >= 0) ret = ::lseek(iFd, pos, SEEK_SET); - if(mem_file != NULL) { - if(pos > filesize) { + if (mem_file != NULL) { + if (pos > filesize) { pos = filesize; } } @@ -163,8 +163,8 @@ int32_t CFile::fwrite(const char *format, ...) { va_list va; va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { - result = this->write((uint8_t *)tmp, strlen(tmp)); + if ((vsprintf(tmp, format, va) >= 0)) { + result = this->write((uint8_t *) tmp, strlen(tmp)); } va_end(va); diff --git a/src/fs/CFile.hpp b/src/fs/CFile.hpp index 8816a49..6c0421b 100644 --- a/src/fs/CFile.hpp +++ b/src/fs/CFile.hpp @@ -18,18 +18,22 @@ public: }; CFile(); - CFile(const std::string & filepath, eOpenTypes mode); - CFile(const uint8_t * memory, int32_t memsize); + + CFile(const std::string &filepath, eOpenTypes mode); + + CFile(const uint8_t *memory, int32_t memsize); + virtual ~CFile(); - int32_t open(const std::string & filepath, eOpenTypes mode); - int32_t open(const uint8_t * memory, int32_t memsize); + int32_t open(const std::string &filepath, eOpenTypes mode); + + int32_t open(const uint8_t *memory, int32_t memsize); BOOL isOpen() const { - if(iFd >= 0) + if (iFd >= 0) return true; - if(mem_file) + if (mem_file) return true; return false; @@ -37,23 +41,29 @@ public: void close(); - int32_t read(uint8_t * ptr, size_t size); - int32_t write(const uint8_t * ptr, size_t size); + int32_t read(uint8_t *ptr, size_t size); + + int32_t write(const uint8_t *ptr, size_t size); + int32_t fwrite(const char *format, ...); + int32_t seek(long int offset, int32_t origin); + uint64_t tell() { return pos; }; + uint64_t size() { return filesize; }; + void rewind() { this->seek(0, SEEK_SET); }; protected: int32_t iFd; - const uint8_t * mem_file; + const uint8_t *mem_file; uint64_t filesize; uint64_t pos; }; diff --git a/src/fs/DirList.cpp b/src/fs/DirList.cpp index 6b98c49..b92437f 100644 --- a/src/fs/DirList.cpp +++ b/src/fs/DirList.cpp @@ -42,7 +42,7 @@ DirList::DirList() { Depth = 0; } -DirList::DirList(const std::string & path, const char *filter, uint32_t flags, uint32_t maxDepth) { +DirList::DirList(const std::string &path, const char *filter, uint32_t flags, uint32_t maxDepth) { this->LoadPath(path, filter, flags, maxDepth); this->SortList(); } @@ -51,8 +51,8 @@ DirList::~DirList() { ClearList(); } -BOOL DirList::LoadPath(const std::string & folder, const char *filter, uint32_t flags, uint32_t maxDepth) { - if(folder.empty()) return false; +BOOL DirList::LoadPath(const std::string &folder, const char *filter, uint32_t flags, uint32_t maxDepth) { + if (folder.empty()) return false; Flags = flags; Filter = filter; @@ -65,11 +65,11 @@ BOOL DirList::LoadPath(const std::string & folder, const char *filter, uint32_t StringTools::RemoveDoubleSlashs(folderpath); //! remove last slash if exists - if(length > 0 && folderpath[length-1] == '/') - folderpath.erase(length-1); + 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 += '/'; } @@ -77,7 +77,7 @@ BOOL DirList::LoadPath(const std::string & folder, const char *filter, uint32_t } BOOL DirList::InternalLoadPath(std::string &folderpath) { - if(folderpath.size() < 3) + if (folderpath.size() < 3) return false; struct dirent *dirent = NULL; @@ -91,13 +91,13 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) { BOOL isDir = dirent->d_type & DT_DIR; const char *filename = dirent->d_name; - if(isDir) { - if(strcmp(filename,".") == 0 || strcmp(filename,"..") == 0) + if (isDir) { + if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0) continue; - if((Flags & CheckSubfolders) && (Depth > 0)) { + if ((Flags & CheckSubfolders) && (Depth > 0)) { int32_t length = folderpath.size(); - if(length > 2 && folderpath[length-1] != '/') { + if (length > 2 && folderpath[length - 1] != '/') { folderpath += '/'; } folderpath += filename; @@ -108,18 +108,18 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) { Depth++; } - if(!(Flags & Dirs)) + if (!(Flags & Dirs)) continue; - } else if(!(Flags & Files)) { + } else if (!(Flags & Files)) { continue; } - if(Filter) { - char * fileext = strrchr(filename, '.'); - if(!fileext) + if (Filter) { + char *fileext = strrchr(filename, '.'); + if (!fileext) continue; - if(StringTools::strtokcmp(fileext, Filter, ",") == 0) + if (StringTools::strtokcmp(fileext, Filter, ",") == 0) AddEntrie(folderpath, filename, isDir); } else { AddEntrie(folderpath, filename, isDir); @@ -130,16 +130,16 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) { return true; } -void DirList::AddEntrie(const std::string &filepath, const char * filename, BOOL isDir) { - if(!filename) +void DirList::AddEntrie(const std::string &filepath, const char *filename, BOOL isDir) { + if (!filename) return; int32_t 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[pos].FilePath = (char *) malloc(filepath.size() + strlen(filename) + 2); + if (!FileInfo[pos].FilePath) { FileInfo.resize(pos); return; } @@ -149,8 +149,8 @@ void DirList::AddEntrie(const std::string &filepath, const char * filename, BOOL } void DirList::ClearList() { - for(uint32_t i = 0; i < FileInfo.size(); ++i) { - if(FileInfo[i].FilePath) { + for (uint32_t i = 0; i < FileInfo.size(); ++i) { + if (FileInfo[i].FilePath) { free(FileInfo[i].FilePath); FileInfo[i].FilePath = NULL; } @@ -160,33 +160,33 @@ void DirList::ClearList() { std::vector().swap(FileInfo); } -const char * DirList::GetFilename(int32_t ind) const { +const char *DirList::GetFilename(int32_t ind) const { if (!valid(ind)) return ""; 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) + if (strcasecmp(f1.FilePath, f2.FilePath) > 0) return false; return true; } void DirList::SortList() { - if(FileInfo.size() > 1) + 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) + if (FileInfo.size() > 1) std::sort(FileInfo.begin(), FileInfo.end(), SortFunc); } @@ -194,14 +194,14 @@ uint64_t DirList::GetFilesize(int32_t index) const { struct stat st; const char *path = GetFilepath(index); - if(!path || stat(path, &st) != 0) + if (!path || stat(path, &st) != 0) return 0; return st.st_size; } int32_t DirList::GetFileIndex(const char *filename) const { - if(!filename) + if (!filename) return -1; for (uint32_t i = 0; i < FileInfo.size(); ++i) { diff --git a/src/fs/DirList.h b/src/fs/DirList.h index 64072df..ed36f97 100644 --- a/src/fs/DirList.h +++ b/src/fs/DirList.h @@ -32,7 +32,7 @@ #include typedef struct { - char * FilePath; + char *FilePath; BOOL isDir; } DirEntry; @@ -40,42 +40,54 @@ 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, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); + DirList(const std::string &path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); + //!Destructor virtual ~DirList(); + //! Load all the files from a directory - BOOL LoadPath(const std::string & path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); + BOOL LoadPath(const std::string &path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); + //! Get a filename of the list //!\param list index - const char * GetFilename(int32_t index) const; + const char *GetFilename(int32_t index) const; + //! Get the a filepath of the list //!\param list index const char *GetFilepath(int32_t index) const { if (!valid(index)) return ""; else return FileInfo[index].FilePath; } + //! Get the a filesize of the list //!\param list index uint64_t GetFilesize(int32_t index) const; + //! Is index a dir or a file //!\param list index BOOL IsDir(int32_t index) const { - if(!valid(index)) return false; + if (!valid(index)) return false; return FileInfo[index].isDir; }; + //! Get the filecount of the whole list int32_t 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 int32_t GetFileIndex(const char *filename) const; + //! Enum for search/filter flags enum { Files = 0x01, @@ -85,10 +97,13 @@ public: protected: // Internal parser BOOL InternalLoadPath(std::string &path); + //!Add a list entrie - void AddEntrie(const std::string &filepath, const char * filename, BOOL isDir); + 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(uint32_t pos) const { return (pos < FileInfo.size()); diff --git a/src/fs/FSUtils.cpp b/src/fs/FSUtils.cpp index e9dadb5..c99b8af 100644 --- a/src/fs/FSUtils.cpp +++ b/src/fs/FSUtils.cpp @@ -10,7 +10,7 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size) { //! always initialze input *inbuffer = NULL; - if(size) + if (size) *size = 0; int32_t iFd = open(filepath, O_RDONLY); @@ -30,12 +30,12 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_ uint32_t done = 0; int32_t readBytes = 0; - while(done < filesize) { - if(done + blocksize > filesize) { + while (done < filesize) { + if (done + blocksize > filesize) { blocksize = filesize - done; } readBytes = read(iFd, buffer + done, blocksize); - if(readBytes <= 0) + if (readBytes <= 0) break; done += readBytes; } @@ -51,27 +51,27 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_ *inbuffer = buffer; //! sign is optional input - if(size) { + if (size) { *size = filesize; } return filesize; } -int32_t FSUtils::CheckFile(const char * filepath) { - if(!filepath) +int32_t FSUtils::CheckFile(const char *filepath) { + if (!filepath) return 0; struct stat filestat; - char dirnoslash[strlen(filepath)+2]; + 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) { + char *notRoot = strrchr(dirnoslash, '/'); + if (!notRoot) { strcat(dirnoslash, "/"); } @@ -81,29 +81,29 @@ int32_t FSUtils::CheckFile(const char * filepath) { return 0; } -int32_t FSUtils::CreateSubfolder(const char * fullpath) { - if(!fullpath) +int32_t FSUtils::CreateSubfolder(const char *fullpath) { + if (!fullpath) return 0; int32_t result = 0; - char dirnoslash[strlen(fullpath)+1]; + char dirnoslash[strlen(fullpath) + 1]; strcpy(dirnoslash, fullpath); - int32_t pos = strlen(dirnoslash)-1; - while(dirnoslash[pos] == '/') { + int32_t pos = strlen(dirnoslash) - 1; + while (dirnoslash[pos] == '/') { dirnoslash[pos] = '\0'; pos--; } - if(CheckFile(dirnoslash)) { + if (CheckFile(dirnoslash)) { return 1; } else { - char parentpath[strlen(dirnoslash)+2]; + char parentpath[strlen(dirnoslash) + 2]; strcpy(parentpath, dirnoslash); - char * ptr = strrchr(parentpath, '/'); + char *ptr = strrchr(parentpath, '/'); - if(!ptr) { + if (!ptr) { //!Device root directory (must be with '/') strcat(parentpath, "/"); struct stat filestat; @@ -119,7 +119,7 @@ int32_t FSUtils::CreateSubfolder(const char * fullpath) { result = CreateSubfolder(parentpath); } - if(!result) + if (!result) return 0; if (mkdir(dirnoslash, 0777) == -1) { @@ -129,13 +129,13 @@ int32_t FSUtils::CreateSubfolder(const char * fullpath) { return 1; } -BOOL FSUtils::saveBufferToFile(const char * path, void * buffer, uint32_t size) { +BOOL FSUtils::saveBufferToFile(const char *path, void *buffer, uint32_t size) { CFile file(path, CFile::WriteOnly); if (!file.isOpen()) { - DEBUG_FUNCTION_LINE("Failed to open %s\n",path); + DEBUG_FUNCTION_LINE("Failed to open %s\n", path); return false; } - file.write((const uint8_t*) buffer,size); + file.write((const uint8_t *) buffer, size); file.close(); return true; } diff --git a/src/fs/FSUtils.h b/src/fs/FSUtils.h index b589a44..489a3d0 100644 --- a/src/fs/FSUtils.h +++ b/src/fs/FSUtils.h @@ -8,9 +8,11 @@ public: static int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size); //! todo: C++ class - static int32_t CreateSubfolder(const char * fullpath); - static int32_t CheckFile(const char * filepath); - static BOOL saveBufferToFile(const char * path, void * buffer, uint32_t size); + static int32_t CreateSubfolder(const char *fullpath); + + static int32_t CheckFile(const char *filepath); + + static BOOL saveBufferToFile(const char *path, void *buffer, uint32_t size); }; #endif // __FS_UTILS_H_ diff --git a/src/main.cpp b/src/main.cpp index 6b19b9f..baf98f5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,8 +10,7 @@ WUPS_PLUGIN_LICENSE("GPL"); WUPS_USE_WUT_CRT() -TcpReceiver * thread = NULL; - +TcpReceiver *thread = NULL; /* Entry point */ ON_APPLICATION_START(args) { @@ -19,18 +18,18 @@ ON_APPLICATION_START(args) { log_init(); DEBUG_FUNCTION_LINE("Started wiiload thread\n"); - thread = new TcpReceiver(4299); + thread = new TcpReceiver(4299); } -void stopThread(){ - if(thread != NULL){ +void stopThread() { + if (thread != NULL) { delete thread; thread = NULL; } } -ON_APPLICATION_END(){ +ON_APPLICATION_END() { DEBUG_FUNCTION_LINE("Kill thread\n"); stopThread(); diff --git a/src/utils/CThread.h b/src/utils/CThread.h index be96e90..8a277e8 100644 --- a/src/utils/CThread.h +++ b/src/utils/CThread.h @@ -25,23 +25,20 @@ class CThread { public: - typedef void (* Callback)(CThread *thread, void *arg); + typedef void (*Callback)(CThread *thread, void *arg); //! constructor CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL) - : pThread(NULL) - , pThreadStack(NULL) - , pCallback(callback) - , pCallbackArg(callbackArg) { + : pThread(NULL), pThreadStack(NULL), pCallback(callback), pCallbackArg(callbackArg) { //! save attribute assignment iAttributes = iAttr; //! allocate the thread - pThread = (OSThread*)memalign(8, sizeof(OSThread)); + pThread = (OSThread *) memalign(8, sizeof(OSThread)); //! allocate the stack pThreadStack = (uint8_t *) memalign(0x20, iStackSize); //! create the thread - if(pThread && pThreadStack) - OSCreateThread(pThread, &CThread::threadCallback, 1, (char*)this, pThreadStack+iStackSize, iStackSize, iPriority, iAttributes); + if (pThread && pThreadStack) + OSCreateThread(pThread, &CThread::threadCallback, 1, (char *) this, pThreadStack + iStackSize, iStackSize, iPriority, iAttributes); } //! destructor @@ -51,64 +48,73 @@ public: } static CThread *create(CThread::Callback callback, void *callbackArg, int32_t iAttr = eAttributeNone, int32_t iPriority = 16, int32_t iStackSize = 0x8000) { - return ( new CThread(iAttr, iPriority, iStackSize, callback, callbackArg) ); + return (new CThread(iAttr, iPriority, iStackSize, callback, callbackArg)); } //! Get thread ID - virtual void* getThread() const { + virtual void *getThread() const { return pThread; } + //! Thread entry function virtual void executeThread(void) { - if(pCallback) + if (pCallback) pCallback(this, pCallbackArg); } + //! Suspend thread virtual void suspendThread(void) { - if(isThreadSuspended()) return; - if(pThread) OSSuspendThread(pThread); + if (isThreadSuspended()) return; + if (pThread) OSSuspendThread(pThread); } + //! Resume thread virtual void resumeThread(void) { - if(!isThreadSuspended()) return; - if(pThread) OSResumeThread(pThread); + if (!isThreadSuspended()) return; + if (pThread) OSResumeThread(pThread); } + //! Set thread priority virtual void setThreadPriority(int prio) { - if(pThread) OSSetThreadPriority(pThread, prio); + if (pThread) OSSetThreadPriority(pThread, prio); } + //! Check if thread is suspended virtual BOOL isThreadSuspended(void) const { - if(pThread) return OSIsThreadSuspended(pThread); + if (pThread) return OSIsThreadSuspended(pThread); return false; } + //! Check if thread is terminated virtual BOOL isThreadTerminated(void) const { - if(pThread) return OSIsThreadTerminated(pThread); + 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)) { - if(isThreadSuspended()) + if (pThread && !(iAttributes & eAttributeDetach)) { + if (isThreadSuspended()) resumeThread(); OSJoinThread(pThread, NULL); } //! free the thread stack buffer - if(pThreadStack) + if (pThreadStack) free(pThreadStack); - if(pThread) + if (pThread) free(pThread); pThread = NULL; pThreadStack = NULL; } + //! Thread attributes enum eCThreadAttributes { eAttributeNone = 0x07, @@ -124,6 +130,7 @@ private: ((CThread *) argv)->executeThread(); return 0; } + int iAttributes; OSThread *pThread; uint8_t *pThreadStack; diff --git a/src/utils/StringTools.cpp b/src/utils/StringTools.cpp index c1d672a..39a5273 100644 --- a/src/utils/StringTools.cpp +++ b/src/utils/StringTools.cpp @@ -36,12 +36,12 @@ #include -BOOL StringTools::EndsWith(const std::string& a, const std::string& b) { +BOOL StringTools::EndsWith(const std::string &a, const std::string &b) { if (b.size() > a.size()) return false; return std::equal(a.begin() + a.size() - b.size(), a.end(), b.begin()); } -const char * StringTools::byte_to_binary(int32_t x) { +const char *StringTools::byte_to_binary(int32_t x) { static char b[9]; b[0] = '\0'; @@ -53,25 +53,25 @@ const char * StringTools::byte_to_binary(int32_t x) { 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) + if (position == std::string::npos) break; output.erase(position, 1); } return output; } -const char * StringTools::fmt(const char * format, ...) { +const char *StringTools::fmt(const char *format, ...) { static char strChar[512]; strChar[0] = 0; va_list va; va_start(va, format); - if((vsprintf(strChar, format, va) >= 0)) { + if ((vsprintf(strChar, format, va) >= 0)) { va_end(va); return (const char *) strChar; } @@ -80,26 +80,26 @@ const char * StringTools::fmt(const char * format, ...) { return NULL; } -const wchar_t * StringTools::wfmt(const char * format, ...) { +const wchar_t *StringTools::wfmt(const char *format, ...) { static char tmp[512]; static wchar_t strWChar[512]; strWChar[0] = 0; tmp[0] = 0; - if(!format) + if (!format) return (const wchar_t *) strWChar; - if(strcmp(format, "") == 0) + if (strcmp(format, "") == 0) return (const wchar_t *) strWChar; va_list va; va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { - int bt; + if ((vsprintf(tmp, format, va) >= 0)) { + int bt; int32_t strlength = strlen(tmp); - bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512 ); + bt = mbstowcs(strWChar, tmp, (strlength < 512) ? strlength : 512); - if(bt > 0) { + if (bt > 0) { strWChar[bt] = 0; return (const wchar_t *) strWChar; } @@ -109,14 +109,14 @@ const wchar_t * StringTools::wfmt(const char * format, ...) { return NULL; } -int32_t StringTools::strprintf(std::string &str, const char * format, ...) { +int32_t StringTools::strprintf(std::string &str, const char *format, ...) { static char tmp[512]; tmp[0] = 0; int32_t result = 0; va_list va; va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { + if ((vsprintf(tmp, format, va) >= 0)) { str = tmp; result = str.size(); } @@ -125,14 +125,14 @@ int32_t StringTools::strprintf(std::string &str, const char * format, ...) { return result; } -std::string StringTools::strfmt(const char * format, ...) { +std::string StringTools::strfmt(const char *format, ...) { std::string str; static char tmp[512]; tmp[0] = 0; va_list va; va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { + if ((vsprintf(tmp, format, va) >= 0)) { str = tmp; } va_end(va); @@ -140,11 +140,11 @@ std::string StringTools::strfmt(const char * format, ...) { return str; } -BOOL StringTools::char2wchar_t(const char * strChar, wchar_t * dest) { - if(!strChar || !dest) +BOOL StringTools::char2wchar_t(const char *strChar, wchar_t *dest) { + if (!strChar || !dest) return false; - int bt; + int bt; bt = mbstowcs(dest, strChar, strlen(strChar)); if (bt > 0) { dest[bt] = 0; @@ -154,39 +154,39 @@ BOOL StringTools::char2wchar_t(const char * strChar, wchar_t * dest) { return false; } -int32_t StringTools::strtokcmp(const char * string, const char * compare, const char * separator) { - if(!string || !compare) +int32_t 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 * strTok = strtok(TokCopy, separator); + char *strTok = strtok(TokCopy, separator); while (strTok != NULL) { if (strcasecmp(string, strTok) == 0) { return 0; } - strTok = strtok(NULL,separator); + strTok = strtok(NULL, separator); } return -1; } -int32_t StringTools::strextcmp(const char * string, const char * extension, char seperator) { - if(!string || !extension) +int32_t StringTools::strextcmp(const char *string, const char *extension, char seperator) { + if (!string || !extension) return -1; char *ptr = strrchr(string, seperator); - if(!ptr) + if (!ptr) return -1; return strcasecmp(ptr + 1, extension); } -std::vector StringTools::stringSplit(const std::string & inValue, const std::string & splitter) { +std::vector StringTools::stringSplit(const std::string &inValue, const std::string &splitter) { std::string value = inValue; std::vector result; while (true) { @@ -201,7 +201,7 @@ std::vector StringTools::stringSplit(const std::string & inValue, c result.push_back(""); break; } - if(index + splitter.size() > value.length()) { + if (index + splitter.size() > value.length()) { break; } value = value.substr(index + splitter.size(), value.length()); diff --git a/src/utils/StringTools.h b/src/utils/StringTools.h index c648f4c..5d9f2c4 100644 --- a/src/utils/StringTools.h +++ b/src/utils/StringTools.h @@ -30,52 +30,58 @@ #include #include -class StringTools{ - public: - static BOOL EndsWith(const std::string& a, const std::string& b); - static const char * byte_to_binary(int32_t x); - static std::string removeCharFromString(std::string& input,char toBeRemoved); - static const char * fmt(const char * format, ...); - static const wchar_t * wfmt(const char * format, ...); - static int32_t strprintf(std::string &str, const char * format, ...); - static std::string strfmt(const char * format, ...); - static BOOL char2wchar_t(const char * src, wchar_t * dest); - static int32_t strtokcmp(const char * string, const char * compare, const char * separator); - static int32_t strextcmp(const char * string, const char * extension, char seperator); +class StringTools { +public: + static BOOL EndsWith(const std::string &a, const std::string &b); - static const char * FullpathToFilename(const char *path){ - if(!path) return path; + static const char *byte_to_binary(int32_t x); - const char * ptr = path; - const char * Filename = ptr; + static std::string removeCharFromString(std::string &input, char toBeRemoved); - while(*ptr != '\0') - { - if(ptr[0] == '/' && ptr[1] != '\0') - Filename = ptr+1; + static const char *fmt(const char *format, ...); - ++ptr; - } + static const wchar_t *wfmt(const char *format, ...); - return Filename; + static int32_t strprintf(std::string &str, const char *format, ...); + + static std::string strfmt(const char *format, ...); + + static BOOL char2wchar_t(const char *src, wchar_t *dest); + + static int32_t strtokcmp(const char *string, const char *compare, const char *separator); + + static int32_t strextcmp(const char *string, const char *extension, char seperator); + + static const char *FullpathToFilename(const char *path) { + if (!path) return path; + + const char *ptr = path; + const char *Filename = ptr; + + while (*ptr != '\0') { + if (ptr[0] == '/' && ptr[1] != '\0') + Filename = ptr + 1; + + ++ptr; } - static void RemoveDoubleSlashs(std::string &str){ - uint32_t length = str.size(); + return Filename; + } - //! clear path of double slashes - for(uint32_t i = 1; i < length; ++i) - { - if(str[i-1] == '/' && str[i] == '/') - { - str.erase(i, 1); - i--; - length--; - } + static void RemoveDoubleSlashs(std::string &str) { + uint32_t length = str.size(); + + //! clear path of double slashes + for (uint32_t i = 1; i < length; ++i) { + if (str[i - 1] == '/' && str[i] == '/') { + str.erase(i, 1); + i--; + length--; } } + } - static std::vector stringSplit(const std::string & value, const std::string & splitter); + static std::vector stringSplit(const std::string &value, const std::string &splitter); }; #endif /* __STRING_TOOLS_H */ diff --git a/src/utils/TcpReceiver.cpp b/src/utils/TcpReceiver.cpp index 5c94148..cb82104 100644 --- a/src/utils/TcpReceiver.cpp +++ b/src/utils/TcpReceiver.cpp @@ -19,15 +19,13 @@ #define RPX_TEMP_FILE "fs:/vol/external01/wiiu/apps/temp.rpx" #define RPX_TEMP_FILE_EX "wiiu/apps/temp.rpx" -extern "C"{ - uint64_t _SYSGetSystemApplicationTitleId(int32_t); - void _SYSLaunchTitleWithStdArgsInNoSplash(uint64_t, uint32_t); +extern "C" { +uint64_t _SYSGetSystemApplicationTitleId(int32_t); +void _SYSLaunchTitleWithStdArgsInNoSplash(uint64_t, uint32_t); } + TcpReceiver::TcpReceiver(int32_t port) - : CThread(CThread::eAttributeAffCore0) - , exitRequested(false) - , serverPort(port) - , serverSocket(-1) { + : CThread(CThread::eAttributeAffCore0), exitRequested(false), serverPort(port), serverSocket(-1) { resumeThread(); } @@ -35,7 +33,7 @@ TcpReceiver::TcpReceiver(int32_t port) TcpReceiver::~TcpReceiver() { exitRequested = true; - if(serverSocket >= 0) { + if (serverSocket >= 0) { shutdown(serverSocket, SHUT_RDWR); } } @@ -62,7 +60,7 @@ void TcpReceiver::executeThread() { socklen_t len; int32_t ret; - if ((ret = bind(serverSocket, (struct sockaddr *)&bindAddress, 16)) < 0) { + if ((ret = bind(serverSocket, (struct sockaddr *) &bindAddress, 16)) < 0) { socketclose(serverSocket); return; } @@ -80,12 +78,12 @@ void TcpReceiver::executeThread() { memset(&clientAddr, 0, sizeof(clientAddr)); int32_t addrlen = sizeof(struct sockaddr); - while(!exitRequested) { + while (!exitRequested) { DEBUG_FUNCTION_LINE("\n"); len = 16; - int32_t clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &len); - if(clientSocket >= 0) { + int32_t clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddr, &len); + if (clientSocket >= 0) { DEBUG_FUNCTION_LINE("\n"); uint32_t ipAddress = clientAddr.sin_addr.s_addr; @@ -94,10 +92,10 @@ void TcpReceiver::executeThread() { //serverReceiveFinished(this, ipAddress, result); socketclose(clientSocket); - if(result > 0) + if (result > 0) break; } else { - DEBUG_FUNCTION_LINE("Server socket accept failed %i %d\n", clientSocket,wiiu_geterrno()); + DEBUG_FUNCTION_LINE("Server socket accept failed %i %d\n", clientSocket, wiiu_geterrno()); OSSleepTicks(OSMicrosecondsToTicks(100000)); } } @@ -111,7 +109,7 @@ typedef struct __attribute((packed)) { uint32_t filesize; uint32_t fileoffset; char path[256]; -}LOAD_REQUEST; +} LOAD_REQUEST; int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { DEBUG_FUNCTION_LINE("Loading file from ip %08X\n", ipAddress); @@ -122,10 +120,10 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { memset(haxx, 0, sizeof(haxx)); //skip haxx recvwait(clientSocket, haxx, sizeof(haxx)); - recvwait(clientSocket, (unsigned char*)&fileSize, sizeof(fileSize)); + recvwait(clientSocket, (unsigned char *) &fileSize, sizeof(fileSize)); if (haxx[4] > 0 || haxx[5] > 4) { - recvwait(clientSocket, (unsigned char*)&fileSizeUnc, sizeof(fileSizeUnc)); // Compressed protocol, read another 4 bytes + recvwait(clientSocket, (unsigned char *) &fileSizeUnc, sizeof(fileSizeUnc)); // Compressed protocol, read another 4 bytes } struct in_addr in; @@ -134,21 +132,21 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { DEBUG_FUNCTION_LINE("transfer start\n"); - unsigned char* loadAddress = (unsigned char*)memalign(0x40, fileSize); - if(!loadAddress) { + unsigned char *loadAddress = (unsigned char *) memalign(0x40, fileSize); + if (!loadAddress) { OSSleepTicks(OSSecondsToTicks(1)); return NOT_ENOUGH_MEMORY; } // Copy rpl in memory - while(bytesRead < fileSize) { + while (bytesRead < fileSize) { uint32_t blockSize = 0x1000; - if(blockSize > (fileSize - bytesRead)) + if (blockSize > (fileSize - bytesRead)) blockSize = fileSize - bytesRead; int32_t ret = recv(clientSocket, loadAddress + bytesRead, blockSize, 0); - if(ret <= 0) { + if (ret <= 0) { DEBUG_FUNCTION_LINE("Failure on reading file\n"); break; } @@ -156,7 +154,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { bytesRead += ret; } - if(bytesRead != fileSize) { + if (bytesRead != fileSize) { free(loadAddress); DEBUG_FUNCTION_LINE("File loading not finished, %i of %i bytes received\n", bytesRead, fileSize); return FILE_READ_ERROR; @@ -167,7 +165,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { // Do we need to unzip this thing? if (haxx[4] > 0 || haxx[5] > 4) { - unsigned char* inflatedData = NULL; + unsigned char *inflatedData = NULL; // We need to unzip... if (loadAddress[0] == 'P' && loadAddress[1] == 'K' && loadAddress[2] == 0x03 && loadAddress[3] == 0x04) { @@ -175,8 +173,8 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { //! mhmm this is incorrect, it has to parse the zip // Section is compressed, inflate - inflatedData = (unsigned char*)malloc(fileSizeUnc); - if(!inflatedData) { + inflatedData = (unsigned char *) malloc(fileSizeUnc); + if (!inflatedData) { free(loadAddress); return NOT_ENOUGH_MEMORY; @@ -199,10 +197,10 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { } s.avail_in = fileSize; - s.next_in = (Bytef *)(&loadAddress[0]); + s.next_in = (Bytef *) (&loadAddress[0]); s.avail_out = fileSizeUnc; - s.next_out = (Bytef *)&inflatedData[0]; + s.next_out = (Bytef *) &inflatedData[0]; ret = inflate(&s, Z_FINISH); if (ret != Z_OK && ret != Z_STREAM_END) { @@ -216,16 +214,16 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { fileSize = fileSizeUnc; } else { // Section is compressed, inflate - inflatedData = (unsigned char*)malloc(fileSizeUnc); - if(!inflatedData) { + inflatedData = (unsigned char *) malloc(fileSizeUnc); + if (!inflatedData) { free(loadAddress); return NOT_ENOUGH_MEMORY; } uLongf f = fileSizeUnc; - int32_t result = uncompress((Bytef*)&inflatedData[0], &f, (Bytef*)loadAddress, fileSize); - if(result != Z_OK) { + int32_t result = uncompress((Bytef *) &inflatedData[0], &f, (Bytef *) loadAddress, fileSize); + if (result != Z_OK) { DEBUG_FUNCTION_LINE("uncompress failed %i\n", result); return FILE_READ_ERROR; @@ -235,16 +233,16 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { fileSize = fileSizeUnc; } - if(inflatedData[0x7] == 0xCA && inflatedData[0x8] == 0xFE && inflatedData[0x9] != 0xDE && inflatedData[0xA] != 0xAD){ + if (inflatedData[0x7] == 0xCA && inflatedData[0x8] == 0xFE && inflatedData[0x9] != 0xDE && inflatedData[0xA] != 0xAD) { DEBUG_FUNCTION_LINE("Try to load a rpx\n"); FSUtils::CreateSubfolder(RPX_TEMP_PATH); - res = FSUtils::saveBufferToFile(RPX_TEMP_FILE,inflatedData, fileSize); + res = FSUtils::saveBufferToFile(RPX_TEMP_FILE, inflatedData, fileSize); free(inflatedData); loadedRPX = true; - }else if(inflatedData[0x7] == 0xCA && inflatedData[0x8] == 0xFE && inflatedData[0x9] == 0xDE && inflatedData[0xA] == 0xAD){ + } else if (inflatedData[0x7] == 0xCA && inflatedData[0x8] == 0xFE && inflatedData[0x9] == 0xDE && inflatedData[0xA] == 0xAD) { - auto newContainer = PluginUtils::getPluginForBuffer((char*)inflatedData, fileSize); - if(newContainer){ + auto newContainer = PluginUtils::getPluginForBuffer((char *) inflatedData, fileSize); + if (newContainer) { auto oldPlugins = PluginUtils::getLoadedPlugins(8); std::vector finalList; @@ -256,7 +254,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { DEBUG_FUNCTION_LINE("Skipping duplicate"); PluginUtils::destroyPluginContainer(plugin); continue; - }else{ + } else { finalList.push_back(plugin); } } @@ -271,39 +269,39 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { if (PluginUtils::LoadAndLinkOnRestart(finalList) != 0) { DEBUG_FUNCTION_LINE("Failed to load& link\n"); PluginUtils::destroyPluginContainer(finalList); - }else{ + } else { PluginUtils::destroyPluginContainer(finalList); - SYSRelaunchTitle(NULL,NULL); + SYSRelaunchTitle(NULL, NULL); } free(inflatedData); free(loadAddress); return fileSize; - }else{ + } else { DEBUG_FUNCTION_LINE("Failed to parse plugin\n"); } free(inflatedData); } } else { - if(loadAddress[0x7] == 0xCA && loadAddress[0x8] == 0xFE){ + if (loadAddress[0x7] == 0xCA && loadAddress[0x8] == 0xFE) { DEBUG_FUNCTION_LINE("Try to load a rpx\n"); FSUtils::CreateSubfolder(RPX_TEMP_PATH); - res = FSUtils::saveBufferToFile(RPX_TEMP_FILE,loadAddress, fileSize); + res = FSUtils::saveBufferToFile(RPX_TEMP_FILE, loadAddress, fileSize); free(loadAddress); loadedRPX = true; - }else if(loadAddress[0x7] == 0xCA && loadAddress[0x8] == 0xFE && loadAddress[0x9] == 0xDE){ + } else if (loadAddress[0x7] == 0xCA && loadAddress[0x8] == 0xFE && loadAddress[0x9] == 0xDE) { OSFatal("Not implemented yet"); free(loadAddress); } } - if(!res) { + if (!res) { return NOT_ENOUGH_MEMORY; } - if(loadedRPX){ + if (loadedRPX) { LOAD_REQUEST request; memset(&request, 0, sizeof(request)); @@ -315,12 +313,12 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { strncpy(request.path, RPX_TEMP_FILE_EX, 255); - int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode)0); - if(mcpFd >= 0) { + int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0); + if (mcpFd >= 0) { int out = 0; IOS_Ioctl(mcpFd, 100, &request, sizeof(request), &out, sizeof(out)); IOS_Close(mcpFd); - if(out == 2) { + if (out == 2) { } } @@ -330,7 +328,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { return fileSize; } - SYSRelaunchTitle(NULL,NULL); + SYSRelaunchTitle(NULL, NULL); return fileSize; } diff --git a/src/utils/TcpReceiver.h b/src/utils/TcpReceiver.h index 6f208df..255f5d8 100644 --- a/src/utils/TcpReceiver.h +++ b/src/utils/TcpReceiver.h @@ -18,6 +18,7 @@ public: }; TcpReceiver(int32_t port); + ~TcpReceiver(); //sigslot::signal2 serverReceiveStart; @@ -26,8 +27,10 @@ public: private: void executeThread(); + int32_t loadToMemory(int32_t clientSocket, uint32_t ipAddress); - bool saveFileToSDCard(const char * path, void * buffer,uint32_t size); + + bool saveFileToSDCard(const char *path, void *buffer, uint32_t size); bool exitRequested; int32_t serverPort; diff --git a/src/utils/logger.c b/src/utils/logger.c index 0922230..5413a70 100644 --- a/src/utils/logger.c +++ b/src/utils/logger.c @@ -10,7 +10,7 @@ #include #include -static int log_socket __attribute__((section(".data")))= -1; +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; @@ -30,11 +30,11 @@ void log_init_() { void log_print_(const char *str) { // socket is always 0 initially as it is in the BSS - if(log_socket < 0) { + if (log_socket < 0) { return; } - while(log_lock) + while (log_lock) OSSleepTicks(OSMicrosecondsToTicks(1000)); log_lock = 1; @@ -42,8 +42,8 @@ void log_print_(const char *str) { int ret; while (len > 0) { int block = len < 1400 ? len : 1400; // take max 1400 bytes per UDP packet - ret = sendto(log_socket, str, block, 0, (struct sockaddr *)&connect_addr, sizeof(struct sockaddr_in)); - if(ret < 0) + ret = sendto(log_socket, str, block, 0, (struct sockaddr *) &connect_addr, sizeof(struct sockaddr_in)); + if (ret < 0) break; len -= ret; @@ -58,14 +58,14 @@ void OSFatal_printf(const char *format, ...) { tmp[0] = 0; va_list va; va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { + if ((vsprintf(tmp, format, va) >= 0)) { OSFatal(tmp); } va_end(va); } void log_printf_(const char *format, ...) { - if(log_socket < 0) { + if (log_socket < 0) { return; } @@ -74,7 +74,7 @@ void log_printf_(const char *format, ...) { va_list va; va_start(va, format); - if((vsprintf(tmp, format, va) >= 0)) { + if ((vsprintf(tmp, format, va) >= 0)) { log_print_(tmp); } va_end(va); diff --git a/src/utils/logger.h b/src/utils/logger.h index d026b05..b6f1040 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -8,9 +8,12 @@ extern "C" { #include void log_init_(); + //void log_deinit_(void); void log_print_(const char *str); + void log_printf_(const char *format, ...); + void OSFatal_printf(const char *format, ...); #define __FILENAME_X__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) @@ -21,7 +24,6 @@ void OSFatal_printf(const char *format, ...); } while (0) - #define log_init() log_init_() //#define log_deinit() log_deinit_() #define log_print(str) log_print_(str) diff --git a/src/utils/net.c b/src/utils/net.c index c615d92..ebc6ee2 100644 --- a/src/utils/net.c +++ b/src/utils/net.c @@ -4,18 +4,18 @@ static volatile int socket_lock __attribute__((section(".data"))) = 0; int32_t recvwait(int32_t sock, void *buffer, int32_t len) { - while(socket_lock) { + while (socket_lock) { usleep(1000); } int32_t ret; while (len > 0) { ret = recv(sock, buffer, len, 0); - if(ret < 0) { + if (ret < 0) { socket_lock = 0; return ret; } len -= ret; - buffer = (void *)(((char *) buffer) + ret); + buffer = (void *) (((char *) buffer) + ret); } socket_lock = 0; return 0; @@ -42,7 +42,7 @@ uint32_t recvword(int32_t sock) { } int32_t checkbyte(int32_t sock) { - while(socket_lock) { + while (socket_lock) { usleep(1000); } unsigned char buffer[1]; @@ -58,7 +58,7 @@ int32_t checkbyte(int32_t sock) { } int32_t sendwait(int32_t sock, const void *buffer, int32_t len) { - while(socket_lock) { + while (socket_lock) { usleep(1000); } int32_t ret; @@ -66,12 +66,12 @@ int32_t sendwait(int32_t sock, const void *buffer, int32_t len) { // For some reason the send blocks/crashes if the buffer is too big.. int cur_length = len <= 0x30 ? len : 0x30; ret = send(sock, buffer, cur_length, 0); - if(ret < 0) { + if (ret < 0) { socket_lock = 0; return ret; } len -= ret; - buffer = (void *)(((char *) buffer) + ret); + buffer = (void *) (((char *) buffer) + ret); } socket_lock = 0; return 0; diff --git a/src/utils/net.h b/src/utils/net.h index 759fe56..ac1befa 100644 --- a/src/utils/net.h +++ b/src/utils/net.h @@ -1,16 +1,23 @@ #ifndef _UTILS_NET_H_ #define _UTILS_NET_H_ + #include #include + #ifdef __cplusplus extern "C" { #endif int32_t recvwait(int32_t sock, void *buffer, int32_t len); + uint8_t recvbyte(int32_t sock); + uint32_t recvword(int32_t sock); + int32_t checkbyte(int32_t sock); + int32_t sendwait(int32_t sock, const void *buffer, int32_t len); + int32_t sendbyte(int32_t sock, unsigned char byte); #ifdef __cplusplus diff --git a/src/utils/romfs_dev.c b/src/utils/romfs_dev.c index 34e9986..9b69085 100644 --- a/src/utils/romfs_dev.c +++ b/src/utils/romfs_dev.c @@ -16,29 +16,29 @@ typedef enum { } RomfsSource; typedef struct romfs_mount { - devoptab_t device; - bool setup; - RomfsSource fd_type; - int32_t id; - int32_t fd; - time_t mtime; - uint64_t offset; - romfs_header header; - romfs_dir *cwd; - uint32_t *dirHashTable, *fileHashTable; - void *dirTable, *fileTable; - char name[32]; + devoptab_t device; + bool setup; + RomfsSource fd_type; + int32_t id; + int32_t fd; + time_t mtime; + uint64_t offset; + romfs_header header; + romfs_dir *cwd; + uint32_t *dirHashTable, *fileHashTable; + void *dirTable, *fileTable; + char name[32]; } romfs_mount; extern int __system_argc; -extern char** __system_argv; +extern char **__system_argv; //static char __thread __component[PATH_MAX+1]; -static char __component[PATH_MAX+1]; +static char __component[PATH_MAX + 1]; #define romFS_root(m) ((romfs_dir*)(m)->dirTable) -#define romFS_dir(m,x) ((romfs_dir*) ((uint8_t*)(m)->dirTable + (x))) -#define romFS_file(m,x) ((romfs_file*)((uint8_t*)(m)->fileTable + (x))) +#define romFS_dir(m, x) ((romfs_dir*) ((uint8_t*)(m)->dirTable + (x))) +#define romFS_file(m, x) ((romfs_file*)((uint8_t*)(m)->fileTable + (x))) #define romFS_none ((uint32_t)~0) #define romFS_dir_mode (S_IFDIR | S_IRUSR | S_IRGRP | S_IROTH) #define romFS_file_mode (S_IFREG | S_IRUSR | S_IRGRP | S_IROTH) @@ -47,7 +47,7 @@ uint64_t swapLong(uint64_t X) { uint64_t x = (uint64_t) X; x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32; x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16; - x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8; + x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8; return x; } @@ -58,12 +58,12 @@ uint64_t swapLong(uint64_t X) { ((n & 0xFF0000) >> 8) | \ ((n & 0xFF000000) >> 24))) -static ssize_t _romfs_read(romfs_mount *mount, uint64_t offset, void* buffer, uint64_t size) { +static ssize_t _romfs_read(romfs_mount *mount, uint64_t offset, void *buffer, uint64_t size) { uint64_t pos = mount->offset + offset; size_t _read = 0; - if(mount->fd_type == RomfsSource_FileDescriptor) { + if (mount->fd_type == RomfsSource_FileDescriptor) { off_t seek_offset = lseek(mount->fd, pos, SEEK_SET); - if(pos != seek_offset) { + if (pos != seek_offset) { return -1; } _read = read(mount->fd, buffer, size); @@ -71,52 +71,62 @@ static ssize_t _romfs_read(romfs_mount *mount, uint64_t offset, void* buffer, ui return _read; } -static bool _romfs_read_chk(romfs_mount *mount, uint64_t offset, void* buffer, uint64_t size) { +static bool _romfs_read_chk(romfs_mount *mount, uint64_t offset, void *buffer, uint64_t size) { return _romfs_read(mount, offset, buffer, size) == size; } //----------------------------------------------------------------------------- -static int romfs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode); -static int romfs_close(struct _reent *r, void *fd); -static ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len); -static off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir); -static int romfs_fstat(struct _reent *r, void *fd, struct stat *st); -static int romfs_stat(struct _reent *r, const char *path, struct stat *st); -static int romfs_chdir(struct _reent *r, const char *path); -static DIR_ITER* romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path); -static int romfs_dirreset(struct _reent *r, DIR_ITER *dirState); -static int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat); -static int romfs_dirclose(struct _reent *r, DIR_ITER *dirState); +static int romfs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode); + +static int romfs_close(struct _reent *r, void *fd); + +static ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len); + +static off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir); + +static int romfs_fstat(struct _reent *r, void *fd, struct stat *st); + +static int romfs_stat(struct _reent *r, const char *path, struct stat *st); + +static int romfs_chdir(struct _reent *r, const char *path); + +static DIR_ITER *romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path); + +static int romfs_dirreset(struct _reent *r, DIR_ITER *dirState); + +static int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat); + +static int romfs_dirclose(struct _reent *r, DIR_ITER *dirState); typedef struct { romfs_mount *mount; - romfs_file *file; - uint64_t offset, pos; + romfs_file *file; + uint64_t offset, pos; } romfs_fileobj; typedef struct { romfs_mount *mount; - romfs_dir* dir; - uint32_t state; - uint32_t childDir; - uint32_t childFile; + romfs_dir *dir; + uint32_t state; + uint32_t childDir; + uint32_t childFile; } romfs_diriter; static devoptab_t romFS_devoptab = { - .structSize = sizeof(romfs_fileobj), - .open_r = romfs_open, - .close_r = romfs_close, - .read_r = romfs_read, - .seek_r = romfs_seek, - .fstat_r = romfs_fstat, - .stat_r = romfs_stat, - .chdir_r = romfs_chdir, - .dirStateSize = sizeof(romfs_diriter), - .diropen_r = romfs_diropen, - .dirreset_r = romfs_dirreset, - .dirnext_r = romfs_dirnext, - .dirclose_r = romfs_dirclose, + .structSize = sizeof(romfs_fileobj), + .open_r = romfs_open, + .close_r = romfs_close, + .read_r = romfs_read, + .seek_r = romfs_seek, + .fstat_r = romfs_fstat, + .stat_r = romfs_stat, + .chdir_r = romfs_chdir, + .dirStateSize = sizeof(romfs_diriter), + .diropen_r = romfs_diropen, + .dirreset_r = romfs_dirreset, + .dirnext_r = romfs_dirnext, + .dirclose_r = romfs_dirclose, }; static bool romfs_initialised = false; @@ -125,6 +135,7 @@ static romfs_mount romfs_mounts[32]; //----------------------------------------------------------------------------- static int32_t romfsMountCommon(const char *name, romfs_mount *mount); + static void romfsInitMtime(romfs_mount *mount); static void _romfsResetMount(romfs_mount *mount, int32_t id) { @@ -139,8 +150,8 @@ static void _romfsInit(void) { uint32_t i; uint32_t total = sizeof(romfs_mounts) / sizeof(romfs_mount); - if(!romfs_initialised) { - for(i = 0; i < total; i++) { + if (!romfs_initialised) { + for (i = 0; i < total; i++) { _romfsResetMount(&romfs_mounts[i], i); } @@ -155,14 +166,14 @@ static romfs_mount *romfsFindMount(const char *name) { _romfsInit(); - for(i=0; isetup) + if (name == NULL) { //Find an unused mount entry. + if (!mount->setup) return mount; - } else if(mount->setup) { //Find the mount with the input name. - if(strncmp(mount->name, name, sizeof(mount->name))==0) + } else if (mount->setup) { //Find the mount with the input name. + if (strncmp(mount->name, name, sizeof(mount->name)) == 0) return mount; } } @@ -170,9 +181,9 @@ static romfs_mount *romfsFindMount(const char *name) { return NULL; } -__attribute__((weak)) const char* __romfs_path = NULL; +__attribute__((weak)) const char *__romfs_path = NULL; -static romfs_mount* romfs_alloc(void) { +static romfs_mount *romfs_alloc(void) { return romfsFindMount(NULL); } @@ -185,15 +196,15 @@ static void romfs_free(romfs_mount *mount) { } static void romfs_mountclose(romfs_mount *mount) { - if(mount->fd_type == RomfsSource_FileDescriptor) { + if (mount->fd_type == RomfsSource_FileDescriptor) { close(mount->fd); } romfs_free(mount); } -int32_t romfsMount(const char *name, const char * filepath) { +int32_t romfsMount(const char *name, const char *filepath) { romfs_mount *mount = romfs_alloc(); - if(mount == NULL) + if (mount == NULL) return 99; // Regular RomFS @@ -208,19 +219,19 @@ int32_t romfsMount(const char *name, const char * filepath) { romfsInitMtime(mount); return romfsMountCommon(name, mount); -_fail0: + _fail0: romfs_mountclose(mount); return 10; } int32_t romfsMountCommon(const char *name, romfs_mount *mount) { memset(mount->name, 0, sizeof(mount->name)); - strncpy(mount->name, name, sizeof(mount->name)-1); + strncpy(mount->name, name, sizeof(mount->name) - 1); if (_romfs_read(mount, 0, &mount->header, sizeof(mount->header)) != sizeof(mount->header)) goto fail; - mount->dirHashTable = (uint32_t*)malloc(swapLong(mount->header.dirHashTableSize)); + mount->dirHashTable = (uint32_t *) malloc(swapLong(mount->header.dirHashTableSize)); if (!mount->dirHashTable) goto fail; if (!_romfs_read_chk(mount, swapLong(mount->header.dirHashTableOff), mount->dirHashTable, swapLong(mount->header.dirHashTableSize))) @@ -231,7 +242,7 @@ int32_t romfsMountCommon(const char *name, romfs_mount *mount) { goto fail; if (!_romfs_read_chk(mount, swapLong(mount->header.dirTableOff), mount->dirTable, swapLong(mount->header.dirTableSize))) goto fail; - mount->fileHashTable = (uint32_t*)malloc(swapLong(mount->header.fileHashTableSize)); + mount->fileHashTable = (uint32_t *) malloc(swapLong(mount->header.fileHashTableSize)); if (!mount->fileHashTable) goto fail; if (!_romfs_read_chk(mount, swapLong(mount->header.fileHashTableOff), mount->fileHashTable, swapLong(mount->header.fileHashTableSize))) @@ -245,12 +256,12 @@ int32_t romfsMountCommon(const char *name, romfs_mount *mount) { mount->cwd = romFS_root(mount); // add device if this is the first one - if(AddDevice(&mount->device) < 0) + if (AddDevice(&mount->device) < 0) goto fail; mount->setup = true; return 0; -fail: + fail: romfs_mountclose(mount); return 10; } @@ -269,8 +280,8 @@ int32_t romfsUnmount(const char *name) { // Remove device memset(tmpname, 0, sizeof(tmpname)); - strncpy(tmpname, mount->name, sizeof(tmpname)-2); - strncat(tmpname, ":", sizeof(tmpname)-strlen(tmpname)-1); + strncpy(tmpname, mount->name, sizeof(tmpname) - 2); + strncat(tmpname, ":", sizeof(tmpname) - strlen(tmpname) - 1); RemoveDevice(tmpname); @@ -281,20 +292,20 @@ int32_t romfsUnmount(const char *name) { //----------------------------------------------------------------------------- -static uint32_t calcHash(uint32_t parent, const uint8_t* name, uint32_t namelen, uint32_t total) { - uint32_t hash = parent ^ 123456789; +static uint32_t calcHash(uint32_t parent, const uint8_t *name, uint32_t namelen, uint32_t total) { + uint32_t hash = parent ^123456789; uint32_t i; - for (i = 0; i < namelen; i ++) { + for (i = 0; i < namelen; i++) { hash = (hash >> 5) | (hash << 27); hash ^= name[i]; } return hash % total; } -static romfs_dir* searchForDir(romfs_mount *mount, romfs_dir* parent, const uint8_t* name, uint32_t namelen) { - uint64_t parentOff = (uintptr_t)parent - (uintptr_t)mount->dirTable; - uint32_t hash = calcHash(parentOff, name, namelen, swapLong(mount->header.dirHashTableSize)/4); - romfs_dir* curDir = NULL; +static romfs_dir *searchForDir(romfs_mount *mount, romfs_dir *parent, const uint8_t *name, uint32_t namelen) { + uint64_t parentOff = (uintptr_t) parent - (uintptr_t) mount->dirTable; + uint32_t hash = calcHash(parentOff, name, namelen, swapLong(mount->header.dirHashTableSize) / 4); + romfs_dir *curDir = NULL; uint32_t curOff; for (curOff = REVERSE_INT(mount->dirHashTable[hash]); curOff != romFS_none; curOff = REVERSE_INT(curDir->nextHash)) { curDir = romFS_dir(mount, curOff); @@ -309,10 +320,10 @@ static romfs_dir* searchForDir(romfs_mount *mount, romfs_dir* parent, const uint return NULL; } -static romfs_file* searchForFile(romfs_mount *mount, romfs_dir* parent, const uint8_t* name, uint32_t namelen) { - uint64_t parentOff = (uintptr_t)parent - (uintptr_t)mount->dirTable; - uint32_t hash = calcHash(parentOff, name, namelen, swapLong(mount->header.fileHashTableSize)/4); - romfs_file* curFile = NULL; +static romfs_file *searchForFile(romfs_mount *mount, romfs_dir *parent, const uint8_t *name, uint32_t namelen) { + uint64_t parentOff = (uintptr_t) parent - (uintptr_t) mount->dirTable; + uint32_t hash = calcHash(parentOff, name, namelen, swapLong(mount->header.fileHashTableSize) / 4); + romfs_file *curFile = NULL; uint32_t curOff; for (curOff = REVERSE_INT(mount->fileHashTable[hash]); curOff != romFS_none; curOff = REVERSE_INT(curFile->nextHash)) { curFile = romFS_file(mount, curOff); @@ -327,10 +338,10 @@ static romfs_file* searchForFile(romfs_mount *mount, romfs_dir* parent, const ui return NULL; } -static int navigateToDir(romfs_mount *mount, romfs_dir** ppDir, const char** pPath, bool isDir) { - char* colonPos = strchr(*pPath, ':'); +static int navigateToDir(romfs_mount *mount, romfs_dir **ppDir, const char **pPath, bool isDir) { + char *colonPos = strchr(*pPath, ':'); if (colonPos) - *pPath = colonPos+1; + *pPath = colonPos + 1; if (!**pPath) return EILSEQ; @@ -341,8 +352,8 @@ static int navigateToDir(romfs_mount *mount, romfs_dir** ppDir, const char** pPa } while (**pPath) { - char* slashPos = strchr(*pPath, '/'); - char* component = __component; + char *slashPos = strchr(*pPath, '/'); + char *component = __component; if (slashPos) { uint32_t len = slashPos - *pPath; @@ -353,23 +364,23 @@ static int navigateToDir(romfs_mount *mount, romfs_dir** ppDir, const char** pPa memcpy(component, *pPath, len); component[len] = 0; - *pPath = slashPos+1; + *pPath = slashPos + 1; } else if (isDir) { - component = (char*)*pPath; + component = (char *) *pPath; *pPath += strlen(component); } else return 0; - if (component[0]=='.') { + if (component[0] == '.') { if (!component[1]) continue; - if (component[1]=='.' && !component[2]) { + if (component[1] == '.' && !component[2]) { *ppDir = romFS_dir(mount, REVERSE_INT((*ppDir)->parent)); continue; } } - *ppDir = searchForDir(mount, *ppDir, (uint8_t*)component, strlen(component)); + *ppDir = searchForDir(mount, *ppDir, (uint8_t *) component, strlen(component)); if (!*ppDir) return EEXIST; } @@ -381,25 +392,25 @@ static int navigateToDir(romfs_mount *mount, romfs_dir** ppDir, const char** pPa } static ino_t dir_inode(romfs_mount *mount, romfs_dir *dir) { - return (uint32_t*)dir - (uint32_t*)mount->dirTable; + return (uint32_t *) dir - (uint32_t *) mount->dirTable; } static off_t dir_size(romfs_dir *dir) { - return sizeof(romfs_dir) + (REVERSE_INT(dir->nameLen)+3)/4; + return sizeof(romfs_dir) + (REVERSE_INT(dir->nameLen) + 3) / 4; } static nlink_t dir_nlink(romfs_mount *mount, romfs_dir *dir) { nlink_t count = 2; // one for self, one for parent - uint32_t offset = REVERSE_INT(dir->childDir); + uint32_t offset = REVERSE_INT(dir->childDir); - while(offset != romFS_none) { + while (offset != romFS_none) { romfs_dir *tmp = romFS_dir(mount, offset); ++count; offset = REVERSE_INT(tmp->sibling); } offset = REVERSE_INT(dir->childFile); - while(offset != romFS_none) { + while (offset != romFS_none) { romfs_file *tmp = romFS_file(mount, offset); ++count; offset = REVERSE_INT(tmp->sibling); @@ -409,24 +420,24 @@ static nlink_t dir_nlink(romfs_mount *mount, romfs_dir *dir) { } static ino_t file_inode(romfs_mount *mount, romfs_file *file) { - return ((uint32_t*)file - (uint32_t*)mount->fileTable) + swapLong(mount->header.dirTableSize)/4; + return ((uint32_t *) file - (uint32_t *) mount->fileTable) + swapLong(mount->header.dirTableSize) / 4; } -int romfs_GetFileInfoPerPath(const char* romfs, const char *path, romfs_fileInfo* out) { - if(out == NULL){ +int romfs_GetFileInfoPerPath(const char *romfs, const char *path, romfs_fileInfo *out) { + if (out == NULL) { return -1; } - romfs_mount* mount = (romfs_mount*)romfsFindMount(romfs); - if(mount == NULL){ + romfs_mount *mount = (romfs_mount *) romfsFindMount(romfs); + if (mount == NULL) { return -2; } - romfs_dir* curDir = NULL; + romfs_dir *curDir = NULL; int errno2 = navigateToDir(mount, &curDir, &path, false); - if (errno2 != 0){ + if (errno2 != 0) { return -3; } - romfs_file* file = searchForFile(mount, curDir, (uint8_t*)path, strlen(path)); + romfs_file *file = searchForFile(mount, curDir, (uint8_t *) path, strlen(path)); if (!file) { return -4; } @@ -441,35 +452,35 @@ int romfs_GetFileInfoPerPath(const char* romfs, const char *path, romfs_fileInfo //----------------------------------------------------------------------------- int romfs_open(struct _reent *r, void *fileStruct, const char *path, int flags, int mode) { - romfs_fileobj* fileobj = (romfs_fileobj*)fileStruct; + romfs_fileobj *fileobj = (romfs_fileobj *) fileStruct; - fileobj->mount = (romfs_mount*)r->deviceData; + fileobj->mount = (romfs_mount *) r->deviceData; if ((flags & O_ACCMODE) != O_RDONLY) { r->_errno = EROFS; return -1; } - romfs_dir* curDir = NULL; + romfs_dir *curDir = NULL; r->_errno = navigateToDir(fileobj->mount, &curDir, &path, false); if (r->_errno != 0) return -1; - romfs_file* file = searchForFile(fileobj->mount, curDir, (uint8_t*)path, strlen(path)); + romfs_file *file = searchForFile(fileobj->mount, curDir, (uint8_t *) path, strlen(path)); if (!file) { - if(flags & O_CREAT) + if (flags & O_CREAT) r->_errno = EROFS; else r->_errno = ENOENT; return -1; - } else if((flags & O_CREAT) && (flags & O_EXCL)) { + } else if ((flags & O_CREAT) && (flags & O_EXCL)) { r->_errno = EEXIST; return -1; } - fileobj->file = file; + fileobj->file = file; fileobj->offset = swapLong(fileobj->mount->header.fileDataOff) + swapLong(file->dataOff); - fileobj->pos = 0; + fileobj->pos = 0; return 0; } @@ -479,20 +490,20 @@ int romfs_close(struct _reent *r, void *fd) { } ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len) { - romfs_fileobj* file = (romfs_fileobj*)fd; + romfs_fileobj *file = (romfs_fileobj *) fd; uint64_t endPos = file->pos + len; /* check if past end-of-file */ - if(file->pos >= swapLong(file->file->dataSize)) + if (file->pos >= swapLong(file->file->dataSize)) return 0; /* truncate the read to end-of-file */ - if(endPos > swapLong(file->file->dataSize)) + if (endPos > swapLong(file->file->dataSize)) endPos = swapLong(file->file->dataSize); len = endPos - file->pos; ssize_t adv = _romfs_read(file->mount, file->offset + file->pos, ptr, len); - if(adv >= 0) { + if (adv >= 0) { file->pos += adv; return adv; } @@ -502,35 +513,35 @@ ssize_t romfs_read(struct _reent *r, void *fd, char *ptr, size_t len) { } off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir) { - romfs_fileobj* file = (romfs_fileobj*)fd; - off_t start; + romfs_fileobj *file = (romfs_fileobj *) fd; + off_t start; switch (dir) { - case SEEK_SET: - start = 0; - break; + case SEEK_SET: + start = 0; + break; - case SEEK_CUR: - start = file->pos; - break; + case SEEK_CUR: + start = file->pos; + break; - case SEEK_END: - start = swapLong(file->file->dataSize); - break; + case SEEK_END: + start = swapLong(file->file->dataSize); + break; - default: - r->_errno = EINVAL; - return -1; + default: + r->_errno = EINVAL; + return -1; } /* don't allow negative position */ - if(pos < 0) { - if(start + pos < 0) { + if (pos < 0) { + if (start + pos < 0) { r->_errno = EINVAL; return -1; } } - /* check for overflow */ - else if(INT64_MAX - pos < start) { + /* check for overflow */ + else if (INT64_MAX - pos < start) { r->_errno = EOVERFLOW; return -1; } @@ -540,49 +551,49 @@ off_t romfs_seek(struct _reent *r, void *fd, off_t pos, int dir) { } int romfs_fstat(struct _reent *r, void *fd, struct stat *st) { - romfs_fileobj* file = (romfs_fileobj*)fd; + romfs_fileobj *file = (romfs_fileobj *) fd; memset(st, 0, sizeof(struct stat)); - st->st_ino = file_inode(file->mount, file->file); - st->st_mode = romFS_file_mode; + st->st_ino = file_inode(file->mount, file->file); + st->st_mode = romFS_file_mode; st->st_nlink = 1; - st->st_size = (off_t)swapLong(file->file->dataSize); + st->st_size = (off_t) swapLong(file->file->dataSize); st->st_blksize = 512; - st->st_blocks = (st->st_blksize + 511) / 512; + st->st_blocks = (st->st_blksize + 511) / 512; st->st_atime = st->st_mtime = st->st_ctime = file->mount->mtime; return 0; } int romfs_stat(struct _reent *r, const char *path, struct stat *st) { - romfs_mount* mount = (romfs_mount*)r->deviceData; - romfs_dir* curDir = NULL; + romfs_mount *mount = (romfs_mount *) r->deviceData; + romfs_dir *curDir = NULL; r->_errno = navigateToDir(mount, &curDir, &path, false); - if(r->_errno != 0) + if (r->_errno != 0) return -1; - romfs_dir* dir = searchForDir(mount, curDir, (uint8_t*)path, strlen(path)); - if(dir) { + romfs_dir *dir = searchForDir(mount, curDir, (uint8_t *) path, strlen(path)); + if (dir) { memset(st, 0, sizeof(*st)); - st->st_ino = dir_inode(mount, dir); - st->st_mode = romFS_dir_mode; - st->st_nlink = dir_nlink(mount, dir); - st->st_size = dir_size(dir); + st->st_ino = dir_inode(mount, dir); + st->st_mode = romFS_dir_mode; + st->st_nlink = dir_nlink(mount, dir); + st->st_size = dir_size(dir); st->st_blksize = 512; - st->st_blocks = (st->st_blksize + 511) / 512; + st->st_blocks = (st->st_blksize + 511) / 512; st->st_atime = st->st_mtime = st->st_ctime = mount->mtime; return 0; } - romfs_file* file = searchForFile(mount, curDir, (uint8_t*)path, strlen(path)); - if(file) { + romfs_file *file = searchForFile(mount, curDir, (uint8_t *) path, strlen(path)); + if (file) { memset(st, 0, sizeof(*st)); - st->st_ino = file_inode(mount, file); - st->st_mode = romFS_file_mode; + st->st_ino = file_inode(mount, file); + st->st_mode = romFS_file_mode; st->st_nlink = 1; - st->st_size = swapLong(file->dataSize); + st->st_size = swapLong(file->dataSize); st->st_blksize = 512; - st->st_blocks = (st->st_blksize + 511) / 512; + st->st_blocks = (st->st_blksize + 511) / 512; st->st_atime = st->st_mtime = st->st_ctime = mount->mtime; return 0; @@ -593,8 +604,8 @@ int romfs_stat(struct _reent *r, const char *path, struct stat *st) { } int romfs_chdir(struct _reent *r, const char *path) { - romfs_mount* mount = (romfs_mount*)r->deviceData; - romfs_dir* curDir = NULL; + romfs_mount *mount = (romfs_mount *) r->deviceData; + romfs_dir *curDir = NULL; r->_errno = navigateToDir(mount, &curDir, &path, false); if (r->_errno != 0) return -1; @@ -603,48 +614,48 @@ int romfs_chdir(struct _reent *r, const char *path) { return 0; } -DIR_ITER* romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path) { - romfs_diriter* iter = (romfs_diriter*)(dirState->dirStruct); - romfs_dir* curDir = NULL; - iter->mount = (romfs_mount*)r->deviceData; +DIR_ITER *romfs_diropen(struct _reent *r, DIR_ITER *dirState, const char *path) { + romfs_diriter *iter = (romfs_diriter *) (dirState->dirStruct); + romfs_dir *curDir = NULL; + iter->mount = (romfs_mount *) r->deviceData; r->_errno = navigateToDir(iter->mount, &curDir, &path, true); - if(r->_errno != 0) + if (r->_errno != 0) return NULL; - iter->dir = curDir; - iter->state = 0; - iter->childDir = REVERSE_INT(curDir->childDir); + iter->dir = curDir; + iter->state = 0; + iter->childDir = REVERSE_INT(curDir->childDir); iter->childFile = REVERSE_INT(curDir->childFile); return dirState; } int romfs_dirreset(struct _reent *r, DIR_ITER *dirState) { - romfs_diriter* iter = (romfs_diriter*)(dirState->dirStruct); + romfs_diriter *iter = (romfs_diriter *) (dirState->dirStruct); - iter->state = 0; - iter->childDir = REVERSE_INT(iter->dir->childDir); + iter->state = 0; + iter->childDir = REVERSE_INT(iter->dir->childDir); iter->childFile = REVERSE_INT(iter->dir->childFile); return 0; } int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct stat *filestat) { - romfs_diriter* iter = (romfs_diriter*)(dirState->dirStruct); + romfs_diriter *iter = (romfs_diriter *) (dirState->dirStruct); - if(iter->state == 0) { + if (iter->state == 0) { /* '.' entry */ memset(filestat, 0, sizeof(*filestat)); - filestat->st_ino = dir_inode(iter->mount, iter->dir); + filestat->st_ino = dir_inode(iter->mount, iter->dir); filestat->st_mode = romFS_dir_mode; strcpy(filename, "."); iter->state = 1; return 0; - } else if(iter->state == 1) { + } else if (iter->state == 1) { /* '..' entry */ - romfs_dir* dir = romFS_dir(iter->mount, REVERSE_INT(iter->dir->parent)); + romfs_dir *dir = romFS_dir(iter->mount, REVERSE_INT(iter->dir->parent)); memset(filestat, 0, sizeof(*filestat)); filestat->st_ino = dir_inode(iter->mount, dir); @@ -655,8 +666,8 @@ int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct s return 0; } - if(iter->childDir != romFS_none) { - romfs_dir* dir = romFS_dir(iter->mount, iter->childDir); + if (iter->childDir != romFS_none) { + romfs_dir *dir = romFS_dir(iter->mount, iter->childDir); iter->childDir = REVERSE_INT(dir->sibling); memset(filestat, 0, sizeof(*filestat)); @@ -665,16 +676,16 @@ int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct s memset(filename, 0, NAME_MAX); - if(REVERSE_INT(dir->nameLen) >= NAME_MAX) { + if (REVERSE_INT(dir->nameLen) >= NAME_MAX) { r->_errno = ENAMETOOLONG; return -1; } - strncpy(filename, (char*)dir->name, REVERSE_INT(dir->nameLen)); + strncpy(filename, (char *) dir->name, REVERSE_INT(dir->nameLen)); return 0; - } else if(iter->childFile != romFS_none) { - romfs_file* file = romFS_file(iter->mount, iter->childFile); + } else if (iter->childFile != romFS_none) { + romfs_file *file = romFS_file(iter->mount, iter->childFile); iter->childFile = REVERSE_INT(file->sibling); memset(filestat, 0, sizeof(*filestat)); @@ -683,12 +694,12 @@ int romfs_dirnext(struct _reent *r, DIR_ITER *dirState, char *filename, struct s memset(filename, 0, NAME_MAX); - if(REVERSE_INT(file->nameLen) >= NAME_MAX) { + if (REVERSE_INT(file->nameLen) >= NAME_MAX) { r->_errno = ENAMETOOLONG; return -1; } - strncpy(filename, (char*)file->name, REVERSE_INT(file->nameLen)); + strncpy(filename, (char *) file->name, REVERSE_INT(file->nameLen)); return 0; } diff --git a/src/utils/romfs_dev.h b/src/utils/romfs_dev.h index 367f202..d01cd8a 100644 --- a/src/utils/romfs_dev.h +++ b/src/utils/romfs_dev.h @@ -54,7 +54,7 @@ extern "C" { * @brief Mounts the Application's RomFS. * @param name Device mount name. */ -int32_t romfsMount(const char *name, const char * path); +int32_t romfsMount(const char *name, const char *path); /** @@ -82,7 +82,7 @@ typedef struct { uint64_t offset; ///< Length of the file's data. } romfs_fileInfo; -int romfs_GetFileInfoPerPath(const char* romfs, const char *path, romfs_fileInfo* out); +int romfs_GetFileInfoPerPath(const char *romfs, const char *path, romfs_fileInfo *out); #ifdef __cplusplus } diff --git a/src/utils/utils.c b/src/utils/utils.c index cd3486b..8bc2982 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -7,31 +7,31 @@ #include // https://gist.github.com/ccbrown/9722406 -void dumpHex(const void* data, size_t size) { +void dumpHex(const void *data, size_t size) { char ascii[17]; size_t i, j; ascii[16] = '\0'; DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data); for (i = 0; i < size; ++i) { - log_printf("%02X ", ((unsigned char*)data)[i]); - if (((unsigned char*)data)[i] >= ' ' && ((unsigned char*)data)[i] <= '~') { - ascii[i % 16] = ((unsigned char*)data)[i]; + log_printf("%02X ", ((unsigned char *) data)[i]); + if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') { + ascii[i % 16] = ((unsigned char *) data)[i]; } else { ascii[i % 16] = '.'; } - if ((i+1) % 8 == 0 || i+1 == size) { + if ((i + 1) % 8 == 0 || i + 1 == size) { log_printf(" "); - if ((i+1) % 16 == 0) { + if ((i + 1) % 16 == 0) { log_printf("| %s \n", ascii); - if(i + 1 < size) { - DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1,i+1); + if (i + 1 < size) { + DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", data + i + 1, i + 1); } - } else if (i+1 == size) { - ascii[(i+1) % 16] = '\0'; - if ((i+1) % 16 <= 8) { + } else if (i + 1 == size) { + ascii[(i + 1) % 16] = '\0'; + if ((i + 1) % 16 <= 8) { log_printf(" "); } - for (j = (i+1) % 16; j < 16; ++j) { + for (j = (i + 1) % 16; j < 16; ++j) { log_printf(" "); } log_printf("| %s \n", ascii); @@ -66,7 +66,7 @@ char *str_replace(char *orig, char *rep, char *with) { ins = tmp + len_rep; } - tmp = result = (char*)malloc(strlen(orig) + (len_with - len_rep) * count + 1); + tmp = result = (char *) malloc(strlen(orig) + (len_with - len_rep) * count + 1); if (!result) return NULL; diff --git a/src/utils/utils.h b/src/utils/utils.h index 5673a41..c53bebb 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -7,12 +7,12 @@ extern "C" { #endif -#define LIMIT(x, min, max) \ - ({ \ - typeof( x ) _x = x; \ - typeof( min ) _min = min; \ - typeof( max ) _max = max; \ - ( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \ +#define LIMIT(x, min, max) \ + ({ \ + typeof( x ) _x = x; \ + typeof( min ) _min = min; \ + typeof( max ) _max = max; \ + ( ( ( _x ) < ( _min ) ) ? ( _min ) : ( ( _x ) > ( _max ) ) ? ( _max) : ( _x ) ); \ }) #define DegToRad(a) ( (a) * 0.01745329252f ) @@ -29,7 +29,7 @@ extern "C" { #define le64(i) ((((uint64_t)le32((i) & 0xFFFFFFFFLL)) << 32) | ((uint64_t)le32(((i) & 0xFFFFFFFF00000000LL) >> 32))) //Needs to have log_init() called beforehand. -void dumpHex(const void* data, size_t size); +void dumpHex(const void *data, size_t size); char *str_replace(char *orig, char *rep, char *with); #ifdef __cplusplus