Commit 3b52e5f55870b0eb2d1c6109256c18562fa3785e

Edward Thomson 2022-04-18T17:12:27

Merge pull request #6265 from libgit2/ethomson/sha256_two sha256: refactoring in preparation for sha256

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
diff --git a/fuzzers/packfile_fuzzer.c b/fuzzers/packfile_fuzzer.c
index 6002fa1..23ecaf8 100644
--- a/fuzzers/packfile_fuzzer.c
+++ b/fuzzers/packfile_fuzzer.c
@@ -94,7 +94,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 			fprintf(stderr, "Failed to compute the SHA1 hash\n");
 			abort();
 		}
-		if (git_indexer_append(indexer, &oid, sizeof(oid), &stats) < 0) {
+		if (git_indexer_append(indexer, &oid.id, GIT_OID_RAWSZ, &stats) < 0) {
 			goto cleanup;
 		}
 	}
diff --git a/src/libgit2/commit_graph.c b/src/libgit2/commit_graph.c
index 70e866b..10947ac 100644
--- a/src/libgit2/commit_graph.c
+++ b/src/libgit2/commit_graph.c
@@ -138,7 +138,7 @@ static int commit_graph_parse_oid_lookup(
 		struct git_commit_graph_chunk *chunk_oid_lookup)
 {
 	uint32_t i;
-	git_oid *oid, *prev_oid, zero_oid = {{0}};
+	unsigned char *oid, *prev_oid, zero_oid[GIT_OID_RAWSZ] = {0};
 
 	if (chunk_oid_lookup->offset == 0)
 		return commit_graph_error("missing OID Lookup chunk");
@@ -147,10 +147,10 @@ static int commit_graph_parse_oid_lookup(
 	if (chunk_oid_lookup->length != file->num_commits * GIT_OID_RAWSZ)
 		return commit_graph_error("OID Lookup chunk has wrong length");
 
-	file->oid_lookup = oid = (git_oid *)(data + chunk_oid_lookup->offset);
-	prev_oid = &zero_oid;
-	for (i = 0; i < file->num_commits; ++i, ++oid) {
-		if (git_oid_cmp(prev_oid, oid) >= 0)
+	file->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset);
+	prev_oid = zero_oid;
+	for (i = 0; i < file->num_commits; ++i, oid += GIT_OID_RAWSZ) {
+		if (git_oid_raw_cmp(prev_oid, oid) >= 0)
 			return commit_graph_error("OID Lookup index is non-monotonic");
 		prev_oid = oid;
 	}
@@ -437,7 +437,7 @@ static int git_commit_graph_entry_get_byindex(
 	}
 
 	commit_data = file->commit_data + pos * (GIT_OID_RAWSZ + 4 * sizeof(uint32_t));
-	git_oid_cpy(&e->tree_oid, (const git_oid *)commit_data);
+	git_oid_fromraw(&e->tree_oid, commit_data);
 	e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + GIT_OID_RAWSZ)));
 	e->parent_indices[1] = ntohl(
 			*((uint32_t *)(commit_data + GIT_OID_RAWSZ + sizeof(uint32_t))));
@@ -470,7 +470,8 @@ static int git_commit_graph_entry_get_byindex(
 			e->parent_count++;
 		}
 	}
-	git_oid_cpy(&e->sha1, &file->oid_lookup[pos]);
+
+	git_oid_fromraw(&e->sha1, &file->oid_lookup[pos * GIT_OID_RAWSZ]);
 	return 0;
 }
 
