diff --git a/src/fs/CFile.cpp b/src/fs/CFile.cpp index e2f9c4a..cd46033 100644 --- a/src/fs/CFile.cpp +++ b/src/fs/CFile.cpp @@ -1,12 +1,11 @@ -#include -#include -#include +#include +#include #include #include CFile::CFile() { iFd = -1; - mem_file = NULL; + mem_file = nullptr; filesize = 0; pos = 0; } diff --git a/src/fs/CFile.hpp b/src/fs/CFile.hpp index 6c0421b..785dc1d 100644 --- a/src/fs/CFile.hpp +++ b/src/fs/CFile.hpp @@ -1,9 +1,8 @@ -#ifndef CFILE_HPP_ -#define CFILE_HPP_ +#pragma once -#include +#include #include -#include +#include #include #include #include @@ -29,7 +28,7 @@ public: int32_t open(const uint8_t *memory, int32_t memsize); - BOOL isOpen() const { + [[nodiscard]] BOOL isOpen() const { if (iFd >= 0) return true; @@ -49,11 +48,11 @@ public: int32_t seek(long int offset, int32_t origin); - uint64_t tell() { + [[nodiscard]] uint64_t tell() const { return pos; }; - uint64_t size() { + [[nodiscard]] uint64_t size() const { return filesize; }; @@ -67,5 +66,3 @@ protected: uint64_t filesize; uint64_t pos; }; - -#endif diff --git a/src/fs/DirList.cpp b/src/fs/DirList.cpp index b92437f..6c074e0 100644 --- a/src/fs/DirList.cpp +++ b/src/fs/DirList.cpp @@ -80,7 +80,7 @@ BOOL DirList::InternalLoadPath(std::string &folderpath) { if (folderpath.size() < 3) return false; - struct dirent *dirent = NULL; + struct dirent *dirent = nullptr; DIR *dir = NULL; dir = opendir(folderpath.c_str()); @@ -152,7 +152,7 @@ void DirList::ClearList() { for (uint32_t i = 0; i < FileInfo.size(); ++i) { if (FileInfo[i].FilePath) { free(FileInfo[i].FilePath); - FileInfo[i].FilePath = NULL; + FileInfo[i].FilePath = nullptr; } } @@ -191,7 +191,7 @@ void DirList::SortList(BOOL (*SortFunc)(const DirEntry &a, const DirEntry &b)) { } uint64_t DirList::GetFilesize(int32_t index) const { - struct stat st; + struct stat st{}; const char *path = GetFilepath(index); if (!path || stat(path, &st) != 0) @@ -204,7 +204,7 @@ int32_t DirList::GetFileIndex(const char *filename) const { if (!filename) return -1; - for (uint32_t i = 0; i < FileInfo.size(); ++i) { + for (int32_t i = 0; i < FileInfo.size(); ++i) { if (strcasecmp(GetFilename(i), filename) == 0) return i; } diff --git a/src/fs/DirList.h b/src/fs/DirList.h index ed36f97..624f529 100644 --- a/src/fs/DirList.h +++ b/src/fs/DirList.h @@ -24,8 +24,7 @@ * DirList Class * for WiiXplorer 2010 ***************************************************************************/ -#ifndef ___DIRLIST_H_ -#define ___DIRLIST_H_ +#pragma once #include #include @@ -39,43 +38,43 @@ typedef struct { class DirList { public: //!Constructor - DirList(void); + DirList(); //!\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); + explicit DirList(const std::string &path, const char *filter = nullptr, 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 = nullptr, 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; + [[nodiscard]] const char *GetFilename(int32_t index) const; //! Get the a filepath of the list //!\param list index - const char *GetFilepath(int32_t index) const { + [[nodiscard]] 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; + [[nodiscard]] uint64_t GetFilesize(int32_t index) const; //! Is index a dir or a file //!\param list index - BOOL IsDir(int32_t index) const { + [[nodiscard]] BOOL IsDir(int32_t index) const { if (!valid(index)) return false; return FileInfo[index].isDir; }; //! Get the filecount of the whole list - int32_t GetFilecount() const { + [[nodiscard]] int32_t GetFilecount() const { return FileInfo.size(); }; @@ -105,14 +104,12 @@ protected: void ClearList(); //! Check if valid pos is requested - inline BOOL valid(uint32_t pos) const { + [[nodiscard]] inline BOOL valid(uint32_t pos) const { return (pos < FileInfo.size()); }; - uint32_t Flags; - uint32_t Depth; - const char *Filter; + uint32_t Flags{}; + uint32_t Depth{}; + const char *Filter{}; std::vector FileInfo; }; - -#endif diff --git a/src/fs/FSUtils.cpp b/src/fs/FSUtils.cpp index c99b8af..4fc99ed 100644 --- a/src/fs/FSUtils.cpp +++ b/src/fs/FSUtils.cpp @@ -9,7 +9,7 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size) { //! always initialze input - *inbuffer = NULL; + *inbuffer = nullptr; if (size) *size = 0; @@ -20,8 +20,8 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_ uint32_t filesize = lseek(iFd, 0, SEEK_END); lseek(iFd, 0, SEEK_SET); - uint8_t *buffer = (uint8_t *) malloc(filesize); - if (buffer == NULL) { + auto *buffer = (uint8_t *) malloc(filesize); + if (buffer == nullptr) { close(iFd); return -2; } @@ -44,7 +44,7 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_ if (done != filesize) { free(buffer); - buffer = NULL; + buffer = nullptr; return -3; } @@ -62,7 +62,7 @@ int32_t 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); @@ -106,7 +106,7 @@ int32_t FSUtils::CreateSubfolder(const char *fullpath) { if (!ptr) { //!Device root directory (must be with '/') strcat(parentpath, "/"); - struct stat filestat; + struct stat filestat{}; if (stat(parentpath, &filestat) == 0) return 1; diff --git a/src/main.cpp b/src/main.cpp index 61ed311..47f1c57 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,6 @@ #include #include "utils/TcpReceiver.h" #include -#include -#include WUPS_PLUGIN_NAME("Wiiload"); WUPS_PLUGIN_DESCRIPTION("Wiiload Server"); diff --git a/src/main.h b/src/main.h index 67b89ad..b92ea46 100644 --- a/src/main.h +++ b/src/main.h @@ -4,10 +4,9 @@ #ifdef __cplusplus extern "C" { #endif -#include +#include #include - #ifdef __cplusplus } #endif diff --git a/src/utils/CThread.h b/src/utils/CThread.h index 8a277e8..b63bca1 100644 --- a/src/utils/CThread.h +++ b/src/utils/CThread.h @@ -14,8 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . ****************************************************************************/ -#ifndef CTHREAD_H_ -#define CTHREAD_H_ +#pragma once #include #include @@ -28,7 +27,7 @@ public: 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) + explicit 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) { //! save attribute assignment iAttributes = iAttr; @@ -52,24 +51,24 @@ public: } //! Get thread ID - virtual void *getThread() const { + [[nodiscard]] virtual void *getThread() const { return pThread; } //! Thread entry function - virtual void executeThread(void) { + virtual void executeThread() { if (pCallback) pCallback(this, pCallbackArg); } //! Suspend thread - virtual void suspendThread(void) { + virtual void suspendThread() { if (isThreadSuspended()) return; if (pThread) OSSuspendThread(pThread); } //! Resume thread - virtual void resumeThread(void) { + virtual void resumeThread() { if (!isThreadSuspended()) return; if (pThread) OSResumeThread(pThread); } @@ -80,30 +79,30 @@ public: } //! Check if thread is suspended - virtual BOOL isThreadSuspended(void) const { + [[nodiscard]] virtual BOOL isThreadSuspended() const { if (pThread) return OSIsThreadSuspended(pThread); return false; } //! Check if thread is terminated - virtual BOOL isThreadTerminated(void) const { + [[nodiscard]] virtual BOOL isThreadTerminated() const { if (pThread) return OSIsThreadTerminated(pThread); return false; } //! Check if thread is running - virtual BOOL isThreadRunning(void) const { + [[nodiscard]] virtual BOOL isThreadRunning() const { return !isThreadSuspended() && !isThreadRunning(); } //! Shutdown thread - virtual void shutdownThread(void) { + virtual void shutdownThread() { //! wait for thread to finish if (pThread && !(iAttributes & eAttributeDetach)) { if (isThreadSuspended()) resumeThread(); - OSJoinThread(pThread, NULL); + OSJoinThread(pThread, nullptr); } //! free the thread stack buffer if (pThreadStack) @@ -111,18 +110,18 @@ public: if (pThread) free(pThread); - pThread = NULL; - pThreadStack = NULL; + pThread = nullptr; + pThreadStack = nullptr; } //! Thread attributes enum eCThreadAttributes { - eAttributeNone = 0x07, - eAttributeAffCore0 = 0x01, - eAttributeAffCore1 = 0x02, - eAttributeAffCore2 = 0x04, - eAttributeDetach = 0x08, - eAttributePinnedAff = 0x10 + eAttributeNone = 0x07, + eAttributeAffCore0 = 0x01, + eAttributeAffCore1 = 0x02, + eAttributeAffCore2 = 0x04, + eAttributeDetach = 0x08, + eAttributePinnedAff = 0x10 }; private: static int threadCallback(int argc, const char **argv) { @@ -136,6 +135,4 @@ private: uint8_t *pThreadStack; Callback pCallback; void *pCallbackArg; -}; - -#endif +}; \ No newline at end of file diff --git a/src/utils/TcpReceiver.cpp b/src/utils/TcpReceiver.cpp index b54809a..6b26b29 100644 --- a/src/utils/TcpReceiver.cpp +++ b/src/utils/TcpReceiver.cpp @@ -34,7 +34,7 @@ void _SYSLaunchTitleWithStdArgsInNoSplash(uint64_t, uint32_t); } TcpReceiver::TcpReceiver(int32_t port) - : CThread(CThread::eAttributeAffCore1, 16,0x20000), exitRequested(false), serverPort(port), serverSocket(-1) { + : CThread(CThread::eAttributeAffCore1, 16, 0x20000), exitRequested(false), serverPort(port), serverSocket(-1) { resumeThread(); } @@ -58,7 +58,7 @@ void TcpReceiver::executeThread() { uint32_t enable = 1; setsockopt(serverSocket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable)); - struct sockaddr_in bindAddress; + struct sockaddr_in bindAddress{}; memset(&bindAddress, 0, sizeof(bindAddress)); bindAddress.sin_family = AF_INET; bindAddress.sin_port = serverPort; @@ -76,7 +76,7 @@ void TcpReceiver::executeThread() { return; } - struct sockaddr_in clientAddr; + struct sockaddr_in clientAddr{}; memset(&clientAddr, 0, sizeof(clientAddr)); int32_t addrlen = sizeof(struct sockaddr); @@ -91,9 +91,9 @@ void TcpReceiver::executeThread() { close(clientSocket); if (result > 0) - if (result >= 0){ - break; - } + if (result >= 0) { + break; + } } else { DEBUG_FUNCTION_LINE("Server socket accept failed %i %d", clientSocket, errno); OSSleepTicks(OSMicrosecondsToTicks(100000)); @@ -105,6 +105,7 @@ void TcpReceiver::executeThread() { extern bool gDoRelaunch; + int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { DEBUG_FUNCTION_LINE("Loading file from ip %08X", ipAddress); @@ -120,13 +121,13 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { recvwait(clientSocket, (unsigned char *) &fileSizeUnc, sizeof(fileSizeUnc)); // Compressed protocol, read another 4 bytes } - struct in_addr in; + struct in_addr in{}; uint32_t bytesRead = 0; in.s_addr = ipAddress; DEBUG_FUNCTION_LINE("transfer start"); - unsigned char *loadAddress = (unsigned char *) memalign(0x40, fileSize); + auto *loadAddress = (unsigned char *) memalign(0x40, fileSize); if (!loadAddress) { OSSleepTicks(OSSecondsToTicks(1)); return NOT_ENOUGH_MEMORY; @@ -160,7 +161,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 = nullptr; // We need to unzip... if (loadAddress[0] == 'P' && loadAddress[1] == 'K' && loadAddress[2] == 0x03 && loadAddress[3] == 0x04) { @@ -233,7 +234,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { FSUtils::CreateSubfolder(RPX_TEMP_PATH); res = FSUtils::saveBufferToFile(WUHB_TEMP_FILE, inflatedData, fileSize); file_path = WUHB_TEMP_FILE_EX; - if(!res){ + if (!res) { // temp.wuhb might be mounted, let's try temp2.wuhb res = FSUtils::saveBufferToFile(WUHB_TEMP_FILE_2, inflatedData, fileSize); file_path = WUHB_TEMP_FILE_2_EX; @@ -253,7 +254,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { std::vector finalList; finalList.push_back(newContainer.value()); - for (auto &plugin : oldPlugins) { + for (auto &plugin: oldPlugins) { if (plugin.metaInformation.getName() == newContainer->metaInformation.getName() && plugin.metaInformation.getAuthor() == newContainer->metaInformation.getAuthor() ) { @@ -265,7 +266,7 @@ int32_t TcpReceiver::loadToMemory(int32_t clientSocket, uint32_t ipAddress) { } } - for (auto &plugin : finalList) { + for (auto &plugin: finalList) { DEBUG_FUNCTION_LINE("name: %s", plugin.getMetaInformation().getName().c_str()); DEBUG_FUNCTION_LINE("author: %s", plugin.getMetaInformation().getAuthor().c_str()); DEBUG_FUNCTION_LINE("handle: %08X", plugin.getPluginData().getHandle()); diff --git a/src/utils/TcpReceiver.h b/src/utils/TcpReceiver.h index 255f5d8..40cb78e 100644 --- a/src/utils/TcpReceiver.h +++ b/src/utils/TcpReceiver.h @@ -17,20 +17,18 @@ public: NOT_A_VALID_PLUGIN = -5, }; - TcpReceiver(int32_t port); + explicit TcpReceiver(int32_t port); - ~TcpReceiver(); + ~TcpReceiver() override; //sigslot::signal2 serverReceiveStart; //sigslot::signal3 serverReceiveFinished; private: - void executeThread(); + void executeThread() override; - int32_t loadToMemory(int32_t clientSocket, uint32_t ipAddress); - - bool saveFileToSDCard(const char *path, void *buffer, uint32_t size); + static int32_t loadToMemory(int32_t clientSocket, uint32_t ipAddress); bool exitRequested; int32_t serverPort; diff --git a/src/utils/logger.h b/src/utils/logger.h index 95f28f9..b760684 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -16,11 +16,11 @@ extern "C" { #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0); + } while (0) #define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0); + } while (0) #ifdef __cplusplus } diff --git a/src/utils/net.c b/src/utils/net.c index ebc6ee2..42453bf 100644 --- a/src/utils/net.c +++ b/src/utils/net.c @@ -7,6 +7,7 @@ int32_t recvwait(int32_t sock, void *buffer, int32_t len) { while (socket_lock) { usleep(1000); } + socket_lock = 1; int32_t ret; while (len > 0) { ret = recv(sock, buffer, len, 0); @@ -45,6 +46,7 @@ int32_t checkbyte(int32_t sock) { while (socket_lock) { usleep(1000); } + socket_lock = 1; unsigned char buffer[1]; int32_t ret; @@ -61,6 +63,7 @@ int32_t sendwait(int32_t sock, const void *buffer, int32_t len) { while (socket_lock) { usleep(1000); } + socket_lock = 1; int32_t ret; while (len > 0) { // For some reason the send blocks/crashes if the buffer is too big.. diff --git a/src/utils/net.h b/src/utils/net.h index 3665713..415c20a 100644 --- a/src/utils/net.h +++ b/src/utils/net.h @@ -1,5 +1,4 @@ -#ifndef _UTILS_NET_H_ -#define _UTILS_NET_H_ +#pragma once #include @@ -32,6 +31,4 @@ int32_t sendbyte(int32_t sock, unsigned char byte); #ifdef __cplusplus } -#endif - -#endif +#endif \ No newline at end of file