Coverage Report

Created: 2025-12-04 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/idn.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 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 https://curl.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
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
/*
26
 * IDN conversions
27
 */
28
29
#include "curl_setup.h"
30
#include "urldata.h"
31
#include "idn.h"
32
#include "sendf.h"
33
#include "curlx/warnless.h"
34
35
#ifdef USE_LIBIDN2
36
#include <idn2.h>
37
38
#if defined(_WIN32) && defined(UNICODE)
39
#define IDN2_LOOKUP(name, host, flags)                                  \
40
  idn2_lookup_u8((const uint8_t *)name, (uint8_t **)host, flags)
41
#else
42
#define IDN2_LOOKUP(name, host, flags)                          \
43
0
  idn2_lookup_ul((const char *)name, (char **)host, flags)
44
#endif
45
#endif  /* USE_LIBIDN2 */
46
47
/* for macOS and iOS targets */
48
#ifdef USE_APPLE_IDN
49
#include <unicode/uidna.h>
50
#include <iconv.h>
51
#include <langinfo.h>
52
53
#define MAX_HOST_LENGTH 512
54
55
static CURLcode iconv_to_utf8(const char *in, size_t inlen,
56
                              char **out, size_t *outlen)
57
{
58
  iconv_t cd = iconv_open("UTF-8", nl_langinfo(CODESET));
59
  if(cd != (iconv_t)-1) {
60
    size_t iconv_outlen = *outlen;
61
    char *iconv_in = (char *)CURL_UNCONST(in);
62
    size_t iconv_inlen = inlen;
63
    size_t iconv_result = iconv(cd, &iconv_in, &iconv_inlen,
64
                                out, &iconv_outlen);
65
    *outlen -= iconv_outlen;
66
    iconv_close(cd);
67
    if(iconv_result == (size_t)-1) {
68
      /* !checksrc! disable ERRNOVAR 1 */
69
      if(errno == ENOMEM)
70
        return CURLE_OUT_OF_MEMORY;
71
      else
72
        return CURLE_URL_MALFORMAT;
73
    }
74
75
    return CURLE_OK;
76
  }
77
  else {
78
    /* !checksrc! disable ERRNOVAR 1 */
79
    if(errno == ENOMEM)
80
      return CURLE_OUT_OF_MEMORY;
81
    else
82
      return CURLE_FAILED_INIT;
83
  }
84
}
85
86
static CURLcode mac_idn_to_ascii(const char *in, char **out)
87
{
88
  size_t inlen = strlen(in);
89
  if(inlen < MAX_HOST_LENGTH) {
90
    char iconv_buffer[MAX_HOST_LENGTH] = { 0 };
91
    char *iconv_outptr = iconv_buffer;
92
    size_t iconv_outlen = sizeof(iconv_buffer);
93
    CURLcode iconv_result = iconv_to_utf8(in, inlen,
94
                                          &iconv_outptr, &iconv_outlen);
95
    if(!iconv_result) {
96
      UErrorCode err = U_ZERO_ERROR;
97
      UIDNA *idna = uidna_openUTS46(
98
        UIDNA_CHECK_BIDI | UIDNA_NONTRANSITIONAL_TO_ASCII, &err);
99
      if(!U_FAILURE(err)) {
100
        UIDNAInfo info = UIDNA_INFO_INITIALIZER;
101
        char buffer[MAX_HOST_LENGTH] = { 0 };
102
        (void)uidna_nameToASCII_UTF8(idna, iconv_buffer, (int)iconv_outlen,
103
                                     buffer, sizeof(buffer) - 1, &info, &err);
104
        uidna_close(idna);
105
        if(!U_FAILURE(err) && !info.errors) {
106
          *out = curlx_strdup(buffer);
107
          if(*out)
108
            return CURLE_OK;
109
          else
110
            return CURLE_OUT_OF_MEMORY;
111
        }
112
      }
113
    }
114
    else
115
      return iconv_result;
116
  }
117
  return CURLE_URL_MALFORMAT;
118
}
119
120
static CURLcode mac_ascii_to_idn(const char *in, char **out)
121
{
122
  size_t inlen = strlen(in);
123
  if(inlen < MAX_HOST_LENGTH) {
124
    UErrorCode err = U_ZERO_ERROR;
125
    UIDNA *idna = uidna_openUTS46(
126
      UIDNA_CHECK_BIDI | UIDNA_NONTRANSITIONAL_TO_UNICODE, &err);
127
    if(!U_FAILURE(err)) {
128
      UIDNAInfo info = UIDNA_INFO_INITIALIZER;
129
      char buffer[MAX_HOST_LENGTH] = { 0 };
130
      (void)uidna_nameToUnicodeUTF8(idna, in, -1, buffer,
131
                                    sizeof(buffer) - 1, &info, &err);
132
      uidna_close(idna);
133
      if(!U_FAILURE(err)) {
134
        *out = curlx_strdup(buffer);
135
        if(*out)
136
          return CURLE_OK;
137
        else
138
          return CURLE_OUT_OF_MEMORY;
139
      }
140
    }
141
  }
142
  return CURLE_URL_MALFORMAT;
143
}
144
#endif
145
146
#ifdef USE_WIN32_IDN
147
/* using Windows kernel32 and normaliz libraries. */
148
149
#if (!defined(_WIN32_WINNT) || _WIN32_WINNT < _WIN32_WINNT_VISTA) && \
150
  (!defined(WINVER) || WINVER < 0x600)
