Skip to content

Commit 3b52e5f

Browse files
authored
Merge pull request libgit2#6265 from libgit2/ethomson/sha256_two
sha256: refactoring in preparation for sha256
2 parents 6397024 + f882140 commit 3b52e5f

File tree

22 files changed

+151
-122
lines changed

22 files changed

+151
-122
lines changed

fuzzers/packfile_fuzzer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9494
fprintf(stderr, "Failed to compute the SHA1 hash\n");
9595
abort();
9696
}
97-
if (git_indexer_append(indexer, &oid, sizeof(oid), &stats) < 0) {
97+
if (git_indexer_append(indexer, &oid.id, GIT_OID_RAWSZ, &stats) < 0) {
9898
goto cleanup;
9999
}
100100
}

src/libgit2/commit_graph.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static int commit_graph_parse_oid_lookup(
138138
struct git_commit_graph_chunk *chunk_oid_lookup)
139139
{
140140
uint32_t i;
141-
git_oid *oid, *prev_oid, zero_oid = {{0}};
141+
unsigned char *oid, *prev_oid, zero_oid[GIT_OID_RAWSZ] = {0};
142142

143143
if (chunk_oid_lookup->offset == 0)
144144
return commit_graph_error("missing OID Lookup chunk");
@@ -147,10 +147,10 @@ static int commit_graph_parse_oid_lookup(
147147
if (chunk_oid_lookup->length != file->num_commits * GIT_OID_RAWSZ)
148148
return commit_graph_error("OID Lookup chunk has wrong length");
149149

150-
file->oid_lookup = oid = (git_oid *)(data + chunk_oid_lookup->offset);
151-
prev_oid = &zero_oid;
152-
for (i = 0; i < file->num_commits; ++i, ++oid) {
153-
if (git_oid_cmp(prev_oid, oid) >= 0)
150+
file->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset);
151+
prev_oid = zero_oid;
152+
for (i = 0; i < file->num_commits; ++i, oid += GIT_OID_RAWSZ) {
153+
if (git_oid_raw_cmp(prev_oid, oid) >= 0)
154154
return commit_graph_error("OID Lookup index is non-monotonic");
155155
prev_oid = oid;
156156
}
@@ -437,7 +437,7 @@ static int git_commit_graph_entry_get_byindex(
437437
}
438438

439439
commit_data = file->commit_data + pos * (GIT_OID_RAWSZ + 4 * sizeof(uint32_t));
440-
git_oid_cpy(&e->tree_oid, (const git_oid *)commit_data);
440+
git_oid_fromraw(&e->tree_oid, commit_data);
441441
e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + GIT_OID_RAWSZ)));
442442
e->parent_indices[1] = ntohl(
443443
*((uint32_t *)(commit_data + GIT_OID_RAWSZ + sizeof(uint32_t))));
@@ -470,7 +470,8 @@ static int git_commit_graph_entry_get_byindex(
470470
e->parent_count++;
471471
}
472472
}
473-
git_oid_cpy(&e->sha1, &file->oid_lookup[pos]);
473+
474+
git_oid_fromraw(&e->sha1, &file->oid_lookup[pos * GIT_OID_RAWSZ]);
474475
return 0;
475476
}
476477

