コード例 #1
0
ファイル: z.c プロジェクト: Keidan/tk
/**
 * @fn int z_uncompress(z_t zip, const char* password, z_uncompress_callback_fct callback)
 * @brief Unzip the ZIP files.
 * @param zip ZIP context.
 * @param password The zip password else NULL or empty.
 * @param callback Callback to received the uncompressed file datas.
 * @return -1 on failure else 0.
 */
int z_uncompress(z_t zip, const char* password, z_uncompress_callback_fct callback) {
  uLong i;
  struct zentry_s entry;
  int ret = 0;
  struct z_s* z = Z_CAST(zip);
  if(!Z_VALID_OPEN(z)) {
    logger(LOG_ERR, "Invalid zip pointer!\n");
    return -1;
  }
  if(!callback) {
    logger(LOG_ERR, "Invalid file content callback!\n");
    return -1;
  }
  
  /* Loop to list all files */
  for(i = 0; i < z->ginfo.number_entry; i++) {
    memset(&entry, 0, sizeof(struct zentry_s));
    /* Get info about current file. */
    if(unzGetCurrentFileInfo64(z->ctx, &entry.info, entry.name, FILENAME_MAX, NULL, 0, NULL, 0) != UNZ_OK) {
      logger(LOG_ERR, "Could not read file info from the zip file '%s'\n", z->filename);
      ret = -1;
      break;
    }
    entry.isdir = z_is_dir(z, entry.name);
    if(entry.isdir) 
      callback(z, entry);
    else {
      // Entry is a file, so extract it.
      if(!password || !strlen(password)) {
	if(unzOpenCurrentFile(z->ctx) != UNZ_OK) {
	  logger(LOG_ERR, "Could not open file '%s' into the zip file '%s'\n", entry.name, z->filename);
	  ret = -1;
	  break;
	}
      } else  {
	if(unzOpenCurrentFilePassword(z->ctx, password) != UNZ_OK) {
	  logger(LOG_ERR, "Could not open file '%s' into the zip file '%s'\n", entry.name, z->filename);
	  ret = -1;
	  break;
	}
      }

      int error = UNZ_OK;
      entry.content = (char*)malloc(entry.info.uncompressed_size);
      if(!entry.content) {
	logger(LOG_ERR, "Unable to alloc a memory for the content of the zipped file '%s'\n", entry.name);
	ret = -1;
	break;
      }
      /* read the file */
      do {
	error = unzReadCurrentFile(z->ctx, entry.content, entry.info.uncompressed_size);
	if ( error < 0 ) {
	  logger(LOG_ERR, "Could not read file '%s' into the zip file '%s': %d\n", entry.name, z->filename, error);
	  unzCloseCurrentFile(z->ctx);
	  break;
	}
      } while ( error > 0 );
    }
    callback(z, entry);
    free(entry.content); /* release content */
    unzCloseCurrentFile(z->ctx);
    /* Go the the next entry listed in the zip file. */
    if((i+1) < z->ginfo.number_entry) {
      if (unzGoToNextFile(z->ctx) != UNZ_OK) {
  	logger(LOG_ERR, "Could not read next file from the zip file '%s'\n", z->filename);
  	break;
      }
    }
  }
  if(ret) z_close(z);
  return ret;
}
コード例 #2
0
void EditorAssetInstaller::ok_pressed() {

	FileAccess *src_f = NULL;
	zlib_filefunc_def io = zipio_create_io_from_file(&src_f);

	unzFile pkg = unzOpen2(package_path.utf8().get_data(), &io);
	if (!pkg) {

		error->set_text(TTR("Error opening package file, not in zip format."));
		return;
	}

	int ret = unzGoToFirstFile(pkg);

	Vector<String> failed_files;

	ProgressDialog::get_singleton()->add_task("uncompress", TTR("Uncompressing Assets"), status_map.size());

	int idx = 0;
	while (ret == UNZ_OK) {

		//get filename
		unz_file_info info;
		char fname[16384];
		ret = unzGetCurrentFileInfo(pkg, &info, fname, 16384, NULL, 0, NULL, 0);

		String name = fname;

		if (status_map.has(name) && status_map[name]->is_checked(0)) {

			String path = status_map[name]->get_metadata(0);
			if (path == String()) { // a dir

				String dirpath;
				TreeItem *t = status_map[name];
				while (t) {
					dirpath = t->get_text(0) + dirpath;
					t = t->get_parent();
				}

				if (dirpath.ends_with("/")) {
					dirpath = dirpath.substr(0, dirpath.length() - 1);
				}

				DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
				da->make_dir(dirpath);
				memdelete(da);

			} else {

				Vector<uint8_t> data;
				data.resize(info.uncompressed_size);

				//read
				unzOpenCurrentFile(pkg);
				unzReadCurrentFile(pkg, data.ptrw(), data.size());
				unzCloseCurrentFile(pkg);

				FileAccess *f = FileAccess::open(path, FileAccess::WRITE);
				if (f) {
					f->store_buffer(data.ptr(), data.size());
					memdelete(f);
				} else {
					failed_files.push_back(path);
				}

				ProgressDialog::get_singleton()->task_step("uncompress", path, idx);
			}
		}

		idx++;
		ret = unzGoToNextFile(pkg);
	}

	ProgressDialog::get_singleton()->end_task("uncompress");
	unzClose(pkg);

	if (failed_files.size()) {
		String msg = "The following files failed extraction from package:\n\n";
		for (int i = 0; i < failed_files.size(); i++) {

			if (i > 15) {
				msg += "\nAnd " + itos(failed_files.size() - i) + " more files.";
				break;
			}
			msg += failed_files[i];
		}
		if (EditorNode::get_singleton() != NULL)
			EditorNode::get_singleton()->show_warning(msg);
	} else {
		if (EditorNode::get_singleton() != NULL)
			EditorNode::get_singleton()->show_warning(TTR("Package Installed Successfully!"), TTR("Success!"));
	}
	EditorFileSystem::get_singleton()->scan_changes();
}
コード例 #3
0
ファイル: loadzip.cpp プロジェクト: Alexander--/SNesoid
bool8 LoadZip(const char* zipname,
	      int32 *TotalFileSize,
	      int32 *headers, uint8* buffer)
{
    *TotalFileSize = 0;
    *headers = 0;
    
    unzFile file = unzOpen(zipname);
    if(file == NULL)
	return (FALSE);

    // find largest file in zip file (under MAX_ROM_SIZE)
    // or a file with extension .1
    char filename[132];
    int filesize = 0;
    int port = unzGoToFirstFile(file);
    unz_file_info info;
    while(port == UNZ_OK)
    {
	char name[132];
	unzGetCurrentFileInfo(file, &info, name,128, NULL,0, NULL,0);

#if 0
	int calc_size = info.uncompressed_size / 0x2000;
	calc_size *= 0x2000;
	if(!(info.uncompressed_size - calc_size == 512 || info.uncompressed_size == calc_size))
	{
	    port = unzGoToNextFile(file);
	    continue;
	}
#endif

	if(info.uncompressed_size > (CMemory::MAX_ROM_SIZE + 512))
	{
	    port = unzGoToNextFile(file);
	    continue;
	}
	
	if ((int) info.uncompressed_size > filesize)
	{
	    strcpy(filename,name);
	    filesize = info.uncompressed_size;
	}
	int len = strlen(name);
	if(name[len-2] == '.' && name[len-1] == '1')
	{
	    strcpy(filename,name);
	    filesize = info.uncompressed_size;
	    break;
	}
	port = unzGoToNextFile(file);
    }
    if( !(port == UNZ_END_OF_LIST_OF_FILE || port == UNZ_OK) || filesize == 0)
    {
	assert( unzClose(file) == UNZ_OK );
	return (FALSE);
    }

    // Find extension
    char tmp[2];
    tmp[0] = tmp[1] = 0;
    char *ext = strrchr(filename,'.');
    if(ext) ext++;
    else ext = tmp;
    
    uint8 *ptr = buffer;
    bool8 more = FALSE;

    unzLocateFile(file,filename,1);
    unzGetCurrentFileInfo(file, &info, filename,128, NULL,0, NULL,0);
    
    if( unzOpenCurrentFile(file) != UNZ_OK )
    {
	unzClose(file);
	return (FALSE);
    }

    do
    {
	assert(info.uncompressed_size <= CMemory::MAX_ROM_SIZE + 512);
	int FileSize = info.uncompressed_size;
	
	int calc_size = FileSize / 0x2000;
	calc_size *= 0x2000;
	
	int l = unzReadCurrentFile(file,ptr,FileSize);
	if(unzCloseCurrentFile(file) == UNZ_CRCERROR)
	{
	    unzClose(file);
	    return (FALSE);
	}
	
	if(l <= 0 || l != FileSize)
	{
	    unzClose(file);
	    switch(l)
	    {
		case UNZ_ERRNO:
		    break;
		case UNZ_EOF:
		    break;
		case UNZ_PARAMERROR:
		    break;
		case UNZ_BADZIPFILE:
		    break;
		case UNZ_INTERNALERROR:
		    break;
		case UNZ_CRCERROR:
		    break;
	    }
	    return (FALSE);
	}

	if ((FileSize - calc_size == 512 && !Settings.ForceNoHeader) ||
	    Settings.ForceHeader)
	{
	    memmove (ptr, ptr + 512, calc_size);
	    (*headers)++;
	    FileSize -= 512;
	}
	ptr += FileSize;
	(*TotalFileSize) += FileSize;

	int len;
	if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 0x200 &&
	    (isdigit (ext [0]) && ext [1] == 0 && ext [0] < '9'))
	{
	    more = TRUE;
	    ext [0]++;
	}
	else if (ptr - Memory.ROM < CMemory::MAX_ROM_SIZE + 0x200 &&
		 (((len = strlen (filename)) == 7 || len == 8) &&
		  strncasecmp (filename, "sf", 2) == 0 &&
		  isdigit (filename [2]) && isdigit (filename [3]) && isdigit (filename [4]) &&
		  isdigit (filename [5]) && isalpha (filename [len - 1])))
	{
	    more = TRUE;
	    filename [len - 1]++;
	}
	else
	    more = FALSE;
	
	if(more)
	{
	    if( unzLocateFile(file,filename,1) != UNZ_OK ||
		unzGetCurrentFileInfo(file, &info, filename,128, NULL,0, NULL,0) != UNZ_OK ||
		unzOpenCurrentFile(file) != UNZ_OK)
		break;
	}
	
    } while(more);
    
    unzClose(file);
    return (TRUE);
}
コード例 #4
0
/*
=================
idFile_InZip::Read

Properly handles partial reads
=================
*/
int idFile_InZip::Read( void *buffer, int len ) {
	int l = unzReadCurrentFile( z, buffer, len );
	fileSystem->AddToReadCount( l );
	return l;
}
コード例 #5
0
ファイル: ZipTools.cpp プロジェクト: sssooonnnggg/LibSrn
// ////////////////////////////////////////////////////////////////////////////////
// @global 释放单个文件
//
int ExtractSingleFile(unzFile& uf)
{
	char filename_inzip[256] = {0};
	unz_file_info64 file_info;

	int err = unzGetCurrentFileInfo64(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);

	if ( UNZ_OK != err )
	{
		DebugTools::OutputDebugPrintfW(
			L"[ZipTools] [ExtractSingleFile] unzGetCurrentFileInfo64 Failed.[%d]\r\n", err);
		return err;
	}

	unsigned int size_buf = WRITEBUFFERSIZE;
	PBYTE buf = new BYTE[size_buf];

	// 判断名字是否以 '\\' 结尾,如果以 '\\'
	// 结尾,代表这是一个目录名,创建目录,
	// 反之则创建文件
	if ( '\\' == filename_inzip[strlen(filename_inzip)-1] )
	{
		WCHAR wzDirName[1024] = {0};
		MultiByteToWideChar(CP_ACP, 0, filename_inzip, -1, wzDirName, 1024);
		FileTools::CreateDirectorys(wzDirName);
	}
	else
	{
		err = unzOpenCurrentFilePassword(uf, NULL);

		if ( err != UNZ_OK)
		{
			DebugTools::OutputDebugPrintfW(
				L"[ZipTools] [ExtractSingleFile] unzOpenCurrentFilePassword Failed.[%d]\r\n", err);
		}

		// 创建文件所在的目录
		WCHAR wzFileName[MAX_PATH] = {0};
		MultiByteToWideChar(CP_ACP, 0, filename_inzip, -1, wzFileName, MAX_PATH);
		WCHAR wzDirName[MAX_PATH] = {0};
		FileTools::GetFileDir(wzFileName, wzDirName, L'\\');
		FileTools::CreateDirectorys(wzDirName);

		DebugTools::OutputDebugPrintfW(
			L"[FileTools] [UnZip] Extracting %s ...\r\n", wzFileName);
		
		FILE* fout = fopen64(filename_inzip, "wb");

		do
		{
			err = unzReadCurrentFile(uf,buf,size_buf);

			if ( err < 0 )
			{
				DebugTools::OutputDebugPrintfW(
					L"[ZipTools] [ExtractSingleFile] unzReadCurrentFile Failed.[%d]\r\n", err);
				break;
			}
			if ( err > 0 )
			{
				if ( fwrite(buf,err,1,fout) != 1 )
				{
					DebugTools::OutputDebugPrintfW(
						L"[ZipTools] [ExtractSingleFile] Write File Failed.[%s]\r\n", 
						filename_inzip);

					err = UNZ_ERRNO;
					break;
				}
			}
		} while ( err > 0 );

		if ( fout )
		{
			fclose(fout);
		}

		// 修改文件时间
		ChangeFileTime(filename_inzip, file_info.dosDate, file_info.tmu_date);

		unzCloseCurrentFile(uf);
		delete [] buf;
	}
}
コード例 #6
0
ファイル: vfs.c プロジェクト: paulvortex/BloodMap
// NOTE: when loading a file, you have to allocate one extra byte and set it to \0
int vfsLoadFile (const char *filename, void **bufferptr, int index)
{
  int i, count = 0;
  char tmp[NAME_MAX], fixed[NAME_MAX];
  GSList *lst;
  
  // filename is a full path
  if (index == -1)
  {
    long len;
    FILE *f;
    
    f = fopen (filename, "rb");
    if (f == NULL)
      return -1;
    
    fseek (f, 0, SEEK_END);
    len = ftell (f);
    rewind (f);
    
    *bufferptr = safe_malloc (len+1);
    if (*bufferptr == NULL)
      return -1;
    
    fread (*bufferptr, 1, len, f);
    fclose (f);
    
    // we need to end the buffer with a 0
    ((char*) (*bufferptr))[len] = 0;
    
    return len;
  }
  
  *bufferptr = NULL;
  strcpy (fixed, filename);
  vfsFixDOSName (fixed);
  g_strdown (fixed);
  
  for (i = 0; i < g_numDirs; i++)
  {
    strcpy (tmp, g_strDirs[i]);
    strcat (tmp, filename);
    if (_access (tmp, R_OK) == 0)
    {
      if (count == index)
      {
        long len;
        FILE *f;
        
        f = fopen (tmp, "rb");
        if (f == NULL)
          return -1;
        
        fseek (f, 0, SEEK_END);
        len = ftell (f);
        rewind (f);
        
        *bufferptr = safe_malloc (len+1);
        if (*bufferptr == NULL)
          return -1;
        
        fread (*bufferptr, 1, len, f);
        fclose (f);
        
        // we need to end the buffer with a 0
        ((char*) (*bufferptr))[len] = 0;
        
        return len;
      }
      
      count++;
    }
  }
  
  for (lst = g_pakFiles; lst != NULL; lst = g_slist_next (lst))
  {
    VFS_PAKFILE* file = (VFS_PAKFILE*)lst->data;
    
    if (strcmp (file->name, fixed) != 0)
      continue;
    
    if (count == index)
    {
      memcpy (file->zipfile, &file->zipinfo, sizeof (unz_s));
      
      if (unzOpenCurrentFile (file->zipfile) != UNZ_OK)
        return -1;
      
      *bufferptr = safe_malloc (file->size+1);
      // we need to end the buffer with a 0
      ((char*) (*bufferptr))[file->size] = 0;
      
      i = unzReadCurrentFile (file->zipfile , *bufferptr, file->size);
      unzCloseCurrentFile (file->zipfile);
      if (i < 0)
        return -1;
      else
        return file->size;
    }
    
    count++;
  }
  
  return -1;
}
コード例 #7
0
void* CALL HGE_Impl::Resource_Load(const char *filename, DWORD *size)
{
	static char *res_err="Can't load resource: %s";

	CResourceList *resItem=res;
	char szName[_MAX_PATH];
	char szZipName[_MAX_PATH];
	unzFile zip;
	unz_file_info file_info;
	int done, i;
	void *ptr;
	//HANDLE hF;
	FILE *file;

	if(filename[0]=='\\' || filename[0]=='/' || filename[1]==':') goto _fromfile; // skip absolute paths

	// Load from pack
 
	strcpy(szName,filename);
	SDL_strupr(szName);
	for(i=0; szName[i]; i++) { if(szName[i]=='/') szName[i]='\\'; }

	while(resItem)
	{
		zip=unzOpen(resItem->filename);
		done=unzGoToFirstFile(zip);
		while(done==UNZ_OK)
		{
			unzGetCurrentFileInfo(zip, &file_info, szZipName, sizeof(szZipName), NULL, 0, NULL, 0);
			SDL_strupr(szZipName);
			for(i=0; szZipName[i]; i++)	{ if(szZipName[i]=='/') szZipName[i]='\\'; }
			if(!strcmp(szName,szZipName))
			{
				if(unzOpenCurrentFilePassword(zip, resItem->password[0] ? resItem->password : 0) != UNZ_OK)
				{
					unzClose(zip);
					sprintf(szName, res_err, filename);
					_PostError(szName);
					return 0;
				}

				ptr = malloc(file_info.uncompressed_size);
				if(!ptr)
				{
					unzCloseCurrentFile(zip);
					unzClose(zip);
					sprintf(szName, res_err, filename);
					_PostError(szName);
					return 0;
				}

				if(unzReadCurrentFile(zip, ptr, file_info.uncompressed_size) < 0)
				{
					unzCloseCurrentFile(zip);
					unzClose(zip);
					free(ptr);
					sprintf(szName, res_err, filename);
					_PostError(szName);
					return 0;
				}
				unzCloseCurrentFile(zip);
				unzClose(zip);
				if(size) *size=file_info.uncompressed_size;
				return ptr;
			}
			
			done=unzGoToNextFile(zip);
		}
		
		unzClose(zip);
		resItem=resItem->next;
	}

	// Load from file
_fromfile:

	/*hF = CreateFile(Resource_MakePath(filename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
	if(hF == INVALID_HANDLE_VALUE)
	{
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}*/
	file = fopen(Resource_MakePath(filename), "rb");
	// if cannot open file
	if (!file){
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}
	//file_info.uncompressed_size = GetFileSize(hF, NULL);
	fseek(file, 0, SEEK_END);
	file_info.uncompressed_size = ftell(file);
	fseek(file, 0, SEEK_SET);
	ptr = malloc(file_info.uncompressed_size);
	if(!ptr)
	{
		//CloseHandle(hF);
		fclose(file);
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}
	int tmp = fread(ptr, file_info.uncompressed_size, 1, file);
	if (tmp <= 0)
	{
		fclose(file);
		free(ptr);
		sprintf(szName, res_err, filename);
		_PostError(szName);
		return 0;
	}
	
	//CloseHandle(hF);
	fclose(file);
	if(size) *size=file_info.uncompressed_size;
	return ptr;
}
コード例 #8
0
ファイル: fileio.c プロジェクト: Meradrin/Genesis-Plus-GX
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
  {
    gzFile *gd = gzopen(filename, "rb");
    if (!gd) return 0;

    size = gzread(gd, buffer, maxsize);

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

    gzclose(gd);
  }*/

  /* Return loaded ROM size */
  return size;
}
コード例 #9
0
ファイル: exemple.c プロジェクト: xerpi/PS2InfoGB_rev6c_dsmod
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; 
 
 }
