From: Sascha Hauer <s.hauer@pengutronix.de>
To: "open list:BAREBOX" <barebox@lists.infradead.org>
Subject: [PATCH 1/7] filetype: add file_detect_compression_type()
Date: Tue, 18 Mar 2025 15:41:42 +0100 [thread overview]
Message-ID: <20250318-filetype-size-reduction-v1-1-4f463ffae7d0@pengutronix.de> (raw)
In-Reply-To: <20250318-filetype-size-reduction-v1-0-4f463ffae7d0@pengutronix.de>
Add file_detect_compression_type() and use that in the uncompression
code which is only interested if the file is one of the supported
compression formats.
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
common/filetype.c | 48 ++++++++++++++++++++++++++++++++----------------
include/filetype.h | 1 +
lib/uncompress.c | 4 ++--
3 files changed, 35 insertions(+), 18 deletions(-)
diff --git a/common/filetype.c b/common/filetype.c
index 73ea17e19b..9916b63b80 100644
--- a/common/filetype.c
+++ b/common/filetype.c
@@ -261,6 +261,34 @@ static bool is_dos_exe(const u8 *buf8)
#define CH_TOC_section_name 0x14
+enum filetype file_detect_compression_type(const void *_buf, size_t bufsize)
+{
+ const u32 *buf = _buf;
+ const u8 *buf8 = _buf;
+
+ if (bufsize < 16)
+ return filetype_unknown;
+
+ if (buf8[0] == 0x89 && buf8[1] == 0x4c && buf8[2] == 0x5a &&
+ buf8[3] == 0x4f)
+ return filetype_lzo_compressed;
+ if (buf8[0] == 0x02 && buf8[1] == 0x21 && buf8[2] == 0x4c &&
+ buf8[3] == 0x18)
+ return filetype_lz4_compressed;
+ if (buf8[0] == 0x1f && buf8[1] == 0x8b && buf8[2] == 0x08)
+ return filetype_gzip;
+ if (buf8[0] == 'B' && buf8[1] == 'Z' && buf8[2] == 'h' &&
+ buf8[3] > '0' && buf8[3] <= '9')
+ return filetype_bzip2;
+ if (buf8[0] == 0xfd && buf8[1] == 0x37 && buf8[2] == 0x7a &&
+ buf8[3] == 0x58 && buf8[4] == 0x5a && buf8[5] == 0x00)
+ return filetype_xz_compressed;
+ if (le32_to_cpu(buf[0]) == le32_to_cpu(ZSTD_MAGICNUMBER))
+ return filetype_zstd_compressed;
+
+ return filetype_unknown;
+}
+
enum filetype file_detect_type(const void *_buf, size_t bufsize)
{
const u32 *buf = _buf;
@@ -278,17 +306,15 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
if (buf[0] == ENVFS_32(ENVFS_MAGIC))
return filetype_barebox_env;
+ type = file_detect_compression_type(_buf, bufsize);
+ if (type != filetype_unknown)
+ return type;
+
if (bufsize < 32)
return filetype_unknown;
if (strncmp(buf8, "BM", 2) == 0)
return filetype_bmp;
- if (buf8[0] == 0x89 && buf8[1] == 0x4c && buf8[2] == 0x5a &&
- buf8[3] == 0x4f)
- return filetype_lzo_compressed;
- if (buf8[0] == 0x02 && buf8[1] == 0x21 && buf8[2] == 0x4c &&
- buf8[3] == 0x18)
- return filetype_lz4_compressed;
if (buf[0] == be32_to_cpu(0x27051956))
return filetype_uimage;
if (buf[0] == 0x23494255)
@@ -297,14 +323,6 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
return filetype_ubifs;
if (buf[0] == 0x20031985)
return filetype_jffs2;
- if (buf8[0] == 0x1f && buf8[1] == 0x8b && buf8[2] == 0x08)
- return filetype_gzip;
- if (buf8[0] == 'B' && buf8[1] == 'Z' && buf8[2] == 'h' &&
- buf8[3] > '0' && buf8[3] <= '9')
- return filetype_bzip2;
- if (buf8[0] == 0xfd && buf8[1] == 0x37 && buf8[2] == 0x7a &&
- buf8[3] == 0x58 && buf8[4] == 0x5a && buf8[5] == 0x00)
- return filetype_xz_compressed;
if (buf8[0] == 'h' && buf8[1] == 's' && buf8[2] == 'q' &&
buf8[3] == 's')
return filetype_squashfs;
@@ -325,8 +343,6 @@ enum filetype file_detect_type(const void *_buf, size_t bufsize)
return filetype_rockchip_rkns_image;
if (le32_to_cpu(buf[0]) == le32_to_cpu(0xaa640001))
return filetype_fip;
- if (le32_to_cpu(buf[0]) == le32_to_cpu(ZSTD_MAGICNUMBER))
- return filetype_zstd_compressed;
if ((buf8[0] == 0x5a || buf8[0] == 0x69 || buf8[0] == 0x78 ||
buf8[0] == 0x8b || buf8[0] == 0x9c) &&
diff --git a/include/filetype.h b/include/filetype.h
index c24d061e8f..b8c0105bf2 100644
--- a/include/filetype.h
+++ b/include/filetype.h
@@ -74,6 +74,7 @@ struct cdev;
const char *file_type_to_string(enum filetype f);
const char *file_type_to_short_string(enum filetype f);
enum filetype file_detect_partition_table(const void *_buf, size_t bufsize);
+enum filetype file_detect_compression_type(const void *_buf, size_t bufsize);
enum filetype file_detect_type(const void *_buf, size_t bufsize);
int file_name_detect_type(const char *filename, enum filetype *type);
int file_name_detect_type_offset(const char *filename, loff_t pos, enum filetype *type);
diff --git a/lib/uncompress.c b/lib/uncompress.c
index 0aaeb066b6..c284bcc6ba 100644
--- a/lib/uncompress.c
+++ b/lib/uncompress.c
@@ -79,7 +79,7 @@ int uncompress(unsigned char *inbuf, long len,
char *err;
if (inbuf) {
- ft = file_detect_type(inbuf, len);
+ ft = file_detect_compression_type(inbuf, len);
uncompress_buf = NULL;
uncompress_size = 0;
} else {
@@ -94,7 +94,7 @@ int uncompress(unsigned char *inbuf, long len,
if (ret < 0)
goto err;
- ft = file_detect_type(uncompress_buf, 32);
+ ft = file_detect_compression_type(uncompress_buf, 32);
}
pr_debug("Filetype detected: %s\n", file_type_to_string(ft));
--
2.39.5
next prev parent reply other threads:[~2025-03-18 14:47 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-18 14:41 [PATCH 0/7] filetype: Some size reduction patches Sascha Hauer
2025-03-18 14:41 ` Sascha Hauer [this message]
2025-03-18 14:41 ` [PATCH 2/7] filetype: add file_detect_fs_type() Sascha Hauer
2025-03-18 14:51 ` Ahmad Fatoum
2025-03-18 15:06 ` Sascha Hauer
2025-03-18 14:41 ` [PATCH 3/7] filetype: add function pointer to file_name_detect_type_offset() Sascha Hauer
2025-03-18 14:41 ` [PATCH 4/7] filetype: let cdev_detect_type() only detect filesystems Sascha Hauer
2025-03-18 14:41 ` [PATCH 5/7] filetype: make file type strings optional Sascha Hauer
2025-03-18 14:41 ` [PATCH 6/7] ARM: am33xx: myirtech-myd: add MLO specific device tree Sascha Hauer
2025-03-18 14:41 ` [PATCH 7/7] ARM: am335x_mlo_defconfig: disable file type strings Sascha Hauer
2025-03-18 15:01 ` [PATCH 0/7] filetype: Some size reduction patches Alexander Shiyan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250318-filetype-size-reduction-v1-1-4f463ffae7d0@pengutronix.de \
--to=s.hauer@pengutronix.de \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox