-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipsdiff.c
169 lines (147 loc) · 2.69 KB
/
ipsdiff.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <u.h>
#include <libc.h>
#include <bio.h>
#define PUT2(p, u) (p)[0] = (u)>>8, (p)[1] = (u)
#define PUT3(p, u) (p)[0] = (u)>>16, (p)[1] = (u)>>8, (p)[2] = (u)
Biobuf *ips;
typedef struct Bin Bin;
struct Bin{
int fd;
long dot;
long n;
uchar data[0xFFFF];
};
static Bin base, modded;
static int dump = 0;
static long
slurp(Bin *b)
{
b->n = readn(b->fd, b->data, sizeof b->data);
if(b->n < 0)
sysfatal("read: %r");
return b->n;
}
static void
emit(u32int off, uchar *data, u16int size)
{
uchar buf[3 + 2];
uchar buf2[2 + 1];
uchar v, *p, *e;
if(size == 0)
return;
PUT3(buf, off);
if(size > 3 && data[0] == data[1]){
p = data + 2;
e = data + size;
v = data[0];
while(p < e && *p == v)
p++;
PUT2(buf2, size);
if(p == e)
size = 0;
buf2[2] = v;
}
PUT2(buf+3, size);
if(dump)
fprint(2, "%ud %ud\n", off, size);
if(Bwrite(ips, buf, sizeof buf) != sizeof buf)
sysfatal("Bwrite: %r");
if(size == 0 && Bwrite(ips, buf2, sizeof buf2) != sizeof buf2)
sysfatal("Bwrite: %r");
if(size != 0 && Bwrite(ips, data, size) != size)
sysfatal("Bwrite: %r");
}
static void
RLEpass(u32int base, uchar *p, uchar *e)
{
uchar *u, *uu;
uchar *start;
uchar v;
if(e - p <= 8){
emit(base, p, e - p);
return;
}
start = p;
for(u = p; u < e; u = uu){
v = *u;
for(uu = u+1; uu < e && *uu == v; uu++)
;
if(uu - u < 8)
continue;
if(u > p)
emit(base + (p - start), p, u - p);
emit(base + (u - start), u, uu - u);
p = uu;
}
if(u > p)
emit(base + (p - start), p, u - p);
}
static void
diff(Bin *a, Bin *b)
{
uchar *ap, *bp;
uchar *ac, *bc;
uchar *ae, *be;
int x;
ap = a->data;
bp = b->data;
ae = a->data + a->n;
be = b->data + b->n;
if(a->n != b->n)
sysfatal("desync: %ld %ld", a->n, a->n);
for(; ap < ae && bp < be; ap = ac, bp = bc){
if(*ap == *bp){
ac = ap+1;
bc = bp+1;
continue;
}
x = 4;
for(ac = ap, bc = bp; ac < ae && bc < be; ac++, bc++){
if(*ac == *bc)
x--;
else
x = 4;
if(x == 0)
break;
}
for(; ac > ap && bc > bp && *ac == *bc; ac--, bc--)
;
bc++;
ac++;
RLEpass(a->dot + (ap - a->data), bp, bc);
}
a->dot += a->n;
b->dot += b->n;
}
void
usage(void)
{
fprint(2, "usage: %s base modified >patch.ips", argv0);
exits("usage");
}
void
main(int argc, char **argv)
{
ARGBEGIN{
case 'd':
dump++;
break;
default:
usage();
}ARGEND;
if(argc < 2)
usage();
ips = Bfdopen(1, OWRITE);
if(ips == nil)
sysfatal("Bfdopen: %r");
Bwrite(ips, "PATCH", 5);
base.fd = open(argv[0], OREAD);
modded.fd = open(argv[1], OREAD);
if(base.fd < 0 || modded.fd < 0)
sysfatal("open: %r");
while(slurp(&base) != 0 && slurp(&modded) != 0)
diff(&base, &modded);
Bwrite(ips, "EOF", 3);
Bflush(ips);
exits(nil);
}