misc/libphysfs/archiver_hog.c
author sheepluva
Fri, 29 Sep 2017 22:33:15 +0200
changeset 12598 d6bd8b327a09
parent 12218 bb5522e88ab2
permissions -rw-r--r--
don't use colorkey for land spray this fixes issue #220
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     1
/*
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     2
 * HOG support routines for PhysicsFS.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     3
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     4
 * This driver handles Descent I/II HOG archives.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 * The format is very simple:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
 *   The file always starts with the 3-byte signature "DHF" (Descent
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
 *   HOG file). After that the files of a HOG are just attached after
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
 *   another, divided by a 17 bytes header, which specifies the name
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
 *   and length (in bytes) of the forthcoming file! So you just read
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
 *   the header with its information of how big the following file is,
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
 *   and then skip exact that number of bytes to get to the next file
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
 *   in that HOG.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
 *    char sig[3] = {'D', 'H', 'F'}; // "DHF"=Descent HOG File
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    17
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    18
 *    struct {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    19
 *     char file_name[13]; // Filename, padded to 13 bytes with 0s
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
 *     int file_size; // filesize in bytes
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
 *     char data[file_size]; // The file data
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
 *    } FILE_STRUCT; // Repeated until the end of the file.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
 * (That info is from http://www.descent2.com/ddn/specs/hog/)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
 *  This file written by Bradley Bell.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
 *  Based on grp.c by Ryan C. Gordon.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
#if PHYSFS_SUPPORTS_HOG
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
static UNPKentry *hogLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 *_entCount)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
    const PHYSFS_uint64 iolen = io->length(io);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
    PHYSFS_uint32 entCount = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
    void *ptr = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
    UNPKentry *entries = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
    UNPKentry *entry = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
    PHYSFS_uint32 size = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
    PHYSFS_uint32 pos = 3;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
    while (pos < iolen)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
        entCount++;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
        ptr = allocator.Realloc(ptr, sizeof (UNPKentry) * entCount);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
        GOTO_IF_MACRO(ptr == NULL, PHYSFS_ERR_OUT_OF_MEMORY, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
        entries = (UNPKentry *) ptr;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
        entry = &entries[entCount-1];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &entry->name, 13), ERRPASS, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
        pos += 13;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
        GOTO_IF_MACRO(!__PHYSFS_readAll(io, &size, 4), ERRPASS, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
        pos += 4;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
        entry->size = PHYSFS_swapULE32(size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
        entry->startPos = pos;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
        pos += size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
        /* skip over entry */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
        GOTO_IF_MACRO(!io->seek(io, pos), ERRPASS, failed);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
    } /* while */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
    *_entCount = entCount;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
    return entries;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    allocator.Free(entries);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
} /* hogLoadEntries */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
static void *HOG_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
    PHYSFS_uint8 buf[3];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
    PHYSFS_uint32 count = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
    UNPKentry *entries = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    83
    assert(io != NULL);  /* shouldn't ever happen. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
    BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 3), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
    BAIL_IF_MACRO(memcmp(buf, "DHF", 3) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    88
    entries = hogLoadEntries(io, &count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    89
    BAIL_IF_MACRO(!entries, ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
    return UNPK_openArchive(io, entries, count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    91
} /* HOG_openArchive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    92
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    93
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    94
const PHYSFS_Archiver __PHYSFS_Archiver_HOG =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    95
{
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
    96
    CURRENT_PHYSFS_ARCHIVER_API_VERSION,
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
        "HOG",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
        "Descent I/II HOG file format",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
        "Bradley Bell <btb@icculus.org>",
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   101
        "https://icculus.org/physfs/",
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   102
        0,  /* supportsSymlinks */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
    },
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   104
    HOG_openArchive,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   105
    UNPK_enumerateFiles,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   106
    UNPK_openRead,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   107
    UNPK_openWrite,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   108
    UNPK_openAppend,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   109
    UNPK_remove,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   110
    UNPK_mkdir,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   111
    UNPK_stat,
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   112
    UNPK_closeArchive
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
#endif  /* defined PHYSFS_SUPPORTS_HOG */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116
12218
bb5522e88ab2 bulk copy of latest physfs to our misc/libphysfs since this seems to fix an off-by-1 error reliably hit in readln read of 1 byte probably introduced in the addition of the buffered read. Whether this is excessive or whether libphysfs should even be maintained by us is another matter. But at least we shouldn't crash
nemo
parents: 8524
diff changeset
   117
/* end of archiver_hog.c ... */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   118