@@ -514,7 +515,7 @@ int git_commit_graph_entry_find(
 {
 	int pos, found = 0;
 	uint32_t hi, lo;
-	const git_oid *current = NULL;
+	const unsigned char *current = NULL;
 
 	GIT_ASSERT_ARG(e);
 	GIT_ASSERT_ARG(file);
@@ -528,26 +529,25 @@ int git_commit_graph_entry_find(
 	if (pos >= 0) {
 		/* An object matching exactly the oid was found */
 		found = 1;
-		current = file->oid_lookup + pos;
+		current = file->oid_lookup + (pos * GIT_OID_RAWSZ);
 	} else {
 		/* No object was found */
 		/* pos refers to the object with the "closest" oid to short_oid */
 		pos = -1 - pos;
 		if (pos < (int)file->num_commits) {
-			current = file->oid_lookup + pos;
+			current = file->oid_lookup + (pos * GIT_OID_RAWSZ);
 
-			if (!git_oid_ncmp(short_oid, current, len))
+			if (!git_oid_raw_ncmp(short_oid->id, current, len))
 				found = 1;
 		}
 	}
 
 	if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)file->num_commits) {
 		/* Check for ambiguousity */
-		const git_oid *next = current + 1;
+		const unsigned char *next = current + GIT_OID_RAWSZ;
 
-		if (!git_oid_ncmp(short_oid, next, len)) {
+		if (!git_oid_raw_ncmp(short_oid->id, next, len))
 			found = 2;
-		}
 	}
 
 	if (!found)
@@ -1019,7 +1019,9 @@ static int commit_graph_write(
 	/* Fill the OID Lookup table. */
 	git_vector_foreach (&w->commits, i, packed_commit) {
 		error = git_str_put(&oid_lookup,
-			(const char *)&packed_commit->sha1, sizeof(git_oid));
+			(const char *)&packed_commit->sha1.id,
+			GIT_OID_RAWSZ);
+
 		if (error < 0)
 			goto cleanup;
 	}
@@ -1034,8 +1036,9 @@ static int commit_graph_write(
 		unsigned int parentcount = (unsigned int)git_array_size(packed_commit->parents);
 
 		error = git_str_put(&commit_data,
-				(const char *)&packed_commit->tree_oid,
-				sizeof(git_oid));
+			(const char *)&packed_commit->tree_oid.id,
+			GIT_OID_RAWSZ);
+
 		if (error < 0)
 			goto cleanup;
 
diff --git a/src/libgit2/commit_graph.h b/src/libgit2/commit_graph.h
index 45e125b..b78ab81 100644
--- a/src/libgit2/commit_graph.h
+++ b/src/libgit2/commit_graph.h
@@ -36,7 +36,7 @@ typedef struct git_commit_graph_file {
 	uint32_t num_commits;
 
 	/* The OID Lookup table. */
-	git_oid *oid_lookup;
+	unsigned char *oid_lookup;
 
 	/*
 	 * The Commit Data table. Each entry contains the OID of the commit followed
diff --git a/src/libgit2/index.c b/src/libgit2/index.c
index aa97c64..f44c507 100644
--- a/src/libgit2/index.c
+++ b/src/libgit2/index.c
@@ -74,7 +74,7 @@ struct entry_short {
 	uint32_t uid;
 	uint32_t gid;
 	uint32_t file_size;
-	git_oid oid;
+	unsigned char oid[GIT_OID_RAWSZ];
 	uint16_t flags;
 	char path[1]; /* arbitrary length */
 };
@@ -88,7 +88,7 @@ struct entry_long {
 	uint32_t uid;
 	uint32_t gid;
 	uint32_t file_size;
-	git_oid oid;
+	unsigned char oid[GIT_OID_RAWSZ];
 	uint16_t flags;
 	uint16_t flags_extended;
 	char path[1]; /* arbitrary length */
@@ -2480,9 +2480,11 @@ static int read_entry(
 	entry.uid = ntohl(source.uid);
 	entry.gid = ntohl(source.gid);
 	entry.file_size = ntohl(source.file_size);
-	git_oid_cpy(&entry.id, &source.oid);
 	entry.flags = ntohs(source.flags);
 
+	if (git_oid_fromraw(&entry.id, source.oid) < 0)
+		return -1;
+
 	if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) {
 		uint16_t flags_raw;
 		size_t flags_offset;
@@ -2803,9 +2805,7 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
 	ondisk.uid = htonl(entry->uid);
 	ondisk.gid = htonl(entry->gid);
 	ondisk.file_size = htonl((uint32_t)entry->file_size);
-
-	git_oid_cpy(&ondisk.oid, &entry->id);
-
+	git_oid_raw_cpy(ondisk.oid, entry->id.id);
 	ondisk.flags = htons(entry->flags);
 
 	if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) {
diff --git a/src/libgit2/indexer.c b/src/libgit2/indexer.c
index f9a32e7..afb9ce8 100644
--- a/src/libgit2/indexer.c
+++ b/src/libgit2/indexer.c
@@ -385,7 +385,7 @@ static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj)
 			size_t i;
 
 			git_array_foreach(tree->entries, i, entry)
-				if (add_expected_oid(idx, entry->oid) < 0)
+				if (add_expected_oid(idx, &entry->oid) < 0)
 					goto out;
 
 			break;
@@ -1269,7 +1269,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
 
 	/* Write out the object names (SHA-1 hashes) */
 	git_vector_foreach(&idx->objects, i, entry) {
-		git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid));
+		git_filebuf_write(&index_file, &entry->oid.id, GIT_OID_RAWSZ);
 	}
 
 	/* Write out the CRC32 values */
diff --git a/src/libgit2/iterator.c b/src/libgit2/iterator.c
index 15bb63d..bc6f766 100644
--- a/src/libgit2/iterator.c
+++ b/src/libgit2/iterator.c
@@ -613,7 +613,7 @@ GIT_INLINE(int) tree_iterator_frame_push_neighbors(
 			break;
 
 		if ((error = git_tree_lookup(&tree,
-			iter->base.repo, entry->tree_entry->oid)) < 0)
+			iter->base.repo, &entry->tree_entry->oid)) < 0)
 			break;
 
 		if (git_vector_insert(&parent_frame->similar_trees, tree) < 0)
@@ -659,7 +659,7 @@ GIT_INLINE(int) tree_iterator_frame_push(
 	int error;
 
 	if ((error = git_tree_lookup(&tree,
-			iter->base.repo, entry->tree_entry->oid)) < 0 ||
+			iter->base.repo, &entry->tree_entry->oid)) < 0 ||
 		(error = tree_iterator_frame_init(iter, tree, entry)) < 0)
 		goto done;
 
