@matheusbsbs / matheus-bagliano / commits / 91ecbeca48
Fork 0

validate reused pack data with CRC when possible

This replaces the inflate validation with a CRC validation when reusing data from a pack which uses index version 2. That makes repacking much safer against corruptions, and it should be a bit faster too. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Apr 10, 2007 at 00:15 UTC 91ecbeca48e675fef8141451edb2c86577f9d63c
1 file changed +34 -12
builtin-pack-objects.c
+34 -12
@@ -233,12 +233,6 @@ static struct revindex_entry * find_packed_object(struct packed_git *p,
233 die("internal error: pack revindex corrupt");
234 }
235
236 -static off_t find_packed_object_size(struct packed_git *p, off_t ofs)
237 -{
238 - struct revindex_entry *entry = find_packed_object(p, ofs);
239 - return entry[1].offset - ofs;
240 -}
241 -
236 static const unsigned char *find_packed_object_name(struct packed_git *p,
237 off_t ofs)
238 {
@@ -321,6 +315,28 @@ static int check_pack_inflate(struct packed_git *p,
315 stream.total_in == len) ? 0 : -1;
316 }
317
318 +static int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
319 + off_t offset, off_t len, unsigned int nr)
320 +{
321 + const uint32_t *index_crc;
322 + uint32_t data_crc = crc32(0, Z_NULL, 0);
323 +
324 + do {
325 + unsigned int avail;
326 + void *data = use_pack(p, w_curs, offset, &avail);
327 + if (avail > len)
328 + avail = len;
329 + data_crc = crc32(data_crc, data, avail);
330 + offset += avail;
331 + len -= avail;
332 + } while (len);
333 +
334 + index_crc = p->index_data;
335 + index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
336 +
337 + return data_crc != ntohl(*index_crc);
338 +}
339 +
340 static void copy_pack_data(struct sha1file *f,
341 struct packed_git *p,
342 struct pack_window **w_curs,
@@ -485,6 +501,7 @@ static unsigned long write_object(struct sha1file *f,
501 else {
502 struct packed_git *p = entry->in_pack;
503 struct pack_window *w_curs = NULL;
504 + struct revindex_entry *revidx;
505 off_t offset;
506
507 if (entry->delta) {
@@ -507,12 +524,17 @@ static unsigned long write_object(struct sha1file *f,
524 hdrlen += 20;
525 }
526
510 - offset = entry->in_pack_offset + entry->in_pack_header_size;
511 - datalen = find_packed_object_size(p, entry->in_pack_offset)
512 - - entry->in_pack_header_size;
513 - if (!pack_to_stdout && check_pack_inflate(p, &w_curs,
514 - offset, datalen, entry->size))
515 - die("corrupt delta in pack %s", sha1_to_hex(entry->sha1));
527 + offset = entry->in_pack_offset;
528 + revidx = find_packed_object(p, offset);
529 + datalen = revidx[1].offset - offset;
530 + if (!pack_to_stdout && p->index_version > 1 &&
531 + check_pack_crc(p, &w_curs, offset, datalen, revidx->nr))
532 + die("bad packed object CRC for %s", sha1_to_hex(entry->sha1));
533 + offset += entry->in_pack_header_size;
534 + datalen -= entry->in_pack_header_size;
535 + if (!pack_to_stdout && p->index_version == 1 &&
536 + check_pack_inflate(p, &w_curs, offset, datalen, entry->size))
537 + die("corrupt packed object for %s", sha1_to_hex(entry->sha1));
538 copy_pack_data(f, p, &w_curs, offset, datalen);
539 unuse_pack(&w_curs);
540 reused++;