]> wimlib.net Git - wimlib/blob - include/wimlib/encoding.h
New helper macro: ALIGN()
[wimlib] / include / wimlib / encoding.h
1 #ifndef _WIMLIB_ENCODING_H
2 #define _WIMLIB_ENCODING_H
3
4 #include <string.h>
5
6 #include "wimlib/error.h"
7 #include "wimlib/util.h"
8 #include "wimlib/types.h"
9
10 extern void
11 iconv_global_init(void);
12
13 extern void
14 iconv_global_cleanup(void);
15
16 extern void
17 init_upcase(void);
18
19 extern bool wimlib_mbs_is_utf8;
20
21 #define DECLARE_CHAR_CONVERSION_FUNCTIONS(varname1, varname2,           \
22                                           chartype1, chartype2)         \
23                                                                         \
24 extern int                                                              \
25 varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes,         \
26                          chartype2 **out_ret,                           \
27                          size_t *out_nbytes_ret);                       \
28                                                                         \
29 extern int                                                              \
30 varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\
31                                   size_t *out_nbytes_ret);              \
32                                                                         \
33 extern int                                                              \
34 varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes,   \
35                                chartype2 *out);
36
37 extern utf16lechar *
38 utf16le_dupz(const void *ustr, size_t usize);
39
40 extern utf16lechar *
41 utf16le_dup(const utf16lechar *ustr);
42
43 extern size_t
44 utf16le_len_bytes(const utf16lechar *s);
45
46 extern size_t
47 utf16le_len_chars(const utf16lechar *s);
48
49 #if !TCHAR_IS_UTF16LE
50 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf16le, tstr, utf16lechar, tchar);
51 DECLARE_CHAR_CONVERSION_FUNCTIONS(tstr, utf16le, tchar, utf16lechar);
52 #else
53
54 static inline int
55 tstr_to_utf16le(const tchar *tstr, size_t tsize,
56                 utf16lechar **ustr_ret, size_t *usize_ret)
57 {
58         utf16lechar *ustr = utf16le_dupz(tstr, tsize);
59         if (!ustr)
60                 return WIMLIB_ERR_NOMEM;
61         *ustr_ret = ustr;
62         *usize_ret = tsize;
63         return 0;
64 }
65
66 #define utf16le_to_tstr tstr_to_utf16le
67
68 #endif
69
70 DECLARE_CHAR_CONVERSION_FUNCTIONS(utf8, tstr, char, tchar);
71 DECLARE_CHAR_CONVERSION_FUNCTIONS(tstr, utf8, tchar, char);
72
73 extern int
74 utf8_to_tstr_simple(const char *utf8str, tchar **out);
75
76 extern int
77 tstr_to_utf8_simple(const tchar *tstr, char **out);
78
79 extern int
80 cmp_utf16le_strings(const utf16lechar *s1, size_t n1,
81                     const utf16lechar *s2, size_t n2,
82                     bool ignore_case);
83
84 extern int
85 cmp_utf16le_strings_z(const utf16lechar *s1, const utf16lechar *s2,
86                       bool ignore_case);
87
88 /* Convert a string in the platform-dependent encoding to UTF-16LE, but if both
89  * encodings are UTF-16LE, simply re-use the string.  Release with
90  * tstr_put_utf16le() when done.  */
91 static inline int
92 tstr_get_utf16le_and_len(const tchar *tstr,
93                          const utf16lechar **ustr_ret, size_t *usize_ret)
94 {
95         size_t tsize = tstrlen(tstr) * sizeof(tchar);
96 #if TCHAR_IS_UTF16LE
97         /* No conversion or copy needed  */
98         *ustr_ret = tstr;
99         *usize_ret = tsize;
100         return 0;
101 #else
102         return tstr_to_utf16le(tstr, tsize, (utf16lechar **)ustr_ret, usize_ret);
103 #endif
104 }
105
106 /* Convert a string in the platform-dependent encoding to UTF-16LE, but if both
107  * encodings are UTF-16LE, simply re-use the string.  Release with
108  * tstr_put_utf16le() when done.  */
109 static inline int
110 tstr_get_utf16le(const tchar *tstr, const utf16lechar **ustr_ret)
111 {
112 #if TCHAR_IS_UTF16LE
113         /* No conversion or copy needed  */
114         *ustr_ret = tstr;
115         return 0;
116 #else
117         size_t tsize = tstrlen(tstr) * sizeof(tchar);
118         size_t dummy;
119         return tstr_to_utf16le(tstr, tsize, (utf16lechar **)ustr_ret, &dummy);
120 #endif
121 }
122
123 /* Release a string acquired with tstr_get_utf16le() or
124  * tstr_get_utf16le_and_len().  */
125 static inline void
126 tstr_put_utf16le(const utf16lechar *ustr)
127 {
128 #if !TCHAR_IS_UTF16LE
129         FREE((void *)ustr);
130 #endif
131 }
132
133 /* Convert a UTF16-LE string to the platform-dependent encoding, but if both
134  * encodings are UTF-16LE, simply re-use the string.  Release with
135  * utf16le_put_tstr() when done.  */
136 static inline int
137 utf16le_get_tstr(const utf16lechar *ustr, size_t usize,
138                  const tchar **tstr_ret, size_t *tsize_ret)
139 {
140 #if TCHAR_IS_UTF16LE
141         /* No conversion or copy needed  */
142         *tstr_ret = ustr;
143         *tsize_ret = usize;
144         return 0;
145 #else
146         return utf16le_to_tstr(ustr, usize, (tchar **)tstr_ret, tsize_ret);
147 #endif
148 }
149
150 /* Release a string acquired with utf16le_get_tstr().  */
151 static inline void
152 utf16le_put_tstr(const tchar *tstr)
153 {
154 #if !TCHAR_IS_UTF16LE
155         FREE((void *)tstr);
156 #endif
157 }
158
159 #endif /* _WIMLIB_ENCODING_H */