]>
Commit | Line | Data |
---|---|---|
d85c5a61 GKB |
1 | /* |
2 | * encoding.c --- convert between encoding magic numbers and strings | |
3 | * | |
4 | * Copyright (C) 2018 Collabora Ltd. | |
5 | * | |
6 | * %Begin-Header% | |
7 | * This file may be redistributed under the terms of the GNU Library | |
8 | * General Public License, version 2. | |
9 | * %End-Header% | |
10 | */ | |
11 | ||
12 | #include "config.h" | |
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <string.h> | |
16 | #include <ctype.h> | |
17 | #include <errno.h> | |
18 | #include <stdio.h> | |
19 | ||
20 | #include "e2p.h" | |
21 | ||
22 | #define ARRAY_SIZE(array) \ | |
23 | (sizeof(array) / sizeof(array[0])) | |
24 | ||
25 | static const struct { | |
5a7761e8 | 26 | const char *name; |
d85c5a61 GKB |
27 | __u16 encoding_magic; |
28 | __u16 default_flags; | |
29 | ||
30 | } ext4_encoding_map[] = { | |
5a7761e8 TT |
31 | { |
32 | .encoding_magic = EXT4_ENC_UTF8_12_1, | |
33 | .name = "utf8-12.1", | |
34 | .default_flags = 0, | |
35 | }, | |
d85c5a61 | 36 | { |
a7fba47e | 37 | .encoding_magic = EXT4_ENC_UTF8_12_1, |
d85c5a61 | 38 | .name = "utf8", |
1b6d59d6 | 39 | .default_flags = 0, |
d85c5a61 GKB |
40 | }, |
41 | }; | |
42 | ||
43 | static const struct enc_flags { | |
44 | __u16 flag; | |
5a7761e8 | 45 | const char *param; |
d85c5a61 GKB |
46 | } encoding_flags[] = { |
47 | { EXT4_ENC_STRICT_MODE_FL, "strict" }, | |
48 | }; | |
49 | ||
50 | /* Return a positive number < 0xff indicating the encoding magic number | |
51 | * or a negative value indicating error. */ | |
52 | int e2p_str2encoding(const char *string) | |
53 | { | |
5a7761e8 | 54 | unsigned int i; |
d85c5a61 GKB |
55 | |
56 | for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) | |
57 | if (!strcmp(string, ext4_encoding_map[i].name)) | |
58 | return ext4_encoding_map[i].encoding_magic; | |
59 | ||
60 | return -EINVAL; | |
61 | } | |
62 | ||
5a7761e8 TT |
63 | /* Return the name of an encoding or NULL */ |
64 | const char *e2p_encoding2str(int encoding) | |
65 | { | |
66 | unsigned int i; | |
67 | static char buf[32]; | |
68 | ||
69 | for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) | |
70 | if (ext4_encoding_map[i].encoding_magic == encoding) | |
71 | return ext4_encoding_map[i].name; | |
72 | sprintf(buf, "UNKNOWN_ENCODING_%d", encoding); | |
73 | return buf; | |
74 | } | |
75 | ||
d85c5a61 GKB |
76 | int e2p_get_encoding_flags(int encoding) |
77 | { | |
5a7761e8 | 78 | unsigned int i; |
d85c5a61 GKB |
79 | |
80 | for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) | |
81 | if (ext4_encoding_map[i].encoding_magic == encoding) | |
c554f555 | 82 | return ext4_encoding_map[i].default_flags; |
d85c5a61 GKB |
83 | |
84 | return 0; | |
85 | } | |
86 | ||
87 | int e2p_str2encoding_flags(int encoding, char *param, __u16 *flags) | |
88 | { | |
89 | char *f = strtok(param, "-"); | |
90 | const struct enc_flags *fl; | |
5a7761e8 | 91 | unsigned int i, neg = 0; |
d85c5a61 GKB |
92 | |
93 | while (f) { | |
94 | neg = 0; | |
95 | if (!strncmp("no", f, 2)) { | |
96 | neg = 1; | |
97 | f += 2; | |
98 | } | |
99 | ||
100 | for (i = 0; i < ARRAY_SIZE(encoding_flags); i++) { | |
101 | fl = &encoding_flags[i]; | |
102 | if (!strcmp(fl->param, f)) { | |
103 | if (neg) | |
104 | *flags &= ~fl->flag; | |
105 | else | |
106 | *flags |= fl->flag; | |
107 | ||
108 | goto next_flag; | |
109 | } | |
110 | } | |
111 | return -EINVAL; | |
112 | next_flag: | |
113 | f = strtok(NULL, "-"); | |
114 | } | |
115 | return 0; | |
116 | } |