114
char *new_name, *old_name, *def_name;
115
unsigned int old_mode, new_mode;
116
int is_rename, is_copy, is_new, is_delete, is_binary;
117
+#define BINARY_DELTA_DEFLATED 1
118
+#define BINARY_LITERAL_DEFLATED 2
119
+ unsigned long deflate_origlen;
120
int lines_added, lines_deleted;
121
int score;
122
struct fragment *fragments;
972
973
static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
974
{
972
- /* We have read "GIT binary patch\n"; what follows is a
973
- * sequence of 'length-byte' followed by base-85 encoded
974
- * delta data.
975
+ /* We have read "GIT binary patch\n"; what follows is a line
976
+ * that says the patch method (currently, either "deflated
977
+ * literal" or "deflated delta") and the length of data before
978
+ * deflating; a sequence of 'length-byte' followed by base-85
979
+ * encoded data follows.
980
*
981
* Each 5-byte sequence of base-85 encodes up to 4 bytes,
982
* and we would limit the patch line to 66 characters,
987
*/
988
int llen, used;
989
struct fragment *fragment;
985
- char *delta = NULL;
990
+ char *data = NULL;
991
987
- patch->is_binary = 1;
992
patch->fragments = fragment = xcalloc(1, sizeof(*fragment));
989
- used = 0;
993
+
994
+ /* Grab the type of patch */
995
+ llen = linelen(buffer, size);
996
+ used = llen;
997
+ linenr++;
998
+
999
+ if (!strncmp(buffer, "delta ", 6)) {
1000
+ patch->is_binary = BINARY_DELTA_DEFLATED;
1001
+ patch->deflate_origlen = strtoul(buffer + 6, NULL, 10);
1002
+ }
1003
+ else if (!strncmp(buffer, "literal ", 8)) {
1004
+ patch->is_binary = BINARY_LITERAL_DEFLATED;
1005
+ patch->deflate_origlen = strtoul(buffer + 8, NULL, 10);
1006
+ }
1007
+ else
1008
+ return error("unrecognized binary patch at line %d: %.*s",
1009
+ linenr-1, llen-1, buffer);
1010
+ buffer += llen;
1011
while (1) {
1012
int byte_length, max_byte_length, newsize;
1013
llen = linelen(buffer, size);
1036
byte_length <= max_byte_length - 4)
1037
goto corrupt;
1038
newsize = fragment->size + byte_length;
1018
- delta = xrealloc(delta, newsize);
1019
- if (decode_85(delta + fragment->size,
1039
+ data = xrealloc(data, newsize);
1040
+ if (decode_85(data + fragment->size,
1041
buffer + 1,
1042
byte_length))
1043
goto corrupt;
1045
buffer += llen;
1046
size -= llen;
1047
}
1027
- fragment->patch = delta;
1048
+ fragment->patch = data;
1049
return used;
1050
corrupt:
1051
return error("corrupt binary patch at line %d: %.*s",
1446
return offset;
1447
}
1448
1449
+static char *inflate_it(const void *data, unsigned long size,
1450
+ unsigned long inflated_size)
1451
+{
1452
+ z_stream stream;
1453
+ void *out;
1454
+ int st;
1455
+
1456
+ memset(&stream, 0, sizeof(stream));
1457
+
1458
+ stream.next_in = (unsigned char *)data;
1459
+ stream.avail_in = size;
1460
+ stream.next_out = out = xmalloc(inflated_size);
1461
+ stream.avail_out = inflated_size;
1462
+ inflateInit(&stream);
1463
+ st = inflate(&stream, Z_FINISH);
1464
+ if ((st != Z_STREAM_END) || stream.total_out != inflated_size) {
1465
+ free(out);
1466
+ return NULL;
1467
+ }
1468
+ return out;
1469
+}
1470
+
1471
+static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
1472
+{
1473
+ unsigned long dst_size;
1474
+ struct fragment *fragment = patch->fragments;
1475
+ void *data;
1476
+ void *result;
1477
+
1478
+ data = inflate_it(fragment->patch, fragment->size,
1479
+ patch->deflate_origlen);
1480
+ if (!data)
1481
+ return error("corrupt patch data");
1482
+ switch (patch->is_binary) {
1483
+ case BINARY_DELTA_DEFLATED:
1484
+ result = patch_delta(desc->buffer, desc->size,
1485
+ data,
1486
+ patch->deflate_origlen,
1487
+ &dst_size);
1488
+ free(desc->buffer);
1489
+ desc->buffer = result;
1490
+ free(data);
1491
+ break;
1492
+ case BINARY_LITERAL_DEFLATED:
1493
+ free(desc->buffer);
1494
+ desc->buffer = data;
1495
+ dst_size = patch->deflate_origlen;
1496
+ break;
1497
+ }
1498
+ if (!desc->buffer)
1499
+ return -1;
1500
+ desc->size = desc->alloc = dst_size;
1501
+ return 0;
1502
+}
1503
+
1504
static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1505
{
1506
const char *name = patch->old_name ? patch->old_name : patch->new_name;
1542
"'%s' but it is not empty", name);
1543
}
1544
1469
- if (desc->buffer) {
1545
+ get_sha1_hex(patch->new_sha1_prefix, sha1);
1546
+ if (!memcmp(sha1, null_sha1, 20)) {
1547
free(desc->buffer);
1548
desc->alloc = desc->size = 0;
1472
- }
1473
- get_sha1_hex(patch->new_sha1_prefix, sha1);
1474
- if (!memcmp(sha1, null_sha1, 20))
1549
+ desc->buffer = NULL;
1550
return 0; /* deletion patch */
1551
+ }
1552
1553
if (has_sha1_file(sha1)) {
1554
+ /* We already have the postimage */
1555
char type[10];
1556
unsigned long size;
1557
1558
+ free(desc->buffer);
1559
desc->buffer = read_sha1_file(sha1, type, &size);
1560
if (!desc->buffer)
1561
return error("the necessary postimage %s for "
1564
desc->alloc = desc->size = size;
1565
}
1566
else {
1489
- char type[10];
1490
- unsigned long src_size, dst_size;
1491
- void *src;
1492
-
1493
- get_sha1_hex(patch->old_sha1_prefix, sha1);
1494
- src = read_sha1_file(sha1, type, &src_size);
1495
- if (!src)
1496
- return error("the necessary preimage %s for "
1497
- "'%s' cannot be read",
1498
- patch->old_sha1_prefix, name);
1499
-
1500
- /* patch->fragment->patch has the delta data and
1501
- * we should apply it to the preimage.
1567
+ /* We have verified desc matches the preimage;
1568
+ * apply the patch data to it, which is stored
1569
+ * in the patch->fragments->{patch,size}.
1570
*/
1503
- desc->buffer = patch_delta(src, src_size,
1504
- (void*) patch->fragments->patch,
1505
- patch->fragments->size,
1506
- &dst_size);
1507
- if (!desc->buffer)
1571
+ if (apply_binary_fragment(desc, patch))
1572
return error("binary patch does not apply to '%s'",
1573
name);
1510
- desc->size = desc->alloc = dst_size;
1574
1575
/* verify that the result matches */
1576
write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
2165
diffstat = 1;
2166
continue;
2167
}
2105
- if (!strcmp(arg, "--allow-binary-replacement")) {
2168
+ if (!strcmp(arg, "--allow-binary-replacement") ||
2169
+ !strcmp(arg, "--binary")) {
2170
allow_binary_replacement = 1;
2171
continue;
2172
}