diff mbox series

[kmod,12/13] libkmod: keep KMOD_FILE_COMPRESSION_NONE/load_reg in comp_types

Message ID 20240212-decompression-fixes-v1-12-06f92ad07985@gmail.com (mailing list archive)
State New
Headers show
Series Load compressed modules with compression-less kmod | expand

Commit Message

Emil Velikov via B4 Relay Feb. 12, 2024, 5:23 p.m. UTC
From: Emil Velikov <emil.l.velikov@gmail.com>

It's cleaner to handle all compression types and load functions in the
same style.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
 libkmod/libkmod-file.c | 19 ++++++++-----------
 1 file changed, 8 insertions(+), 11 deletions(-)

Comments

Lucas De Marchi April 29, 2024, 11:32 p.m. UTC | #1
On Mon, Feb 12, 2024 at 05:23:13PM GMT, Emil Velikov via B4 Relay wrote:
>From: Emil Velikov <emil.l.velikov@gmail.com>
>
>It's cleaner to handle all compression types and load functions in the
>same style.
>
>Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>


Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>

thanks
Lucas De Marchi

>---
> libkmod/libkmod-file.c | 19 ++++++++-----------
> 1 file changed, 8 insertions(+), 11 deletions(-)
>
>diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
>index db775a6..f162a10 100644
>--- a/libkmod/libkmod-file.c
>+++ b/libkmod/libkmod-file.c
>@@ -385,7 +385,7 @@ static const struct comp_type {
> 	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
> 	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
> 	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
>-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
>+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, load_reg}
> };
>
> struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
>@@ -409,7 +409,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> 						const char *filename)
> {
> 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
>-	const struct comp_type *itr;
> 	char buf[7];
> 	ssize_t sz;
>
>@@ -439,19 +438,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
> 		return NULL;
> 	}
>
>-	for (itr = comp_types; itr->load != NULL; itr++) {
>-		if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
>-			file->load = itr->load;
>-			file->compression = itr->compression;
>+	for (unsigned int i = 0; i < ARRAY_SIZE(comp_types); i++) {
>+		const struct comp_type *itr = &comp_types[i];
>+
>+		file->load = itr->load;
>+		file->compression = itr->compression;
>+		if (itr->magic_size &&
>+		    memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
> 			break;
> 		}
> 	}
>
>-	if (file->load == NULL) {
>-		file->load = load_reg;
>-		file->compression = KMOD_FILE_COMPRESSION_NONE;
>-	}
>-
> 	file->ctx = ctx;
>
> 	return file;
>
>-- 
>2.43.0
>
diff mbox series

Patch

diff --git a/libkmod/libkmod-file.c b/libkmod/libkmod-file.c
index db775a6..f162a10 100644
--- a/libkmod/libkmod-file.c
+++ b/libkmod/libkmod-file.c
@@ -385,7 +385,7 @@  static const struct comp_type {
 	{sizeof(magic_zstd),	KMOD_FILE_COMPRESSION_ZSTD, magic_zstd, load_zstd},
 	{sizeof(magic_xz),	KMOD_FILE_COMPRESSION_XZ, magic_xz, load_xz},
 	{sizeof(magic_zlib),	KMOD_FILE_COMPRESSION_ZLIB, magic_zlib, load_zlib},
-	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, NULL}
+	{0,			KMOD_FILE_COMPRESSION_NONE, NULL, load_reg}
 };
 
 struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
@@ -409,7 +409,6 @@  struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 						const char *filename)
 {
 	struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
-	const struct comp_type *itr;
 	char buf[7];
 	ssize_t sz;
 
@@ -439,19 +438,17 @@  struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 		return NULL;
 	}
 
-	for (itr = comp_types; itr->load != NULL; itr++) {
-		if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-			file->load = itr->load;
-			file->compression = itr->compression;
+	for (unsigned int i = 0; i < ARRAY_SIZE(comp_types); i++) {
+		const struct comp_type *itr = &comp_types[i];
+
+		file->load = itr->load;
+		file->compression = itr->compression;
+		if (itr->magic_size &&
+		    memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
 			break;
 		}
 	}
 
-	if (file->load == NULL) {
-		file->load = load_reg;
-		file->compression = KMOD_FILE_COMPRESSION_NONE;
-	}
-
 	file->ctx = ctx;
 
 	return file;