ArchiveHandle::ArchiveHandle(const char * filepath) { szFile = NULL; zipFile = NULL; rarFile = NULL; char checkbuffer[6]; memset(checkbuffer, 0, sizeof(checkbuffer)); FILE * file = fopen(filepath, "rb"); if(!file) return; int ret = 1; while(checkbuffer[0] == 0 && ret > 0) ret = fread(&checkbuffer, 1, 1, file); fread(&checkbuffer[1], 1, 5, file); fclose(file); if(IsZipFile(checkbuffer)) zipFile = new ZipFile(filepath); if(Is7ZipFile(checkbuffer)) szFile = new SzFile(filepath); if(IsRarFile(checkbuffer)) rarFile = new RarFile(filepath); }
/**************************************************************************** * LoadFATFile ***************************************************************************/ int LoadFATFile (char * rbuffer, int length) { char zipbuffer[2048]; char filepath[MAXPATHLEN]; u32 size; /* Check filename length */ if (!MakeROMPath(filepath, METHOD_SD)) { WaitPrompt((char*) "Maximum filepath length reached!"); return -1; } fatfile = fopen (filepath, "rb"); if (fatfile > 0) { if(length > 0 && length <= 2048) // do a partial read (eg: to check file header) { fread (rbuffer, 1, length, fatfile); size = length; } else // load whole file { fread (zipbuffer, 1, 2048, fatfile); if (IsZipFile (zipbuffer)) { size = UnZipBuffer ((unsigned char *)rbuffer, METHOD_SD); // unzip from FAT } else { // Just load the file up fseek(fatfile, 0, SEEK_END); size = ftell(fatfile); // get filesize fseek(fatfile, 2048, SEEK_SET); // seek back to point where we left off memcpy (rbuffer, zipbuffer, 2048); // copy what we already read ShowProgress ((char *)"Loading...", 2048, size); u32 offset = 2048; while(offset < size) { offset += fread (rbuffer + offset, 1, (1024*512), fatfile); // read in 512K chunks ShowProgress ((char *)"Loading...", offset, size); } } } fclose (fatfile); return size; } else { WaitPrompt((char*) "Error opening file"); return 0; } }
/** * LoadDVDFile * * This function will load a file from DVD, in BIN, SMD or ZIP format. * The values for offset and length are inherited from rootdir and * rootdirlength. * * The buffer parameter should re-use the initial ROM buffer. */ int LoadDVDFile (unsigned char *buffer) { int offset; int blocks; int i; u64 discoffset; char readbuffer[2048]; /*** SDCard Addition ***/ if (rootdirlength == 0) return 0; /*** How many 2k blocks to read ***/ blocks = rootdirlength / 2048; offset = 0; discoffset = rootdir; ShowAction ((char*) "Loading ... Wait"); dvd_read (readbuffer, 2048, discoffset); if (!IsZipFile (readbuffer)) { for (i = 0; i < blocks; i++) { dvd_read (readbuffer, 2048, discoffset); memcpy (buffer + offset, readbuffer, 2048); offset += 2048; discoffset += 2048; } /*** And final cleanup ***/ if (rootdirlength % 2048) { i = rootdirlength % 2048; dvd_read (readbuffer, 2048, discoffset); memcpy (buffer + offset, readbuffer, i); } } else { return UnZipBuffer (buffer, discoffset, rootdirlength); } return rootdirlength; }
/**************************************************************************** * LoadFATFile ****************************************************************************/ int LoadFATFile (char *filename, int length) { char zipbuffer[2048]; char filepath[MAXPATHLEN]; FILE *handle; u32 size; /* Check filename length */ if ((strlen(currentdir)+1+strlen(filelist[selection].filename)) < MAXPATHLEN) sprintf(filepath, "%s/%s",currentdir,filelist[selection].filename); else { WaitPrompt((char*) "Maximum filepath length reached!"); return -1; } handle = fopen (filepath, "rb"); if (handle > 0) { fread (zipbuffer, 1, 2048, handle); if (IsZipFile (zipbuffer)) { size = UnZipFATFile (nesromptr, handle); // unzip from FAT } else { // Just load the file up fseek(handle, 0, SEEK_END); length = ftell(handle); // get filesize fseek(handle, 2048, SEEK_SET); // seek back to point where we left off memcpy (nesromptr, zipbuffer, 2048); // copy what we already read fread (nesromptr + 2048, 1, length - 2048, handle); size = length; } fclose (handle); return size; } else { WaitPrompt((char*) "Error opening file"); return 0; } return 0; }
int LoadBufferFromSMB (char * sbuffer, char *filepath, bool silent) { if(!ConnectShare (NOTSILENT)) return 0; SMBFILE smbfile; int ret; int boffset = 0; smbfile = SMB_OpenFile (SMBPath(filepath), SMB_OPEN_READING, SMB_OF_OPEN, smbconn); if (!smbfile) { if(!silent) { char msg[100]; sprintf(msg, "Couldn't open SMB: %s", SMBPath(filepath)); WaitPrompt (msg); } return 0; } ret = SMB_ReadFile (sbuffer, 1024, boffset, smbfile); if (IsZipFile (sbuffer)) { boffset = UnZipFile ((unsigned char *)sbuffer, smbfile); // unzip from SMB } else { // Just load the file up while ((ret = SMB_ReadFile (sbuffer + boffset, 1024, boffset, smbfile)) > 0) boffset += ret; } SMB_CloseFile (smbfile); return boffset; }
/**************************************************************************** * LoadFile ***************************************************************************/ size_t LoadFile (char * rbuffer, char *filepath, size_t length, bool silent) { char zipbuffer[2048]; size_t size = 0, offset = 0, readsize = 0; int retry = 1; int device; if(!FindDevice(filepath, &device)) return 0; // stop checking if devices were removed/inserted // since we're loading a file HaltDeviceThread(); // halt parsing HaltParseThread(); // open the file while(retry) { if(!ChangeInterface(device, silent)) break; file = fopen (filepath, "rb"); if(!file) { if(silent) break; retry = ErrorPromptRetry("Error opening file!"); continue; } if(length > 0 && length <= 2048) // do a partial read (eg: to check file header) { size = fread (rbuffer, 1, length, file); } else // load whole file { readsize = fread (zipbuffer, 1, 32, file); if(!readsize) { unmountRequired[device] = true; retry = ErrorPromptRetry("Error reading file!"); fclose (file); continue; } if (IsZipFile (zipbuffer)) { size = UnZipBuffer ((unsigned char *)rbuffer); // unzip } else { fseeko(file,0,SEEK_END); size = ftello(file); fseeko(file,0,SEEK_SET); while(!feof(file)) { // If the size requested is *less* than the filesize, only read that much - we don't want to overrun the buffer int toread = 4096; if (length > 0 && offset+toread > length) { toread = length - offset; } ShowProgress ("Loading...", offset, size); readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk if(readsize <= 0) break; // reading finished (or failed) offset += readsize; if (length > 0 && offset >= length) { break; } } size = offset; CancelAction(); } } retry = 0; fclose (file); } // go back to checking if devices were inserted/removed ResumeDeviceThread(); CancelAction(); return size; }
int LoadSDFile (char *filename, int length) { char zipbuffer[2048]; char filepath[MAXPATHLEN]; FILE *handle; char *rbuffer; PKZIPHEADER pkzip; z_stream zs; int res, outbytes = 0; int size; int have; rbuffer = (char *) Memory.ROM; /* Check filename length */ if ((strlen(currSDdir)+1+strlen(filelist[selection].filename)) < MAXPATHLEN) sprintf(filepath, "%s/%s",currSDdir,filelist[selection].filename); else { WaitPrompt((char*) "Maximum Filename Length reached !"); haveSDdir = 0; // reset everything before next access return -1; } handle = fopen (filepath, "rb"); if (handle > 0) { fread (zipbuffer, 1, 2048, handle); if (IsZipFile (zipbuffer)) { /*** Unzip the ROM ***/ memcpy (&pkzip, zipbuffer, sizeof (PKZIPHEADER)); pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize); memset (&zs, 0, sizeof (zs)); zs.zalloc = Z_NULL; zs.zfree = Z_NULL; zs.opaque = Z_NULL; zs.avail_in = 0; zs.next_in = Z_NULL; res = inflateInit2 (&zs, -MAX_WBITS); if (res != Z_OK) { fclose (handle); return 0; } size = (sizeof (PKZIPHEADER) + FLIP16 (pkzip.filenameLength) + FLIP16 (pkzip.extraDataLength)); do { zs.avail_in = 2048 - size; zs.next_in = (Bytef *) zipbuffer + size; do { zs.avail_out = 16384; zs.next_out = (Bytef *) output; res = inflate (&zs, Z_NO_FLUSH); have = 16384 - zs.avail_out; if (have) { memcpy (rbuffer + outbytes, output, have); outbytes += have; } } while (zs.avail_out == 0); sprintf (filepath, "Read %d bytes of %d", outbytes, pkzip.uncompressedSize); //ShowAction (filepath); ShowProgress (filepath, outbytes, pkzip.uncompressedSize); size = 0; fread (zipbuffer, 1, 2048, handle); } while (res != Z_STREAM_END && (u32) outbytes < pkzip.uncompressedSize); inflateEnd (&zs); fclose (handle); return pkzip.uncompressedSize; } else { /*** Just load the file up ***/ fseek(handle, 0, SEEK_END); length = ftell(handle); // get filesize fseek(handle, 2048, SEEK_SET); // seek back to point where we left off sprintf (filepath, "Loading %d bytes", length); ShowAction (filepath); memcpy (rbuffer, zipbuffer, 2048); // copy what we already read fread (rbuffer + 2048, 1, length - 2048, handle); fclose (handle); return length; } } else { WaitPrompt((char*) "Error opening file"); return 0; } return 0; }
/**************************************************************************** * FAT_LoadFile * * This function will load a BIN, SMD or ZIP file from DVD into the ROM buffer. * This functions return the actual size of data copied into the buffer * ****************************************************************************/ int FAT_LoadFile(u8 *buffer, u32 selection) { /* If loading from history then we need to setup a few more things. */ if(useHistory) { /* Get the parent folder for the file. */ strncpy(fatdir, history.entries[selection].filepath, MAXJOLIET-1); fatdir[MAXJOLIET-1] = '\0'; /* Get the length of the file. This has to be done * before calling LoadFile(). */ char filepath[MAXJOLIET]; struct stat filestat; snprintf(filepath, MAXJOLIET-1, "%s%s", history.entries[selection].filepath, history.entries[selection].filename); filepath[MAXJOLIET-1] = '\0'; if(stat(filepath, &filestat) == 0) { filelist[selection].length = filestat.st_size; } /* update filelist */ haveFATdir = 0; } /* file size */ int length = filelist[selection].length; if (length > 0) { /* Add/move the file to the top of the history. */ history_add_file(fatdir, filelist[selection].filename); /* full filename */ char fname[MAXPATHLEN]; sprintf(fname, "%s%s",fatdir,filelist[selection].filename); /* open file */ FILE *sdfile = fopen(fname, "rb"); if (sdfile == NULL) { GUI_WaitPrompt("Error","Unable to open file !"); haveFATdir = 0; return 0; } /* Read first data chunk */ unsigned char temp[FATCHUNK]; fread(temp, FATCHUNK, 1, sdfile); fclose(sdfile); /* determine file type */ if (!IsZipFile ((char *) temp)) { /* re-open and read file */ sdfile = fopen(fname, "rb"); if (sdfile) { char msg[50]; sprintf(msg,"Loading %d bytes ...", length); GUI_MsgBoxOpen("Information",msg,1); int i = 0; while (length > FATCHUNK) { fread(buffer+i, FATCHUNK, 1, sdfile); length -= FATCHUNK; i += FATCHUNK; } fread(buffer+i, length, 1, sdfile); fclose(sdfile); return filelist[selection].length; } } else { /* unzip file */ return UnZipBuffer (buffer, 0, fname); } } return 0; }
// // LoadFile - Can be used in a threaded scenario so while loading graphics can update // - Probably not needed on PS3. Loading always seems instant. // size_t LoadFile (char * rbuffer, const char *filepath, size_t length, bool silent) { FCEU_PrintError("LoadFile(%p, %c, %d, %b)", rbuffer, filepath, length, silent); char zipbuffer[2048]; size_t size = 0, offset = 0, readsize = 0; int retry = 1; int device; // open the file while(retry) { file = fopen (filepath, "rb"); if(!file) { FCEU_PrintError("1: LoadFile %s failed", filepath); return 0; } /*if(length > 0 && length <= 2048) // do a partial read (eg: to check file header) { size = fread (rbuffer, 1, length, file); } else // load whole file*/ { readsize = fread (zipbuffer, 1, 32, file); if(!readsize) { fclose (file); FCEU_PrintError("2: LoadFile %s failed", filepath); return 0; } if (IsZipFile (zipbuffer)) { size = UnZipBuffer ((unsigned char *)rbuffer); // unzip } else { fseek(file,0,SEEK_END); size = ftell(file); fseek(file,0,SEEK_SET); //fseeko(file,0,SEEK_END); //size = ftello(file); //fseeko(file,0,SEEK_SET); while(!feof(file)) { FCEU_printf("Loading...%d/%d", offset, size); readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk if(readsize <= 0) break; // reading finished (or failed) offset += readsize; } size = offset; } } retry = 0; fclose (file); } // go back to checking if devices were inserted/removed return size; }