[PATCH v1 1/4] util: add compressed-string parser and img setter macros

2 views
Skip to first unread message

He Yong

unread,
Apr 17, 2026, 12:44:34 PM (5 days ago) Apr 17
to swup...@googlegroups.com
Add a shared helper to convert compressed strings to internal type values,
and introduce reusable image setter macros used by parser and Lua mapping code.

Signed-off-by: He Yong <hyy...@163.com>
---
core/util.c | 22 ++++++++++++++++++++++
include/util.h | 18 ++++++++++++++++++
2 files changed, 40 insertions(+)

diff --git a/core/util.c b/core/util.c
index 3f62e070..89046f3c 100644
--- a/core/util.c
+++ b/core/util.c
@@ -76,6 +76,28 @@ int ascii_to_bin(unsigned char *dest, size_t dstlen, const char *src)
return 0;
}

+static const struct {
+ const char *name;
+ int type;
+} compressed_by_name[] = {
+ { "zlib", COMPRESSED_ZLIB },
+ { "xz", COMPRESSED_XZ },
+ { "zstd", COMPRESSED_ZSTD },
+};
+
+int compressed_string_to_type(const char *s, int *out_type)
+{
+ size_t i;
+
+ for (i = 0; i < ARRAY_SIZE(compressed_by_name); i++) {
+ if (!strcmp(s, compressed_by_name[i].name)) {
+ *out_type = compressed_by_name[i].type;
+ return 0;
+ }
+ }
+ return -1;
+}
+
static int countargc(char *args, char **argv)
{
int count = 0;
diff --git a/include/util.h b/include/util.h
index d4874f64..dcd0604c 100644
--- a/include/util.h
+++ b/include/util.h
@@ -184,6 +184,23 @@ void notifier_set_color(int level, char *col);
p = strdup(v); \
} while (0)

+#define DEFINE_IMG_STRLCPY_SETTER(_name, _field) \
+static void _name(struct img_type *img, const char *value) \
+{ \
+ strlcpy(img->_field, value, sizeof(img->_field)); \
+}
+
+#define DEFINE_IMG_BOOL_SETTER(_name, _field) \
+static void _name(struct img_type *img, bool val) \
+{ \
+ img->_field = (bool)val; \
+}
+
+#define DEFINE_IMG_NUMBER_SETTER(_name, _field, _type) \
+static void _name(struct img_type *img, double val) \
+{ \
+ img->_field = (_type)val; \
+}

#define IS_STR_EQUAL(s,s1) (s && s1 && !strcmp(s,s1))
#define UNUSED __attribute__((__unused__))
@@ -197,6 +214,7 @@ int ascii_to_bin(unsigned char *dest, size_t dstlen, const char *src);
void hash_to_ascii(const unsigned char *hash, char *s);
int IsValidHash(const unsigned char *hash);
bool is_hex_str(const char *ascii);
+int compressed_string_to_type(const char *s, int *out_type);

#ifndef typeof
#define typeof __typeof__
--
2.43.0

Reply all
Reply to author
Forward
0 new messages