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