Skip to content

Latest commit

 

History

History
2705 lines (2199 loc) · 72.1 KB

mojopatch.c

File metadata and controls

2705 lines (2199 loc) · 72.1 KB
 
Jan 5, 2004
Jan 5, 2004
1
2
3
4
5
6
7
8
9
10
11
12
/*
*----------------------------------------------------------------------------
*
* mojopatch
* Copyright (C) 2003 Ryan C. Gordon.
*
*----------------------------------------------------------------------------
*
* (Insert documentation here.)
*
*----------------------------------------------------------------------------
*
Oct 29, 2006
Oct 29, 2006
13
14
15
* The latest version of MojoPatch is available here:
* http://icculus.org/mojopatch/
*
Jan 5, 2004
Jan 5, 2004
16
17
18
19
* This software was written quickly, is not well-engineered, and may have
* catastrophic bugs. Its method is brute-force, at best. Use at your
* own risk. Don't eat yellow snow.
*
May 27, 2004
May 27, 2004
20
21
* Please see the file LICENSE in the root of the source tree.
*
Jan 5, 2004
Jan 5, 2004
22
* Send patches, improvements, suggestions, etc to Ryan:
Oct 29, 2006
Oct 29, 2006
23
* icculus@icculus.org.
Jan 5, 2004
Jan 5, 2004
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
*
*----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include "platform.h"
#include "ui.h"
#include "md5.h"
May 30, 2004
May 30, 2004
41
42
43
44
45
46
47
48
#define VER_EXT_ZLIB
#if USE_ZLIB
#include "zlib.h"
#undef VER_EXT_ZLIB
#define VER_EXT_ZLIB " (w/zlib)"
#endif
Apr 22, 2005
Apr 22, 2005
49
50
51
52
53
/*
* The version string is really file format version, not program version.
* This is to prevent incompatible builds of the program from (mis)processing
* a patchfile.
*/
Jan 27, 2006
Jan 27, 2006
54
#define VERSION "0.0.7" VER_EXT_ZLIB
Jan 5, 2004
Jan 5, 2004
55
56
57
#define DEFAULT_PATCHFILENAME "default.mojopatch"
Oct 29, 2006
Oct 29, 2006
58
#define MOJOPATCHSIG "mojopatch " VERSION ": http://icculus.org/mojopatch/\r\n"
Jan 5, 2004
Jan 5, 2004
59
May 25, 2004
May 25, 2004
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#define STATIC_STRING_SIZE 1024
static void make_static_string(char *statstr, const char *str)
{
size_t len = strlen(str);
if (len >= STATIC_STRING_SIZE)
_fatal("Unexpected data in make_static_string()");
else
strcpy(statstr, str);
} /* make_static_string */
typedef struct
{
char signature[sizeof (MOJOPATCHSIG)];
char product[STATIC_STRING_SIZE];
char identifier[STATIC_STRING_SIZE];
char version[STATIC_STRING_SIZE];
char newversion[STATIC_STRING_SIZE];
char readmefname[STATIC_STRING_SIZE];
char *readmedata;
char renamedir[STATIC_STRING_SIZE];
May 27, 2004
May 27, 2004
82
char titlebar[STATIC_STRING_SIZE];
May 18, 2005
May 18, 2005
83
char startupmsg[STATIC_STRING_SIZE];
May 25, 2004
May 25, 2004
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
} PatchHeader;
typedef enum
{
OPERATION_DELETE = 0,
OPERATION_DELETEDIRECTORY,
OPERATION_ADD,
OPERATION_ADDDIRECTORY,
OPERATION_PATCH,
OPERATION_REPLACE,
OPERATION_DONE,
OPERATION_TOTAL /* must be last! */
} OperationType;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
} DeleteOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
} DeleteDirOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
Jan 27, 2006
Jan 27, 2006
114
unsigned int fsize;
May 25, 2004
May 25, 2004
115
md5_byte_t md5[16];
Jan 27, 2006
Jan 27, 2006
116
unsigned int mode;
May 25, 2004
May 25, 2004
117
118
119
120
121
122
} AddOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
Jan 27, 2006
Jan 27, 2006
123
unsigned int mode;
May 25, 2004
May 25, 2004
124
125
126
127
128
129
130
131
} AddDirOperation;
typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
md5_byte_t md5_1[16];
md5_byte_t md5_2[16];
Jan 27, 2006
Jan 27, 2006
132
133
134
unsigned int fsize;
unsigned int deltasize;
unsigned int mode;
May 25, 2004
May 25, 2004
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
} PatchOperation;
typedef struct
{
OperationType operation;
} DoneOperation;
typedef union
{
OperationType operation;
DeleteOperation del;
DeleteDirOperation deldir;
AddOperation add;
AddDirOperation adddir;
PatchOperation patch;
AddOperation replace;
DoneOperation done;
} Operations;
typedef struct
{
FILE *io;
int reading;
} SerialArchive;
Jan 5, 2004
Jan 5, 2004
159
160
161
162
163
164
165
166
167
168
169
typedef enum
{
COMMAND_NONE = 0,
COMMAND_CREATE,
COMMAND_INFO,
COMMAND_DOPATCHING,
COMMAND_TOTAL
} PatchCommands;
May 30, 2004
May 30, 2004
170
171
typedef enum
{
Jan 27, 2006
Jan 27, 2006
172
ZLIB_NONE = 0,
May 30, 2004
May 30, 2004
173
174
175
176
ZLIB_COMPRESS,
ZLIB_UNCOMPRESS
} ZlibOptions;
May 18, 2005
May 18, 2005
177
178
179
180
181
182
183
typedef enum
{
ISPATCHABLE_ERROR,
ISPATCHABLE_YES,
ISPATCHABLE_NO,
ISPATCHABLE_MATCHES,
} IsPatchable;
May 25, 2004
May 25, 2004
184
Jan 5, 2004
Jan 5, 2004
185
186
187
static int debug = 0;
static int interactive = 0;
static int replace = 0;
May 27, 2004
May 27, 2004
188
static int appending = 0;
May 28, 2004
May 28, 2004
189
static int alwaysadd = 0;
May 28, 2004
May 28, 2004
190
static int quietonsuccess = 0;
May 18, 2005
May 18, 2005
191
static int skip_patch = 0; /* global flag to skip current patch. */
May 30, 2004
May 30, 2004
192
static int zliblevel = 9;
Jan 5, 2004
Jan 5, 2004
193
194
195
196
197
198
199
200
201
static PatchCommands command = COMMAND_NONE;
static const char *patchfile = NULL;
static const char *dir1 = NULL;
static const char *dir2 = NULL;
static char *patchtmpfile = NULL;
static char *patchtmpfile2 = NULL;
May 25, 2004
May 25, 2004
202
static PatchHeader header;
Jan 5, 2004
Jan 5, 2004
203
204
205
206
static char **ignorelist = NULL;
static int ignorecount = 0;
May 18, 2005
May 18, 2005
207
208
char *patchfiledir = NULL;
Jan 5, 2004
Jan 5, 2004
209
210
211
212
static unsigned int maxxdeltamem = 128; /* in megabytes. */
static unsigned char iobuf[512 * 1024];
Jun 16, 2004
Jun 16, 2004
213
214
215
#if USE_ZLIB
static unsigned char compbuf[520 * 1024];
#endif
May 25, 2004
May 25, 2004
216
Mar 23, 2005
Mar 23, 2005
217
218
219
220
221
222
223
224
225
226
static int flush_archive(SerialArchive *ar)
{
if ((ar->reading) || (ar->io == NULL))
return(PATCHSUCCESS);
return((fflush(ar->io) == 0) ? PATCHSUCCESS : PATCHERROR);
} /* flush_archive */
May 25, 2004
May 25, 2004
227
228
229
static int serialize(SerialArchive *ar, void *val, size_t size)
{
int rc;
May 27, 2004
May 27, 2004
230
231
232
233
if (size == 0)
return(1);
May 25, 2004
May 25, 2004
234
235
236
237
238
if (ar->reading)
rc = fread(val, size, 1, ar->io);
else
rc = fwrite(val, size, 1, ar->io);
May 27, 2004
May 27, 2004
239
if ( (rc != 1) && (ferror(ar->io)) )
May 25, 2004
May 25, 2004
240
241
_fatal("%s error: %s.", ar->reading ? "Read":"Write", strerror(errno));
May 27, 2004
May 27, 2004
242
return(rc == 1); /* may fail without calling _fatal() on EOF! */
May 25, 2004
May 25, 2004
243
244
245
246
247
} /* serialize */
#define SERIALIZE(ar, x) serialize(ar, &x, sizeof (x))
Oct 29, 2006
Oct 29, 2006
248
249
250
251
252
253
254
255
256
static inline unsigned int swapui32(unsigned int x)
{
#if PLATFORM_BIGENDIAN
return (((x)>>24) + (((x)>>8)&0xff00) + (((x)<<8)&0xff0000) + ((x)<<24));
#else
return (x);
#endif
}
Jan 27, 2006
Jan 27, 2006
257
258
259
260
261
262
static int serialize_uint32(SerialArchive *ar, unsigned int *val)
{
assert(sizeof (unsigned int) == 4);
unsigned int x = *val;
if (!ar->reading)
Oct 29, 2006
Oct 29, 2006
263
x = swapui32(x);
Jan 27, 2006
Jan 27, 2006
264
265
266
267
if (!SERIALIZE(ar, x))
return(0);
Oct 29, 2006
Oct 29, 2006
268
*val = swapui32(x);
Jan 27, 2006
Jan 27, 2006
269
270
271
272
return(1);
} /* serialize_uint32 */
May 25, 2004
May 25, 2004
273
274
static int serialize_static_string(SerialArchive *ar, char *val)
{
Jan 27, 2006
Jan 27, 2006
275
unsigned int len = 0;
May 25, 2004
May 25, 2004
276
May 27, 2004
May 27, 2004
277
if (!ar->reading)
Jan 27, 2006
Jan 27, 2006
278
len = (unsigned int) strlen(val);
May 27, 2004
May 27, 2004
279
Jan 27, 2006
Jan 27, 2006
280
if (!serialize_uint32(ar, &len))
May 25, 2004
May 25, 2004
281
282
283
284
285
286
287
288
289
return(0);
if (len >= STATIC_STRING_SIZE)
{
_fatal("Bogus string data in patchfile.");
return(0);
} /* if */
val[len] = 0;
May 27, 2004
May 27, 2004
290
return(serialize(ar, val, len));
May 25, 2004
May 25, 2004
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
} /* serialize_static_string */
/*
* This will only overwrite (val)'s contents if (val) is empty ("!val[0]").
* Mostly, this lets us optionally override patchfiles from the command line.
* Writing is done, regardless of string's state.
*/
static int serialize_static_string_if_empty(SerialArchive *ar, char *val)
{
if ((!ar->reading) || (val[0] == '\0'))
return(serialize_static_string(ar, val));
else
{
char buffer[STATIC_STRING_SIZE];
return(serialize_static_string(ar, buffer)); /* throw away data */
} /* else */
} /* serialize_static_string_if_empty */
static int serialize_asciz_string(SerialArchive *ar, char **_buffer)
{
char *buffer = *_buffer;
size_t i = 0;
size_t allocated = 0;
int ch;
if (!ar->reading)
return(serialize(ar, buffer, strlen(buffer) + 1));
buffer = NULL;
/* read an arbitrary amount, allocate storage... */
do
{
if (i <= allocated)
{
allocated += 128;
buffer = realloc(buffer, allocated);
if (buffer == NULL)
{
_fatal("Out of memory.");
return(0);
} /* if */
} /* if */
ch = fgetc(ar->io);
if (ch == EOF)
{
if (feof(ar->io))
_fatal("Unexpected EOF during read.");
else
_fatal("Error during read: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
buffer[i] = (char) ch;
} while (buffer[i++] != '\0');
*_buffer = buffer;
return(1);
} /* serialize_asciz_string */
May 27, 2004
May 27, 2004
355
static int serialize_header(SerialArchive *ar, PatchHeader *h, int *legitEOF)
May 25, 2004
May 25, 2004
356
{
May 27, 2004
May 27, 2004
357
358
359
360
361
int rc;
int dummy = 0;
if (legitEOF == NULL)
legitEOF = &dummy;
May 27, 2004
May 27, 2004
362
363
364
assert(sizeof (h->signature) == sizeof (MOJOPATCHSIG));
memcpy(h->signature, MOJOPATCHSIG, sizeof (h->signature));
May 27, 2004
May 27, 2004
365
366
367
368
rc = SERIALIZE(ar, h->signature);
*legitEOF = ( (feof(ar->io)) && (!ferror(ar->io)) );
if (!rc)
return(*legitEOF);
May 25, 2004
May 25, 2004
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
if (memcmp(h->signature, MOJOPATCHSIG, sizeof (h->signature)) != 0)
{
h->signature[sizeof (h->signature) - 1] = '\0'; /* just in case. */
_fatal("[%s] is not a compatible mojopatch file.", patchfile);
_log("signature is: %s.", h->signature);
_log(" expected: %s.", MOJOPATCHSIG);
return(PATCHERROR);
} /* if */
if (serialize_static_string_if_empty(ar, h->product))
if (serialize_static_string_if_empty(ar, h->identifier))
if (serialize_static_string_if_empty(ar, h->version))
if (serialize_static_string_if_empty(ar, h->newversion))
if (serialize_static_string_if_empty(ar, h->readmefname))
if (serialize_asciz_string(ar, &h->readmedata))
if (serialize_static_string_if_empty(ar, h->renamedir))
May 27, 2004
May 27, 2004
386
if (serialize_static_string_if_empty(ar, h->titlebar))
May 18, 2005
May 18, 2005
387
if (serialize_static_string_if_empty(ar, h->startupmsg))
Mar 23, 2005
Mar 23, 2005
388
return(flush_archive(ar));
May 25, 2004
May 25, 2004
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
return(0);
} /* serialize_header */
static int serialize_delete_op(SerialArchive *ar, void *d)
{
DeleteOperation *del = (DeleteOperation *) d;
assert(del->operation == OPERATION_DELETE);
if (serialize_static_string(ar, del->fname))
return(1);
return(0);
} /* serialize_delete_op */
static int serialize_deldir_op(SerialArchive *ar, void *d)
{
DeleteDirOperation *deldir = (DeleteDirOperation *) d;
assert(deldir->operation == OPERATION_DELETEDIRECTORY);
if (serialize_static_string(ar, deldir->fname))
return(1);
return(0);
} /* serialize_deldir_op */
static int serialize_add_op(SerialArchive *ar, void *d)
{
AddOperation *add = (AddOperation *) d;
assert((add->operation == OPERATION_ADD) ||
(add->operation == OPERATION_REPLACE));
if (serialize_static_string(ar, add->fname))
Jan 27, 2006
Jan 27, 2006
419
if (serialize_uint32(ar, &add->fsize))
May 25, 2004
May 25, 2004
420
if (SERIALIZE(ar, add->md5))
Jan 27, 2006
Jan 27, 2006
421
if (serialize_uint32(ar, &add->mode))
May 25, 2004
May 25, 2004
422
423
424
425
426
427
428
429
430
431
return(1);
return(0);
} /* serialize_add_op */
static int serialize_adddir_op(SerialArchive *ar, void *d)
{
AddDirOperation *adddir = (AddDirOperation *) d;
assert(adddir->operation == OPERATION_ADDDIRECTORY);
if (serialize_static_string(ar, adddir->fname))
Jan 27, 2006
Jan 27, 2006
432
if (serialize_uint32(ar, &adddir->mode))
May 25, 2004
May 25, 2004
433
434
435
436
437
438
439
440
441
442
443
return(1);
return(0);
} /* serialize_adddir_op */
static int serialize_patch_op(SerialArchive *ar, void *d)
{
PatchOperation *patch = (PatchOperation *) d;
assert(patch->operation == OPERATION_PATCH);
if (serialize_static_string(ar, patch->fname))
if (SERIALIZE(ar, patch->md5_1))
May 27, 2004
May 27, 2004
444
if (SERIALIZE(ar, patch->md5_2))
Jan 27, 2006
Jan 27, 2006
445
446
447
if (serialize_uint32(ar, &patch->fsize))
if (serialize_uint32(ar, &patch->deltasize))
if (serialize_uint32(ar, &patch->mode))
May 25, 2004
May 25, 2004
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
return(1);
return(0);
} /* serialize_patch_op */
static int serialize_replace_op(SerialArchive *ar, void *d)
{
AddOperation *add = (AddOperation *) d;
assert(add->operation == OPERATION_REPLACE);
return(serialize_add_op(ar, d));
} /* serialize_replace_op */
static int serialize_done_op(SerialArchive *ar, void *d)
{
DoneOperation *done = (DoneOperation *) d;
assert(done->operation == OPERATION_DONE);
return(1);
} /* serialize_done_op */
typedef int (*OpSerializers)(SerialArchive *ar, void *data);
static OpSerializers serializers[OPERATION_TOTAL] =
{
/* Must match OperationType order! */
serialize_delete_op,
serialize_deldir_op,
serialize_add_op,
serialize_adddir_op,
serialize_patch_op,
serialize_replace_op,
serialize_done_op,
};
static int handle_delete_op(SerialArchive *ar, OperationType op, void *data);
static int handle_deldir_op(SerialArchive *ar, OperationType op, void *data);
static int handle_add_op(SerialArchive *ar, OperationType op, void *data);
static int handle_adddir_op(SerialArchive *ar, OperationType op, void *data);
static int handle_patch_op(SerialArchive *ar, OperationType op, void *data);
static int handle_replace_op(SerialArchive *ar, OperationType op, void *data);
May 27, 2004
May 27, 2004
489
static int handle_done_op(SerialArchive *ar, OperationType op, void *data);
May 25, 2004
May 25, 2004
490
491
492
493
494
495
496
497
498
499
500
typedef int (*OpHandlers)(SerialArchive *ar, OperationType op, void *data);
static OpHandlers operation_handlers[OPERATION_TOTAL] =
{
/* Must match OperationType order! */
handle_delete_op,
handle_deldir_op,
handle_add_op,
handle_adddir_op,
handle_patch_op,
handle_replace_op,
May 27, 2004
May 27, 2004
501
handle_done_op,
May 25, 2004
May 25, 2004
502
503
504
505
506
507
};
static int serialize_operation(SerialArchive *ar, Operations *ops)
{
Jan 27, 2006
Jan 27, 2006
508
509
unsigned char op = (unsigned char) ops->operation;
if (!SERIALIZE(ar, op))
May 25, 2004
May 25, 2004
510
511
return(0);
Jan 27, 2006
Jan 27, 2006
512
ops->operation = (OperationType) op;
May 25, 2004
May 25, 2004
513
514
515
516
517
518
if ((ops->operation < 0) || (ops->operation >= OPERATION_TOTAL))
{
_fatal("Invalid operation in patch file.");
return(0);
} /* if */
Mar 23, 2005
Mar 23, 2005
519
520
521
522
if (!serializers[ops->operation](ar, ops))
return(0);
return(flush_archive(ar));
May 25, 2004
May 25, 2004
523
524
525
526
527
528
529
} /* serialize_operation */
static int open_serialized_archive(SerialArchive *ar,
const char *fname,
int is_reading,
int *sizeok,
Jan 27, 2006
Jan 27, 2006
530
unsigned int *file_size)
May 25, 2004
May 25, 2004
531
532
533
534
535
536
{
memset(ar, '\0', sizeof (*ar));
if (strcmp(fname, "-") == 0) /* read from stdin? */
ar->io = (is_reading) ? stdin : stdout;
else
{
May 27, 2004
May 27, 2004
537
538
539
540
const char *fopenstr = "rb";
if (!is_reading)
fopenstr = ((appending) ? "ab" : "wb");
May 25, 2004
May 25, 2004
541
542
543
544
545
546
547
if (file_size != NULL)
{
int tmp = get_file_size(patchfile, file_size);
if (sizeok != NULL)
*sizeok = tmp;
} /* if */
May 27, 2004
May 27, 2004
548
ar->io = fopen(patchfile, fopenstr);
May 25, 2004
May 25, 2004
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
if (ar->io == NULL)
{
_fatal("Couldn't open [%s]: %s.", patchfile, strerror(errno));
return(PATCHERROR);
} /* if */
} /* else */
ar->reading = is_reading;
return(PATCHSUCCESS);
} /* open_serialized_archive */
static inline int close_serialized_archive(SerialArchive *ar)
{
if (ar->io != NULL)
{
if (fclose(ar->io) == EOF)
return(PATCHERROR);
ar->io = NULL;
} /* if */
return(PATCHSUCCESS);
} /* close_serialized_archive */
Jan 5, 2004
Jan 5, 2004
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* printf-style: makes string for UI to put in the log. */
void _fatal(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
ui_fatal(buf);
ui_pump();
} /* _fatal */
/* printf-style: makes string for UI to put in the log. */
void _log(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
ui_add_to_log(buf, 0);
ui_pump();
} /* _log */
/* printf-style: makes string for UI to put in the log if debugging enabled. */
void _dlog(const char *fmt, ...)
{
if (debug)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
ui_add_to_log(buf, 1);
ui_pump();
} /* if */
} /* _dlog */
static void _current_operation(const char *fmt, ...)
{
char buf[512];
May 28, 2004
May 28, 2004
621
622
623
624
625
626
627
628
629
630
if (skip_patch)
strcpy(buf, "Skipping ahead...");
else
{
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
} /* else */
Jan 5, 2004
Jan 5, 2004
631
632
633
634
635
ui_status(buf);
ui_pump();
} /* _current_operation */
May 18, 2005
May 18, 2005
636
637
638
639
640
641
642
/* !!! FIXME: don't taunt this function. */
/*
* ver == installed app's current version.
* allowed_ver == allowed versions ("123", "123 or 456", "less than 789").
* newver == version that patched app will become.
*/
IsPatchable version_ok(const char *ver, const char *allowed_ver, const char *newver)
May 25, 2004
May 25, 2004
643
644
{
char *ptr;
May 27, 2004
May 27, 2004
645
646
char *buf;
May 18, 2005
May 18, 2005
647
648
if (*allowed_ver == '\0') /* No specified version? Anything is okay. */
return ISPATCHABLE_YES;
May 27, 2004
May 27, 2004
649
May 18, 2005
May 18, 2005
650
if (strcmp(ver, newver) == 0) /* all patched up? */
May 18, 2005
May 18, 2005
651
return ISPATCHABLE_MATCHES;
Jun 16, 2004
Jun 16, 2004
652
May 27, 2004
May 27, 2004
653
buf = (char *) alloca(strlen(allowed_ver) + 1);
May 25, 2004
May 25, 2004
654
655
strcpy(buf, allowed_ver);
Jun 16, 2004
Jun 16, 2004
656
657
658
659
660
661
662
663
664
665
666
const char *lessThan = "less than ";
const size_t lessThanLen = strlen(lessThan);
if (strncmp(buf, lessThan, lessThanLen) == 0)
{
char *endptr = NULL;
double dver;
double dallow;
ptr = buf + lessThanLen;
dver = strtod(ver, &endptr);
if (endptr == ver)
May 18, 2005
May 18, 2005
667
return ISPATCHABLE_ERROR;
Jun 16, 2004
Jun 16, 2004
668
669
670
dallow = strtod(ptr, &endptr);
if (endptr == ptr)
May 18, 2005
May 18, 2005
671
return ISPATCHABLE_ERROR;
Jun 16, 2004
Jun 16, 2004
672
673
if (dver < dallow)
May 18, 2005
May 18, 2005
674
return(ISPATCHABLE_YES);
Jun 16, 2004
Jun 16, 2004
675
May 18, 2005
May 18, 2005
676
return(ISPATCHABLE_NO);
Jun 16, 2004
Jun 16, 2004
677
678
} /* if */
May 25, 2004
May 25, 2004
679
680
681
682
while ((ptr = strstr(buf, " or ")) != NULL)
{
*ptr = '\0';
if (strcmp(ver, buf) == 0)
May 18, 2005
May 18, 2005
683
return(ISPATCHABLE_YES);
May 25, 2004
May 25, 2004
684
685
686
687
buf = ptr + 4;
} /* while */
May 18, 2005
May 18, 2005
688
return( (strcmp(ver, buf) == 0) ? ISPATCHABLE_YES : ISPATCHABLE_NO );
May 25, 2004
May 25, 2004
689
690
691
} /* version_ok */
May 18, 2005
May 18, 2005
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
static IsPatchable check_product_version(const char *ident,
const char *version,
const char *newversion)
{
char buf[128];
IsPatchable retval = ISPATCHABLE_ERROR;
if (!get_product_version(ident, buf, sizeof (buf)))
_fatal("Can't determine product's installed version.");
else
{
retval = version_ok(buf, version, newversion);
if (retval == ISPATCHABLE_MATCHES)
_fatal("You seem to be all patched up already!");
else if (retval == ISPATCHABLE_ERROR)
_fatal("This patch is misbuilt! Contact your vendor!");
else if (retval == ISPATCHABLE_NO)
{
_fatal("This patch applies to version '%s', but you have '%s'.",
version, buf);
} /* else */
else
{
assert(retval == ISPATCHABLE_YES);
} /* else */
} /* else */
return(retval);
} /* check_product_version */
Jan 5, 2004
Jan 5, 2004
723
724
725
726
727
728
729
730
731
static int _do_xdelta(const char *fmt, ...)
{
char buf[512];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
buf[sizeof(buf)-1] = '\0';
_dlog("(xdelta call: [%s].)", buf);
May 18, 2005
May 18, 2005
732
return(spawn_xdelta(buf) == SPAWN_RETURNGOOD);
Jan 5, 2004
Jan 5, 2004
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
} /* _do_xdelta */
static int in_ignore_list(const char *fname)
{
int i;
for (i = 0; i < ignorecount; i++)
{
if (strcmp(fname, ignorelist[i]) == 0)
{
_log("Ignoring %s on user's instructions.", fname);
return(1);
} /* if */
} /* for */
return(0);
} /* in_ignore_list */
static inline int info_only(void)
{
May 28, 2004
May 28, 2004
754
return((command == COMMAND_INFO) || (skip_patch));
Jan 5, 2004
Jan 5, 2004
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
} /* info_only */
static void free_filelist(file_list *list)
{
file_list *next;
while (list != NULL)
{
next = list->next;
free(list->fname);
free(list);
list = next;
} /* while */
} /* free_filelist */
May 30, 2004
May 30, 2004
771
772
#if USE_ZLIB
static int write_between_files_compress(FILE *in, FILE *out, long fsize)
Jan 5, 2004
Jan 5, 2004
773
{
May 30, 2004
May 30, 2004
774
775
uLongf compsize;
uLongf uncompsize;
Oct 29, 2006
Oct 29, 2006
776
777
unsigned int uncompsizeui32;
unsigned int compsizeui32;
May 30, 2004
May 30, 2004
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
while (fsize > 0)
{
uncompsize = sizeof (iobuf);
if (uncompsize > fsize)
uncompsize = fsize;
if (fread(iobuf, uncompsize, 1, in) != 1)
{
_fatal("read error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
ui_pump();
fsize -= uncompsize;
compsize = sizeof (compbuf);
if (compress2(compbuf, &compsize, iobuf, uncompsize, zliblevel)!=Z_OK)
{
_fatal("zlib compression error.");
return(PATCHERROR);
} /* if */
ui_pump();
Oct 29, 2006
Oct 29, 2006
802
803
804
805
806
/* !!! FIXME: serialize? */
uncompsizeui32 = swapui32(uncompsize);
compsizeui32 = swapui32(compsize);
if ( (fwrite(&uncompsizeui32, sizeof (uncompsizeui32), 1, out) != 1) ||
(fwrite(&compsizeui32, sizeof (compsizeui32), 1, out) != 1) ||
May 30, 2004
May 30, 2004
807
808
809
810
811
812
813
814
(fwrite(compbuf, compsize, 1, out) != 1) )
{
_fatal("write error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
ui_pump();
} /* while */
Mar 23, 2005
Mar 23, 2005
815
return(fflush(out) == 0 ? PATCHSUCCESS : PATCHERROR);
May 30, 2004
May 30, 2004
816
817
818
} /* write_between_files_compress */
May 30, 2004
May 30, 2004
819
820
static int write_between_files_uncompress(FILE *in, FILE *out,
long fsize, int skip)
May 30, 2004
May 30, 2004
821
822
823
{
uLongf compsize;
uLongf uncompsize;
Oct 29, 2006
Oct 29, 2006
824
825
unsigned int uncompsizeui32;
unsigned int compsizeui32;
May 30, 2004
May 30, 2004
826
827
828
while (fsize > 0)
{
Oct 29, 2006
Oct 29, 2006
829
830
if ( (fread(&uncompsizeui32, sizeof (uncompsizeui32), 1, in) != 1) ||
(fread(&compsizeui32, sizeof (compsizeui32), 1, in) != 1) )
May 30, 2004
May 30, 2004
831
832
833
834
835
{
_fatal("read error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
Oct 29, 2006
Oct 29, 2006
836
837
838
839
/* !!! FIXME: serialize? */
uncompsize = swapui32(uncompsizeui32);
compsize = swapui32(compsizeui32);
May 30, 2004
May 30, 2004
840
841
842
843
844
845
if ( (compsize > sizeof (compbuf)) || (uncompsize > sizeof (iobuf)) )
{
_fatal("bogus compression data.");
return(PATCHERROR);
} /* if */
May 30, 2004
May 30, 2004
846
847
848
849
/* fsize is the uncompressed file size... */
fsize -= uncompsize;
if (skip)
May 30, 2004
May 30, 2004
850
{
May 30, 2004
May 30, 2004
851
852
853
854
855
if (fseek(in, compsize, SEEK_CUR) < 0)
{
_fatal("seek error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
May 30, 2004
May 30, 2004
856
857
} /* if */
May 30, 2004
May 30, 2004
858
else
May 30, 2004
May 30, 2004
859
{
May 30, 2004
May 30, 2004
860
861
862
863
864
865
if (fread(compbuf, compsize, 1, in) != 1)
{
_fatal("read error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
ui_pump();
May 30, 2004
May 30, 2004
866
May 30, 2004
May 30, 2004
867
868
869
870
871
872
873
874
875
876
877
878
879
if (uncompress(iobuf, &uncompsize, compbuf, compsize) != Z_OK)
{
_fatal("zlib decompression error.");
return(PATCHERROR);
} /* if */
ui_pump();
if (fwrite(iobuf, uncompsize, 1, out) != 1)
{
_fatal("write error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
} /* else */
May 30, 2004
May 30, 2004
880
881
882
883
ui_pump();
} /* while */
Mar 23, 2005
Mar 23, 2005
884
return(fflush(out) == 0 ? PATCHSUCCESS : PATCHERROR);
May 30, 2004
May 30, 2004
885
886
887
888
889
890
891
892
893
894
} /* write_between_files_uncompress */
#endif
static int write_between_files(FILE *in, FILE *out, long fsize, ZlibOptions z)
{
#if USE_ZLIB
if (z == ZLIB_COMPRESS)
return(write_between_files_compress(in, out, fsize));
else if (z == ZLIB_UNCOMPRESS)
May 30, 2004
May 30, 2004
895
return(write_between_files_uncompress(in, out, fsize, 0));
May 30, 2004
May 30, 2004
896
897
898
899
else
assert(z == ZLIB_NONE);
#endif
Jan 5, 2004
Jan 5, 2004
900
901
902
903
904
905
while (fsize > 0)
{
int max = sizeof (iobuf);
if (max > fsize)
max = fsize;
May 30, 2004
May 30, 2004
906
if (fread(iobuf, max, 1, in) != 1)
Jan 5, 2004
Jan 5, 2004
907
908
909
910
911
912
{
_fatal("read error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
ui_pump();
May 30, 2004
May 30, 2004
913
914
915
fsize -= max;
if (fwrite(iobuf, max, 1, out) != 1)
Jan 5, 2004
Jan 5, 2004
916
917
918
919
920
921
922
{
_fatal("write error: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
ui_pump();
} /* while */
Mar 23, 2005
Mar 23, 2005
923
return(fflush(out) == 0 ? PATCHSUCCESS : PATCHERROR);
Jan 5, 2004
Jan 5, 2004
924
925
926
927
928
929
930
} /* write_between_files */
static int do_rename(const char *from, const char *to)
{
FILE *in;
FILE *out;
Jan 27, 2006
Jan 27, 2006
931
unsigned int fsize;
Jan 5, 2004
Jan 5, 2004
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
int rc;
unlink(to); /* just in case. */
if (rename(from, to) != -1)
return(PATCHSUCCESS);
/* rename() might fail if from and to are on seperate filesystems. */
rc = get_file_size(from, &fsize);
in = fopen(from, "rb");
out = fopen(to, "wb");
if ((!rc) || (!in) || (!out))
{
if (in)
fclose(in);
if (out)
fclose(out);
unlink(to);
_fatal("File copy failed.");
return(PATCHERROR);
} /* if */
May 30, 2004
May 30, 2004
954
rc = write_between_files(in, out, fsize, ZLIB_NONE);
Jan 5, 2004
Jan 5, 2004
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
fclose(in);
if ((fclose(out) == -1) && (rc != PATCHERROR))
{
_fatal("File copy failed.");
return(PATCHERROR);
} /* if */
unlink(from);
return(rc);
} /* do_rename */
static int md5sum(FILE *in, md5_byte_t *digest, int output)
{
md5_state_t md5state;
long br;
_dlog("md5summing...");
memset(digest, '\0', 16);
md5_init(&md5state);
if (fseek(in, 0, SEEK_SET) == -1)
{
_fatal("Couldn't seek in file: %s.", strerror(errno));
return(PATCHERROR);
} /* if */
while (1)
{
ui_pump();
br = fread(iobuf, 1, sizeof (iobuf), in);
if (br == 0)
{
int err = errno;
if (feof(in))
break;
else
{
_fatal("Read error: %s.", strerror(err));
return(PATCHERROR);
} /* else */
} /* if */