From 169468ad603445b8c41797873fdffd08968dd256 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 4 Mar 2018 16:10:02 +0100 Subject: [PATCH] Added "dumpHex", "FSUtils::saveBufferToFile" and "recvwait" --- Makefile | 2 +- source/fs/FSUtils.cpp | 14 +++++++++ source/fs/FSUtils.h | 1 + source/utils/net.c | 20 +++++++++++++ source/utils/net.h | 14 +++++++++ source/utils/utils.c | 42 +++++++++++++++++++++++++++ source/utils/utils.h | 3 ++ source/utils/{utils.s => utils_asm.s} | 0 8 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 source/utils/net.c create mode 100644 source/utils/net.h create mode 100644 source/utils/utils.c rename source/utils/{utils.s => utils_asm.s} (100%) diff --git a/Makefile b/Makefile index 7302bea..0ea09bc 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ LIB := lib #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- -CFLAGS = -g -O2 -Wall -D__wiiu__ -D_GNU_SOURCE $(MACHDEP) $(INCLUDE) +CFLAGS = -g -Os -Wall -D__wiiu__ -D_GNU_SOURCE $(MACHDEP) $(INCLUDE) CXXFLAGS = $(CFLAGS) ifeq ($(DO_LOGGING), 1) diff --git a/source/fs/FSUtils.cpp b/source/fs/FSUtils.cpp index df34f3c..69040c5 100644 --- a/source/fs/FSUtils.cpp +++ b/source/fs/FSUtils.cpp @@ -4,6 +4,7 @@ #include #include #include "FSUtils.h" +#include "CFile.hpp" s32 FSUtils::LoadFileToMem(const char *filepath, u8 **inbuffer, u32 *size){ //! always initialze input @@ -136,3 +137,16 @@ s32 FSUtils::CreateSubfolder(const char * fullpath){ return 1; } + +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()){ + return false; + } + file.write((const u8*) buffer,size); + file.close(); + return true; +} + diff --git a/source/fs/FSUtils.h b/source/fs/FSUtils.h index f642ad7..a9fa1a9 100644 --- a/source/fs/FSUtils.h +++ b/source/fs/FSUtils.h @@ -9,6 +9,7 @@ class FSUtils{ //! 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_ diff --git a/source/utils/net.c b/source/utils/net.c new file mode 100644 index 0000000..01e5eab --- /dev/null +++ b/source/utils/net.c @@ -0,0 +1,20 @@ +#include "dynamic_libs/socket_functions.h" + +int recvwait(int sock, unsigned char *buffer, int len) +{ + int recvBytes = 0; + + while(len) + { + int ret = recv(sock, buffer, len, 0); + if(ret <= 0) { + return ret; + } + + len -= ret; + buffer += ret; + recvBytes += ret; + } + + return recvBytes; +} diff --git a/source/utils/net.h b/source/utils/net.h new file mode 100644 index 0000000..9f543c3 --- /dev/null +++ b/source/utils/net.h @@ -0,0 +1,14 @@ +#ifndef __NET_H_ +#define __NET_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +int recvwait(int sock, unsigned char *buffer, int len); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/source/utils/utils.c b/source/utils/utils.c new file mode 100644 index 0000000..00c9faf --- /dev/null +++ b/source/utils/utils.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include +#include "utils.h" +#include "logger.h" + +// https://gist.github.com/ccbrown/9722406 +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]; + } else { + ascii[i % 16] = '.'; + } + if ((i+1) % 8 == 0 || i+1 == size) { + log_printf(" "); + 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); + } + } 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) { + log_printf(" "); + } + log_printf("| %s \n", ascii); + } + } + } +} \ No newline at end of file diff --git a/source/utils/utils.h b/source/utils/utils.h index 95bf98d..39324ed 100644 --- a/source/utils/utils.h +++ b/source/utils/utils.h @@ -41,6 +41,9 @@ extern "C" { unsigned int getApplicationEndAddr(void); +//Need to have log_init() called beforehand. +void dumpHex(const void* data, size_t size); + #ifdef __cplusplus } #endif diff --git a/source/utils/utils.s b/source/utils/utils_asm.s similarity index 100% rename from source/utils/utils.s rename to source/utils/utils_asm.s