@@ -740,7 +740,7 @@ static void tree_iterator_set_current(
 
 	iter->entry.mode = tree_entry->attr;
 	iter->entry.path = iter->entry_path.ptr;
-	git_oid_cpy(&iter->entry.id, tree_entry->oid);
+	git_oid_cpy(&iter->entry.id, &tree_entry->oid);
 }
 
 static int tree_iterator_advance(const git_index_entry **out, git_iterator *i)
diff --git a/src/libgit2/midx.c b/src/libgit2/midx.c
index 0092601..d4e53c4 100644
--- a/src/libgit2/midx.c
+++ b/src/libgit2/midx.c
@@ -115,7 +115,7 @@ static int midx_parse_oid_lookup(
 		struct git_midx_chunk *chunk_oid_lookup)
 {
 	uint32_t i;
-	git_oid *oid, *prev_oid, zero_oid = {{0}};
+	unsigned char *oid, *prev_oid, zero_oid[GIT_OID_RAWSZ] = {0};
 
 	if (chunk_oid_lookup->offset == 0)
 		return midx_error("missing OID Lookup chunk");
@@ -124,10 +124,10 @@ static int midx_parse_oid_lookup(
 	if (chunk_oid_lookup->length != idx->num_objects * GIT_OID_RAWSZ)
 		return midx_error("OID Lookup chunk has wrong length");
 
-	idx->oid_lookup = oid = (git_oid *)(data + chunk_oid_lookup->offset);
-	prev_oid = &zero_oid;
-	for (i = 0; i < idx->num_objects; ++i, ++oid) {
-		if (git_oid_cmp(prev_oid, oid) >= 0)
+	idx->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset);
+	prev_oid = zero_oid;
+	for (i = 0; i < idx->num_objects; ++i, oid += GIT_OID_RAWSZ) {
+		if (git_oid_raw_cmp(prev_oid, oid) >= 0)
 			return midx_error("OID Lookup index is non-monotonic");
 		prev_oid = oid;
 	}
@@ -389,7 +389,7 @@ int git_midx_entry_find(
 	int pos, found = 0;
 	size_t pack_index;
 	uint32_t hi, lo;
-	const git_oid *current = NULL;
+	unsigned char *current = NULL;
 	const unsigned char *object_offset;
 	off64_t offset;
 
@@ -403,26 +403,25 @@ int git_midx_entry_find(
 	if (pos >= 0) {
 		/* An object matching exactly the oid was found */
 		found = 1;
-		current = idx->oid_lookup + pos;
+		current = idx->oid_lookup + (pos * GIT_OID_RAWSZ);
 	} else {
 		/* No object was found */
 		/* pos refers to the object with the "closest" oid to short_oid */
 		pos = -1 - pos;
 		if (pos < (int)idx->num_objects) {
-			current = idx->oid_lookup + pos;
+			current = idx->oid_lookup + (pos * GIT_OID_RAWSZ);
 
-			if (!git_oid_ncmp(short_oid, current, len))
+			if (!git_oid_raw_ncmp(short_oid->id, current, len))
 				found = 1;
 		}
 	}
 
 	if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)idx->num_objects) {
 		/* Check for ambiguousity */
-		const git_oid *next = current + 1;
+		const unsigned char *next = current + GIT_OID_RAWSZ;
 
-		if (!git_oid_ncmp(short_oid, next, len)) {
+		if (!git_oid_raw_ncmp(short_oid->id, next, len))
 			found = 2;
-		}
 	}
 
 	if (!found)
@@ -450,7 +449,7 @@ int git_midx_entry_find(
 		return midx_error("invalid index into the packfile names table");
 	e->pack_index = pack_index;
 	e->offset = offset;
-	git_oid_cpy(&e->sha1, current);
+	git_oid_fromraw(&e->sha1, current);
 	return 0;
 }
 
@@ -459,13 +458,17 @@ int git_midx_foreach_entry(
 		git_odb_foreach_cb cb,
 		void *data)
 {
+	git_oid oid;
 	size_t i;
 	int error;
 
 	GIT_ASSERT_ARG(idx);
 
 	for (i = 0; i < idx->num_objects; ++i) {
-		if ((error = cb(&idx->oid_lookup[i], data)) != 0)
+		if ((error = git_oid_fromraw(&oid, &idx->oid_lookup[i * GIT_OID_RAWSZ])) < 0)
+			return error;
+
+		if ((error = cb(&oid, data)) != 0)
 			return git_error_set_after_callback(error);
 	}
 
@@ -751,7 +754,7 @@ static int midx_write(
 
 	/* Fill the OID Lookup table. */
 	git_vector_foreach (&object_entries, i, entry) {
-		error = git_str_put(&oid_lookup, (const char *)&entry->sha1, sizeof(entry->sha1));
+		error = git_str_put(&oid_lookup, (char *)&entry->sha1.id, GIT_OID_RAWSZ);
 		if (error < 0)
 			goto cleanup;
 	}
diff --git a/src/libgit2/midx.h b/src/libgit2/midx.h
index 7dd851b..bcb0d9a 100644
--- a/src/libgit2/midx.h
+++ b/src/libgit2/midx.h
@@ -17,6 +17,7 @@
 #include "map.h"
 #include "mwindow.h"
 #include "odb.h"
+#include "oid.h"
 
 /*
  * A multi-pack-index file.
@@ -40,7 +41,7 @@ typedef struct git_midx_file {
 	uint32_t num_objects;
 
 	/* The OID Lookup table. */
-	git_oid *oid_lookup;
+	unsigned char *oid_lookup;
 
 	/* The Object Offsets table. Each entry has two 4-byte fields with the pack index and the offset. */
 	const unsigned char *object_offsets;
diff --git a/src/libgit2/oid.c b/src/libgit2/oid.c
index 19061e8..fb92174 100644
--- a/src/libgit2/oid.c
+++ b/src/libgit2/oid.c
@@ -187,8 +187,7 @@ int git_oid_fromraw(git_oid *out, const unsigned char *raw)
 
 int git_oid_cpy(git_oid *out, const git_oid *src)
 {
-	memcpy(out->id, src->id, sizeof(out->id));
-	return 0;
+	return git_oid_raw_cpy(out->id, src->id);
 }
 
 int git_oid_cmp(const git_oid *a, const git_oid *b)
@@ -203,25 +202,7 @@ int git_oid_equal(const git_oid *a, const git_oid *b)
 
 int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
 {
-	const unsigned char *a = oid_a->id;
-	const unsigned char *b = oid_b->id;
-
-	if (len > GIT_OID_HEXSZ)
-		len = GIT_OID_HEXSZ;
-
-	while (len > 1) {
-		if (*a != *b)
-			return 1;
-		a++;
-		b++;
-		len -= 2;
-	};
-
-	if (len)
-		if ((*a ^ *b) & 0xf0)
-			return 1;
-
-	return 0;
+	return git_oid_raw_ncmp(oid_a->id, oid_b->id, len);
 }
 
 int git_oid_strcmp(const git_oid *oid_a, const char *str)
diff --git a/src/libgit2/oid.h b/src/libgit2/oid.h
index 5baec33..abae9a4 100644
--- a/src/libgit2/oid.h
+++ b/src/libgit2/oid.h
@@ -25,11 +25,44 @@ extern const git_oid git_oid__empty_tree_sha1;
  */
 char *git_oid_allocfmt(const git_oid *id);
 
-GIT_INLINE(int) git_oid__hashcmp(const unsigned char *sha1, const unsigned char *sha2)
+GIT_INLINE(int) git_oid_raw_ncmp(
+	const unsigned char *sha1,
+	const unsigned char *sha2,
+	size_t len)
+{
+	if (len > GIT_OID_HEXSZ)
+		len = GIT_OID_HEXSZ;
+
+	while (len > 1) {
+		if (*sha1 != *sha2)
+			return 1;
+		sha1++;
+		sha2++;
+		len -= 2;
+	};
+
+	if (len)
+		if ((*sha1 ^ *sha2) & 0xf0)
+			return 1;
+
+	return 0;
+}
+
+GIT_INLINE(int) git_oid_raw_cmp(
+	const unsigned char *sha1,
+	const unsigned char *sha2)
 {
 	return memcmp(sha1, sha2, GIT_OID_RAWSZ);
 }
 
+GIT_INLINE(int) git_oid_raw_cpy(
+	unsigned char *dst,
+	const unsigned char *src)
+{
+	memcpy(dst, src, GIT_OID_RAWSZ);
+	return 0;
+}
+
 /*
  * Compare two oid structures.
  *
@@ -39,7 +72,7 @@ GIT_INLINE(int) git_oid__hashcmp(const unsigned char *sha1, const unsigned char 
  */
 GIT_INLINE(int) git_oid__cmp(const git_oid *a, const git_oid *b)
 {
-	return git_oid__hashcmp(a->id, b->id);
+	return git_oid_raw_cmp(a->id, b->id);
 }
 
 GIT_INLINE(void) git_oid__cpy_prefix(
diff --git a/src/libgit2/oidmap.c b/src/libgit2/oidmap.c
index 0ae8bf3..eaf9fa0 100644
--- a/src/libgit2/oidmap.c
+++ b/src/libgit2/oidmap.c
@@ -19,7 +19,7 @@ __KHASH_TYPE(oid, const git_oid *, void *)
 GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
 {
 	khint_t h;
-	memcpy(&h, oid, sizeof(khint_t));
+	memcpy(&h, oid->id, sizeof(khint_t));
 	return h;
 }
 
diff --git a/src/libgit2/pack.c b/src/libgit2/pack.c
index 5c0cba7..16fe378 100644
--- a/src/libgit2/pack.c
+++ b/src/libgit2/pack.c
@@ -1001,13 +1001,14 @@ int get_delta_base(
 		base_offset = delta_obj_offset - unsigned_base_offset;
 		*curpos += used;
 	} else if (type == GIT_OBJECT_REF_DELTA) {
+		git_oid base_oid;
+		git_oid_fromraw(&base_oid, base_info);
+
 		/* If we have the cooperative cache, search in it first */
 		if (p->has_cache) {
 			struct git_pack_entry *entry;
-			git_oid oid;
 
-			git_oid_fromraw(&oid, base_info);
-			if ((entry = git_oidmap_get(p->idx_cache, &oid)) != NULL) {
+			if ((entry = git_oidmap_get(p->idx_cache, &base_oid)) != NULL) {
 				if (entry->offset == 0)
 					return packfile_error("delta offset is zero");
 
@@ -1024,7 +1025,7 @@ int get_delta_base(
 		}
 
 		/* The base entry _must_ be in the same pack */
-		if (pack_entry_find_offset(&base_offset, &unused, p, (git_oid *)base_info, GIT_OID_HEXSZ) < 0)
+		if (pack_entry_find_offset(&base_offset, &unused, p, &base_oid, GIT_OID_HEXSZ) < 0)
 			return packfile_error("base entry delta is not in the same pack");
 		*curpos += 20;
 	} else
@@ -1082,7 +1083,7 @@ static int packfile_open_locked(struct git_pack_file *p)
 {
 	struct stat st;
 	struct git_pack_header hdr;
-	git_oid sha1;
+	unsigned char sha1[GIT_OID_RAWSZ];
 	unsigned char *idx_sha1;
 
 	if (pack_index_open_locked(p) < 0)
@@ -1130,12 +1131,12 @@ static int packfile_open_locked(struct git_pack_file *p)
 
 	/* Verify the pack matches its index. */
 	if (p->num_objects != ntohl(hdr.hdr_entries) ||
-		p_pread(p->mwf.fd, sha1.id, GIT_OID_RAWSZ, p->mwf.size - GIT_OID_RAWSZ) < 0)
+	    p_pread(p->mwf.fd, sha1, GIT_OID_RAWSZ, p->mwf.size - GIT_OID_RAWSZ) < 0)
 		goto cleanup;
 
 	idx_sha1 = ((unsigned char *)p->index_map.data) + p->index_map.len - 40;
 
-	if (git_oid__cmp(&sha1, (git_oid *)idx_sha1) != 0)
+	if (git_oid_raw_cmp(sha1, idx_sha1) != 0)
 		goto cleanup;
 
 	if (git_mwindow_file_register(&p->mwf) < 0)
@@ -1340,10 +1341,14 @@ int git_pack_foreach_entry(
 		}
 
 		git_vector_free(&offsets);
-		p->oids = (git_oid **)git_vector_detach(NULL, NULL, &oids);
+		p->oids = (unsigned char **)git_vector_detach(NULL, NULL, &oids);
 	}
 
-	/* We need to copy the OIDs to another array before we relinquish the lock to avoid races. */
+	/*
+	 * We need to copy the OIDs to another array before we
+	 * relinquish the lock to avoid races.  We can also take
+	 * this opportunity to put them into normal form.
+	 */
 	git_array_init_to_size(oids, p->num_objects);
 	if (!oids.ptr) {
 		git_mutex_unlock(&p->lock);
@@ -1357,7 +1362,7 @@ int git_pack_foreach_entry(
 			git_array_clear(oids);
 			GIT_ERROR_CHECK_ALLOC(oid);
 		}
-		git_oid_cpy(oid, p->oids[i]);
+		git_oid_fromraw(oid, p->oids[i]);
 	}
 
 	git_mutex_unlock(&p->lock);
@@ -1380,7 +1385,7 @@ int git_pack_foreach_entry_offset(
 {
 	const unsigned char *index;
 	off64_t current_offset;
-	const git_oid *current_oid;
+	git_oid current_oid;
 	uint32_t i;
 	int error = 0;
 
@@ -1422,8 +1427,9 @@ int git_pack_foreach_entry_offset(
 				current_offset = (((off64_t)ntohl(*((uint32_t *)(large_offset_ptr + 0)))) << 32) |
 						ntohl(*((uint32_t *)(large_offset_ptr + 4)));
 			}
-			current_oid = (const git_oid *)(index + 20 * i);
-			if ((error = cb(current_oid, current_offset, data)) != 0) {
+
+			git_oid_fromraw(&current_oid, (index + 20 * i));
+			if ((error = cb(&current_oid, current_offset, data)) != 0) {
 				error = git_error_set_after_callback(error);
 				goto cleanup;
 			}
@@ -1431,8 +1437,8 @@ int git_pack_foreach_entry_offset(
 	} else {
 		for (i = 0; i < p->num_objects; i++) {
 			current_offset = ntohl(*(const uint32_t *)(index + 24 * i));
-			current_oid = (const git_oid *)(index + 24 * i + 4);
-			if ((error = cb(current_oid, current_offset, data)) != 0) {
+			git_oid_fromraw(&current_oid, (index + 24 * i + 4));
+			if ((error = cb(&current_oid, current_offset, data)) != 0) {
 				error = git_error_set_after_callback(error);
 				goto cleanup;
 			}
@@ -1451,7 +1457,7 @@ int git_pack__lookup_sha1(const void *oid_lookup_table, size_t stride, unsigned 
 
 	while (lo < hi) {
 		unsigned mi = (lo + hi) / 2;
-		int cmp = git_oid__hashcmp(base + mi * stride, oid_prefix);
+		int cmp = git_oid_raw_cmp(base + mi * stride, oid_prefix);
 
 		if (!cmp)
 			return mi;
@@ -1530,7 +1536,7 @@ static int pack_entry_find_offset(
 		if (pos < (int)p->num_objects) {
 			current = index + pos * stride;
 
-			if (!git_oid_ncmp(short_oid, (const git_oid *)current, len))
+			if (!git_oid_raw_ncmp(short_oid->id, current, len))
 				found = 1;
 		}
 	}
@@ -1539,7 +1545,7 @@ static int pack_entry_find_offset(
 		/* Check for ambiguousity */
 		const unsigned char *next = current + stride;
 
-		if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) {
+		if (!git_oid_raw_ncmp(short_oid->id, next, len)) {
 			found = 2;
 		}
 	}
diff --git a/src/libgit2/pack.h b/src/libgit2/pack.h
index bf279c6..f87249b 100644
--- a/src/libgit2/pack.h
+++ b/src/libgit2/pack.h
@@ -19,6 +19,7 @@
 #include "offmap.h"
 #include "oidmap.h"
 #include "zstream.h"
+#include "oid.h"
 
 /**
  * Function type for callbacks from git_pack_foreach_entry_offset.
@@ -104,7 +105,7 @@ struct git_pack_file {
 	git_time_t mtime;
 	unsigned pack_local:1, pack_keep:1, has_cache:1;
 	git_oidmap *idx_cache;
-	git_oid **oids;
+	unsigned char **oids;
 
 	git_pack_cache bases; /* delta base cache */
 
diff --git a/src/libgit2/remote.c b/src/libgit2/remote.c
index 1a79faa..3d05937 100644
--- a/src/libgit2/remote.c
+++ b/src/libgit2/remote.c
@@ -1830,7 +1830,7 @@ static int update_one_tip(
 	}
 
 	if (error == GIT_ENOTFOUND) {
-		memset(&old, 0, GIT_OID_RAWSZ);
+		memset(&old, 0, sizeof(git_oid));
 		error = 0;
 
 		if (autotag && (error = git_vector_insert(update_heads, head)) < 0)
diff --git a/src/libgit2/tree-cache.c b/src/libgit2/tree-cache.c
index 0977c92..cd69e7b 100644
--- a/src/libgit2/tree-cache.c
+++ b/src/libgit2/tree-cache.c
@@ -263,7 +263,7 @@ static void write_tree(git_str *out, git_tree_cache *tree)
 	git_str_printf(out, "%s%c%"PRIdZ" %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count);
 
 	if (tree->entry_count != -1)
-		git_str_put(out, (const char *) &tree->oid, GIT_OID_RAWSZ);
+		git_str_put(out, (char *)&tree->oid.id, GIT_OID_RAWSZ);
 
 	for (i = 0; i < tree->children_count; i++)
 		write_tree(out, tree->children[i]);
diff --git a/src/libgit2/tree.c b/src/libgit2/tree.c
index a1545dc..a5371fd 100644
--- a/src/libgit2/tree.c
+++ b/src/libgit2/tree.c
@@ -82,6 +82,7 @@ int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2)
 static git_tree_entry *alloc_entry(const char *filename, size_t filename_len, const git_oid *id)
 {
 	git_tree_entry *entry = NULL;
+	char *filename_ptr;
 	size_t tree_len;
 
 	TREE_ENTRY_CHECK_NAMELEN(filename_len);
@@ -95,21 +96,13 @@ static git_tree_entry *alloc_entry(const char *filename, size_t filename_len, co
 	if (!entry)
 		return NULL;
 
-	{
-		char *filename_ptr;
-		void *id_ptr;
-
-		filename_ptr = ((char *) entry) + sizeof(git_tree_entry);
-		memcpy(filename_ptr, filename, filename_len);
-		entry->filename = filename_ptr;
-
-		id_ptr = filename_ptr + filename_len + 1;
-		git_oid_cpy(id_ptr, id);
-		entry->oid = id_ptr;
-	}
-
+	filename_ptr = ((char *) entry) + sizeof(git_tree_entry);
+	memcpy(filename_ptr, filename, filename_len);
+	entry->filename = filename_ptr;
 	entry->filename_len = (uint16_t)filename_len;
 
+	git_oid_cpy(&entry->oid, id);
+
 	return entry;
 }
 
@@ -231,7 +224,7 @@ int git_tree_entry_dup(git_tree_entry **dest, const git_tree_entry *source)
 
 	GIT_ASSERT_ARG(source);
 
-	cpy = alloc_entry(source->filename, source->filename_len, source->oid);
+	cpy = alloc_entry(source->filename, source->filename_len, &source->oid);
 	if (cpy == NULL)
 		return -1;
 
@@ -269,7 +262,7 @@ const char *git_tree_entry_name(const git_tree_entry *entry)
 const git_oid *git_tree_entry_id(const git_tree_entry *entry)
 {
 	GIT_ASSERT_ARG_WITH_RETVAL(entry, NULL);
-	return entry->oid;
+	return &entry->oid;
 }
 
 git_object_t git_tree_entry_type(const git_tree_entry *entry)
@@ -292,7 +285,7 @@ int git_tree_entry_to_object(
 	GIT_ASSERT_ARG(entry);
 	GIT_ASSERT_ARG(object_out);
 
-	return git_object_lookup(object_out, repo, entry->oid, GIT_OBJECT_ANY);
+	return git_object_lookup(object_out, repo, &entry->oid, GIT_OBJECT_ANY);
 }
 
 static const git_tree_entry *entry_fromname(
@@ -331,7 +324,7 @@ const git_tree_entry *git_tree_entry_byid(
 	GIT_ASSERT_ARG_WITH_RETVAL(tree, NULL);
 
 	git_array_foreach(tree->entries, i, e) {
-		if (memcmp(&e->oid->id, &id->id, sizeof(id->id)) == 0)
+		if (git_oid_equal(&e->oid, id))
 			return e;
 	}
 
@@ -432,7 +425,7 @@ int git_tree__parse_raw(void *_tree, const char *data, size_t size)
 			entry->attr = attr;
 			entry->filename_len = (uint16_t)filename_len;
 			entry->filename = buffer;
-			entry->oid = (git_oid *) ((char *) buffer + filename_len + 1);
+			git_oid_fromraw(&entry->oid, ((unsigned char *) buffer + filename_len + 1));
 		}
 
 		buffer += filename_len + 1;
@@ -536,7 +529,7 @@ static int git_treebuilder__write_with_buffer(
 
 		git_str_printf(buf, "%o ", entry->attr);
 		git_str_put(buf, entry->filename, entry->filename_len + 1);
-		git_str_put(buf, (char *)entry->oid->id, GIT_OID_RAWSZ);
+		git_str_put(buf, (char *)entry->oid.id, GIT_OID_RAWSZ);
 
 		if (git_str_oom(buf)) {
 			error = -1;
@@ -765,7 +758,7 @@ int git_treebuilder_new(
 		git_array_foreach(source->entries, i, entry_src) {
 			if (append_entry(
 				bld, entry_src->filename,
-				entry_src->oid,
+				&entry_src->oid,
 				entry_src->attr,
 				false) < 0)
 				goto on_error;
@@ -798,7 +791,7 @@ int git_treebuilder_insert(
 		return error;
 
 	if ((entry = git_strmap_get(bld->map, filename)) != NULL) {
-		git_oid_cpy((git_oid *) entry->oid, id);
+		git_oid_cpy(&entry->oid, id);
 	} else {
 		entry = alloc_entry(filename, strlen(filename), id);
 		GIT_ERROR_CHECK_ALLOC(entry);
@@ -954,7 +947,7 @@ int git_tree_entry_bypath(
 		return git_tree_entry_dup(entry_out, entry);
 	}
 
-	if (git_tree_lookup(&subtree, root->object.repo, entry->oid) < 0)
+	if (git_tree_lookup(&subtree, root->object.repo, &entry->oid) < 0)
 		return -1;
 
 	error = git_tree_entry_bypath(
@@ -995,7 +988,7 @@ static int tree_walk(
 			git_tree *subtree;
 			size_t path_len = git_str_len(path);
 
-			error = git_tree_lookup(&subtree, tree->object.repo, entry->oid);
+			error = git_tree_lookup(&subtree, tree->object.repo, &entry->oid);
 			if (error < 0)
 				break;
 
diff --git a/src/libgit2/tree.h b/src/libgit2/tree.h
index 6bd9ed6..0dd963f 100644
--- a/src/libgit2/tree.h
+++ b/src/libgit2/tree.h
@@ -19,7 +19,7 @@
 struct git_tree_entry {
 	uint16_t attr;
 	uint16_t filename_len;
-	const git_oid *oid;
+	git_oid oid;
 	const char *filename;
 };
 
diff --git a/tests/libgit2/iterator/tree.c b/tests/libgit2/iterator/tree.c
index 4145c8d..06a920a 100644
--- a/tests/libgit2/iterator/tree.c
+++ b/tests/libgit2/iterator/tree.c
@@ -268,7 +268,7 @@ static void check_tree_entry(
 
 	cl_git_pass(git_iterator_current_tree_entry(&te, i));
 	cl_assert(te);
-	cl_assert(git_oid_streq(te->oid, oid) == 0);
+	cl_assert(git_oid_streq(&te->oid, oid) == 0);
 
 	cl_git_pass(git_iterator_current(&ie, i));
 
diff --git a/tests/libgit2/object/raw/hash.c b/tests/libgit2/object/raw/hash.c
index 5a3e818..8f22fe0 100644
--- a/tests/libgit2/object/raw/hash.c
+++ b/tests/libgit2/object/raw/hash.c
@@ -24,20 +24,23 @@ static char *bye_text = "bye world\n";
 void test_object_raw_hash__hash_by_blocks(void)
 {
 	git_hash_ctx ctx;
+	unsigned char hash[GIT_HASH_SHA1_SIZE];
 	git_oid id1, id2;
 
 	cl_git_pass(git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1));
 
 	/* should already be init'd */
 	cl_git_pass(git_hash_update(&ctx, hello_text, strlen(hello_text)));
-	cl_git_pass(git_hash_final(id2.id, &ctx));
+	cl_git_pass(git_hash_final(hash, &ctx));
+	cl_git_pass(git_oid_fromraw(&id2, hash));
 	cl_git_pass(git_oid_fromstr(&id1, hello_id));
 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
 
 	/* reinit should permit reuse */
 	cl_git_pass(git_hash_init(&ctx));
 	cl_git_pass(git_hash_update(&ctx, bye_text, strlen(bye_text)));
-	cl_git_pass(git_hash_final(id2.id, &ctx));
+	cl_git_pass(git_hash_final(hash, &ctx));
+	cl_git_pass(git_oid_fromraw(&id2, hash));
 	cl_git_pass(git_oid_fromstr(&id1, bye_id));
 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
 
@@ -47,9 +50,11 @@ void test_object_raw_hash__hash_by_blocks(void)
 void test_object_raw_hash__hash_buffer_in_single_call(void)
 {
 	git_oid id1, id2;
+	unsigned char hash[GIT_HASH_SHA1_SIZE];
 
 	cl_git_pass(git_oid_fromstr(&id1, hello_id));
-	git_hash_buf(id2.id, hello_text, strlen(hello_text), GIT_HASH_ALGORITHM_SHA1);
+	cl_git_pass(git_hash_buf(hash, hello_text, strlen(hello_text), GIT_HASH_ALGORITHM_SHA1));
+	cl_git_pass(git_oid_fromraw(&id2, hash));
 	cl_assert(git_oid_cmp(&id1, &id2) == 0);
 }
 
diff --git a/tests/libgit2/object/raw/short.c b/tests/libgit2/object/raw/short.c
index e8d2cf5..cc2b5f6 100644
--- a/tests/libgit2/object/raw/short.c
+++ b/tests/libgit2/object/raw/short.c
@@ -28,12 +28,15 @@ static int insert_sequential_oids(
 	int i, min_len = 0;
 	char numbuf[16];
 	git_oid oid;
+	unsigned char hashbuf[GIT_HASH_SHA1_SIZE];
 	char **oids = git__calloc(n, sizeof(char *));
 	cl_assert(oids != NULL);
 
 	for (i = 0; i < n; ++i) {
 		p_snprintf(numbuf, sizeof(numbuf), "%u", (unsigned int)i);
-		git_hash_buf(oid.id, numbuf, strlen(numbuf), GIT_HASH_ALGORITHM_SHA1);
+		git_hash_buf(hashbuf, numbuf, strlen(numbuf), GIT_HASH_ALGORITHM_SHA1);
+
+		git_oid_fromraw(&oid, hashbuf);
 
 		oids[i] = git__malloc(GIT_OID_HEXSZ + 1);
 		cl_assert(oids[i]);
diff --git a/tests/libgit2/object/tree/parse.c b/tests/libgit2/object/tree/parse.c
index 9d76a74..4e871dc 100644
--- a/tests/libgit2/object/tree/parse.c
+++ b/tests/libgit2/object/tree/parse.c
@@ -40,7 +40,7 @@ static void assert_tree_parses(const char *data, size_t datalen,
 		cl_assert(entry = git_tree_entry_byname(tree, expected->filename));
 		cl_assert_equal_s(expected->filename, entry->filename);
 		cl_assert_equal_i(expected->attr, entry->attr);
-		cl_assert_equal_oid(&oid, entry->oid);
+		cl_assert_equal_oid(&oid, &entry->oid);
 	}
 
 	git_object_free(&tree->object);
diff --git a/tests/libgit2/pack/packbuilder.c b/tests/libgit2/pack/packbuilder.c
index f235798..0889f46 100644
--- a/tests/libgit2/pack/packbuilder.c
+++ b/tests/libgit2/pack/packbuilder.c
@@ -68,7 +68,7 @@ static void seed_packbuilder(void)
 	cl_git_pass(git_revwalk_push_ref(_revwalker, "HEAD"));
 
 	while (git_revwalk_next(&oid, _revwalker) == 0) {
-		o = git__malloc(GIT_OID_RAWSZ);
+		o = git__malloc(sizeof(git_oid));
 		cl_assert(o != NULL);
 		git_oid_cpy(o, &oid);
 		cl_git_pass(git_vector_insert(&_commits, o));