|
| 1 | +/* |
| 2 | + Created on: Nov 22, 2018 |
| 3 | +
|
| 4 | + Copyright 2018 flyinghead |
| 5 | +
|
| 6 | + This file is part of reicast. |
| 7 | +
|
| 8 | + reicast is free software: you can redistribute it and/or modify |
| 9 | + it under the terms of the GNU General Public License as published by |
| 10 | + the Free Software Foundation, either version 2 of the License, or |
| 11 | + (at your option) any later version. |
| 12 | +
|
| 13 | + reicast is distributed in the hope that it will be useful, |
| 14 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + GNU General Public License for more details. |
| 17 | +
|
| 18 | + You should have received a copy of the GNU General Public License |
| 19 | + along with reicast. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | +#include "7zArchive.h" |
| 22 | +#include "deps/lzma/7z.h" |
| 23 | +#include "deps/lzma/7zCrc.h" |
| 24 | +#include "deps/lzma/Alloc.h" |
| 25 | + |
| 26 | +#define kInputBufSize ((size_t)1 << 18) |
| 27 | + |
| 28 | +static bool crc_tables_generated; |
| 29 | + |
| 30 | +bool SzArchive::Open(const char* path) |
| 31 | +{ |
| 32 | + SzArEx_Init(&szarchive); |
| 33 | + |
| 34 | + if (InFile_Open(&archiveStream.file, path)) |
| 35 | + return false; |
| 36 | + FileInStream_CreateVTable(&archiveStream); |
| 37 | + LookToRead2_CreateVTable(&lookStream, false); |
| 38 | + lookStream.buf = (Byte *)ISzAlloc_Alloc(&g_Alloc, kInputBufSize); |
| 39 | + if (lookStream.buf == NULL) |
| 40 | + return false; |
| 41 | + lookStream.bufSize = kInputBufSize; |
| 42 | + lookStream.realStream = &archiveStream.vt; |
| 43 | + LookToRead2_Init(&lookStream); |
| 44 | + |
| 45 | + if (!crc_tables_generated) |
| 46 | + { |
| 47 | + CrcGenerateTable(); |
| 48 | + crc_tables_generated = true; |
| 49 | + } |
| 50 | + SRes res = SzArEx_Open(&szarchive, &lookStream.vt, &g_Alloc, &g_Alloc); |
| 51 | + |
| 52 | + return (res == SZ_OK); |
| 53 | +} |
| 54 | + |
| 55 | +ArchiveFile* SzArchive::OpenFile(const char* name) |
| 56 | +{ |
| 57 | + u16 fname[512]; |
| 58 | + for (int i = 0; i < szarchive.NumFiles; i++) |
| 59 | + { |
| 60 | + unsigned isDir = SzArEx_IsDir(&szarchive, i); |
| 61 | + if (isDir) |
| 62 | + continue; |
| 63 | + |
| 64 | + int len = SzArEx_GetFileNameUtf16(&szarchive, i, fname); |
| 65 | + char szname[512]; |
| 66 | + int j = 0; |
| 67 | + for (; j < len && j < sizeof(szname) - 1; j++) |
| 68 | + szname[j] = fname[j]; |
| 69 | + szname[j] = 0; |
| 70 | + if (strcmp(name, szname)) |
| 71 | + continue; |
| 72 | + |
| 73 | + size_t offset = 0; |
| 74 | + size_t out_size_processed = 0; |
| 75 | + SRes res = SzArEx_Extract(&szarchive, &lookStream.vt, i, &block_idx, &out_buffer, &out_buffer_size, &offset, &out_size_processed, &g_Alloc, &g_Alloc); |
| 76 | + if (res != SZ_OK) |
| 77 | + return NULL; |
| 78 | + |
| 79 | + return new SzArchiveFile(out_buffer, offset, (u32)out_size_processed); |
| 80 | + } |
| 81 | + return NULL; |
| 82 | +} |
| 83 | + |
| 84 | +SzArchive::~SzArchive() |
| 85 | +{ |
| 86 | + if (lookStream.buf != NULL) |
| 87 | + { |
| 88 | + File_Close(&archiveStream.file); |
| 89 | + ISzAlloc_Free(&g_Alloc, lookStream.buf); |
| 90 | + if (out_buffer != NULL) |
| 91 | + ISzAlloc_Free(&g_Alloc, out_buffer); |
| 92 | + SzArEx_Free(&szarchive, &g_Alloc); |
| 93 | + } |
| 94 | +} |
0 commit comments