Skip to content

Commit bdb5421

Browse files
committed
hash: commoncrypto hash should support large files
Teach the CommonCrypto hash mechanisms to support large files. The hash primitives take a `CC_LONG` (aka `uint32_t`) at a time. So loop to give the hash function at most an unsigned 32 bit's worth of bytes until we have hashed the entire file.
1 parent a89560d commit bdb5421

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/hash/hash_common_crypto.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ struct git_hash_ctx {
1616
CC_SHA1_CTX c;
1717
};
1818

19+
#define CC_LONG_MAX ((CC_LONG)-1)
20+
1921
#define git_hash_global_init() 0
2022
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2123
#define git_hash_ctx_cleanup(ctx)
@@ -27,10 +29,21 @@ GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
2729
return 0;
2830
}
2931

30-
GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *data, size_t len)
32+
GIT_INLINE(int) git_hash_update(git_hash_ctx *ctx, const void *_data, size_t len)
3133
{
34+
const unsigned char *data = _data;
35+
3236
assert(ctx);
33-
CC_SHA1_Update(&ctx->c, data, len);
37+
38+
while (len > 0) {
39+
CC_LONG chunk = (len > CC_LONG_MAX) ? CC_LONG_MAX : (CC_LONG)len;
40+
41+
CC_SHA1_Update(&ctx->c, data, chunk);
42+
43+
data += chunk;
44+
len -= chunk;
45+
}
46+
3447
return 0;
3548
}
3649

0 commit comments

Comments
 (0)