url.c 20 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*****************************************************************************
 * url.c: URL related functions
 *****************************************************************************
 * Copyright (C) 2006 VLC authors and VideoLAN
 * Copyright (C) 2008-2012 Rémi Denis-Courmont
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

26
#include <errno.h>
27
#include <stdio.h>
28 29 30
#include <stdlib.h>
#include <string.h>
#include <assert.h>
31
#ifdef _WIN32
32 33 34 35
# include <io.h>
#endif

#include <vlc_common.h>
36
#include <vlc_memstream.h>
37 38
#include <vlc_url.h>
#include <vlc_fs.h>
39
#include <ctype.h>
40

41
char *vlc_uri_decode_duplicate (const char *str)
42 43
{
    char *buf = strdup (str);
44
    if (vlc_uri_decode (buf) == NULL)
45 46 47 48
    {
        free (buf);
        buf = NULL;
    }
49 50 51
    return buf;
}

52
char *vlc_uri_decode (char *str)
53 54 55 56 57
{
    char *in = str, *out = str;
    if (in == NULL)
        return NULL;

58
    char c;
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    while ((c = *(in++)) != '\0')
    {
        if (c == '%')
        {
            char hex[3];

            if (!(hex[0] = *(in++)) || !(hex[1] = *(in++)))
                return NULL;
            hex[2] = '\0';
            *(out++) = strtoul (hex, NULL, 0x10);
        }
        else
            *(out++) = c;
    }
    *out = '\0';
    return str;
}

77
static bool isurisafe (int c)
78 79 80 81 82 83 84 85
{
    /* These are the _unreserved_ URI characters (RFC3986 §2.3) */
    return ((unsigned char)(c - 'a') < 26)
        || ((unsigned char)(c - 'A') < 26)
        || ((unsigned char)(c - '0') < 10)
        || (strchr ("-._~", c) != NULL);
}

86 87 88 89 90 91 92 93 94 95 96 97
static bool isurisubdelim(int c)
{
    return strchr("!$&'()*+,;=", c) != NULL;
}

static bool isurihex(int c)
{   /* Same as isxdigit() but does not depend on locale and unsignedness */
    return ((unsigned char)(c - '0') < 10)
        || ((unsigned char)(c - 'A') < 6)
        || ((unsigned char)(c - 'a') < 6);
}

98 99 100 101 102 103 104 105 106
static char *encode_URI_bytes (const char *str, size_t *restrict lenp)
{
    char *buf = malloc (3 * *lenp + 1);
    if (unlikely(buf == NULL))
        return NULL;

    char *out = buf;
    for (size_t i = 0; i < *lenp; i++)
    {
107
        static const char hex[] = "0123456789ABCDEF";
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        unsigned char c = str[i];

        if (isurisafe (c))
            *(out++) = c;
        /* This is URI encoding, not HTTP forms:
         * Space is encoded as '%20', not '+'. */
        else
        {
            *(out++) = '%';
            *(out++) = hex[c >> 4];
            *(out++) = hex[c & 0xf];
        }
    }

    *lenp = out - buf;
    out = realloc (buf, *lenp + 1);
    return likely(out != NULL) ? out : buf;
}

127
char *vlc_uri_encode (const char *str)
128 129 130 131 132 133 134 135
{
    size_t len = strlen (str);
    char *ret = encode_URI_bytes (str, &len);
    if (likely(ret != NULL))
        ret[len] = '\0';
    return ret;
}

