1313#include "odb.h"
1414#include "oid.h"
1515#include "sha1_lookup.h"
16- #include "zstream.h"
17-
18- #include <zlib.h>
1916
2017/* Option to bypass checking existence of '.keep' files */
2118bool git_disable_pack_keep_file_checks = false;
@@ -766,31 +763,13 @@ int git_packfile_unpack(
766763 return error ;
767764}
768765
769- static void * use_git_alloc (void * opaq , unsigned int count , unsigned int size )
770- {
771- GIT_UNUSED (opaq );
772- return git__calloc (count , size );
773- }
774-
775- static void use_git_free (void * opaq , void * ptr )
776- {
777- GIT_UNUSED (opaq );
778- git__free (ptr );
779- }
780-
781766int git_packfile_stream_open (git_packfile_stream * obj , struct git_pack_file * p , off64_t curpos )
782767{
783- int st ;
784-
785768 memset (obj , 0 , sizeof (git_packfile_stream ));
786769 obj -> curpos = curpos ;
787770 obj -> p = p ;
788- obj -> zstream .zalloc = use_git_alloc ;
789- obj -> zstream .zfree = use_git_free ;
790- obj -> zstream .next_in = Z_NULL ;
791- obj -> zstream .next_out = Z_NULL ;
792- st = inflateInit (& obj -> zstream );
793- if (st != Z_OK ) {
771+
772+ if (git_zstream_init (& obj -> zstream , GIT_ZSTREAM_INFLATE ) < 0 ) {
794773 git_error_set (GIT_ERROR_ZLIB , "failed to init packfile stream" );
795774 return -1 ;
796775 }
@@ -800,47 +779,41 @@ int git_packfile_stream_open(git_packfile_stream *obj, struct git_pack_file *p,
800779
801780ssize_t git_packfile_stream_read (git_packfile_stream * obj , void * buffer , size_t len )
802781{
782+ unsigned int window_len ;
803783 unsigned char * in ;
804- size_t written ;
805- int st ;
784+ int error ;
806785
807786 if (obj -> done )
808787 return 0 ;
809788
810- in = pack_window_open (obj -> p , & obj -> mw , obj -> curpos , & obj -> zstream .avail_in );
811- if (in == NULL )
789+ if ((in = pack_window_open (obj -> p , & obj -> mw , obj -> curpos , & window_len )) == NULL )
812790 return GIT_EBUFS ;
813791
814- obj -> zstream .next_out = buffer ;
815- obj -> zstream .avail_out = (unsigned int )len ;
816- obj -> zstream .next_in = in ;
817-
818- st = inflate (& obj -> zstream , Z_SYNC_FLUSH );
819- git_mwindow_close (& obj -> mw );
820-
821- obj -> curpos += obj -> zstream .next_in - in ;
822- written = len - obj -> zstream .avail_out ;
823-
824- if (st != Z_OK && st != Z_STREAM_END ) {
792+ if ((error = git_zstream_set_input (& obj -> zstream , in , window_len )) < 0 ||
793+ (error = git_zstream_get_output_chunk (buffer , & len , & obj -> zstream )) < 0 ) {
794+ git_mwindow_close (& obj -> mw );
825795 git_error_set (GIT_ERROR_ZLIB , "error reading from the zlib stream" );
826796 return -1 ;
827797 }
828798
829- if (st == Z_STREAM_END )
830- obj -> done = 1 ;
799+ git_mwindow_close (& obj -> mw );
831800
801+ obj -> curpos += window_len - obj -> zstream .in_len ;
802+
803+ if (git_zstream_eos (& obj -> zstream ))
804+ obj -> done = 1 ;
832805
833806 /* If we didn't write anything out but we're not done, we need more data */
834- if (!written && st != Z_STREAM_END )
807+ if (!len && ! git_zstream_eos ( & obj -> zstream ) )
835808 return GIT_EBUFS ;
836809
837- return written ;
810+ return len ;
838811
839812}
840813
841814void git_packfile_stream_dispose (git_packfile_stream * obj )
842815{
843- inflateEnd (& obj -> zstream );
816+ git_zstream_free (& obj -> zstream );
844817}
845818
846819static int packfile_unpack_compressed (
0 commit comments