]>
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 { | |
26 | char *name; | |
27 | __u16 encoding_magic; | |
28 | __u16 default_flags; | |
29 | ||
30 | } ext4_encoding_map[] = { | |
31 | { | |
32 | .encoding_magic = EXT4_ENC_ASCII, | |
33 | .name = "ascii", | |
34 | .default_flags = 0 | |
35 | }, | |
36 | { | |
a7fba47e | 37 | .encoding_magic = EXT4_ENC_UTF8_12_1, |
d85c5a61 GKB |
38 | .name = "utf8", |
39 | .default_flags = (EXT4_UTF8_NORMALIZATION_TYPE_NFKD | | |
40 | EXT4_UTF8_CASEFOLD_TYPE_NFKDCF) | |
41 | }, | |
42 | }; | |
43 | ||
44 | static const struct enc_flags { | |
45 | __u16 flag; | |
46 | char *param; | |
47 | } encoding_flags[] = { | |
48 | { EXT4_ENC_STRICT_MODE_FL, "strict" }, | |
49 | }; | |
50 | ||
51 | /* Return a positive number < 0xff indicating the encoding magic number | |
52 | * or a negative value indicating error. */ | |
53 | int e2p_str2encoding(const char *string) | |
54 | { | |
55 | int i; | |
56 | ||
57 | for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) | |
58 | if (!strcmp(string, ext4_encoding_map[i].name)) | |
59 | return ext4_encoding_map[i].encoding_magic; | |
60 | ||
61 | return -EINVAL; | |
62 | } | |
63 | ||
64 | int e2p_get_encoding_flags(int encoding) | |
65 | { | |
66 | int i; | |
67 | ||
68 | for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++) | |
69 | if (ext4_encoding_map[i].encoding_magic == encoding) | |
c554f555 | 70 | return ext4_encoding_map[i].default_flags; |
d85c5a61 GKB |
71 | |
72 | return 0; | |
73 | } | |
74 | ||
75 | int e2p_str2encoding_flags(int encoding, char *param, __u16 *flags) | |
76 | { | |
77 | char *f = strtok(param, "-"); | |
78 | const struct enc_flags *fl; | |
79 | int i, neg = 0; | |
80 | ||
81 | while (f) { | |
82 | neg = 0; | |
83 | if (!strncmp("no", f, 2)) { | |
84 | neg = 1; | |
85 | f += 2; | |
86 | } | |
87 | ||
88 | for (i = 0; i < ARRAY_SIZE(encoding_flags); i++) { | |
89 | fl = &encoding_flags[i]; | |
90 | if (!strcmp(fl->param, f)) { | |
91 | if (neg) | |
92 | *flags &= ~fl->flag; | |
93 | else | |
94 | *flags |= fl->flag; | |
95 | ||
96 | goto next_flag; | |
97 | } | |
98 | } | |
99 | return -EINVAL; | |
100 | next_flag: | |
101 | f = strtok(NULL, "-"); | |
102 | } | |
103 | return 0; | |
104 | } |