/** * 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; }
/***************************************************************************** * UnZipBuffer * * It should be noted that there is a limit of 5MB total size for any ROM ******************************************************************************/ int UnZipBuffer (unsigned char *outbuffer, u64 discoffset, char *filename) { PKZIPHEADER pkzip; int zipoffset = 0; int zipchunk = 0; char out[ZIPCHUNK]; z_stream zs; int res; int bufferoffset = 0; int have = 0; char readbuffer[2048]; char msg[128]; FILE *fatfile = NULL; /*** FAT file support ***/ if (filename) { fatfile = fopen(filename, "rb"); if (fatfile == NULL) return 0; } /*** Read Zip Header ***/ if (fatfile) { fseek(fatfile, 0, SEEK_SET); fread(readbuffer, FATCHUNK, 1, fatfile); } else { dvd_read (&readbuffer, DVDCHUNK, discoffset); } /*** Copy PKZip header to local, used as info ***/ memcpy (&pkzip, &readbuffer, sizeof (PKZIPHEADER)); sprintf (msg, "Unzipping %d bytes ...", FLIP32 (pkzip.uncompressedSize)); GUI_MsgBoxOpen("Information",msg,1); /*** Prepare the zip stream ***/ memset (&zs, 0, sizeof (z_stream)); 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) { GUI_WaitPrompt("Error","Unable to unzip file !"); return 0; } /*** Set ZipChunk for first pass ***/ zipoffset = (sizeof (PKZIPHEADER) + FLIP16 (pkzip.filenameLength) + FLIP16 (pkzip.extraDataLength)); zipchunk = ZIPCHUNK - zipoffset; /*** Now do it! ***/ do { zs.avail_in = zipchunk; zs.next_in = (Bytef *) & readbuffer[zipoffset]; /*** Now inflate until input buffer is exhausted ***/ do { zs.avail_out = ZIPCHUNK; zs.next_out = (Bytef *) & out; res = inflate (&zs, Z_NO_FLUSH); if (res == Z_MEM_ERROR) { inflateEnd (&zs); GUI_WaitPrompt("Error","Unable to unzip file !"); return 0; } have = ZIPCHUNK - zs.avail_out; if (have) { /*** Copy to normal block buffer ***/ memcpy (&outbuffer[bufferoffset], &out, have); bufferoffset += have; } } while (zs.avail_out == 0); /*** Readup the next 2k block ***/ zipoffset = 0; zipchunk = ZIPCHUNK; if (fatfile) { fread(readbuffer, FATCHUNK, 1, fatfile); } else { discoffset += DVDCHUNK; dvd_read (&readbuffer, DVDCHUNK, discoffset); } } while (res != Z_STREAM_END); inflateEnd (&zs); /* close file */ if (fatfile) fclose(fatfile); if (res == Z_STREAM_END) { if (FLIP32 (pkzip.uncompressedSize) == (u32) bufferoffset) return bufferoffset; else return FLIP32 (pkzip.uncompressedSize); } return 0; }
int UnZipBuffer (unsigned char *outbuffer, int method) { PKZIPHEADER pkzip; int zipoffset = 0; int zipchunk = 0; char out[ZIPCHUNK]; z_stream zs; int res; int bufferoffset = 0; int readoffset = 0; int have = 0; char readbuffer[ZIPCHUNK]; u64 discoffset = 0; // Read Zip Header switch (method) { case METHOD_SD: case METHOD_USB: fseek(fatfile, 0, SEEK_SET); fread (readbuffer, 1, ZIPCHUNK, fatfile); break; case METHOD_DVD: discoffset = dvddir; dvd_read (readbuffer, ZIPCHUNK, discoffset); break; case METHOD_SMB: SMB_ReadFile(readbuffer, ZIPCHUNK, 0, smbfile); break; } /*** Copy PKZip header to local, used as info ***/ memcpy (&pkzip, readbuffer, sizeof (PKZIPHEADER)); pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize); ShowProgress ((char *)"Loading...", 0, pkzip.uncompressedSize); /*** Prepare the zip stream ***/ memset (&zs, 0, sizeof (z_stream)); 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) return 0; /*** Set ZipChunk for first pass ***/ zipoffset = (sizeof (PKZIPHEADER) + FLIP16 (pkzip.filenameLength) + FLIP16 (pkzip.extraDataLength)); zipchunk = ZIPCHUNK - zipoffset; /*** Now do it! ***/ do { zs.avail_in = zipchunk; zs.next_in = (Bytef *) & readbuffer[zipoffset]; /*** Now inflate until input buffer is exhausted ***/ do { zs.avail_out = ZIPCHUNK; zs.next_out = (Bytef *) & out; res = inflate (&zs, Z_NO_FLUSH); if (res == Z_MEM_ERROR) { inflateEnd (&zs); return 0; } have = ZIPCHUNK - zs.avail_out; if (have) { /*** Copy to normal block buffer ***/ memcpy (&outbuffer[bufferoffset], &out, have); bufferoffset += have; } } while (zs.avail_out == 0); // Readup the next 2k block zipoffset = 0; zipchunk = ZIPCHUNK; switch (method) { case METHOD_SD: case METHOD_USB: fread (readbuffer, 1, ZIPCHUNK, fatfile); break; case METHOD_DVD: readoffset += ZIPCHUNK; dvd_read (readbuffer, ZIPCHUNK, discoffset+readoffset); break; case METHOD_SMB: readoffset += ZIPCHUNK; SMB_ReadFile(readbuffer, ZIPCHUNK, readoffset, smbfile); break; } ShowProgress ((char *)"Loading...", bufferoffset, pkzip.uncompressedSize); } while (res != Z_STREAM_END); inflateEnd (&zs); if (res == Z_STREAM_END) { if (pkzip.uncompressedSize == (u32) bufferoffset) return bufferoffset; else return pkzip.uncompressedSize; } return 0; }
/***************************************************************************** * unzip * * It should be noted that there is a limit of 5MB total size for any ROM ******************************************************************************/ int UnZipBuffer (unsigned char *outbuffer, u64 discoffset, short where, FILE* filehandle) { PKZIPHEADER pkzip; int zipoffset = 0; int zipchunk = 0; char out[ZIPCHUNK]; z_stream zs; int res; int bufferoffset = 0; int have = 0; char readbuffer[2048]; char msg[128]; /*** Read Zip Header ***/ switch (where) { case 0: // SD Card fseek(filehandle, 0, SEEK_SET); fread (readbuffer, 1, 2048, filehandle); break; case 1: // DVD dvd_read (readbuffer, 2048, discoffset); break; } /*** Copy PKZip header to local, used as info ***/ memcpy (&pkzip, readbuffer, sizeof (PKZIPHEADER)); pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize); sprintf (msg, "Unzipping %d bytes ... Wait", pkzip.uncompressedSize); ShowAction (msg); /*** Prepare the zip stream ***/ memset (&zs, 0, sizeof (z_stream)); 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) return 0; /*** Set ZipChunk for first pass ***/ zipoffset = (sizeof (PKZIPHEADER) + FLIP16 (pkzip.filenameLength) + FLIP16 (pkzip.extraDataLength)); zipchunk = ZIPCHUNK - zipoffset; /*** Now do it! ***/ do { zs.avail_in = zipchunk; zs.next_in = (Bytef *) & readbuffer[zipoffset]; /*** Now inflate until input buffer is exhausted ***/ do { zs.avail_out = ZIPCHUNK; zs.next_out = (Bytef *) & out; res = inflate (&zs, Z_NO_FLUSH); if (res == Z_MEM_ERROR) { inflateEnd (&zs); return 0; } have = ZIPCHUNK - zs.avail_out; if (have) { /*** Copy to normal block buffer ***/ memcpy (&outbuffer[bufferoffset], &out, have); bufferoffset += have; } } while (zs.avail_out == 0); /*** Readup the next 2k block ***/ zipoffset = 0; zipchunk = ZIPCHUNK; switch (where) { case 0: // SD Card fread (readbuffer, 1, 2048, filehandle); break; case 1: // DVD discoffset += 2048; dvd_read (readbuffer, 2048, discoffset); break; } } while (res != Z_STREAM_END); inflateEnd (&zs); if (res == Z_STREAM_END) { if (pkzip.uncompressedSize == (u32) bufferoffset) return bufferoffset; else return pkzip.uncompressedSize; } return 0; }