151
WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags,
152
                                 const WCHAR *lpUnicodeCharStr,
153
                                 int cchUnicodeChar,
154
                                 WCHAR *lpASCIICharStr,
155
                                 int cchASCIIChar);
156
WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags,
157
                                   const WCHAR *lpASCIICharStr,
158
                                   int cchASCIIChar,
159
                                   WCHAR *lpUnicodeCharStr,
160
                                   int cchUnicodeChar);
161
#endif
162
163
#define IDN_MAX_LENGTH 255
164
165
static char *idn_curlx_convert_wchar_to_UTF8(const wchar_t *str_w, int chars)
166
{
167
  char *str_utf8 = NULL;
168
  int bytes = WideCharToMultiByte(CP_UTF8, 0, str_w, chars, NULL, 0,
169
                                  NULL, NULL);
170
  if(bytes > 0) {
171
    str_utf8 = curlx_malloc(bytes);
172
    if(str_utf8) {
173
      if(WideCharToMultiByte(CP_UTF8, 0, str_w, chars, str_utf8, bytes,
174
                             NULL, NULL) == 0) {
175
        curlx_free(str_utf8);
176
        return NULL;
177
      }
178
    }
179
  }
180
  return str_utf8;
181
}
182
183
static CURLcode win32_idn_to_ascii(const char *in, char **out)
184
{
185
  wchar_t in_w[IDN_MAX_LENGTH];
186
  int in_w_len;
187
  *out = NULL;
188
  in_w_len = MultiByteToWideChar(CP_UTF8, 0, in, -1, in_w, IDN_MAX_LENGTH);
189
  if(in_w_len) {
190
    wchar_t punycode[IDN_MAX_LENGTH];
191
    int chars = IdnToAscii(0, in_w, in_w_len, punycode, IDN_MAX_LENGTH);
192
    if(chars > 0) {
193
      *out = idn_curlx_convert_wchar_to_UTF8(punycode, chars);
194
      if(!*out)
195
        return CURLE_OUT_OF_MEMORY;
196
    }
197
    else
198
      return CURLE_URL_MALFORMAT;
199
  }
200
  else
201
    return CURLE_URL_MALFORMAT;
202
203
  return CURLE_OK;
204
}
205
206
static CURLcode win32_ascii_to_idn(const char *in, char **out)
207
{
208
  wchar_t in_w[IDN_MAX_LENGTH];
209
  int in_w_len;
210
  *out = NULL;
211
  in_w_len = MultiByteToWideChar(CP_UTF8, 0, in, -1, in_w, IDN_MAX_LENGTH);
212
  if(in_w_len) {
213
    WCHAR idn[IDN_MAX_LENGTH]; /* stores a UTF-16 string */
214
    int chars = IdnToUnicode(0, in_w, in_w_len, idn, IDN_MAX_LENGTH);
215
    if(chars > 0) {  /* 'chars' is "the number of characters retrieved" */
216
      *out = idn_curlx_convert_wchar_to_UTF8(idn, chars);
217
      if(!*out)
218
        return CURLE_OUT_OF_MEMORY;
219
    }
220
    else
221
      return CURLE_URL_MALFORMAT;
222
  }
223
  else
224
    return CURLE_URL_MALFORMAT;
225
226
  return CURLE_OK;
227
}
228
229
#endif /* USE_WIN32_IDN */
230
231
/*
232
 * Helpers for IDNA conversions.
233
 */
234
bool Curl_is_ASCII_name(const char *hostname)
235
0
{
236
  /* get an UNSIGNED local version of the pointer */
237
0
  const unsigned char *ch = (const unsigned char *)hostname;
238
239
0
  if(!hostname) /* bad input, consider it ASCII! */
240
0
    return TRUE;
241
242
0
  while(*ch) {
243
0
    if(*ch++ & 0x80)
244
0
      return FALSE;
245
0
  }
246
0
  return TRUE;
247
0
}
248
249
#ifdef USE_IDN
250
/*
251
 * Curl_idn_decode() returns an allocated IDN decoded string if it was
252
 * possible. NULL on error.
253
 *
254
 * CURLE_URL_MALFORMAT - the hostname could not be converted
255
 * CURLE_OUT_OF_MEMORY - memory problem
256
 *
257
 */
