Skip to content

Commit a89560d

Browse files
committed
hash: win32 hash mechanism should support large files
Teach the win32 hash mechanisms to support large files. The hash primitives take at most `ULONG_MAX` bytes at a time. Loop, giving the hash function the maximum supported number of bytes, until we have hashed the entire file.
1 parent 3e6533b commit a89560d

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/hash/hash_win32.c

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,21 @@ GIT_INLINE(int) hash_cryptoapi_init(git_hash_ctx *ctx)
136136
return 0;
137137
}
138138

139-
GIT_INLINE(int) hash_cryptoapi_update(git_hash_ctx *ctx, const void *data, size_t len)
139+
GIT_INLINE(int) hash_cryptoapi_update(git_hash_ctx *ctx, const void *_data, size_t len)
140140
{
141+
const BYTE *data = (BYTE *)_data;
142+
141143
assert(ctx->ctx.cryptoapi.valid);
142144

143-
if (!CryptHashData(ctx->ctx.cryptoapi.hash_handle, (const BYTE *)data, (DWORD)len, 0))
144-
return -1;
145+
while (len > 0) {
146+
DWORD chunk = (len > MAXDWORD) ? MAXDWORD : (DWORD)len;
147+
148+
if (!CryptHashData(ctx->ctx.cryptoapi.hash_handle, data, chunk, 0))
149+
return -1;
150+
151+
data += chunk;
152+
len -= chunk;
153+
}
145154

146155
return 0;
147156
}
@@ -202,10 +211,19 @@ GIT_INLINE(int) hash_cng_init(git_hash_ctx *ctx)
202211
return 0;
203212
}
204213

205-
GIT_INLINE(int) hash_cng_update(git_hash_ctx *ctx, const void *data, size_t len)
214+
GIT_INLINE(int) hash_cng_update(git_hash_ctx *ctx, const void *_data, size_t len)
206215
{
207-
if (ctx->prov->prov.cng.hash_data(ctx->ctx.cng.hash_handle, (PBYTE)data, (ULONG)len, 0) < 0)
208-
return -1;
216+
PBYTE data = (PBYTE)_data;
217+
218+
while (len > 0) {
219+
ULONG chunk = (len > ULONG_MAX) ? ULONG_MAX : (ULONG)len;
220+
221+
if (ctx->prov->prov.cng.hash_data(ctx->ctx.cng.hash_handle, data, chunk, 0) < 0)
222+
return -1;
223+
224+
data += chunk;
225+
len -= chunk;
226+
}
209227

210228
return 0;
211229
}

0 commit comments

Comments
 (0)