136
char *vlc_path2uri (const char *path, const char *scheme)
137 138
{
    if (path == NULL)
139 140
    {
        errno = EINVAL;
141
        return NULL;
142
    }
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    if (scheme == NULL && !strcmp (path, "-"))
        return strdup ("fd://0"); // standard input
    /* Note: VLC cannot handle URI schemes without double slash after the
     * scheme name (such as mailto: or news:). */

    char *buf;

#ifdef __OS2__
    char p[strlen (path) + 1];

    for (buf = p; *path; buf++, path++)
        *buf = (*path == '/') ? DIR_SEP_CHAR : *path;
    *buf = '\0';

    path = p;
#endif

160
#if defined (_WIN32) || defined (__OS2__)
161 162 163 164 165 166 167 168 169
    /* Drive letter */
    if (isalpha ((unsigned char)path[0]) && (path[1] == ':'))
    {
        if (asprintf (&buf, "%s:///%c:", scheme ? scheme : "file",
                      path[0]) == -1)
            buf = NULL;
        path += 2;
# warning Drive letter-relative path not implemented!
        if (path[0] != DIR_SEP_CHAR)
170 171
        {
            errno = ENOTSUP;
172
            return NULL;
173
        }
174 175 176 177 178
    }
    else
    if (!strncmp (path, "\\\\", 2))
    {   /* Windows UNC paths */
        /* \\host\share\path -> file://host/share/path */
179
        int hostlen = strcspn (path + 2, DIR_SEP);
180

181
        if (asprintf (&buf, "file://%.*s", hostlen, path + 2) == -1)
182
            buf = NULL;
183 184 185 186 187 188
        path += 2 + hostlen;

        if (path[0] == '\0')
            return buf; /* Hostname without path */
    }
    else
189
#endif
190 191 192 193 194 195 196 197 198 199
    if (path[0] != DIR_SEP_CHAR)
    {   /* Relative path: prepend the current working directory */
        char *cwd, *ret;

        if ((cwd = vlc_getcwd ()) == NULL)
            return NULL;
        if (asprintf (&buf, "%s"DIR_SEP"%s", cwd, path) == -1)
            buf = NULL;

        free (cwd);
200
        ret = (buf != NULL) ? vlc_path2uri (buf, scheme) : NULL;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
        free (buf);
        return ret;
    }
    else
    if (asprintf (&buf, "%s://", scheme ? scheme : "file") == -1)
        buf = NULL;
    if (buf == NULL)
        return NULL;

    /* Absolute file path */
    assert (path[0] == DIR_SEP_CHAR);
    do
    {
        size_t len = strcspn (++path, DIR_SEP);
        path += len;

        char *component = encode_URI_bytes (path - len, &len);
        if (unlikely(component == NULL))
        {
            free (buf);
            return NULL;
        }
        component[len] = '\0';

        char *uri;
        int val = asprintf (&uri, "%s/%s", buf, component);
        free (component);
        free (buf);
        if (unlikely(val == -1))
            return NULL;
        buf = uri;
    }
    while (*path);

    return buf;
}

238
char *vlc_uri2path (const char *url)
239 240 241 242 243 244 245 246 247 248 249 250
{
    char *ret = NULL;
    char *end;

    char *path = strstr (url, "://");
    if (path == NULL)
        return NULL; /* unsupported scheme or invalid syntax */

    end = memchr (url, '/', path - url);
    size_t schemelen = ((end != NULL) ? end : path) - url;
    path += 3; /* skip "://" */

251 252 253
    /* Remove request parameters and/or HTML anchor if present */
    end = path + strcspn (path, "?#");
    path = strndup (path, end - path);
254 255 256 257
    if (unlikely(path == NULL))
        return NULL; /* boom! */

    /* Decode path */
258
    vlc_uri_decode (path);
259 260 261

    if (schemelen == 4 && !strncasecmp (url, "file", 4))
    {
262
#if !defined (_WIN32) && !defined (__OS2__)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        /* Leading slash => local path */
        if (*path == '/')
            return path;
        /* Local path disguised as a remote one */
        if (!strncasecmp (path, "localhost/", 10))
            return memmove (path, path + 9, strlen (path + 9) + 1);
#else
        /* cannot start with a space */
        if (*path == ' ')
            goto out;
        for (char *p = strchr (path, '/'); p; p = strchr (p + 1, '/'))
            *p = '\\';

        /* Leading backslash => local path */
        if (*path == '\\')
            return memmove (path, path + 1, strlen (path + 1) + 1);
        /* Local path disguised as a remote one */
        if (!strncasecmp (path, "localhost\\", 10))
            return memmove (path, path + 10, strlen (path + 10) + 1);
        /* UNC path */
        if (*path && asprintf (&ret, "\\\\%s", path) == -1)
            ret = NULL;
#endif
        /* non-local path :-( */
    }
    else
    if (schemelen == 2 && !strncasecmp (url, "fd", 2))
    {
        int fd = strtol (path, &end, 0);

        if (*end)
            goto out;

296
#if !defined( _WIN32 ) && !defined( __OS2__ )
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
        switch (fd)
        {
            case 0:
                ret = strdup ("/dev/stdin");
                break;
            case 1:
                ret = strdup ("/dev/stdout");
                break;
            case 2:
                ret = strdup ("/dev/stderr");
                break;
            default:
                if (asprintf (&ret, "/dev/fd/%d", fd) == -1)
                    ret = NULL;
        }
#else
        /* XXX: Does this work on WinCE? */
        if (fd < 2)
            ret = strdup ("CON");
#endif
    }

out:
    free (path);
    return ret; /* unknown scheme */
}
323

