Browse Source
* jc/bindiff: improve base85 generated assembly code binary diff and apply: testsuite. binary diff: further updates. binary patch.maint
Junio C Hamano
19 years ago
8 changed files with 575 additions and 69 deletions
@ -0,0 +1,140 @@
@@ -0,0 +1,140 @@
|
||||
#include "cache.h" |
||||
|
||||
#undef DEBUG_85 |
||||
|
||||
#ifdef DEBUG_85 |
||||
#define say(a) fprintf(stderr, a) |
||||
#define say1(a,b) fprintf(stderr, a, b) |
||||
#define say2(a,b,c) fprintf(stderr, a, b, c) |
||||
#else |
||||
#define say(a) do {} while(0) |
||||
#define say1(a,b) do {} while(0) |
||||
#define say2(a,b,c) do {} while(0) |
||||
#endif |
||||
|
||||
static const char en85[] = { |
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
||||
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', |
||||
'U', 'V', 'W', 'X', 'Y', 'Z', |
||||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', |
||||
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', |
||||
'u', 'v', 'w', 'x', 'y', 'z', |
||||
'!', '#', '$', '%', '&', '(', ')', '*', '+', '-', |
||||
';', '<', '=', '>', '?', '@', '^', '_', '`', '{', |
||||
'|', '}', '~' |
||||
}; |
||||
|
||||
static char de85[256]; |
||||
static void prep_base85(void) |
||||
{ |
||||
int i; |
||||
if (de85['Z']) |
||||
return; |
||||
for (i = 0; i < ARRAY_SIZE(en85); i++) { |
||||
int ch = en85[i]; |
||||
de85[ch] = i + 1; |
||||
} |
||||
} |
||||
|
||||
int decode_85(char *dst, char *buffer, int len) |
||||
{ |
||||
prep_base85(); |
||||
|
||||
say2("decode 85 <%.*s>", len/4*5, buffer); |
||||
while (len) { |
||||
unsigned acc = 0; |
||||
int de, cnt = 4; |
||||
unsigned char ch; |
||||
do { |
||||
ch = *buffer++; |
||||
de = de85[ch]; |
||||
if (--de < 0) |
||||
return error("invalid base85 alphabet %c", ch); |
||||
acc = acc * 85 + de; |
||||
} while (--cnt); |
||||
ch = *buffer++; |
||||
de = de85[ch]; |
||||
if (--de < 0) |
||||
return error("invalid base85 alphabet %c", ch); |
||||
/* |
||||
* Detect overflow. The largest |
||||
* 5-letter possible is "|NsC0" to |
||||
* encode 0xffffffff, and "|NsC" gives |
||||
* 0x03030303 at this point (i.e. |
||||
* 0xffffffff = 0x03030303 * 85). |
||||
*/ |
||||
if (0x03030303 < acc || |
||||
0xffffffff - de < (acc *= 85)) |
||||
error("invalid base85 sequence %.5s", buffer-5); |
||||
acc += de; |
||||
say1(" %08x", acc); |
||||
|
||||
cnt = (len < 4) ? len : 4; |
||||
len -= cnt; |
||||
do { |
||||
acc = (acc << 8) | (acc >> 24); |
||||
*dst++ = acc; |
||||
} while (--cnt); |
||||
} |
||||
say("\n"); |
||||
|
||||
return 0; |
||||
} |
||||
|
||||
void encode_85(char *buf, unsigned char *data, int bytes) |
||||
{ |
||||
prep_base85(); |
||||
|
||||
say("encode 85"); |
||||
while (bytes) { |
||||
unsigned acc = 0; |
||||
int cnt; |
||||
for (cnt = 24; cnt >= 0; cnt -= 8) { |
||||
int ch = *data++; |
||||
acc |= ch << cnt; |
||||
if (--bytes == 0) |
||||
break; |
||||
} |
||||
say1(" %08x", acc); |
||||
for (cnt = 4; cnt >= 0; cnt--) { |
||||
int val = acc % 85; |
||||
acc /= 85; |
||||
buf[cnt] = en85[val]; |
||||
} |
||||
buf += 5; |
||||
} |
||||
say("\n"); |
||||
|
||||
*buf = 0; |
||||
} |
||||
|
||||
#ifdef DEBUG_85 |
||||
int main(int ac, char **av) |
||||
{ |
||||
char buf[1024]; |
||||
|
||||
if (!strcmp(av[1], "-e")) { |
||||
int len = strlen(av[2]); |
||||
encode_85(buf, av[2], len); |
||||
if (len <= 26) len = len + 'A' - 1; |
||||
else len = len + 'a' - 26 + 1; |
||||
printf("encoded: %c%s\n", len, buf); |
||||
return 0; |
||||
} |
||||
if (!strcmp(av[1], "-d")) { |
||||
int len = *av[2]; |
||||
if ('A' <= len && len <= 'Z') len = len - 'A' + 1; |
||||
else len = len - 'a' + 26 + 1; |
||||
decode_85(buf, av[2]+1, len); |
||||
printf("decoded: %.*s\n", len, buf); |
||||
return 0; |
||||
} |
||||
if (!strcmp(av[1], "-t")) { |
||||
char t[4] = { -1,-1,-1,-1 }; |
||||
encode_85(buf, t, 4); |
||||
printf("encoded: D%s\n", buf); |
||||
return 0; |
||||
} |
||||
} |
||||
#endif |
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
#!/bin/sh |
||||
# |
||||
# Copyright (c) 2006 Junio C Hamano |
||||
# |
||||
|
||||
test_description='Binary diff and apply |
||||
' |
||||
|
||||
. ./test-lib.sh |
||||
|
||||
test_expect_success 'prepare repository' \ |
||||
'echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d && |
||||
git-update-index --add a b c d && |
||||
echo git >a && |
||||
cat ../test4012.png >b && |
||||
echo git >c && |
||||
cat b b >d' |
||||
|
||||
test_expect_success 'diff without --binary' \ |
||||
'git-diff | git-apply --stat --summary >current && |
||||
cmp current - <<\EOF |
||||
a | 2 +- |
||||
b | Bin |
||||
c | 2 +- |
||||
d | Bin |
||||
4 files changed, 2 insertions(+), 2 deletions(-) |
||||
EOF' |
||||
|
||||
test_expect_success 'diff with --binary' \ |
||||
'git-diff --binary | git-apply --stat --summary >current && |
||||
cmp current - <<\EOF |
||||
a | 2 +- |
||||
b | Bin |
||||
c | 2 +- |
||||
d | Bin |
||||
4 files changed, 2 insertions(+), 2 deletions(-) |
||||
EOF' |
||||
|
||||
# apply needs to be able to skip the binary material correctly |
||||
# in order to report the line number of a corrupt patch. |
||||
test_expect_success 'apply detecting corrupt patch correctly' \ |
||||
'git-diff | sed -e 's/-CIT/xCIT/' >broken && |
||||
if git-apply --stat --summary broken 2>detected |
||||
then |
||||
echo unhappy - should have detected an error |
||||
(exit 1) |
||||
else |
||||
echo happy |
||||
fi && |
||||
detected=`cat detected` && |
||||
detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` && |
||||
detected=`sed -ne "${detected}p" broken` && |
||||
test "$detected" = xCIT' |
||||
|
||||
test_expect_success 'apply detecting corrupt patch correctly' \ |
||||
'git-diff --binary | sed -e 's/-CIT/xCIT/' >broken && |
||||
if git-apply --stat --summary broken 2>detected |
||||
then |
||||
echo unhappy - should have detected an error |
||||
(exit 1) |
||||
else |
||||
echo happy |
||||
fi && |
||||
detected=`cat detected` && |
||||
detected=`expr "$detected" : "fatal.*at line \\([0-9]*\\)\$"` && |
||||
detected=`sed -ne "${detected}p" broken` && |
||||
test "$detected" = xCIT' |
||||
|
||||
test_expect_success 'initial commit' 'git-commit -a -m initial' |
||||
|
||||
# Try removal (b), modification (d), and creation (e). |
||||
test_expect_success 'diff-index with --binary' \ |
||||
'echo AIT >a && mv b e && echo CIT >c && cat e >d && |
||||
git-update-index --add --remove a b c d e && |
||||
tree0=`git-write-tree` && |
||||
git-diff --cached --binary >current && |
||||
git-apply --stat --summary current' |
||||
|
||||
test_expect_success 'apply binary patch' \ |
||||
'git-reset --hard && |
||||
git-apply --binary --index <current && |
||||
tree1=`git-write-tree` && |
||||
test "$tree1" = "$tree0"' |
||||
|
||||
test_done |
After Width: | Height: | Size: 5.5 KiB |
Loading…
Reference in new issue