10
#include "cache.h"
11
#include "quote.h"
12
#include "blob.h"
13
+#include "delta.h"
14
15
// --check turns on checking that the working tree matches the
16
// files that are being modified, but doesn't apply the patch
967
patch->old_mode != patch->new_mode);
968
}
969
970
+static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
971
+{
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
+ *
976
+ * Each 5-byte sequence of base-85 encodes up to 4 bytes,
977
+ * and we would limit the patch line to 66 characters,
978
+ * so one line can fit up to 13 groups that would decode
979
+ * to 52 bytes max. The length byte 'A'-'Z' corresponds
980
+ * to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
981
+ * The end of binary is signalled with an empty line.
982
+ */
983
+ int llen, used;
984
+ struct fragment *fragment;
985
+ char *delta = NULL;
986
+
987
+ patch->is_binary = 1;
988
+ patch->fragments = fragment = xcalloc(1, sizeof(*fragment));
989
+ used = 0;
990
+ while (1) {
991
+ int byte_length, max_byte_length, newsize;
992
+ llen = linelen(buffer, size);
993
+ used += llen;
994
+ linenr++;
995
+ if (llen == 1)
996
+ break;
997
+ /* Minimum line is "A00000\n" which is 7-byte long,
998
+ * and the line length must be multiple of 5 plus 2.
999
+ */
1000
+ if ((llen < 7) || (llen-2) % 5)
1001
+ goto corrupt;
1002
+ max_byte_length = (llen - 2) / 5 * 4;
1003
+ byte_length = *buffer;
1004
+ if ('A' <= byte_length && byte_length <= 'Z')
1005
+ byte_length = byte_length - 'A' + 1;
1006
+ else if ('a' <= byte_length && byte_length <= 'z')
1007
+ byte_length = byte_length - 'a' + 27;
1008
+ else
1009
+ goto corrupt;
1010
+ /* if the input length was not multiple of 4, we would
1011
+ * have filler at the end but the filler should never
1012
+ * exceed 3 bytes
1013
+ */
1014
+ if (max_byte_length < byte_length ||
1015
+ byte_length <= max_byte_length - 4)
1016
+ goto corrupt;
1017
+ newsize = fragment->size + byte_length;
1018
+ delta = xrealloc(delta, newsize);
1019
+ if (decode_85(delta + fragment->size,
1020
+ buffer + 1,
1021
+ byte_length))
1022
+ goto corrupt;
1023
+ fragment->size = newsize;
1024
+ buffer += llen;
1025
+ size -= llen;
1026
+ }
1027
+ fragment->patch = delta;
1028
+ return used;
1029
+ corrupt:
1030
+ return error("corrupt binary patch at line %d: %.*s",
1031
+ linenr-1, llen-1, buffer);
1032
+}
1033
+
1034
static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1035
{
1036
int hdrsize, patchsize;
1047
"Files ",
1048
NULL,
1049
};
1050
+ static const char git_binary[] = "GIT binary patch\n";
1051
int i;
1052
int hd = hdrsize + offset;
1053
unsigned long llen = linelen(buffer + hd, size - hd);
1054
989
- if (!memcmp(" differ\n", buffer + hd + llen - 8, 8))
1055
+ if (llen == sizeof(git_binary) - 1 &&
1056
+ !memcmp(git_binary, buffer + hd, llen)) {
1057
+ int used;
1058
+ linenr++;
1059
+ used = parse_binary(buffer + hd + llen,
1060
+ size - hd - llen, patch);
1061
+ if (used)
1062
+ patchsize = used + llen;
1063
+ else
1064
+ patchsize = 0;
1065
+ }
1066
+ else if (!memcmp(" differ\n", buffer + hd + llen - 8, 8)) {
1067
for (i = 0; binhdr[i]; i++) {
1068
int len = strlen(binhdr[i]);
1069
if (len < size - hd &&
1070
!memcmp(binhdr[i], buffer + hd, len)) {
1071
+ linenr++;
1072
patch->is_binary = 1;
1073
+ patchsize = llen;
1074
break;
1075
}
1076
}
1077
+ }
1078
1079
/* Empty patch cannot be applied if:
1080
* - it is a binary patch and we do not do binary_replace, or
1425
return offset;
1426
}
1427
1348
-static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1428
+static int apply_binary(struct buffer_desc *desc, struct patch *patch)
1429
{
1350
- struct fragment *frag = patch->fragments;
1430
const char *name = patch->old_name ? patch->old_name : patch->new_name;
1431
+ unsigned char sha1[20];
1432
+ unsigned char hdr[50];
1433
+ int hdrlen;
1434
1353
- if (patch->is_binary) {
1354
- unsigned char sha1[20];
1435
+ if (!allow_binary_replacement)
1436
+ return error("cannot apply binary patch to '%s' "
1437
+ "without --allow-binary-replacement",
1438
+ name);
1439
1356
- if (!allow_binary_replacement)
1357
- return error("cannot apply binary patch to '%s' "
1358
- "without --allow-binary-replacement",
1359
- name);
1440
+ /* For safety, we require patch index line to contain
1441
+ * full 40-byte textual SHA1 for old and new, at least for now.
1442
+ */
1443
+ if (strlen(patch->old_sha1_prefix) != 40 ||
1444
+ strlen(patch->new_sha1_prefix) != 40 ||
1445
+ get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1446
+ get_sha1_hex(patch->new_sha1_prefix, sha1))
1447
+ return error("cannot apply binary patch to '%s' "
1448
+ "without full index line", name);
1449
1361
- /* For safety, we require patch index line to contain
1362
- * full 40-byte textual SHA1 for old and new, at least for now.
1450
+ if (patch->old_name) {
1451
+ /* See if the old one matches what the patch
1452
+ * applies to.
1453
*/
1364
- if (strlen(patch->old_sha1_prefix) != 40 ||
1365
- strlen(patch->new_sha1_prefix) != 40 ||
1366
- get_sha1_hex(patch->old_sha1_prefix, sha1) ||
1367
- get_sha1_hex(patch->new_sha1_prefix, sha1))
1368
- return error("cannot apply binary patch to '%s' "
1369
- "without full index line", name);
1370
-
1371
- if (patch->old_name) {
1372
- unsigned char hdr[50];
1373
- int hdrlen;
1374
-
1375
- /* See if the old one matches what the patch
1376
- * applies to.
1377
- */
1378
- write_sha1_file_prepare(desc->buffer, desc->size,
1379
- blob_type, sha1, hdr, &hdrlen);
1380
- if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1381
- return error("the patch applies to '%s' (%s), "
1382
- "which does not match the "
1383
- "current contents.",
1384
- name, sha1_to_hex(sha1));
1385
- }
1386
- else {
1387
- /* Otherwise, the old one must be empty. */
1388
- if (desc->size)
1389
- return error("the patch applies to an empty "
1390
- "'%s' but it is not empty", name);
1391
- }
1454
+ write_sha1_file_prepare(desc->buffer, desc->size,
1455
+ blob_type, sha1, hdr, &hdrlen);
1456
+ if (strcmp(sha1_to_hex(sha1), patch->old_sha1_prefix))
1457
+ return error("the patch applies to '%s' (%s), "
1458
+ "which does not match the "
1459
+ "current contents.",
1460
+ name, sha1_to_hex(sha1));
1461
+ }
1462
+ else {
1463
+ /* Otherwise, the old one must be empty. */
1464
+ if (desc->size)
1465
+ return error("the patch applies to an empty "
1466
+ "'%s' but it is not empty", name);
1467
+ }
1468
+
1469
+ if (desc->buffer) {
1470
+ free(desc->buffer);
1471
+ desc->alloc = desc->size = 0;
1472
+ }
1473
+ get_sha1_hex(patch->new_sha1_prefix, sha1);
1474
+ if (!memcmp(sha1, null_sha1, 20))
1475
+ return 0; /* deletion patch */
1476
+
1477
+ if (has_sha1_file(sha1)) {
1478
+ char type[10];
1479
+ unsigned long size;
1480
1393
- /* For now, we do not record post-image data in the patch,
1394
- * and require the object already present in the recipient's
1395
- * object database.
1481
+ desc->buffer = read_sha1_file(sha1, type, &size);
1482
+ if (!desc->buffer)
1483
+ return error("the necessary postimage %s for "
1484
+ "'%s' cannot be read",
1485
+ patch->new_sha1_prefix, name);
1486
+ desc->alloc = desc->size = size;
1487
+ }
1488
+ 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.
1502
*/
1397
- if (desc->buffer) {
1398
- free(desc->buffer);
1399
- desc->alloc = desc->size = 0;
1400
- }
1401
- get_sha1_hex(patch->new_sha1_prefix, sha1);
1402
-
1403
- if (memcmp(sha1, null_sha1, 20)) {
1404
- char type[10];
1405
- unsigned long size;
1406
-
1407
- desc->buffer = read_sha1_file(sha1, type, &size);
1408
- if (!desc->buffer)
1409
- return error("the necessary postimage %s for "
1410
- "'%s' does not exist",
1411
- patch->new_sha1_prefix, name);
1412
- desc->alloc = desc->size = size;
1413
- }
1503
+ desc->buffer = patch_delta(src, src_size,
1504
+ (void*) patch->fragments->patch,
1505
+ patch->fragments->size,
1506
+ &dst_size);
1507
+ if (!desc->buffer)
1508
+ return error("binary patch does not apply to '%s'",
1509
+ name);
1510
+ desc->size = desc->alloc = dst_size;
1511
1415
- return 0;
1512
+ /* verify that the result matches */
1513
+ write_sha1_file_prepare(desc->buffer, desc->size, blob_type,
1514
+ sha1, hdr, &hdrlen);
1515
+ if (strcmp(sha1_to_hex(sha1), patch->new_sha1_prefix))
1516
+ return error("binary patch to '%s' creates incorrect result", name);
1517
}
1518
1519
+ return 0;
1520
+}
1521
+
1522
+static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1523
+{
1524
+ struct fragment *frag = patch->fragments;
1525
+ const char *name = patch->old_name ? patch->old_name : patch->new_name;
1526
+
1527
+ if (patch->is_binary)
1528
+ return apply_binary(desc, patch);
1529
+
1530
while (frag) {
1531
if (apply_one_fragment(desc, frag) < 0)
1532
return error("patch failed: %s:%ld",