src/physfs_archiver_grp.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 17 May 2020 01:41:52 -0400
changeset 1691 c66c9326b05d
parent 1571 fbd9698219c1
permissions -rw-r--r--
7z: Make error handling a little more robust.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * GRP support routines for PhysicsFS.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles BUILD engine archives ("groupfiles"). This format
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *  (but not this driver) was put together by Ken Silverman.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 * The format is simple enough. In Ken's words:
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *    What's the .GRP file format?
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *     The ".grp" file format is just a collection of a lot of files stored
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *     into 1 big one. I tried to make the format as simple as possible: The
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *     first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *     the number of files that were compacted into the group file. Then for
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *     each file, there is a 16 byte structure, where the first 12 bytes are
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *     the filename, and the last 4 bytes are the file's size. The rest of
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *     the group file is just the raw data packed one after the other in the
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *     same order as the list of files.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 * (That info is from http://www.advsys.net/ken/build.htm ...)
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
    22
 * Please see the file LICENSE.txt in the source's root directory.
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *  This file written by Ryan C. Gordon.
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
#define __PHYSICSFS_INTERNAL__
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
#include "physfs_internal.h"
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
1258
074d08049aa7 Changed so that this builds a reasonable default with no command line #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
    30
#if PHYSFS_SUPPORTS_GRP
074d08049aa7 Changed so that this builds a reasonable default with no command line #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
    31
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    32
static int grpLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    33
{
1504
803282f78987 Minor code cleanups: whitespace fixes, 80-column overflows, wrong comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1498
diff changeset
    34
    PHYSFS_uint32 pos = 16 + (16 * count);  /* past sig+metadata. */
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    35
    PHYSFS_uint32 i;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    37
    for (i = 0; i < count; i++)
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    38
    {
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    39
        char *ptr;
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    40
        char name[13];
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    41
        PHYSFS_uint32 size;
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    42
        BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 12), 0);
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    43
        BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
370
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
    44
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    45
        name[12] = '\0';  /* name isn't null-terminated in file. */
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    46
        if ((ptr = strchr(name, ' ')) != NULL)
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    47
            *ptr = '\0';  /* trim extra spaces. */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    48
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    49
        size = PHYSFS_swapULE32(size);
1504
803282f78987 Minor code cleanups: whitespace fixes, 80-column overflows, wrong comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1498
diff changeset
    50
        BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    51
1504
803282f78987 Minor code cleanups: whitespace fixes, 80-column overflows, wrong comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1498
diff changeset
    52
        pos += size;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    53
    } /* for */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    54
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    55
    return 1;
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    56
} /* grpLoadEntries */
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    57
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    58
1571
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    59
static void *GRP_openArchive(PHYSFS_Io *io, const char *name,
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    60
                             int forWriting, int *claimed)
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    61
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    62
    PHYSFS_uint8 buf[12];
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    63
    PHYSFS_uint32 count = 0;
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    64
    void *unpkarc = NULL;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    65
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    66
    assert(io != NULL);  /* shouldn't ever happen. */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    68
    BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
    69
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    70
    BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, sizeof (buf)), NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    71
    if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    72
        BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
1571
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    74
    *claimed = 1;
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    75
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    76
    BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL);
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    77
    count = PHYSFS_swapULE32(count);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
1494
4e2c7ce9bf11 DirTrees and unpacked archives shouldn't demand a count of entries for init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1463
diff changeset
    79
    unpkarc = UNPK_openArchive(io);
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    80
    BAIL_IF_ERRPASS(!unpkarc, NULL);
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    81
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    82
    if (!grpLoadEntries(io, count, unpkarc))
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    83
    {
1497
b9d771d6805f unpacked: let archivers "abandon" the archive instead of closing it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1494
diff changeset
    84
        UNPK_abandonArchive(unpkarc);
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    85
        return NULL;
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    86
    } /* if */
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    87
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    88
    return unpkarc;
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    89
} /* GRP_openArchive */
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    90
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    91
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    92
const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    93
{
1322
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1320
diff changeset
    94
    CURRENT_PHYSFS_ARCHIVER_API_VERSION,
1281
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    95
    {
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    96
        "GRP",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    97
        "Build engine Groupfile format",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    98
        "Ryan C. Gordon <icculus@icculus.org>",
1373
527ef3c6a2d6 HTTPS all the things.
Ryan C. Gordon <icculus@icculus.org>
parents: 1331
diff changeset
    99
        "https://icculus.org/physfs/",
1326
ac0fac4fe6d6 Moved supportsSymlinks into PHYSFS_ArchiveInfo.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
   100
        0,  /* supportsSymlinks */
1281
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
   101
    },
1330
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   102
    GRP_openArchive,
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
   103
    UNPK_enumerate,
1330
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   104
    UNPK_openRead,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   105
    UNPK_openWrite,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   106
    UNPK_openAppend,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   107
    UNPK_remove,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   108
    UNPK_mkdir,
1331
eb06a37afa7e Rearranged PHYSFS_Archiver struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1330
diff changeset
   109
    UNPK_stat,
eb06a37afa7e Rearranged PHYSFS_Archiver struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1330
diff changeset
   110
    UNPK_closeArchive
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   111
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   112
214
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
   113
#endif  /* defined PHYSFS_SUPPORTS_GRP */
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
   114
1507
8953b8a171e7 Update filename comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1506
diff changeset
   115
/* end of physfs_archiver_grp.c ... */
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116