Browse Source

filesystem: zip: remove 'big' block

pull/2/head
Mr0maks 6 years ago
parent
commit
01a33dcf7e
  1. 155
      engine/common/filesystem.c

155
engine/common/filesystem.c

@ -830,119 +830,118 @@ static byte *Zip_LoadFile( const char *path, fs_offset_t *sizeptr, qboolean game @@ -830,119 +830,118 @@ static byte *Zip_LoadFile( const char *path, fs_offset_t *sizeptr, qboolean game
search = FS_FindFile( path, &index, gamedironly );
if( search && search->zip )
{
if( !search || !search->zip )
return NULL;
file = &search->zip->files[index];
file = &search->zip->files[index];
FS_Seek( search->zip->handle, file->offset, SEEK_SET );
FS_Read( search->zip->handle, (void*)&header, sizeof( header ) );
FS_Seek( search->zip->handle, file->offset, SEEK_SET );
FS_Read( search->zip->handle, (void*)&header, sizeof( header ) );
if( header.signature != ZIP_HEADER_LF )
{
Con_Reportf( S_ERROR "Zip_LoadFile: %s signature error\n", file->name );
return NULL;
}
if( header.signature != ZIP_HEADER_LF )
{
Con_Reportf( S_ERROR "Zip_LoadFile: %s signature error\n", file->name );
return NULL;
}
if( header.compression_flags == ZIP_COMPRESSION_NO_COMPRESSION )
{
if( header.filename_len )
FS_Seek( search->zip->handle, header.filename_len, SEEK_CUR );
if( header.compression_flags == ZIP_COMPRESSION_NO_COMPRESSION )
{
if( header.filename_len )
FS_Seek( search->zip->handle, header.filename_len, SEEK_CUR );
if( header.extrafield_len )
FS_Seek( search->zip->handle, header.extrafield_len, SEEK_CUR );
if( header.extrafield_len )
FS_Seek( search->zip->handle, header.extrafield_len, SEEK_CUR );
decompressed_buffer = Mem_Malloc( search->zip->mempool, file->size + 1 );
decompressed_buffer[file->size] = '\0';
decompressed_buffer = Mem_Malloc( search->zip->mempool, file->size + 1 );
decompressed_buffer[file->size] = '\0';
FS_Read( search->zip->handle, decompressed_buffer, file->size );
FS_Read( search->zip->handle, decompressed_buffer, file->size );
CRC32_Init( &test_crc );
CRC32_ProcessBuffer( &test_crc, decompressed_buffer, file->size );
CRC32_Init( &test_crc );
CRC32_ProcessBuffer( &test_crc, decompressed_buffer, file->size );
final_crc = CRC32_Final( test_crc );
final_crc = CRC32_Final( test_crc );
if( final_crc != header.crc32 )
{
Con_Reportf( S_ERROR "Zip_LoadFile: %s file crc32 mismatch\n", file->name );
Mem_Free( decompressed_buffer );
return NULL;
}
if( sizeptr ) *sizeptr = file->size;
return decompressed_buffer;
}
else if( header.compression_flags == ZIP_COMPRESSION_DEFLATED )
if( final_crc != header.crc32 )
{
if( header.filename_len )
FS_Seek( search->zip->handle, header.filename_len, SEEK_CUR );
Con_Reportf( S_ERROR "Zip_LoadFile: %s file crc32 mismatch\n", file->name );
Mem_Free( decompressed_buffer );
return NULL;
}
if( header.extrafield_len )
FS_Seek( search->zip->handle, header.extrafield_len, SEEK_CUR );
if( sizeptr ) *sizeptr = file->size;
compressed_buffer = Mem_Malloc( search->zip->mempool, file->compressed_size + 1 );
decompressed_buffer = Mem_Malloc( search->zip->mempool, file->size + 1 );
decompressed_buffer[file->size] = '\0';
return decompressed_buffer;
}
else if( header.compression_flags == ZIP_COMPRESSION_DEFLATED )
{
if( header.filename_len )
FS_Seek( search->zip->handle, header.filename_len, SEEK_CUR );
FS_Read( search->zip->handle, compressed_buffer, file->compressed_size );
if( header.extrafield_len )
FS_Seek( search->zip->handle, header.extrafield_len, SEEK_CUR );
memset( &decompress_stream, 0, sizeof( decompress_stream ) );
compressed_buffer = Mem_Malloc( search->zip->mempool, file->compressed_size + 1 );
decompressed_buffer = Mem_Malloc( search->zip->mempool, file->size + 1 );
decompressed_buffer[file->size] = '\0';
decompress_stream.total_in = decompress_stream.avail_in = file->compressed_size;
decompress_stream.next_in = (Bytef *)compressed_buffer;
decompress_stream.total_out = decompress_stream.avail_out = file->size;
decompress_stream.next_out = (Bytef *)decompressed_buffer;
FS_Read( search->zip->handle, compressed_buffer, file->compressed_size );
decompress_stream.zalloc = Z_NULL;
decompress_stream.zfree = Z_NULL;
decompress_stream.opaque = Z_NULL;
memset( &decompress_stream, 0, sizeof( decompress_stream ) );
if( inflateInit2( &decompress_stream, -MAX_WBITS ) != Z_OK )
{
Con_Printf( S_ERROR "Zip_LoadFile: inflateInit2 failed\n" );
Mem_Free( compressed_buffer );
Mem_Free( decompressed_buffer );
return NULL;
}
decompress_stream.total_in = decompress_stream.avail_in = file->compressed_size;
decompress_stream.next_in = (Bytef *)compressed_buffer;
decompress_stream.total_out = decompress_stream.avail_out = file->size;
decompress_stream.next_out = (Bytef *)decompressed_buffer;
zlib_result = inflate( &decompress_stream, Z_NO_FLUSH );
inflateEnd( &decompress_stream );
decompress_stream.zalloc = Z_NULL;
decompress_stream.zfree = Z_NULL;
decompress_stream.opaque = Z_NULL;
if( zlib_result == Z_OK || zlib_result == Z_STREAM_END )
{
Mem_Free( compressed_buffer ); // finaly free compressed buffer
if( inflateInit2( &decompress_stream, -MAX_WBITS ) != Z_OK )
{
Con_Printf( S_ERROR "Zip_LoadFile: inflateInit2 failed\n" );
Mem_Free( compressed_buffer );
Mem_Free( decompressed_buffer );
return NULL;
}
CRC32_Init( &test_crc );
CRC32_ProcessBuffer( &test_crc, decompressed_buffer, file->size );
zlib_result = inflate( &decompress_stream, Z_NO_FLUSH );
inflateEnd( &decompress_stream );
final_crc = CRC32_Final( test_crc );
if( zlib_result == Z_OK || zlib_result == Z_STREAM_END )
{
Mem_Free( compressed_buffer ); // finaly free compressed buffer
if( final_crc != header.crc32 )
{
Con_Reportf( S_ERROR "Zip_LoadFile: %s file crc32 mismatch\n", file->name );
Mem_Free( decompressed_buffer );
return NULL;
}
CRC32_Init( &test_crc );
CRC32_ProcessBuffer( &test_crc, decompressed_buffer, file->size );
if( sizeptr ) *sizeptr = file->size;
final_crc = CRC32_Final( test_crc );
return decompressed_buffer;
}
else
if( final_crc != header.crc32 )
{
Con_Reportf( S_ERROR "Zip_LoadFile: %s : error while file decompressing. Zlib return code %d.\n", file->name, zlib_result );
Mem_Free( compressed_buffer );
Con_Reportf( S_ERROR "Zip_LoadFile: %s file crc32 mismatch\n", file->name );
Mem_Free( decompressed_buffer );
return NULL;
}
if( sizeptr ) *sizeptr = file->size;
return decompressed_buffer;
}
else
{
Con_Reportf( S_ERROR "Zip_LoadFile: %s : file compressed with unknown algorithm.\n", file->name );
Con_Reportf( S_ERROR "Zip_LoadFile: %s : error while file decompressing. Zlib return code %d.\n", file->name, zlib_result );
Mem_Free( compressed_buffer );
Mem_Free( decompressed_buffer );
return NULL;
}
}
else
{
Con_Reportf( S_ERROR "Zip_LoadFile: %s : file compressed with unknown algorithm.\n", file->name );
return NULL;
}
return NULL;

Loading…
Cancel
Save