⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unzip.c

📁 SFC游戏模拟器 snes9x 1.43 的原代码
💻 C
📖 第 1 页 / 共 3 页
字号:
    pfile_in_zip_read_info->size_local_extrafield = size_local_extrafield;    pfile_in_zip_read_info->pos_local_extrafield = 0;    if (pfile_in_zip_read_info->read_buffer == NULL)    {	TRYFREE (pfile_in_zip_read_info);	return UNZ_INTERNALERROR;    }    pfile_in_zip_read_info->stream_initialised = 0;    if (s->cur_file_info.compression_method > Z_DEFLATED)	err = UNZ_BADZIPFILE;    pfile_in_zip_read_info->crc32_wait = s->cur_file_info.crc;    pfile_in_zip_read_info->crc32 = 0;    pfile_in_zip_read_info->compression_method =	s->cur_file_info.compression_method;    pfile_in_zip_read_info->file = s->file;    pfile_in_zip_read_info->byte_before_the_zipfile = s->byte_before_the_zipfile;    pfile_in_zip_read_info->stream.total_out = 0;    switch (s->cur_file_info.compression_method)    {    case UNZ_STORED:	break;    case UNZ_SHRUNK:	break;    case UNZ_REDUCED1:    case UNZ_REDUCED2:    case UNZ_REDUCED3:    case UNZ_REDUCED4:	break;    case UNZ_IMPLODED:	break;    case UNZ_DEFLATED:	pfile_in_zip_read_info->stream.zalloc = (alloc_func) 0;	pfile_in_zip_read_info->stream.zfree = (free_func) 0;	pfile_in_zip_read_info->stream.opaque = (voidpf) 0;	err = inflateInit2 (&pfile_in_zip_read_info->stream, -MAX_WBITS);	if (err == Z_OK)	    pfile_in_zip_read_info->stream_initialised = 1;	/*	 * windowBits is passed < 0 to tell that there is no zlib header.	 * Note that in this case inflate *requires* an extra "dummy" byte	 * after the compressed stream in order to complete decompression and	 * return Z_STREAM_END. In unzip, i don't wait absolutely	 * Z_STREAM_END because I known the size of both compressed and	 * uncompressed data	 */	break;    default:	return UNZ_INTERNALERROR;	break;    }    pfile_in_zip_read_info->rest_read_compressed =	s->cur_file_info.compressed_size;    pfile_in_zip_read_info->rest_read_uncompressed =	s->cur_file_info.uncompressed_size;    pfile_in_zip_read_info->pos_in_zipfile =	s->cur_file_info_internal.offset_curfile + SIZEZIPLOCALHEADER +	iSizeVar;    pfile_in_zip_read_info->stream.avail_in = (uInt) 0;    s->pfile_in_zip_read = pfile_in_zip_read_info;    return UNZ_OK;}/* * Read bytes from the current file. buf contain buffer where data must be * copied len the size of buf. *  * return the number of byte copied if somes bytes are copied return 0 if the * end of file was reached return <0 with error code if there is an error * (UNZ_ERRNO for IO error, or zLib error for uncompress error) */ file_in_zip_read_info_s *pfile_in_zip_read_info = NULL;unz_s *pUnzip = NULL;extern int ZEXPORT unzReadCurrentFile (		    unzFile file,		    voidp buf,		    unsigned len){    int             err = UNZ_OK;    uInt            iRead = 0;    unz_s          *s;    if (file == NULL)	return UNZ_PARAMERROR;    s = (unz_s *) file;    pUnzip = s;    pfile_in_zip_read_info = s->pfile_in_zip_read;    if (pfile_in_zip_read_info == NULL)	return UNZ_PARAMERROR;    if ((pfile_in_zip_read_info->read_buffer == NULL))	return UNZ_END_OF_LIST_OF_FILE;    if (len == 0)	return 0;    pfile_in_zip_read_info->stream.next_out = (Bytef *) buf;    pfile_in_zip_read_info->stream.avail_out = (uInt) len;    if (len > pfile_in_zip_read_info->rest_read_uncompressed)	pfile_in_zip_read_info->stream.avail_out =	    (uInt) pfile_in_zip_read_info->rest_read_uncompressed;    while (pfile_in_zip_read_info->stream.avail_out > 0 && err == UNZ_OK)    {	switch (pfile_in_zip_read_info->compression_method)	{	case UNZ_STORED:	case UNZ_DEFLATED:	    if ((pfile_in_zip_read_info->stream.avail_in == 0) &&		(pfile_in_zip_read_info->rest_read_compressed > 0))	    {		uInt            uReadThis = UNZ_BUFSIZE;		if (pfile_in_zip_read_info->rest_read_compressed < uReadThis)		    uReadThis = (uInt) pfile_in_zip_read_info->rest_read_compressed;		if (uReadThis == 0)		    return UNZ_EOF;		if (fseek (pfile_in_zip_read_info->file,			   pfile_in_zip_read_info->pos_in_zipfile +		pfile_in_zip_read_info->byte_before_the_zipfile, SEEK_SET) != 0)		    return UNZ_ERRNO;		if (fread (pfile_in_zip_read_info->read_buffer, uReadThis, 1,			   pfile_in_zip_read_info->file) != 1)		    return UNZ_ERRNO;		pfile_in_zip_read_info->pos_in_zipfile += uReadThis;		pfile_in_zip_read_info->rest_read_compressed -= uReadThis;		pfile_in_zip_read_info->stream.next_in =		    (Bytef *) pfile_in_zip_read_info->read_buffer;		pfile_in_zip_read_info->stream.avail_in = (uInt) uReadThis;	    }	    break;	}	switch (pfile_in_zip_read_info->compression_method)	{	case UNZ_STORED:	{	    uInt            uDoCopy, i;	    if (pfile_in_zip_read_info->stream.avail_out <		pfile_in_zip_read_info->stream.avail_in)		uDoCopy = pfile_in_zip_read_info->stream.avail_out;	    else		uDoCopy = pfile_in_zip_read_info->stream.avail_in;	    for (i = 0; i < uDoCopy; i++)		*(pfile_in_zip_read_info->stream.next_out + i) =		    *(pfile_in_zip_read_info->stream.next_in + i);	    pfile_in_zip_read_info->crc32 = crc32 (pfile_in_zip_read_info->crc32,				    pfile_in_zip_read_info->stream.next_out,						   uDoCopy);	    pfile_in_zip_read_info->rest_read_uncompressed -= uDoCopy;	    pfile_in_zip_read_info->stream.avail_in -= uDoCopy;	    pfile_in_zip_read_info->stream.avail_out -= uDoCopy;	    pfile_in_zip_read_info->stream.next_out += uDoCopy;	    pfile_in_zip_read_info->stream.next_in += uDoCopy;	    pfile_in_zip_read_info->stream.total_out += uDoCopy;	    iRead += uDoCopy;	    break;	}	case UNZ_SHRUNK:	    iRead = pfile_in_zip_read_info->rest_read_uncompressed;	    unShrink ();	    break;	case UNZ_REDUCED1:	case UNZ_REDUCED2:	case UNZ_REDUCED3:	case UNZ_REDUCED4:	    iRead = pfile_in_zip_read_info->rest_read_uncompressed;	    unReduce ();	    break;	case UNZ_IMPLODED:	    iRead = pfile_in_zip_read_info->rest_read_uncompressed;	    err = explode ();	    break;	case UNZ_DEFLATED:	{	    uLong           uTotalOutBefore, uTotalOutAfter;	    const Bytef    *bufBefore;	    uLong           uOutThis;	    int             flush = Z_SYNC_FLUSH;	    uTotalOutBefore = pfile_in_zip_read_info->stream.total_out;	    bufBefore = pfile_in_zip_read_info->stream.next_out;	    /*	     * if ((pfile_in_zip_read_info->rest_read_uncompressed ==	     * pfile_in_zip_read_info->stream.avail_out) &&	     * (pfile_in_zip_read_info->rest_read_compressed == 0)) flush =	     * Z_FINISH;	     */	    err = inflate (&pfile_in_zip_read_info->stream, flush);	    uTotalOutAfter = pfile_in_zip_read_info->stream.total_out;	    uOutThis = uTotalOutAfter - uTotalOutBefore;	    pfile_in_zip_read_info->crc32 =		crc32 (pfile_in_zip_read_info->crc32, bufBefore,		       (uInt) (uOutThis));	    pfile_in_zip_read_info->rest_read_uncompressed -=		uOutThis;	    iRead += (uInt) (uTotalOutAfter - uTotalOutBefore);	    if (err == Z_STREAM_END)		return (iRead == 0) ? UNZ_EOF : iRead;	    if (err != Z_OK)		break;	    break;	}	default:	    return (UNZ_EOF);	}    }    if (err == Z_OK)	return iRead;    return err;}/* * Give the current position in uncompressed data */extern z_off_t ZEXPORT unztell (unzFile file){    unz_s          *s;    file_in_zip_read_info_s *pfile_in_zip_read_info;    if (file == NULL)	return UNZ_PARAMERROR;    s = (unz_s *) file;    pfile_in_zip_read_info = s->pfile_in_zip_read;    if (pfile_in_zip_read_info == NULL)	return UNZ_PARAMERROR;    return (z_off_t) pfile_in_zip_read_info->stream.total_out;}/* * return 1 if the end of file was reached, 0 elsewhere */extern int ZEXPORT unzeof (unzFile file){    unz_s          *s;    file_in_zip_read_info_s *pfile_in_zip_read_info;    if (file == NULL)	return UNZ_PARAMERROR;    s = (unz_s *) file;    pfile_in_zip_read_info = s->pfile_in_zip_read;    if (pfile_in_zip_read_info == NULL)	return UNZ_PARAMERROR;    if (pfile_in_zip_read_info->rest_read_uncompressed == 0)	return 1;    else	return 0;}/* * Read extra field from the current file (opened by unzOpenCurrentFile) This * is the local-header version of the extra field (sometimes, there is more * info in the local-header version than in the central-header) *  * if buf==NULL, it return the size of the local extra field that can be read *  * if buf!=NULL, len is the size of the buffer, the extra header is copied in * buf. the return value is the number of bytes copied in buf, or (if <0) the * error code */extern int ZEXPORT unzGetLocalExtrafield (		       unzFile file,		       voidp buf,		       unsigned len){    unz_s          *s;    file_in_zip_read_info_s *pfile_in_zip_read_info;    uInt            read_now;    uLong           size_to_read;    if (file == NULL)	return UNZ_PARAMERROR;    s = (unz_s *) file;    pfile_in_zip_read_info = s->pfile_in_zip_read;    if (pfile_in_zip_read_info == NULL)	return UNZ_PARAMERROR;    size_to_read = (pfile_in_zip_read_info->size_local_extrafield -		    pfile_in_zip_read_info->pos_local_extrafield);    if (buf == NULL)	return (int) size_to_read;    if (len > size_to_read)	read_now = (uInt) size_to_read;    else	read_now = (uInt) len;    if (read_now == 0)	return 0;    if (fseek (pfile_in_zip_read_info->file,	       pfile_in_zip_read_info->offset_local_extrafield +	       pfile_in_zip_read_info->pos_local_extrafield, SEEK_SET) != 0)	return UNZ_ERRNO;    if (fread (buf, (uInt) size_to_read, 1, pfile_in_zip_read_info->file) != 1)	return UNZ_ERRNO;    return (int) read_now;}/* * Close the file in zip opened with unzipOpenCurrentFile Return UNZ_CRCERROR * if all the file was read but the CRC is not good */extern int ZEXPORT unzCloseCurrentFile (unzFile file){    int             err = UNZ_OK;    unz_s          *s;    file_in_zip_read_info_s *pfile_in_zip_read_info;    if (file == NULL)	return UNZ_PARAMERROR;    s = (unz_s *) file;    pfile_in_zip_read_info = s->pfile_in_zip_read;    if (pfile_in_zip_read_info == NULL)	return UNZ_PARAMERROR;    if (pfile_in_zip_read_info->rest_read_uncompressed == 0)    {	if (pfile_in_zip_read_info->crc32 != pfile_in_zip_read_info->crc32_wait)	    err = UNZ_CRCERROR;    }    TRYFREE (pfile_in_zip_read_info->read_buffer);    pfile_in_zip_read_info->read_buffer = NULL;    if (pfile_in_zip_read_info->stream_initialised)	inflateEnd (&pfile_in_zip_read_info->stream);    pfile_in_zip_read_info->stream_initialised = 0;    TRYFREE (pfile_in_zip_read_info);    s->pfile_in_zip_read = NULL;    return err;}/* * Get the global comment string of the ZipFile, in the szComment buffer. * uSizeBuf is the size of the szComment buffer. return the number of byte * copied or an error code <0 */extern int ZEXPORT unzGetGlobalComment (		     unzFile file,		     char *szComment,		     uLong uSizeBuf){    unz_s          *s;    uLong           uReadThis;    if (file == NULL)	return UNZ_PARAMERROR;    s = (unz_s *) file;    uReadThis = uSizeBuf;    if (uReadThis > s->gi.size_comment)	uReadThis = s->gi.size_comment;    if (fseek (s->file, s->central_pos + 22, SEEK_SET) != 0)	return UNZ_ERRNO;    if (uReadThis > 0)    {	*szComment = '\0';	if (fread (szComment, (uInt) uReadThis, 1, s->file) != 1)	    return UNZ_ERRNO;    }    if ((szComment != NULL) && (uSizeBuf > s->gi.size_comment))	*(szComment + s->gi.size_comment) = '\0';    return (int) uReadThis;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -