misc/physfs/src/archiver_grp.c
branchicegun
changeset 8604 fa44e3218a08
parent 8589 3bb505fc707e
parent 8603 1b58da1fd9bf
child 8606 45f6c35ffd33
equal deleted inserted replaced
8589:3bb505fc707e 8604:fa44e3218a08
     1 /*
       
     2  * GRP support routines for PhysicsFS.
       
     3  *
       
     4  * This driver handles BUILD engine archives ("groupfiles"). This format
       
     5  *  (but not this driver) was put together by Ken Silverman.
       
     6  *
       
     7  * The format is simple enough. In Ken's words:
       
     8  *
       
     9  *    What's the .GRP file format?
       
    10  *
       
    11  *     The ".grp" file format is just a collection of a lot of files stored
       
    12  *     into 1 big one. I tried to make the format as simple as possible: The
       
    13  *     first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
       
    14  *     the number of files that were compacted into the group file. Then for
       
    15  *     each file, there is a 16 byte structure, where the first 12 bytes are
       
    16  *     the filename, and the last 4 bytes are the file's size. The rest of
       
    17  *     the group file is just the raw data packed one after the other in the
       
    18  *     same order as the list of files.
       
    19  *
       
    20  * (That info is from http://www.advsys.net/ken/build.htm ...)
       
    21  *
       
    22  * Please see the file LICENSE.txt in the source's root directory.
       
    23  *
       
    24  *  This file written by Ryan C. Gordon.
       
    25  */
       
    26 
       
    27 #define __PHYSICSFS_INTERNAL__
       
    28 #include "physfs_internal.h"
       
    29 
       
    30 #if PHYSFS_SUPPORTS_GRP
       
    31 
       
    32 static UNPKentry *grpLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
       
    33 {
       
    34     PHYSFS_uint32 location = 16;  /* sizeof sig. */
       
    35     UNPKentry *entries = NULL;
       
    36     UNPKentry *entry = NULL;
       
    37     char *ptr = NULL;
       
    38 
       
    39     entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
       
    40     BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
       
    41 
       
    42     location += (16 * fileCount);
       
    43 
       
    44     for (entry = entries; fileCount > 0; fileCount--, entry++)
       
    45     {
       
    46         GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 12), ERRPASS, failed);
       
    47         GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->size, 4), ERRPASS, failed);
       
    48         entry->name[12] = '\0';  /* name isn't null-terminated in file. */
       
    49         if ((ptr = strchr(entry->name, ' ')) != NULL)
       
    50             *ptr = '\0';  /* trim extra spaces. */
       
    51 
       
    52         entry->size = PHYSFS_swapULE32(entry->size);
       
    53         entry->startPos = location;
       
    54         location += entry->size;
       
    55     } /* for */
       
    56 
       
    57     return entries;
       
    58 
       
    59 failed:
       
    60     allocator.Free(entries);
       
    61     return NULL;
       
    62 } /* grpLoadEntries */
       
    63 
       
    64 
       
    65 static void *GRP_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
       
    66 {
       
    67     PHYSFS_uint8 buf[12];
       
    68     PHYSFS_uint32 count = 0;
       
    69     UNPKentry *entries = NULL;
       
    70 
       
    71     assert(io != NULL);  /* shouldn't ever happen. */
       
    72 
       
    73     BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
       
    74 
       
    75     BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, sizeof (buf)), ERRPASS, NULL);
       
    76     if (memcmp(buf, "KenSilverman", sizeof (buf)) != 0)
       
    77         BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
       
    78 
       
    79     BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof(count)), ERRPASS, NULL);
       
    80     count = PHYSFS_swapULE32(count);
       
    81 
       
    82     entries = grpLoadEntries(io, count);
       
    83     BAIL_IF_MACRO(!entries, ERRPASS, NULL);
       
    84     return UNPK_openArchive(io, entries, count);
       
    85 } /* GRP_openArchive */
       
    86 
       
    87 
       
    88 const PHYSFS_Archiver __PHYSFS_Archiver_GRP =
       
    89 {
       
    90     {
       
    91         "GRP",
       
    92         "Build engine Groupfile format",
       
    93         "Ryan C. Gordon <icculus@icculus.org>",
       
    94         "http://icculus.org/physfs/",
       
    95     },
       
    96     GRP_openArchive,        /* openArchive() method    */
       
    97     UNPK_enumerateFiles,    /* enumerateFiles() method */
       
    98     UNPK_openRead,          /* openRead() method       */
       
    99     UNPK_openWrite,         /* openWrite() method      */
       
   100     UNPK_openAppend,        /* openAppend() method     */
       
   101     UNPK_remove,            /* remove() method         */
       
   102     UNPK_mkdir,             /* mkdir() method          */
       
   103     UNPK_closeArchive,      /* closeArchive() method   */
       
   104     UNPK_stat               /* stat() method           */
       
   105 };
       
   106 
       
   107 #endif  /* defined PHYSFS_SUPPORTS_GRP */
       
   108 
       
   109 /* end of grp.c ... */
       
   110