mirror of
https://github.com/Maschell/libutils.git
synced 2024-11-24 04:59:15 +01:00
Added "dumpHex", "FSUtils::saveBufferToFile" and "recvwait"
This commit is contained in:
parent
b115979fb9
commit
169468ad60
2
Makefile
2
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)
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
@ -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_
|
||||
|
20
source/utils/net.c
Normal file
20
source/utils/net.c
Normal file
@ -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;
|
||||
}
|
14
source/utils/net.h
Normal file
14
source/utils/net.h
Normal file
@ -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
|
42
source/utils/utils.c
Normal file
42
source/utils/utils.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user