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