xref: /curl/lib/curl_multibyte.h (revision f81f351b)
1 #ifndef HEADER_CURL_MULTIBYTE_H
2 #define HEADER_CURL_MULTIBYTE_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 #include "curl_setup.h"
27 
28 #if defined(_WIN32)
29 
30  /*
31   * MultiByte conversions using Windows kernel32 library.
32   */
33 
34 wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8);
35 char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w);
36 #endif /* _WIN32 */
37 
38 /*
39  * Macros curlx_convert_UTF8_to_tchar(), curlx_convert_tchar_to_UTF8()
40  * and curlx_unicodefree() main purpose is to minimize the number of
41  * preprocessor conditional directives needed by code using these
42  * to differentiate Unicode from non-Unicode builds.
43  *
44  * In the case of a non-Unicode build the tchar strings are char strings that
45  * are duplicated via strdup and remain in whatever the passed in encoding is,
46  * which is assumed to be UTF-8 but may be other encoding. Therefore the
47  * significance of the conversion functions is primarily for Unicode builds.
48  *
49  * Allocated memory should be free'd with curlx_unicodefree().
50  *
51  * Note: Because these are curlx functions their memory usage is not tracked
52  * by the curl memory tracker memdebug. you will notice that curlx
53  * function-like macros call free and strdup in parentheses, eg (strdup)(ptr),
54  * and that is to ensure that the curl memdebug override macros do not replace
55  * them.
56  */
57 
58 #if defined(UNICODE) && defined(_WIN32)
59 
60 #define curlx_convert_UTF8_to_tchar(ptr) curlx_convert_UTF8_to_wchar((ptr))
61 #define curlx_convert_tchar_to_UTF8(ptr) curlx_convert_wchar_to_UTF8((ptr))
62 
63 typedef union {
64   unsigned short       *tchar_ptr;
65   const unsigned short *const_tchar_ptr;
66   unsigned short       *tbyte_ptr;
67   const unsigned short *const_tbyte_ptr;
68 } xcharp_u;
69 
70 #else
71 
72 #define curlx_convert_UTF8_to_tchar(ptr) (strdup)(ptr)
73 #define curlx_convert_tchar_to_UTF8(ptr) (strdup)(ptr)
74 
75 typedef union {
76   char                *tchar_ptr;
77   const char          *const_tchar_ptr;
78   unsigned char       *tbyte_ptr;
79   const unsigned char *const_tbyte_ptr;
80 } xcharp_u;
81 
82 #endif /* UNICODE && _WIN32 */
83 
84 #define curlx_unicodefree(ptr)                          \
85   do {                                                  \
86     if(ptr) {                                           \
87       (free)(ptr);                                      \
88       (ptr) = NULL;                                     \
89     }                                                   \
90   } while(0)
91 
92 #endif /* HEADER_CURL_MULTIBYTE_H */
93