258
static CURLcode idn_decode(const char *input, char **output)
259
0
{
260
0
  char *decoded = NULL;
261
0
  CURLcode result = CURLE_OK;
262
0
#ifdef USE_LIBIDN2
263
0
  if(idn2_check_version(IDN2_VERSION)) {
264
0
    int flags = IDN2_NFC_INPUT
265
0
#if IDN2_VERSION_NUMBER >= 0x00140000
266
      /* IDN2_NFC_INPUT: Normalize input string using normalization form C.
267
         IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional
268
         processing. */
269
0
      | IDN2_NONTRANSITIONAL
270
0
#endif
271
0
      ;
272
0
    int rc = IDN2_LOOKUP(input, &decoded, flags);
273
0
    if(rc != IDN2_OK)
274
      /* fallback to TR46 Transitional mode for better IDNA2003
275
         compatibility */
276
0
      rc = IDN2_LOOKUP(input, &decoded, IDN2_TRANSITIONAL);
277
0
    if(rc != IDN2_OK)
278
0
      result = CURLE_URL_MALFORMAT;
279
0
  }
280
0
  else
281
    /* a too old libidn2 version */
282
0
    result = CURLE_NOT_BUILT_IN;
283
#elif defined(USE_WIN32_IDN)
284
  result = win32_idn_to_ascii(input, &decoded);
285
#elif defined(USE_APPLE_IDN)
286
  result = mac_idn_to_ascii(input, &decoded);
287
#endif
288
0
  if(!result)
289
0
    *output = decoded;
290
0
  return result;
291
0
}
292
293
static CURLcode idn_encode(const char *puny, char **output)
294
0
{
295
0
  char *enc = NULL;
296
0
#ifdef USE_LIBIDN2
297
0
  int rc = idn2_to_unicode_8z8z(puny, &enc, 0);
298
0
  if(rc != IDNA_SUCCESS)
299
0
    return rc == IDNA_MALLOC_ERROR ? CURLE_OUT_OF_MEMORY : CURLE_URL_MALFORMAT;
300
#elif defined(USE_WIN32_IDN)
301
  CURLcode result = win32_ascii_to_idn(puny, &enc);
302
  if(result)
303
    return result;
304
#elif defined(USE_APPLE_IDN)
305
  CURLcode result = mac_ascii_to_idn(puny, &enc);
306
  if(result)
307
    return result;
308
#endif
309
0
  *output = enc;
310
0
  return CURLE_OK;
311
0
}
312
313
CURLcode Curl_idn_decode(const char *input, char **output)
314
0
{
315
0
  char *d = NULL;
316
0
  CURLcode result = idn_decode(input, &d);
317
0
#ifdef USE_LIBIDN2
318
0
  if(!result) {
319
0
    char *c = curlx_strdup(d);
320
0
    idn2_free(d);
321
0
    if(c)
322
0
      d = c;
323
0
    else
324
0
      result = CURLE_OUT_OF_MEMORY;
325
0
  }
326
0
#endif
327
0
  if(!result) {
328
0
    if(!d[0]) { /* ended up zero length, not acceptable */
329
0
      result = CURLE_URL_MALFORMAT;
330
0
      curlx_free(d);
331
0
    }
332
0
    else
333
0
      *output = d;
334
0
  }
335
0
  return result;
336
0
}
337
338
CURLcode Curl_idn_encode(const char *puny, char **output)
339
0
{
340
0
  char *d = NULL;
341
0
  CURLcode result = idn_encode(puny, &d);
342
0
#ifdef USE_LIBIDN2
343
0
  if(!result) {
344
0
    char *c = curlx_strdup(d);
345
0
    idn2_free(d);
346
0
    if(c)
347
0
      d = c;
348
0
    else
349
0
      result = CURLE_OUT_OF_MEMORY;
350
0
  }
351
0
#endif
352
0
  if(!result)
353
0
    *output = d;
354
0
  return result;
355
0
}
356
357
/*
358
 * Frees data allocated by idnconvert_hostname()
359
 */
360
void Curl_free_idnconverted_hostname(struct hostname *host)
361
0
{
362
0
  Curl_safefree(host->encalloc);
363
0
}
364
365
#endif /* USE_IDN */
366
367
/*
368
 * Perform any necessary IDN conversion of hostname
369
 */
370
CURLcode Curl_idnconvert_hostname(struct hostname *host)
371
0
{
372
  /* set the name we use to display the hostname */
373
0
  host->dispname = host->name;
374
375
0
#ifdef USE_IDN
376
  /* Check name for non-ASCII and convert hostname if we can */
377
0
  if(!Curl_is_ASCII_name(host->name)) {
378
0
    char *decoded;
379
0
    CURLcode result = Curl_idn_decode(host->name, &decoded);
380
0
    if(result)
381
0
      return result;
382
    /* successful */
383
0
    host->name = host->encalloc = decoded;
384
0
  }
385
0
#endif
386
0
  return CURLE_OK;
387
0
}