src/physfs_archiver_mvl.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 17 May 2020 01:41:52 -0400
changeset 1691 c66c9326b05d
parent 1571 fbd9698219c1
permissions -rw-r--r--
7z: Make error handling a little more robust.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
/*
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
 * MVL support routines for PhysicsFS.
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
 * This driver handles Descent II Movielib archives.
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
 * The file format of MVL is quite easy...
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
 *   //MVL File format - Written by Heiko Herrmann
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
 *   char sig[4] = {'D','M', 'V', 'L'}; // "DMVL"=Descent MoVie Library
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
 *   int num_files; // the number of files in this MVL
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
 *   struct {
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
 *    char file_name[13]; // Filename, padded to 13 bytes with 0s
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
 *    int file_size; // filesize in bytes
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
 *   }DIR_STRUCT[num_files];
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
 *   struct {
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
 *    char data[file_size]; // The file data
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
 *   }FILE_STRUCT[num_files];
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
 * (That info is from http://www.descent2.com/ddn/specs/mvl/)
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
 *
809
116b8fe30371 Renamed LICENSE to LICENSE.txt
Ryan C. Gordon <icculus@icculus.org>
parents: 808
diff changeset
    24
 * Please see the file LICENSE.txt in the source's root directory.
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
 *
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
 *  This file written by Bradley Bell.
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
 *  Based on grp.c by Ryan C. Gordon.
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
 */
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
#define __PHYSICSFS_INTERNAL__
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
#include "physfs_internal.h"
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
1258
074d08049aa7 Changed so that this builds a reasonable default with no command line #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
    33
#if PHYSFS_SUPPORTS_MVL
074d08049aa7 Changed so that this builds a reasonable default with no command line #defines.
Ryan C. Gordon <icculus@icculus.org>
parents: 1240
diff changeset
    34
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    35
static int mvlLoadEntries(PHYSFS_Io *io, const PHYSFS_uint32 count, void *arc)
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
{
1504
803282f78987 Minor code cleanups: whitespace fixes, 80-column overflows, wrong comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1498
diff changeset
    37
    PHYSFS_uint32 pos = 8 + (17 * count);   /* past sig+metadata. */
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    38
    PHYSFS_uint32 i;
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    40
    for (i = 0; i < count; i++)
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    {
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    42
        PHYSFS_uint32 size;
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    43
        char name[13];
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    44
        BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 13), 0);
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    45
        BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &size, 4), 0);
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    46
        name[12] = '\0';  /* just in case. */
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    47
        size = PHYSFS_swapULE32(size);
1504
803282f78987 Minor code cleanups: whitespace fixes, 80-column overflows, wrong comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1498
diff changeset
    48
        BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, pos, size), 0);
803282f78987 Minor code cleanups: whitespace fixes, 80-column overflows, wrong comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1498
diff changeset
    49
        pos += size;
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
    } /* for */
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    52
    return 1;
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    53
} /* mvlLoadEntries */
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
1571
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    56
static void *MVL_openArchive(PHYSFS_Io *io, const char *name,
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    57
                             int forWriting, int *claimed)
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
{
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    59
    PHYSFS_uint8 buf[4];
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    60
    PHYSFS_uint32 count = 0;
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    61
    void *unpkarc;
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
1118
2e09fc635fdd Abstracted file i/o into PHYSFS_Io interface.
Ryan C. Gordon <icculus@icculus.org>
parents: 1115
diff changeset
    63
    assert(io != NULL);  /* shouldn't ever happen. */
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    64
    BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL);
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    65
    BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, buf, 4), NULL);
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    66
    BAIL_IF(memcmp(buf, "DMVL", 4) != 0, PHYSFS_ERR_UNSUPPORTED, NULL);
1571
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    67
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    68
    *claimed = 1;
fbd9698219c1 Archivers can now specify whether an archive definitely was intended for them.
Ryan C. Gordon <icculus@icculus.org>
parents: 1559
diff changeset
    69
1402
c5a5dadb901d Cleanup BAIL_* and GOTO_* macros to be less cluttered.
Ryan C. Gordon <icculus@icculus.org>
parents: 1373
diff changeset
    70
    BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &count, sizeof(count)), NULL);
