Ejemplo n.º 1
0
/*****************************************************************************
 * 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;
}
Ejemplo n.º 2
0
int load_archive(char *filename, unsigned char *buffer, int maxsize, char *extension)
{
  int size = 0;
  char in[CHUNKSIZE];
  char msg[64];
  char type[16];

  /* Open file */
  FILE *fd = fopen(filename, "rb");

  /* Autodetect needed System ROM files */
  if (filename == CD_BIOS_US)
  {
    sprintf(type,"CD BIOS (USA)");
  }
  else if (filename == CD_BIOS_EU)
  {
    sprintf(type,"CD BIOS (PAL)");
  }
  else if (filename == CD_BIOS_JP)
  {
    sprintf(type,"CD BIOS (JAP)");
  }
  else if (filename == AR_ROM)
  {
    sprintf(type,"Action Replay");
  }
  else if (filename == GG_ROM)
  {
    sprintf(type,"Game Genie");
  }
  else if (filename == SK_ROM)
  {
    sprintf(type,"S&K (2MB ROM)");
  }
  else if (filename == SK_UPMEM)
  {
    sprintf(type,"S2&K (256K ROM)");
  }
  else if ((filename == MS_BIOS_US) || (filename == MS_BIOS_EU) || (filename == MS_BIOS_JP) || (filename == GG_BIOS) || (filename == MD_BIOS))
  {
    /* Mega Drive / Genesis, Master System & Game Gear BIOS are optional so we disable error messages */
    SILENT = 1;
  }
  else
  {
    sprintf(type,"file");
  }

  if (!fd)
  {
    sprintf(msg,"Unable to open %s", type);
    GUI_WaitPrompt("Error", msg);
    SILENT = 0;
    return 0;
  }

  /* Read first chunk */
  fread(in, CHUNKSIZE, 1, fd);

  /* Detect Zip file */
  if (memcmp(in, "PK", 2) == 0)
  {
    /* Inflate buffer */
    char out[CHUNKSIZE];

    /* PKZip header pointer */
    PKZIPHEADER *pkzip = (PKZIPHEADER *) in;

    /* Retrieve uncompressed ROM size */
    size = FLIP32(pkzip->uncompressedSize);

    /* Check ROM size */
    if (size > MAXROMSIZE)
    {
      fclose(fd);
      GUI_WaitPrompt("Error","File is too large");
      SILENT = 0;
      return 0;
    }
    else if (size > maxsize)
    {
      size = maxsize;
    }

    sprintf (msg, "Unzipping %d bytes ...", size);
    GUI_MsgBoxUpdate("Information",msg);

    /* Initialize zip stream */
    z_stream zs;
    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;
    int res = inflateInit2(&zs, -MAX_WBITS);
    if (res != Z_OK)
    {
      fclose(fd);
      sprintf(msg,"Unable to unzip %s", type);
      GUI_WaitPrompt("Error",msg);
      SILENT = 0;
      return 0;
    }

    /* Compressed filename offset */
    int offset = sizeof (PKZIPHEADER) + FLIP16(pkzip->filenameLength);
    if (extension)
    {
      memcpy(extension, &in[offset - 3], 3);
      extension[3] = 0;
    }

    /* Initial Zip buffer offset */
    offset += FLIP16(pkzip->extraDataLength);
    zs.next_in = (Bytef *)&in[offset];

    /* Initial Zip remaining chunk size */
    zs.avail_in = CHUNKSIZE - offset;

    /* Initialize output size */
    size = 0;

    /* Start unzipping file */
    do
    {
      /* Inflate data until output buffer is empty */
      do
      {
        zs.avail_out = CHUNKSIZE;
        zs.next_out = (Bytef *) out;
        res = inflate(&zs, Z_NO_FLUSH);

        if (res == Z_MEM_ERROR)
        {
          inflateEnd(&zs);
          fclose(fd);
          sprintf(msg,"Unable to unzip %s", type);
          GUI_WaitPrompt("Error",msg);
          SILENT = 0;
          return 0;
        }

        offset = CHUNKSIZE - zs.avail_out;

        if ((size + offset) > maxsize)
        {
          offset = maxsize - size;
        }

        if (offset)
        {
          memcpy(buffer, out, offset);
          buffer += offset;
          size += offset;
        }
      }
      while ((zs.avail_out == 0) && (size < maxsize));

      /* Read next chunk of zipped data */
      fread(in, CHUNKSIZE, 1, fd);
      zs.next_in = (Bytef *)&in[0];
      zs.avail_in = CHUNKSIZE;
    }
    while ((res != Z_STREAM_END) && (size < maxsize));
    inflateEnd (&zs);
  }
  else
  {
    /* Get file size */
    fseek(fd, 0, SEEK_END);
    size = ftell(fd);
    fseek(fd, 0, SEEK_SET);

    /* Check ROM size */
    if (size > MAXROMSIZE)
    {
      fclose(fd);
      GUI_WaitPrompt("Error","File is too large");
      SILENT = 0;
      return 0;
    }
    else if (size > maxsize)
    {
      size = maxsize;
    }

    sprintf((char *)msg,"Loading %d bytes ...", size);
    GUI_MsgBoxUpdate("Information", (char *)msg);

    /* filename extension */
    if (extension)
    {
      memcpy(extension, &filename[strlen(filename) - 3], 3);
      extension[3] = 0;
    }

    /* Read into buffer */
    int left = size;
    while (left > CHUNKSIZE)
    {
      fread(buffer, CHUNKSIZE, 1, fd);
      buffer += CHUNKSIZE;
      left -= CHUNKSIZE;
    }

    /* Read remaining bytes */
    fread(buffer, left, 1, fd);
  }

  /* Close file */
  fclose(fd);

  /* Return loaded ROM size */
  SILENT = 0;
  return size;
}
Ejemplo n.º 3
0
size_t
UnZipBuffer (unsigned char *outbuffer)
{
	PKZIPHEADER pkzip;
	size_t zipoffset = 0;
	size_t zipchunk = 0;
	char out[ZIPCHUNK];
	z_stream zs;
	int res;
	size_t bufferoffset = 0;
	size_t have = 0;
	char readbuffer[ZIPCHUNK];
	size_t sizeread = 0;

	// Read Zip Header
	fseek(file, 0, SEEK_SET);
	sizeread = fread (readbuffer, 1, ZIPCHUNK, file);

	if(sizeread <= 0)
		return 0;

	/*** Copy PKZip header to local, used as info ***/
	memcpy (&pkzip, readbuffer, sizeof (PKZIPHEADER));

	pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize);

	ShowProgress ("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)
		goto done;

	/*** 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)
			{
				goto done;
			}

			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;

		sizeread = fread (readbuffer, 1, ZIPCHUNK, file);
		if(sizeread <= 0)
			goto done; // read failure

		ShowProgress ("Loading...", bufferoffset, pkzip.uncompressedSize);
	}
	while (res != Z_STREAM_END);

done:
	inflateEnd (&zs);
	CancelAction();

	if (res == Z_STREAM_END)
		return pkzip.uncompressedSize;
	else
		return 0;
}
Ejemplo n.º 4
0
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;
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
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;

}
Ejemplo n.º 7
0
int UnZipBuffer (unsigned char *outbuffer, long 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;
	int size=0;

	/*** 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, 1, 2048, fatfile);
	}
	else
	{
		printf("\t\t\tUnable to unzip file nt found\n");
		return 0;
	}

	/*** Copy PKZip header to local, used as info ***/
	memcpy (&pkzip, &readbuffer, sizeof (PKZIPHEADER));

	sprintf (msg, "\t\t\t\tUnzipping %d bytes ...\n", FLIP32 (pkzip.uncompressedSize));
	printf(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)
	{
		printf("\t\t\tUnable to unzip file\n");
		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);
				printf("\t\t\tUnable to unzip file\n");
				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, 1, 2048, fatfile);
		}

		//avance le chargement ...
		char loadingTxt[256];
