misc/libphysfs/archiver_grp.c
branchui-scaling
changeset 15283 c4fd2813b127
parent 13390 0135e64c6c66
parent 15279 7ab5cf405686
child 15663 d92eeb468dad
equal deleted inserted replaced
13390:0135e64c6c66 15283:c4fd2813b127
     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     CURRENT_PHYSFS_ARCHIVER_API_VERSION,
       
    91     {
       
    92         "GRP",
       
    93         "Build engine Groupfile format",
       
    94         "Ryan C. Gordon <icculus@icculus.org>",
       
    95         "https://icculus.org/physfs/",
       
    96         0,  /* supportsSymlinks */
       
    97     },
       
    98     GRP_openArchive,
       
    99     UNPK_enumerateFiles,
       
   100     UNPK_openRead,
       
   101     UNPK_openWrite,
       
   102     UNPK_openAppend,
       
   103     UNPK_remove,
       
   104     UNPK_mkdir,
       
   105     UNPK_stat,
       
   106     UNPK_closeArchive
       
   107 };
       
   108 
       
   109 #endif  /* defined PHYSFS_SUPPORTS_GRP */
       
   110 
       
   111 /* end of archiver_grp.c ... */
       
   112