324 325
static char *vlc_idna_to_ascii (const char *);

326 327
static bool vlc_uri_component_validate(const char *str, const char *extras)
{
328
    assert(str != NULL);
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349

    for (size_t i = 0; str[i] != '\0'; i++)
    {
        int c = str[i];

        if (isurisafe(c) || isurisubdelim(c))
            continue;
        if (strchr(extras, c) != NULL)
            continue;
        if (c == '%' && isurihex(str[i + 1]) && isurihex(str[i + 2]))
        {
            i += 2;
            continue;
        }
        return false;
    }
    return true;
}

static bool vlc_uri_host_validate(const char *str)
{
350
    return vlc_uri_component_validate(str, ":");
351 352 353 354 355 356 357
}

static bool vlc_uri_path_validate(const char *str)
{
    return vlc_uri_component_validate(str, "/@:");
}

358
int vlc_UrlParse(vlc_url_t *restrict url, const char *str)
359 360 361 362 363 364 365 366 367 368 369
{
    url->psz_protocol = NULL;
    url->psz_username = NULL;
    url->psz_password = NULL;
    url->psz_host = NULL;
    url->i_port = 0;
    url->psz_path = NULL;
    url->psz_option = NULL;
    url->psz_buffer = NULL;

    if (str == NULL)
370 371 372 373
    {
        errno = EINVAL;
        return -1;
    }
374 375 376

    char *buf = strdup (str);
    if (unlikely(buf == NULL))
377
        return -1;
378 379 380
    url->psz_buffer = buf;

    char *cur = buf, *next;
381
    int ret = 0;
382

383
    /* URI scheme */
384 385
    next = buf;
    while ((*next >= 'A' && *next <= 'Z') || (*next >= 'a' && *next <= 'z')
386
        || (*next >= '0' && *next <= '9') || memchr ("+-.", *next, 3) != NULL)
387
        next++;
388 389

    if (*next == ':')
390
    {
391
        *(next++) = '\0';
392 393 394 395
        url->psz_protocol = cur;
        cur = next;
    }

396 397 398 399 400 401 402 403 404 405 406 407
    /* Fragment */
    next = strchr(cur, '#');
    if (next != NULL)
    {
#if 0  /* TODO */
       *(next++) = '\0';
       url->psz_fragment = next;
#else
       *next = '\0';
#endif
    }

408
    /* Query parameters */
409 410
    next = strchr(cur, '?');
    if (next != NULL)
411
    {
412 413
        *(next++) = '\0';
        url->psz_option = next;
414 415
    }

416 417
    /* Authority */
    if (strncmp(cur, "//", 2) == 0)
418
    {
419
        cur += 2;
420

421 422 423 424 425 426 427 428 429
        /* Path */
        next = strchr(cur, '/');
        if (next != NULL)
        {
            *next = '\0'; /* temporary nul, reset to slash later */
            url->psz_path = next;
        }
        /*else
            url->psz_path = "/";*/
430

431 432
        /* User name */
        next = strrchr(cur, '@');
433 434 435
        if (next != NULL)
        {
            *(next++) = '\0';
436 437 438 439 440 441 442 443 444 445 446 447
            url->psz_username = cur;
            cur = next;

            /* Password (obsolete) */
            next = strchr(url->psz_username, ':');
            if (next != NULL)
            {
                *(next++) = '\0';
                url->psz_password = next;
                vlc_uri_decode(url->psz_password);
            }
            vlc_uri_decode(url->psz_username);
448 449
        }

450 451 452 453 454
        /* Host name */
        if (*cur == '[' && (next = strrchr(cur, ']')) != NULL)
        {   /* Try IPv6 numeral within brackets */
            *(next++) = '\0';
            url->psz_host = strdup(cur + 1);
455

456 457 458 459 460
            if (*next == ':')
                next++;
            else
                next = NULL;
        }
461
        else
462 463 464 465 466 467 468
        {
            next = strchr(cur, ':');
            if (next != NULL)
                *(next++) = '\0';

            url->psz_host = vlc_idna_to_ascii (cur);
        }
469 470 471 472

        if (url->psz_host == NULL)
            ret = -1;
        else
473 474 475 476
        if (!vlc_uri_host_validate(url->psz_host))
        {
            free(url->psz_host);
            url->psz_host = NULL;
477 478
            errno = EINVAL;
            ret = -1;
479 480 481
        }

        /* Port number */
482
        if (next != NULL)
483
            url->i_port = atoi(next);
484

485 486
        if (url->psz_path != NULL)
            *url->psz_path = '/'; /* restore leading slash */
487
    }
488
    else
489
    {
490
        url->psz_path = cur;
491
    }
492

493 494
    if (url->psz_path != NULL && !vlc_uri_path_validate(url->psz_path))
    {
495
        url->psz_path = NULL;
496 497 498 499 500
        errno = EINVAL;
        ret = -1;
    }

    return ret;
501 502 503 504 505 506 507
}

