684
return map;
685
}
686
687
-static int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
687
+static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
688
{
689
+ unsigned char c;
690
+ unsigned int word, bits;
691
+ unsigned long size;
692
+ static const char *typename[8] = {
693
+ NULL, /* OBJ_EXT */
694
+ "commit", "tree", "blob", "tag",
695
+ NULL, NULL, NULL
696
+ };
697
+ const char *type;
698
+
699
/* Get the data stream */
700
memset(stream, 0, sizeof(*stream));
701
stream->next_in = map;
702
stream->avail_in = mapsize;
703
stream->next_out = buffer;
694
- stream->avail_out = size;
704
+ stream->avail_out = bufsiz;
705
+
706
+ /*
707
+ * Is it a zlib-compressed buffer? If so, the first byte
708
+ * must be 0x78 (15-bit window size, deflated), and the
709
+ * first 16-bit word is evenly divisible by 31
710
+ */
711
+ word = (map[0] << 8) + map[1];
712
+ if (map[0] == 0x78 && !(word % 31)) {
713
+ inflateInit(stream);
714
+ return inflate(stream, 0);
715
+ }
716
+
717
+ c = *map++;
718
+ mapsize--;
719
+ type = typename[(c >> 4) & 7];
720
+ if (!type)
721
+ return -1;
722
+
723
+ bits = 4;
724
+ size = c & 0xf;
725
+ while ((c & 0x80)) {
726
+ if (bits >= 8*sizeof(long))
727
+ return -1;
728
+ c = *map++;
729
+ size += (c & 0x7f) << bits;
730
+ bits += 7;
731
+ mapsize--;
732
+ }
733
734
+ /* Set up the stream for the rest.. */
735
+ stream->next_in = map;
736
+ stream->avail_in = mapsize;
737
inflateInit(stream);
697
- return inflate(stream, 0);
738
+
739
+ /* And generate the fake traditional header */
740
+ stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu", type, size);
741
+ return 0;
742
}
743
744
static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
745
{
746
int bytes = strlen(buffer) + 1;
747
unsigned char *buf = xmalloc(1+size);
748
+ unsigned long n;
749
705
- memcpy(buf, (char *) buffer + bytes, stream->total_out - bytes);
706
- bytes = stream->total_out - bytes;
750
+ n = stream->total_out - bytes;
751
+ if (n > size)
752
+ n = size;
753
+ memcpy(buf, (char *) buffer + bytes, n);
754
+ bytes = n;
755
if (bytes < size) {
756
stream->next_out = buf + bytes;
757
stream->avail_out = size - bytes;
1462
return 0;
1463
}
1464
1465
+static int write_binary_header(unsigned char *hdr, enum object_type type, unsigned long len)
1466
+{
1467
+ int hdr_len;
1468
+ unsigned char c;
1469
+
1470
+ c = (type << 4) | (len & 15);
1471
+ len >>= 4;
1472
+ hdr_len = 1;
1473
+ while (len) {
1474
+ *hdr++ = c | 0x80;
1475
+ hdr_len++;
1476
+ c = (len & 0x7f);
1477
+ len >>= 7;
1478
+ }
1479
+ *hdr = c;
1480
+ return hdr_len;
1481
+}
1482
+
1483
+static void setup_object_header(z_stream *stream, const char *type, unsigned long len)
1484
+{
1485
+ int obj_type, hdr;
1486
+
1487
+ if (use_legacy_headers) {
1488
+ while (deflate(stream, 0) == Z_OK)
1489
+ /* nothing */;
1490
+ return;
1491
+ }
1492
+ if (!strcmp(type, blob_type))
1493
+ obj_type = OBJ_BLOB;
1494
+ else if (!strcmp(type, tree_type))
1495
+ obj_type = OBJ_TREE;
1496
+ else if (!strcmp(type, commit_type))
1497
+ obj_type = OBJ_COMMIT;
1498
+ else if (!strcmp(type, tag_type))
1499
+ obj_type = OBJ_TAG;
1500
+ else
1501
+ die("trying to generate bogus object of type '%s'", type);
1502
+ hdr = write_binary_header(stream->next_out, obj_type, len);
1503
+ stream->total_out = hdr;
1504
+ stream->next_out += hdr;
1505
+ stream->avail_out -= hdr;
1506
+}
1507
+
1508
int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1509
{
1510
int size;
1550
/* Set it up */
1551
memset(&stream, 0, sizeof(stream));
1552
deflateInit(&stream, zlib_compression_level);
1462
- size = deflateBound(&stream, len+hdrlen);
1553
+ size = 8 + deflateBound(&stream, len+hdrlen);
1554
compressed = xmalloc(size);
1555
1556
/* Compress it */
1560
/* First header.. */
1561
stream.next_in = hdr;
1562
stream.avail_in = hdrlen;
1472
- while (deflate(&stream, 0) == Z_OK)
1473
- /* nothing */;
1563
+ setup_object_header(&stream, type, len);
1564
1565
/* Then the data itself.. */
1566
stream.next_in = buf;