|
@@ -38,7 +38,7 @@ struct zinfo_pack {
|
38
|
38
|
uint32_t align;
|
39
|
39
|
};
|
40
|
40
|
|
41
|
|
-struct zinfo_subtract {
|
|
41
|
+struct zinfo_add {
|
42
|
42
|
char type[4];
|
43
|
43
|
uint32_t offset;
|
44
|
44
|
uint32_t divisor;
|
|
@@ -49,7 +49,7 @@ union zinfo_record {
|
49
|
49
|
struct zinfo_common common;
|
50
|
50
|
struct zinfo_copy copy;
|
51
|
51
|
struct zinfo_pack pack;
|
52
|
|
- struct zinfo_subtract subtract;
|
|
52
|
+ struct zinfo_add add;
|
53
|
53
|
};
|
54
|
54
|
|
55
|
55
|
struct zinfo_file {
|
|
@@ -157,8 +157,9 @@ static int process_zinfo_copy ( struct input_file *input,
|
157
|
157
|
}
|
158
|
158
|
|
159
|
159
|
if ( DEBUG ) {
|
160
|
|
- fprintf ( stderr, "COPY [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
|
161
|
|
- output->len, ( output->len + len ) );
|
|
160
|
+ fprintf ( stderr, "COPY [%#zx,%#zx) to [%#zx,%#zx)\n",
|
|
161
|
+ offset, ( offset + len ), output->len,
|
|
162
|
+ ( output->len + len ) );
|
162
|
163
|
}
|
163
|
164
|
|
164
|
165
|
memcpy ( ( output->buf + output->len ),
|
|
@@ -194,8 +195,9 @@ static int process_zinfo_pack ( struct input_file *input,
|
194
|
195
|
}
|
195
|
196
|
|
196
|
197
|
if ( DEBUG ) {
|
197
|
|
- fprintf ( stderr, "PACK [%#zx,%#zx) to [%#zx,%#zx)\n", offset, ( offset + len ),
|
198
|
|
- output->len, ( output->len + packed_len ) );
|
|
198
|
+ fprintf ( stderr, "PACK [%#zx,%#zx) to [%#zx,%#zx)\n",
|
|
199
|
+ offset, ( offset + len ), output->len,
|
|
200
|
+ ( output->len + packed_len ) );
|
199
|
201
|
}
|
200
|
202
|
|
201
|
203
|
output->len += packed_len;
|
|
@@ -207,78 +209,102 @@ static int process_zinfo_pack ( struct input_file *input,
|
207
|
209
|
return 0;
|
208
|
210
|
}
|
209
|
211
|
|
210
|
|
-static int process_zinfo_subtract ( struct input_file *input,
|
211
|
|
- struct output_file *output,
|
212
|
|
- struct zinfo_subtract *subtract,
|
213
|
|
- size_t datasize ) {
|
214
|
|
- size_t offset = subtract->offset;
|
|
212
|
+static int process_zinfo_add ( struct input_file *input,
|
|
213
|
+ struct output_file *output,
|
|
214
|
+ struct zinfo_add *add,
|
|
215
|
+ size_t datasize ) {
|
|
216
|
+ size_t offset = add->offset;
|
215
|
217
|
void *target;
|
216
|
|
- signed long raw_delta;
|
217
|
|
- signed long delta;
|
218
|
|
- unsigned long old;
|
219
|
|
- unsigned long new;
|
|
218
|
+ signed long addend;
|
|
219
|
+ unsigned long size;
|
|
220
|
+ signed long val;
|
|
221
|
+ unsigned long mask;
|
220
|
222
|
|
221
|
223
|
if ( ( offset + datasize ) > output->len ) {
|
222
|
|
- fprintf ( stderr, "Subtract at %#zx outside output buffer\n",
|
|
224
|
+ fprintf ( stderr, "Add at %#zx outside output buffer\n",
|
223
|
225
|
offset );
|
224
|
226
|
return -1;
|
225
|
227
|
}
|
226
|
228
|
|
227
|
229
|
target = ( output->buf + offset );
|
228
|
|
- raw_delta = ( align ( output->len, subtract->divisor ) -
|
229
|
|
- align ( input->len, subtract->divisor ) );
|
230
|
|
- delta = ( raw_delta / ( ( signed long ) subtract->divisor ) );
|
|
230
|
+ size = ( align ( output->len, add->divisor ) / add->divisor );
|
231
|
231
|
|
232
|
232
|
switch ( datasize ) {
|
233
|
|
- case 1: {
|
234
|
|
- uint8_t *byte = target;
|
235
|
|
- old = *byte;
|
236
|
|
- *byte += delta;
|
237
|
|
- new = *byte;
|
238
|
|
- break; }
|
239
|
|
- case 2: {
|
240
|
|
- uint16_t *word = target;
|
241
|
|
- old = *word;
|
242
|
|
- *word += delta;
|
243
|
|
- new = *word;
|
244
|
|
- break; }
|
245
|
|
- case 4: {
|
246
|
|
- uint32_t *dword = target;
|
247
|
|
- old = *dword;
|
248
|
|
- *dword += delta;
|
249
|
|
- new = *dword;
|
250
|
|
- break; }
|
|
233
|
+ case 1:
|
|
234
|
+ addend = *( ( int8_t * ) target );
|
|
235
|
+ break;
|
|
236
|
+ case 2:
|
|
237
|
+ addend = *( ( int16_t * ) target );
|
|
238
|
+ break;
|
|
239
|
+ case 4:
|
|
240
|
+ addend = *( ( int32_t * ) target );
|
|
241
|
+ break;
|
251
|
242
|
default:
|
252
|
|
- fprintf ( stderr, "Unsupported subtract datasize %d\n",
|
|
243
|
+ fprintf ( stderr, "Unsupported add datasize %d\n",
|
253
|
244
|
datasize );
|
254
|
245
|
return -1;
|
255
|
246
|
}
|
256
|
247
|
|
|
248
|
+ val = size + addend;
|
|
249
|
+
|
|
250
|
+ /* The result of 1UL << ( 8 * sizeof(unsigned long) ) is undefined */
|
|
251
|
+ mask = ( ( datasize < sizeof ( mask ) ) ?
|
|
252
|
+ ( ( 1UL << ( 8 * datasize ) ) - 1 ) : ~0UL );
|
|
253
|
+
|
|
254
|
+ if ( val < 0 ) {
|
|
255
|
+ fprintf ( stderr, "Add %s%#lx+%#lx at %#zx %sflows field\n",
|
|
256
|
+ ( ( addend < 0 ) ? "-" : "" ), abs ( addend ), size,
|
|
257
|
+ offset, ( ( addend < 0 ) ? "under" : "over" ) );
|
|
258
|
+ return -1;
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ if ( val & ~mask ) {
|
|
262
|
+ fprintf ( stderr, "Add %s%#lx+%#lx at %#zx overflows %d-byte "
|
|
263
|
+ "field (%d bytes too big)\n",
|
|
264
|
+ ( ( addend < 0 ) ? "-" : "" ), abs ( addend ), size,
|
|
265
|
+ offset, datasize,
|
|
266
|
+ ( ( val - mask - 1 ) * add->divisor ) );
|
|
267
|
+ return -1;
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ switch ( datasize ) {
|
|
271
|
+ case 1:
|
|
272
|
+ *( ( uint8_t * ) target ) = val;
|
|
273
|
+ break;
|
|
274
|
+ case 2:
|
|
275
|
+ *( ( uint16_t * ) target ) = val;
|
|
276
|
+ break;
|
|
277
|
+ case 4:
|
|
278
|
+ *( ( uint32_t * ) target ) = val;
|
|
279
|
+ break;
|
|
280
|
+ }
|
|
281
|
+
|
257
|
282
|
if ( DEBUG ) {
|
258
|
|
- fprintf ( stderr, "SUBx [%#zx,%#zx) (%#lx+(%#lx/%#x)-(%#lx/%#x)) = %#lx\n",
|
259
|
|
- offset, ( offset + datasize ), old, output->len, subtract->divisor,
|
260
|
|
- input->len, subtract->divisor, new );
|
|
283
|
+ fprintf ( stderr, "ADDx [%#zx,%#zx) (%s%#lx+(%#lx/%#x)) = "
|
|
284
|
+ "%#lx\n", offset, ( offset + datasize ),
|
|
285
|
+ ( ( addend < 0 ) ? "-" : "" ), abs ( addend ),
|
|
286
|
+ output->len, add->divisor, val );
|
261
|
287
|
}
|
262
|
288
|
|
263
|
289
|
return 0;
|
264
|
290
|
}
|
265
|
291
|
|
266
|
|
-static int process_zinfo_subb ( struct input_file *input,
|
|
292
|
+static int process_zinfo_addb ( struct input_file *input,
|
267
|
293
|
struct output_file *output,
|
268
|
294
|
union zinfo_record *zinfo ) {
|
269
|
|
- return process_zinfo_subtract ( input, output, &zinfo->subtract, 1 );
|
|
295
|
+ return process_zinfo_add ( input, output, &zinfo->add, 1 );
|
270
|
296
|
}
|
271
|
297
|
|
272
|
|
-static int process_zinfo_subw ( struct input_file *input,
|
|
298
|
+static int process_zinfo_addw ( struct input_file *input,
|
273
|
299
|
struct output_file *output,
|
274
|
300
|
union zinfo_record *zinfo ) {
|
275
|
|
- return process_zinfo_subtract ( input, output, &zinfo->subtract, 2 );
|
|
301
|
+ return process_zinfo_add ( input, output, &zinfo->add, 2 );
|
276
|
302
|
}
|
277
|
303
|
|
278
|
|
-static int process_zinfo_subl ( struct input_file *input,
|
|
304
|
+static int process_zinfo_addl ( struct input_file *input,
|
279
|
305
|
struct output_file *output,
|
280
|
306
|
union zinfo_record *zinfo ) {
|
281
|
|
- return process_zinfo_subtract ( input, output, &zinfo->subtract, 4 );
|
|
307
|
+ return process_zinfo_add ( input, output, &zinfo->add, 4 );
|
282
|
308
|
}
|
283
|
309
|
|
284
|
310
|
struct zinfo_processor {
|
|
@@ -291,9 +317,9 @@ struct zinfo_processor {
|
291
|
317
|
static struct zinfo_processor zinfo_processors[] = {
|
292
|
318
|
{ "COPY", process_zinfo_copy },
|
293
|
319
|
{ "PACK", process_zinfo_pack },
|
294
|
|
- { "SUBB", process_zinfo_subb },
|
295
|
|
- { "SUBW", process_zinfo_subw },
|
296
|
|
- { "SUBL", process_zinfo_subl },
|
|
320
|
+ { "ADDB", process_zinfo_addb },
|
|
321
|
+ { "ADDW", process_zinfo_addw },
|
|
322
|
+ { "ADDL", process_zinfo_addl },
|
297
|
323
|
};
|
298
|
324
|
|
299
|
325
|
static int process_zinfo ( struct input_file *input,
|