void vlc_UrlClean (vlc_url_t *restrict url)
{
    free (url->psz_host);
    free (url->psz_buffer);
}
508

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
/**
 * Merge paths
 *
 * See IETF RFC3986 section 5.2.3 for details.
 */
static char *vlc_uri_merge_paths(const char *base, const char *ref)
{
    char *str;
    int len;

    if (base == NULL)
        len = asprintf(&str, "/%s", ref);
    else
    {
        const char *end = strrchr(base, '/');

        if (end != NULL)
            end++;
        else
            end = base;

        len = asprintf(&str, "%.*s%s", (int)(end - base), base, ref);
    }

    if (unlikely(len == -1))
        str = NULL;
    return str;
}

/**
 * Remove dot segments
 *
 * See IETF RFC3986 section 5.2.4 for details.
 */
static char *vlc_uri_remove_dot_segments(char *str)
{
    char *input = str, *output = str;

    while (input[0] != '\0')
    {
        assert(output <= input);

        if (strncmp(input, "../", 3) == 0)
        {
            input += 3;
            continue;
        }
        if (strncmp(input, "./", 2) == 0)
        {
            input += 2;
            continue;
        }
        if (strncmp(input, "/./", 3) == 0)
        {
            input += 2;
            continue;
        }
        if (strcmp(input, "/.") == 0)
        {
            input[1] = '\0';
            continue;
        }
        if (strncmp(input, "/../", 4) == 0)
        {
            input += 3;
            output = memrchr(str, '/', output - str);
            if (output == NULL)
                output = str;
            continue;
        }
        if (strcmp(input, "/..") == 0)
        {
            input[1] = '\0';
            output = memrchr(str, '/', output - str);
            if (output == NULL)
                output = str;
            continue;
        }
        if (strcmp(input, ".") == 0)
        {
            input++;
            continue;
        }
        if (strcmp(input, "..") == 0)
        {
            input += 2;
            continue;
        }

        if (input[0] == '/')
            *(output++) = *(input++);

        size_t len = strcspn(input, "/");

        if (input != output)
            memmove(output, input, len);

        input += len;
        output += len;
    }

    output[0] = '\0';
    return str;
}

