1 /* idna.c Convert to or from IDN strings.
2 * Copyright (C) 2002, 2003, 2004, 2011 Simon Josefsson
4 * This file is part of GNU Libidn.
6 * GNU Libidn is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * GNU Libidn is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with GNU Libidn; if not, see <http://www.gnu.org/licenses/>.
26 #include <stringprep.h>
32 #define DOTP(c) ((c) == 0x002E || (c) == 0x3002 || \
33 (c) == 0xFF0E || (c) == 0xFF61)
39 * @in: input array with unicode code points.
40 * @inlen: length of input array with unicode code points.
41 * @out: output zero terminated string that must have room for at
42 * least 63 characters plus the terminating zero.
43 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
45 * The ToASCII operation takes a sequence of Unicode code points that make
46 * up one label and transforms it into a sequence of code points in the
47 * ASCII range (0..7F). If ToASCII succeeds, the original sequence and the
48 * resulting sequence are equivalent labels.
50 * It is important to note that the ToASCII operation can fail. ToASCII
51 * fails if any step of it fails. If any step of the ToASCII operation
52 * fails on any label in a domain name, that domain name MUST NOT be used
53 * as an internationalized domain name. The method for deadling with this
54 * failure is application-specific.
56 * The inputs to ToASCII are a sequence of code points, the AllowUnassigned
57 * flag, and the UseSTD3ASCIIRules flag. The output of ToASCII is either a
58 * sequence of ASCII code points or a failure condition.
60 * ToASCII never alters a sequence of code points that are all in the ASCII
61 * range to begin with (although it could fail). Applying the ToASCII
62 * operation multiple times has exactly the same effect as applying it just
65 * Return value: Returns 0 on success, or an error code.
68 idna_to_ascii_4i (const uint32_t * in
, size_t inlen
, char *out
, int flags
)
71 uint32_t *src
; /* XXX don't need to copy data? */
75 * ToASCII consists of the following steps:
77 * 1. If all code points in the sequence are in the ASCII range (0..7F)
78 * then skip to step 3.
86 for (i
= 0; i
< inlen
; i
++)
91 src
= malloc (sizeof (in
[0]) * (inlen
+ 1));
93 return IDNA_MALLOC_ERROR
;
95 memcpy (src
, in
, sizeof (in
[0]) * inlen
);
103 * 2. Perform the steps specified in [NAMEPREP] and fail if there is
104 * an error. The AllowUnassigned flag is used in [NAMEPREP].
110 p
= stringprep_ucs4_to_utf8 (in
, inlen
, NULL
, NULL
);
112 return IDNA_MALLOC_ERROR
;
119 len
= 2 * len
+ 10; /* XXX better guess? */
120 newp
= realloc (p
, len
);
124 return IDNA_MALLOC_ERROR
;
128 if (flags
& IDNA_ALLOW_UNASSIGNED
)
129 rc
= stringprep_nameprep (p
, len
);
131 rc
= stringprep_nameprep_no_unassigned (p
, len
);
133 while (rc
== STRINGPREP_TOO_SMALL_BUFFER
);
135 if (rc
!= STRINGPREP_OK
)
138 return IDNA_STRINGPREP_ERROR
;
141 src
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
148 * 3. If the UseSTD3ASCIIRules flag is set, then perform these checks:
150 * (a) Verify the absence of non-LDH ASCII code points; that is,
151 * the absence of 0..2C, 2E..2F, 3A..40, 5B..60, and 7B..7F.
153 * (b) Verify the absence of leading and trailing hyphen-minus;
154 * that is, the absence of U+002D at the beginning and end of
158 if (flags
& IDNA_USE_STD3_ASCII_RULES
)
162 for (i
= 0; src
[i
]; i
++)
163 if (src
[i
] <= 0x2C || src
[i
] == 0x2E || src
[i
] == 0x2F ||
164 (src
[i
] >= 0x3A && src
[i
] <= 0x40) ||
165 (src
[i
] >= 0x5B && src
[i
] <= 0x60) ||
166 (src
[i
] >= 0x7B && src
[i
] <= 0x7F))
169 return IDNA_CONTAINS_NON_LDH
;
172 if (src
[0] == 0x002D || (i
> 0 && src
[i
- 1] == 0x002D))
175 return IDNA_CONTAINS_MINUS
;
180 * 4. If all code points in the sequence are in the ASCII range
181 * (0..7F), then skip to step 8.
189 for (i
= 0; src
[i
]; i
++)
193 /* copy string to output buffer if we are about to skip to step8 */
204 * 5. Verify that the sequence does NOT begin with the ACE prefix.
213 for (i
= 0; match
&& i
< strlen (IDNA_ACE_PREFIX
); i
++)
214 if (((uint32_t) IDNA_ACE_PREFIX
[i
] & 0xFF) != src
[i
])
219 return IDNA_CONTAINS_ACE_PREFIX
;
224 * 6. Encode the sequence using the encoding algorithm in [PUNYCODE]
225 * and fail if there is an error.
227 for (len
= 0; src
[len
]; len
++)
230 outlen
= 63 - strlen (IDNA_ACE_PREFIX
);
231 rc
= punycode_encode (len
, src
, NULL
,
232 &outlen
, &out
[strlen (IDNA_ACE_PREFIX
)]);
233 if (rc
!= PUNYCODE_SUCCESS
)
236 return IDNA_PUNYCODE_ERROR
;
238 out
[strlen (IDNA_ACE_PREFIX
) + outlen
] = '\0';
241 * 7. Prepend the ACE prefix.
244 memcpy (out
, IDNA_ACE_PREFIX
, strlen (IDNA_ACE_PREFIX
));
247 * 8. Verify that the number of code points is in the range 1 to 63
248 * inclusive (0 is excluded).
253 if (strlen (out
) < 1 || strlen (out
) > 63)
254 return IDNA_INVALID_LENGTH
;
259 /* ToUnicode(). May realloc() utf8in. */
261 idna_to_unicode_internal (char *utf8in
,
262 uint32_t * out
, size_t * outlen
, int flags
)
266 size_t utf8len
= strlen (utf8in
) + 1;
270 * ToUnicode consists of the following steps:
272 * 1. If the sequence contains any code points outside the ASCII range
273 * (0..7F) then proceed to step 2, otherwise skip to step 3.
281 for (i
= 0; utf8in
[i
]; i
++)
282 if (utf8in
[i
] & ~0x7F)
289 * 2. Perform the steps specified in [NAMEPREP] and fail if there is an
290 * error. (If step 3 of ToASCII is also performed here, it will not
291 * affect the overall behavior of ToUnicode, but it is not
292 * necessary.) The AllowUnassigned flag is used in [NAMEPREP].
296 char *newp
= realloc (utf8in
, utf8len
+ addlen
);
300 return IDNA_MALLOC_ERROR
;
303 if (flags
& IDNA_ALLOW_UNASSIGNED
)
304 rc
= stringprep_nameprep (utf8in
, utf8len
+ addlen
);
306 rc
= stringprep_nameprep_no_unassigned (utf8in
, utf8len
+ addlen
);
309 while (rc
== STRINGPREP_TOO_SMALL_BUFFER
);
311 if (rc
!= STRINGPREP_OK
)
314 return IDNA_STRINGPREP_ERROR
;
317 /* 3. Verify that the sequence begins with the ACE prefix, and save a
318 * copy of the sequence.
322 if (memcmp (IDNA_ACE_PREFIX
, utf8in
, strlen (IDNA_ACE_PREFIX
)) != 0)
325 return IDNA_NO_ACE_PREFIX
;
328 /* 4. Remove the ACE prefix.
331 memmove (utf8in
, &utf8in
[strlen (IDNA_ACE_PREFIX
)],
332 strlen (utf8in
) - strlen (IDNA_ACE_PREFIX
) + 1);
334 /* 5. Decode the sequence using the decoding algorithm in [PUNYCODE]
335 * and fail if there is an error. Save a copy of the result of
339 (*outlen
)--; /* reserve one for the zero */
341 rc
= punycode_decode (strlen (utf8in
), utf8in
, outlen
, out
, NULL
);
342 if (rc
!= PUNYCODE_SUCCESS
)
345 return IDNA_PUNYCODE_ERROR
;
348 out
[*outlen
] = 0; /* add zero */
353 rc
= idna_to_ascii_4i (out
, *outlen
, tmpout
, flags
);
354 if (rc
!= IDNA_SUCCESS
)
360 /* 7. Verify that the result of step 6 matches the saved copy from
361 * step 3, using a case-insensitive ASCII comparison.
364 if (strcasecmp (utf8in
, tmpout
+ strlen (IDNA_ACE_PREFIX
)) != 0)
367 return IDNA_ROUNDTRIP_VERIFY_ERROR
;
370 /* 8. Return the saved copy from step 5.
378 * idna_to_unicode_44i
379 * @in: input array with unicode code points.
380 * @inlen: length of input array with unicode code points.
381 * @out: output array with unicode code points.
382 * @outlen: on input, maximum size of output array with unicode code points,
383 * on exit, actual size of output array with unicode code points.
384 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
386 * The ToUnicode operation takes a sequence of Unicode code points
387 * that make up one label and returns a sequence of Unicode code
388 * points. If the input sequence is a label in ACE form, then the
389 * result is an equivalent internationalized label that is not in ACE
390 * form, otherwise the original sequence is returned unaltered.
392 * ToUnicode never fails. If any step fails, then the original input
393 * sequence is returned immediately in that step.
395 * The Punycode decoder can never output more code points than it
396 * inputs, but Nameprep can, and therefore ToUnicode can. Note that
397 * the number of octets needed to represent a sequence of code points
398 * depends on the particular character encoding used.
400 * The inputs to ToUnicode are a sequence of code points, the
401 * AllowUnassigned flag, and the UseSTD3ASCIIRules flag. The output of
402 * ToUnicode is always a sequence of Unicode code points.
404 * Return value: Returns error condition, but it must only be used for
405 * debugging purposes. The output buffer is always
406 * guaranteed to contain the correct data according to
407 * the specification (sans malloc induced errors). NB!
408 * This means that you normally ignore the return code
409 * from this function, as checking it means breaking the
413 idna_to_unicode_44i (const uint32_t * in
, size_t inlen
,
414 uint32_t * out
, size_t * outlen
, int flags
)
417 size_t outlensave
= *outlen
;
420 p
= stringprep_ucs4_to_utf8 (in
, inlen
, NULL
, NULL
);
422 return IDNA_MALLOC_ERROR
;
424 rc
= idna_to_unicode_internal (p
, out
, outlen
, flags
);
425 if (rc
!= IDNA_SUCCESS
)
427 memcpy (out
, in
, sizeof (in
[0]) * (inlen
< outlensave
?
428 inlen
: outlensave
));
432 /* p is freed in idna_to_unicode_internal. */
437 /* Wrappers that handle several labels */
441 * @input: zero terminated input Unicode string.
442 * @output: pointer to newly allocated output string.
443 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
445 * Convert UCS-4 domain name to ASCII string. The domain name may
446 * contain several labels, separated by dots. The output buffer must
447 * be deallocated by the caller.
449 * Return value: Returns IDNA_SUCCESS on success, or error code.
452 idna_to_ascii_4z (const uint32_t * input
, char **output
, int flags
)
454 const uint32_t *start
= input
;
455 const uint32_t *end
= input
;
460 /* 1) Whenever dots are used as label separators, the following
461 characters MUST be recognized as dots: U+002E (full stop),
462 U+3002 (ideographic full stop), U+FF0E (fullwidth full stop),
463 U+FF61 (halfwidth ideographic full stop). */
467 /* Handle implicit zero-length root label. */
468 *output
= malloc (1);
470 return IDNA_MALLOC_ERROR
;
471 strcpy (*output
, "");
475 if (DOTP (input
[0]) && input
[1] == 0)
477 /* Handle explicit zero-length root label. */
478 *output
= malloc (2);
480 return IDNA_MALLOC_ERROR
;
481 strcpy (*output
, ".");
490 for (; *end
&& !DOTP (*end
); end
++)
493 if (*end
== '\0' && start
== end
)
495 /* Handle explicit zero-length root label. */
500 rc
= idna_to_ascii_4i (start
, end
- start
, buf
, flags
);
501 if (rc
!= IDNA_SUCCESS
)
507 char *newp
= realloc (out
, strlen (out
) + 1 + strlen (buf
) + 1);
511 return IDNA_MALLOC_ERROR
;
519 out
= (char *) malloc (strlen (buf
) + 1);
521 return IDNA_MALLOC_ERROR
;
536 * @input: zero terminated input UTF-8 string.
537 * @output: pointer to newly allocated output string.
538 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
540 * Convert UTF-8 domain name to ASCII string. The domain name may
541 * contain several labels, separated by dots. The output buffer must
542 * be deallocated by the caller.
544 * Return value: Returns IDNA_SUCCESS on success, or error code.
547 idna_to_ascii_8z (const char *input
, char **output
, int flags
)
553 ucs4
= stringprep_utf8_to_ucs4 (input
, -1, &ucs4len
);
555 return IDNA_ICONV_ERROR
;
557 rc
= idna_to_ascii_4z (ucs4
, output
, flags
);
567 * @input: zero terminated input UTF-8 string.
568 * @output: pointer to newly allocated output string.
569 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
571 * Convert domain name in the locale's encoding to ASCII string. The
572 * domain name may contain several labels, separated by dots. The
573 * output buffer must be deallocated by the caller.
575 * Return value: Returns IDNA_SUCCESS on success, or error code.
578 idna_to_ascii_lz (const char *input
, char **output
, int flags
)
583 utf8
= stringprep_locale_to_utf8 (input
);
585 return IDNA_ICONV_ERROR
;
587 rc
= idna_to_ascii_8z (utf8
, output
, flags
);
595 * idna_to_unicode_4z4z:
596 * @input: zero-terminated Unicode string.
597 * @output: pointer to newly allocated output Unicode string.
598 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
600 * Convert possibly ACE encoded domain name in UCS-4 format into a
601 * UCS-4 string. The domain name may contain several labels,
602 * separated by dots. The output buffer must be deallocated by the
605 * Return value: Returns IDNA_SUCCESS on success, or error code.
608 idna_to_unicode_4z4z (const uint32_t * input
, uint32_t ** output
, int flags
)
610 const uint32_t *start
= input
;
611 const uint32_t *end
= input
;
614 uint32_t *out
= NULL
;
623 for (; *end
&& !DOTP (*end
); end
++)
626 buflen
= end
- start
;
627 buf
= malloc (sizeof (buf
[0]) * (buflen
+ 1));
629 return IDNA_MALLOC_ERROR
;
631 idna_to_unicode_44i (start
, end
- start
, buf
, &buflen
, flags
);
632 /* don't check return value as per specification! */
636 uint32_t *newp
= realloc (out
,
638 * (outlen
+ 1 + buflen
+ 1));
643 return IDNA_MALLOC_ERROR
;
646 out
[outlen
++] = 0x002E; /* '.' (full stop) */
647 memcpy (out
+ outlen
, buf
, sizeof (buf
[0]) * buflen
);
669 * idna_to_unicode_8z4z:
670 * @input: zero-terminated UTF-8 string.
671 * @output: pointer to newly allocated output Unicode string.
672 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
674 * Convert possibly ACE encoded domain name in UTF-8 format into a
675 * UCS-4 string. The domain name may contain several labels,
676 * separated by dots. The output buffer must be deallocated by the
679 * Return value: Returns IDNA_SUCCESS on success, or error code.
682 idna_to_unicode_8z4z (const char *input
, uint32_t ** output
, int flags
)
688 ucs4
= stringprep_utf8_to_ucs4 (input
, -1, &ucs4len
);
690 return IDNA_ICONV_ERROR
;
692 rc
= idna_to_unicode_4z4z (ucs4
, output
, flags
);
699 * idna_to_unicode_8z8z:
700 * @input: zero-terminated UTF-8 string.
701 * @output: pointer to newly allocated output UTF-8 string.
702 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
704 * Convert possibly ACE encoded domain name in UTF-8 format into a
705 * UTF-8 string. The domain name may contain several labels,
706 * separated by dots. The output buffer must be deallocated by the
709 * Return value: Returns IDNA_SUCCESS on success, or error code.
712 idna_to_unicode_8z8z (const char *input
, char **output
, int flags
)
717 rc
= idna_to_unicode_8z4z (input
, &ucs4
, flags
);
718 *output
= stringprep_ucs4_to_utf8 (ucs4
, -1, NULL
, NULL
);
722 return IDNA_ICONV_ERROR
;
728 * idna_to_unicode_8zlz:
729 * @input: zero-terminated UTF-8 string.
730 * @output: pointer to newly allocated output string encoded in the
731 * current locale's character set.
732 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
734 * Convert possibly ACE encoded domain name in UTF-8 format into a
735 * string encoded in the current locale's character set. The domain
736 * name may contain several labels, separated by dots. The output
737 * buffer must be deallocated by the caller.
739 * Return value: Returns IDNA_SUCCESS on success, or error code.
742 idna_to_unicode_8zlz (const char *input
, char **output
, int flags
)
747 rc
= idna_to_unicode_8z8z (input
, &utf8
, flags
);
748 *output
= stringprep_utf8_to_locale (utf8
);
752 return IDNA_ICONV_ERROR
;
758 * idna_to_unicode_lzlz:
759 * @input: zero-terminated string encoded in the current locale's
761 * @output: pointer to newly allocated output string encoded in the
762 * current locale's character set.
763 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
765 * Convert possibly ACE encoded domain name in the locale's character
766 * set into a string encoded in the current locale's character set.
767 * The domain name may contain several labels, separated by dots. The
768 * output buffer must be deallocated by the caller.
770 * Return value: Returns IDNA_SUCCESS on success, or error code.
773 idna_to_unicode_lzlz (const char *input
, char **output
, int flags
)
778 utf8
= stringprep_locale_to_utf8 (input
);
780 return IDNA_ICONV_ERROR
;
782 rc
= idna_to_unicode_8zlz (utf8
, output
, flags
);
791 * The IANA allocated prefix to use for IDNA. "xn--"
796 * @IDNA_SUCCESS: Successful operation. This value is guaranteed to
797 * always be zero, the remaining ones are only guaranteed to hold
798 * non-zero values, for logical comparison purposes.
799 * @IDNA_STRINGPREP_ERROR: Error during string preparation.
800 * @IDNA_PUNYCODE_ERROR: Error during punycode operation.
801 * @IDNA_CONTAINS_NON_LDH: For IDNA_USE_STD3_ASCII_RULES, indicate that
802 * the string contains non-LDH ASCII characters.
803 * @IDNA_CONTAINS_MINUS: For IDNA_USE_STD3_ASCII_RULES, indicate that
804 * the string contains a leading or trailing hyphen-minus (U+002D).
805 * @IDNA_INVALID_LENGTH: The final output string is not within the
806 * (inclusive) range 1 to 63 characters.
807 * @IDNA_NO_ACE_PREFIX: The string does not contain the ACE prefix
809 * @IDNA_ROUNDTRIP_VERIFY_ERROR: The ToASCII operation on output
810 * string does not equal the input.
811 * @IDNA_CONTAINS_ACE_PREFIX: The input contains the ACE prefix (for
813 * @IDNA_ICONV_ERROR: Could not convert string in locale encoding.
814 * @IDNA_MALLOC_ERROR: Could not allocate buffer (this is typically a
816 * @IDNA_DLOPEN_ERROR: Could not dlopen the libcidn DSO (only used
817 * internally in libc).
819 * Enumerated return codes of idna_to_ascii_4i(),
820 * idna_to_unicode_44i() functions (and functions derived from those
821 * functions). The value 0 is guaranteed to always correspond to
828 * @IDNA_ALLOW_UNASSIGNED: Don't reject strings containing unassigned
829 * Unicode code points.
830 * @IDNA_USE_STD3_ASCII_RULES: Validate strings according to STD3
831 * rules (i.e., normal host name rules).
833 * Flags to pass to idna_to_ascii_4i(), idna_to_unicode_44i() etc.