Revert "Imported Upstream version 7.44.0"
[platform/upstream/curl.git] / lib / curl_multibyte.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #if defined(USE_WIN32_IDN) || ((defined(USE_WINDOWS_SSPI) || \
26                                 defined(CURL_LDAP_WIN)) && defined(UNICODE))
27
28  /*
29   * MultiByte conversions using Windows kernel32 library.
30   */
31
32 #include "curl_multibyte.h"
33
34 #define _MPRINTF_REPLACE /* use our functions only */
35 #include <curl/mprintf.h>
36
37 #include "curl_memory.h"
38 /* The last #include file should be: */
39 #include "memdebug.h"
40
41 wchar_t *Curl_convert_UTF8_to_wchar(const char *str_utf8)
42 {
43   wchar_t *str_w = NULL;
44
45   if(str_utf8) {
46     int str_w_len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS,
47                                         str_utf8, -1, NULL, 0);
48     if(str_w_len > 0) {
49       str_w = malloc(str_w_len * sizeof(wchar_t));
50       if(str_w) {
51         if(MultiByteToWideChar(CP_UTF8, 0, str_utf8, -1, str_w,
52                                str_w_len) == 0) {
53           Curl_safefree(str_w);
54         }
55       }
56     }
57   }
58
59   return str_w;
60 }
61
62 char *Curl_convert_wchar_to_UTF8(const wchar_t *str_w)
63 {
64   char *str_utf8 = NULL;
65
66   if(str_w) {
67     int str_utf8_len = WideCharToMultiByte(CP_UTF8, 0, str_w, -1, NULL,
68                                            0, NULL, NULL);
69     if(str_utf8_len > 0) {
70       str_utf8 = malloc(str_utf8_len * sizeof(wchar_t));
71       if(str_utf8) {
72         if(WideCharToMultiByte(CP_UTF8, 0, str_w, -1, str_utf8, str_utf8_len,
73                                NULL, FALSE) == 0) {
74           Curl_safefree(str_utf8);
75         }
76       }
77     }
78   }
79
80   return str_utf8;
81 }
82
83 #endif /* USE_WIN32_IDN || ((USE_WINDOWS_SSPI || CURL_LDAP_WIN) && UNICODE) */