misc/libphysfs/archiver_hog.c
author unc0rr
Thu, 27 Feb 2014 12:48:27 +0400
changeset 10163 b994afa40326
parent 8524 a65e9bcf0a03
child 12218 bb5522e88ab2
permissions -rw-r--r--
Oops, remove debug stuff which hid the feature
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
{
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    96
    {
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    97
        "HOG",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    98
        "Descent I/II HOG file format",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
    99
        "Bradley Bell <btb@icculus.org>",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   100
        "http://icculus.org/physfs/",
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   101
    },
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   102
    HOG_openArchive,         /* openArchive() method    */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   103
    UNPK_enumerateFiles,     /* enumerateFiles() method */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   104
    UNPK_openRead,           /* openRead() method       */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   105
    UNPK_openWrite,          /* openWrite() method      */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   106
    UNPK_openAppend,         /* openAppend() method     */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   107
    UNPK_remove,             /* remove() method         */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   108
    UNPK_mkdir,              /* mkdir() method          */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   109
    UNPK_closeArchive,       /* closeArchive() method   */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   110
    UNPK_stat                /* stat() method           */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   111
};
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   112
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   113
#endif  /* defined PHYSFS_SUPPORTS_HOG */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   114
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   115
/* end of hog.c ... */
13e2037ebc79 Try using PhysicsFS.
unc0rr
parents:
diff changeset
   116