src/archiver_grp.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 20 Mar 2012 15:38:12 -0400
changeset 1240 22d4d1bd4e21
parent 1203 55f147714ce2
child 1258 074d08049aa7
permissions -rw-r--r--
Reworked the error reporting API. Now we use error codes instead of strings. This is based on work originally done by Christoph Nelles.
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
214
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
    27
#if (defined PHYSFS_SUPPORTS_GRP)
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
    28
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
#define __PHYSICSFS_INTERNAL__
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#include "physfs_internal.h"
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    32
static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
467
99664d9842cb Bunch of tedious corrections, optimizations, and cleanups.
Ryan C. Gordon <icculus@icculus.org>
parents: 464
diff changeset
    33
{
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    34
    PHYSFS_uint32 location = 16;  /* sizeof sig. */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    35
    UNPKentry *entries = NULL;
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    36
    UNPKentry *entry = NULL;
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    37
    char *ptr = NULL;
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    39
    entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    40
    BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    41
370
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
    42
    location += (16 * fileCount);
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
    43
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    44
    for (entry = entries; fileCount > 0; fileCount--, entry++)
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    45
    {
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    46
        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 12), ERRPASS, failed);
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    47
        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), ERRPASS, failed);
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    48
        entry->name[12] = '\0';  /* name isn't null-terminated in file. */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    49
        if ((ptr = strchr(entry->name, ' ')) != NULL)
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    50
            *ptr = '\0';  /* trim extra spaces. */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    51
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    52
        entry->size = PHYSFS_swapULE32(entry->size);
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    53
        entry->startPos = location;
370
da631e118455 Fixed entry loading.
Ryan C. Gordon <icculus@icculus.org>
parents: 364
diff changeset
    54
        location += entry->size;
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    55
    } /* for */
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    56
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    57
    return entries;
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    58
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    59
failed:
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    60
    allocator.Free(entries);
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    61
    return NULL;
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    62
} /* grpLoadEntries */
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    63
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    64
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    65
static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
364
51da162c76f7 Major overhauls. More efficient, robust, and bug-free.
Ryan C. Gordon <icculus@icculus.org>
parents: 335
diff changeset
    66
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    67
    PHYSFS_uint8 buf[12];
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    68
    PHYSFS_uint32 count = 0;
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    69
    UNPKentry *entries = NULL;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    70
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    71
    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
    72
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    73
    BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, 0);
139
616420dc210c Converted to file i/o abstraction, removed race condition.
Ryan C. Gordon <icculus@icculus.org>
parents: 132
diff changeset
    74
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    75
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), ERRPASS, NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    76
    if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    77
        BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    79
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof(count)), ERRPASS, NULL);
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    80
    count = PHYSFS_swapULE32(count);
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    82
    entries = grpLoadEntries(io, count);
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1203
diff changeset
    83
    BAIL_IF_MACRO(!entries, ERRPASS, NULL);
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    84
    return UNPK_openArchive(io, entries, count);
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    85
} /* GRP_openArchive */
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    86
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    87
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    88
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    89
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    90
    "GRP",
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    91
    GRP_ARCHIVE_DESCRIPTION,
767
db29bf06d171 Changed my email address.
Ryan C. Gordon <icculus@icculus.org>
parents: 754
diff changeset
    92
    "Ryan C. Gordon <icculus@icculus.org>",
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    93
    "http://icculus.org/physfs/",
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    94
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    95
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    96
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    97
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
    98
{
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    99
    &__PHYSFS_ArchiveInfo_GRP,
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   100
    GRP_openArchive,        /* openArchive() method    */
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   101
    UNPK_enumerateFiles,    /* enumerateFiles() method */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   102
    UNPK_openRead,          /* openRead() method       */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   103
    UNPK_openWrite,         /* openWrite() method      */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   104
    UNPK_openAppend,        /* openAppend() method     */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   105
    UNPK_remove,            /* remove() method         */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   106
    UNPK_mkdir,             /* mkdir() method          */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   107
    UNPK_dirClose,          /* dirClose() method       */
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
   108
    UNPK_stat               /* stat() method           */
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   109
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   110
214
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
   111
#endif  /* defined PHYSFS_SUPPORTS_GRP */
19846c18071b Initial autoconf support.
Ryan C. Gordon <icculus@icculus.org>
parents: 194
diff changeset
   112
24
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
/* end of grp.c ... */
b050804123a3 Initial add. Implemented, buggy, but not crashing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114