Replace zlib, zstd, libchdr, libzip and libpng with libretro-common

Deflate, zlib and gzip streams go through rinflate and rdeflate, zstd
through rzstd, CHD images through rchd, zip archives through
rzip_archive and PNG through rpng. The bundled libraries, their build
glue and the system-library options are gone; adler32 and uncompress
keep zlib's ABI for the prebuilt FFmpeg archives that still call them.
libretro-common is one CMake library that the core, the headless tool
and the unit tests all link.

The reads take shorter paths on the way. CHD decodes straight from the
loader's reads and a range of blocks at a time, and a file read at any
alignment decodes into place; CSO keeps one inflate stream instead of
one per block; a plain ISO serves a file read with one read of its exact
bytes; a stored zip member is read in place and a deflated one through
restart points rather than a copy of the whole image in memory; zip
members decode into the caller's buffer; sequential reads through the
frontend's VFS skip the seek, which also keeps its buffering.

libretro/test reads one image as ISO, CSO, CHD and zip through the block
devices, boots each, and checks the codecs against Python-made vectors;
a CI job runs it.
This commit is contained in:
libretroadmin committed 2026-10-08 14:14:38 +02:00
1 parent 6b22f99818
commit d56b453891
252 files changed
+1751 -91370

No files matched your search

+51 -145
View File
@@ -1,69 +1,46 @@
#ifdef SHARED_LIBZIP
#include <zip.h>
#else
#include "ext/libzip/zip.h"
#endif
#include <zip/rzip_archive.h>
#include "Core/FileLoaders/LocalFileLoader.h"
#include "Core/FileLoaders/ZipFileLoader.h"
int64_t ZipFileLoader::BackendRead(void *userdata, uint64_t off, void *dst, size_t len) {
FileLoader *backend = (FileLoader *)userdata;
return (int64_t)backend->ReadAt((s64)off, len, dst);
}
ZipFileLoader::ZipFileLoader(FileLoader *sourceLoader)
: ProxiedFileLoader(sourceLoader), zipArchive_(nullptr) {
: ProxiedFileLoader(sourceLoader) {
if (!backend_ || !backend_->Exists() || backend_->IsDirectory()) {
return;
}
zip_error_t error{};
zip_source_t* zipSource = zip_source_function_create([](void* userdata, void* data, zip_uint64_t len, zip_source_cmd_t cmd) -> zip_int64_t {
ZipFileLoader *loader = (ZipFileLoader *)userdata;
return loader->ZipSourceCallback(data, len, cmd);
}, this, &error);
if (!zipSource) {
ERROR_LOG(Log::IO, "Failed to create ZIP source: %s", zip_error_strerror(&error));
return;
}
zipArchive_ = zip_open_from_source(zipSource, ZIP_RDONLY, &error);
if (!zipArchive_) {
ERROR_LOG(Log::IO, "Failed to open ZIP archive: %s", zip_error_strerror(&error));
zip_source_free(zipSource);
// zipArchive_ is already nullptr here.
zip_ = ZipContainer(&BackendRead, backend_, (uint64_t)backend_->FileSize());
if (!zip_) {
ERROR_LOG(Log::IO, "Failed to open ZIP archive: %s", backend_->GetPath().c_str());
}
}
ZipFileLoader::~ZipFileLoader() {
if (dataFile_) {
zip_fclose(dataFile_);
}
if (zipArchive_) {
zip_discard(zipArchive_);
}
if (data_) {
free(data_);
if (seek_) {
rzip_seek_free(seek_);
}
}
bool ZipFileLoader::Initialize(int fileIndex) {
_dbg_assert_(!data_);
_dbg_assert_(!initialized_);
if (!zipArchive_) {
if (!zip_) {
ERROR_LOG(Log::IO, "Cannot initialize: ZIP archive is null");
return false;
}
struct zip_stat zstat;
int retval = zip_stat_index(zipArchive_, fileIndex, ZIP_FL_NOCASE | ZIP_FL_UNCHANGED, &zstat);
if (retval < 0) {
return false;
}
const char *name = zip_get_name(zipArchive_, fileIndex, ZIP_FL_NOCASE | ZIP_FL_UNCHANGED);
if (!name) {
ERROR_LOG(Log::IO, "Failed to get name for file index %d", fileIndex);
const rzip_entry_t *entry = rzip_archive_entry(zip_.Raw(), (uint32_t)fileIndex);
if (!entry) {
ERROR_LOG(Log::IO, "Failed to get file index %d", fileIndex);
return false;
}
// Callers (Identify_File and friends) compare against lowercase extensions, like
// Path::GetFileExtension returns. Not using tolower to avoid the Turkish I problem.
fileExtension_ = KeepIncludingLast(name, '.');
fileExtension_ = KeepIncludingLast(entry->name, '.');
for (size_t i = 0; i < fileExtension_.size(); i++) {
char c = fileExtension_[i];
if (c >= 'A' && c <= 'Z') {
@@ -71,32 +48,27 @@ bool ZipFileLoader::Initialize(int fileIndex) {
}
}
_dbg_assert_(zstat.index == fileIndex);
dataFileSize_ = zstat.size;
dataFile_ = zip_fopen_index(zipArchive_, zstat.index, ZIP_FL_UNCHANGED);
if (!dataFile_) {
ERROR_LOG(Log::IO, "Failed to open file index %d from ZIP", fileIndex);
return false;
}
// Handle zero-size files
if (dataFileSize_ == 0) {
data_ = nullptr;
return true;
}
data_ = (u8 *)malloc(dataFileSize_);
if (!data_) {
ERROR_LOG(Log::IO, "Failed to allocate %lld bytes for ZIP file data", (long long)dataFileSize_);
zip_fclose(dataFile_);
dataFile_ = nullptr;
return false;
dataFileSize_ = (s64)entry->size;
if (entry->method == RZIP_METHOD_STORED) {
if (entry->csize != entry->size) {
ERROR_LOG(Log::IO, "Stored ZIP member %d has mismatched sizes", fileIndex);
return false;
}
stored_ = true;
dataOffset_ = entry->data_off;
} else {
seek_ = rzip_seek_new(zip_.Raw(), (uint32_t)fileIndex, 0);
if (!seek_) {
ERROR_LOG(Log::IO, "Can't read ZIP member %d (method %u)", fileIndex, entry->method);
return false;
}
}
initialized_ = true;
return true;
}
size_t ZipFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags flags) {
if (!dataFile_ || absolutePos < 0 || absolutePos >= dataFileSize_) {
if (!initialized_ || absolutePos < 0 || absolutePos >= dataFileSize_) {
return 0;
}
@@ -104,90 +76,24 @@ size_t ZipFileLoader::ReadAt(s64 absolutePos, size_t bytes, void *data, Flags fl
bytes = dataFileSize_ - absolutePos;
}
// Decompress until the requested point, filling up data_ as we go. TODO: Do on thread.
while (dataReadPos_ < absolutePos + (s64)bytes) {
int remaining = BLOCK_SIZE;
if (dataReadPos_ + remaining > dataFileSize_) {
remaining = (int)(dataFileSize_ - dataReadPos_);
}
zip_int64_t retval = zip_fread(dataFile_, data_ + dataReadPos_, remaining);
if (retval < 0) {
ERROR_LOG(Log::IO, "zip_fread failed with error code %lld", (long long)retval);
return 0;
}
if (retval != remaining) {
ERROR_LOG(Log::IO, "zip_fread: expected %d bytes, got %lld", remaining, (long long)retval);
return 0;
}
dataReadPos_ += retval;
if (stored_) {
return backend_->ReadAt((s64)dataOffset_ + absolutePos, bytes, data, flags);
}
// Perform the read.
memcpy(data, data_ + absolutePos, bytes);
// Index on until the range is covered, then decode from the nearest point.
const uint64_t end = (uint64_t)absolutePos + bytes;
while (rzip_seek_covered(seek_) < end) {
const int state = rzip_seek_build(seek_, end);
if (state < 0) {
ERROR_LOG(Log::IO, "ZIP member is corrupt (%d)", state);
return 0;
}
if (state == 1)
break;
}
if (rzip_seek_read(seek_, (uint64_t)absolutePos, (uint8_t *)data, bytes) != RZIP_OK) {
ERROR_LOG(Log::IO, "ZIP read failed at %lld", (long long)absolutePos);
return 0;
}
return bytes;
}
zip_int64_t ZipFileLoader::ZipSourceCallback(void *data, zip_uint64_t len, zip_source_cmd_t cmd) {
switch (cmd) {
case ZIP_SOURCE_OPEN:
{
zipReadPos_ = 0;
return 0;
}
case ZIP_SOURCE_READ:
{
size_t readBytes = backend_->ReadAt(zipReadPos_, len, data);
zipReadPos_ += readBytes;
return static_cast<zip_int64_t>(readBytes);
}
case ZIP_SOURCE_SEEK:
{
if (len < sizeof(zip_source_args_seek_t)) {
return -1;
}
const zip_source_args_seek_t *seekArgs = (const zip_source_args_seek_t *)data;
zip_int64_t new_offset;
switch (seekArgs->whence) {
case SEEK_SET:
new_offset = seekArgs->offset;
break;
case SEEK_CUR:
new_offset = zipReadPos_ + seekArgs->offset;
break;
case SEEK_END:
new_offset = backend_->FileSize() + seekArgs->offset;
break;
default:
return -1;
}
if (new_offset < 0 || new_offset > backend_->FileSize()) {
return -1;
}
zipReadPos_ = new_offset;
return 0;
}
case ZIP_SOURCE_TELL:
return zipReadPos_;
case ZIP_SOURCE_CLOSE:
return 0;
case ZIP_SOURCE_STAT:
{
if (len < sizeof(zip_stat_t)) {
return -1;
}
zip_stat_t* st = static_cast<zip_stat_t*>(data);
zip_stat_init(st);
st->valid = ZIP_STAT_SIZE;
st->size = static_cast<zip_uint64_t>(backend_->FileSize());
return sizeof(zip_stat_t);
}
case ZIP_SOURCE_ERROR:
return -1;
case ZIP_SOURCE_FREE:
return 0;
case ZIP_SOURCE_SUPPORTS:
return zip_source_make_command_bitmap(ZIP_SOURCE_READ, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_OPEN, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, -1);
default:
return -1;
}
}