614 615
char *vlc_uri_compose(const vlc_url_t *uri)
{
616
    struct vlc_memstream stream;
617
    char *enc;
618

619
    vlc_memstream_open(&stream);
620 621

    if (uri->psz_protocol != NULL)
622
        vlc_memstream_printf(&stream, "%s:", uri->psz_protocol);
623 624 625

    if (uri->psz_host != NULL)
    {
626
        vlc_memstream_write(&stream, "//", 2);
627 628 629 630 631 632 633

        if (uri->psz_username != NULL)
        {
            enc = vlc_uri_encode(uri->psz_username);
            if (enc == NULL)
                goto error;

634
            vlc_memstream_puts(&stream, enc);
635 636 637 638 639 640 641 642
            free(enc);

            if (uri->psz_password != NULL)
            {
                enc = vlc_uri_encode(uri->psz_password);
                if (unlikely(enc == NULL))
                    goto error;

643
                vlc_memstream_printf(&stream, ":%s", enc);
644
                free(enc);
645
            }
646
            vlc_memstream_putc(&stream, '@');
647 648 649 650 651 652 653 654 655
        }

        const char *fmt;

        if (strchr(uri->psz_host, ':') != NULL)
            fmt = (uri->i_port != 0) ? "[%s]:%d" : "[%s]";
        else
            fmt = (uri->i_port != 0) ? "%s:%d" : "%s";
        /* No IDNA decoding here. Seems unnecessary, dangerous even. */
656
        vlc_memstream_printf(&stream, fmt, uri->psz_host, uri->i_port);
657 658 659
    }

    if (uri->psz_path != NULL)
660
        vlc_memstream_puts(&stream, uri->psz_path);
661
    if (uri->psz_option != NULL)
662
        vlc_memstream_printf(&stream, "?%s", uri->psz_option);
663 664
    /* NOTE: fragment not handled currently */

665
    if (vlc_memstream_close(&stream))
666
        return NULL;
667
    return stream.ptr;
668 669

error:
670 671
    if (vlc_memstream_close(&stream) == 0)
        free(stream.ptr);
672 673 674
    return NULL;
}

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
char *vlc_uri_resolve(const char *base, const char *ref)
{
    vlc_url_t base_uri, rel_uri;
    vlc_url_t tgt_uri;
    char *pathbuf = NULL, *ret = NULL;

    vlc_UrlParse(&rel_uri, ref);

    if (rel_uri.psz_protocol != NULL)
    {   /* Short circuit in case of absolute URI */
        vlc_UrlClean(&rel_uri);
        return strdup(ref);
    }

    vlc_UrlParse(&base_uri, base);

    /* RFC3986 section 5.2.2 */
    do
    {
        tgt_uri = rel_uri;
        tgt_uri.psz_protocol = base_uri.psz_protocol;

        if (rel_uri.psz_host != NULL)
            break;

        tgt_uri.psz_username = base_uri.psz_username;
        tgt_uri.psz_password = base_uri.psz_password;
        tgt_uri.psz_host = base_uri.psz_host;
        tgt_uri.i_port = base_uri.i_port;

        if (rel_uri.psz_path == NULL || rel_uri.psz_path[0] == '\0')
        {
            tgt_uri.psz_path = base_uri.psz_path;
            if (rel_uri.psz_option == NULL)
                tgt_uri.psz_option = base_uri.psz_option;
            break;
        }

        if (rel_uri.psz_path[0] == '/')
            break;

        pathbuf = vlc_uri_merge_paths(base_uri.psz_path, rel_uri.psz_path);
        if (unlikely(pathbuf == NULL))
            goto error;

        tgt_uri.psz_path = pathbuf;
    }
    while (0);

    if (tgt_uri.psz_path != NULL)
        vlc_uri_remove_dot_segments(tgt_uri.psz_path);

    ret = vlc_uri_compose(&tgt_uri);
error:
    free(pathbuf);
    vlc_UrlClean(&base_uri);
    vlc_UrlClean(&rel_uri);
    return ret;
}

