1111#include "progress.h"
1212#include "csum-file.h"
1313
14- static void sha1flush (struct sha1file * f , unsigned int count )
14+ static void sha1flush (struct sha1file * f , void * buf , unsigned int count )
1515{
16- void * buf = f -> buffer ;
17-
1816 for (;;) {
1917 int ret = xwrite (f -> fd , buf , count );
2018 if (ret > 0 ) {
@@ -39,15 +37,15 @@ int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
3937
4038 if (offset ) {
4139 SHA1_Update (& f -> ctx , f -> buffer , offset );
42- sha1flush (f , offset );
40+ sha1flush (f , f -> buffer , offset );
4341 f -> offset = 0 ;
4442 }
4543 SHA1_Final (f -> buffer , & f -> ctx );
4644 if (result )
4745 hashcpy (result , f -> buffer );
4846 if (flags & (CSUM_CLOSE | CSUM_FSYNC )) {
4947 /* write checksum and close fd */
50- sha1flush (f , 20 );
48+ sha1flush (f , f -> buffer , 20 );
5149 if (flags & CSUM_FSYNC )
5250 fsync_or_die (f -> fd , f -> name );
5351 if (close (f -> fd ))
@@ -62,21 +60,30 @@ int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
6260
6361int sha1write (struct sha1file * f , void * buf , unsigned int count )
6462{
65- if (f -> do_crc )
66- f -> crc32 = crc32 (f -> crc32 , buf , count );
6763 while (count ) {
6864 unsigned offset = f -> offset ;
6965 unsigned left = sizeof (f -> buffer ) - offset ;
7066 unsigned nr = count > left ? left : count ;
67+ void * data ;
68+
69+ if (f -> do_crc )
70+ f -> crc32 = crc32 (f -> crc32 , buf , nr );
71+
72+ if (nr == sizeof (f -> buffer )) {
73+ /* process full buffer directly without copy */
74+ data = buf ;
75+ } else {
76+ memcpy (f -> buffer + offset , buf , nr );
77+ data = f -> buffer ;
78+ }
7179
72- memcpy (f -> buffer + offset , buf , nr );
7380 count -= nr ;
7481 offset += nr ;
7582 buf = (char * ) buf + nr ;
7683 left -= nr ;
7784 if (!left ) {
78- SHA1_Update (& f -> ctx , f -> buffer , offset );
79- sha1flush (f , offset );
85+ SHA1_Update (& f -> ctx , data , offset );
86+ sha1flush (f , data , offset );
8087 offset = 0 ;
8188 }
8289 f -> offset = offset ;
0 commit comments