From 7e0f6b5142d46f26f1ae0946aa71891197c5231b Mon Sep 17 00:00:00 2001 From: masterzorag Date: Fri, 7 Dec 2018 19:56:58 +0100 Subject: [PATCH 1/2] implement DataFromFile() add standard facility to open file, alloc, read and close if path start with "host0:" file will be readed via ps4link if not, from local via open/read/close this can be reused anytime we have to read a file in memory --- libps4link/include/ps4link.h | 1 + libps4link/source/ps4link.c | 68 +++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/libps4link/include/ps4link.h b/libps4link/include/ps4link.h index 78c47ef..97f0156 100644 --- a/libps4link/include/ps4link.h +++ b/libps4link/include/ps4link.h @@ -65,6 +65,7 @@ typedef struct OrbisDirEntry char name[256]; }OrbisDirEntry; +uint8_t *DataFromFile(const char *path, int additional_size); int ps4LinkOpen(const char *file, int flags, int mode); int ps4LinkClose(int fd); diff --git a/libps4link/source/ps4link.c b/libps4link/source/ps4link.c index 746e204..0b45341 100644 --- a/libps4link/source/ps4link.c +++ b/libps4link/source/ps4link.c @@ -17,7 +17,7 @@ #include #include #include - +#include #include "ps4link_internal.h" @@ -317,3 +317,69 @@ void ps4LinkFinish() debugNetFinish(); } } + +/* + uses standard open/lseek/read/close to access sandbox'ed content, + if patch starts with host0:/... , file will be readed via ps4link +*/ +uint8_t *DataFromFile(const char *path, int additional_size) +{ + int fd; // descriptor to manage file from /mnt/sanbox/... + int filesize = 0; // variable to control file size + int numread = 0; + int use_host = 0; + uint8_t *buf = NULL; // buffer for read from file + + + if(!memcmp(path, "host0:", 6)) use_host=1; // set flag + + debugNetPrintf(INFO,"DataFromFile(%s), use_host:%d, add:%d\n",path,use_host,additional_size); + + // we open file in read only + if(use_host) fd=ps4LinkOpen(path,O_RDONLY,0); + else fd=open(path,O_RDONLY); + + if(fd<0) //If we can't open file, print the error and return + { + debugNetPrintf(DEBUG,"open file returned error %d\n",fd); + return NULL; + } + + // Seek to end to get file size + if(use_host) filesize=ps4LinkLseek(fd,0,SEEK_END); + else filesize=lseek(fd,0,SEEK_END); + + if(filesize<0) // If we get an error print it and return + { + debugNetPrintf(DEBUG,"lseek returned error %d\n",fd); + if(buf) free(buf), buf = NULL; + goto end; + } + + buf=malloc(filesize + additional_size); // Reserve memory for read buffer + if(!buf) + return NULL; + + memset(buf, 0, filesize + additional_size); // wipe + + // Seek back to start and read filsesize bytes to buf + if(use_host) { + ps4LinkLseek(fd,0,SEEK_SET); + numread=ps4LinkRead(fd,buf,filesize); + } else { + lseek(fd,0,SEEK_SET); + numread=read(fd,buf,filesize); + } + +end: + if(use_host) ps4LinkClose(fd); + else close(fd); + + if(numread!=filesize) // if we don't get filesize bytes we are in trouble + { + debugNetPrintf(DEBUG,"read returned error %d\n",numread); + if(buf) free(buf), buf = NULL; + } + + return buf; // remember to free() +} From 9e92375f5dbb22f653a36b577a2323e150b6baf4 Mon Sep 17 00:00:00 2001 From: masterzorag Date: Fri, 7 Dec 2018 20:00:54 +0100 Subject: [PATCH 2/2] make use of DataFromFile() for .mod and .png files --- libmod/source/modplayer.c | 31 +++++++------------------ liborbis2d/source/orbis2dImagePng.c | 36 ++--------------------------- 2 files changed, 10 insertions(+), 57 deletions(-) diff --git a/libmod/source/modplayer.c b/libmod/source/modplayer.c index d95e883..4417fdc 100644 --- a/libmod/source/modplayer.c +++ b/libmod/source/modplayer.c @@ -111,8 +111,8 @@ static int m_TrackDat_num; static TrackData *m_TrackDat; // Stores info for each track being played static RowData *m_CurrentRow; // Pointer to the current row being played static int m_bPlaying; // Set to true when a mod is being played -static u8 *data; -int size = 0; +static u8 *data = NULL; + ////////////////////////////////////////////////////////////////////// // These are the public functions ////////////////////////////////////////////////////////////////////// @@ -183,8 +183,7 @@ void Mod_FreeTune() // Tear down all the mallocs done //free the file itself - if (data) - free(data); + if (data) free(data), data = NULL; // Free patterns for (i = 0; i < m_Patterns_num; i++) { for (row = 0; row < 64; row++) @@ -251,25 +250,11 @@ int Mod_Load(char *filename) int index = 0; int numsamples; char modname[21]; - int fd; - if ((fd = ps4LinkOpen(filename, O_RDONLY, 0)) > 0) { - // opened file, so get size now - size = ps4LinkLseek(fd, 0, SEEK_END); - ps4LinkLseek(fd, 0, SEEK_SET); - data = (unsigned char *) malloc(size + 8); - memset(data, 0, size + 8); - if (data != 0) { // Read file in - ps4LinkRead(fd, data, size); - } else { - printf("Error allocing\n"); - ps4LinkClose(fd); - return 0; - } - // Close file - ps4LinkClose(fd); - } else { //if we couldn't open the file - return 0; - } + + // allocate 8 bytes more than readed + data = DataFromFile(filename, 8); + if(!data) + return 0; //BPM_RATE = 130; BPM_RATE = 125; //PAL diff --git a/liborbis2d/source/orbis2dImagePng.c b/liborbis2d/source/orbis2dImagePng.c index 39e1a1e..1d788d0 100644 --- a/liborbis2d/source/orbis2dImagePng.c +++ b/liborbis2d/source/orbis2dImagePng.c @@ -163,41 +163,9 @@ Orbis2dTexture *orbis2dLoadPngFromHost(const char *path) Orbis2dTexture *orbis2dLoadPngFromHost_v2(const char *path) { - int fd; // descriptor to manage file from host0 - int filesize; // variable to control file size - uint8_t *buf=NULL; // buffer for read from host0 file - - // we open file in read only from host0 ps4sh include the full path with host0:/....... - fd=ps4LinkOpen(path,O_RDONLY,0); - - if(fd<0) //If we can't open file from host0 print the error and return - { - debugNetPrintf(DEBUG,"[PS4LINK] ps4LinkOpen returned error %d\n",fd); - return NULL; - } - filesize=ps4LinkLseek(fd,0,SEEK_END); // Seek to end to get file size - if(filesize<0) // If we get an error print it and return - { - debugNetPrintf(DEBUG,"[PS4LINK] ps4LinkSeek returned error %d\n",fd); - ps4LinkClose(fd); - return NULL; - } + uint8_t *buf=DataFromFile(path, 0); - ps4LinkLseek(fd,0,SEEK_SET); // Seek back to start - buf=malloc(filesize); // Reserve memory for read buffer - if(!buf) - return NULL; - - int numread=ps4LinkRead(fd,buf,filesize); //Read filsesize bytes to buf - ps4LinkClose(fd); //Close file - - if(numread!=filesize) //if we don't get filesize bytes we are in trouble - { - debugNetPrintf(DEBUG,"[PS4LINK] ps4LinkRead returned error %d\n",numread); - return NULL; - } - - return orbis2dLoadPngFromBuffer(buf); //create png from buf + return orbis2dLoadPngFromBuffer(buf); // create png from buf } // uses standard open/lseek/read/close to access sandbox'ed content