From 464df1d5e5ab1322e2dd0a7796939fff1aeefa9a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 17:49:25 +0200 Subject: Adding upstream version 1.47.0. Signed-off-by: Daniel Baumann --- lib/e2p/encoding.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 lib/e2p/encoding.c (limited to 'lib/e2p/encoding.c') diff --git a/lib/e2p/encoding.c b/lib/e2p/encoding.c new file mode 100644 index 0000000..24266fc --- /dev/null +++ b/lib/e2p/encoding.c @@ -0,0 +1,118 @@ +/* + * encoding.c --- convert between encoding magic numbers and strings + * + * Copyright (C) 2018 Collabora Ltd. + * + * %Begin-Header% + * This file may be redistributed under the terms of the GNU Library + * General Public License, version 2. + * %End-Header% + */ + +#include "config.h" +#include +#include +#include +#include +#include +#include + +#include "e2p.h" + +#define ARRAY_SIZE(array) \ + (sizeof(array) / sizeof(array[0])) + +static const struct { + const char *name; + __u16 encoding_magic; + __u16 default_flags; + +} ext4_encoding_map[] = { + { + .encoding_magic = EXT4_ENC_UTF8_12_1, + .name = "utf8-12.1", + .default_flags = 0, + }, + { + .encoding_magic = EXT4_ENC_UTF8_12_1, + .name = "utf8", + .default_flags = 0, + }, +}; + +static const struct enc_flags { + __u16 flag; + const char *param; +} encoding_flags[] = { + { EXT4_ENC_STRICT_MODE_FL, "strict" }, +}; + +/* Return a positive number < 0xff indicating the encoding magic number + * or a negative value indicating error. */ +int e2p_str2encoding(const char *string) +{ + unsigned int i; + + for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) + if (!strcmp(string, ext4_encoding_map[i].name)) + return ext4_encoding_map[i].encoding_magic; + + return -EINVAL; +} + +/* Return the name of an encoding or NULL */ +const char *e2p_encoding2str(int encoding) +{ + unsigned int i; + static char buf[32]; + + for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) + if (ext4_encoding_map[i].encoding_magic == encoding) + return ext4_encoding_map[i].name; + sprintf(buf, "UNKNOWN_ENCODING_%d", encoding); + return buf; +} + +int e2p_get_encoding_flags(int encoding) +{ + unsigned int i; + + for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) + if (ext4_encoding_map[i].encoding_magic == encoding) + return ext4_encoding_map[i].default_flags; + + return 0; +} + +int e2p_str2encoding_flags(int encoding, char *param, __u16 *flags) +{ + char *f = strtok(param, "-"); + const struct enc_flags *fl; + unsigned int i, neg = 0; + + if (encoding != EXT4_ENC_UTF8_12_1) + return -EINVAL; + while (f) { + neg = 0; + if (!strncmp("no", f, 2)) { + neg = 1; + f += 2; + } + + for (i = 0; i < ARRAY_SIZE(encoding_flags); i++) { + fl = &encoding_flags[i]; + if (!strcmp(fl->param, f)) { + if (neg) + *flags &= ~fl->flag; + else + *flags |= fl->flag; + + goto next_flag; + } + } + return -EINVAL; + next_flag: + f = strtok(NULL, "-"); + } + return 0; +} -- cgit v1.2.3