misc/libphysfs/archiver_mvl.c
author unc0rr
Fri, 30 Mar 2018 20:42:24 +0200
changeset 13311 806347b3c978
parent 12218 bb5522e88ab2
permissions -rw-r--r--
Improve error message a bit
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
 * MVL 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 II Movielib archives.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     5
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     6
 * The file format of MVL is quite easy...
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     7
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     8
 *   //MVL File format - Written by Heiko Herrmann
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
     9
 *   char sig[4] = {'D','M', 'V', 'L'}; // "DMVL"=Descent MoVie Library
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    10
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    11
 *   int num_files; // the number of files in this MVL
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    12
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    13
 *   struct {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    14
 *    char file_name[13]; // Filename, padded to 13 bytes with 0s
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    15
 *    int file_size; // filesize in bytes
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    16
 *   }DIR_STRUCT[num_files];
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 data[file_size]; // The file data
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    20
 *   }FILE_STRUCT[num_files];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    21
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    22
 * (That info is from http://www.descent2.com/ddn/specs/mvl/)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    23
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    24
 * Please see the file LICENSE.txt in the source's root directory.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    25
 *
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    26
 *  This file written by Bradley Bell.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    27
 *  Based on grp.c by Ryan C. Gordon.
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    28
 */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    29
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    30
#define __PHYSICSFS_INTERNAL__
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    31
#include "physfs_internal.h"
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    32
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    33
#if PHYSFS_SUPPORTS_MVL
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    34
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    35
static UNPKentry *mvlLoadEntries(PHYSFS_Io *io, PHYSFS_uint32 fileCount)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    36
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    37
    PHYSFS_uint32 location = 8;  /* sizeof sig. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    38
    UNPKentry *entries = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    39
    UNPKentry *entry = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    40
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    41
    entries = (UNPKentry *) allocator.Malloc(sizeof (UNPKentry) * fileCount);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    42
    BAIL_IF_MACRO(entries == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    43
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    44
    location += (17 * fileCount);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    45
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    46
    for (entry = entries; fileCount > 0; fileCount--, entry++)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    47
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    48
        if (!__PHYSFS_readAll(io, &entry->name, 13)) goto failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    49
        if (!__PHYSFS_readAll(io, &entry->size, 4)) goto failed;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    50
        entry->size = PHYSFS_swapULE32(entry->size);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    51
        entry->startPos = location;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    52
        location += entry->size;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    53
    } /* for */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    54
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    55
    return entries;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    56
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    57
failed:
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    58
    allocator.Free(entries);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    59
    return NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    60
} /* mvlLoadEntries */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    61
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    62
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    63
static void *MVL_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    64
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    65
    PHYSFS_uint8 buf[4];
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    66
    PHYSFS_uint32 count = 0;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    67
    UNPKentry *entries = NULL;
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    68
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    69
    assert(io != NULL);  /* shouldn't ever happen. */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    70
    BAIL_IF_MACRO(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    71
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, buf, 4), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    72
    BAIL_IF_MACRO(memcmp(buf, "DMVL", 4) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    73
    BAIL_IF_MACRO(!__PHYSFS_readAll(io, &count, sizeof(count)), ERRPASS, NULL);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    74
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    75
    count = PHYSFS_swapULE32(count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    76
    entries = mvlLoadEntries(io, count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    77
    return (!entries) ? NULL : UNPK_openArchive(io, entries, count);
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    78
} /* MVL_openArchive */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    79
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    80
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    81
const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    82
{
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
    83
    CURRENT_PHYSFS_ARCHIVER_API_VERSION,
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    84
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    85
        "MVL",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    86
        "Descent II Movielib format",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    87
        "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
    88
        "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
    89
        0,  /* supportsSymlinks */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    90
    },
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
    91
    MVL_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
    92
    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
    93
    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
    94
    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
    95
    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
    96
    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
    97
    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
    98
    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
    99
    UNPK_closeArchive
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
#endif  /* defined PHYSFS_SUPPORTS_MVL */
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
/* end of archiver_mvl.c ... */
7768
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105