Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_NLS_H
0003 #define _LINUX_NLS_H
0004 
0005 #include <linux/init.h>
0006 
0007 /* Unicode has changed over the years.  Unicode code points no longer
0008  * fit into 16 bits; as of Unicode 5 valid code points range from 0
0009  * to 0x10ffff (17 planes, where each plane holds 65536 code points).
0010  *
0011  * The original decision to represent Unicode characters as 16-bit
0012  * wchar_t values is now outdated.  But plane 0 still includes the
0013  * most commonly used characters, so we will retain it.  The newer
0014  * 32-bit unicode_t type can be used when it is necessary to
0015  * represent the full Unicode character set.
0016  */
0017 
0018 /* Plane-0 Unicode character */
0019 typedef u16 wchar_t;
0020 #define MAX_WCHAR_T 0xffff
0021 
0022 /* Arbitrary Unicode character */
0023 typedef u32 unicode_t;
0024 
0025 struct nls_table {
0026     const char *charset;
0027     const char *alias;
0028     int (*uni2char) (wchar_t uni, unsigned char *out, int boundlen);
0029     int (*char2uni) (const unsigned char *rawstring, int boundlen,
0030              wchar_t *uni);
0031     const unsigned char *charset2lower;
0032     const unsigned char *charset2upper;
0033     struct module *owner;
0034     struct nls_table *next;
0035 };
0036 
0037 /* this value hold the maximum octet of charset */
0038 #define NLS_MAX_CHARSET_SIZE 6 /* for UTF-8 */
0039 
0040 /* Byte order for UTF-16 strings */
0041 enum utf16_endian {
0042     UTF16_HOST_ENDIAN,
0043     UTF16_LITTLE_ENDIAN,
0044     UTF16_BIG_ENDIAN
0045 };
0046 
0047 /* nls_base.c */
0048 extern int __register_nls(struct nls_table *, struct module *);
0049 extern int unregister_nls(struct nls_table *);
0050 extern struct nls_table *load_nls(char *);
0051 extern void unload_nls(struct nls_table *);
0052 extern struct nls_table *load_nls_default(void);
0053 #define register_nls(nls) __register_nls((nls), THIS_MODULE)
0054 
0055 extern int utf8_to_utf32(const u8 *s, int len, unicode_t *pu);
0056 extern int utf32_to_utf8(unicode_t u, u8 *s, int maxlen);
0057 extern int utf8s_to_utf16s(const u8 *s, int len,
0058         enum utf16_endian endian, wchar_t *pwcs, int maxlen);
0059 extern int utf16s_to_utf8s(const wchar_t *pwcs, int len,
0060         enum utf16_endian endian, u8 *s, int maxlen);
0061 
0062 static inline unsigned char nls_tolower(struct nls_table *t, unsigned char c)
0063 {
0064     unsigned char nc = t->charset2lower[c];
0065 
0066     return nc ? nc : c;
0067 }
0068 
0069 static inline unsigned char nls_toupper(struct nls_table *t, unsigned char c)
0070 {
0071     unsigned char nc = t->charset2upper[c];
0072 
0073     return nc ? nc : c;
0074 }
0075 
0076 static inline int nls_strnicmp(struct nls_table *t, const unsigned char *s1,
0077         const unsigned char *s2, int len)
0078 {
0079     while (len--) {
0080         if (nls_tolower(t, *s1++) != nls_tolower(t, *s2++))
0081             return 1;
0082     }
0083 
0084     return 0;
0085 }
0086 
0087 /*
0088  * nls_nullsize - return length of null character for codepage
0089  * @codepage - codepage for which to return length of NULL terminator
0090  *
0091  * Since we can't guarantee that the null terminator will be a particular
0092  * length, we have to check against the codepage. If there's a problem
0093  * determining it, assume a single-byte NULL terminator.
0094  */
0095 static inline int
0096 nls_nullsize(const struct nls_table *codepage)
0097 {
0098     int charlen;
0099     char tmp[NLS_MAX_CHARSET_SIZE];
0100 
0101     charlen = codepage->uni2char(0, tmp, NLS_MAX_CHARSET_SIZE);
0102 
0103     return charlen > 0 ? charlen : 1;
0104 }
0105 
0106 #define MODULE_ALIAS_NLS(name)  MODULE_ALIAS("nls_" __stringify(name))
0107 
0108 #endif /* _LINUX_NLS_H */
0109