Skip to content

Commit

Permalink
Cleaned up some datatype sizes and handle byte order explicitly...thi…
Browse files Browse the repository at this point in the history
…s allows

 one Universal Binary on the Mac to use the same datafile on all platforms.
  • Loading branch information
icculus committed Jan 27, 2006
1 parent 1ca1dae commit 6183ac2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 31 deletions.
80 changes: 54 additions & 26 deletions mojopatch.c
Expand Up @@ -48,7 +48,7 @@
* This is to prevent incompatible builds of the program from (mis)processing
* a patchfile.
*/
#define VERSION "0.0.6" VER_EXT_ZLIB
#define VERSION "0.0.7" VER_EXT_ZLIB

#define DEFAULT_PATCHFILENAME "default.mojopatch"

Expand Down Expand Up @@ -108,16 +108,16 @@ typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
size_t fsize;
unsigned int fsize;
md5_byte_t md5[16];
mode_t mode;
unsigned int mode;
} AddOperation;

typedef struct
{
OperationType operation;
char fname[STATIC_STRING_SIZE];
mode_t mode;
unsigned int mode;
} AddDirOperation;

typedef struct
Expand All @@ -126,9 +126,9 @@ typedef struct
char fname[STATIC_STRING_SIZE];
md5_byte_t md5_1[16];
md5_byte_t md5_2[16];
size_t fsize;
size_t deltasize;
mode_t mode;
unsigned int fsize;
unsigned int deltasize;
unsigned int mode;
} PatchOperation;

typedef struct
Expand Down Expand Up @@ -166,7 +166,7 @@ typedef enum

typedef enum
{
ZLIB_NONE,
ZLIB_NONE = 0,
ZLIB_COMPRESS,
ZLIB_UNCOMPRESS
} ZlibOptions;
Expand Down Expand Up @@ -242,14 +242,37 @@ static int serialize(SerialArchive *ar, void *val, size_t size)

#define SERIALIZE(ar, x) serialize(ar, &x, sizeof (x))

static int serialize_uint32(SerialArchive *ar, unsigned int *val)
{
assert(sizeof (unsigned int) == 4);
unsigned int x = *val;

#if PLATFORM_BIGENDIAN
if (!ar->reading)
x = (((x)>>24) + (((x)>>8)&0xff00) + (((x)<<8)&0xff0000) + ((x)<<24));
#endif

if (!SERIALIZE(ar, x))
return(0);

#if PLATFORM_BIGENDIAN
if (ar->reading)
x = (((x)>>24) + (((x)>>8)&0xff00) + (((x)<<8)&0xff0000) + ((x)<<24));
#endif

*val = x;
return(1);
} /* serialize_uint32 */


static int serialize_static_string(SerialArchive *ar, char *val)
{
size_t len;
unsigned int len = 0;

if (!ar->reading)
len = strlen(val);
len = (unsigned int) strlen(val);

if (!SERIALIZE(ar, len))
if (!serialize_uint32(ar, &len))
return(0);

if (len >= STATIC_STRING_SIZE)
Expand Down Expand Up @@ -388,9 +411,9 @@ static int serialize_add_op(SerialArchive *ar, void *d)
assert((add->operation == OPERATION_ADD) ||
(add->operation == OPERATION_REPLACE));
if (serialize_static_string(ar, add->fname))
if (SERIALIZE(ar, add->fsize))
if (serialize_uint32(ar, &add->fsize))
if (SERIALIZE(ar, add->md5))
if (SERIALIZE(ar, add->mode))
if (serialize_uint32(ar, &add->mode))
return(1);

return(0);
Expand All @@ -401,7 +424,7 @@ 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))
if (SERIALIZE(ar, adddir->mode))
if (serialize_uint32(ar, &adddir->mode))
return(1);

return(0);
Expand All @@ -414,9 +437,9 @@ static int serialize_patch_op(SerialArchive *ar, void *d)
if (serialize_static_string(ar, patch->fname))
if (SERIALIZE(ar, patch->md5_1))
if (SERIALIZE(ar, patch->md5_2))
if (SERIALIZE(ar, patch->fsize))
if (SERIALIZE(ar, patch->deltasize))
if (SERIALIZE(ar, patch->mode))
if (serialize_uint32(ar, &patch->fsize))
if (serialize_uint32(ar, &patch->deltasize))
if (serialize_uint32(ar, &patch->mode))
return(1);

return(0);
Expand Down Expand Up @@ -477,9 +500,11 @@ static OpHandlers operation_handlers[OPERATION_TOTAL] =

