Skip to content

Commit 4baa8d5

Browse files
committed
Add .zst archive file support, and zstd compression to .zip
1 parent 935e6f5 commit 4baa8d5

File tree

10 files changed

+462
-4
lines changed

10 files changed

+462
-4
lines changed

Makefile.common

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2022,7 +2022,8 @@ ifeq ($(HAVE_ZSTD),1)
20222022
$(DEPS_DIR)/zstd/lib/decompress/zstd_decompress.o \
20232023
$(DEPS_DIR)/zstd/lib/decompress/zstd_decompress_block.o
20242024

2025-
OBJ += $(ZSOBJ)
2025+
OBJ += $(LIBRETRO_COMM_DIR)/file/archive_file_zstd.o \
2026+
$(ZSOBJ)
20262027
endif
20272028

20282029
ifeq ($(HAVE_IBXM), 1)

deps/7zip/7zDec.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include "Ppmd7.h"
2121
#endif
2222

23+
#ifdef HAVE_ZSTD
24+
#include <zstd.h>
25+
#endif
26+
2327
#define k_Copy 0
2428
#define k_Delta 3
2529
#define k_LZMA2 0x21
@@ -32,6 +36,10 @@
3236
#define k_ARMT 0x3030701
3337
#define k_SPARC 0x3030805
3438

39+
#ifdef HAVE_ZSTD
40+
#define k_ZSTD 0x4F71101
41+
#endif
42+
3543

3644
#ifdef _7ZIP_PPMD_SUPPPORT
3745

@@ -280,6 +288,9 @@ static BoolInt IS_MAIN_METHOD(uint32_t m)
280288
#endif
281289
#ifdef _7ZIP_PPMD_SUPPPORT
282290
case k_PPMD:
291+
#endif
292+
#ifdef HAVE_ZSTD
293+
case k_ZSTD:
283294
#endif
284295
return True;
285296
}
@@ -446,6 +457,20 @@ static SRes SzFolder_Decode2(const CSzFolder *folder,
446457
RINOK(SzDecodePpmd(propsData + coder->PropsOffset, coder->PropsSize, inSize, inStream, outBufCur, outSizeCur, allocMain));
447458
}
448459
#endif
460+
#ifdef HAVE_ZSTD
461+
else if (coder->MethodID == k_ZSTD)
462+
{
463+
Byte *inBuf = (Byte *)ISzAlloc_Alloc(allocMain, (size_t)inSize);
464+
size_t ret;
465+
if (!inBuf)
466+
return SZ_ERROR_MEM;
467+
RINOK(SzDecodeCopy(inSize, inStream, inBuf));
468+
ret = ZSTD_decompress(outBufCur, outSizeCur, inBuf, (size_t)inSize);
469+
ISzAlloc_Free(allocMain, inBuf);
470+
if (ZSTD_isError(ret) || ret != outSizeCur)
471+
return SZ_ERROR_DATA;
472+
}
473+
#endif
449474
else
450475
return SZ_ERROR_UNSUPPORTED;
451476
}

griffin/griffin.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ ARCHIVE FILE
134134
#include "../libretro-common/file/archive_file_7z.c"
135135
#endif
136136

137+
#ifdef HAVE_ZSTD
138+
#include "../libretro-common/file/archive_file_zstd.c"
139+
#endif
140+
137141
/*============================================================
138142
COMPRESSION
139143
============================================================ */

libretro-common/file/archive_file.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,18 @@ const struct file_archive_file_backend *file_archive_get_7z_file_backend(void)
581581
#endif
582582
}
583583

584+
const struct file_archive_file_backend *file_archive_get_zstd_file_backend(void)
585+
{
586+
#ifdef HAVE_ZSTD
587+
return &zstd_backend;
588+
#else
589+
return NULL;
590+
#endif
591+
}
592+
584593
const struct file_archive_file_backend* file_archive_get_file_backend(const char *path)
585594
{
586-
#if defined(HAVE_7ZIP) || defined(HAVE_ZLIB)
595+
#if defined(HAVE_7ZIP) || defined(HAVE_ZLIB) || defined(HAVE_ZSTD)
587596
char newpath[PATH_MAX_LENGTH];
588597
const char *file_ext = NULL;
589598
char *last = NULL;
@@ -606,6 +615,11 @@ const struct file_archive_file_backend* file_archive_get_file_backend(const char
606615
)
607616
return &zlib_backend;
608617
#endif
618+
619+
#ifdef HAVE_ZSTD
620+
if (string_is_equal_noncase(file_ext, "zst"))
621+
return &zstd_backend;
622+
#endif
609623
#endif
610624

611625
return NULL;

libretro-common/file/archive_file_zlib.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131

3232
#include <zlib.h>
3333

34+
#ifdef HAVE_ZSTD
35+
#include <zstd.h>
36+
#endif
37+
3438
#ifndef CENTRAL_FILE_HEADER_SIGNATURE
3539
#define CENTRAL_FILE_HEADER_SIGNATURE 0x02014b50
3640
#endif
@@ -44,7 +48,8 @@
4448
enum file_archive_compression_mode
4549
{
4650
ZIP_MODE_STORED = 0,
47-
ZIP_MODE_DEFLATED = 8
51+
ZIP_MODE_DEFLATED = 8,
52+
ZIP_MODE_ZSTD = 93
4853
};
4954

5055
typedef struct
@@ -163,6 +168,19 @@ static bool zlib_stream_decompress_data_to_file_init(
163168
return false;
164169
}
165170
}
171+
#ifdef HAVE_ZSTD
172+
else if (cmode == ZIP_MODE_ZSTD)
173+
{
174+
/* Allocate a buffer to read compressed data into;
175+
* decompression is done in one shot during iterate */
176+
zip_context->tmpbuf = (uint8_t*)malloc(csize);
177+
if (!zip_context->tmpbuf)
178+
{
179+
zip_context_free_stream(zip_context, false);
180+
return false;
181+
}
182+
}
183+
#endif
166184

167185
return true;
168186
}
@@ -243,6 +261,45 @@ static int zlib_stream_decompress_data_to_file_iterate(
243261

244262
return 0; /* still more data to process */
245263
}
264+
#ifdef HAVE_ZSTD
265+
else if (zip_context->cmode == ZIP_MODE_ZSTD)
266+
{
267+
size_t result;
268+
269+
#ifdef HAVE_MMAP
270+
if (state->archive_mmap_data)
271+
{
272+
result = ZSTD_decompress(
273+
zip_context->decompressed_data, zip_context->usize,
274+
state->archive_mmap_data + (size_t)zip_context->fdoffset,
275+
zip_context->csize);
276+
}
277+
else
278+
#endif
279+
{
280+
/* Read all compressed data, then decompress */
281+
filestream_seek(state->archive_file,
282+
zip_context->fdoffset,
283+
RETRO_VFS_SEEK_POSITION_START);
284+
if (filestream_read(state->archive_file,
285+
zip_context->tmpbuf, zip_context->csize) < 0)
286+
return -1;
287+
288+
result = ZSTD_decompress(
289+
zip_context->decompressed_data, zip_context->usize,
290+
zip_context->tmpbuf, zip_context->csize);
291+
}
292+
293+
if (ZSTD_isError(result))
294+
return -1;
295+
296+
free(zip_context->tmpbuf);
297+
zip_context->tmpbuf = NULL;
298+
299+
handle->data = zip_context->decompressed_data;
300+
return 1;
301+
}
302+
#endif
246303

247304
/* No idea what kind of compression this is */
248305
return -1;

0 commit comments

Comments
 (0)