|
| 1 | +/* |
| 2 | + * tar support routines for PhysicsFS. |
| 3 | + * |
| 4 | + * Please see the file LICENSE.txt in the source's root directory. |
| 5 | + * |
| 6 | + * based on code by Uli Köhler: https://techoverflow.net/2013/03/29/reading-tar-files-in-c/ |
| 7 | + */ |
| 8 | + |
| 9 | +#define __PHYSICSFS_INTERNAL__ |
| 10 | +#include "physfs_internal.h" |
| 11 | + |
| 12 | +#if PHYSFS_SUPPORTS_TAR |
| 13 | +#include "physfs_tar.h" |
| 14 | + |
| 15 | +static bool TAR_loadEntries(PHYSFS_Io *io, void *arc) |
| 16 | +{ |
| 17 | + union block zero_block; |
| 18 | + union block current_block; |
| 19 | + PHYSFS_uint64 count = 0; |
| 20 | + |
| 21 | + memset(zero_block.buffer, 0, sizeof(BLOCKSIZE)); |
| 22 | + memset(current_block.buffer, 0, sizeof(BLOCKSIZE)); |
| 23 | + |
| 24 | + /* read header block until zero-only terminated block */ |
| 25 | + for(; __PHYSFS_readAll(io, current_block.buffer, BLOCKSIZE); count++) |
| 26 | + { |
| 27 | + if( memcmp(current_block.buffer, zero_block.buffer, BLOCKSIZE) == 0 ) |
| 28 | + return true; |
| 29 | + |
| 30 | + /* verify magic */ |
| 31 | + switch(TAR_magic(¤t_block)) |
| 32 | + { |
| 33 | + case POSIX_FORMAT: |
| 34 | + TAR_posix_block(io, arc, ¤t_block, &count); |
| 35 | + break; |
| 36 | + case OLDGNU_FORMAT: |
| 37 | + break; |
| 38 | + case GNU_FORMAT: |
| 39 | + break; |
| 40 | + case V7_FORMAT: |
| 41 | + break; |
| 42 | + case USTAR_FORMAT: |
| 43 | + break; |
| 44 | + case STAR_FORMAT: |
| 45 | + break; |
| 46 | + default: |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return false; |
| 52 | +} |
| 53 | + |
| 54 | +static void *TAR_openArchive(PHYSFS_Io *io, const char *name, |
| 55 | + int forWriting, int *claimed) |
| 56 | +{ |
| 57 | + void *unpkarc = NULL; |
| 58 | + union block first; |
| 59 | + enum archive_format format; |
| 60 | + |
| 61 | + assert(io != NULL); /* shouldn't ever happen. */ |
| 62 | + |
| 63 | + BAIL_IF(forWriting, PHYSFS_ERR_READ_ONLY, NULL); |
| 64 | + |
| 65 | + BAIL_IF_ERRPASS(!__PHYSFS_readAll(io, first.buffer, BLOCKSIZE), NULL); |
| 66 | + format = TAR_magic(&first); |
| 67 | + io->seek(io, 0); |
| 68 | + *claimed = format == DEFAULT_FORMAT ? 0 : 1; |
| 69 | + |
| 70 | + unpkarc = UNPK_openArchive(io, 0, 1); |
| 71 | + BAIL_IF_ERRPASS(!unpkarc, NULL); |
| 72 | + |
| 73 | + if (!TAR_loadEntries(io, unpkarc)) |
| 74 | + { |
| 75 | + UNPK_abandonArchive(unpkarc); |
| 76 | + return NULL; |
| 77 | + } /* if */ |
| 78 | + |
| 79 | + |
| 80 | + return unpkarc; |
| 81 | +} /* TAR_openArchive */ |
| 82 | + |
| 83 | + |
| 84 | +const PHYSFS_Archiver __PHYSFS_Archiver_TAR = |
| 85 | +{ |
| 86 | + CURRENT_PHYSFS_ARCHIVER_API_VERSION, |
| 87 | + { |
| 88 | + "TAR", |
| 89 | + "POSIX tar archives / Chasm: the Rift Remastered", |
| 90 | + "Jon Daniel <jondaniel879@gmail.com>", |
| 91 | + "http://www.gnu.org/software/tar/", |
| 92 | + 0, |
| 93 | + }, |
| 94 | + TAR_openArchive, |
| 95 | + UNPK_enumerate, |
| 96 | + UNPK_openRead, |
| 97 | + UNPK_openWrite, |
| 98 | + UNPK_openAppend, |
| 99 | + UNPK_remove, |
| 100 | + UNPK_mkdir, |
| 101 | + UNPK_stat, |
| 102 | + UNPK_closeArchive |
| 103 | +}; |
| 104 | + |
| 105 | +#endif /* defined PHYSFS_SUPPORTS_TAR */ |
| 106 | + |
| 107 | +/* end of physfs_archiver_tar.c ... */ |
| 108 | + |
0 commit comments