1 /* idna.c Convert to or from IDN strings.
2 * Copyright (C) 2002, 2003, 2004 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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <stringprep.h>
33 #define DOTP(c) ((c) == 0x002E || (c) == 0x3002 || \
34 (c) == 0xFF0E || (c) == 0xFF61)
40 * @in: input array with unicode code points.
41 * @inlen: length of input array with unicode code points.
42 * @out: output zero terminated string that must have room for at
43 * least 63 characters plus the terminating zero.
44 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
46 * The ToASCII operation takes a sequence of Unicode code points that make
47 * up one label and transforms it into a sequence of code points in the
48 * ASCII range (0..7F). If ToASCII succeeds, the original sequence and the
49 * resulting sequence are equivalent labels.
51 * It is important to note that the ToASCII operation can fail. ToASCII
52 * fails if any step of it fails. If any step of the ToASCII operation
53 * fails on any label in a domain name, that domain name MUST NOT be used
54 * as an internationalized domain name. The method for deadling with this
55 * failure is application-specific.
57 * The inputs to ToASCII are a sequence of code points, the AllowUnassigned
58 * flag, and the UseSTD3ASCIIRules flag. The output of ToASCII is either a
59 * sequence of ASCII code points or a failure condition.
61 * ToASCII never alters a sequence of code points that are all in the ASCII
62 * range to begin with (although it could fail). Applying the ToASCII
63 * operation multiple times has exactly the same effect as applying it just
66 * Return value: Returns 0 on success, or an error code.
69 idna_to_ascii_4i (const uint32_t * in
, size_t inlen
, char *out
, int flags
)
72 uint32_t *src
; /* XXX don't need to copy data? */
76 * ToASCII consists of the following steps:
78 * 1. If all code points in the sequence are in the ASCII range (0..7F)
79 * then skip to step 3.
87 for (i
= 0; i
< inlen
; i
++)
92 src
= malloc (sizeof (in
[0]) * (inlen
+ 1));
94 return IDNA_MALLOC_ERROR
;
96 memcpy (src
, in
, sizeof (in
[0]) * inlen
);
104 * 2. Perform the steps specified in [NAMEPREP] and fail if there is
105 * an error. The AllowUnassigned flag is used in [NAMEPREP].
111 p
= stringprep_ucs4_to_utf8 (in
, inlen
, NULL
, NULL
);
113 return IDNA_MALLOC_ERROR
;
120 len
= 2 * len
+ 10; /* XXX better guess? */
121 newp
= realloc (p
, len
);
125 return IDNA_MALLOC_ERROR
;
129 if (flags
& IDNA_ALLOW_UNASSIGNED
)
130 rc
= stringprep_nameprep (p
, len
);
132 rc
= stringprep_nameprep_no_unassigned (p
, len
);
134 while (rc
== STRINGPREP_TOO_SMALL_BUFFER
);
136 if (rc
!= STRINGPREP_OK
)
139 return IDNA_STRINGPREP_ERROR
;
142 src
= stringprep_utf8_to_ucs4 (p
, -1, NULL
);
149 * 3. If the UseSTD3ASCIIRules flag is set, then perform these checks:
151 * (a) Verify the absence of non-LDH ASCII code points; that is,
152 * the absence of 0..2C, 2E..2F, 3A..40, 5B..60, and 7B..7F.
154 * (b) Verify the absence of leading and trailing hyphen-minus;
155 * that is, the absence of U+002D at the beginning and end of
159 if (flags
& IDNA_USE_STD3_ASCII_RULES
)
163 for (i
= 0; src
[i
]; i
++)
164 if (src
[i
] <= 0x2C || src
[i
] == 0x2E || src
[i
] == 0x2F ||
165 (src
[i
] >= 0x3A && src
[i
] <= 0x40) ||
166 (src
[i
] >= 0x5B && src
[i
] <= 0x60) ||
167 (src
[i
] >= 0x7B && src
[i
] <= 0x7F))
170 return IDNA_CONTAINS_NON_LDH
;
173 if (src
[0] == 0x002D || (i
> 0 && src
[i
- 1] == 0x002D))
176 return IDNA_CONTAINS_MINUS
;
181 * 4. If all code points in the sequence are in the ASCII range
182 * (0..7F), then skip to step 8.
190 for (i
= 0; src
[i
]; i
++)
194 /* copy string to output buffer if we are about to skip to step8 */
205 * 5. Verify that the sequence does NOT begin with the ACE prefix.
214 for (i
= 0; match
&& i
< strlen (IDNA_ACE_PREFIX
); i
++)
215 if (((uint32_t) IDNA_ACE_PREFIX
[i
] & 0xFF) != src
[i
])
220 return IDNA_CONTAINS_ACE_PREFIX
;
225 * 6. Encode the sequence using the encoding algorithm in [PUNYCODE]
226 * and fail if there is an error.
228 for (len
= 0; src
[len
]; len
++)
231 outlen
= 63 - strlen (IDNA_ACE_PREFIX
);
232 rc
= punycode_encode (len
, src
, NULL
,
233 &outlen
, &out
[strlen (IDNA_ACE_PREFIX
)]);
234 if (rc
!= PUNYCODE_SUCCESS
)
237 return IDNA_PUNYCODE_ERROR
;
239 out
[strlen (IDNA_ACE_PREFIX
) + outlen
] = '\0';
242 * 7. Prepend the ACE prefix.
245 memcpy (out
, IDNA_ACE_PREFIX
, strlen (IDNA_ACE_PREFIX
));
248 * 8. Verify that the number of code points is in the range 1 to 63
249 * inclusive (0 is excluded).
254 if (strlen (out
) < 1 || strlen (out
) > 63)
255 return IDNA_INVALID_LENGTH
;
260 /* ToUnicode(). May realloc() utf8in. */
262 idna_to_unicode_internal (char *utf8in
,
263 uint32_t * out
, size_t * outlen
, int flags
)
267 size_t utf8len
= strlen (utf8in
) + 1;
271 * ToUnicode consists of the following steps:
273 * 1. If the sequence contains any code points outside the ASCII range
274 * (0..7F) then proceed to step 2, otherwise skip to step 3.
282 for (i
= 0; utf8in
[i
]; i
++)
283 if (utf8in
[i
] & ~0x7F)
290 * 2. Perform the steps specified in [NAMEPREP] and fail if there is an
291 * error. (If step 3 of ToASCII is also performed here, it will not
292 * affect the overall behavior of ToUnicode, but it is not
293 * necessary.) The AllowUnassigned flag is used in [NAMEPREP].
297 char *newp
= realloc (utf8in
, utf8len
+ addlen
);
301 return IDNA_MALLOC_ERROR
;
304 if (flags
& IDNA_ALLOW_UNASSIGNED
)
305 rc
= stringprep_nameprep (utf8in
, utf8len
+ addlen
);
307 rc
= stringprep_nameprep_no_unassigned (utf8in
, utf8len
+ addlen
);
310 while (rc
== STRINGPREP_TOO_SMALL_BUFFER
);
312 if (rc
!= STRINGPREP_OK
)
315 return IDNA_STRINGPREP_ERROR
;
318 /* 3. Verify that the sequence begins with the ACE prefix, and save a
319 * copy of the sequence.
323 if (memcmp (IDNA_ACE_PREFIX
, utf8in
, strlen (IDNA_ACE_PREFIX
)) != 0)
326 return IDNA_NO_ACE_PREFIX
;
329 /* 4. Remove the ACE prefix.
332 memmove (utf8in
, &utf8in
[strlen (IDNA_ACE_PREFIX
)],
333 strlen (utf8in
) - strlen (IDNA_ACE_PREFIX
) + 1);
335 /* 5. Decode the sequence using the decoding algorithm in [PUNYCODE]
336 * and fail if there is an error. Save a copy of the result of
340 (*outlen
)--; /* reserve one for the zero */
342 rc
= punycode_decode (strlen (utf8in
), utf8in
, outlen
, out
, NULL
);
343 if (rc
!= PUNYCODE_SUCCESS
)
346 return IDNA_PUNYCODE_ERROR
;
349 out
[*outlen
] = 0; /* add zero */
354 rc
= idna_to_ascii_4i (out
, *outlen
, tmpout
, flags
);
355 if (rc
!= IDNA_SUCCESS
)
361 /* 7. Verify that the result of step 6 matches the saved copy from
362 * step 3, using a case-insensitive ASCII comparison.
365 if (strcasecmp (utf8in
, tmpout
+ strlen (IDNA_ACE_PREFIX
)) != 0)
368 return IDNA_ROUNDTRIP_VERIFY_ERROR
;
371 /* 8. Return the saved copy from step 5.
379 * idna_to_unicode_44i
380 * @in: input array with unicode code points.
381 * @inlen: length of input array with unicode code points.
382 * @out: output array with unicode code points.
383 * @outlen: on input, maximum size of output array with unicode code points,
384 * on exit, actual size of output array with unicode code points.
385 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
387 * The ToUnicode operation takes a sequence of Unicode code points
388 * that make up one label and returns a sequence of Unicode code
389 * points. If the input sequence is a label in ACE form, then the
390 * result is an equivalent internationalized label that is not in ACE
391 * form, otherwise the original sequence is returned unaltered.
393 * ToUnicode never fails. If any step fails, then the original input
394 * sequence is returned immediately in that step.
396 * The Punycode decoder can never output more code points than it
397 * inputs, but Nameprep can, and therefore ToUnicode can. Note that
398 * the number of octets needed to represent a sequence of code points
399 * depends on the particular character encoding used.
401 * The inputs to ToUnicode are a sequence of code points, the
402 * AllowUnassigned flag, and the UseSTD3ASCIIRules flag. The output of
403 * ToUnicode is always a sequence of Unicode code points.
405 * Return value: Returns error condition, but it must only be used for
406 * debugging purposes. The output buffer is always
407 * guaranteed to contain the correct data according to
408 * the specification (sans malloc induced errors). NB!
409 * This means that you normally ignore the return code
410 * from this function, as checking it means breaking the
414 idna_to_unicode_44i (const uint32_t * in
, size_t inlen
,
415 uint32_t * out
, size_t * outlen
, int flags
)
418 size_t outlensave
= *outlen
;
421 p
= stringprep_ucs4_to_utf8 (in
, inlen
, NULL
, NULL
);
423 return IDNA_MALLOC_ERROR
;
425 rc
= idna_to_unicode_internal (p
, out
, outlen
, flags
);
426 if (rc
!= IDNA_SUCCESS
)
428 memcpy (out
, in
, sizeof (in
[0]) * (inlen
< outlensave
?
429 inlen
: outlensave
));
433 /* p is freed in idna_to_unicode_internal. */
438 /* Wrappers that handle several labels */
442 * @input: zero terminated input Unicode string.
443 * @output: pointer to newly allocated output string.
444 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
446 * Convert UCS-4 domain name to ASCII string. The domain name may
447 * contain several labels, separated by dots. The output buffer must
448 * be deallocated by the caller.
450 * Return value: Returns IDNA_SUCCESS on success, or error code.
453 idna_to_ascii_4z (const uint32_t * input
, char **output
, int flags
)
455 const uint32_t *start
= input
;
456 const uint32_t *end
= input
;
461 /* 1) Whenever dots are used as label separators, the following
462 characters MUST be recognized as dots: U+002E (full stop),
463 U+3002 (ideographic full stop), U+FF0E (fullwidth full stop),
464 U+FF61 (halfwidth ideographic full stop). */
468 /* Handle implicit zero-length root label. */
469 *output
= malloc (1);
471 return IDNA_MALLOC_ERROR
;
472 strcpy (*output
, "");
476 if (DOTP (input
[0]) && input
[1] == 0)
478 /* Handle explicit zero-length root label. */
479 *output
= malloc (2);
481 return IDNA_MALLOC_ERROR
;
482 strcpy (*output
, ".");
491 for (; *end
&& !DOTP (*end
); end
++)
494 if (*end
== '\0' && start
== end
)
496 /* Handle explicit zero-length root label. */
501 rc
= idna_to_ascii_4i (start
, end
- start
, buf
, flags
);
502 if (rc
!= IDNA_SUCCESS
)
508 char *newp
= realloc (out
, strlen (out
) + 1 + strlen (buf
) + 1);
512 return IDNA_MALLOC_ERROR
;
520 out
= (char *) malloc (strlen (buf
) + 1);
522 return IDNA_MALLOC_ERROR
;
537 * @input: zero terminated input UTF-8 string.
538 * @output: pointer to newly allocated output string.
539 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
541 * Convert UTF-8 domain name to ASCII string. The domain name may
542 * contain several labels, separated by dots. The output buffer must
543 * be deallocated by the caller.
545 * Return value: Returns IDNA_SUCCESS on success, or error code.
548 idna_to_ascii_8z (const char *input
, char **output
, int flags
)
554 ucs4
= stringprep_utf8_to_ucs4 (input
, -1, &ucs4len
);
556 return IDNA_ICONV_ERROR
;
558 rc
= idna_to_ascii_4z (ucs4
, output
, flags
);
568 * @input: zero terminated input UTF-8 string.
569 * @output: pointer to newly allocated output string.
570 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
572 * Convert domain name in the locale's encoding to ASCII string. The
573 * domain name may contain several labels, separated by dots. The
574 * output buffer must be deallocated by the caller.
576 * Return value: Returns IDNA_SUCCESS on success, or error code.
579 idna_to_ascii_lz (const char *input
, char **output
, int flags
)
584 utf8
= stringprep_locale_to_utf8 (input
);
586 return IDNA_ICONV_ERROR
;
588 rc
= idna_to_ascii_8z (utf8
, output
, flags
);
596 * idna_to_unicode_4z4z:
597 * @input: zero-terminated Unicode string.
598 * @output: pointer to newly allocated output Unicode string.
599 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
601 * Convert possibly ACE encoded domain name in UCS-4 format into a
602 * UCS-4 string. The domain name may contain several labels,
603 * separated by dots. The output buffer must be deallocated by the
606 * Return value: Returns IDNA_SUCCESS on success, or error code.
609 idna_to_unicode_4z4z (const uint32_t * input
, uint32_t ** output
, int flags
)
611 const uint32_t *start
= input
;
612 const uint32_t *end
= input
;
615 uint32_t *out
= NULL
;
625 for (; *end
&& !DOTP (*end
); end
++)
628 buflen
= end
- start
;
629 buf
= malloc (sizeof (buf
[0]) * (buflen
+ 1));
631 return IDNA_MALLOC_ERROR
;
633 rc
= idna_to_unicode_44i (start
, end
- start
, buf
, &buflen
, flags
);
634 /* don't check rc as per specification! */
638 uint32_t *newp
= realloc (out
,
640 * (outlen
+ 1 + buflen
+ 1));
645 return IDNA_MALLOC_ERROR
;
648 out
[outlen
++] = 0x002E; /* '.' (full stop) */
649 memcpy (out
+ outlen
, buf
, sizeof (buf
[0]) * buflen
);
671 * idna_to_unicode_8z4z:
672 * @input: zero-terminated UTF-8 string.
673 * @output: pointer to newly allocated output Unicode string.
674 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
676 * Convert possibly ACE encoded domain name in UTF-8 format into a
677 * UCS-4 string. The domain name may contain several labels,
678 * separated by dots. The output buffer must be deallocated by the
681 * Return value: Returns IDNA_SUCCESS on success, or error code.
684 idna_to_unicode_8z4z (const char *input
, uint32_t ** output
, int flags
)
690 ucs4
= stringprep_utf8_to_ucs4 (input
, -1, &ucs4len
);
692 return IDNA_ICONV_ERROR
;
694 rc
= idna_to_unicode_4z4z (ucs4
, output
, flags
);
701 * idna_to_unicode_8z8z:
702 * @input: zero-terminated UTF-8 string.
703 * @output: pointer to newly allocated output UTF-8 string.
704 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
706 * Convert possibly ACE encoded domain name in UTF-8 format into a
707 * UTF-8 string. The domain name may contain several labels,
708 * separated by dots. The output buffer must be deallocated by the
711 * Return value: Returns IDNA_SUCCESS on success, or error code.
714 idna_to_unicode_8z8z (const char *input
, char **output
, int flags
)
719 rc
= idna_to_unicode_8z4z (input
, &ucs4
, flags
);
720 *output
= stringprep_ucs4_to_utf8 (ucs4
, -1, NULL
, NULL
);
724 return IDNA_ICONV_ERROR
;
730 * idna_to_unicode_8zlz:
731 * @input: zero-terminated UTF-8 string.
732 * @output: pointer to newly allocated output string encoded in the
733 * current locale's character set.
734 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
736 * Convert possibly ACE encoded domain name in UTF-8 format into a
737 * string encoded in the current locale's character set. The domain
738 * name may contain several labels, separated by dots. The output
739 * buffer must be deallocated by the caller.
741 * Return value: Returns IDNA_SUCCESS on success, or error code.
744 idna_to_unicode_8zlz (const char *input
, char **output
, int flags
)
749 rc
= idna_to_unicode_8z8z (input
, &utf8
, flags
);
750 *output
= stringprep_utf8_to_locale (utf8
);
754 return IDNA_ICONV_ERROR
;
760 * idna_to_unicode_lzlz:
761 * @input: zero-terminated string encoded in the current locale's
763 * @output: pointer to newly allocated output string encoded in the
764 * current locale's character set.
765 * @flags: IDNA flags, e.g. IDNA_ALLOW_UNASSIGNED or IDNA_USE_STD3_ASCII_RULES.
767 * Convert possibly ACE encoded domain name in the locale's character
768 * set into a string encoded in the current locale's character set.
769 * The domain name may contain several labels, separated by dots. The
770 * output buffer must be deallocated by the caller.
772 * Return value: Returns IDNA_SUCCESS on success, or error code.
775 idna_to_unicode_lzlz (const char *input
, char **output
, int flags
)
780 utf8
= stringprep_locale_to_utf8 (input
);
782 return IDNA_ICONV_ERROR
;
784 rc
= idna_to_unicode_8zlz (utf8
, output
, flags
);
793 * The IANA allocated prefix to use for IDNA. "xn--"
798 * @IDNA_SUCCESS: Successful operation. This value is guaranteed to
799 * always be zero, the remaining ones are only guaranteed to hold
800 * non-zero values, for logical comparison purposes.
801 * @IDNA_STRINGPREP_ERROR: Error during string preparation.
802 * @IDNA_PUNYCODE_ERROR: Error during punycode operation.
803 * @IDNA_CONTAINS_NON_LDH: For IDNA_USE_STD3_ASCII_RULES, indicate that
804 * the string contains non-LDH ASCII characters.
805 * @IDNA_CONTAINS_MINUS: For IDNA_USE_STD3_ASCII_RULES, indicate that
806 * the string contains a leading or trailing hyphen-minus (U+002D).
807 * @IDNA_INVALID_LENGTH: The final output string is not within the
808 * (inclusive) range 1 to 63 characters.
809 * @IDNA_NO_ACE_PREFIX: The string does not contain the ACE prefix
811 * @IDNA_ROUNDTRIP_VERIFY_ERROR: The ToASCII operation on output
812 * string does not equal the input.
813 * @IDNA_CONTAINS_ACE_PREFIX: The input contains the ACE prefix (for
815 * @IDNA_ICONV_ERROR: Could not convert string in locale encoding.
816 * @IDNA_MALLOC_ERROR: Could not allocate buffer (this is typically a
818 * @IDNA_DLOPEN_ERROR: Could not dlopen the libcidn DSO (only used
819 * internally in libc).
821 * Enumerated return codes of idna_to_ascii_4i(),
822 * idna_to_unicode_44i() functions (and functions derived from those
823 * functions). The value 0 is guaranteed to always correspond to
830 * @IDNA_ALLOW_UNASSIGNED: Don't reject strings containing unassigned
831 * Unicode code points.
832 * @IDNA_USE_STD3_ASCII_RULES: Validate strings according to STD3
833 * rules (i.e., normal host name rules).
835 * Flags to pass to idna_to_ascii_4i(), idna_to_unicode_44i() etc.