Adam Tkac 7c0542
diff -up bind-9.5.0b1/bin/dig/dighost.c.libidn2 bind-9.5.0b1/bin/dig/dighost.c
Adam Tkac 7c0542
--- bind-9.5.0b1/bin/dig/dighost.c.libidn2	2007-12-10 13:12:26.000000000 +0100
Adam Tkac 7c0542
+++ bind-9.5.0b1/bin/dig/dighost.c	2007-12-10 14:21:09.000000000 +0100
Adam Tkac 7c0542
@@ -153,7 +153,7 @@ int  idnoptions	= 0;
Adam Tkac 7c0542
 #endif
Adam Tkac 7c0542
 
Adam Tkac 7c0542
 #ifdef WITH_LIBIDN
Adam Tkac 7c0542
-static isc_result_t	libidn_locale_to_utf8 (const char* from, char **to);
Adam Tkac 7c0542
+static isc_result_t	libidn_locale_to_utf8 (const char* from, char *to);
Adam Tkac 7c0542
 static isc_result_t	libidn_utf8_to_ascii (const char* from, char *to);
Adam Tkac 7c0542
 static isc_result_t	output_filter (isc_buffer_t *buffer,
Adam Tkac 7c0542
 				       unsigned int used_org,
Adam Tkac 7c0542
@@ -1764,17 +1764,13 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 7c0542
 	char utf8_textname[MXNAME], utf8_origin[MXNAME], idn_textname[MXNAME];
Adam Tkac 7c0542
 #endif
Adam Tkac 7c0542
 #ifdef WITH_LIBIDN
Adam Tkac 7c0542
-	char *utf8_str = NULL, utf8_name[MXNAME], ascii_name[MXNAME];
Adam Tkac 7c0542
+	char utf8_str[MXNAME], utf8_name[MXNAME], ascii_name[MXNAME];
Adam Tkac 7c0542
 #endif
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-#ifdef WITH_IDN
Adam Tkac 7c0542
+#if defined (WITH_IDN) || defined (WITH_LIBIDN)
Adam Tkac 7c0542
 	result = dns_name_settotextfilter(output_filter);
Adam Tkac 7c0542
 	check_result(result, "dns_name_settotextfilter");
Adam Tkac 7c0542
 #endif
Adam Tkac 7c0542
-#ifdef WITH_LIBIDN
Adam Tkac 7c0542
-	result = dns_name_settotextfilter (output_filter);
Adam Tkac 7c0542
-	check_result(result, "dns_name_settotextfilter");
Adam Tkac 7c0542
-#endif
Adam Tkac 7c0542
 	REQUIRE(lookup != NULL);
Adam Tkac 7c0542
 	INSIST(!free_now);
Adam Tkac 7c0542
 
Adam Tkac 7c0542
@@ -1812,15 +1808,13 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 7c0542
 			    utf8_textname, sizeof(utf8_textname));
Adam Tkac 7c0542
 	idn_check_result(mr, "convert textname to UTF-8");
Adam Tkac 7c0542
 #elif defined (WITH_LIBIDN)
Adam Tkac 7c0542
-	result = libidn_locale_to_utf8 (lookup->textname, &utf8_str);
Adam Tkac 7c0542
-	check_result (result, "converting textname to UTF-8");
Adam Tkac 7c0542
+	result = libidn_locale_to_utf8 (lookup->textname, utf8_str);
Adam Tkac 7c0542
+	check_result (result, "convert textname to UTF-8");
Adam Tkac 7c0542
 	len = strlen (utf8_str);
Adam Tkac 7c0542
-	if (len < MXNAME) {
Adam Tkac 7c0542
+	if (len < MXNAME)
Adam Tkac 7c0542
 		(void) strcpy (utf8_name, utf8_str);
Adam Tkac 7c0542
-	} else {
Adam Tkac 7c0542
+	else
Adam Tkac 7c0542
 		fatal ("Too long name");
Adam Tkac 7c0542
-	}
Adam Tkac 7c0542
-	isc_mem_free (mctx, utf8_str);
Adam Tkac 7c0542
 #endif
Adam Tkac 7c0542
 
