Coverage Report

Created: 2026-04-29 07:01

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