1#include "cache.h"
2#include "pack.h"
3#include "pack-revindex.h"
4#include "progress.h"
56
struct idx_entry {
7off_t offset;
8const unsigned char *sha1;
9unsigned int nr;
10};
1112
static int compare_entries(const void *e1, const void *e2)
13{
14const struct idx_entry *entry1 = e1;
15const struct idx_entry *entry2 = e2;
16if (entry1->offset < entry2->offset)
17return -1;
18if (entry1->offset > entry2->offset)
19return 1;
20return 0;
21}
2223
int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
24off_t offset, off_t len, unsigned int nr)
25{
26const uint32_t *index_crc;
27uint32_t data_crc = crc32(0, NULL, 0);
2829
do {
30unsigned long avail;
31void *data = use_pack(p, w_curs, offset, &avail);
32if (avail > len)
33avail = len;
34data_crc = crc32(data_crc, data, avail);
35offset += avail;
36len -= avail;
37} while (len);
3839
index_crc = p->index_data;
40index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
4142
return data_crc != ntohl(*index_crc);
43}
4445
static int verify_packfile(struct packed_git *p,
46struct pack_window **w_curs,
47verify_fn fn,
48struct progress *progress, uint32_t base_count)
4950
{
51off_t index_size = p->index_size;
52const unsigned char *index_base = p->index_data;
53git_SHA_CTX ctx;
54unsigned char sha1[20], *pack_sig;
55off_t offset = 0, pack_sig_ofs = 0;
56uint32_t nr_objects, i;
57int err = 0;
58struct idx_entry *entries;
5960
/* Note that the pack header checks are actually performed by
61* use_pack when it first opens the pack file. If anything
62* goes wrong during those checks then the call will die out
63* immediately.
64*/
6566
git_SHA1_Init(&ctx);
67do {
68unsigned long remaining;
69unsigned char *in = use_pack(p, w_curs, offset, &remaining);
70offset += remaining;
71if (!pack_sig_ofs)
72pack_sig_ofs = p->pack_size - 20;
73if (offset > pack_sig_ofs)
74remaining -= (unsigned int)(offset - pack_sig_ofs);
75git_SHA1_Update(&ctx, in, remaining);
76} while (offset < pack_sig_ofs);
77git_SHA1_Final(sha1, &ctx);
78pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
79if (hashcmp(sha1, pack_sig))
80err = error("%s SHA1 checksum mismatch",
81p->pack_name);
82if (hashcmp(index_base + index_size - 40, pack_sig))
83err = error("%s SHA1 does not match its index",
84p->pack_name);
85unuse_pack(w_curs);
8687
/* Make sure everything reachable from idx is valid. Since we
88* have verified that nr_objects matches between idx and pack,
89* we do not do scan-streaming check on the pack file.
90*/
91nr_objects = p->num_objects;
92entries = xmalloc((nr_objects + 1) * sizeof(*entries));
93entries[nr_objects].offset = pack_sig_ofs;
94/* first sort entries by pack offset, since unpacking them is more efficient that way */
95for (i = 0; i < nr_objects; i++) {
96entries[i].sha1 = nth_packed_object_sha1(p, i);
97if (!entries[i].sha1)
98die("internal error pack-check nth-packed-object");
99entries[i].offset = nth_packed_object_offset(p, i);
100entries[i].nr = i;
101}
102qsort(entries, nr_objects, sizeof(*entries), compare_entries);
103104
for (i = 0; i < nr_objects; i++) {
105void *data;
106enum object_type type;
107unsigned long size;
108109
if (p->index_version > 1) {
110off_t offset = entries[i].offset;
111off_t len = entries[i+1].offset - offset;
112unsigned int nr = entries[i].nr;
113if (check_pack_crc(p, w_curs, offset, len, nr))
114err = error("index CRC mismatch for object %s "
115"from %s at offset %"PRIuMAX"",
116sha1_to_hex(entries[i].sha1),
117p->pack_name, (uintmax_t)offset);
118}
119data = unpack_entry(p, entries[i].offset, &type, &size);
120if (!data)
121err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
122sha1_to_hex(entries[i].sha1), p->pack_name,
123(uintmax_t)entries[i].offset);
124else if (check_sha1_signature(entries[i].sha1, data, size, typename(type)))
125err = error("packed %s from %s is corrupt",
126sha1_to_hex(entries[i].sha1), p->pack_name);
127else if (fn) {
128int eaten = 0;
129err |= fn(entries[i].sha1, type, size, data, &eaten);
130if (eaten)
131data = NULL;
132}
133if (((base_count + i) & 1023) == 0)
134display_progress(progress, base_count + i);
135free(data);
136137
}
138display_progress(progress, base_count + i);
139free(entries);
140141
return err;
142}
143144
int verify_pack_index(struct packed_git *p)
145{
146off_t index_size;
147const unsigned char *index_base;
148git_SHA_CTX ctx;
149unsigned char sha1[20];
150int err = 0;
151152
if (open_pack_index(p))
153return error("packfile %s index not opened", p->pack_name);
154index_size = p->index_size;
155index_base = p->index_data;
156157
/* Verify SHA1 sum of the index file */
158git_SHA1_Init(&ctx);
159git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
160git_SHA1_Final(sha1, &ctx);
161if (hashcmp(sha1, index_base + index_size - 20))
162err = error("Packfile index for %s SHA1 mismatch",
163p->pack_name);
164return err;
165}
166167
int verify_pack(struct packed_git *p, verify_fn fn,
168struct progress *progress, uint32_t base_count)
169{
170int err = 0;
171struct pack_window *w_curs = NULL;
172173
err |= verify_pack_index(p);
174if (!p->index_data)
175return -1;
176177
err |= verify_packfile(p, &w_curs, fn, progress, base_count);
178unuse_pack(&w_curs);
179180
return err;
181}