forked from prettyhatemachine/sph-sgminer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpatch.c
211 lines (182 loc) · 6.18 KB
/
jpatch.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*******************************************************************************
* Jojo's Patch: apply patch from jdiff to a binary file
*
* Copyright (C) 2002-2009 Joris Heirbaut
*
* + some simplifications for dashminer project
*
* Author Version Date Modification
* --------------------- ------- ------- -----------------------
* Joris Heirbaut v0.0 10-06-2002 hashed compare
* Joris Heirbaut v0.1 20-06-2002 optimized esc-sequences & lengths
* Joris Heirbaut v0.4c 09-01-2003 use seek for DEL-instruction
* Joris Heirbaut v0.5 13-05-2003 do not count past EOF
* Joris Heirbaut v0.6 13-05-2005 large-file support
* Joris Heirbaut v0.7 01-09-2009 large-file support
* Joris Heirbaut v0.7 29-10-2009 use buffered reading
* Joris Heirbaut v0.7 01-11-2009 do not read original file on MOD
* Joris Heirbaut v0.8 15-09-2011 C++ wrapping
*
* Licence
* -------
*
* This program is free software. Terms of the GNU General Public License apply.
*
* This program is distributed WITHOUT ANY WARRANTY, without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.a
*
* A copy of the GNU General Public License if found in the file "Licence.txt"
* deliverd along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Parts or all of this source code may only be reused within other GNU free, open
* source, software.
* So if your project is not an open source project, you are non entitled to read
* any further below this line!
*
*******************************************************************************/
/**
* Output routine constants
*/
#define ESC 0xA7 /* Escape */
#define MOD 0xA6 /* Modify */
#define INS 0xA5 /* Insert */
#define DEL 0xA4 /* Delete */
#define EQL 0xA3 /* Equal */
#define BKT 0xA2 /* Backtrace */
/*******************************************************************************
* Input routines
*******************************************************************************/
#define ESC 0xA7
#define MOD 0xA6
#define INS 0xA5
#define DEL 0xA4
#define EQL 0xA3
#define BKT 0xA2
off_t ufGetInt( FILE *lpFil ){
off_t liVal ;
liVal = getc(lpFil) ;
if (liVal < 252)
return liVal + 1 ;
if (liVal == 252)
return 253 + getc(lpFil) ;
if (liVal == 253) {
liVal = getc(lpFil) ;
liVal = (liVal << 8) + getc(lpFil) ;
return liVal ;
}
if (liVal == 254) {
liVal = getc(lpFil) ;
liVal = (liVal << 8) + getc(lpFil) ;
liVal = (liVal << 8) + getc(lpFil) ;
liVal = (liVal << 8) + getc(lpFil) ;
return liVal ;
}
//fprintf(stderr, "64-bit length numbers not supported!\n");
return 0;
}
/*******************************************************************************
* Patch function
*******************************************************************************
* Input stream consists of a series of
* <op> (<data> || <len>)
* where
* <op> = <ESC> (<MOD>||<INS>||<DEL>||<EQL>)
* <data> = <chr>||<ESC><ESC>
* <chr> = any byte different from <ESC><MOD><INS><DEL> or <EQL>
* <ESC><ESC> yields one <ESC> byte
*******************************************************************************/
int jpatch ( FILE *asFilOrg, FILE *asFilPch, unsigned char *pOutBuf, int iOutMaxLen )
{
int liInp ; /* Current input from patch file */
int liOpr ; /* Current operand */
off_t lzOff ; /* Current operand's offset */
off_t lzMod = 0; /* Number of bytes to skip on MOD */
int lbChg=false ; /* Changing operand? */
int lbEsc=false ; /* Non-operand escape char found? */
int iCurOutLen = 0;
liOpr = ESC ;
while ((liInp = getc(asFilPch)) != EOF) {
if(iCurOutLen >= iOutMaxLen)
return -1;//overflow
// Parse an operator: ESC liOpr [lzOff]
if (liInp == ESC) {
liInp = getc(asFilPch);
switch(liInp) {
case MOD:
liOpr = MOD;
lbChg = true;
break ;
case INS:
liOpr = INS;
lbChg = true;
break ;
case DEL:
liOpr = DEL;
lzOff = ufGetInt(asFilPch);
if (fseek(asFilOrg, lzOff + lzMod, SEEK_CUR) != 0) {
return -2;//fprintf(stderr, "Could not position on original file (seek %"PRIzd" + %"PRIzd").\n", lzOff, lzMod);
}
lzMod = 0;
lbChg = true;
break ;
case EQL:
liOpr = EQL;
lzOff = ufGetInt(asFilPch);
if (lzMod > 0) {
if (fseek(asFilOrg, lzMod, SEEK_CUR) != 0) {
return -3;//fprintf(stderr, "Could not position on original file (skip %"PRIzd").\n", lzMod);
}
lzMod = 0;
}
if((iCurOutLen + lzOff) > iOutMaxLen)
return 0;
if (fread(pOutBuf + iCurOutLen, 1, lzOff, asFilOrg) != lzOff)
return -4;//fprintf(stderr, "Error reading original file.\n");
iCurOutLen += lzOff;
lbChg = true;
break ;
case BKT:
liOpr = BKT ;
lzOff = ufGetInt(asFilPch) ;
if (fseek(asFilOrg, lzMod - lzOff, SEEK_CUR) != 0) {
return -5;//fprintf(stderr, "Could not position on original file (seek back %"PRIzd" - %"PRIzd").\n", lzMod, lzOff);
}
lzMod = 0 ;
lbChg = true;
break ;
case ESC:
break;
default:
lbEsc = true;
break;
}
}
if ( lbChg ) {
lbChg = false ;
} else {
switch (liOpr) {
case DEL: break;
case EQL: break;
case BKT: break;
case MOD:
if (lbEsc) {
pOutBuf[iCurOutLen++] = ESC;
lzMod ++ ;
}
pOutBuf[iCurOutLen++] = liInp;
lzMod ++ ;
break ;
case INS :
if (lbEsc) {
pOutBuf[iCurOutLen++] = ESC;
}
pOutBuf[iCurOutLen++] = liInp;
break ;
}
} /* if lbChg */
lbEsc = false ;
} /* while */
return iCurOutLen;
}