• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef HEADER_CURL_MULTIBYTE_H
2 #define HEADER_CURL_MULTIBYTE_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2021, 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  ***************************************************************************/
24 #include "curl_setup.h"
25 
26 #if defined(WIN32)
27 
28  /*
29   * MultiByte conversions using Windows kernel32 library.
30   */
31 
32 wchar_t *curlx_convert_UTF8_to_wchar(const char *str_utf8);
33 char *curlx_convert_wchar_to_UTF8(const wchar_t *str_w);
34 #endif /* WIN32 */
35 
36 /*
37  * Macros curlx_convert_UTF8_to_tchar(), curlx_convert_tchar_to_UTF8()
38  * and curlx_unicodefree() main purpose is to minimize the number of
39  * preprocessor conditional directives needed by code using these
40  * to differentiate UNICODE from non-UNICODE builds.
41  *
42  * In the case of a non-UNICODE build the tchar strings are char strings that
43  * are duplicated via strdup and remain in whatever the passed in encoding is,
44  * which is assumed to be UTF-8 but may be other encoding. Therefore the
45  * significance of the conversion functions is primarily for UNICODE builds.
46  *
47  * Allocated memory should be free'd with curlx_unicodefree().
48  *
49  * Note: Because these are curlx functions their memory usage is not tracked
50  * by the curl memory tracker memdebug. You'll notice that curlx function-like
51  * macros call free and strdup in parentheses, eg (strdup)(ptr), and that's to
52  * ensure that the curl memdebug override macros do not replace them.
53  */
54 
55 #if defined(UNICODE) && defined(WIN32)
56 
57 #define curlx_convert_UTF8_to_tchar(ptr) curlx_convert_UTF8_to_wchar((ptr))
58 #define curlx_convert_tchar_to_UTF8(ptr) curlx_convert_wchar_to_UTF8((ptr))
59 
60 typedef union {
61   unsigned short       *tchar_ptr;
62   const unsigned short *const_tchar_ptr;
63   unsigned short       *tbyte_ptr;
64   const unsigned short *const_tbyte_ptr;
65 } xcharp_u;
66 
67 #else
68 
69 #define curlx_convert_UTF8_to_tchar(ptr) (strdup)(ptr)
70 #define curlx_convert_tchar_to_UTF8(ptr) (strdup)(ptr)
71 
72 typedef union {
73   char                *tchar_ptr;
74   const char          *const_tchar_ptr;
75   unsigned char       *tbyte_ptr;
76   const unsigned char *const_tbyte_ptr;
77 } xcharp_u;
78 
79 #endif /* UNICODE && WIN32 */
80 
81 #define curlx_unicodefree(ptr)                          \
82   do {                                                  \
83     if(ptr) {                                           \
84       (free)(ptr);                                      \
85       (ptr) = NULL;                                     \
86     }                                                   \
87   } while(0)
88 
89 #endif /* HEADER_CURL_MULTIBYTE_H */
90