コード例 #10
0
ファイル: xml.c プロジェクト: gnils/usbloader-gx
bool OpenXMLFile(char *filename) {
    //if (xmldebug) dbg_time1();

    if (xml_loaded)
        return false;

    gameinfo = gameinfo_reset;
    nodedata=NULL;
    nodetree=NULL;
    nodeid=NULL;
    nodeidtmp=NULL;
    nodefound=NULL;

    char* strresult = strstr(filename,".zip");
    if (strresult == NULL) {
        /* Load XML file */
        FILE *filexml;
        filexml = fopen(filename, "rb");
        if (!filexml)
            return false;

        nodetree = mxmlLoadFile(NULL, filexml, MXML_OPAQUE_CALLBACK);
        fclose(filexml);

    } else {
        /* load zipped XML file */
        unzFile unzfile = unzOpen(filename);
        if (unzfile == NULL)
            return false;
        unzOpenCurrentFile(unzfile);

        unz_file_info zipfileinfo;
        unzGetCurrentFileInfo(unzfile, &zipfileinfo, NULL, 0, NULL, 0, NULL, 0);
        int zipfilebuffersize = zipfileinfo.uncompressed_size;
        if (zipfilebuffersize >= xmlmaxsize) {
            unzCloseCurrentFile(unzfile);
            unzClose(unzfile);
            return false;
        }

        char * zipfilebuffer = malloc(zipfilebuffersize);
        memset(zipfilebuffer, 0, zipfilebuffersize);
        if (zipfilebuffer == NULL) {
            unzCloseCurrentFile(unzfile);
            unzClose(unzfile);
            return false;
        }

        unzReadCurrentFile(unzfile, zipfilebuffer, zipfilebuffersize);
        unzCloseCurrentFile(unzfile);
        unzClose(unzfile);

        nodetree = mxmlLoadString(NULL, zipfilebuffer, MXML_OPAQUE_CALLBACK);
        free(zipfilebuffer);
    }

    if (nodetree == NULL)
        return false;

    nodedata = mxmlFindElement(nodetree, nodetree, "datafile", NULL, NULL, MXML_DESCEND);
    if (nodedata == NULL) {
        return false;
    } else {
        //if (xmldebug)	xmlloadtime = dbg_time2(NULL);
        xml_loaded = true;
        return true;
    }
}
コード例 #11
0
ファイル: Scene.cpp プロジェクト: DCubix/1.4.0
std::string CScene::ReadZipFile(CChar* zipFile, CChar* fileInZip) {
    int err = UNZ_OK;                 // error status
    uInt size_buf = WRITEBUFFERSIZE;  // byte size of buffer to store raw csv data
    void* buf;                        // the buffer  
	std::string sout;                      // output strings
    char filename_inzip[256];         // for unzGetCurrentFileInfo
    unz_file_info file_info;          // for unzGetCurrentFileInfo   
 
    unzFile uf = unzOpen(zipFile); // open zipfile stream
    if (uf==NULL) {
		CChar temp[MAX_URI_SIZE];
        sprintf( temp, "\n%s %s %s", "Cannot open '", zipFile, "'" );
		MessageBoxA(NULL, temp, "Vanda Engine Error", MB_OK | MB_ICONERROR );
        return sout;
    } // file is open
 
    if ( unzLocateFile(uf,fileInZip,1) ) { // try to locate file inside zip
		CChar temp[MAX_NAME_SIZE];
		sprintf( temp, "\n%s %s %s %s %s", "File '", fileInZip, "' not found in '", zipFile, "'" );
		MessageBoxA(NULL, temp, "Vanda Engine Error", MB_OK | MB_ICONERROR );
        // second argument of unzLocateFile: 1 = case sensitive, 0 = case-insensitive
        return sout;
    } // file inside zip found
 
    if (unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0)) {
		CChar temp[MAX_URI_SIZE];
        sprintf( temp, "\n%s %s %s", "Error with zipfile '", zipFile, "' in unzGetCurrentFileInfo()" );
		MessageBoxA(NULL, temp, "Vanda Engine Error", MB_OK | MB_ICONERROR );
        return sout;
    } // obtained the necessary details about file inside zip
 
    buf = (void*)malloc(size_buf); // setup buffer
    if (buf==NULL) {
        MessageBoxA( NULL, "Error allocating memory for read buffer", "Vanda Engine Error", MB_OK | MB_ICONERROR );
        return sout;
    } // buffer ready
 
	if( Cmp( g_currentPassword, "\n" ) )
		err = unzOpenCurrentFilePassword(uf,NULL); // Open the file inside the zip (password = NULL)
	else
	    err = unzOpenCurrentFilePassword(uf,g_currentPassword); // Open the file inside the zip (password = NULL)
    if (err!=UNZ_OK) {
		CChar temp[MAX_URI_SIZE];
        sprintf( temp, "%s %s %s", "Error with zipfile '", zipFile, "' in unzOpenCurrentFilePassword()" );
		MessageBoxA(NULL, temp, "Vanda Engine Error", MB_OK | MB_ICONERROR );
        return sout;
    } // file inside the zip is open
 
    // Copy contents of the file inside the zip to the buffer
	//CChar temp[MAX_URI_SIZE];
    //sprintf( temp, "\n%s %s %s %s %s", "Extracting '", filename_inzip, "' from '", zipFile, "'");
	//PrintInfo( temp );
    do {
        err = unzReadCurrentFile(uf,buf,size_buf);
        if (err<0) {
			CChar temp[MAX_URI_SIZE];
            sprintf( temp, "\n%s %s %s", "Error with zipfile '", zipFile, "' in unzReadCurrentFile()");
			MessageBoxA(NULL, temp, "Vanda Engine Error", MB_OK | MB_ICONERROR );
            sout = ""; // empty output string
            break;
        }
        // copy the buffer to a string
        if (err>0) for (int i = 0; i < (int) err; i++) 
		{
			sout.push_back( *(((char*)buf)+i) );
		}
    } while (err>0);
 
    err = unzCloseCurrentFile (uf);  // close the zipfile
    if (err!=UNZ_OK) {
		CChar temp[MAX_URI_SIZE];
        sprintf( temp, "\n%s %s %s", "Error with zipfile '", zipFile, "' in unzCloseCurrentFile()");
		MessageBoxA(NULL, temp, "Vanda Engine Error", MB_OK | MB_ICONERROR );
        sout = ""; // empty output string
    }
	unzClose( uf );
    free(buf); // free up buffer memory
    return sout;
}
コード例 #12
0
byte * FileSystemZip::Get( string fileName, int *pSizeOut )
{
	zipCacheMap::iterator itor = m_cache.find(m_rootDir+fileName);

	if (itor == m_cache.end())
	{
		return NULL; //not found in this zip
		//bingo!
	}
	
	int err = UNZ_OK;

	err = unzGoToFilePos(m_uf, &itor->second.m_filepos);
	
	if (err!=UNZ_OK)
	{
		LogError("error %d with zipfile in unzGoToFilePos",err);
		return NULL;
	}

	/*
	//old slow way of locating a file
	if (unzLocateFile(m_uf,(m_rootDir+fileName).c_str(),CASESENSITIVITY)!=UNZ_OK)
	{
		return NULL;
	}
	*/

	unz_file_info file_info;

	char st_filename_inzip[512];

	
	err = unzGetCurrentFileInfo(m_uf,&file_info,st_filename_inzip,sizeof(st_filename_inzip),NULL,0,NULL,0);

	if (err!=UNZ_OK)
	{
		LogError("error %d with zipfile in unzGetCurrentFileInfo",err);
		return NULL;
	}
	

	//let's allocate our own memory and pass the pointer back to them.
	byte *pBytes = new byte[file_info.uncompressed_size+1]; //the extra is because I will add a null later, helps when processing
	//text files and can't hurt
	
	
	if (pBytes)
	{
		//memory allocated
		*pSizeOut =  file_info.uncompressed_size;
		pBytes[file_info.uncompressed_size] = 0;
	}   else
	{
		LogError("Couldn't allocate the required %d bytes to unzip into.", file_info.uncompressed_size+1);
		return NULL;
	}

	err = unzOpenCurrentFile(m_uf);

	if (err!=UNZ_OK)
	{
		LogError("error %d with zipfile in unzOpenCurrentFile",err);
		return NULL;
	}

	err = unzReadCurrentFile(m_uf,pBytes,file_info.uncompressed_size);
	if (err<0)	
	{
		LogError("error %d with zipfile in unzReadCurrentFile",err);
		return NULL;

	}

	err = unzCloseCurrentFile(m_uf);
	if (err!=UNZ_OK)
	{
		LogError("error %d with zipfile in unzCloseCurrentFile",err);
		return NULL;
	}

	return pBytes;
}