@@ -514,7 +515,7 @@ int git_commit_graph_entry_find(
514515
{
515516
int pos, found = 0;
516517
uint32_t hi, lo;
517-
const git_oid *current = NULL;
518+
const unsigned char *current = NULL;
518519

519520
GIT_ASSERT_ARG(e);
520521
GIT_ASSERT_ARG(file);
@@ -528,26 +529,25 @@ int git_commit_graph_entry_find(
528529
if (pos >= 0) {
529530
/* An object matching exactly the oid was found */
530531
found = 1;
531-
current = file->oid_lookup + pos;
532+
current = file->oid_lookup + (pos * GIT_OID_RAWSZ);
532533
} else {
533534
/* No object was found */
534535
/* pos refers to the object with the "closest" oid to short_oid */
535536
pos = -1 - pos;
536537
if (pos < (int)file->num_commits) {
537-
current = file->oid_lookup + pos;
538+
current = file->oid_lookup + (pos * GIT_OID_RAWSZ);
538539

539-
if (!git_oid_ncmp(short_oid, current, len))
540+
if (!git_oid_raw_ncmp(short_oid->id, current, len))
540541
found = 1;
541542
}
542543
}
543544

544545
if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)file->num_commits) {
545546
/* Check for ambiguousity */
546-
const git_oid *next = current + 1;
547+
const unsigned char *next = current + GIT_OID_RAWSZ;
547548

548-
if (!git_oid_ncmp(short_oid, next, len)) {
549+
if (!git_oid_raw_ncmp(short_oid->id, next, len))
549550
found = 2;
550-
}
551551
}
552552

553553
if (!found)
@@ -1019,7 +1019,9 @@ static int commit_graph_write(
10191019
/* Fill the OID Lookup table. */
10201020
git_vector_foreach (&w->commits, i, packed_commit) {
10211021
error = git_str_put(&oid_lookup,
1022-
(const char *)&packed_commit->sha1, sizeof(git_oid));
1022+
(const char *)&packed_commit->sha1.id,
1023+
GIT_OID_RAWSZ);
1024+
10231025
if (error < 0)
10241026
goto cleanup;
10251027
}
@@ -1034,8 +1036,9 @@ static int commit_graph_write(
10341036
unsigned int parentcount = (unsigned int)git_array_size(packed_commit->parents);
10351037

10361038
error = git_str_put(&commit_data,
1037-
(const char *)&packed_commit->tree_oid,
1038-
sizeof(git_oid));
1039+
(const char *)&packed_commit->tree_oid.id,
1040+
GIT_OID_RAWSZ);
1041+
10391042
if (error < 0)
10401043
goto cleanup;
10411044

src/libgit2/commit_graph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ typedef struct git_commit_graph_file {
3636
uint32_t num_commits;
3737

3838
/* The OID Lookup table. */
39-
git_oid *oid_lookup;
39+
unsigned char *oid_lookup;
4040

4141
/*
4242
* The Commit Data table. Each entry contains the OID of the commit followed

src/libgit2/index.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct entry_short {
7474
uint32_t uid;
7575
uint32_t gid;
7676
uint32_t file_size;
77-
git_oid oid;
77+
unsigned char oid[GIT_OID_RAWSZ];
7878
uint16_t flags;
7979
char path[1]; /* arbitrary length */
8080
};
@@ -88,7 +88,7 @@ struct entry_long {
8888
uint32_t uid;
8989
uint32_t gid;
9090
uint32_t file_size;
91-
git_oid oid;
91+
unsigned char oid[GIT_OID_RAWSZ];
9292
uint16_t flags;
9393
uint16_t flags_extended;
9494
char path[1]; /* arbitrary length */
@@ -2480,9 +2480,11 @@ static int read_entry(
24802480
entry.uid = ntohl(source.uid);
24812481
entry.gid = ntohl(source.gid);
24822482
entry.file_size = ntohl(source.file_size);
2483-
git_oid_cpy(&entry.id, &source.oid);
24842483
entry.flags = ntohs(source.flags);
24852484

2485+
if (git_oid_fromraw(&entry.id, source.oid) < 0)
2486+
return -1;
2487+
24862488
if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) {
24872489
uint16_t flags_raw;
24882490
size_t flags_offset;
@@ -2803,9 +2805,7 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha
28032805
ondisk.uid = htonl(entry->uid);
28042806
ondisk.gid = htonl(entry->gid);
28052807
ondisk.file_size = htonl((uint32_t)entry->file_size);
2806-
2807-
git_oid_cpy(&ondisk.oid, &entry->id);
2808-
2808+
git_oid_raw_cpy(ondisk.oid, entry->id.id);
28092809
ondisk.flags = htons(entry->flags);
28102810

28112811
if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) {

src/libgit2/indexer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static int check_object_connectivity(git_indexer *idx, const git_rawobj *obj)
385385
size_t i;
386386

387387
git_array_foreach(tree->entries, i, entry)
388-
if (add_expected_oid(idx, entry->oid) < 0)
388+
if (add_expected_oid(idx, &entry->oid) < 0)
389389
goto out;
390390

391391
break;
@@ -1269,7 +1269,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats)
12691269

12701270
/* Write out the object names (SHA-1 hashes) */
12711271
git_vector_foreach(&idx->objects, i, entry) {
1272-
git_filebuf_write(&index_file, &entry->oid, sizeof(git_oid));
1272+
git_filebuf_write(&index_file, &entry->oid.id, GIT_OID_RAWSZ);
12731273
}
12741274

12751275
/* Write out the CRC32 values */

src/libgit2/iterator.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ GIT_INLINE(int) tree_iterator_frame_push_neighbors(
613613
break;
614614

615615
if ((error = git_tree_lookup(&tree,
616-
iter->base.repo, entry->tree_entry->oid)) < 0)
616+
iter->base.repo, &entry->tree_entry->oid)) < 0)
617617
break;
618618

