From 3ca3b1220d379aa27792be769be6527265d1f5e4 Mon Sep 17 00:00:00 2001 From: Stefanos Kornilios Mitsis Poiitidis Date: Fri, 9 Jan 2015 15:50:07 +0100 Subject: [PATCH] http support for cdi & gdi, updated based gdipsr - Fix coreio fsize - Fix coreio fopen to seek to 0 - Fix coreio/http to smart-escape urls - For urls that include \ or % in the filenames, you have to pass them escaped - Update gdi parser to use (mostly) streamstream. That code is horrible and should be rewritten - coreio core is hacky and horrible at places as well - Update imgreader to use coreio - Update cdi parser + driver to use coreio --- core/deps/chdpsr/cdipsr.cpp | 8 +++-- core/deps/chdpsr/cdipsr.h | 15 ++++----- core/deps/coreio/coreio.cpp | 40 +++++++++++++++++++--- core/deps/coreio/coreio.h | 3 +- core/imgread/cdi.cpp | 22 ++++++------- core/imgread/common.h | 12 ++++--- core/imgread/gdi.cpp | 66 +++++++++++++++++++++++++------------ 7 files changed, 114 insertions(+), 52 deletions(-) diff --git a/core/deps/chdpsr/cdipsr.cpp b/core/deps/chdpsr/cdipsr.cpp index 33456fd5c..dcb054187 100644 --- a/core/deps/chdpsr/cdipsr.cpp +++ b/core/deps/chdpsr/cdipsr.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "deps/coreio/coreio.h" #include "cdipsr.h" // Global variables @@ -11,6 +12,10 @@ unsigned long temp_value; ///////////////////////////////////////////////////////////////////////////// +#define FILE core_file +#define fread(buff,sz,cnt,fc) core_fread(fc,buff,sz*cnt) +#define fseek core_fseek +#define ftell core_ftell unsigned long ask_type(FILE *fsource, long header_position) { @@ -110,8 +115,7 @@ void CDI_get_tracks (FILE *fsource, image_s *image) void CDI_init (FILE *fsource, image_s *image, char *fsourcename) { - fseek(fsource, 0L, SEEK_END); - image->length = ftell(fsource); + image->length = core_fsize(fsource); if (image->length < 8) printf( "Image file is too short"); diff --git a/core/deps/chdpsr/cdipsr.h b/core/deps/chdpsr/cdipsr.h index 22e4c560b..b4bfaa6d7 100644 --- a/core/deps/chdpsr/cdipsr.h +++ b/core/deps/chdpsr/cdipsr.h @@ -1,7 +1,7 @@ #ifndef __CDI_H__ #define __CDI_H__ - +#include "deps/coreio/coreio.h" /* Basic structures */ @@ -38,12 +38,11 @@ typedef struct track_s #define CDI_V3 0x80000005 #define CDI_V35 0x80000006 -unsigned long ask_type(FILE *fsource, long header_position); -void CDI_init (FILE *fsource, image_s *image, char *fsourcename); -void CDI_get_sessions (FILE *fsource, image_s *image); -void CDI_get_tracks (FILE *fsource, image_s *image); -void CDI_read_track (FILE *fsource, image_s *image, track_s *track); -void CDI_skip_next_session (FILE *fsource, image_s *image); +unsigned long ask_type(core_file *fsource, long header_position); +void CDI_init(core_file *fsource, image_s *image, char *fsourcename); +void CDI_get_sessions(core_file *fsource, image_s *image); +void CDI_get_tracks(core_file *fsource, image_s *image); +void CDI_read_track(core_file *fsource, image_s *image, track_s *track); +void CDI_skip_next_session(core_file *fsource, image_s *image); #endif - diff --git a/core/deps/coreio/coreio.cpp b/core/deps/coreio/coreio.cpp index f726bb17c..f7d8922ab 100644 --- a/core/deps/coreio/coreio.cpp +++ b/core/deps/coreio/coreio.cpp @@ -5,6 +5,10 @@ #include #include #include +#include +#include +#include + #define TRUE 1 #define FALSE 0 @@ -22,6 +26,27 @@ #include #endif +string url_encode(const string &value) { + ostringstream escaped; + escaped.fill('0'); + escaped << hex; + + for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) { + string::value_type c = (*i); + + // Keep alphanumeric and other accepted characters intact + if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' || c == '/' || c =='%' ) { + escaped << c; + continue; + } + + // Any other characters are percent-encoded + escaped << '%' << setw(2) << int((unsigned char)c); + } + + return escaped.str(); +} + size_t HTTP_GET(string host, int port,string path, size_t offs, size_t len, void* pdata){ string request; string response; @@ -97,7 +122,7 @@ size_t HTTP_GET(string host, int port,string path, size_t offs, size_t len, void */ size_t content_length = 0; - size_t rv = content_length; + size_t rv = 0; for (;;) { stringstream ss; @@ -128,10 +153,10 @@ size_t HTTP_GET(string host, int port,string path, size_t offs, size_t len, void _data: if (len == 0) { - + rv = content_length; } else { - verify(len == content_length); + verify(len == content_length); //crash is a bit too harsh here perhaps? u8* ptr = (u8*)pdata; do { @@ -141,6 +166,8 @@ _data: ptr += rcv; } while (len >0); + + rv = content_length; } _cleanup: @@ -180,7 +207,7 @@ core_file* core_fopen(const char* filename) rv->host = p.substr(7,p.npos); rv->host = rv->host.substr(0, rv->host.find_first_of("/")); - rv->path = p.substr(p.find("/", 7), p.npos); + rv->path = url_encode(p.substr(p.find("/", 7), p.npos)); rv->port = 80; size_t pos = rv->host.find_first_of(":"); @@ -198,6 +225,7 @@ core_file* core_fopen(const char* filename) } } + core_fseek((core_file*)rv, 0, SEEK_SET); return (core_file*)rv; } @@ -217,6 +245,10 @@ size_t core_fseek(core_file* fc, size_t offs, size_t origin) { return 0; } +size_t core_ftell(core_file* fc) { + CORE_FILE* f = (CORE_FILE*)fc; + return f->seek_ptr; +} int core_fread(core_file* fc, void* buff, size_t len) { CORE_FILE* f = (CORE_FILE*)fc; diff --git a/core/deps/coreio/coreio.h b/core/deps/coreio/coreio.h index cdf28435c..796d29049 100644 --- a/core/deps/coreio/coreio.h +++ b/core/deps/coreio/coreio.h @@ -9,4 +9,5 @@ core_file* core_fopen(const char* filename); size_t core_fseek(core_file* fc, size_t offs, size_t origin); int core_fread(core_file* fc, void* buff, size_t len); int core_fclose(core_file* fc); -size_t core_fsize(core_file* fc); \ No newline at end of file +size_t core_fsize(core_file* fc); +size_t core_ftell(core_file* fc); \ No newline at end of file diff --git a/core/imgread/cdi.cpp b/core/imgread/cdi.cpp index 068652fc0..913259406 100644 --- a/core/imgread/cdi.cpp +++ b/core/imgread/cdi.cpp @@ -8,7 +8,7 @@ Disc* cdi_parse(const wchar* file) { - FILE* fsource=fopen(file,"rb"); + core_file* fsource=core_fopen(file); if (!fsource) return 0; @@ -34,7 +34,7 @@ Disc* cdi_parse(const wchar* file) CDI_get_tracks (fsource, &image); - image.header_position = ftell(fsource); + image.header_position = core_ftell(fsource); printf("\nSession %d has %d track(s)\n",image.global_current_session,image.tracks); @@ -54,7 +54,7 @@ Disc* cdi_parse(const wchar* file) CDI_read_track (fsource, &image, &track); - image.header_position = ftell(fsource); + image.header_position = core_ftell(fsource); // Show info @@ -98,7 +98,7 @@ Disc* cdi_parse(const wchar* file) t.CTRL=track.mode==0?0:4; t.StartFAD=track.start_lba+track.pregap_length; t.EndFAD=t.StartFAD+track.length-1; - t.file = new RawTrackFile(fopen(file,"rb"),track.position + track.pregap_length * track.sector_size,t.StartFAD,track.sector_size); + t.file = new RawTrackFile(core_fopen(file),track.position + track.pregap_length * track.sector_size,t.StartFAD,track.sector_size); rv->tracks.push_back(t); @@ -115,21 +115,21 @@ Disc* cdi_parse(const wchar* file) if (track.total_length < track.length + track.pregap_length) { printf("\nThis track seems truncated. Skipping...\n"); - fseek(fsource, track.position, SEEK_SET); - fseek(fsource, track.total_length, SEEK_CUR); - track.position = ftell(fsource); + core_fseek(fsource, track.position, SEEK_SET); + core_fseek(fsource, track.total_length, SEEK_CUR); + track.position = core_ftell(fsource); } else { printf("Track position: %d\n",track.position + track.pregap_length * track.sector_size); - fseek(fsource, track.position, SEEK_SET); + core_fseek(fsource, track.position, SEEK_SET); // fseek(fsource, track->pregap_length * track->sector_size, SEEK_CUR); // fseek(fsource, track->length * track->sector_size, SEEK_CUR); - fseek(fsource, track.total_length * track.sector_size, SEEK_CUR); + core_fseek(fsource, track.total_length * track.sector_size, SEEK_CUR); //savetrack(fsource, &image, &track, &opts, &flags); - track.position = ftell(fsource); + track.position = core_ftell(fsource); rv->EndFAD=track.start_lba +track.total_length; // Generate cuesheet entries @@ -140,7 +140,7 @@ Disc* cdi_parse(const wchar* file) } } - fseek(fsource, image.header_position, SEEK_SET); + core_fseek(fsource, image.header_position, SEEK_SET); // Close loops diff --git a/core/imgread/common.h b/core/imgread/common.h index 5d84e4cd8..b6a6bf110 100644 --- a/core/imgread/common.h +++ b/core/imgread/common.h @@ -3,6 +3,8 @@ #include using namespace std; +#include "deps/coreio/coreio.h" + extern u32 NullDriveDiscType; struct TocTrackInfo { @@ -261,12 +263,12 @@ Disc* OpenDisc(const wchar* fn); struct RawTrackFile : TrackFile { - FILE* file; + core_file* file; s32 offset; u32 fmt; bool cleanup; - RawTrackFile(FILE* file,u32 file_offs,u32 first_fad,u32 secfmt) + RawTrackFile(core_file* file,u32 file_offs,u32 first_fad,u32 secfmt) { verify(file!=0); this->file=file; @@ -289,13 +291,13 @@ struct RawTrackFile : TrackFile verify(false); } - fseek(file,offset+FAD*fmt,SEEK_SET); - fread(dst,1,fmt,file); + core_fseek(file,offset+FAD*fmt,SEEK_SET); + core_fread(file, dst, fmt); } virtual ~RawTrackFile() { if (cleanup && file) - fclose(file); + core_fclose(file); } }; diff --git a/core/imgread/gdi.cpp b/core/imgread/gdi.cpp index 5863d7838..86bd922bf 100644 --- a/core/imgread/gdi.cpp +++ b/core/imgread/gdi.cpp @@ -1,5 +1,6 @@ #include "common.h" #include +#include Disc* load_gdi(const char* file) { @@ -8,13 +9,28 @@ Disc* load_gdi(const char* file) //memset(&gdi_toc,0xFFFFFFFF,sizeof(gdi_toc)); //memset(&gdi_ses,0xFFFFFFFF,sizeof(gdi_ses)); - FILE* t=fopen(file,"rb"); + core_file* t=core_fopen(file); if (!t) return 0; - fscanf(t,"%d\r\n",&iso_tc); + + size_t gdi_len = core_fsize(t); + + char gdi_data[8193] = { 0 }; + + if (gdi_len >= sizeof(gdi_data)) + { + core_fclose(t); + return 0; + } + + core_fread(t, gdi_data, gdi_len); + core_fclose(t); + + istringstream gdi(gdi_data); + + gdi >> iso_tc; printf("\nGDI : %d tracks\n",iso_tc); - char temp[512]; char path[512]; strcpy(path,file); size_t len=strlen(file); @@ -30,30 +46,38 @@ Disc* load_gdi(const char* file) s32 OFFSET=0; for (u32 i=0;itracks.push_back( - while(isspace(fgetc(t))) ; - fseek(t,-1,SEEK_CUR); - if (fgetc(t)=='"') + gdi >> TRACK; + gdi >> FADS; + gdi >> CTRL; + gdi >> SSIZE; + + char last; + + do { + gdi >> last; + } while (isspace(last)); + + if (last == '"') { - char c; - int i=0; - while((c=fgetc(t))!='"') - temp[i++]=c; - temp[i]=0; + for(;;) { + gdi >> last; + if (last == '"') + break; + track_filename += last; + } } else { - fseek(t,-1,SEEK_CUR); - fscanf(t,"%s",temp); + gdi >> track_filename; + track_filename = last + track_filename; } - fscanf(t,"%d\r\n",&OFFSET); - printf("file[%d] \"%s\": FAD:%d, CTRL:%d, SSIZE:%d, OFFSET:%d\n",TRACK,temp,FADS,CTRL,SSIZE,OFFSET); - + gdi >> OFFSET; + printf("file[%d] \"%s\": FAD:%d, CTRL:%d, SSIZE:%d, OFFSET:%d\n", TRACK, track_filename.c_str(), FADS, CTRL, SSIZE, OFFSET); Track t; t.ADDR=0; @@ -63,8 +87,8 @@ Disc* load_gdi(const char* file) if (SSIZE!=0) { - strcpy(pathptr,temp); - t.file = new RawTrackFile(fopen(path,"rb"),OFFSET,t.StartFAD,SSIZE); + strcpy(pathptr, track_filename.c_str()); + t.file = new RawTrackFile(core_fopen(path),OFFSET,t.StartFAD,SSIZE); } disc->tracks.push_back(t); }