346
}
347
}
348
349
-static int check_loose_inflate(unsigned char *data, unsigned long len, unsigned long expect)
350
-{
351
- z_stream stream;
352
- unsigned char fakebuf[4096];
353
- int st;
354
-
355
- memset(&stream, 0, sizeof(stream));
356
- stream.next_in = data;
357
- stream.avail_in = len;
358
- stream.next_out = fakebuf;
359
- stream.avail_out = sizeof(fakebuf);
360
- inflateInit(&stream);
361
-
362
- while (1) {
363
- st = inflate(&stream, Z_FINISH);
364
- if (st == Z_STREAM_END || st == Z_OK) {
365
- st = (stream.total_out == expect &&
366
- stream.total_in == len) ? 0 : -1;
367
- break;
368
- }
369
- if (st != Z_BUF_ERROR) {
370
- st = -1;
371
- break;
372
- }
373
- stream.next_out = fakebuf;
374
- stream.avail_out = sizeof(fakebuf);
375
- }
376
- inflateEnd(&stream);
377
- return st;
378
-}
379
-
380
-static int revalidate_loose_object(struct object_entry *entry,
381
- unsigned char *map,
382
- unsigned long mapsize)
383
-{
384
- /* we already know this is a loose object with new type header. */
385
- enum object_type type;
386
- unsigned long size, used;
387
-
388
- if (pack_to_stdout)
389
- return 0;
390
-
391
- used = unpack_object_header_gently(map, mapsize, &type, &size);
392
- if (!used)
393
- return -1;
394
- map += used;
395
- mapsize -= used;
396
- return check_loose_inflate(map, mapsize, size);
397
-}
398
-
349
static unsigned long write_object(struct sha1file *f,
350
struct object_entry *entry)
351
{
377
* and we do not need to deltify it.
378
*/
379
430
- if (!no_reuse_object && !entry->in_pack && !entry->delta) {
431
- unsigned char *map;
432
- unsigned long mapsize;
433
- map = map_sha1_file(entry->sha1, &mapsize);
434
- if (map && !legacy_loose_object(map)) {
435
- /* We can copy straight into the pack file */
436
- if (revalidate_loose_object(entry, map, mapsize))
437
- die("corrupt loose object %s",
438
- sha1_to_hex(entry->sha1));
439
- sha1write(f, map, mapsize);
440
- munmap(map, mapsize);
441
- written++;
442
- reused++;
443
- return mapsize;
444
- }
445
- if (map)
446
- munmap(map, mapsize);
447
- }
448
-
380
if (!to_reuse) {
381
buf = read_sha1_file(entry->sha1, &type, &size);
382
if (!buf)