Whamcloud - gitweb
79593f2a66d0429a65356ed5efc7130651282b04
[tools/e2fsprogs.git] / lib / ext2fs / nls.h
1 /*
2  * nls.h - Header for encoding support functions
3  *
4  * Copyright (C) 2017 Collabora Ltd.
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 3 of the License, or (at
10  *  your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef EXT2FS_NLS_H
22 #define EXT2FS_NLS_H
23
24 #include <unistd.h>
25 #include <string.h>
26 #include <stdio.h>
27
28 #include "ext2_fs.h"
29
30 struct nls_table;
31
32 #define ARRAY_SIZE(array)                       \
33         (sizeof(array) / sizeof(array[0]))
34
35 struct nls_ops {
36         int (*normalize)(const struct nls_table *charset,
37                          const unsigned char *str, size_t len,
38                          unsigned char *dest, size_t dlen);
39
40         int (*casefold)(const struct nls_table *charset,
41                         const unsigned char *str, size_t len,
42                         unsigned char *dest, size_t dlen);
43 };
44
45 struct nls_table {
46         int version;
47         const struct nls_ops *ops;
48 };
49
50 extern const struct nls_table nls_ascii;
51 extern const struct nls_table nls_utf8_12_1;
52
53 static const struct {
54         int encoding_magic;
55         const struct nls_table *tbl;
56 } nls_map[] = {
57         { EXT4_ENC_ASCII, &nls_ascii },
58         { EXT4_ENC_UTF8_12_1, &nls_utf8_12_1 },
59 };
60
61 static const struct nls_table *nls_load_table(int encoding)
62 {
63         int i;
64
65         for (i = 0; i < ARRAY_SIZE(nls_map); i++) {
66                 if (encoding == nls_map[i].encoding_magic)
67                         return nls_map[i].tbl;
68         }
69         return NULL;
70 }
71
72 #endif