]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: Move zlib-related functions to separate file
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 9 Jul 2024 06:27:59 +0000 (01:27 -0500)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 26 Jul 2024 18:41:24 +0000 (13:41 -0500)
Move zlib-related function to a separate file so it's easier to isolate
the dependency on each decompression library.

Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/58
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Makefile.am
configure.ac
libkmod/libkmod-file-zlib.c [new file with mode: 0644]
libkmod/libkmod-file.c
libkmod/libkmod-internal-file.h

index fbd205de1cda69ede9bfa12def1442f0b101ab9f..443c04d22a90434d520c0051583727649e5bd22c 100644 (file)
@@ -80,6 +80,10 @@ if ENABLE_XZ
 libkmod_libkmod_la_SOURCES += libkmod/libkmod-file-xz.c
 endif
 
+if ENABLE_ZLIB
+libkmod_libkmod_la_SOURCES += libkmod/libkmod-file-zlib.c
+endif
+
 EXTRA_DIST += libkmod/libkmod.sym
 EXTRA_DIST += libkmod/README \
        libkmod/COPYING testsuite/COPYING tools/COPYING COPYING
index 5768e5e7c7d7fce5e9dc20cfa84db17cfc21e69e..85fefe3cf60892967e09b5f01025239ca523d2a0 100644 (file)
@@ -150,6 +150,7 @@ AS_IF([test "x$with_zlib" != "xno"], [
        AC_MSG_NOTICE([zlib support not requested])
 ])
 CC_FEATURE_APPEND([with_features], [with_zlib], [ZLIB])
+AM_CONDITIONAL([ENABLE_ZLIB], [test "x$with_zlib" != "xno"])
 
 AC_ARG_WITH([openssl],
        AS_HELP_STRING([--with-openssl], [handle PKCS7 signatures @<:@default=disabled@:>@]),
diff --git a/libkmod/libkmod-file-zlib.c b/libkmod/libkmod-file-zlib.c
new file mode 100644 (file)
index 0000000..80c7bfe
--- /dev/null
@@ -0,0 +1,81 @@
+// SPDX-License-Identifier: LGPL-2.1-or-later
+/*
+ * Copyright © 2024 Intel Corporation
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <zlib.h>
+
+#include <shared/util.h>
+
+#include "libkmod.h"
+#include "libkmod-internal.h"
+#include "libkmod-internal-file.h"
+
+#define READ_STEP (4 * 1024 * 1024)
+
+int kmod_file_load_zlib(struct kmod_file *file)
+{
+       int err = 0;
+       off_t did = 0, total = 0;
+       _cleanup_free_ unsigned char *p = NULL;
+       gzFile gzf;
+       int gzfd;
+
+       errno = 0;
+       gzfd = fcntl(file->fd, F_DUPFD_CLOEXEC, 3);
+       if (gzfd < 0)
+               return -errno;
+
+       gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
+       if (gzf == NULL) {
+               close(gzfd);
+               return -errno;
+       }
+
+       for (;;) {
+               int r;
+
+               if (did == total) {
+                       void *tmp = realloc(p, total + READ_STEP);
+                       if (tmp == NULL) {
+                               err = -errno;
+                               goto error;
+                       }
+                       total += READ_STEP;
+                       p = tmp;
+               }
+
+               r = gzread(gzf, p + did, total - did);
+               if (r == 0)
+                       break;
+               else if (r < 0) {
+                       int gzerr;
+                       const char *gz_errmsg = gzerror(gzf, &gzerr);
+
+                       ERR(file->ctx, "gzip: %s\n", gz_errmsg);
+
+                       /* gzip might not set errno here */
+                       err = gzerr == Z_ERRNO ? -errno : -EINVAL;
+                       goto error;
+               }
+               did += r;
+       }
+
+       file->memory = p;
+       file->size = did;
+       p = NULL;
+       gzclose(gzf);
+       return 0;
+
+error:
+       gzclose(gzf); /* closes the gzfd */
+       return err;
+}
+
index 29e89ca21167dcb0e2656bdce08a60b8560fd5ac..140e9ca666278290624e0f0b2b011121ca83b982 100644 (file)
@@ -29,9 +29,6 @@
 #ifdef ENABLE_ZSTD
 #include <zstd.h>
 #endif
