static long url_inflate_read(URL url, void *buff, long n)
{
    URL_inflate *urlp = (URL_inflate *)url;

    n = zip_inflate(urlp->decoder, (char *)buff, n);
    if(n <= 0)
	return n;
    urlp->pos += n;
    return n;
}
示例#2
0
文件: zip.c 项目: pombredanne/nmergec
void test_zip( int *passed, int *failed )
{
    int res = 0;
    char_buf *cb = char_buf_create( 1024 );
    if ( cb != NULL )
    {
        int slen = strlen(src);
        int blen = slen*100;
        unsigned char *bbuf = calloc( blen+1, 1 );
        if ( bbuf != NULL )
        {
            int i,pos;
            // fill bbuf with copies of test text
            for ( i=0,pos=0;i<100;i++ )
            {
                memcpy( &bbuf[pos], src, slen );
                pos += slen;
            }
            //  jumble up the sample text
            for ( i=0;i<blen;i++ )
            {
                int d = rand()%blen;
                if ( i != d )
                {
                    unsigned char u = bbuf[d];
                    bbuf[d] = bbuf[i];
                    bbuf[i] = u;
                }
            }
            // now compress it
            res = zip_deflate( bbuf, pos, cb );
            if ( res )
            {
                int len;
                char_buf *text = char_buf_create( 1024 );
                if ( text != NULL )
                {
                    int ilen;
                    // now decompress
                    unsigned char *buf = char_buf_get(cb,&len);
                    res = zip_inflate( buf,len,text);
                    unsigned char *t = char_buf_get(text,&ilen);
                    // check that the decompressed text matches
                    if ( blen == ilen )
                    {
                        for ( i=0;i<ilen;i++ )
                        {
                            if ( bbuf[i] != t[i] )
                            {
                                fprintf(stderr,"zip: mismatch at %d\n",i);
                                break;
                            }
                        }
                        if ( i == ilen )
                        {
                            *passed += 1;
                            res = 1;
                        }
                    }
                    char_buf_dispose( text );
                }
            }
            free( bbuf );
        }
        char_buf_dispose( cb );
    }
    if ( !res )
        *failed += 1;
}
示例#3
0
文件: unzip.c 项目: sdlBasic/sdlbrt
/*	Pass the path to the zipfile and the name of the file within the zipfile.
	buf will be set to point to the uncompressed image of that zipped file.
	length will be set to the length of the uncompressed data. */
int /* error */ load_zipped_file (const char *zipfile, const char *filename,
	unsigned char **buf, int *length)
{
	FILE 				*fp = NULL;
	t_end_of_cent_dir	ecd;
	t_central_dir_ent	cd;
	t_local_file_hdr	lfh;
	unsigned char		*inbuf = 0, *outbuf = 0;
	char				filenameUpper[32], *p;
	int 				err;

	/* open zipfile for binary read */
	if ((fp = fopen (zipfile,"rb")) != NULL)
	{
		/* determine length of zip file */
		err = get_file_length (fp, &gZipLen);
		if (err!=0)
		{
			ERRORMSG ("Error in zipfile: get_file_length() failed\n");
			goto bail;
		}

		/* read end-of-central-directory */
		err = read_end_of_cent_dir (fp, &ecd);
		if (err!=0)
		{
			ERRORMSG ("Error reading 'end of central directory'\n");
			goto bail;
		}

		/* verify that we can work with this zipfile (no disk spanning allowed) */
		if ((ecd.number_of_this_disk != ecd.number_of_disk_start_cent_dir) ||
			(ecd.total_entries_cent_dir_this_disk != ecd.total_entries_cent_dir) ||
			(ecd.total_entries_cent_dir < 1))
		{
			err = -1;
			ERRORMSG ("Unsupported zipfile: zipfile cannot span disks\n");
			goto bail;
		}

		/* find matching file in central directory (force upper case) */
		for (p=filenameUpper; (*p++ = toupper(*filename++)) != '\0';){};

		err = find_matching_cd_entry (fp, filenameUpper, &ecd, &cd);
		if (err!=0)
		{
			ERRORMSG("Could not find %s in zipfile %s\n", filenameUpper, zipfile);
			goto bail;
		}

		/* read in local file header */
		err = read_local_file_header (fp, &cd, &lfh);
		if (err!=0)
		{
			ERRORMSG ("Error reading 'local file header'\n");
			goto bail;
		}

		/* extract file based on compression method */
		if (lfh.compression_method == 0x0000)
		{
			/* file is not compressed, simply stored -- copy directly to output buffer */
			err = create_input_buffer (fp, &cd, &lfh, &outbuf);
			if (err!=0)
				ERRORMSG ("Couldn't extract uncompressed file\n");
		}
		else if (lfh.compression_method == 0x0008)
		{
			/* file is compressed using "Deflate" method */

			/* create input and output buffers */
			err = create_input_buffer (fp, &cd, &lfh, &inbuf);
			if (err==0)
			{
				g_nextbyte = inbuf;
				outbuf = malloc (lfh.uncompressed_size);
				if (outbuf!=0)
					g_outbuf = outbuf;
				else
				{
					ERRORMSG ("Couldn't allocate %d bytes for output buffer\n",
						lfh.uncompressed_size);
					err = -1;
				}
			}
			else
			{
				ERRORMSG ("Could not create input buffer\n");
			}

			/* create sliding window for inflate() */
			if (err==0)
			{
				slide = malloc (0x8000);
				if (slide==0)
				{
					ERRORMSG ("Could not create 32K sliding window\n");
					err = -1;
				}
			}

			/* inflate the compressed file (now in memory) */
			if (err==0)
			{
				err = zip_inflate ();
				if (err!=0)
				{
					ERRORMSG ("Error inflating compressed file: %d", err);
				}
			}
		}

		/* return pointer to uncompressed data */
		if (err==0)
		{
			*buf = outbuf;
			*length = lfh.uncompressed_size;
			outbuf = 0;		/* prevent data from being freed() */
		}
	}
	else
	{
		ERRORMSG ("Could not open zipfile %s\n", zipfile);
		err = -1;
	}

bail:
	if (fp) fclose (fp);
	if (inbuf) free (inbuf);
	if (outbuf) free (outbuf);
	if (slide)
	{
		free (slide);
		slide = 0;
	}
	return err;
}