diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 726bd1d..ae0bf5b 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -2,6 +2,9 @@ * CHANGELOG. */ +02202008 - Various archiver swap and compare functions now check if they are + swapping/comparing an item against itself, for efficiency and + to prevent overlapping memcpy() calls. 02132008 - Minor Windows fix (thanks, fydo!). 02012008 - lzma fixes (thanks, eH!). 01222008 - Upgraded lzma sdk, lzma.c improvements (thanks, Dennis!). diff --git a/archivers/grp.c b/archivers/grp.c index 820a881..8c63f1e 100644 --- a/archivers/grp.c +++ b/archivers/grp.c @@ -191,19 +191,27 @@ static int GRP_isArchive(const char *filename, int forWriting) static int grp_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - GRPentry *a = (GRPentry *) _a; - return(strcmp(a[one].name, a[two].name)); + if (one != two) + { + const GRPentry *a = (const GRPentry *) _a; + return(strcmp(a[one].name, a[two].name)); + } /* if */ + + return 0; } /* grp_entry_cmp */ static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - GRPentry tmp; - GRPentry *first = &(((GRPentry *) _a)[one]); - GRPentry *second = &(((GRPentry *) _a)[two]); - memcpy(&tmp, first, sizeof (GRPentry)); - memcpy(first, second, sizeof (GRPentry)); - memcpy(second, &tmp, sizeof (GRPentry)); + if (one != two) + { + GRPentry tmp; + GRPentry *first = &(((GRPentry *) _a)[one]); + GRPentry *second = &(((GRPentry *) _a)[two]); + memcpy(&tmp, first, sizeof (GRPentry)); + memcpy(first, second, sizeof (GRPentry)); + memcpy(second, &tmp, sizeof (GRPentry)); + } /* if */ } /* grp_entry_swap */ diff --git a/archivers/hog.c b/archivers/hog.c index 136b7cb..43620f6 100644 --- a/archivers/hog.c +++ b/archivers/hog.c @@ -228,19 +228,27 @@ static int HOG_isArchive(const char *filename, int forWriting) static int hog_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - HOGentry *a = (HOGentry *) _a; - return(__PHYSFS_stricmpASCII(a[one].name, a[two].name)); + if (one != two) + { + const HOGentry *a = (const HOGentry *) _a; + return(__PHYSFS_stricmpASCII(a[one].name, a[two].name)); + } /* if */ + + return 0; } /* hog_entry_cmp */ static void hog_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - HOGentry tmp; - HOGentry *first = &(((HOGentry *) _a)[one]); - HOGentry *second = &(((HOGentry *) _a)[two]); - memcpy(&tmp, first, sizeof (HOGentry)); - memcpy(first, second, sizeof (HOGentry)); - memcpy(second, &tmp, sizeof (HOGentry)); + if (one != two) + { + HOGentry tmp; + HOGentry *first = &(((HOGentry *) _a)[one]); + HOGentry *second = &(((HOGentry *) _a)[two]); + memcpy(&tmp, first, sizeof (HOGentry)); + memcpy(first, second, sizeof (HOGentry)); + memcpy(second, &tmp, sizeof (HOGentry)); + } /* if */ } /* hog_entry_swap */ diff --git a/archivers/mvl.c b/archivers/mvl.c index 650b49b..8fbda23 100644 --- a/archivers/mvl.c +++ b/archivers/mvl.c @@ -194,19 +194,27 @@ static int MVL_isArchive(const char *filename, int forWriting) static int mvl_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - MVLentry *a = (MVLentry *) _a; - return(strcmp(a[one].name, a[two].name)); + if (one != two) + { + const MVLentry *a = (const MVLentry *) _a; + return(strcmp(a[one].name, a[two].name)); + } /* if */ + + return 0; } /* mvl_entry_cmp */ static void mvl_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - MVLentry tmp; - MVLentry *first = &(((MVLentry *) _a)[one]); - MVLentry *second = &(((MVLentry *) _a)[two]); - memcpy(&tmp, first, sizeof (MVLentry)); - memcpy(first, second, sizeof (MVLentry)); - memcpy(second, &tmp, sizeof (MVLentry)); + if (one != two) + { + MVLentry tmp; + MVLentry *first = &(((MVLentry *) _a)[one]); + MVLentry *second = &(((MVLentry *) _a)[two]); + memcpy(&tmp, first, sizeof (MVLentry)); + memcpy(first, second, sizeof (MVLentry)); + memcpy(second, &tmp, sizeof (MVLentry)); + } /* if */ } /* mvl_entry_swap */ diff --git a/archivers/qpak.c b/archivers/qpak.c index f4aa7e7..15832c4 100644 --- a/archivers/qpak.c +++ b/archivers/qpak.c @@ -217,19 +217,27 @@ static int QPAK_isArchive(const char *filename, int forWriting) static int qpak_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - QPAKentry *a = (QPAKentry *) _a; - return(QPAK_strcmp(a[one].name, a[two].name)); + if (one != two) + { + const QPAKentry *a = (const QPAKentry *) _a; + return(QPAK_strcmp(a[one].name, a[two].name)); + } /* if */ + + return 0; } /* qpak_entry_cmp */ static void qpak_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - QPAKentry tmp; - QPAKentry *first = &(((QPAKentry *) _a)[one]); - QPAKentry *second = &(((QPAKentry *) _a)[two]); - memcpy(&tmp, first, sizeof (QPAKentry)); - memcpy(first, second, sizeof (QPAKentry)); - memcpy(second, &tmp, sizeof (QPAKentry)); + if (one != two) + { + QPAKentry tmp; + QPAKentry *first = &(((QPAKentry *) _a)[one]); + QPAKentry *second = &(((QPAKentry *) _a)[two]); + memcpy(&tmp, first, sizeof (QPAKentry)); + memcpy(first, second, sizeof (QPAKentry)); + memcpy(second, &tmp, sizeof (QPAKentry)); + } /* if */ } /* qpak_entry_swap */ diff --git a/archivers/wad.c b/archivers/wad.c index fddc22a..cb2626c 100644 --- a/archivers/wad.c +++ b/archivers/wad.c @@ -215,19 +215,27 @@ static int WAD_isArchive(const char *filename, int forWriting) static int wad_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - WADentry *a = (WADentry *) _a; - return(strcmp(a[one].name, a[two].name)); + if (one != two) + { + const WADentry *a = (const WADentry *) _a; + return(strcmp(a[one].name, a[two].name)); + } /* if */ + + return 0; } /* wad_entry_cmp */ static void wad_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - WADentry tmp; - WADentry *first = &(((WADentry *) _a)[one]); - WADentry *second = &(((WADentry *) _a)[two]); - memcpy(&tmp, first, sizeof (WADentry)); - memcpy(first, second, sizeof (WADentry)); - memcpy(second, &tmp, sizeof (WADentry)); + if (one != two) + { + WADentry tmp; + WADentry *first = &(((WADentry *) _a)[one]); + WADentry *second = &(((WADentry *) _a)[two]); + memcpy(&tmp, first, sizeof (WADentry)); + memcpy(first, second, sizeof (WADentry)); + memcpy(second, &tmp, sizeof (WADentry)); + } /* if */ } /* wad_entry_swap */ diff --git a/archivers/zip.c b/archivers/zip.c index da4f3a5..92fe223 100644 --- a/archivers/zip.c +++ b/archivers/zip.c @@ -952,19 +952,27 @@ zip_load_entry_puked: static int zip_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - ZIPentry *a = (ZIPentry *) _a; - return(strcmp(a[one].name, a[two].name)); + if (one != two) + { + const ZIPentry *a = (const ZIPentry *) _a; + return(strcmp(a[one].name, a[two].name)); + } /* if */ + + return 0; } /* zip_entry_cmp */ static void zip_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two) { - ZIPentry tmp; - ZIPentry *first = &(((ZIPentry *) _a)[one]); - ZIPentry *second = &(((ZIPentry *) _a)[two]); - memcpy(&tmp, first, sizeof (ZIPentry)); - memcpy(first, second, sizeof (ZIPentry)); - memcpy(second, &tmp, sizeof (ZIPentry)); + if (one != two) + { + ZIPentry tmp; + ZIPentry *first = &(((ZIPentry *) _a)[one]); + ZIPentry *second = &(((ZIPentry *) _a)[two]); + memcpy(&tmp, first, sizeof (ZIPentry)); + memcpy(first, second, sizeof (ZIPentry)); + memcpy(second, &tmp, sizeof (ZIPentry)); + } /* if */ } /* zip_entry_swap */