1#include "cache.h"2#include "pack.h"3#include "pack-revindex.h"4#include "progress.h"56struct idx_entry {7off_t offset;8union idx_entry_object {9const unsigned char *hash;10struct object_id *oid;11} oid;12unsigned int nr;13};1415static int compare_entries(const void *e1, const void *e2)16{17const struct idx_entry *entry1 = e1;18const struct idx_entry *entry2 = e2;19if (entry1->offset < entry2->offset)20return -1;21if (entry1->offset > entry2->offset)22return 1;23return 0;24}2526int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,27off_t offset, off_t len, unsigned int nr)28{29const uint32_t *index_crc;30uint32_t data_crc = crc32(0, NULL, 0);3132do {33unsigned long avail;34void *data = use_pack(p, w_curs, offset, &avail);35if (avail > len)36avail = len;37data_crc = crc32(data_crc, data, avail);38offset += avail;39len -= avail;40} while (len);4142index_crc = p->index_data;43index_crc += 2 + 256 + p->num_objects * (20/4) + nr;4445return data_crc != ntohl(*index_crc);46}4748static int verify_packfile(struct packed_git *p,49struct pack_window **w_curs,50verify_fn fn,51struct progress *progress, uint32_t base_count)5253{54off_t index_size = p->index_size;55const unsigned char *index_base = p->index_data;56git_SHA_CTX ctx;57unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;58off_t offset = 0, pack_sig_ofs = 0;59uint32_t nr_objects, i;60int err = 0;61struct idx_entry *entries;6263if (!is_pack_valid(p))64return error("packfile %s cannot be accessed", p->pack_name);6566git_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(hash, &ctx);78pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);79if (hashcmp(hash, 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 we88* 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;92ALLOC_ARRAY(entries, nr_objects + 1);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].oid.hash = nth_packed_object_sha1(p, i);97if (!entries[i].oid.hash)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, compare_entries);103104for (i = 0; i < nr_objects; i++) {105void *data;106enum object_type type;107unsigned long size;108off_t curpos;109int data_valid;110111if (p->index_version > 1) {112off_t offset = entries[i].offset;113off_t len = entries[i+1].offset - offset;114unsigned int nr = entries[i].nr;115if (check_pack_crc(p, w_curs, offset, len, nr))116err = error("index CRC mismatch for object %s "117"from %s at offset %"PRIuMAX"",118oid_to_hex(entries[i].oid.oid),119p->pack_name, (uintmax_t)offset);120}121122curpos = entries[i].offset;123type = unpack_object_header(p, w_curs, &curpos, &size);124unuse_pack(w_curs);125126if (type == OBJ_BLOB && big_file_threshold <= size) {127/*128* Let check_sha1_signature() check it with129* the streaming interface; no point slurping130* the data in-core only to discard.131*/132data = NULL;133data_valid = 0;134} else {135data = unpack_entry(p, entries[i].offset, &type, &size);136data_valid = 1;137}138139if (data_valid && !data)140err = error("cannot unpack %s from %s at offset %"PRIuMAX"",141oid_to_hex(entries[i].oid.oid), p->pack_name,142(uintmax_t)entries[i].offset);143else if (check_sha1_signature(entries[i].oid.hash, data, size, typename(type)))144err = error("packed %s from %s is corrupt",145oid_to_hex(entries[i].oid.oid), p->pack_name);146else if (fn) {147int eaten = 0;148err |= fn(entries[i].oid.oid, type, size, data, &eaten);149if (eaten)150data = NULL;151}152if (((base_count + i) & 1023) == 0)153display_progress(progress, base_count + i);154free(data);155156}157display_progress(progress, base_count + i);158free(entries);159160return err;161}162163int verify_pack_index(struct packed_git *p)164{165off_t index_size;166const unsigned char *index_base;167git_SHA_CTX ctx;168unsigned char sha1[20];169int err = 0;170171if (open_pack_index(p))172return error("packfile %s index not opened", p->pack_name);173index_size = p->index_size;174index_base = p->index_data;175176/* Verify SHA1 sum of the index file */177git_SHA1_Init(&ctx);178git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));179git_SHA1_Final(sha1, &ctx);180if (hashcmp(sha1, index_base + index_size - 20))181err = error("Packfile index for %s SHA1 mismatch",182p->pack_name);183return err;184}185186int verify_pack(struct packed_git *p, verify_fn fn,187struct progress *progress, uint32_t base_count)188{189int err = 0;190struct pack_window *w_curs = NULL;191192err |= verify_pack_index(p);193if (!p->index_data)194return -1;195196err |= verify_packfile(p, &w_curs, fn, progress, base_count);197unuse_pack(&w_curs);198199return err;200}