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>
31 #define DOTP(c) ((c) == 0x002E || (c) == 0x3002 || \
32 (c) == 0xFF0E || (c) == 0xFF61)
38 * @in: input array with unicode code points.
39 * @inlen: length of input array with unicode code points.
40 * @out: output zero terminated string that must have room for at
41 * least 63 characters plus the terminating zero.
42 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
44 * The ToASCII operation takes a sequence of Unicode code points that make
45 * up one label and transforms it into a sequence of code points in the
46 * ASCII range (0..7F). If ToASCII succeeds, the original sequence and the
47 * resulting sequence are equivalent labels.
49 * It is important to note that the ToASCII operation can fail. ToASCII
50 * fails if any step of it fails. If any step of the ToASCII operation
51 * fails on any label in a domain name, that domain name MUST NOT be used
52 * as an internationalized domain name. The method for deadling with this
53 * failure is application-specific.
55 * The inputs to ToASCII are a sequence of code points, the AllowUnassigned
56 * flag, and the UseSTD3ASCIIRules flag. The output of ToASCII is either a
57 * sequence of ASCII code points or a failure condition.
59 * ToASCII never alters a sequence of code points that are all in the ASCII
60 * range to begin with (although it could fail). Applying the ToASCII
61 * operation multiple times has exactly the same effect as applying it just
64 * Return value: Returns 0 on success, or an error code.
67 idna_to_ascii_4i (const uint32_t * in
, size_t inlen
, char *out
, int flags
)
70 uint32_t *src
; /* XXX don't need to copy data? */
74 * ToASCII consists of the following steps:
76 * 1. If all code points in the sequence are in the ASCII range (0..7F)
77 * then skip to step 3.
85 for (i
= 0; i
< inlen
; i
++)
90 src
= malloc (sizeof (in
[0]) * (inlen
+ 1));
92 return IDNA_MALLOC_ERROR
;
94 memcpy (src
, in
, sizeof (in
[0]) * inlen
);
102 * 2. Perform the steps specified in [NAMEPREP] and fail if there is
103 * an error. The AllowUnassigned flag is used in [NAMEPREP].
109 p
= stringprep_ucs4_to_utf8 (in
, inlen
, NULL
, NULL
);
111 return IDNA_MALLOC_ERROR
;
118 len
= 2 * len
+ 10; /* XXX better guess? */
119 newp
= realloc (p
, len
);
123 return IDNA_MALLOC_ERROR
;
127 if (flags
& IDNA_ALLOW_UNASSIGNED
)
128 rc
= stringprep_nameprep (p
, len
);
130 rc
= stringprep_nameprep_no_unassigned (p
, len
);
132 while (rc
== STRINGPREP_TOO_SMALL_BUFFER
);
134 if (rc
!= STRINGPREP_OK
)
137 return IDNA_STRINGPREP_ERROR
;
140 src
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
147 * 3. If the UseSTD3ASCIIRules flag is set, then perform these checks:
149 * (a) Verify the absence of non-LDH ASCII code points; that is,
150 * the absence of 0..2C, 2E..2F, 3A..40, 5B..60, and 7B..7F.
152 * (b) Verify the absence of leading and trailing hyphen-minus;
153 * that is, the absence of U+002D at the beginning and end of
157 if (flags
& IDNA_USE_STD3_ASCII_RULES
)
161 for (i
= 0; src
[i
]; i
++)
162 if (src
[i
] <= 0x2C || src
[i
] == 0x2E || src
[i
] == 0x2F ||
163 (src
[i
] >= 0x3A && src
[i
] <= 0x40) ||
164 (src
[i
] >= 0x5B && src
[i
] <= 0x60) ||
165 (src
[i
] >= 0x7B && src
[i
] <= 0x7F))
168 return IDNA_CONTAINS_NON_LDH
;
171 if (src
[0] == 0x002D || (i
> 0 && src
[i
- 1] == 0x002D))
174 return IDNA_CONTAINS_MINUS
;
179 * 4. If all code points in the sequence are in the ASCII range
180 * (0..7F), then skip to step 8.
188 for (i
= 0; src
[i
]; i
++)
192 /* copy string to output buffer if we are about to skip to step8 */
203 * 5. Verify that the sequence does NOT begin with the ACE prefix.
212 for (i
= 0; match
&& i
< strlen (IDNA_ACE_PREFIX
); i
++)
213 if (((uint32_t) IDNA_ACE_PREFIX
[i
] & 0xFF) != src
[i
])
218 return IDNA_CONTAINS_ACE_PREFIX
;
223 * 6. Encode the sequence using the encoding algorithm in [PUNYCODE]
224 * and fail if there is an error.
226 for (len
= 0; src
[len
]; len
++)
229 outlen
= 63 - strlen (IDNA_ACE_PREFIX
);
230 rc
= punycode_encode (len
, src
, NULL
,
231 &outlen
, &out
[strlen (IDNA_ACE_PREFIX
)]);
232 if (rc
!= PUNYCODE_SUCCESS
)
235 return IDNA_PUNYCODE_ERROR
;
237 out
[strlen (IDNA_ACE_PREFIX
) + outlen
] = '\0';
240 * 7. Prepend the ACE prefix.
243 memcpy (out
, IDNA_ACE_PREFIX
, strlen (IDNA_ACE_PREFIX
));
246 * 8. Verify that the number of code points is in the range 1 to 63
247 * inclusive (0 is excluded).
252 if (strlen (out
) < 1 || strlen (out
) > 63)
253 return IDNA_INVALID_LENGTH
;
258 /* ToUnicode(). May realloc() utf8in. */
260 idna_to_unicode_internal (char *utf8in
,
261 uint32_t * out
, size_t * outlen
, int flags
)
265 size_t utf8len
= strlen (utf8in
) + 1;
269 * ToUnicode consists of the following steps:
271 * 1. If the sequence contains any code points outside the ASCII range
272 * (0..7F) then proceed to step 2, otherwise skip to step 3.
280 for (i
= 0; utf8in
[i
]; i
++)
281 if (utf8in
[i
] & ~0x7F)
288 * 2. Perform the steps specified in [NAMEPREP] and fail if there is an
289 * error. (If step 3 of ToASCII is also performed here, it will not
290 * affect the overall behavior of ToUnicode, but it is not
291 * necessary.) The AllowUnassigned flag is used in [NAMEPREP].
295 char *newp
= realloc (utf8in
, utf8len
+ addlen
);
299 return IDNA_MALLOC_ERROR
;
302 if (flags
& IDNA_ALLOW_UNASSIGNED
)
303 rc
= stringprep_nameprep (utf8in
, utf8len
+ addlen
);
305 rc
= stringprep_nameprep_no_unassigned (utf8in
, utf8len
+ addlen
);
308 while (rc
== STRINGPREP_TOO_SMALL_BUFFER
);
310 if (rc
!= STRINGPREP_OK
)
313 return IDNA_STRINGPREP_ERROR
;
316 /* 3. Verify that the sequence begins with the ACE prefix, and save a
317 * copy of the sequence.
321 if (memcmp (IDNA_ACE_PREFIX
, utf8in
, strlen (IDNA_ACE_PREFIX
)) != 0)
324 return IDNA_NO_ACE_PREFIX
;
327 /* 4. Remove the ACE prefix.
330 memmove (utf8in
, &utf8in
[strlen (IDNA_ACE_PREFIX
)],
331 strlen (utf8in
) - strlen (IDNA_ACE_PREFIX
) + 1);
333 /* 5. Decode the sequence using the decoding algorithm in [PUNYCODE]
334 * and fail if there is an error. Save a copy of the result of
338 (*outlen
)--; /* reserve one for the zero */
340 rc
= punycode_decode (strlen (utf8in
), utf8in
, outlen
, out
, NULL
);
341 if (rc
!= PUNYCODE_SUCCESS
)
344 return IDNA_PUNYCODE_ERROR
;
347 out
[*outlen
] = 0; /* add zero */
352 rc
= idna_to_ascii_4i (out
, *outlen
, tmpout
, flags
);
353 if (rc
!= IDNA_SUCCESS
)
359 /* 7. Verify that the result of step 6 matches the saved copy from
360 * step 3, using a case-insensitive ASCII comparison.
363 if (strcasecmp (utf8in
, tmpout
+ strlen (IDNA_ACE_PREFIX
)) != 0)
366 return IDNA_ROUNDTRIP_VERIFY_ERROR
;
369 /* 8. Return the saved copy from step 5.
377 * idna_to_unicode_44i
378 * @in: input array with unicode code points.
379 * @inlen: length of input array with unicode code points.
380 * @out: output array with unicode code points.
381 * @outlen: on input, maximum size of output array with unicode code points,
382 * on exit, actual size of output array with unicode code points.
383 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
385 * The ToUnicode operation takes a sequence of Unicode code points
386 * that make up one label and returns a sequence of Unicode code
387 * points. If the input sequence is a label in ACE form, then the
388 * result is an equivalent internationalized label that is not in ACE
389 * form, otherwise the original sequence is returned unaltered.
391 * ToUnicode never fails. If any step fails, then the original input
392 * sequence is returned immediately in that step.
394 * The Punycode decoder can never output more code points than it
395 * inputs, but Nameprep can, and therefore ToUnicode can. Note that
396 * the number of octets needed to represent a sequence of code points
397 * depends on the particular character encoding used.
399 * The inputs to ToUnicode are a sequence of code points, the
400 * AllowUnassigned flag, and the UseSTD3ASCIIRules flag. The output of
401 * ToUnicode is always a sequence of Unicode code points.
403 * Return value: Returns error condition, but it must only be used for
404 * debugging purposes. The output buffer is always
405 * guaranteed to contain the correct data according to
406 * the specification (sans malloc induced errors). NB!
407 * This means that you normally ignore the return code
408 * from this function, as checking it means breaking the
412 idna_to_unicode_44i (const uint32_t * in
, size_t inlen
,
413 uint32_t * out
, size_t * outlen
, int flags
)
416 size_t outlensave
= *outlen
;
419 p
= stringprep_ucs4_to_utf8 (in
, inlen
, NULL
, NULL
);
421 return IDNA_MALLOC_ERROR
;
423 rc
= idna_to_unicode_internal (p
, out
, outlen
, flags
);
424 if (rc
!= IDNA_SUCCESS
)
426 memcpy (out
, in
, sizeof (in
[0]) * (inlen
< outlensave
?
427 inlen
: outlensave
));
431 /* p is freed in idna_to_unicode_internal. */
436 /* Wrappers that handle several labels */
440 * @input: zero terminated input Unicode string.
441 * @output: pointer to newly allocated output string.
442 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
444 * Convert UCS-4 domain name to ASCII string. The domain name may
445 * contain several labels, separated by dots. The output buffer must
446 * be deallocated by the caller.
448 * Return value: Returns IDNA_SUCCESS on success, or error code.
451 idna_to_ascii_4z (const uint32_t * input
, char **output
, int flags
)
453 const uint32_t *start
= input
;
454 const uint32_t *end
= input
;
459 /* 1) Whenever dots are used as label separators, the following
460 characters MUST be recognized as dots: U+002E (full stop),
461 U+3002 (ideographic full stop), U+FF0E (fullwidth full stop),
462 U+FF61 (halfwidth ideographic full stop). */
466 /* Handle implicit zero-length root label. */
467 *output
= malloc (1);
469 return IDNA_MALLOC_ERROR
;
470 strcpy (*output
, "");
474 if (DOTP (input
[0]) && input
[1] == 0)
476 /* Handle explicit zero-length root label. */
477 *output
= malloc (2);
479 return IDNA_MALLOC_ERROR
;
480 strcpy (*output
, ".");
489 for (; *end
&& !DOTP (*end
); end
++)
492 if (*end
== '\0' && start
== end
)
494 /* Handle explicit zero-length root label. */
499 rc
= idna_to_ascii_4i (start
, end
- start
, buf
, flags
);
500 if (rc
!= IDNA_SUCCESS
)
506 char *newp
= realloc (out
, strlen (out
) + 1 + strlen (buf
) + 1);
510 return IDNA_MALLOC_ERROR
;
518 out
= (char *) malloc (strlen (buf
) + 1);
520 return IDNA_MALLOC_ERROR
;
535 * @input: zero terminated input UTF-8 string.
536 * @output: pointer to newly allocated output string.
537 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
539 * Convert UTF-8 domain name to ASCII string. The domain name may
540 * contain several labels, separated by dots. The output buffer must
541 * be deallocated by the caller.
543 * Return value: Returns IDNA_SUCCESS on success, or error code.
546 idna_to_ascii_8z (const char *input
, char **output
, int flags
)
552 ucs4
= stringprep_utf8_to_ucs4 (input
, -1, &ucs4len
);
554 return IDNA_ICONV_ERROR
;
556 rc
= idna_to_ascii_4z (ucs4
, output
, flags
);
566 * @input: zero terminated input UTF-8 string.
567 * @output: pointer to newly allocated output string.
568 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
570 * Convert domain name in the locale's encoding to ASCII string. The
571 * domain name may contain several labels, separated by dots. The
572 * output buffer must be deallocated by the caller.
574 * Return value: Returns IDNA_SUCCESS on success, or error code.
577 idna_to_ascii_lz (const char *input
, char **output
, int flags
)
582 utf8
= stringprep_locale_to_utf8 (input
);
584 return IDNA_ICONV_ERROR
;
586 rc
= idna_to_ascii_8z (utf8
, output
, flags
);
594 * idna_to_unicode_4z4z:
595 * @input: zero-terminated Unicode string.
596 * @output: pointer to newly allocated output Unicode string.
597 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
599 * Convert possibly ACE encoded domain name in UCS-4 format into a
600 * UCS-4 string. The domain name may contain several labels,
601 * separated by dots. The output buffer must be deallocated by the
604 * Return value: Returns IDNA_SUCCESS on success, or error code.
607 idna_to_unicode_4z4z (const uint32_t * input
, uint32_t ** output
, int flags
)
609 const uint32_t *start
= input
;
610 const uint32_t *end
= input
;
613 uint32_t *out
= NULL
;
622 for (; *end
&& !DOTP (*end
); end
++)
625 buflen
= end
- start
;
626 buf
= malloc (sizeof (buf
[0]) * (buflen
+ 1));
628 return IDNA_MALLOC_ERROR
;
630 idna_to_unicode_44i (start
, end
- start
, buf
, &buflen
, flags
);
631 /* don't check return value as per specification! */
635 uint32_t *newp
= realloc (out
,
637 * (outlen
+ 1 + buflen
+ 1));
642 return IDNA_MALLOC_ERROR
;
645 out
[outlen
++] = 0x002E; /* '.' (full stop) */
646 memcpy (out
+ outlen
, buf
, sizeof (buf
[0]) * buflen
);
668 * idna_to_unicode_8z4z:
669 * @input: zero-terminated UTF-8 string.
670 * @output: pointer to newly allocated output Unicode string.
671 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
673 * Convert possibly ACE encoded domain name in UTF-8 format into a
674 * UCS-4 string. The domain name may contain several labels,
675 * separated by dots. The output buffer must be deallocated by the
678 * Return value: Returns IDNA_SUCCESS on success, or error code.
681 idna_to_unicode_8z4z (const char *input
, uint32_t ** output
, int flags
)
687 ucs4
= stringprep_utf8_to_ucs4 (input
, -1, &ucs4len
);
689 return IDNA_ICONV_ERROR
;
691 rc
= idna_to_unicode_4z4z (ucs4
, output
, flags
);
698 * idna_to_unicode_8z8z:
699 * @input: zero-terminated UTF-8 string.
700 * @output: pointer to newly allocated output UTF-8 string.
701 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
703 * Convert possibly ACE encoded domain name in UTF-8 format into a
704 * UTF-8 string. The domain name may contain several labels,
705 * separated by dots. The output buffer must be deallocated by the
708 * Return value: Returns IDNA_SUCCESS on success, or error code.
711 idna_to_unicode_8z8z (const char *input
, char **output
, int flags
)
716 rc
= idna_to_unicode_8z4z (input
, &ucs4
, flags
);
717 *output
= stringprep_ucs4_to_utf8 (ucs4
, -1, NULL
, NULL
);
721 return IDNA_ICONV_ERROR
;
727 * idna_to_unicode_8zlz:
728 * @input: zero-terminated UTF-8 string.
729 * @output: pointer to newly allocated output string encoded in the
730 * current locale's character set.
731 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
733 * Convert possibly ACE encoded domain name in UTF-8 format into a
734 * string encoded in the current locale's character set. The domain
735 * name may contain several labels, separated by dots. The output
736 * buffer must be deallocated by the caller.
738 * Return value: Returns IDNA_SUCCESS on success, or error code.
741 idna_to_unicode_8zlz (const char *input
, char **output
, int flags
)
746 rc
= idna_to_unicode_8z8z (input
, &utf8
, flags
);
747 *output
= stringprep_utf8_to_locale (utf8
);
751 return IDNA_ICONV_ERROR
;
757 * idna_to_unicode_lzlz:
758 * @input: zero-terminated string encoded in the current locale's
760 * @output: pointer to newly allocated output string encoded in the
761 * current locale's character set.
762 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
764 * Convert possibly ACE encoded domain name in the locale's character
765 * set into a string encoded in the current locale's character set.
766 * The domain name may contain several labels, separated by dots. The
767 * output buffer must be deallocated by the caller.
769 * Return value: Returns IDNA_SUCCESS on success, or error code.
772 idna_to_unicode_lzlz (const char *input
, char **output
, int flags
)
777 utf8
= stringprep_locale_to_utf8 (input
);
779 return IDNA_ICONV_ERROR
;
781 rc
= idna_to_unicode_8zlz (utf8
, output
, flags
);
790 * The IANA allocated prefix to use for IDNA. "xn--"
795 * @IDNA_SUCCESS: Successful operation. This value is guaranteed to
796 * always be zero, the remaining ones are only guaranteed to hold
797 * non-zero values, for logical comparison purposes.
798 * @IDNA_STRINGPREP_ERROR: Error during string preparation.
799 * @IDNA_PUNYCODE_ERROR: Error during punycode operation.
800 * @IDNA_CONTAINS_NON_LDH: For IDNA_USE_STD3_ASCII_RULES, indicate that
801 * the string contains non-LDH ASCII characters.
802 * @IDNA_CONTAINS_MINUS: For IDNA_USE_STD3_ASCII_RULES, indicate that
803 * the string contains a leading or trailing hyphen-minus (U+002D).
804 * @IDNA_INVALID_LENGTH: The final output string is not within the
805 * (inclusive) range 1 to 63 characters.
806 * @IDNA_NO_ACE_PREFIX: The string does not contain the ACE prefix
808 * @IDNA_ROUNDTRIP_VERIFY_ERROR: The ToASCII operation on output
809 * string does not equal the input.
810 * @IDNA_CONTAINS_ACE_PREFIX: The input contains the ACE prefix (for
812 * @IDNA_ICONV_ERROR: Could not convert string in locale encoding.
813 * @IDNA_MALLOC_ERROR: Could not allocate buffer (this is typically a
815 * @IDNA_DLOPEN_ERROR: Could not dlopen the libcidn DSO (only used
816 * internally in libc).
818 * Enumerated return codes of idna_to_ascii_4i(),
819 * idna_to_unicode_44i() functions (and functions derived from those
820 * functions). The value 0 is guaranteed to always correspond to
827 * @IDNA_ALLOW_UNASSIGNED: Don't reject strings containing unassigned
828 * Unicode code points.
829 * @IDNA_USE_STD3_ASCII_RULES: Validate strings according to STD3
830 * rules (i.e., normal host name rules).
832 * Flags to pass to idna_to_ascii_4i(), idna_to_unicode_44i() etc.