curl_multibyte.h 3.3 KB

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