Adam Tkac 7c0542
 	/*
Adam Tkac 7c0542
@@ -1833,24 +1827,11 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 7c0542
 	if (lookup->new_search) {
Adam Tkac 7c0542
 #ifdef WITH_IDN
Adam Tkac 7c0542
 		if ((count_dots(utf8_textname) >= ndots) || !usesearch) {
Adam Tkac 7c0542
-			lookup->origin = NULL; /* Force abs lookup */
Adam Tkac 7c0542
-			lookup->done_as_is = ISC_TRUE;
Adam Tkac 7c0542
-			lookup->need_search = usesearch;
Adam Tkac 7c0542
-		} else if (lookup->origin == NULL && usesearch) {
Adam Tkac 7c0542
-			lookup->origin = ISC_LIST_HEAD(search_list);
Adam Tkac 7c0542
-			lookup->need_search = ISC_FALSE;
Adam Tkac 7c0542
-		}
Adam Tkac 7c0542
 #elif defined (WITH_LIBIDN)
Adam Tkac 7c0542
 		if ((count_dots(utf8_name) >= ndots) || !usesearch) {
Adam Tkac 7c0542
-			lookup->origin = NULL; /* Force abs lookup */
Adam Tkac 7c0542
-			lookup->done_as_is = ISC_TRUE;
Adam Tkac 7c0542
-			lookup->need_search = usesearch;
Adam Tkac 7c0542
-		} else if (lookup->origin == NULL && usesearch) {
Adam Tkac 7c0542
-			lookup->origin = ISC_LIST_HEAD(search_list);
Adam Tkac 7c0542
-			lookup->need_search = ISC_FALSE;
Adam Tkac 7c0542
-		}
Adam Tkac 7c0542
 #else
Adam Tkac 7c0542
 		if ((count_dots(lookup->textname) >= ndots) || !usesearch) {
Adam Tkac 7c0542
+#endif
Adam Tkac 7c0542
 			lookup->origin = NULL; /* Force abs lookup */
Adam Tkac 7c0542
 			lookup->done_as_is = ISC_TRUE;
Adam Tkac 7c0542
 			lookup->need_search = usesearch;
Adam Tkac 7c0542
@@ -1858,7 +1839,6 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 7c0542
 			lookup->origin = ISC_LIST_HEAD(search_list);
Adam Tkac 7c0542
 			lookup->need_search = ISC_FALSE;
Adam Tkac 7c0542
 		}
Adam Tkac 7c0542
-#endif
Adam Tkac 7c0542
 	}
Adam Tkac 7c0542
 
Adam Tkac 7c0542
 #ifdef WITH_IDN