735 736 737 738 739 740 741 742 743 744 745 746 747 748
char *vlc_uri_fixup(const char *str)
{
    /* Rule number one is do not change a (potentially) valid URI */
    if (vlc_uri_component_validate(str, ":/?#[]@"))
        return strdup(str);

    bool encode_percent = false;
    for (size_t i = 0; str[i] != '\0'; i++)
        if (str[i] == '%' && !(isurihex(str[i+1]) && isurihex(str[i+2])))
        {
            encode_percent = true;
            break;
        }

749 750 751
    struct vlc_memstream stream;

    vlc_memstream_open(&stream);
752 753 754 755 756 757 758

    for (size_t i = 0; str[i] != '\0'; i++)
    {
        unsigned char c = str[i];

        if (isurisafe(c) || isurisubdelim(c) || (strchr(":/?#[]@", c) != NULL)
         || (c == '%' && !encode_percent))
759
            vlc_memstream_putc(&stream, c);
760
        else
761
            vlc_memstream_printf(&stream, "%%%02hhX", c);
762 763
    }

764 765 766
    if (vlc_memstream_close(&stream))
        return NULL;
    return stream.ptr;
767 768
}

769 770
#if defined (HAVE_IDN)
# include <idna.h>
771
#elif defined (_WIN32)
772 773 774 775 776 777 778 779 780 781 782 783 784 785
# include <windows.h>
# include <vlc_charset.h>
#endif

/**
 * Converts a UTF-8 nul-terminated IDN to nul-terminated ASCII domain name.
 * \param idn UTF-8 Internationalized Domain Name to convert
 * \return a heap-allocated string or NULL on error.
 */
static char *vlc_idna_to_ascii (const char *idn)
{
#if defined (HAVE_IDN)
    char *adn;

786 787 788 789 790 791 792 793 794 795 796 797 798 799
    switch (idna_to_ascii_8z(idn, &adn, IDNA_ALLOW_UNASSIGNED))
    {
        case IDNA_SUCCESS:
            return adn;
        case IDNA_MALLOC_ERROR:
            errno = ENOMEM;
            return NULL;
        case IDNA_DLOPEN_ERROR:
            errno = ENOSYS;
            return NULL;
        default:
            errno = EINVAL;
            return NULL;
    }
800

801
#elif defined (_WIN32) && (_WIN32_WINNT >= 0x0601)
802 803 804 805 806 807 808 809
    char *ret = NULL;

    wchar_t *wide = ToWide (idn);
    if (wide == NULL)
        return NULL;

    int len = IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, NULL, 0);
    if (len == 0)
810 811
    {
        errno = EINVAL;
812
        goto error;
813
    }
814 815 816 817 818 819 820

    wchar_t *buf = malloc (sizeof (*buf) * len);
    if (unlikely(buf == NULL))
        goto error;
    if (!IdnToAscii (IDN_ALLOW_UNASSIGNED, wide, -1, buf, len))
    {
        free (buf);
821
        errno = EINVAL;
822 823 824 825 826 827 828 829 830 831 832 833
        goto error;
    }
    ret = FromWide (buf);
    free (buf);
error:
    free (wide);
    return ret;

#else
    /* No IDN support, filter out non-ASCII domain names */
    for (const char *p = idn; *p; p++)
        if (((unsigned char)*p) >= 0x80)
834 835
        {
            errno = ENOSYS;
836
            return NULL;
837
        }
838 839 840 841 842

    return strdup (idn);

#endif
}