//sprintf(loadingTxt,"Loading %s (%d%%)\n", filename, (pkzip.uncompressedSize - size) * 100 /pkzip.uncompressedSize);
		sprintf(loadingTxt,"Loading %s (%d%%)\n",filename,(pkzip.compressedSize - size) * 100 /pkzip.compressedSize);
		fastPrintf(200,240,0x00000000,loadingTxt);
		size+=2048;
	}
	while (res != Z_STREAM_END);

	inflateEnd (&zs);


	/* close file */
	if (fatfile) fclose(fatfile);

	if (res == Z_STREAM_END)
	{
		if (FLIP32 (pkzip.uncompressedSize) == (int) bufferoffset) return bufferoffset;
		else return FLIP32 (pkzip.uncompressedSize);
	}
	return 0;
}
Ejemplo n.º 8
0
/*****************************************************************************
 * UnZipBuffer
 *
 * It should be noted that there is a limit of 5MB total size for any ROM
 ******************************************************************************/
int UnZipBuffer (unsigned char *outbuffer, FILE *fd)
{
  PKZIPHEADER pkzip;
  int zipoffset = 0;
  int zipchunk = 0;
  char out[ZIPCHUNK];
  z_stream zs;
  int res;
  int bufferoffset = 0;
  int have = 0;
  char readbuffer[ZIPCHUNK];
  char msg[64];

  /*** Read Zip Header ***/
  fread(readbuffer, ZIPCHUNK,  1, fd);

  /*** Copy PKZip header to local, used as info ***/
  memcpy (&pkzip, &readbuffer, sizeof (PKZIPHEADER));

  if (FLIP32 (pkzip.uncompressedSize) > MAXROMSIZE)
  {
    printf("Error","File is too large !");
    return 0;
  }

  sprintf (msg, "Unzipping %d bytes ...", FLIP32 (pkzip.uncompressedSize));
  printf("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)
  {
    printf("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);
        printf("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;
    fread(readbuffer, ZIPCHUNK, 1, fd);
  }
  while (res != Z_STREAM_END);

  inflateEnd (&zs);

  if (res == Z_STREAM_END)
  {
    if (FLIP32 (pkzip.uncompressedSize) == (unsigned int) bufferoffset)
      return bufferoffset;
    else
      return FLIP32 (pkzip.uncompressedSize);
  }

  return 0;
}