static int serialize_operation(SerialArchive *ar, Operations *ops)
{
if (!SERIALIZE(ar, ops->operation))
unsigned char op = (unsigned char) ops->operation;
if (!SERIALIZE(ar, op))
return(0);

ops->operation = (OperationType) op;
if ((ops->operation < 0) || (ops->operation >= OPERATION_TOTAL))
{
_fatal("Invalid operation in patch file.");
Expand All @@ -497,7 +522,7 @@ static int open_serialized_archive(SerialArchive *ar,
const char *fname,
int is_reading,
int *sizeok,
size_t *file_size)
unsigned int *file_size)
{
memset(ar, '\0', sizeof (*ar));
if (strcmp(fname, "-") == 0) /* read from stdin? */
Expand Down Expand Up @@ -887,7 +912,7 @@ static int do_rename(const char *from, const char *to)
{
FILE *in;
FILE *out;
long fsize;
unsigned int fsize;
int rc;

unlink(to); /* just in case. */
Expand Down Expand Up @@ -1237,7 +1262,7 @@ static int put_add(SerialArchive *ar, const char *fname)

ops.operation = (replace) ? OPERATION_REPLACE : OPERATION_ADD;
ops.add.fsize = statbuf.st_size;
ops.add.mode = (mode_t) statbuf.st_mode;
ops.add.mode = (unsigned int) statbuf.st_mode;
make_static_string(ops.add.fname, fname);

if (!serialize_operation(ar, &ops))
Expand Down Expand Up @@ -1394,7 +1419,7 @@ static int put_add_dir(SerialArchive *ar, const char *fname)
} /* if */

ops.operation = OPERATION_ADDDIRECTORY;
ops.adddir.mode = (mode_t) statbuf.st_mode;
ops.adddir.mode = (unsigned int) statbuf.st_mode;
make_static_string(ops.adddir.fname, fname);

if (!serialize_operation(ar, &ops))
Expand Down Expand Up @@ -1562,7 +1587,7 @@ static int put_patch(SerialArchive *ar, const char *fname1, const char *fname2)
} /* if */

ops.operation = OPERATION_PATCH;
ops.patch.mode = (mode_t) statbuf.st_mode;
ops.patch.mode = (unsigned int) statbuf.st_mode;
ops.patch.fsize = statbuf.st_size;
make_static_string(ops.patch.fname, fname2);
if (!serialize_operation(ar, &ops))
Expand Down Expand Up @@ -1837,7 +1862,7 @@ static char *read_whole_file(const char *fname)
int i;
int rc;
FILE *io = NULL;
long fsize = 0;
unsigned int fsize = 0;
char *retval = NULL;

if (!get_file_size(fname, &fsize))
Expand Down Expand Up @@ -1889,7 +1914,7 @@ static int create_patchfile(void)
{
SerialArchive ar;
int retval = PATCHSUCCESS;
size_t fsize;
unsigned int fsize;
char *real1 = NULL;
char *real2 = NULL;
char *real3 = NULL;
Expand Down Expand Up @@ -2293,7 +2318,7 @@ static int do_patching(void)
int retval = PATCHERROR;
int installed_patches = 0;
int skipped_patches = 0;
long file_size = 0; /* !!! FIXME: make this size_t? */
unsigned int file_size = 0;
int do_progress = 0;

ui_total_progress(do_progress ? 0 : -1);
Expand Down Expand Up @@ -2621,6 +2646,9 @@ int mojopatch_main(int argc, char **argv)
time_t starttime = time(NULL);
int retval = PATCHSUCCESS;

/* !!! FIXME: We need to serialize this, so we serialize it as uint32. */
assert(sizeof (mode_t) == sizeof (unsigned int));

memset(&header, '\0', sizeof (header));

if (!kickoff_ui(argc, argv))
Expand Down
8 changes: 7 additions & 1 deletion platform.h
Expand Up @@ -6,6 +6,12 @@
extern "C" {
#endif

#ifdef __POWERPC__
#define PLATFORM_BIGENDIAN 0
#else
#define PLATFORM_BIGENDIAN 1
#endif

#define PATCHERROR 0
#define PATCHSUCCESS 1

Expand Down Expand Up @@ -62,7 +68,7 @@ int file_exists(const char *fname);
int file_is_directory(const char *fname);
int file_is_symlink(const char *fname);
file_list *make_filelist(const char *base); /* must use malloc(). */
int get_file_size(const char *fname, long *fsize);
int get_file_size(const char *fname, unsigned int *fsize);
char *get_current_dir(char *buf, size_t bufsize);
char *get_realpath(const char *path);
int update_version(const char *ver);
Expand Down
6 changes: 3 additions & 3 deletions platform_unix.c
Expand Up @@ -126,7 +126,7 @@ file_list *make_filelist(const char *base)
} /* make_filelist */


int get_file_size(const char *fname, long *fsize)
int get_file_size(const char *fname, unsigned int *fsize)
{
struct stat statbuf;

Expand Down Expand Up @@ -297,7 +297,7 @@ int get_product_version(const char *ident, char *buf, size_t bufsize)
const char *fname = "Contents/Info.plist"; /* already chdir'd for this. */
char *mem = NULL;
char *ptr;
long fsize;
unsigned int fsize;
int retval = 0;
FILE *io = NULL;

Expand Down Expand Up @@ -395,7 +395,7 @@ int update_version(const char *ver)
const char *fname = "Contents/Info.plist"; /* already chdir'd for this. */
char *mem = NULL;
char *ptr;
long fsize;
unsigned int fsize;
int retval = 0;
long writestart;
long writeend;
Expand Down
2 changes: 1 addition & 1 deletion platform_win32.c
Expand Up @@ -99,7 +99,7 @@ file_list *make_filelist(const char *base)
} /* make_filelist */


int get_file_size(const char *fname, long *fsize)
int get_file_size(const char *fname, unsigned int *fsize)
{
DWORD FileSz;
DWORD FileSzHigh;
Expand Down

0 comments on commit 6183ac2

Please sign in to comment.