summaryrefslogtreecommitdiffstats
path: root/lib/e2p/encoding.c
blob: 24266fcd210d3f82cc809c5d0027049a280e0562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>

#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;
}