-#ifdef ENABLE_ZLIB
-#include <zlib.h>
-#endif
 
 #include <shared/util.h>
 
@@ -175,74 +172,6 @@ static int load_zstd(struct kmod_file *file)
 
 static const char magic_zstd[] = {0x28, 0xB5, 0x2F, 0xFD};
 static const char magic_xz[] = {0xfd, '7', 'z', 'X', 'Z', 0};
-
-#ifdef ENABLE_ZLIB
-#define READ_STEP (4 * 1024 * 1024)
-static int load_zlib(struct kmod_file *file)
-{
-       int err = 0;
-       off_t did = 0, total = 0;
-       _cleanup_free_ unsigned char *p = NULL;
-       gzFile gzf;
-       int gzfd;
-
-       errno = 0;
-       gzfd = fcntl(file->fd, F_DUPFD_CLOEXEC, 3);
-       if (gzfd < 0)
-               return -errno;
-
-       gzf = gzdopen(gzfd, "rb"); /* takes ownership of the fd */
-       if (gzf == NULL) {
-               close(gzfd);
-               return -errno;
-       }
-
-       for (;;) {
-               int r;
-
-               if (did == total) {
-                       void *tmp = realloc(p, total + READ_STEP);
-                       if (tmp == NULL) {
-                               err = -errno;
-                               goto error;
-                       }
-                       total += READ_STEP;
-                       p = tmp;
-               }
-
-               r = gzread(gzf, p + did, total - did);
-               if (r == 0)
-                       break;
-               else if (r < 0) {
-                       int gzerr;
-                       const char *gz_errmsg = gzerror(gzf, &gzerr);
-
-                       ERR(file->ctx, "gzip: %s\n", gz_errmsg);
-
-                       /* gzip might not set errno here */
-                       err = gzerr == Z_ERRNO ? -errno : -EINVAL;
-                       goto error;
-               }
-               did += r;
-       }
-
-       file->memory = p;
-       file->size = did;
-       p = NULL;
-       gzclose(gzf);
-       return 0;
-
-error:
-       gzclose(gzf); /* closes the gzfd */
-       return err;
-}
-#else
-static int load_zlib(struct kmod_file *file)
-{
-       return -ENOSYS;
-}
-#endif
-
 static const char magic_zlib[] = {0x1f, 0x8b};
 
 static int load_reg(struct kmod_file *file)
@@ -271,7 +200,7 @@ static const struct comp_type {
 } comp_types[] = {
        {sizeof(magic_zstd),    KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
        {sizeof(magic_xz),      KMOD_FILE_COMPRESSION_XZ, magic_xz, kmod_file_load_xz},
-       {sizeof(magic_zlib),    KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
+       {sizeof(magic_zlib),    KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, kmod_file_load_zlib},
        {0,                     KMOD_FILE_COMPRESSION_NONE, NULL, load_reg}
 };
 
index 1d74aed55c17ae9fadc6e5728d4ac36a0eab527b..ca9f86803a05c81a17233402dc53ff94214ba9b1 100644 (file)
@@ -25,3 +25,10 @@ int kmod_file_load_xz(struct kmod_file *file);
 #else
 static inline int kmod_file_load_xz(struct kmod_file *file) { return -ENOSYS; }
 #endif
+
+#ifdef ENABLE_ZLIB
+int kmod_file_load_zlib(struct kmod_file *file);
+#else
+static inline int kmod_file_load_zlib(struct kmod_file *file) { return -ENOSYS; }
+#endif
+