+#ifdef ENABLE_ZSTD_COMPRESSION
+
+#include <zstd.h>
+
+typedef struct zstdfile {
+ ZSTD_CStream *cstream;
+ ZSTD_DStream *dstream;
+ FILE *file;
+ int encoding;
+ int eof;
+ ZSTD_inBuffer in;
+ ZSTD_outBuffer out;
+ unsigned char buf[1 << 15];
+} ZSTDFILE;
+
+static ZSTDFILE *zstdopen(const char *path, const char *mode, int fd)
+{
+ int level = 7;
+ int encoding = 0;
+ FILE *fp;
+ ZSTDFILE *zstdfile;
+
+ if (!path && fd < 0)
+ return 0;
+ for (; *mode; mode++)
+ {
+ if (*mode == 'w')
+ encoding = 1;
+ else if (*mode == 'r')
+ encoding = 0;
+ else if (*mode >= '1' && *mode <= '9')
+ level = *mode - '0';
+ }
+ if (fd != -1)
+ fp = fdopen(fd, encoding ? "w" : "r");
+ else
+ fp = fopen(path, encoding ? "w" : "r");
+ if (!fp)
+ return 0;
+ zstdfile = solv_calloc(1, sizeof(*zstdfile));
+ zstdfile->encoding = encoding;
+ if (encoding)
+ {
+ zstdfile->cstream = ZSTD_createCStream();
+ zstdfile->encoding = 1;
+ if (!zstdfile->cstream)
+ {
+ solv_free(zstdfile);
+ fclose(fp);
+ return 0;
+ }
+ if (ZSTD_isError(ZSTD_initCStream(zstdfile->cstream, level)))
+ {
+ ZSTD_freeCStream(zstdfile->cstream);
+ solv_free(zstdfile);
+ fclose(fp);
+ return 0;
+ }
+ zstdfile->out.dst = zstdfile->buf;
+ zstdfile->out.pos = 0;
+ zstdfile->out.size = sizeof(zstdfile->buf);
+ }
+ else
+ {
+ zstdfile->dstream = ZSTD_createDStream();
+ if (ZSTD_isError(ZSTD_initDStream(zstdfile->dstream)))
+ {
+ ZSTD_freeDStream(zstdfile->dstream);
+ solv_free(zstdfile);
+ fclose(fp);
+ return 0;
+ }
+ zstdfile->in.src = zstdfile->buf;
+ zstdfile->in.pos = 0;
+ zstdfile->in.size = 0;
+ }
+ zstdfile->file = fp;
+ return zstdfile;
+}
+
+static int zstdclose(void *cookie)
+{
+ ZSTDFILE *zstdfile = cookie;
+ int rc;
+
+ if (!zstdfile)
+ return -1;
+ if (zstdfile->encoding)
+ {
+ for (;;)
+ {
+ size_t ret;
+ zstdfile->out.pos = 0;
+ ret = ZSTD_endStream(zstdfile->cstream, &zstdfile->out);
+ if (ZSTD_isError(ret))
+ return -1;
+ if (zstdfile->out.pos && fwrite(zstdfile->buf, 1, zstdfile->out.pos, zstdfile->file) != zstdfile->out.pos)
+ return -1;
+ if (ret == 0)
+ break;
+ }
+ ZSTD_freeCStream(zstdfile->cstream);
+ }
+ else
+ {
+ ZSTD_freeDStream(zstdfile->dstream);
+ }
+ rc = fclose(zstdfile->file);
+ free(zstdfile);
+ return rc;
+}
+
+static ssize_t zstdread(void *cookie, char *buf, size_t len)
+{
+ ZSTDFILE *zstdfile = cookie;
+ int eof = 0;
+ size_t ret = 0;
+ if (!zstdfile || zstdfile->encoding)
+ return -1;
+ if (zstdfile->eof)
+ return 0;
+ zstdfile->out.dst = buf;
+ zstdfile->out.pos = 0;
+ zstdfile->out.size = len;
+ for (;;)
+ {
+ if (!eof && zstdfile->in.pos == zstdfile->in.size)
+ {
+ zstdfile->in.pos = 0;
+ zstdfile->in.size = fread(zstdfile->buf, 1, sizeof(zstdfile->buf), zstdfile->file);
+ if (!zstdfile->in.size)
+ eof = 1;
+ }
+ if (ret || !eof)
+ ret = ZSTD_decompressStream(zstdfile->dstream, &zstdfile->out, &zstdfile->in);
+ if (ret == 0 && eof)
+ {
+ zstdfile->eof = 1;
+ return zstdfile->out.pos;
+ }
+ if (ZSTD_isError(ret))
+ return -1;
+ if (zstdfile->out.pos == len)
+ return len;
+ }
+}
+
+static ssize_t zstdwrite(void *cookie, const char *buf, size_t len)
+{
+ ZSTDFILE *zstdfile = cookie;
+ if (!zstdfile || !zstdfile->encoding)
+ return -1;
+ if (!len)
+ return 0;
+ zstdfile->in.src = buf;
+ zstdfile->in.pos = 0;
+ zstdfile->in.size = len;
+
+ for (;;)
+ {
+ size_t ret;
+ zstdfile->out.pos = 0;
+ ret = ZSTD_compressStream(zstdfile->cstream, &zstdfile->out, &zstdfile->in);
+ if (ZSTD_isError(ret))
+ return -1;
+ if (zstdfile->out.pos && fwrite(zstdfile->buf, 1, zstdfile->out.pos, zstdfile->file) != zstdfile->out.pos)
+ return -1;
+ if (zstdfile->in.pos == len)
+ return len;
+ }
+}
+
+static inline FILE *myzstdfopen(const char *fn, const char *mode)
+{
+ ZSTDFILE *zstdfile = zstdopen(fn, mode, -1);
+ return cookieopen(zstdfile, mode, zstdread, zstdwrite, zstdclose);
+}
+
+static inline FILE *myzstdfdopen(int fd, const char *mode)
+{
+ ZSTDFILE *zstdfile = zstdopen(0, mode, fd);
+ return cookieopen(zstdfile, mode, zstdread, zstdwrite, zstdclose);
+}
+
+#endif
+
+#ifdef ENABLE_ZCHUNK_COMPRESSION
+
+#ifdef WITH_SYSTEM_ZCHUNK
+/* use the system's zchunk library that supports reading and writing of zchunk files */
+
+#include <zck.h>
+
+static ssize_t cookie_zckread(void *cookie, char *buf, size_t nbytes)
+{
+ return zck_read((zckCtx *)cookie, buf, nbytes);
+}
+
+static ssize_t cookie_zckwrite(void *cookie, const char *buf, size_t nbytes)
+{
+ return zck_write((zckCtx *)cookie, buf, nbytes);
+}
+
+static int cookie_zckclose(void *cookie)
+{
+ zckCtx *zck = (zckCtx *)cookie;
+ int fd = zck_get_fd(zck);
+ if (fd != -1)
+ close(fd);
+ zck_free(&zck);
+ return 0;
+}
+
+static void *zchunkopen(const char *path, const char *mode, int fd)
+{
+ zckCtx *f;
+
+ if (!path && fd < 0)
+ return 0;
+ if (fd == -1)
+ {
+ if (*mode != 'w')
+ fd = open(path, O_RDONLY);
+ else
+ fd = open(path, O_WRONLY | O_CREAT, 0666);
+ if (fd == -1)
+ return 0;
+ }
+ f = zck_create();
+ if (!f)
+ {
+ close(fd);
+ return 0;
+ }
+ if (*mode != 'w')
+ {
+ if(!zck_init_read(f, fd))
+ return 0;
+ }
+ else
+ {
+ if(!zck_init_write(f, fd))
+ return 0;
+ }
+ return cookieopen(f, mode, cookie_zckread, cookie_zckwrite, cookie_zckclose);
+}
+
+#else
+
+#include "solv_zchunk.h"
+/* use the libsolv's limited zchunk implementation that only supports reading of zchunk files */
+
+static void *zchunkopen(const char *path, const char *mode, int fd)
+{
+ FILE *fp;
+ void *f;
+ if (!path && fd < 0)
+ return 0;
+ if (fd != -1)
+ fp = fdopen(fd, mode);
+ else
+ fp = fopen(path, mode);
+ if (!fp)
+ return 0;
+ if (strcmp(mode, "r") != 0)
+ return 0;
+ f = solv_zchunk_open(fp, 1);
+ if (!f)
+ fclose(fp);
+ return cookieopen(f, mode, (ssize_t (*)(void *, char *, size_t))solv_zchunk_read, 0, (int (*)(void *))solv_zchunk_close);
+}
+
+#endif
+
+static inline FILE *myzchunkfopen(const char *fn, const char *mode)
+{
+ return zchunkopen(fn, mode, -1);
+}
+
+static inline FILE *myzchunkfdopen(int fd, const char *mode)
+{
+ return zchunkopen(0, mode, fd);
+}
+
+#endif /* ENABLE_ZCHUNK_COMPRESSION */
+
+#else
+/* no cookies no compression */
+#undef ENABLE_ZLIB_COMPRESSION
+#undef ENABLE_LZMA_COMPRESSION
+#undef ENABLE_BZIP2_COMPRESSION
+#undef ENABLE_ZSTD_COMPRESSION
+#undef ENABLE_ZCHUNK_COMPRESSION
+#endif
+
+