@@ -1668,50 +1668,41 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
1668
1668
inflateEnd (& z_info .strm );
1669
1669
return 0 ;
1670
1670
}
1671
- if (info .file_length > 0 ) {
1672
- char buf [64 * 1024 ];
1673
- if (info .file_length < sizeof (buf )) {
1674
- if (!read_data (buf , info .file_length , & z_info , input )) {
1671
+ fclose (out_file );
1672
+ out_file = fopen (full_name , "ab" );
1673
+ if (out_file == NULL ) {
1674
+ fprintf (stderr , "%s: Unable to open %s for appending: %s\n" ,
1675
+ routine_name , full_name , strerror (errno ));
1676
+ inflateEnd (& z_info .strm );
1677
+ return 0 ;
1678
+ }
1679
+ char buf [64 * 1024 ];
1680
+ size_t left_to_read = info .file_length ;
1681
+ // If left_to_read == 0 then read_data() will error out with Z_BUF_ERROR
1682
+ // since we'd be giving it '0' bytes of output space to put the
1683
+ // decompressed data
1684
+ if (left_to_read > 0 ) {
1685
+ for (;;) {
1686
+ const size_t num_to_read =
1687
+ (left_to_read > sizeof (buf )) ? sizeof (buf ) : left_to_read ;
1688
+ if (!read_data (buf , num_to_read , & z_info , input )) {
1675
1689
fprintf (stderr , "%s: Error reading file data for %s from buffer\n" ,
1676
1690
routine_name , full_name );
1677
1691
fclose (out_file );
1678
1692
inflateEnd (& z_info .strm );
1679
1693
return 0 ;
1680
1694
}
1681
- if (fwrite (buf , info . file_length , 1 , out_file ) != 1 ) {
1695
+ if (fwrite (buf , num_to_read , 1 , out_file ) != 1 ) {
1682
1696
fprintf (stderr , "%s: Failed to write to %s: %s\n" , routine_name ,
1683
1697
full_name , strerror (errno ));
1684
1698
fclose (out_file );
1685
1699
inflateEnd (& z_info .strm );
1686
1700
return 0 ;
1687
1701
}
1688
- } else {
1689
- char * buf2 = malloc (info .file_length );
1690
- if (buf2 == NULL ) {
1691
- fprintf (stderr , "%s: Failed to allocate buffer to write %s: %s\n" ,
1692
- routine_name , full_name , strerror (errno ));
1693
- fclose (out_file );
1694
- free (buf2 );
1695
- inflateEnd (& z_info .strm );
1696
- return PACK_END ;
1697
- }
1698
- if (!read_data (buf2 , info .file_length , & z_info , input )) {
1699
- fprintf (stderr , "%s: Error reading file data for %s from buffer\n" ,
1700
- routine_name , full_name );
1701
- fclose (out_file );
1702
- free (buf2 );
1703
- inflateEnd (& z_info .strm );
1704
- return 0 ;
1705
- }
1706
- if (fwrite (buf2 , info .file_length , 1 , out_file ) != 1 ) {
1707
- fprintf (stderr , "%s: Failed to write to %s: %s\n" , routine_name ,
1708
- full_name , strerror (errno ));
1709
- fclose (out_file );
1710
- free (buf2 );
1711
- inflateEnd (& z_info .strm );
1712
- return 0 ;
1702
+ if (left_to_read <= sizeof (buf )) {
1703
+ break ;
1713
1704
}
1714
- free ( buf2 );
1705
+ left_to_read -= sizeof ( buf );
1715
1706
}
1716
1707
}
1717
1708
fclose (out_file );
0 commit comments