1203
55f147714ce2 Cleaned up all the readAll() cut and paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 1129
diff changeset
    71
    count = PHYSFS_swapULE32(count);
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    72
1494
4e2c7ce9bf11 DirTrees and unpacked archives shouldn't demand a count of entries for init.
Ryan C. Gordon <icculus@icculus.org>
parents: 1463
diff changeset
    73
    unpkarc = UNPK_openArchive(io);
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    74
    BAIL_IF_ERRPASS(!unpkarc, NULL);
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    75
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    76
    if (!mvlLoadEntries(io, count, unpkarc))
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    77
    {
1497
b9d771d6805f unpacked: let archivers "abandon" the archive instead of closing it.
Ryan C. Gordon <icculus@icculus.org>
parents: 1494
diff changeset
    78
        UNPK_abandonArchive(unpkarc);
1463
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    79
        return NULL;
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    80
    } /* if */
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    81
c23e55bad32d Reworked the "unpacked" archivers to use DirTree.
Ryan C. Gordon <icculus@icculus.org>
parents: 1402
diff changeset
    82
    return unpkarc;
1128
067d8e76261e Moved most the cut-and-paste between simple archivers to one file.
Ryan C. Gordon <icculus@icculus.org>
parents: 1125
diff changeset
    83
} /* MVL_openArchive */
1054
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    84
57f4af811ffb THIS is Christoph's PHYSFS_stat() work.
Ryan C. Gordon <icculus@icculus.org>
parents: 1053
diff changeset
    85
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    86
const PHYSFS_Archiver __PHYSFS_Archiver_MVL =
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
    87
{
1322
5476917b8ddf Allow application-supplied archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 1320
diff changeset
    88
    CURRENT_PHYSFS_ARCHIVER_API_VERSION,
1281
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    89
    {
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    90
        "MVL",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    91
        "Descent II Movielib format",
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    92
        "Bradley Bell <btb@icculus.org>",
1373
527ef3c6a2d6 HTTPS all the things.
Ryan C. Gordon <icculus@icculus.org>
parents: 1331
diff changeset
    93
        "https://icculus.org/physfs/",
1326
ac0fac4fe6d6 Moved supportsSymlinks into PHYSFS_ArchiveInfo.
Ryan C. Gordon <icculus@icculus.org>
parents: 1324
diff changeset
    94
        0,  /* supportsSymlinks */
1281
cd136b8d3b1a Moved PHYSFS_ArchiveInfo into PHYSFS_Archiver.
Ryan C. Gordon <icculus@icculus.org>
parents: 1270
diff changeset
    95
    },
1330
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
    96
    MVL_openArchive,
1559
aaee2b83291f Reworked enumeration to be more powerful.
Ryan C. Gordon <icculus@icculus.org>
parents: 1507
diff changeset
    97
    UNPK_enumerate,
1330
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
    98
    UNPK_openRead,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
    99
    UNPK_openWrite,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   100
    UNPK_openAppend,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   101
    UNPK_remove,
ce6b4a66b3eb Removed comments added by Captain Obvious.
Ryan C. Gordon <icculus@icculus.org>
parents: 1326
diff changeset
   102
    UNPK_mkdir,
1331
eb06a37afa7e Rearranged PHYSFS_Archiver struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1330
diff changeset
   103
    UNPK_stat,
eb06a37afa7e Rearranged PHYSFS_Archiver struct.
Ryan C. Gordon <icculus@icculus.org>
parents: 1330
diff changeset
   104
    UNPK_closeArchive
658
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   105
};
1981818c6170 Removed all the forward declaration cruft from the archivers.
Ryan C. Gordon <icculus@icculus.org>
parents: 657
diff changeset
   106
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
#endif  /* defined PHYSFS_SUPPORTS_MVL */
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
1507
8953b8a171e7 Update filename comments.
Ryan C. Gordon <icculus@icculus.org>
parents: 1506
diff changeset
   109
/* end of physfs_archiver_mvl.c ... */
553
4338d9c0bbcd Descent I/II HOG and MVL archive support (thanks, Bradley Bell!).
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110