Adam Tkac 7c0542
@@ -1877,15 +1857,12 @@ setup_lookup(dig_lookup_t *lookup) {
Adam Tkac 7c0542
 	idn_check_result(mr, "convert UTF-8 textname to IDN encoding");
Adam Tkac 7c0542
 #elif defined (WITH_LIBIDN)
Adam Tkac 7c0542
 	if (lookup->origin != NULL) {
Adam Tkac 7c0542
-		result = libidn_locale_to_utf8 (lookup->origin->origin, &utf8_str);
Adam Tkac 7c0542
+		result = libidn_locale_to_utf8 (lookup->origin->origin, utf8_str);
Adam Tkac 7c0542
 		check_result (result, "convert origin to UTF-8");
Adam Tkac 7c0542
-		if (len + strlen (utf8_str) + 1 < MXNAME) {
Adam Tkac 7c0542
-			utf8_name[len++] = '.';
Adam Tkac 7c0542
+		if (len + strlen (utf8_str) < MXNAME)
Adam Tkac 7c0542
 			(void) strcpy (utf8_name + len, utf8_str);
Adam Tkac 7c0542
-		} else {
Adam Tkac 7c0542
+		else
Adam Tkac 7c0542
 			fatal ("Too long name + origin");
Adam Tkac 7c0542
-		}
Adam Tkac 7c0542
-		isc_mem_free (mctx, utf8_str);
Adam Tkac 7c0542
 	}
Adam Tkac 7c0542
 
Adam Tkac 7c0542
 	result = libidn_utf8_to_ascii (utf8_name, ascii_name);
Adam Tkac 7c0542
@@ -3600,76 +3577,85 @@ idn_check_result(idn_result_t r, const c
Adam Tkac 7c0542
 }
Adam Tkac 7c0542
 #endif /* WITH_IDN */
Adam Tkac 7c0542
 #ifdef WITH_LIBIDN
Adam Tkac 7c0542
-/* If stringprep_locale_to_utf8 fails simple copy string */
Adam Tkac 7c0542
 static isc_result_t
Adam Tkac 7c0542
-libidn_locale_to_utf8 (const char *from, char **to) {
Adam Tkac 7c0542
+libidn_locale_to_utf8 (const char *from, char *to) {
Adam Tkac 7c0542
 	char *utf8_str;
Adam Tkac 7c0542
 
Adam Tkac 7c0542
+	debug ("libidn_locale_to_utf8");
Adam Tkac 7c0542
 	utf8_str = stringprep_locale_to_utf8 (from);
Adam Tkac 7c0542
-	if (utf8_str == NULL) {
Adam Tkac 7c0542
-		*to = isc_mem_allocate (mctx, strlen (from) + 1);
Adam Tkac 7c0542
-		if (*to == NULL)
Adam Tkac 7c0542
-			return (ISC_R_NOMEMORY);
Adam Tkac 7c0542
-		(void) strcpy (*to, from);
Adam Tkac 7c0542
-	} else {
Adam Tkac 7c0542
-		*to = isc_mem_allocate (mctx, strlen (utf8_str) + 1);
Adam Tkac 7c0542
-		if (*to == NULL)
Adam Tkac 7c0542
-			return (ISC_R_NOMEMORY);
Adam Tkac 7c0542
-		(void) strcpy (*to, utf8_str);
Adam Tkac 7c0542
+	if (utf8_str != NULL) {
Adam Tkac 7c0542
+		(void) strcpy (to, utf8_str);
Adam Tkac 7c0542
 		free (utf8_str);
Adam Tkac 7c0542
+		return ISC_R_SUCCESS;
Adam Tkac 7c0542
 	}
Adam Tkac 7c0542
-	return (ISC_R_SUCCESS);
Adam Tkac 7c0542
+
Adam Tkac 7c0542
+	debug ("libidn_locale_to_utf8: failure");
Adam Tkac 7c0542
+	return ISC_R_FAILURE;
Adam Tkac 7c0542
 }
Adam Tkac 7c0542
 static isc_result_t
Adam Tkac 7c0542
 libidn_utf8_to_ascii (const char *from, char *to) {
Adam Tkac 7c0542
 	char *ascii;
Adam Tkac 7c0542
+	int iresult;
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-	if (idna_to_ascii_8z (from, &ascii, 0) != IDNA_SUCCESS)
Adam Tkac 7c0542
-		return (ISC_R_FAILURE);
Adam Tkac 7c0542
+	debug ("libidn_utf8_to_ascii");
Adam Tkac 7c0542
+	iresult = idna_to_ascii_8z (from, &ascii, 0);
Adam Tkac 7c0542
+	if (iresult != IDNA_SUCCESS) {
Adam Tkac 7c0542
+		debug ("idna_to_ascii_8z: %s", idna_strerror (iresult));
Adam Tkac 7c0542
+		return ISC_R_FAILURE;
Adam Tkac 7c0542
+	}
Adam Tkac 7c0542
 
Adam Tkac 7c0542
 	(void) strcpy (to, ascii);
Adam Tkac 7c0542
 	free (ascii);
Adam Tkac 7c0542
-	return (ISC_R_SUCCESS);
Adam Tkac 7c0542
+	return ISC_R_SUCCESS;
Adam Tkac 7c0542
 }
Adam Tkac 7c0542
-/* based on idnkit's code*/
Adam Tkac 7c0542
+
Adam Tkac 7c0542
 static isc_result_t
Adam Tkac 7c0542
 output_filter (isc_buffer_t *buffer, unsigned int used_org,
Adam Tkac 7c0542
 	       isc_boolean_t absolute) {
Adam Tkac 7c0542
+
Adam Tkac 7c0542
 	char tmp1[MXNAME], *tmp2;
Adam Tkac 7c0542
         size_t fromlen, tolen;
Adam Tkac 7c0542
         isc_boolean_t end_with_dot;
Adam Tkac 7c0542
+	int iresult;
Adam Tkac 7c0542
+
Adam Tkac 7c0542
+	debug ("output_filter");
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-        fromlen = isc_buffer_usedlength(buffer) - used_org;
Adam Tkac 7c0542
+        fromlen = isc_buffer_usedlength (buffer) - used_org;
Adam Tkac 7c0542
 	if (fromlen >= MXNAME)
Adam Tkac 7c0542
-		return (ISC_R_SUCCESS);
Adam Tkac 7c0542
-        memcpy(tmp1, (char *)isc_buffer_base(buffer) + used_org, fromlen);
Adam Tkac 7c0542
+		return ISC_R_SUCCESS;
Adam Tkac 7c0542
+        memcpy (tmp1, (char *) isc_buffer_base (buffer) + used_org, fromlen);
Adam Tkac 7c0542
         end_with_dot = (tmp1[fromlen - 1] == '.') ? ISC_TRUE : ISC_FALSE;
Adam Tkac 7c0542
         if (absolute && !end_with_dot) {
Adam Tkac 7c0542
                 fromlen++;
Adam Tkac 7c0542
 		if (fromlen >= MXNAME)
Adam Tkac 7c0542
-			return (ISC_R_SUCCESS);
Adam Tkac 7c0542
+			return ISC_R_SUCCESS;
Adam Tkac 7c0542
                 tmp1[fromlen - 1] = '.';
Adam Tkac 7c0542
         }
Adam Tkac 7c0542
         tmp1[fromlen] = '\0';
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-	if (idna_to_unicode_lzlz (tmp1, &tmp2, 0) != IDNA_SUCCESS)
Adam Tkac 7c0542
-		return (ISC_R_SUCCESS);
Adam Tkac 7c0542
+	iresult = idna_to_unicode_8z8z (tmp1, &tmp2, 0);
Adam Tkac 7c0542
+	if (iresult != IDNA_SUCCESS) {
Adam Tkac 7c0542
+		debug ("output_filter: %s", idna_strerror (iresult));
Adam Tkac 7c0542
+		return ISC_R_SUCCESS;
Adam Tkac 7c0542
+	}
Adam Tkac 7c0542
 
Adam Tkac 7c0542
 	(void) strcpy (tmp1, tmp2);
Adam Tkac 7c0542
 	free (tmp2);
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-        tolen = strlen(tmp1);
Adam Tkac 7c0542
+        tolen = strlen (tmp1);
Adam Tkac 7c0542
         if (absolute && !end_with_dot && tmp1[tolen - 1] == '.')
Adam Tkac 7c0542
                 tolen--;
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-        if (isc_buffer_length(buffer) < used_org + tolen)
Adam Tkac 7c0542
-                return (ISC_R_NOSPACE);
Adam Tkac 7c0542
+        if (isc_buffer_length (buffer) < used_org + tolen)
Adam Tkac 7c0542
+                return ISC_R_NOSPACE;
Adam Tkac 7c0542
+
Adam Tkac 7c0542
+	debug ("%s", tmp1);
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-        isc_buffer_subtract(buffer, isc_buffer_usedlength(buffer) - used_org);
Adam Tkac 7c0542
-        memcpy(isc_buffer_used(buffer), tmp1, tolen);
Adam Tkac 7c0542
-        isc_buffer_add(buffer, tolen);
Adam Tkac 7c0542
+        isc_buffer_subtract (buffer, isc_buffer_usedlength (buffer) - used_org);
Adam Tkac 7c0542
+        memcpy (isc_buffer_used (buffer), tmp1, tolen);
Adam Tkac 7c0542
+        isc_buffer_add (buffer, tolen);
Adam Tkac 7c0542
 
Adam Tkac 7c0542
-        return (ISC_R_SUCCESS);
Adam Tkac 7c0542
+        return ISC_R_SUCCESS;
Adam Tkac 7c0542
 }
Adam Tkac 7c0542
 #endif /* WITH_LIBIDN*/
Adam Tkac 7c0542