int handleInputFile(char *romName) { SceUID romFile; int iDepth = 0; int size; printf("handle"); initSysInfo(); //initialize it all //if it's a ZIP file, we need to handle that here. iDepth = strrchr2(romName, '.'); iDepth++; if( ( strcmp( romName + iDepth, "zip" ) == 0 ) || ( strcmp( romName + iDepth, "ZIP" ) == 0 )) { //get ROM from ZIP printf("check_zip %s",romName); if(check_zip(romName)) { char name[_MAX_PATH]; printf("loadfromzip"); if(!loadFromZipByName(mainrom, romName, name, &size)) { printf("Load failed from %s\n", romName); return 0; } m_emuInfo.romSize = size; printf("strcpy %d",size); strcpy(m_emuInfo.RomFileName, romName); printf("strcpy %d",size); } else { printf("%s not PKZIP file\n", romName); return 0; } } else { //get ROM from binary ROM file romFile = sceIoOpen(romName,PSP2_O_RDONLY,0777); if(romFile<=0) { printf("Couldn't open %s file\n", romName); return 0; } m_emuInfo.romSize = sceIoRead(romFile,mainrom,4*1024*1024); strcpy(m_emuInfo.RomFileName, romName); } printf("initRom"); if(!initRom()) { printf("initRom couldn't handle %s file\n", romName); return 0; } printf("setflashSize"); setFlashSize(m_emuInfo.romSize); return 1; }
int load_rom(const char *filename) { #ifdef USE_ZLIB if (check_zip(filename)) return load_rom_zip(filename); #endif return load_rom_normal(filename); }
int load_rom(char *filename) { if(check_zip(filename)==0) { printf("\t\t\tUtilise la méthode normale\n"); return load_rom_file(filename); } else { return load_zip_file(filename); } }
static long state_unc_open(const char *fname, const char *mode) { //mode = "wb" or "rb" //If mode is write then create a new buffer to hold written data //when file is closed buffer will be compressed to zip file and then freed if(mode[0]=='r') { //Read mode requested if(check_zip((char*)fname)) { //File is a zip, so uncompress mFileZipMode = 1; //zip mode mFileRWMode = 0; mFileMem=load_archive((char*)fname,&mFileMemSize); if(!mFileMem) return 0; mFileMemPos=0; strcpy(mFileName,fname); return 1; } else { mFileZipMode = 0; //normal file mode mFile = fopen(fname, mode); return (long) mFile; } } else { //Write mode requested. Zip only option mFileRWMode = 1; mFileZipMode = 1; //always zip mFileMem=(char*)malloc(200); mFileMemSize=200; mFileMemPos = 0; strcpy(mFileName,fname); return 1; } }
/* Load a normal file, or ZIP/GZ archive. Returns NULL if an error occured. */ uint8 *load_archive(char *filename, int *file_size) { int size = 0; uint8 *buf = NULL; if(check_zip(filename)) { unzFile *fd = NULL; unz_file_info info; int ret = 0; /* Attempt to open the archive */ fd = unzOpen(filename); if(!fd) return (NULL); /* Go to first file in archive */ ret = unzGoToFirstFile(fd); if(ret != UNZ_OK) { unzClose(fd); return (NULL); } ret = unzGetCurrentFileInfo(fd, &info, NULL, 0, NULL, 0, NULL, 0); if(ret != UNZ_OK) { unzClose(fd); return (NULL); } /* Open the file for reading */ ret = unzOpenCurrentFile(fd); if(ret != UNZ_OK) { unzClose(fd); return (NULL); } /* Allocate file data buffer */ size = info.uncompressed_size; buf = malloc(size); if(!buf) { unzClose(fd); return (NULL); } /* Read (decompress) the file */ ret = unzReadCurrentFile(fd, buf, info.uncompressed_size); if(ret != info.uncompressed_size) { free(buf); unzCloseCurrentFile(fd); unzClose(fd); return (NULL); } /* Close the current file */ ret = unzCloseCurrentFile(fd); if(ret != UNZ_OK) { free(buf); unzClose(fd); return (NULL); } /* Close the archive */ ret = unzClose(fd); if(ret != UNZ_OK) { free(buf); return (NULL); } /* Update file size and return pointer to file data */ *file_size = size; return (buf); } else { gzFile *gd = NULL; /* Open file */ gd = gzopen(filename, "rb"); if(!gd) return (0); /* Get file size */ size = gzsize(gd); /* Allocate file data buffer */ buf = malloc(size); if(!buf) { gzclose(gd); return (0); } /* Read file data */ gzread(gd, buf, size); /* Close file */ gzclose(gd); /* Update file size and return pointer to file data */ *file_size = size; return (buf); } }
void unzip_if_needed(uint32_t * where_p, uint32_t * size_p) { uint32_t where = *where_p; uint32_t size = *size_p; uint32_t zbytes; uint32_t startrange, endrange; uint32_t gzdatasize, gzwhere; uint32_t orig_crc, offset; uint32_t target = 0; int i, okmem; /* Is it a gzip image? */ if (check_zip((void *)where, size, &zbytes, &gzdatasize, &orig_crc, &offset) == 0) { if (offset + zbytes > size) { /* * Assertion failure; check_zip is supposed to guarantee this * never happens. */ die("internal error: check_zip returned nonsense\n"); } /* * Find a good place to put it: search memory ranges in descending * order until we find one that is legal and fits */ okmem = 0; for (i = nranges - 1; i >= 0; i--) { /* * We can't use > 4G memory (32 bits only.) Truncate to 2^32-1 * so we don't have to deal with funny wraparound issues. */ /* Must be memory */ if (ranges[i].type != 1) continue; /* Range start */ if (ranges[i].start >= 0xFFFFFFFF) continue; startrange = (uint32_t) ranges[i].start; /* Range end (0 for end means 2^64) */ endrange = ((ranges[i + 1].start >= 0xFFFFFFFF || ranges[i + 1].start == 0) ? 0xFFFFFFFF : (uint32_t) ranges[i + 1].start); /* Make sure we don't overwrite ourselves */ if (startrange < (uint32_t) & _end) startrange = (uint32_t) & _end; /* Allow for alignment */ startrange = (ranges[i].start + (UNZIP_ALIGN - 1)) & ~(UNZIP_ALIGN - 1); /* In case we just killed the whole range... */ if (startrange >= endrange) continue; /* * Must be large enough... don't rely on gzwhere for this * (wraparound) */ if (endrange - startrange < gzdatasize) continue; /* * This is where the gz image would be put if we put it in this * range... */ gzwhere = (endrange - gzdatasize) & ~(UNZIP_ALIGN - 1); /* Cast to uint64_t just in case we're flush with the top byte */ if ((uint64_t) where + size >= gzwhere && where < endrange) { /* * Need to move source data to avoid compressed/uncompressed * overlap */ uint32_t newwhere; if (gzwhere - startrange < size) continue; /* Can't fit both old and new */ newwhere = (gzwhere - size) & ~(UNZIP_ALIGN - 1); printf("Moving compressed data from 0x%08x to 0x%08x\n", where, newwhere); memmove((void *)newwhere, (void *)where, size); where = newwhere; } target = gzwhere; okmem = 1; break; } if (!okmem) die("Not enough memory to decompress image (need 0x%08x bytes)\n", gzdatasize); printf("gzip image: decompressed addr 0x%08x, len 0x%08x: ", target, gzdatasize); *size_p = gzdatasize; *where_p = (uint32_t) unzip((void *)(where + offset), zbytes, gzdatasize, orig_crc, (void *)target); } }
int load_archive(char *filename, unsigned char *buffer, int maxsize, char *extension) { int size = 0; if(check_zip(filename)) { unz_file_info info; int ret = 0; char fname[256]; /* Attempt to open the archive */ unzFile *fd = unzOpen(filename); if (!fd) return 0; /* Go to first file in archive */ ret = unzGoToFirstFile(fd); if(ret != UNZ_OK) { unzClose(fd); return 0; } /* Get file informations and update filename */ ret = unzGetCurrentFileInfo(fd, &info, fname, 256, NULL, 0, NULL, 0); if(ret != UNZ_OK) { unzClose(fd); return 0; } /* Compressed filename extension */ if (extension) { strncpy(extension, &fname[strlen(fname) - 3], 3); extension[3] = 0; } /* Open the file for reading */ ret = unzOpenCurrentFile(fd); if(ret != UNZ_OK) { unzClose(fd); return 0; } /* Retrieve uncompressed file size */ size = info.uncompressed_size; if(size > maxsize) { size = maxsize; } /* Read (decompress) the file */ ret = unzReadCurrentFile(fd, buffer, size); if(ret != size) { unzCloseCurrentFile(fd); unzClose(fd); return 0; } /* Close the current file */ ret = unzCloseCurrentFile(fd); if(ret != UNZ_OK) { unzClose(fd); return 0; } /* Close the archive */ ret = unzClose(fd); if(ret != UNZ_OK) return 0; } else { /* Open file */ gzFile *gd = gzopen(filename, "rb"); if (!gd) return 0; /* Read file data */ size = gzread(gd, buffer, maxsize); /* filename extension */ if (extension) { strncpy(extension, &filename[strlen(filename) - 3], 3); extension[3] = 0; } /* Close file */ gzclose(gd); } /* Return loaded ROM size */ return size; }
int load_rom(char *filename) { unsigned long file_length; int fd,fd_size; int n; int ret = 0; int ok=0; char tmpnom[0x100]; if(check_zip(filename)) { unzFile fd2=unzOpen(filename); if(fd2==NULL)return (1); // Go to first file in archive ret = unzGoToFirstFile(fd2); if(ret != UNZ_OK) { unzClose(fd2); return (1); } // Get information on the file ret = unzGetCurrentFileInfo(fd2, &info, tmpnom, 0x100, NULL, 0, NULL, 0); if(ret != UNZ_OK) { unzClose(fd2); return (1); } //Open the file for reading ret = unzOpenCurrentFile(fd2); if(ret != UNZ_OK) { unzClose(fd2); return (1); } // Allocate file data buffer fd_size = info.uncompressed_size; // Read (decompress) the file // cartridge_rom = (unsigned char *)malloc(fd_size); cartridge_rom = (u8 *)memalign(64, fd_size); ret = unzReadCurrentFile(fd2,/*(char *)*/cartridge_rom, info.uncompressed_size); if(ret != info.uncompressed_size) { //free(buf2); unzCloseCurrentFile(fd2); unzClose(fd2); return (1); } //printf("zip decomp %d \n",(int)info.uncompressed_size); // Close the current file ret = unzCloseCurrentFile(fd2); if(ret != UNZ_OK) { unzClose(fd2); return (1); } // printf("zip close file\n"); // Close the archive ret = unzClose(fd2); if(ret != UNZ_OK) { return (1); } // printf("zip close archive\n"); // Check for 512-byte header // ok=0; // cartridge_rom = (unsigned char *)malloc(fd_size); // for(n = 0; n <fd_size;n++)cartridge_rom[n]=buf[n+ok]; printf("zip header / rom copy %d %d %s \n",fd_size,ok,tmpnom); } else{ fd = fioOpen(filename, O_RDONLY); if(fd <= 0) { // display_error("Error opening file.",0); printf("%s not found.\n",filename); return 0; } file_length = fioLseek(fd,0,SEEK_END); fioLseek(fd,0,SEEK_SET); cartridge_rom = (unsigned char *)malloc(file_length); fioRead(fd, (char *)cartridge_rom, file_length); fioClose(fd); } // traitement du fich rom .... return 0; }