Skip to content

Commit 001fd7a

Browse files
Atousagitster
authored andcommitted
sha1: allow limiting the size of the data passed to SHA1_Update()
Using the previous commit's inredirection mechanism for SHA1, support a chunked implementation of SHA1_Update() that limits the amount of data in the chunk passed to SHA1_Update(). This is enabled by using the Makefile variable SHA1_MAX_BLOCK_SIZE to specify chunk size. When using Apple's CommonCrypto library this is set to 1GiB (the implementation cannot handle more 4GiB). Signed-off-by: Atousa Pahlevan Duprat <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3bc72fd commit 001fd7a

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ all::
141141
# Define PPC_SHA1 environment variable when running make to make use of
142142
# a bundled SHA1 routine optimized for PowerPC.
143143
#
144+
# Define SHA1_MAX_BLOCK_SIZE to limit the amount of data that will be hashed
145+
# in one call to the platform's SHA1_Update(). e.g. APPLE_COMMON_CRYPTO
146+
# wants 'SHA1_MAX_BLOCK_SIZE=1024L*1024L*1024L' defined.
147+
#
144148
# Define NEEDS_CRYPTO_WITH_SSL if you need -lcrypto when using -lssl (Darwin).
145149
#
146150
# Define NEEDS_SSL_WITH_CRYPTO if you need -lssl when using -lcrypto (Darwin).
@@ -1276,6 +1280,11 @@ ifdef NO_POSIX_GOODIES
12761280
BASIC_CFLAGS += -DNO_POSIX_GOODIES
12771281
endif
12781282

1283+
ifdef APPLE_COMMON_CRYPTO
1284+
# Apple CommonCrypto requires chunking
1285+
SHA1_MAX_BLOCK_SIZE = 1024L*1024L*1024L
1286+
endif
1287+
12791288
ifdef BLK_SHA1
12801289
SHA1_HEADER = "block-sha1/sha1.h"
12811290
LIB_OBJS += block-sha1/sha1.o
@@ -1294,6 +1303,10 @@ endif
12941303
endif
12951304
endif
12961305

1306+
ifdef SHA1_MAX_BLOCK_SIZE
1307+
LIB_OBJS += compat/sha1-chunked.o
1308+
BASIC_CFLAGS += -DSHA1_MAX_BLOCK_SIZE="$(SHA1_MAX_BLOCK_SIZE)"
1309+
endif
12971310
ifdef NO_PERL_MAKEMAKER
12981311
export NO_PERL_MAKEMAKER
12991312
endif

cache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
#define git_SHA1_Update platform_SHA1_Update
3131
#define git_SHA1_Final platform_SHA1_Final
3232

33+
#ifdef SHA1_MAX_BLOCK_SIZE
34+
#include "compat/sha1-chunked.h"
35+
#undef git_SHA1_Update
36+
#define git_SHA1_Update git_SHA1_Update_Chunked
37+
#endif
38+
3339
#include <zlib.h>
3440
typedef struct git_zstream {
3541
z_stream z;

compat/apple-common-crypto.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#undef TYPE_BOOL
1717
#endif
1818

19+
#ifndef SHA1_MAX_BLOCK_SIZE
20+
#error Using Apple Common Crypto library requires setting SHA1_MAX_BLOCK_SIZE
21+
#endif
22+
1923
#ifdef APPLE_LION_OR_NEWER
2024
#define git_CC_error_check(pattern, err) \
2125
do { \

compat/sha1-chunked.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "cache.h"
2+
3+
int git_SHA1_Update_Chunked(platform_SHA_CTX *c, const void *data, size_t len)
4+
{
5+
size_t nr;
6+
size_t total = 0;
7+
const char *cdata = (const char*)data;
8+
9+
while (len) {
10+
nr = len;
11+
if (nr > SHA1_MAX_BLOCK_SIZE)
12+
nr = SHA1_MAX_BLOCK_SIZE;
13+
platform_SHA1_Update(c, cdata, nr);
14+
total += nr;
15+
cdata += nr;
16+
len -= nr;
17+
}
18+
return total;
19+
}

compat/sha1-chunked.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
int git_SHA1_Update_Chunked(platform_SHA_CTX *c, const void *data, size_t len);

0 commit comments

Comments
 (0)