619619
if (git_vector_insert(&parent_frame->similar_trees, tree) < 0)
@@ -659,7 +659,7 @@ GIT_INLINE(int) tree_iterator_frame_push(
659659
int error;
660660

661661
if ((error = git_tree_lookup(&tree,
662-
iter->base.repo, entry->tree_entry->oid)) < 0 ||
662+
iter->base.repo, &entry->tree_entry->oid)) < 0 ||
663663
(error = tree_iterator_frame_init(iter, tree, entry)) < 0)
664664
goto done;
665665

@@ -740,7 +740,7 @@ static void tree_iterator_set_current(
740740

741741
iter->entry.mode = tree_entry->attr;
742742
iter->entry.path = iter->entry_path.ptr;
743-
git_oid_cpy(&iter->entry.id, tree_entry->oid);
743+
git_oid_cpy(&iter->entry.id, &tree_entry->oid);
744744
}
745745

746746
static int tree_iterator_advance(const git_index_entry **out, git_iterator *i)

src/libgit2/midx.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static int midx_parse_oid_lookup(
115115
struct git_midx_chunk *chunk_oid_lookup)
116116
{
117117
uint32_t i;
118-
git_oid *oid, *prev_oid, zero_oid = {{0}};
118+
unsigned char *oid, *prev_oid, zero_oid[GIT_OID_RAWSZ] = {0};
119119

120120
if (chunk_oid_lookup->offset == 0)
121121
return midx_error("missing OID Lookup chunk");
@@ -124,10 +124,10 @@ static int midx_parse_oid_lookup(
124124
if (chunk_oid_lookup->length != idx->num_objects * GIT_OID_RAWSZ)
125125
return midx_error("OID Lookup chunk has wrong length");
126126

127-
idx->oid_lookup = oid = (git_oid *)(data + chunk_oid_lookup->offset);
128-
prev_oid = &zero_oid;
129-
for (i = 0; i < idx->num_objects; ++i, ++oid) {
130-
if (git_oid_cmp(prev_oid, oid) >= 0)
127+
idx->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset);
128+
prev_oid = zero_oid;
129+
for (i = 0; i < idx->num_objects; ++i, oid += GIT_OID_RAWSZ) {
130+
if (git_oid_raw_cmp(prev_oid, oid) >= 0)
131131
return midx_error("OID Lookup index is non-monotonic");
132132
prev_oid = oid;
133133
}
@@ -389,7 +389,7 @@ int git_midx_entry_find(
389389
int pos, found = 0;
390390
size_t pack_index;
391391
uint32_t hi, lo;
392-
const git_oid *current = NULL;
392+
unsigned char *current = NULL;
393393
const unsigned char *object_offset;
394394
off64_t offset;
395395

@@ -403,26 +403,25 @@ int git_midx_entry_find(
403403
if (pos >= 0) {
404404
/* An object matching exactly the oid was found */
405405
found = 1;
406-
current = idx->oid_lookup + pos;
406+
current = idx->oid_lookup + (pos * GIT_OID_RAWSZ);
407407
} else {
408408
/* No object was found */
409409
/* pos refers to the object with the "closest" oid to short_oid */
410410
pos = -1 - pos;
411411
if (pos < (int)idx->num_objects) {
412-
current = idx->oid_lookup + pos;
412+
current = idx->oid_lookup + (pos * GIT_OID_RAWSZ);
413413

414-
if (!git_oid_ncmp(short_oid, current, len))
414+
if (!git_oid_raw_ncmp(short_oid->id, current, len))
415415
found = 1;
416416
}
417417
}
418418

419419
if (found && len != GIT_OID_HEXSZ && pos + 1 < (int)idx->num_objects) {
420420
/* Check for ambiguousity */
421-
const git_oid *next = current + 1;
421+
const unsigned char *next = current + GIT_OID_RAWSZ;
422422

423-
if (!git_oid_ncmp(short_oid, next, len)) {
423+
if (!git_oid_raw_ncmp(short_oid->id, next, len))
424424
found = 2;
425-
}
426425
}
427426

428427
if (!found)
@@ -450,7 +449,7 @@ int git_midx_entry_find(
450449
return midx_error("invalid index into the packfile names table");
451450
e->pack_index = pack_index;
452451
e->offset = offset;
453-
git_oid_cpy(&e->sha1, current);
452+
git_oid_fromraw(&e->sha1, current);
454453
return 0;
455454
}
456455

@@ -459,13 +458,17 @@ int git_midx_foreach_entry(
459458
git_odb_foreach_cb cb,
460459
void *data)
461460
{
461+
git_oid oid;
462462
size_t i;
463463
int error;
464464

465465
GIT_ASSERT_ARG(idx);
466466

467467
for (i = 0; i < idx->num_objects; ++i) {
468-
if ((error = cb(&idx->oid_lookup[i], data)) != 0)
468+
if ((error = git_oid_fromraw(&oid, &idx->oid_lookup[i * GIT_OID_RAWSZ])) < 0)
469+
return error;
470+
471+
if ((error = cb(&oid, data)) != 0)
469472
return git_error_set_after_callback(error);
470473
}
471474

@@ -751,7 +754,7 @@ static int midx_write(
751754

752755
/* Fill the OID Lookup table. */
753756
git_vector_foreach (&object_entries, i, entry) {
754-
error = git_str_put(&oid_lookup, (const char *)&entry->sha1, sizeof(entry->sha1));
757+
error = git_str_put(&oid_lookup, (char *)&entry->sha1.id, GIT_OID_RAWSZ);
755758
if (error < 0)
756759
goto cleanup;
757760
}

src/libgit2/midx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "map.h"
1818
#include "mwindow.h"
1919
#include "odb.h"
20+
#include "oid.h"
2021

2122
/*
2223
* A multi-pack-index file.
@@ -40,7 +41,7 @@ typedef struct git_midx_file {
4041
uint32_t num_objects;
4142

4243
/* The OID Lookup table. */
43-
git_oid *oid_lookup;
44+
unsigned char *oid_lookup;
4445

4546
/* The Object Offsets table. Each entry has two 4-byte fields with the pack index and the offset. */
4647
const unsigned char *object_offsets;

src/libgit2/oid.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ int git_oid_fromraw(git_oid *out, const unsigned char *raw)
187187

188188
int git_oid_cpy(git_oid *out, const git_oid *src)
189189
{
190-
memcpy(out->id, src->id, sizeof(out->id));
191-
return 0;
190+
return git_oid_raw_cpy(out->id, src->id);
192191
}
193192

194193
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)
203202

204203
int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
205204
{
206-
const unsigned char *a = oid_a->id;
207-
const unsigned char *b = oid_b->id;
208-
209-
if (len > GIT_OID_HEXSZ)
210-
len = GIT_OID_HEXSZ;
211-
212-
while (len > 1) {
213-
if (*a != *b)
214-
return 1;
215-
a++;
216-
b++;
217-
len -= 2;
218-
};
219-
220-
if (len)
221-
if ((*a ^ *b) & 0xf0)
222-
return 1;
223-
224-
return 0;
205+
return git_oid_raw_ncmp(oid_a->id, oid_b->id, len);
225206
}
226207

227208
int git_oid_strcmp(const git_oid *oid_a, const char *str)

0 commit comments

Comments
 (0)