src/archiver_slb.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 06 Jul 2017 11:51:41 -0400
changeset 1402 c5a5dadb901d
parent 1373 527ef3c6a2d6
child 1463 c23e55bad32d
permissions -rw-r--r--
Cleanup BAIL_* and GOTO_* macros to be less cluttered. This also fixes really aggressive compiler warnings about the "if (!ERRPASS)" construct producing unreachable code.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     1
/*
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     2
 * SLB support routines for PhysicsFS.
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     3
 *
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     4
 * This driver handles SLB archives ("slab files"). This uncompressed format
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     5
 * is used in I-War / Independence War and Independence War: Defiance.
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     6
 *
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     7
 * The format begins with four zero bytes (version?), the file count and the
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     8
 * location of the table of contents. Each ToC entry contains a 64-byte buffer
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
     9
 * containing a zero-terminated filename, the offset of the data, and its size.
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    10
 * All the filenames begin with the separator character '\'. 
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    11
 *
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    12
 * Please see the file LICENSE.txt in the source's root directory.
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    13
 *
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    14
 * This file written by Aleksi Nurmi, based on the GRP archiver by
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    15
 * Ryan C. Gordon.
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    16
 */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    17
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    18
#define __PHYSICSFS_INTERNAL__
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    19
#include "physfs_internal.h"
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    20
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    21
#if PHYSFS_SUPPORTS_SLB
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    22
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    23
static UNPKentry *slbLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    24
{
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    25
    UNPKentry *entries = NULL;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    26
    UNPKentry *entry = NULL;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    27
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    28
    entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    29
    BAIL_IF(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    30
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    31
    for (entry = entries; fileCount > 0; fileCount--, entry++)
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    32
    {
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    33
        char *ptr;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    34
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    35
        /* don't include the '\' in the beginning */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    36
        char backslash;
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    37
        GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &backslash, 1), failed);
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    38
        GOTO_IF_ERRPASS(backslash != '\\', failed);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    39
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    40
        /* read the rest of the buffer, 63 bytes */
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    41
        GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->name, 63), failed);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    42
        entry->name[63] = '\0'; /* in case the name lacks the null terminator */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    43
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    44
        /* convert backslashes */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    45
        for (ptr = entry->name; *ptr; ptr++)
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    46
        {
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    47
            if (*ptr == '\\')
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    48
                *ptr = '/';
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    49
        } /* for */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    50
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    51
        GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->startPos, 4), failed);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    52
        entry->startPos = PHYSFS_swapULE32(entry->startPos);
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    53
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    54
        GOTO_IF_ERRPASS(!__PHYSFS_readAll(io, &entry->size, 4), failed);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    55
        entry->size = PHYSFS_swapULE32(entry->size);
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    56
    } /* for */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    57
    
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    58
    return entries;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    59
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    60
failed:
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    61
    allocator.Free(entries);
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    62
    return NULL;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    63
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    64
} /* slbLoadEntries */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    65
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    66
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    67
static void *SLB_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    68
{
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    69
    PHYSFS_uint32 version;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    70
    PHYSFS_uint32 count = 0;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    71
    PHYSFS_uint32 tocPos = 0;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    72
    UNPKentry *entries = NULL;
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    73
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    74
    assert(io != NULL);  /* shouldn't ever happen. */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
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(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    77
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    78
    BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &version, sizeof(version)), NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    79
    version = PHYSFS_swapULE32(version);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    80
    BAIL_IF(version != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    81
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    82
    BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    83
    count = PHYSFS_swapULE32(count);
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    84
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    85
    /* offset of the table of contents */
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    86
    BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &tocPos, sizeof(tocPos)), NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    87
    tocPos = PHYSFS_swapULE32(tocPos);
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    88
    
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    89
    /* seek to the table of contents */
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    90
    BAIL_IF_ERRPASS(!io->seek(io, tocPos), NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    91
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    92
    entries = slbLoadEntries(io, count);
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    93
    BAIL_IF_ERRPASS(!entries, NULL);
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    94
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    95
    return UNPK_openArchive(io, entries, count);
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    96
} /* SLB_openArchive */
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    97
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    98
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
    99
const PHYSFS_Archiver __PHYSFS_Archiver_SLB =
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   100
{
1322
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1320
diff changeset
   101
    CURRENT_PHYSFS_ARCHIVER_API_VERSION,
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   102
    {
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   103
        "SLB",
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   104
        "I-War / Independence War Slab file",
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   105
        "Aleksi Nurmi <aleksi.nurmi@gmail.com>",
1373
527ef3c6a2d6 HTTPS all the things.
Ryan C. Gordon <icculus@icculus.org>
parents: 1331
diff changeset
   106
        "https://bitbucket.org/ahnurmi/",
1326
ac0fac4fe6d6 Moved supportsSymlinks into PHYSFS_ArchiveInfo.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
   107
        0,  /* supportsSymlinks */
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   108
    },
1330
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   109
    SLB_openArchive,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   110
    UNPK_enumerateFiles,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   111
    UNPK_openRead,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   112
    UNPK_openWrite,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   113
    UNPK_openAppend,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   114
    UNPK_remove,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   115
    UNPK_mkdir,
1331
eb06a37afa7e Rearranged PHYSFS_Archiver struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1330
diff changeset
   116
    UNPK_stat,
eb06a37afa7e Rearranged PHYSFS_Archiver struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1330
diff changeset
   117
    UNPK_closeArchive
1315
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   118
};
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   119
c686e4f84ab5 SLB archiver
Aleksi Nurmi <aleksi.nurmi@gmail.com>
parents:
diff changeset
   120
#endif  /* defined PHYSFS_SUPPORTS_SLB */
1320
c1da17c10a41 Cleaned up some simple comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1315
diff changeset
   121
c1da17c10a41 Cleaned up some simple comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1315
diff changeset
   122
/* end of archiver_slb.c ... */
c1da17c10a41 Cleaned up some simple comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1315
diff changeset
   123