don't verify pack index SHA1 upon regular access
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
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index f0d6dcd..ed55c45 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -144,7 +144,7 @@ struct got_packfile_obj_data {
} __attribute__((__packed__));
const struct got_error *got_packidx_open(struct got_packidx **,
- const char *);
+ const char *, int);
const struct got_error* got_packidx_close(struct got_packidx *);
const struct got_error *got_packfile_open_object(struct got_object **,
diff --git a/lib/pack.c b/lib/pack.c
index 61b8394..2636e0c 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -119,7 +119,7 @@ get_packfile_size(size_t *size, const char *path)
}
const struct got_error *
-got_packidx_open(struct got_packidx **packidx, const char *path)
+got_packidx_open(struct got_packidx **packidx, const char *path, int verify)
{
struct got_packidx *p;
struct got_packidx_v2_hdr *h;
@@ -180,7 +180,8 @@ got_packidx_open(struct got_packidx **packidx, const char *path)
offset += sizeof(*h->magic);
remain -= sizeof(*h->magic);
- SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
+ if (verify)
+ SHA1Update(&ctx, (uint8_t *)h->magic, sizeof(*h->magic));
if (remain < sizeof(*h->version)) {
err = got_error(GOT_ERR_BAD_PACKIDX);
@@ -194,7 +195,8 @@ got_packidx_open(struct got_packidx **packidx, const char *path)
offset += sizeof(*h->version);
remain -= sizeof(*h->version);
- SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
+ if (verify)
+ SHA1Update(&ctx, (uint8_t *)h->version, sizeof(*h->version));
len_fanout =
sizeof(*h->fanout_table) * GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS;
@@ -206,7 +208,8 @@ got_packidx_open(struct got_packidx **packidx, const char *path)
err = verify_fanout_table(h->fanout_table);
if (err)
goto done;
- SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
+ if (verify)
+ SHA1Update(&ctx, (uint8_t *)h->fanout_table, len_fanout);
offset += len_fanout;
remain -= len_fanout;
@@ -218,7 +221,8 @@ got_packidx_open(struct got_packidx **packidx, const char *path)
}
h->sorted_ids =
(struct got_packidx_object_id *)((uint8_t*)(p->map + offset));
- SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
+ if (verify)
+ SHA1Update(&ctx, (uint8_t *)h->sorted_ids, len_ids);
offset += len_ids;
remain -= len_ids;
@@ -227,7 +231,8 @@ got_packidx_open(struct got_packidx **packidx, const char *path)
goto done;
}
h->crc32 = (uint32_t *)((uint8_t*)(p->map + offset));
- SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
+ if (verify)
+ SHA1Update(&ctx, (uint8_t *)h->crc32, nobj * sizeof(*h->crc32));
remain -= nobj * sizeof(*h->crc32);
offset += nobj * sizeof(*h->crc32);
@@ -236,7 +241,9 @@ got_packidx_open(struct got_packidx **packidx, const char *path)
goto done;
}
h->offsets = (uint32_t *)((uint8_t*)(p->map + offset));
- SHA1Update(&ctx, (uint8_t *)h->offsets, nobj * sizeof(*h->offsets));
+ if (verify)
+ SHA1Update(&ctx, (uint8_t *)h->offsets,
+ nobj * sizeof(*h->offsets));
remain -= nobj * sizeof(*h->offsets);
offset += nobj * sizeof(*h->offsets);
@@ -249,8 +256,9 @@ got_packidx_open(struct got_packidx **packidx, const char *path)
goto done;
}
h->large_offsets = (uint64_t *)((uint8_t*)(p->map + offset));
- SHA1Update(&ctx, (uint8_t*)h->large_offsets,
- nobj * sizeof(*h->large_offsets));
+ if (verify)
+ SHA1Update(&ctx, (uint8_t*)h->large_offsets,
+ nobj * sizeof(*h->large_offsets));
remain -= nobj * sizeof(*h->large_offsets);
offset += nobj * sizeof(*h->large_offsets);
@@ -261,10 +269,13 @@ checksum:
}
h->trailer =
(struct got_packidx_trailer *)((uint8_t*)(p->map + offset));
- SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
- SHA1Final(sha1, &ctx);
- if (memcmp(h->trailer->packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
- err = got_error(GOT_ERR_PACKIDX_CSUM);
+ if (verify) {
+ SHA1Update(&ctx, h->trailer->packfile_sha1, SHA1_DIGEST_LENGTH);
+ SHA1Final(sha1, &ctx);
+ if (memcmp(h->trailer->packidx_sha1, sha1,
+ SHA1_DIGEST_LENGTH) != 0)
+ err = got_error(GOT_ERR_PACKIDX_CSUM);
+ }
done:
if (err)
got_packidx_close(p);
@@ -419,7 +430,7 @@ search_packidx(struct got_packidx **packidx, int *idx,
goto done;
}
- err = got_packidx_open(packidx, path_packidx);
+ err = got_packidx_open(packidx, path_packidx, 0);
free(path_packidx);
if (err)
goto done;