Git fork

Merge branch 'ps/hash-cleanup'

Further code clean-up on the use of hash functions. Now the
context object knows what hash function it is working with.

* ps/hash-cleanup:
global: adapt callers to use generic hash context helpers
hash: provide generic wrappers to update hash contexts
hash: stop typedeffing the hash context
hash: convert hashing context to a structure

+226 -202
+8 -8
builtin/fast-import.c
··· 954 unsigned char hdr[96]; 955 struct object_id oid; 956 unsigned long hdrlen, deltalen; 957 - git_hash_ctx c; 958 git_zstream s; 959 960 hdrlen = format_object_header((char *)hdr, sizeof(hdr), type, 961 dat->len); 962 the_hash_algo->init_fn(&c); 963 - the_hash_algo->update_fn(&c, hdr, hdrlen); 964 - the_hash_algo->update_fn(&c, dat->buf, dat->len); 965 - the_hash_algo->final_oid_fn(&oid, &c); 966 if (oidout) 967 oidcpy(oidout, &oid); 968 ··· 1096 struct object_id oid; 1097 unsigned long hdrlen; 1098 off_t offset; 1099 - git_hash_ctx c; 1100 git_zstream s; 1101 struct hashfile_checkpoint checkpoint; 1102 int status = Z_OK; ··· 1114 hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len); 1115 1116 the_hash_algo->init_fn(&c); 1117 - the_hash_algo->update_fn(&c, out_buf, hdrlen); 1118 1119 crc32_begin(pack_file); 1120 ··· 1132 if (!n && feof(stdin)) 1133 die("EOF in data (%" PRIuMAX " bytes remaining)", len); 1134 1135 - the_hash_algo->update_fn(&c, in_buf, n); 1136 s.next_in = in_buf; 1137 s.avail_in = n; 1138 len -= n; ··· 1158 } 1159 } 1160 git_deflate_end(&s); 1161 - the_hash_algo->final_oid_fn(&oid, &c); 1162 1163 if (oidout) 1164 oidcpy(oidout, &oid);
··· 954 unsigned char hdr[96]; 955 struct object_id oid; 956 unsigned long hdrlen, deltalen; 957 + struct git_hash_ctx c; 958 git_zstream s; 959 960 hdrlen = format_object_header((char *)hdr, sizeof(hdr), type, 961 dat->len); 962 the_hash_algo->init_fn(&c); 963 + git_hash_update(&c, hdr, hdrlen); 964 + git_hash_update(&c, dat->buf, dat->len); 965 + git_hash_final_oid(&oid, &c); 966 if (oidout) 967 oidcpy(oidout, &oid); 968 ··· 1096 struct object_id oid; 1097 unsigned long hdrlen; 1098 off_t offset; 1099 + struct git_hash_ctx c; 1100 git_zstream s; 1101 struct hashfile_checkpoint checkpoint; 1102 int status = Z_OK; ··· 1114 hdrlen = format_object_header((char *)out_buf, out_sz, OBJ_BLOB, len); 1115 1116 the_hash_algo->init_fn(&c); 1117 + git_hash_update(&c, out_buf, hdrlen); 1118 1119 crc32_begin(pack_file); 1120 ··· 1132 if (!n && feof(stdin)) 1133 die("EOF in data (%" PRIuMAX " bytes remaining)", len); 1134 1135 + git_hash_update(&c, in_buf, n); 1136 s.next_in = in_buf; 1137 s.avail_in = n; 1138 len -= n; ··· 1158 } 1159 } 1160 git_deflate_end(&s); 1161 + git_hash_final_oid(&oid, &c); 1162 1163 if (oidout) 1164 oidcpy(oidout, &oid);
+9 -10
builtin/index-pack.c
··· 151 static off_t consumed_bytes; 152 static off_t max_input_size; 153 static unsigned deepest_delta; 154 - static git_hash_ctx input_ctx; 155 static uint32_t input_crc32; 156 static int input_fd, output_fd; 157 static const char *curr_pack; ··· 301 if (input_offset) { 302 if (output_fd >= 0) 303 write_or_die(output_fd, input_buffer, input_offset); 304 - the_hash_algo->update_fn(&input_ctx, input_buffer, input_offset); 305 memmove(input_buffer, input_buffer + input_offset, input_len); 306 input_offset = 0; 307 } ··· 475 int status; 476 git_zstream stream; 477 void *buf; 478 - git_hash_ctx c; 479 char hdr[32]; 480 int hdrlen; 481 482 if (!is_delta_type(type)) { 483 hdrlen = format_object_header(hdr, sizeof(hdr), type, size); 484 the_hash_algo->init_fn(&c); 485 - the_hash_algo->update_fn(&c, hdr, hdrlen); 486 } else 487 oid = NULL; 488 if (type == OBJ_BLOB && size > big_file_threshold) ··· 502 status = git_inflate(&stream, 0); 503 use(input_len - stream.avail_in); 504 if (oid) 505 - the_hash_algo->update_fn(&c, last_out, stream.next_out - last_out); 506 if (buf == fixed_buf) { 507 stream.next_out = buf; 508 stream.avail_out = sizeof(fixed_buf); ··· 512 bad_object(offset, _("inflate returned %d"), status); 513 git_inflate_end(&stream); 514 if (oid) 515 - the_hash_algo->final_oid_fn(oid, &c); 516 return buf == fixed_buf ? NULL : buf; 517 } 518 ··· 1248 struct ofs_delta_entry *ofs_delta = ofs_deltas; 1249 struct object_id ref_delta_oid; 1250 struct stat st; 1251 - git_hash_ctx tmp_ctx; 1252 1253 if (verbose) 1254 progress = start_progress( ··· 1286 1287 /* Check pack integrity */ 1288 flush(); 1289 - the_hash_algo->init_fn(&tmp_ctx); 1290 - the_hash_algo->clone_fn(&tmp_ctx, &input_ctx); 1291 - the_hash_algo->final_fn(hash, &tmp_ctx); 1292 if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo)) 1293 die(_("pack is corrupted (SHA1 mismatch)")); 1294 use(the_hash_algo->rawsz);
··· 151 static off_t consumed_bytes; 152 static off_t max_input_size; 153 static unsigned deepest_delta; 154 + static struct git_hash_ctx input_ctx; 155 static uint32_t input_crc32; 156 static int input_fd, output_fd; 157 static const char *curr_pack; ··· 301 if (input_offset) { 302 if (output_fd >= 0) 303 write_or_die(output_fd, input_buffer, input_offset); 304 + git_hash_update(&input_ctx, input_buffer, input_offset); 305 memmove(input_buffer, input_buffer + input_offset, input_len); 306 input_offset = 0; 307 } ··· 475 int status; 476 git_zstream stream; 477 void *buf; 478 + struct git_hash_ctx c; 479 char hdr[32]; 480 int hdrlen; 481 482 if (!is_delta_type(type)) { 483 hdrlen = format_object_header(hdr, sizeof(hdr), type, size); 484 the_hash_algo->init_fn(&c); 485 + git_hash_update(&c, hdr, hdrlen); 486 } else 487 oid = NULL; 488 if (type == OBJ_BLOB && size > big_file_threshold) ··· 502 status = git_inflate(&stream, 0); 503 use(input_len - stream.avail_in); 504 if (oid) 505 + git_hash_update(&c, last_out, stream.next_out - last_out); 506 if (buf == fixed_buf) { 507 stream.next_out = buf; 508 stream.avail_out = sizeof(fixed_buf); ··· 512 bad_object(offset, _("inflate returned %d"), status); 513 git_inflate_end(&stream); 514 if (oid) 515 + git_hash_final_oid(oid, &c); 516 return buf == fixed_buf ? NULL : buf; 517 } 518 ··· 1248 struct ofs_delta_entry *ofs_delta = ofs_deltas; 1249 struct object_id ref_delta_oid; 1250 struct stat st; 1251 + struct git_hash_ctx tmp_ctx; 1252 1253 if (verbose) 1254 progress = start_progress( ··· 1286 1287 /* Check pack integrity */ 1288 flush(); 1289 + git_hash_clone(&tmp_ctx, &input_ctx); 1290 + git_hash_final(hash, &tmp_ctx); 1291 if (!hasheq(fill(the_hash_algo->rawsz), hash, the_repository->hash_algo)) 1292 die(_("pack is corrupted (SHA1 mismatch)")); 1293 use(the_hash_algo->rawsz);
+7 -7
builtin/patch-id.c
··· 70 int before = -1, after = -1; 71 int diff_is_binary = 0; 72 char pre_oid_str[GIT_MAX_HEXSZ + 1], post_oid_str[GIT_MAX_HEXSZ + 1]; 73 - git_hash_ctx ctx; 74 75 the_hash_algo->init_fn(&ctx); 76 oidclr(result, the_repository->hash_algo); ··· 85 !skip_prefix(line, "From ", &p) && 86 starts_with(line, "\\ ") && 12 < strlen(line)) { 87 if (verbatim) 88 - the_hash_algo->update_fn(&ctx, line, strlen(line)); 89 continue; 90 } 91 ··· 104 starts_with(line, "Binary files")) { 105 diff_is_binary = 1; 106 before = 0; 107 - the_hash_algo->update_fn(&ctx, pre_oid_str, 108 - strlen(pre_oid_str)); 109 - the_hash_algo->update_fn(&ctx, post_oid_str, 110 - strlen(post_oid_str)); 111 if (stable) 112 flush_one_hunk(result, &ctx); 113 continue; ··· 165 /* Add line to hash algo (possibly removing whitespace) */ 166 len = verbatim ? strlen(line) : remove_space(line); 167 patchlen += len; 168 - the_hash_algo->update_fn(&ctx, line, len); 169 } 170 171 if (!found_next)
··· 70 int before = -1, after = -1; 71 int diff_is_binary = 0; 72 char pre_oid_str[GIT_MAX_HEXSZ + 1], post_oid_str[GIT_MAX_HEXSZ + 1]; 73 + struct git_hash_ctx ctx; 74 75 the_hash_algo->init_fn(&ctx); 76 oidclr(result, the_repository->hash_algo); ··· 85 !skip_prefix(line, "From ", &p) && 86 starts_with(line, "\\ ") && 12 < strlen(line)) { 87 if (verbatim) 88 + git_hash_update(&ctx, line, strlen(line)); 89 continue; 90 } 91 ··· 104 starts_with(line, "Binary files")) { 105 diff_is_binary = 1; 106 before = 0; 107 + git_hash_update(&ctx, pre_oid_str, 108 + strlen(pre_oid_str)); 109 + git_hash_update(&ctx, post_oid_str, 110 + strlen(post_oid_str)); 111 if (stable) 112 flush_one_hunk(result, &ctx); 113 continue; ··· 165 /* Add line to hash algo (possibly removing whitespace) */ 166 len = verbatim ? strlen(line) : remove_space(line); 167 patchlen += len; 168 + git_hash_update(&ctx, line, len); 169 } 170 171 if (!found_next)
+9 -9
builtin/receive-pack.c
··· 566 unsigned char k_ipad[GIT_MAX_BLKSZ]; 567 unsigned char k_opad[GIT_MAX_BLKSZ]; 568 int i; 569 - git_hash_ctx ctx; 570 571 /* RFC 2104 2. (1) */ 572 memset(key, '\0', GIT_MAX_BLKSZ); 573 if (the_hash_algo->blksz < key_len) { 574 the_hash_algo->init_fn(&ctx); 575 - the_hash_algo->update_fn(&ctx, key_in, key_len); 576 - the_hash_algo->final_fn(key, &ctx); 577 } else { 578 memcpy(key, key_in, key_len); 579 } ··· 586 587 /* RFC 2104 2. (3) & (4) */ 588 the_hash_algo->init_fn(&ctx); 589 - the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad)); 590 - the_hash_algo->update_fn(&ctx, text, text_len); 591 - the_hash_algo->final_fn(out, &ctx); 592 593 /* RFC 2104 2. (6) & (7) */ 594 the_hash_algo->init_fn(&ctx); 595 - the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad)); 596 - the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz); 597 - the_hash_algo->final_fn(out, &ctx); 598 } 599 600 static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
··· 566 unsigned char k_ipad[GIT_MAX_BLKSZ]; 567 unsigned char k_opad[GIT_MAX_BLKSZ]; 568 int i; 569 + struct git_hash_ctx ctx; 570 571 /* RFC 2104 2. (1) */ 572 memset(key, '\0', GIT_MAX_BLKSZ); 573 if (the_hash_algo->blksz < key_len) { 574 the_hash_algo->init_fn(&ctx); 575 + git_hash_update(&ctx, key_in, key_len); 576 + git_hash_final(key, &ctx); 577 } else { 578 memcpy(key, key_in, key_len); 579 } ··· 586 587 /* RFC 2104 2. (3) & (4) */ 588 the_hash_algo->init_fn(&ctx); 589 + git_hash_update(&ctx, k_ipad, sizeof(k_ipad)); 590 + git_hash_update(&ctx, text, text_len); 591 + git_hash_final(out, &ctx); 592 593 /* RFC 2104 2. (6) & (7) */ 594 the_hash_algo->init_fn(&ctx); 595 + git_hash_update(&ctx, k_opad, sizeof(k_opad)); 596 + git_hash_update(&ctx, out, the_hash_algo->rawsz); 597 + git_hash_final(out, &ctx); 598 } 599 600 static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
+6 -7
builtin/unpack-objects.c
··· 28 static unsigned int offset, len; 29 static off_t consumed_bytes; 30 static off_t max_input_size; 31 - static git_hash_ctx ctx; 32 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; 33 static struct progress *progress; 34 ··· 70 if (min > sizeof(buffer)) 71 die("cannot fill %d bytes", min); 72 if (offset) { 73 - the_hash_algo->update_fn(&ctx, buffer, offset); 74 memmove(buffer, buffer + offset, len); 75 offset = 0; 76 } ··· 614 { 615 int i; 616 struct object_id oid; 617 - git_hash_ctx tmp_ctx; 618 619 disable_replace_refs(); 620 ··· 667 } 668 the_hash_algo->init_fn(&ctx); 669 unpack_all(); 670 - the_hash_algo->update_fn(&ctx, buffer, offset); 671 - the_hash_algo->init_fn(&tmp_ctx); 672 - the_hash_algo->clone_fn(&tmp_ctx, &ctx); 673 - the_hash_algo->final_oid_fn(&oid, &tmp_ctx); 674 if (strict) { 675 write_rest(); 676 if (fsck_finish(&fsck_options))
··· 28 static unsigned int offset, len; 29 static off_t consumed_bytes; 30 static off_t max_input_size; 31 + static struct git_hash_ctx ctx; 32 static struct fsck_options fsck_options = FSCK_OPTIONS_STRICT; 33 static struct progress *progress; 34 ··· 70 if (min > sizeof(buffer)) 71 die("cannot fill %d bytes", min); 72 if (offset) { 73 + git_hash_update(&ctx, buffer, offset); 74 memmove(buffer, buffer + offset, len); 75 offset = 0; 76 } ··· 614 { 615 int i; 616 struct object_id oid; 617 + struct git_hash_ctx tmp_ctx; 618 619 disable_replace_refs(); 620 ··· 667 } 668 the_hash_algo->init_fn(&ctx); 669 unpack_all(); 670 + git_hash_update(&ctx, buffer, offset); 671 + git_hash_clone(&tmp_ctx, &ctx); 672 + git_hash_final_oid(&oid, &tmp_ctx); 673 if (strict) { 674 write_rest(); 675 if (fsck_finish(&fsck_options))
+5 -5
bulk-checkin.c
··· 162 * with a new pack. 163 */ 164 static int stream_blob_to_pack(struct bulk_checkin_packfile *state, 165 - git_hash_ctx *ctx, off_t *already_hashed_to, 166 int fd, size_t size, const char *path, 167 unsigned flags) 168 { ··· 195 if (rsize < hsize) 196 hsize = rsize; 197 if (hsize) 198 - the_hash_algo->update_fn(ctx, ibuf, hsize); 199 *already_hashed_to = offset; 200 } 201 s.next_in = ibuf; ··· 259 const char *path, unsigned flags) 260 { 261 off_t seekback, already_hashed_to; 262 - git_hash_ctx ctx; 263 unsigned char obuf[16384]; 264 unsigned header_len; 265 struct hashfile_checkpoint checkpoint; ··· 272 header_len = format_object_header((char *)obuf, sizeof(obuf), 273 OBJ_BLOB, size); 274 the_hash_algo->init_fn(&ctx); 275 - the_hash_algo->update_fn(&ctx, obuf, header_len); 276 277 /* Note: idx is non-NULL when we are writing */ 278 if ((flags & HASH_WRITE_OBJECT) != 0) { ··· 307 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1) 308 return error("cannot seek back"); 309 } 310 - the_hash_algo->final_oid_fn(result_oid, &ctx); 311 if (!idx) 312 return 0; 313
··· 162 * with a new pack. 163 */ 164 static int stream_blob_to_pack(struct bulk_checkin_packfile *state, 165 + struct git_hash_ctx *ctx, off_t *already_hashed_to, 166 int fd, size_t size, const char *path, 167 unsigned flags) 168 { ··· 195 if (rsize < hsize) 196 hsize = rsize; 197 if (hsize) 198 + git_hash_update(ctx, ibuf, hsize); 199 *already_hashed_to = offset; 200 } 201 s.next_in = ibuf; ··· 259 const char *path, unsigned flags) 260 { 261 off_t seekback, already_hashed_to; 262 + struct git_hash_ctx ctx; 263 unsigned char obuf[16384]; 264 unsigned header_len; 265 struct hashfile_checkpoint checkpoint; ··· 272 header_len = format_object_header((char *)obuf, sizeof(obuf), 273 OBJ_BLOB, size); 274 the_hash_algo->init_fn(&ctx); 275 + git_hash_update(&ctx, obuf, header_len); 276 277 /* Note: idx is non-NULL when we are writing */ 278 if ((flags & HASH_WRITE_OBJECT) != 0) { ··· 307 if (lseek(fd, seekback, SEEK_SET) == (off_t) -1) 308 return error("cannot seek back"); 309 } 310 + git_hash_final_oid(result_oid, &ctx); 311 if (!idx) 312 return 0; 313
+8 -8
csum-file.c
··· 51 52 if (offset) { 53 if (!f->skip_hash) 54 - f->algop->update_fn(&f->ctx, f->buffer, offset); 55 flush(f, f->buffer, offset); 56 f->offset = 0; 57 } ··· 74 if (f->skip_hash) 75 hashclr(f->buffer, f->algop); 76 else 77 - f->algop->final_fn(f->buffer, &f->ctx); 78 79 if (result) 80 hashcpy(result, f->buffer, f->algop); ··· 129 * f->offset is necessarily zero. 130 */ 131 if (!f->skip_hash) 132 - f->algop->update_fn(&f->ctx, buf, nr); 133 flush(f, buf, nr); 134 } else { 135 /* ··· 218 { 219 hashflush(f); 220 checkpoint->offset = f->total; 221 - f->algop->clone_fn(&checkpoint->ctx, &f->ctx); 222 } 223 224 int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) ··· 229 lseek(f->fd, offset, SEEK_SET) != offset) 230 return -1; 231 f->total = offset; 232 - f->algop->clone_fn(&f->ctx, &checkpoint->ctx); 233 f->offset = 0; /* hashflush() was called in checkpoint */ 234 return 0; 235 } ··· 249 int hashfile_checksum_valid(const unsigned char *data, size_t total_len) 250 { 251 unsigned char got[GIT_MAX_RAWSZ]; 252 - git_hash_ctx ctx; 253 const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo); 254 size_t data_len = total_len - algop->rawsz; 255 ··· 257 return 0; /* say "too short"? */ 258 259 algop->init_fn(&ctx); 260 - algop->update_fn(&ctx, data, data_len); 261 - algop->final_fn(got, &ctx); 262 263 return hasheq(got, data + data_len, algop); 264 }
··· 51 52 if (offset) { 53 if (!f->skip_hash) 54 + git_hash_update(&f->ctx, f->buffer, offset); 55 flush(f, f->buffer, offset); 56 f->offset = 0; 57 } ··· 74 if (f->skip_hash) 75 hashclr(f->buffer, f->algop); 76 else 77 + git_hash_final(f->buffer, &f->ctx); 78 79 if (result) 80 hashcpy(result, f->buffer, f->algop); ··· 129 * f->offset is necessarily zero. 130 */ 131 if (!f->skip_hash) 132 + git_hash_update(&f->ctx, buf, nr); 133 flush(f, buf, nr); 134 } else { 135 /* ··· 218 { 219 hashflush(f); 220 checkpoint->offset = f->total; 221 + git_hash_clone(&checkpoint->ctx, &f->ctx); 222 } 223 224 int hashfile_truncate(struct hashfile *f, struct hashfile_checkpoint *checkpoint) ··· 229 lseek(f->fd, offset, SEEK_SET) != offset) 230 return -1; 231 f->total = offset; 232 + git_hash_clone(&f->ctx, &checkpoint->ctx); 233 f->offset = 0; /* hashflush() was called in checkpoint */ 234 return 0; 235 } ··· 249 int hashfile_checksum_valid(const unsigned char *data, size_t total_len) 250 { 251 unsigned char got[GIT_MAX_RAWSZ]; 252 + struct git_hash_ctx ctx; 253 const struct git_hash_algo *algop = unsafe_hash_algo(the_hash_algo); 254 size_t data_len = total_len - algop->rawsz; 255 ··· 257 return 0; /* say "too short"? */ 258 259 algop->init_fn(&ctx); 260 + git_hash_update(&ctx, data, data_len); 261 + git_hash_final(got, &ctx); 262 263 return hasheq(got, data + data_len, algop); 264 }
+2 -2
csum-file.h
··· 11 int fd; 12 int check_fd; 13 unsigned int offset; 14 - git_hash_ctx ctx; 15 off_t total; 16 struct progress *tp; 17 const char *name; ··· 33 /* Checkpoint */ 34 struct hashfile_checkpoint { 35 off_t offset; 36 - git_hash_ctx ctx; 37 }; 38 39 void hashfile_checkpoint_init(struct hashfile *, struct hashfile_checkpoint *);
··· 11 int fd; 12 int check_fd; 13 unsigned int offset; 14 + struct git_hash_ctx ctx; 15 off_t total; 16 struct progress *tp; 17 const char *name; ··· 33 /* Checkpoint */ 34 struct hashfile_checkpoint { 35 off_t offset; 36 + struct git_hash_ctx ctx; 37 }; 38 39 void hashfile_checkpoint_init(struct hashfile *, struct hashfile_checkpoint *);
+17 -17
diff.c
··· 6392 } 6393 6394 struct patch_id_t { 6395 - git_hash_ctx *ctx; 6396 int patchlen; 6397 }; 6398 ··· 6409 return dst - line; 6410 } 6411 6412 - void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx) 6413 { 6414 unsigned char hash[GIT_MAX_RAWSZ]; 6415 unsigned short carry = 0; 6416 int i; 6417 6418 - the_hash_algo->final_fn(hash, ctx); 6419 the_hash_algo->init_fn(ctx); 6420 /* 20-byte sum, with carry */ 6421 for (i = 0; i < the_hash_algo->rawsz; ++i) { ··· 6434 return 0; 6435 new_len = remove_space(line, len); 6436 6437 - the_hash_algo->update_fn(data->ctx, line, new_len); 6438 data->patchlen += new_len; 6439 return 0; 6440 } 6441 6442 - static void patch_id_add_string(git_hash_ctx *ctx, const char *str) 6443 { 6444 - the_hash_algo->update_fn(ctx, str, strlen(str)); 6445 } 6446 6447 - static void patch_id_add_mode(git_hash_ctx *ctx, unsigned mode) 6448 { 6449 /* large enough for 2^32 in octal */ 6450 char buf[12]; 6451 int len = xsnprintf(buf, sizeof(buf), "%06o", mode); 6452 - the_hash_algo->update_fn(ctx, buf, len); 6453 } 6454 6455 /* returns 0 upon success, and writes result into oid */ ··· 6457 { 6458 struct diff_queue_struct *q = &diff_queued_diff; 6459 int i; 6460 - git_hash_ctx ctx; 6461 struct patch_id_t data; 6462 6463 the_hash_algo->init_fn(&ctx); ··· 6493 len2 = remove_space(p->two->path, strlen(p->two->path)); 6494 patch_id_add_string(&ctx, "diff--git"); 6495 patch_id_add_string(&ctx, "a/"); 6496 - the_hash_algo->update_fn(&ctx, p->one->path, len1); 6497 patch_id_add_string(&ctx, "b/"); 6498 - the_hash_algo->update_fn(&ctx, p->two->path, len2); 6499 6500 if (p->one->mode == 0) { 6501 patch_id_add_string(&ctx, "newfilemode"); ··· 6514 /* don't do anything since we're only populating header info */ 6515 } else if (diff_filespec_is_binary(options->repo, p->one) || 6516 diff_filespec_is_binary(options->repo, p->two)) { 6517 - the_hash_algo->update_fn(&ctx, oid_to_hex(&p->one->oid), 6518 the_hash_algo->hexsz); 6519 - the_hash_algo->update_fn(&ctx, oid_to_hex(&p->two->oid), 6520 the_hash_algo->hexsz); 6521 } else { 6522 if (p->one->mode == 0) { 6523 patch_id_add_string(&ctx, "---/dev/null"); 6524 patch_id_add_string(&ctx, "+++b/"); 6525 - the_hash_algo->update_fn(&ctx, p->two->path, len2); 6526 } else if (p->two->mode == 0) { 6527 patch_id_add_string(&ctx, "---a/"); 6528 - the_hash_algo->update_fn(&ctx, p->one->path, len1); 6529 patch_id_add_string(&ctx, "+++/dev/null"); 6530 } else { 6531 patch_id_add_string(&ctx, "---a/"); 6532 - the_hash_algo->update_fn(&ctx, p->one->path, len1); 6533 patch_id_add_string(&ctx, "+++b/"); 6534 - the_hash_algo->update_fn(&ctx, p->two->path, len2); 6535 } 6536 6537 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
··· 6392 } 6393 6394 struct patch_id_t { 6395 + struct git_hash_ctx *ctx; 6396 int patchlen; 6397 }; 6398 ··· 6409 return dst - line; 6410 } 6411 6412 + void flush_one_hunk(struct object_id *result, struct git_hash_ctx *ctx) 6413 { 6414 unsigned char hash[GIT_MAX_RAWSZ]; 6415 unsigned short carry = 0; 6416 int i; 6417 6418 + git_hash_final(hash, ctx); 6419 the_hash_algo->init_fn(ctx); 6420 /* 20-byte sum, with carry */ 6421 for (i = 0; i < the_hash_algo->rawsz; ++i) { ··· 6434 return 0; 6435 new_len = remove_space(line, len); 6436 6437 + git_hash_update(data->ctx, line, new_len); 6438 data->patchlen += new_len; 6439 return 0; 6440 } 6441 6442 + static void patch_id_add_string(struct git_hash_ctx *ctx, const char *str) 6443 { 6444 + git_hash_update(ctx, str, strlen(str)); 6445 } 6446 6447 + static void patch_id_add_mode(struct git_hash_ctx *ctx, unsigned mode) 6448 { 6449 /* large enough for 2^32 in octal */ 6450 char buf[12]; 6451 int len = xsnprintf(buf, sizeof(buf), "%06o", mode); 6452 + git_hash_update(ctx, buf, len); 6453 } 6454 6455 /* returns 0 upon success, and writes result into oid */ ··· 6457 { 6458 struct diff_queue_struct *q = &diff_queued_diff; 6459 int i; 6460 + struct git_hash_ctx ctx; 6461 struct patch_id_t data; 6462 6463 the_hash_algo->init_fn(&ctx); ··· 6493 len2 = remove_space(p->two->path, strlen(p->two->path)); 6494 patch_id_add_string(&ctx, "diff--git"); 6495 patch_id_add_string(&ctx, "a/"); 6496 + git_hash_update(&ctx, p->one->path, len1); 6497 patch_id_add_string(&ctx, "b/"); 6498 + git_hash_update(&ctx, p->two->path, len2); 6499 6500 if (p->one->mode == 0) { 6501 patch_id_add_string(&ctx, "newfilemode"); ··· 6514 /* don't do anything since we're only populating header info */ 6515 } else if (diff_filespec_is_binary(options->repo, p->one) || 6516 diff_filespec_is_binary(options->repo, p->two)) { 6517 + git_hash_update(&ctx, oid_to_hex(&p->one->oid), 6518 the_hash_algo->hexsz); 6519 + git_hash_update(&ctx, oid_to_hex(&p->two->oid), 6520 the_hash_algo->hexsz); 6521 } else { 6522 if (p->one->mode == 0) { 6523 patch_id_add_string(&ctx, "---/dev/null"); 6524 patch_id_add_string(&ctx, "+++b/"); 6525 + git_hash_update(&ctx, p->two->path, len2); 6526 } else if (p->two->mode == 0) { 6527 patch_id_add_string(&ctx, "---a/"); 6528 + git_hash_update(&ctx, p->one->path, len1); 6529 patch_id_add_string(&ctx, "+++/dev/null"); 6530 } else { 6531 patch_id_add_string(&ctx, "---a/"); 6532 + git_hash_update(&ctx, p->one->path, len1); 6533 patch_id_add_string(&ctx, "+++b/"); 6534 + git_hash_update(&ctx, p->two->path, len2); 6535 } 6536 6537 if (fill_mmfile(options->repo, &mf1, p->one) < 0 ||
+1 -1
diff.h
··· 652 653 int do_diff_cache(const struct object_id *, struct diff_options *); 654 int diff_flush_patch_id(struct diff_options *, struct object_id *, int); 655 - void flush_one_hunk(struct object_id *result, git_hash_ctx *ctx); 656 657 int diff_result_code(struct rev_info *); 658
··· 652 653 int do_diff_cache(const struct object_id *, struct diff_options *); 654 int diff_flush_patch_id(struct diff_options *, struct object_id *, int); 655 + void flush_one_hunk(struct object_id *result, struct git_hash_ctx *ctx); 656 657 int diff_result_code(struct rev_info *); 658
+32 -11
hash.h
··· 234 #endif 235 236 /* A suitably aligned type for stack allocations of hash contexts. */ 237 - union git_hash_ctx { 238 - git_SHA_CTX sha1; 239 - git_SHA_CTX_unsafe sha1_unsafe; 240 - 241 - git_SHA256_CTX sha256; 242 }; 243 - typedef union git_hash_ctx git_hash_ctx; 244 245 - typedef void (*git_hash_init_fn)(git_hash_ctx *ctx); 246 - typedef void (*git_hash_clone_fn)(git_hash_ctx *dst, const git_hash_ctx *src); 247 - typedef void (*git_hash_update_fn)(git_hash_ctx *ctx, const void *in, size_t len); 248 - typedef void (*git_hash_final_fn)(unsigned char *hash, git_hash_ctx *ctx); 249 - typedef void (*git_hash_final_oid_fn)(struct object_id *oid, git_hash_ctx *ctx); 250 251 struct git_hash_algo { 252 /* ··· 295 const struct git_hash_algo *unsafe; 296 }; 297 extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS]; 298 299 /* 300 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
··· 234 #endif 235 236 /* A suitably aligned type for stack allocations of hash contexts. */ 237 + struct git_hash_ctx { 238 + const struct git_hash_algo *algop; 239 + union { 240 + git_SHA_CTX sha1; 241 + git_SHA_CTX_unsafe sha1_unsafe; 242 + git_SHA256_CTX sha256; 243 + } state; 244 }; 245 246 + typedef void (*git_hash_init_fn)(struct git_hash_ctx *ctx); 247 + typedef void (*git_hash_clone_fn)(struct git_hash_ctx *dst, const struct git_hash_ctx *src); 248 + typedef void (*git_hash_update_fn)(struct git_hash_ctx *ctx, const void *in, size_t len); 249 + typedef void (*git_hash_final_fn)(unsigned char *hash, struct git_hash_ctx *ctx); 250 + typedef void (*git_hash_final_oid_fn)(struct object_id *oid, struct git_hash_ctx *ctx); 251 252 struct git_hash_algo { 253 /* ··· 296 const struct git_hash_algo *unsafe; 297 }; 298 extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS]; 299 + 300 + static inline void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src) 301 + { 302 + src->algop->clone_fn(dst, src); 303 + } 304 + 305 + static inline void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len) 306 + { 307 + ctx->algop->update_fn(ctx, in, len); 308 + } 309 + 310 + static inline void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx) 311 + { 312 + ctx->algop->final_fn(hash, ctx); 313 + } 314 + 315 + static inline void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx) 316 + { 317 + ctx->algop->final_oid_fn(oid, ctx); 318 + } 319 320 /* 321 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
+3 -3
http-push.c
··· 760 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed) 761 { 762 struct remote_lock *lock = (struct remote_lock *)ctx->userData; 763 - git_hash_ctx hash_ctx; 764 unsigned char lock_token_hash[GIT_MAX_RAWSZ]; 765 766 if (tag_closed && ctx->cdata) { ··· 774 lock->token = xstrdup(ctx->cdata); 775 776 the_hash_algo->init_fn(&hash_ctx); 777 - the_hash_algo->update_fn(&hash_ctx, lock->token, strlen(lock->token)); 778 - the_hash_algo->final_fn(lock_token_hash, &hash_ctx); 779 780 lock->tmpfile_suffix[0] = '_'; 781 memcpy(lock->tmpfile_suffix + 1, hash_to_hex(lock_token_hash), the_hash_algo->hexsz);
··· 760 static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed) 761 { 762 struct remote_lock *lock = (struct remote_lock *)ctx->userData; 763 + struct git_hash_ctx hash_ctx; 764 unsigned char lock_token_hash[GIT_MAX_RAWSZ]; 765 766 if (tag_closed && ctx->cdata) { ··· 774 lock->token = xstrdup(ctx->cdata); 775 776 the_hash_algo->init_fn(&hash_ctx); 777 + git_hash_update(&hash_ctx, lock->token, strlen(lock->token)); 778 + git_hash_final(lock_token_hash, &hash_ctx); 779 780 lock->tmpfile_suffix[0] = '_'; 781 memcpy(lock->tmpfile_suffix + 1, hash_to_hex(lock_token_hash), the_hash_algo->hexsz);
+3 -3
http.c
··· 2597 freq->stream.next_out = expn; 2598 freq->stream.avail_out = sizeof(expn); 2599 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH); 2600 - the_hash_algo->update_fn(&freq->c, expn, 2601 - sizeof(expn) - freq->stream.avail_out); 2602 } while (freq->stream.avail_in && freq->zret == Z_OK); 2603 return nmemb; 2604 } ··· 2763 return -1; 2764 } 2765 2766 - the_hash_algo->final_oid_fn(&freq->real_oid, &freq->c); 2767 if (freq->zret != Z_STREAM_END) { 2768 unlink_or_warn(freq->tmpfile.buf); 2769 return -1;
··· 2597 freq->stream.next_out = expn; 2598 freq->stream.avail_out = sizeof(expn); 2599 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH); 2600 + git_hash_update(&freq->c, expn, 2601 + sizeof(expn) - freq->stream.avail_out); 2602 } while (freq->stream.avail_in && freq->zret == Z_OK); 2603 return nmemb; 2604 } ··· 2763 return -1; 2764 } 2765 2766 + git_hash_final_oid(&freq->real_oid, &freq->c); 2767 if (freq->zret != Z_STREAM_END) { 2768 unlink_or_warn(freq->tmpfile.buf); 2769 return -1;
+1 -1
http.h
··· 228 long http_code; 229 struct object_id oid; 230 struct object_id real_oid; 231 - git_hash_ctx c; 232 git_zstream stream; 233 int zret; 234 int rename;
··· 228 long http_code; 229 struct object_id oid; 230 struct object_id real_oid; 231 + struct git_hash_ctx c; 232 git_zstream stream; 233 int zret; 234 int rename;
+67 -63
object-file.c
··· 86 .algo = GIT_HASH_SHA256, 87 }; 88 89 - static void git_hash_sha1_init(git_hash_ctx *ctx) 90 { 91 - git_SHA1_Init(&ctx->sha1); 92 } 93 94 - static void git_hash_sha1_clone(git_hash_ctx *dst, const git_hash_ctx *src) 95 { 96 - git_SHA1_Clone(&dst->sha1, &src->sha1); 97 } 98 99 - static void git_hash_sha1_update(git_hash_ctx *ctx, const void *data, size_t len) 100 { 101 - git_SHA1_Update(&ctx->sha1, data, len); 102 } 103 104 - static void git_hash_sha1_final(unsigned char *hash, git_hash_ctx *ctx) 105 { 106 - git_SHA1_Final(hash, &ctx->sha1); 107 } 108 109 - static void git_hash_sha1_final_oid(struct object_id *oid, git_hash_ctx *ctx) 110 { 111 - git_SHA1_Final(oid->hash, &ctx->sha1); 112 memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ); 113 oid->algo = GIT_HASH_SHA1; 114 } 115 116 - static void git_hash_sha1_init_unsafe(git_hash_ctx *ctx) 117 { 118 - git_SHA1_Init_unsafe(&ctx->sha1_unsafe); 119 } 120 121 - static void git_hash_sha1_clone_unsafe(git_hash_ctx *dst, const git_hash_ctx *src) 122 { 123 - git_SHA1_Clone_unsafe(&dst->sha1_unsafe, &src->sha1_unsafe); 124 } 125 126 - static void git_hash_sha1_update_unsafe(git_hash_ctx *ctx, const void *data, 127 size_t len) 128 { 129 - git_SHA1_Update_unsafe(&ctx->sha1_unsafe, data, len); 130 } 131 132 - static void git_hash_sha1_final_unsafe(unsigned char *hash, git_hash_ctx *ctx) 133 { 134 - git_SHA1_Final_unsafe(hash, &ctx->sha1_unsafe); 135 } 136 137 - static void git_hash_sha1_final_oid_unsafe(struct object_id *oid, git_hash_ctx *ctx) 138 { 139 - git_SHA1_Final_unsafe(oid->hash, &ctx->sha1_unsafe); 140 memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ); 141 oid->algo = GIT_HASH_SHA1; 142 } 143 144 - static void git_hash_sha256_init(git_hash_ctx *ctx) 145 { 146 - git_SHA256_Init(&ctx->sha256); 147 } 148 149 - static void git_hash_sha256_clone(git_hash_ctx *dst, const git_hash_ctx *src) 150 { 151 - git_SHA256_Clone(&dst->sha256, &src->sha256); 152 } 153 154 - static void git_hash_sha256_update(git_hash_ctx *ctx, const void *data, size_t len) 155 { 156 - git_SHA256_Update(&ctx->sha256, data, len); 157 } 158 159 - static void git_hash_sha256_final(unsigned char *hash, git_hash_ctx *ctx) 160 { 161 - git_SHA256_Final(hash, &ctx->sha256); 162 } 163 164 - static void git_hash_sha256_final_oid(struct object_id *oid, git_hash_ctx *ctx) 165 { 166 - git_SHA256_Final(oid->hash, &ctx->sha256); 167 /* 168 * This currently does nothing, so the compiler should optimize it out, 169 * but keep it in case we extend the hash size again. ··· 172 oid->algo = GIT_HASH_SHA256; 173 } 174 175 - static void git_hash_unknown_init(git_hash_ctx *ctx UNUSED) 176 { 177 BUG("trying to init unknown hash"); 178 } 179 180 - static void git_hash_unknown_clone(git_hash_ctx *dst UNUSED, 181 - const git_hash_ctx *src UNUSED) 182 { 183 BUG("trying to clone unknown hash"); 184 } 185 186 - static void git_hash_unknown_update(git_hash_ctx *ctx UNUSED, 187 const void *data UNUSED, 188 size_t len UNUSED) 189 { ··· 191 } 192 193 static void git_hash_unknown_final(unsigned char *hash UNUSED, 194 - git_hash_ctx *ctx UNUSED) 195 { 196 BUG("trying to finalize unknown hash"); 197 } 198 199 static void git_hash_unknown_final_oid(struct object_id *oid UNUSED, 200 - git_hash_ctx *ctx UNUSED) 201 { 202 BUG("trying to finalize unknown hash"); 203 } ··· 1180 unsigned long size; 1181 enum object_type obj_type; 1182 struct git_istream *st; 1183 - git_hash_ctx c; 1184 char hdr[MAX_HEADER_LEN]; 1185 int hdrlen; 1186 ··· 1193 1194 /* Sha1.. */ 1195 r->hash_algo->init_fn(&c); 1196 - r->hash_algo->update_fn(&c, hdr, hdrlen); 1197 for (;;) { 1198 char buf[1024 * 16]; 1199 ssize_t readlen = read_istream(st, buf, sizeof(buf)); ··· 1204 } 1205 if (!readlen) 1206 break; 1207 - r->hash_algo->update_fn(&c, buf, readlen); 1208 } 1209 - r->hash_algo->final_oid_fn(&real_oid, &c); 1210 close_istream(st); 1211 return !oideq(oid, &real_oid) ? -1 : 0; 1212 } ··· 1945 } 1946 } 1947 1948 - static void hash_object_body(const struct git_hash_algo *algo, git_hash_ctx *c, 1949 const void *buf, unsigned long len, 1950 struct object_id *oid, 1951 char *hdr, int *hdrlen) 1952 { 1953 algo->init_fn(c); 1954 - algo->update_fn(c, hdr, *hdrlen); 1955 - algo->update_fn(c, buf, len); 1956 - algo->final_oid_fn(oid, c); 1957 } 1958 1959 static void write_object_file_prepare(const struct git_hash_algo *algo, ··· 1961 enum object_type type, struct object_id *oid, 1962 char *hdr, int *hdrlen) 1963 { 1964 - git_hash_ctx c; 1965 1966 /* Generate the header */ 1967 *hdrlen = format_object_header(hdr, *hdrlen, type, len); ··· 1975 const char *type, struct object_id *oid, 1976 char *hdr, int *hdrlen) 1977 { 1978 - git_hash_ctx c; 1979 1980 *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); 1981 hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); ··· 2206 const char *filename, unsigned flags, 2207 git_zstream *stream, 2208 unsigned char *buf, size_t buflen, 2209 - git_hash_ctx *c, git_hash_ctx *compat_c, 2210 char *hdr, int hdrlen) 2211 { 2212 struct repository *repo = the_repository; ··· 2240 stream->avail_in = hdrlen; 2241 while (git_deflate(stream, 0) == Z_OK) 2242 ; /* nothing */ 2243 - algo->update_fn(c, hdr, hdrlen); 2244 if (compat && compat_c) 2245 - compat->update_fn(compat_c, hdr, hdrlen); 2246 2247 return fd; 2248 } ··· 2251 * Common steps for the inner git_deflate() loop for writing loose 2252 * objects. Returns what git_deflate() returns. 2253 */ 2254 - static int write_loose_object_common(git_hash_ctx *c, git_hash_ctx *compat_c, 2255 git_zstream *stream, const int flush, 2256 unsigned char *in0, const int fd, 2257 unsigned char *compressed, 2258 const size_t compressed_len) 2259 { 2260 struct repository *repo = the_repository; 2261 - const struct git_hash_algo *algo = repo->hash_algo; 2262 const struct git_hash_algo *compat = repo->compat_hash_algo; 2263 int ret; 2264 2265 ret = git_deflate(stream, flush ? Z_FINISH : 0); 2266 - algo->update_fn(c, in0, stream->next_in - in0); 2267 if (compat && compat_c) 2268 - compat->update_fn(compat_c, in0, stream->next_in - in0); 2269 if (write_in_full(fd, compressed, stream->next_out - compressed) < 0) 2270 die_errno(_("unable to write loose object file")); 2271 stream->next_out = compressed; ··· 2280 * - End the compression of zlib stream. 2281 * - Get the calculated oid to "oid". 2282 */ 2283 - static int end_loose_object_common(git_hash_ctx *c, git_hash_ctx *compat_c, 2284 git_zstream *stream, struct object_id *oid, 2285 struct object_id *compat_oid) 2286 { 2287 struct repository *repo = the_repository; 2288 - const struct git_hash_algo *algo = repo->hash_algo; 2289 const struct git_hash_algo *compat = repo->compat_hash_algo; 2290 int ret; 2291 2292 ret = git_deflate_end_gently(stream); 2293 if (ret != Z_OK) 2294 return ret; 2295 - algo->final_oid_fn(oid, c); 2296 if (compat && compat_c) 2297 - compat->final_oid_fn(compat_oid, compat_c); 2298 2299 return Z_OK; 2300 } ··· 2306 int fd, ret; 2307 unsigned char compressed[4096]; 2308 git_zstream stream; 2309 - git_hash_ctx c; 2310 struct object_id parano_oid; 2311 static struct strbuf tmp_file = STRBUF_INIT; 2312 static struct strbuf filename = STRBUF_INIT; ··· 2386 int fd, ret, err = 0, flush = 0; 2387 unsigned char compressed[4096]; 2388 git_zstream stream; 2389 - git_hash_ctx c, compat_c; 2390 struct strbuf tmp_file = STRBUF_INIT; 2391 struct strbuf filename = STRBUF_INIT; 2392 int dirlen; ··· 3046 const char *path, 3047 const struct object_id *expected_oid) 3048 { 3049 - git_hash_ctx c; 3050 struct object_id real_oid; 3051 unsigned char buf[4096]; 3052 unsigned long total_read; 3053 int status = Z_OK; 3054 3055 the_hash_algo->init_fn(&c); 3056 - the_hash_algo->update_fn(&c, hdr, stream->total_out); 3057 3058 /* 3059 * We already read some bytes into hdr, but the ones up to the NUL ··· 3073 if (size - total_read < stream->avail_out) 3074 stream->avail_out = size - total_read; 3075 status = git_inflate(stream, Z_FINISH); 3076 - the_hash_algo->update_fn(&c, buf, stream->next_out - buf); 3077 total_read += stream->next_out - buf; 3078 } 3079 git_inflate_end(stream); ··· 3088 return -1; 3089 } 3090 3091 - the_hash_algo->final_oid_fn(&real_oid, &c); 3092 if (!oideq(expected_oid, &real_oid)) { 3093 error(_("hash mismatch for %s (expected %s)"), path, 3094 oid_to_hex(expected_oid));
··· 86 .algo = GIT_HASH_SHA256, 87 }; 88 89 + static void git_hash_sha1_init(struct git_hash_ctx *ctx) 90 { 91 + ctx->algop = &hash_algos[GIT_HASH_SHA1]; 92 + git_SHA1_Init(&ctx->state.sha1); 93 } 94 95 + static void git_hash_sha1_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src) 96 { 97 + dst->algop = src->algop; 98 + git_SHA1_Clone(&dst->state.sha1, &src->state.sha1); 99 } 100 101 + static void git_hash_sha1_update(struct git_hash_ctx *ctx, const void *data, size_t len) 102 { 103 + git_SHA1_Update(&ctx->state.sha1, data, len); 104 } 105 106 + static void git_hash_sha1_final(unsigned char *hash, struct git_hash_ctx *ctx) 107 { 108 + git_SHA1_Final(hash, &ctx->state.sha1); 109 } 110 111 + static void git_hash_sha1_final_oid(struct object_id *oid, struct git_hash_ctx *ctx) 112 { 113 + git_SHA1_Final(oid->hash, &ctx->state.sha1); 114 memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ); 115 oid->algo = GIT_HASH_SHA1; 116 } 117 118 + static void git_hash_sha1_init_unsafe(struct git_hash_ctx *ctx) 119 { 120 + ctx->algop = unsafe_hash_algo(&hash_algos[GIT_HASH_SHA1]); 121 + git_SHA1_Init_unsafe(&ctx->state.sha1_unsafe); 122 } 123 124 + static void git_hash_sha1_clone_unsafe(struct git_hash_ctx *dst, const struct git_hash_ctx *src) 125 { 126 + dst->algop = src->algop; 127 + git_SHA1_Clone_unsafe(&dst->state.sha1_unsafe, &src->state.sha1_unsafe); 128 } 129 130 + static void git_hash_sha1_update_unsafe(struct git_hash_ctx *ctx, const void *data, 131 size_t len) 132 { 133 + git_SHA1_Update_unsafe(&ctx->state.sha1_unsafe, data, len); 134 } 135 136 + static void git_hash_sha1_final_unsafe(unsigned char *hash, struct git_hash_ctx *ctx) 137 { 138 + git_SHA1_Final_unsafe(hash, &ctx->state.sha1_unsafe); 139 } 140 141 + static void git_hash_sha1_final_oid_unsafe(struct object_id *oid, struct git_hash_ctx *ctx) 142 { 143 + git_SHA1_Final_unsafe(oid->hash, &ctx->state.sha1_unsafe); 144 memset(oid->hash + GIT_SHA1_RAWSZ, 0, GIT_MAX_RAWSZ - GIT_SHA1_RAWSZ); 145 oid->algo = GIT_HASH_SHA1; 146 } 147 148 + static void git_hash_sha256_init(struct git_hash_ctx *ctx) 149 { 150 + ctx->algop = unsafe_hash_algo(&hash_algos[GIT_HASH_SHA256]); 151 + git_SHA256_Init(&ctx->state.sha256); 152 } 153 154 + static void git_hash_sha256_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src) 155 { 156 + dst->algop = src->algop; 157 + git_SHA256_Clone(&dst->state.sha256, &src->state.sha256); 158 } 159 160 + static void git_hash_sha256_update(struct git_hash_ctx *ctx, const void *data, size_t len) 161 { 162 + git_SHA256_Update(&ctx->state.sha256, data, len); 163 } 164 165 + static void git_hash_sha256_final(unsigned char *hash, struct git_hash_ctx *ctx) 166 { 167 + git_SHA256_Final(hash, &ctx->state.sha256); 168 } 169 170 + static void git_hash_sha256_final_oid(struct object_id *oid, struct git_hash_ctx *ctx) 171 { 172 + git_SHA256_Final(oid->hash, &ctx->state.sha256); 173 /* 174 * This currently does nothing, so the compiler should optimize it out, 175 * but keep it in case we extend the hash size again. ··· 178 oid->algo = GIT_HASH_SHA256; 179 } 180 181 + static void git_hash_unknown_init(struct git_hash_ctx *ctx UNUSED) 182 { 183 BUG("trying to init unknown hash"); 184 } 185 186 + static void git_hash_unknown_clone(struct git_hash_ctx *dst UNUSED, 187 + const struct git_hash_ctx *src UNUSED) 188 { 189 BUG("trying to clone unknown hash"); 190 } 191 192 + static void git_hash_unknown_update(struct git_hash_ctx *ctx UNUSED, 193 const void *data UNUSED, 194 size_t len UNUSED) 195 { ··· 197 } 198 199 static void git_hash_unknown_final(unsigned char *hash UNUSED, 200 + struct git_hash_ctx *ctx UNUSED) 201 { 202 BUG("trying to finalize unknown hash"); 203 } 204 205 static void git_hash_unknown_final_oid(struct object_id *oid UNUSED, 206 + struct git_hash_ctx *ctx UNUSED) 207 { 208 BUG("trying to finalize unknown hash"); 209 } ··· 1186 unsigned long size; 1187 enum object_type obj_type; 1188 struct git_istream *st; 1189 + struct git_hash_ctx c; 1190 char hdr[MAX_HEADER_LEN]; 1191 int hdrlen; 1192 ··· 1199 1200 /* Sha1.. */ 1201 r->hash_algo->init_fn(&c); 1202 + git_hash_update(&c, hdr, hdrlen); 1203 for (;;) { 1204 char buf[1024 * 16]; 1205 ssize_t readlen = read_istream(st, buf, sizeof(buf)); ··· 1210 } 1211 if (!readlen) 1212 break; 1213 + git_hash_update(&c, buf, readlen); 1214 } 1215 + git_hash_final_oid(&real_oid, &c); 1216 close_istream(st); 1217 return !oideq(oid, &real_oid) ? -1 : 0; 1218 } ··· 1951 } 1952 } 1953 1954 + static void hash_object_body(const struct git_hash_algo *algo, struct git_hash_ctx *c, 1955 const void *buf, unsigned long len, 1956 struct object_id *oid, 1957 char *hdr, int *hdrlen) 1958 { 1959 algo->init_fn(c); 1960 + git_hash_update(c, hdr, *hdrlen); 1961 + git_hash_update(c, buf, len); 1962 + git_hash_final_oid(oid, c); 1963 } 1964 1965 static void write_object_file_prepare(const struct git_hash_algo *algo, ··· 1967 enum object_type type, struct object_id *oid, 1968 char *hdr, int *hdrlen) 1969 { 1970 + struct git_hash_ctx c; 1971 1972 /* Generate the header */ 1973 *hdrlen = format_object_header(hdr, *hdrlen, type, len); ··· 1981 const char *type, struct object_id *oid, 1982 char *hdr, int *hdrlen) 1983 { 1984 + struct git_hash_ctx c; 1985 1986 *hdrlen = format_object_header_literally(hdr, *hdrlen, type, len); 1987 hash_object_body(algo, &c, buf, len, oid, hdr, hdrlen); ··· 2212 const char *filename, unsigned flags, 2213 git_zstream *stream, 2214 unsigned char *buf, size_t buflen, 2215 + struct git_hash_ctx *c, struct git_hash_ctx *compat_c, 2216 char *hdr, int hdrlen) 2217 { 2218 struct repository *repo = the_repository; ··· 2246 stream->avail_in = hdrlen; 2247 while (git_deflate(stream, 0) == Z_OK) 2248 ; /* nothing */ 2249 + git_hash_update(c, hdr, hdrlen); 2250 if (compat && compat_c) 2251 + git_hash_update(compat_c, hdr, hdrlen); 2252 2253 return fd; 2254 } ··· 2257 * Common steps for the inner git_deflate() loop for writing loose 2258 * objects. Returns what git_deflate() returns. 2259 */ 2260 + static int write_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c, 2261 git_zstream *stream, const int flush, 2262 unsigned char *in0, const int fd, 2263 unsigned char *compressed, 2264 const size_t compressed_len) 2265 { 2266 struct repository *repo = the_repository; 2267 const struct git_hash_algo *compat = repo->compat_hash_algo; 2268 int ret; 2269 2270 ret = git_deflate(stream, flush ? Z_FINISH : 0); 2271 + git_hash_update(c, in0, stream->next_in - in0); 2272 if (compat && compat_c) 2273 + git_hash_update(compat_c, in0, stream->next_in - in0); 2274 if (write_in_full(fd, compressed, stream->next_out - compressed) < 0) 2275 die_errno(_("unable to write loose object file")); 2276 stream->next_out = compressed; ··· 2285 * - End the compression of zlib stream. 2286 * - Get the calculated oid to "oid". 2287 */ 2288 + static int end_loose_object_common(struct git_hash_ctx *c, struct git_hash_ctx *compat_c, 2289 git_zstream *stream, struct object_id *oid, 2290 struct object_id *compat_oid) 2291 { 2292 struct repository *repo = the_repository; 2293 const struct git_hash_algo *compat = repo->compat_hash_algo; 2294 int ret; 2295 2296 ret = git_deflate_end_gently(stream); 2297 if (ret != Z_OK) 2298 return ret; 2299 + git_hash_final_oid(oid, c); 2300 if (compat && compat_c) 2301 + git_hash_final_oid(compat_oid, compat_c); 2302 2303 return Z_OK; 2304 } ··· 2310 int fd, ret; 2311 unsigned char compressed[4096]; 2312 git_zstream stream; 2313 + struct git_hash_ctx c; 2314 struct object_id parano_oid; 2315 static struct strbuf tmp_file = STRBUF_INIT; 2316 static struct strbuf filename = STRBUF_INIT; ··· 2390 int fd, ret, err = 0, flush = 0; 2391 unsigned char compressed[4096]; 2392 git_zstream stream; 2393 + struct git_hash_ctx c, compat_c; 2394 struct strbuf tmp_file = STRBUF_INIT; 2395 struct strbuf filename = STRBUF_INIT; 2396 int dirlen; ··· 3050 const char *path, 3051 const struct object_id *expected_oid) 3052 { 3053 + struct git_hash_ctx c; 3054 struct object_id real_oid; 3055 unsigned char buf[4096]; 3056 unsigned long total_read; 3057 int status = Z_OK; 3058 3059 the_hash_algo->init_fn(&c); 3060 + git_hash_update(&c, hdr, stream->total_out); 3061 3062 /* 3063 * We already read some bytes into hdr, but the ones up to the NUL ··· 3077 if (size - total_read < stream->avail_out) 3078 stream->avail_out = size - total_read; 3079 status = git_inflate(stream, Z_FINISH); 3080 + git_hash_update(&c, buf, stream->next_out - buf); 3081 total_read += stream->next_out - buf; 3082 } 3083 git_inflate_end(stream); ··· 3092 return -1; 3093 } 3094 3095 + git_hash_final_oid(&real_oid, &c); 3096 if (!oideq(expected_oid, &real_oid)) { 3097 error(_("hash mismatch for %s (expected %s)"), path, 3098 oid_to_hex(expected_oid));
+3 -3
pack-check.c
··· 58 { 59 off_t index_size = p->index_size; 60 const unsigned char *index_base = p->index_data; 61 - git_hash_ctx ctx; 62 unsigned char hash[GIT_MAX_RAWSZ], *pack_sig; 63 off_t offset = 0, pack_sig_ofs = 0; 64 uint32_t nr_objects, i; ··· 77 pack_sig_ofs = p->pack_size - r->hash_algo->rawsz; 78 if (offset > pack_sig_ofs) 79 remaining -= (unsigned int)(offset - pack_sig_ofs); 80 - r->hash_algo->update_fn(&ctx, in, remaining); 81 } while (offset < pack_sig_ofs); 82 - r->hash_algo->final_fn(hash, &ctx); 83 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL); 84 if (!hasheq(hash, pack_sig, the_repository->hash_algo)) 85 err = error("%s pack checksum mismatch",
··· 58 { 59 off_t index_size = p->index_size; 60 const unsigned char *index_base = p->index_data; 61 + struct git_hash_ctx ctx; 62 unsigned char hash[GIT_MAX_RAWSZ], *pack_sig; 63 off_t offset = 0, pack_sig_ofs = 0; 64 uint32_t nr_objects, i; ··· 77 pack_sig_ofs = p->pack_size - r->hash_algo->rawsz; 78 if (offset > pack_sig_ofs) 79 remaining -= (unsigned int)(offset - pack_sig_ofs); 80 + git_hash_update(&ctx, in, remaining); 81 } while (offset < pack_sig_ofs); 82 + git_hash_final(hash, &ctx); 83 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL); 84 if (!hasheq(hash, pack_sig, the_repository->hash_algo)) 85 err = error("%s pack checksum mismatch",
+10 -9
pack-write.c
··· 395 off_t partial_pack_offset) 396 { 397 int aligned_sz, buf_sz = 8 * 1024; 398 - git_hash_ctx old_hash_ctx, new_hash_ctx; 399 struct pack_header hdr; 400 char *buf; 401 ssize_t read_result; ··· 413 pack_name); 414 if (lseek(pack_fd, 0, SEEK_SET) != 0) 415 die_errno("Failed seeking to start of '%s'", pack_name); 416 - hash_algo->update_fn(&old_hash_ctx, &hdr, sizeof(hdr)); 417 hdr.hdr_entries = htonl(object_count); 418 - hash_algo->update_fn(&new_hash_ctx, &hdr, sizeof(hdr)); 419 write_or_die(pack_fd, &hdr, sizeof(hdr)); 420 partial_pack_offset -= sizeof(hdr); 421 ··· 430 break; 431 if (n < 0) 432 die_errno("Failed to checksum '%s'", pack_name); 433 - hash_algo->update_fn(&new_hash_ctx, buf, n); 434 435 aligned_sz -= n; 436 if (!aligned_sz) ··· 439 if (!partial_pack_hash) 440 continue; 441 442 - hash_algo->update_fn(&old_hash_ctx, buf, n); 443 partial_pack_offset -= n; 444 if (partial_pack_offset == 0) { 445 unsigned char hash[GIT_MAX_RAWSZ]; 446 - hash_algo->final_fn(hash, &old_hash_ctx); 447 - if (!hasheq(hash, partial_pack_hash, hash_algo)) 448 die("Unexpected checksum for %s " 449 "(disk corruption?)", pack_name); 450 /* ··· 460 free(buf); 461 462 if (partial_pack_hash) 463 - hash_algo->final_fn(partial_pack_hash, &old_hash_ctx); 464 - hash_algo->final_fn(new_pack_hash, &new_hash_ctx); 465 write_or_die(pack_fd, new_pack_hash, hash_algo->rawsz); 466 fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name); 467 }
··· 395 off_t partial_pack_offset) 396 { 397 int aligned_sz, buf_sz = 8 * 1024; 398 + struct git_hash_ctx old_hash_ctx, new_hash_ctx; 399 struct pack_header hdr; 400 char *buf; 401 ssize_t read_result; ··· 413 pack_name); 414 if (lseek(pack_fd, 0, SEEK_SET) != 0) 415 die_errno("Failed seeking to start of '%s'", pack_name); 416 + git_hash_update(&old_hash_ctx, &hdr, sizeof(hdr)); 417 hdr.hdr_entries = htonl(object_count); 418 + git_hash_update(&new_hash_ctx, &hdr, sizeof(hdr)); 419 write_or_die(pack_fd, &hdr, sizeof(hdr)); 420 partial_pack_offset -= sizeof(hdr); 421 ··· 430 break; 431 if (n < 0) 432 die_errno("Failed to checksum '%s'", pack_name); 433 + git_hash_update(&new_hash_ctx, buf, n); 434 435 aligned_sz -= n; 436 if (!aligned_sz) ··· 439 if (!partial_pack_hash) 440 continue; 441 442 + git_hash_update(&old_hash_ctx, buf, n); 443 partial_pack_offset -= n; 444 if (partial_pack_offset == 0) { 445 unsigned char hash[GIT_MAX_RAWSZ]; 446 + git_hash_final(hash, &old_hash_ctx); 447 + if (!hasheq(hash, partial_pack_hash, 448 + hash_algo)) 449 die("Unexpected checksum for %s " 450 "(disk corruption?)", pack_name); 451 /* ··· 461 free(buf); 462 463 if (partial_pack_hash) 464 + git_hash_final(partial_pack_hash, &old_hash_ctx); 465 + git_hash_final(new_pack_hash, &new_hash_ctx); 466 write_or_die(pack_fd, new_pack_hash, hash_algo->rawsz); 467 fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name); 468 }
+13 -13
read-cache.c
··· 1717 1718 static int verify_hdr(const struct cache_header *hdr, unsigned long size) 1719 { 1720 - git_hash_ctx c; 1721 unsigned char hash[GIT_MAX_RAWSZ]; 1722 int hdr_version; 1723 unsigned char *start, *end; ··· 1739 return 0; 1740 1741 the_hash_algo->init_fn(&c); 1742 - the_hash_algo->update_fn(&c, hdr, size - the_hash_algo->rawsz); 1743 - the_hash_algo->final_fn(hash, &c); 1744 if (!hasheq(hash, start, the_repository->hash_algo)) 1745 return error(_("bad index file sha1 signature")); 1746 return 0; ··· 2002 static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot); 2003 2004 static size_t read_eoie_extension(const char *mmap, size_t mmap_size); 2005 - static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset); 2006 2007 struct load_index_extensions 2008 { ··· 2566 } 2567 2568 static int write_index_ext_header(struct hashfile *f, 2569 - git_hash_ctx *eoie_f, 2570 unsigned int ext, 2571 unsigned int sz) 2572 { ··· 2576 if (eoie_f) { 2577 ext = htonl(ext); 2578 sz = htonl(sz); 2579 - the_hash_algo->update_fn(eoie_f, &ext, sizeof(ext)); 2580 - the_hash_algo->update_fn(eoie_f, &sz, sizeof(sz)); 2581 } 2582 return 0; 2583 } ··· 2831 { 2832 uint64_t start = getnanotime(); 2833 struct hashfile *f; 2834 - git_hash_ctx *eoie_c = NULL; 2835 struct cache_header hdr; 2836 int i, err = 0, removed, extended, hdr_version; 2837 struct cache_entry **cache = istate->cache; ··· 3579 uint32_t extsize; 3580 size_t offset, src_offset; 3581 unsigned char hash[GIT_MAX_RAWSZ]; 3582 - git_hash_ctx c; 3583 3584 /* ensure we have an index big enough to contain an EOIE extension */ 3585 if (mmap_size < sizeof(struct cache_header) + EOIE_SIZE_WITH_HEADER + the_hash_algo->rawsz) ··· 3634 if (src_offset + 8 + extsize < src_offset) 3635 return 0; 3636 3637 - the_hash_algo->update_fn(&c, mmap + src_offset, 8); 3638 3639 src_offset += 8; 3640 src_offset += extsize; 3641 } 3642 - the_hash_algo->final_fn(hash, &c); 3643 if (!hasheq(hash, (const unsigned char *)index, the_repository->hash_algo)) 3644 return 0; 3645 ··· 3650 return offset; 3651 } 3652 3653 - static void write_eoie_extension(struct strbuf *sb, git_hash_ctx *eoie_context, size_t offset) 3654 { 3655 uint32_t buffer; 3656 unsigned char hash[GIT_MAX_RAWSZ]; ··· 3660 strbuf_add(sb, &buffer, sizeof(uint32_t)); 3661 3662 /* hash */ 3663 - the_hash_algo->final_fn(hash, eoie_context); 3664 strbuf_add(sb, hash, the_hash_algo->rawsz); 3665 } 3666
··· 1717 1718 static int verify_hdr(const struct cache_header *hdr, unsigned long size) 1719 { 1720 + struct git_hash_ctx c; 1721 unsigned char hash[GIT_MAX_RAWSZ]; 1722 int hdr_version; 1723 unsigned char *start, *end; ··· 1739 return 0; 1740 1741 the_hash_algo->init_fn(&c); 1742 + git_hash_update(&c, hdr, size - the_hash_algo->rawsz); 1743 + git_hash_final(hash, &c); 1744 if (!hasheq(hash, start, the_repository->hash_algo)) 1745 return error(_("bad index file sha1 signature")); 1746 return 0; ··· 2002 static void write_ieot_extension(struct strbuf *sb, struct index_entry_offset_table *ieot); 2003 2004 static size_t read_eoie_extension(const char *mmap, size_t mmap_size); 2005 + static void write_eoie_extension(struct strbuf *sb, struct git_hash_ctx *eoie_context, size_t offset); 2006 2007 struct load_index_extensions 2008 { ··· 2566 } 2567 2568 static int write_index_ext_header(struct hashfile *f, 2569 + struct git_hash_ctx *eoie_f, 2570 unsigned int ext, 2571 unsigned int sz) 2572 { ··· 2576 if (eoie_f) { 2577 ext = htonl(ext); 2578 sz = htonl(sz); 2579 + git_hash_update(eoie_f, &ext, sizeof(ext)); 2580 + git_hash_update(eoie_f, &sz, sizeof(sz)); 2581 } 2582 return 0; 2583 } ··· 2831 { 2832 uint64_t start = getnanotime(); 2833 struct hashfile *f; 2834 + struct git_hash_ctx *eoie_c = NULL; 2835 struct cache_header hdr; 2836 int i, err = 0, removed, extended, hdr_version; 2837 struct cache_entry **cache = istate->cache; ··· 3579 uint32_t extsize; 3580 size_t offset, src_offset; 3581 unsigned char hash[GIT_MAX_RAWSZ]; 3582 + struct git_hash_ctx c; 3583 3584 /* ensure we have an index big enough to contain an EOIE extension */ 3585 if (mmap_size < sizeof(struct cache_header) + EOIE_SIZE_WITH_HEADER + the_hash_algo->rawsz) ··· 3634 if (src_offset + 8 + extsize < src_offset) 3635 return 0; 3636 3637 + git_hash_update(&c, mmap + src_offset, 8); 3638 3639 src_offset += 8; 3640 src_offset += extsize; 3641 } 3642 + git_hash_final(hash, &c); 3643 if (!hasheq(hash, (const unsigned char *)index, the_repository->hash_algo)) 3644 return 0; 3645 ··· 3650 return offset; 3651 } 3652 3653 + static void write_eoie_extension(struct strbuf *sb, struct git_hash_ctx *eoie_context, size_t offset) 3654 { 3655 uint32_t buffer; 3656 unsigned char hash[GIT_MAX_RAWSZ]; ··· 3660 strbuf_add(sb, &buffer, sizeof(uint32_t)); 3661 3662 /* hash */ 3663 + git_hash_final(hash, eoie_context); 3664 strbuf_add(sb, hash, the_hash_algo->rawsz); 3665 } 3666
+9 -9
rerere.c
··· 358 } 359 360 static int handle_conflict(struct strbuf *out, struct rerere_io *io, 361 - int marker_size, git_hash_ctx *ctx) 362 { 363 enum { 364 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL ··· 396 strbuf_addbuf(out, &two); 397 rerere_strbuf_putconflict(out, '>', marker_size); 398 if (ctx) { 399 - the_hash_algo->update_fn(ctx, one.buf ? 400 - one.buf : "", 401 - one.len + 1); 402 - the_hash_algo->update_fn(ctx, two.buf ? 403 - two.buf : "", 404 - two.len + 1); 405 } 406 break; 407 } else if (hunk == RR_SIDE_1) ··· 432 */ 433 static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size) 434 { 435 - git_hash_ctx ctx; 436 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT; 437 int has_conflicts = 0; 438 if (hash) ··· 453 strbuf_release(&out); 454 455 if (hash) 456 - the_hash_algo->final_fn(hash, &ctx); 457 458 return has_conflicts; 459 }
··· 358 } 359 360 static int handle_conflict(struct strbuf *out, struct rerere_io *io, 361 + int marker_size, struct git_hash_ctx *ctx) 362 { 363 enum { 364 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL ··· 396 strbuf_addbuf(out, &two); 397 rerere_strbuf_putconflict(out, '>', marker_size); 398 if (ctx) { 399 + git_hash_update(ctx, one.buf ? 400 + one.buf : "", 401 + one.len + 1); 402 + git_hash_update(ctx, two.buf ? 403 + two.buf : "", 404 + two.len + 1); 405 } 406 break; 407 } else if (hunk == RR_SIDE_1) ··· 432 */ 433 static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size) 434 { 435 + struct git_hash_ctx ctx; 436 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT; 437 int has_conflicts = 0; 438 if (hash) ··· 453 strbuf_release(&out); 454 455 if (hash) 456 + git_hash_final(hash, &ctx); 457 458 return has_conflicts; 459 }
+4 -4
t/helper/test-hash-speed.c
··· 3 4 #define NUM_SECONDS 3 5 6 - static inline void compute_hash(const struct git_hash_algo *algo, git_hash_ctx *ctx, uint8_t *final, const void *p, size_t len) 7 { 8 algo->init_fn(ctx); 9 - algo->update_fn(ctx, p, len); 10 - algo->final_fn(final, ctx); 11 } 12 13 int cmd__hash_speed(int ac, const char **av) 14 { 15 - git_hash_ctx ctx; 16 unsigned char hash[GIT_MAX_RAWSZ]; 17 clock_t initial, start, end; 18 unsigned bufsizes[] = { 64, 256, 1024, 8192, 16384 };
··· 3 4 #define NUM_SECONDS 3 5 6 + static inline void compute_hash(const struct git_hash_algo *algo, struct git_hash_ctx *ctx, uint8_t *final, const void *p, size_t len) 7 { 8 algo->init_fn(ctx); 9 + git_hash_update(ctx, p, len); 10 + git_hash_final(final, ctx); 11 } 12 13 int cmd__hash_speed(int ac, const char **av) 14 { 15 + struct git_hash_ctx ctx; 16 unsigned char hash[GIT_MAX_RAWSZ]; 17 clock_t initial, start, end; 18 unsigned bufsizes[] = { 64, 256, 1024, 8192, 16384 };
+3 -3
t/helper/test-hash.c
··· 3 4 int cmd_hash_impl(int ac, const char **av, int algo, int unsafe) 5 { 6 - git_hash_ctx ctx; 7 unsigned char hash[GIT_MAX_HEXSZ]; 8 unsigned bufsz = 8192; 9 int binary = 0; ··· 48 } 49 if (this_sz == 0) 50 break; 51 - algop->update_fn(&ctx, buffer, this_sz); 52 } 53 - algop->final_fn(hash, &ctx); 54 55 if (binary) 56 fwrite(hash, 1, algop->rawsz, stdout);
··· 3 4 int cmd_hash_impl(int ac, const char **av, int algo, int unsafe) 5 { 6 + struct git_hash_ctx ctx; 7 unsigned char hash[GIT_MAX_HEXSZ]; 8 unsigned bufsz = 8192; 9 int binary = 0; ··· 48 } 49 if (this_sz == 0) 50 break; 51 + git_hash_update(&ctx, buffer, this_sz); 52 } 53 + git_hash_final(hash, &ctx); 54 55 if (binary) 56 fwrite(hash, 1, algop->rawsz, stdout);
+3 -3
t/unit-tests/u-hash.c
··· 8 cl_assert(data != NULL); 9 10 for (size_t i = 1; i < ARRAY_SIZE(hash_algos); i++) { 11 - git_hash_ctx ctx; 12 unsigned char hash[GIT_MAX_HEXSZ]; 13 const struct git_hash_algo *algop = &hash_algos[i]; 14 15 algop->init_fn(&ctx); 16 - algop->update_fn(&ctx, data, data_length); 17 - algop->final_fn(hash, &ctx); 18 19 cl_assert_equal_s(hash_to_hex_algop(hash,algop), expected_hashes[i - 1]); 20 }
··· 8 cl_assert(data != NULL); 9 10 for (size_t i = 1; i < ARRAY_SIZE(hash_algos); i++) { 11 + struct git_hash_ctx ctx; 12 unsigned char hash[GIT_MAX_HEXSZ]; 13 const struct git_hash_algo *algop = &hash_algos[i]; 14 15 algop->init_fn(&ctx); 16 + git_hash_update(&ctx, data, data_length); 17 + git_hash_final(hash, &ctx); 18 19 cl_assert_equal_s(hash_to_hex_algop(hash,algop), expected_hashes[i - 1]); 20 }
+3 -3
trace2/tr2_sid.c
··· 32 { 33 const struct git_hash_algo *algo = &hash_algos[GIT_HASH_SHA1]; 34 struct tr2_tbuf tb_now; 35 - git_hash_ctx ctx; 36 pid_t pid = getpid(); 37 unsigned char hash[GIT_MAX_RAWSZ + 1]; 38 char hex[GIT_MAX_HEXSZ + 1]; ··· 46 strbuf_add(&tr2sid_buf, "Localhost", 9); 47 else { 48 algo->init_fn(&ctx); 49 - algo->update_fn(&ctx, hostname, strlen(hostname)); 50 - algo->final_fn(hash, &ctx); 51 hash_to_hex_algop_r(hex, hash, algo); 52 strbuf_addch(&tr2sid_buf, 'H'); 53 strbuf_add(&tr2sid_buf, hex, 8);
··· 32 { 33 const struct git_hash_algo *algo = &hash_algos[GIT_HASH_SHA1]; 34 struct tr2_tbuf tb_now; 35 + struct git_hash_ctx ctx; 36 pid_t pid = getpid(); 37 unsigned char hash[GIT_MAX_RAWSZ + 1]; 38 char hex[GIT_MAX_HEXSZ + 1]; ··· 46 strbuf_add(&tr2sid_buf, "Localhost", 9); 47 else { 48 algo->init_fn(&ctx); 49 + git_hash_update(&ctx, hostname, strlen(hostname)); 50 + git_hash_final(hash, &ctx); 51 hash_to_hex_algop_r(hex, hash, algo); 52 strbuf_addch(&tr2sid_buf, 'H'); 53 strbuf_add(&tr2sid_buf, hex, 8);