src/archiver_qpak.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 29 Nov 2012 11:47:16 -0500
changeset 1324 d12c1c033a7d
parent 1322 5476917b8ddf
child 1326 ac0fac4fe6d6
permissions -rw-r--r--
Centralize responsibility for filtering symlinks during enumeration.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * QPAK support routines for PhysicsFS.
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 *  This archiver handles the archive format utilized by Quake 1 and 2.
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *  Quake3-based games use the PkZip/Info-Zip format (which our zip.c
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 *  archiver handles).
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *  ========================================================================
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *  This format info (in more detail) comes from:
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *     http://debian.fmi.uni-sofia.bg/~sergei/cgsr/docs/pak.txt
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *  Quake PAK Format
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *  Header
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *   (4 bytes)  signature = 'PACK'
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *   (4 bytes)  directory offset
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *   (4 bytes)  directory length
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *  Directory
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *   (56 bytes) file name
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 *   (4 bytes)  file position
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *   (4 bytes)  file length
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *  ========================================================================
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
    27
 * Please see the file LICENSE.txt in the source's root directory.
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 *
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
 *  This file written by Ryan C. Gordon.
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
 */
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
#define __PHYSICSFS_INTERNAL__
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
#include "physfs_internal.h"
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
1258
074d08049aa7 Changed so that this builds a reasonable default with no command line #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 1247
diff changeset
    35
#if PHYSFS_SUPPORTS_QPAK
074d08049aa7 Changed so that this builds a reasonable default with no command line #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 1247
diff changeset
    36
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    37
#define QPAK_SIG 0x4B434150   /* "PACK" in ASCII. */
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    39
static UNPKentry *qpakLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
{
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    41
    UNPKentry *entries = NULL;
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    42
    UNPKentry *entry = NULL;
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    44
    entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    45
    BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    46
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    47
    for (entry = entries; fileCount > 0; fileCount--, entry++)
928
0e37b8163248 Various archiver swap and compare functions now check if they are
Ryan C. Gordon <icculus@icculus.org>
parents: 852
diff changeset
    48
    {
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    49
        if (!__PHYSFS_readAll(io, &entry->name, 56)) goto failed;
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    50
        if (!__PHYSFS_readAll(io, &entry->startPos, 4)) goto failed;
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    51
        if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        entry->size = PHYSFS_swapULE32(entry->size);
1098
4e86cec1143f Moved all the file i/o from stdio-style to POSIX-style.
Ryan C. Gordon <icculus@icculus.org>
parents: 1054
diff changeset
    53
        entry->startPos = PHYSFS_swapULE32(entry->startPos);
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    } /* for */
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    56
    return entries;
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    57
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    58
failed:
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    59
    allocator.Free(entries);
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    60
    return NULL;
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    61
} /* qpakLoadEntries */
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    64
static void *QPAK_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
{
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    66
    UNPKentry *entries = NULL;
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    67
    PHYSFS_uint32 val = 0;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    68
    PHYSFS_uint32 pos = 0;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    69
    PHYSFS_uint32 count = 0;
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. */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
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: 1208
diff changeset
    73
    BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
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: 1208
diff changeset
    75
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1208
diff changeset
    76
    if (PHYSFS_swapULE32(val) != QPAK_SIG)
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1208
diff changeset
    77
        BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
581
712fedcbf02c Reimplemented qpak.c for relicensing.
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: 1208
diff changeset
    79
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    80
    pos = PHYSFS_swapULE32(val);  /* directory table offset. */
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    81
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1208
diff changeset
    82
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &val, 4), ERRPASS, NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    83
    count = PHYSFS_swapULE32(val);
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    84
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    85
    /* corrupted archive? */
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1208
diff changeset
    86
    BAIL_IF_MACRO((count % 64) != 0, PHYSFS_ERR_CORRUPT, NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    87
    count /= 64;
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    88
1240
22d4d1bd4e21 Reworked the error reporting API. Now we use error codes instead of strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 1208
diff changeset
    89
    BAIL_IF_MACRO(!io->seek(io, pos), ERRPASS, NULL);
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    90
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    91
    entries = qpakLoadEntries(io, count);
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    92
    BAIL_IF_MACRO(!entries, ERRPASS, NULL);
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
    93
    return UNPK_openArchive(io, entries, count);
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
} /* QPAK_openArchive */
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
658
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_QPAK =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    98
{
1322
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1320
diff changeset
    99
    CURRENT_PHYSFS_ARCHIVER_API_VERSION,
1281
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
   100
    {
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
   101
        "PAK",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
   102
        "Quake I/II format",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
   103
        "Ryan C. Gordon <icculus@icculus.org>",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
   104
        "http://icculus.org/physfs/",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
   105
    },
1324
d12c1c033a7d Centralize responsibility for filtering symlinks during enumeration.
Ryan C. Gordon <icculus@icculus.org>
parents: 1322
diff changeset
   106
    0,  /* supportsSymlinks */
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   107
    QPAK_openArchive,       /* openArchive() method    */
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   108
    UNPK_enumerateFiles,    /* enumerateFiles() method */
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   109
    UNPK_openRead,          /* openRead() method       */
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   110
    UNPK_openWrite,         /* openWrite() method      */
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   111
    UNPK_openAppend,        /* openAppend() method     */
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   112
    UNPK_remove,            /* remove() method         */
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   113
    UNPK_mkdir,             /* mkdir() method          */
1320
c1da17c10a41 Cleaned up some simple comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1281
diff changeset
   114
    UNPK_closeArchive,      /* closeArchive() method   */
1261
cd5a31f4602a Moved QPAK support into archiver_unpacked.c
Ryan C. Gordon <icculus@icculus.org>
parents: 1259
diff changeset
   115
    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
   116
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   117
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
#endif  /* defined PHYSFS_SUPPORTS_QPAK */
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
1320
c1da17c10a41 Cleaned up some simple comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1281
diff changeset
   120
/* end of archiver_qpak.c ... */
581
712fedcbf02c Reimplemented qpak.c for relicensing.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121