Whamcloud - gitweb
ext2fs: drop Unicode normalization support
[tools/e2fsprogs.git] / lib / e2p / encoding.c
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         {
37                 .encoding_magic = EXT4_ENC_UTF8_12_1,
38                 .name = "utf8",
39                 .default_flags = 0,
40         },
41 };
42
43 static const struct enc_flags {
44         __u16 flag;
45         char *param;
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 {
54         int i;
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
63 int e2p_get_encoding_flags(int encoding)
64 {
65         int i;
66
67         for (i = 0 ; i < ARRAY_SIZE(ext4_encoding_map); i++)
68                 if (ext4_encoding_map[i].encoding_magic == encoding)
69                         return ext4_encoding_map[i].default_flags;
70
71         return 0;
72 }
73
74 int e2p_str2encoding_flags(int encoding, char *param, __u16 *flags)
75 {
76         char *f = strtok(param, "-");
77         const struct enc_flags *fl;
78         int i, neg = 0;
79
80         while (f) {
81                 neg = 0;
82                 if (!strncmp("no", f, 2)) {
83                         neg = 1;
84                         f += 2;
85                 }
86
87                 for (i = 0; i < ARRAY_SIZE(encoding_flags); i++) {
88                         fl = &encoding_flags[i];
89                         if (!strcmp(fl->param, f)) {
90                                 if (neg)
91                                         *flags &= ~fl->flag;
92                                 else
93                                         *flags |= fl->flag;
94
95                                 goto next_flag;
96                         }
97                 }
98                 return -EINVAL;
99         next_flag:
100                 f = strtok(NULL, "-");
101         }
102         return 0;
103 }