|
| 1 | +/* |
| 2 | + * POD support routines for PhysicsFS. |
| 3 | + * |
| 4 | + * This driver handles Terminal Velocity POD archives. |
| 5 | + * |
| 6 | + * The POD format is as follows: |
| 7 | + * |
| 8 | + * While there is no known signature, the file starts with an |
| 9 | + * unsigned 32-bit integer with the file count, followed by |
| 10 | + * an 80-character, null-padded string with a description of |
| 11 | + * the pod file. The file entries follow, and are as such: |
| 12 | + * |
| 13 | + * struct { |
| 14 | + * char file_path[32]; // Full file path, IE: "ART\VGA.ACT" |
| 15 | + * uint32_t file_size; // filesize in bytes |
| 16 | + * uint32_t offset; // The offset of the data. |
| 17 | + * } fileEntry_t; // Once per file. The file data itself is at offset after all entries. |
| 18 | + * |
| 19 | + * (This info is from: https://moddingwiki.shikadi.net/wiki/POD_Format) |
| 20 | + * |
| 21 | + * There are other POD file formats, but they haven't been implemented yet. |
| 22 | + * |
| 23 | + * Please see the file LICENSE.txt in the source's root directory. |
| 24 | + * |
| 25 | + * This file written by Jordon Moss. |
| 26 | + */ |
| 27 | + |
| 28 | +#define __PHYSICSFS_INTERNAL__ |
| 29 | +#include "physfs_internal.h" |
| 30 | + |
| 31 | +#if PHYSFS_SUPPORTS_POD |
| 32 | + |
| 33 | +static int readui32(PHYSFS_Io *io, PHYSFS_uint32 *val) |
| 34 | +{ |
| 35 | + PHYSFS_uint32 v; |
| 36 | + BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, &v, sizeof (v)), 0); |
| 37 | + *val = PHYSFS_swapULE32(v); |
| 38 | + return 1; |
| 39 | +} /* readui32 */ |
| 40 | + |
| 41 | +static void replaceChar(char* str, char oldChar, char newChar) |
| 42 | +{ |
| 43 | + int i = 0; |
| 44 | + while (str[i] != '\0') |
| 45 | + { // Iterate until the null terminator is found |
| 46 | + if (str[i] == oldChar) |
| 47 | + { |
| 48 | + str[i] = newChar; // Replace the character |
| 49 | + } |
| 50 | + i++; |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +static int pod1LoadEntries(PHYSFS_Io *io, void *arc) |
| 55 | +{ |
| 56 | + PHYSFS_uint32 numfiles; |
| 57 | + PHYSFS_uint32 i; |
| 58 | + |
| 59 | + BAIL_IF_ERRPASS(!readui32(io, &numfiles), 0); |
| 60 | + BAIL_IF_ERRPASS(!io->seek(io, 84), 0); /* skip past description */ |
| 61 | + |
| 62 | + for (i = 0; i < numfiles; i++) { |
| 63 | + char name[33]; |
| 64 | + PHYSFS_uint32 size; |
| 65 | + PHYSFS_uint32 offset; |
| 66 | + BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, name, 32), 0); |
| 67 | + BAIL_IF_ERRPASS(!readui32(io, &size), 0); |
| 68 | + BAIL_IF_ERRPASS(!readui32(io, &offset), 0); |
| 69 | + name[32] = '\0'; /* just in case */ |
| 70 | + |
| 71 | + replaceChar(name, '\\', '/'); |
| 72 | + |
| 73 | + BAIL_IF_ERRPASS(!UNPK_addEntry(arc, name, 0, -1, -1, offset, size), 0); |
| 74 | + } |
| 75 | + |
| 76 | + return 1; |
| 77 | +} /* pod1LoadEntries */ |
| 78 | + |
| 79 | + |
| 80 | +static void *POD_openArchive(PHYSFS_Io *io, const char *name, |
| 81 | + int forWriting, int *claimed) |
| 82 | +{ |
| 83 | + void *unpkarc = NULL; |
| 84 | + PHYSFS_uint32 dummy; |
| 85 | + char description[80]; |
| 86 | + |
| 87 | + assert(io != NULL); /* shouldn't ever happen. */ |
| 88 | + BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL); |
| 89 | + |
| 90 | + BAIL_IF_ERRPASS(!readui32(io, &dummy), 0); |
| 91 | + BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, description, 80), NULL); |
| 92 | + if ((description[0] == 0) || (description[79] != 0)) // Check if we're lacking a description or last char isn't a null terminator. |
| 93 | + return NULL; |
| 94 | + |
| 95 | + io->seek(io, 0); |
| 96 | + |
| 97 | + *claimed = 1; |
| 98 | + |
| 99 | + unpkarc = UNPK_openArchive(io, 0, 1); |
| 100 | + BAIL_IF_ERRPASS(!unpkarc, NULL); |
| 101 | + |
| 102 | + if (!(pod1LoadEntries(io, unpkarc))) |
| 103 | + { |
| 104 | + UNPK_abandonArchive(unpkarc); |
| 105 | + return NULL; |
| 106 | + } /* if */ |
| 107 | + |
| 108 | + return unpkarc; |
| 109 | +} /* POD_openArchive */ |
| 110 | + |
| 111 | + |
| 112 | +const PHYSFS_Archiver __PHYSFS_Archiver_POD = |
| 113 | +{ |
| 114 | + CURRENT_PHYSFS_ARCHIVER_API_VERSION, |
| 115 | + { |
| 116 | + "POD", |
| 117 | + "Terminal Reality POD file format", |
| 118 | + "Jordon Moss <mossj32@gmail.com>", |
| 119 | + "https://icculus.org/physfs/", |
| 120 | + 0, /* supportsSymlinks */ |
| 121 | + }, |
| 122 | + POD_openArchive, |
| 123 | + UNPK_enumerate, |
| 124 | + UNPK_openRead, |
| 125 | + UNPK_openWrite, |
| 126 | + UNPK_openAppend, |
| 127 | + UNPK_remove, |
| 128 | + UNPK_mkdir, |
| 129 | + UNPK_stat, |
| 130 | + UNPK_closeArchive |
| 131 | +}; |
| 132 | + |
| 133 | +#endif /* defined PHYSFS_SUPPORTS_POD */ |
| 134 | + |
| 135 | +/* end of physfs_archiver_pod.c ... */ |
| 136 | + |
0 commit comments