⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 res.c

📁 package of develop dns
💻 C
📖 第 1 页 / 共 3 页
字号:
#ifndef lintstatic char *rcsid = "$Id: res.c,v 1.1.1.1 2003/06/04 00:26:10 marka Exp $";#endif/* * Copyright (c) 2000,2002 Japan Network Information Center. * All rights reserved. *   * By using this file, you agree to the terms and conditions set forth bellow. *  * 			LICENSE TERMS AND CONDITIONS  *  * The following License Terms and Conditions apply, unless a different * license is obtained from Japan Network Information Center ("JPNIC"), * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda, * Chiyoda-ku, Tokyo 101-0047, Japan. *  * 1. Use, Modification and Redistribution (including distribution of any *    modified or derived work) in source and/or binary forms is permitted *    under this License Terms and Conditions. *  * 2. Redistribution of source code must retain the copyright notices as they *    appear in each source code file, this License Terms and Conditions. *  * 3. Redistribution in binary form must reproduce the Copyright Notice, *    this License Terms and Conditions, in the documentation and/or other *    materials provided with the distribution.  For the purposes of binary *    distribution the "Copyright Notice" refers to the following language: *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved." *  * 4. The name of JPNIC may not be used to endorse or promote products *    derived from this Software without specific prior written approval of *    JPNIC. *  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. */#include <config.h>#include <stddef.h>#include <stdlib.h>#include <string.h>#include <idn/result.h>#include <idn/assert.h>#include <idn/logmacro.h>#include <idn/converter.h>#include <idn/normalizer.h>#include <idn/checker.h>#include <idn/mapper.h>#include <idn/mapselector.h>#include <idn/delimitermap.h>#include <idn/resconf.h>#include <idn/res.h>#include <idn/util.h>#include <idn/debug.h>#include <idn/ucs4.h>#ifndef IDN_UTF8_ENCODING_NAME#define IDN_UTF8_ENCODING_NAME "UTF-8"		/* by IANA */#endif#ifndef WITHOUT_ICONV#define ENCODE_MASK \	(IDN_LOCALCONV | IDN_DELIMMAP | IDN_LOCALMAP | IDN_MAP | \	 IDN_NORMALIZE | IDN_PROHCHECK | IDN_UNASCHECK | IDN_BIDICHECK | \	 IDN_ASCCHECK | IDN_IDNCONV | IDN_LENCHECK | IDN_ENCODE_QUERY | \	 IDN_UNDOIFERR)#define DECODE_MASK \	(IDN_DELIMMAP | IDN_MAP | IDN_NORMALIZE | IDN_PROHCHECK | \	 IDN_UNASCHECK | IDN_BIDICHECK | IDN_IDNCONV | IDN_ASCCHECK | \	 IDN_RTCHECK | IDN_LOCALCONV | IDN_DECODE_QUERY)#else#define ENCODE_MASK \	(IDN_DELIMMAP | IDN_LOCALMAP | IDN_MAP | IDN_NORMALIZE | \	 IDN_PROHCHECK | IDN_UNASCHECK | IDN_BIDICHECK | IDN_ASCCHECK | \	 IDN_IDNCONV | IDN_LENCHECK | IDN_ENCODE_QUERY | IDN_UNDOIFERR)#define DECODE_MASK \	(IDN_DELIMMAP | IDN_MAP | IDN_NORMALIZE | IDN_PROHCHECK | \	 IDN_UNASCHECK | IDN_BIDICHECK | IDN_IDNCONV | IDN_ASCCHECK | \	 IDN_RTCHECK | IDN_DECODE_QUERY)#endif#define MAX_LABEL_LENGTH	63/* * label to convert. */typedef struct labellist * labellist_t;struct labellist {	unsigned long *name;	size_t name_length;	unsigned long *undo_name;	labellist_t next;	labellist_t previous;	int dot_followed;};typedef idn_result_t (*res_insnproc_t)(idn_resconf_t ctx,				       labellist_t label);static void		idn_res_initialize(void);static idn_result_t	copy_verbatim(const char *from, char *to,				      size_t tolen);static idn_result_t	labellist_create(const unsigned long *name,					 labellist_t *labelp);static void		labellist_destroy(labellist_t label);static idn_result_t	labellist_setname(labellist_t label,					  const unsigned long *name);static const unsigned long *			labellist_getname(labellist_t label);static const unsigned long *			labellist_gettldname(labellist_t label);static idn_result_t	labellist_getnamelist(labellist_t label,					      unsigned long *name,					      size_t label_length);static void		labellist_undo(labellist_t label);static labellist_t	labellist_tail(labellist_t label);static labellist_t	labellist_previous(labellist_t label);#ifndef WITHOUT_ICONVstatic idn_result_t	label_localdecodecheck(idn_resconf_t ctx,					       labellist_t label);#endifstatic idn_result_t	label_idnencode_ace(idn_resconf_t ctx,					    labellist_t label);static idn_result_t	label_idndecode(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_localmap(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_map(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_normalize(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_prohcheck(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_unascheck(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_bidicheck(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_asccheck(idn_resconf_t ctx, labellist_t label);static idn_result_t	label_lencheck_ace(idn_resconf_t ctx,					   labellist_t label);static idn_result_t	label_lencheck_nonace(idn_resconf_t ctx,					      labellist_t label);static idn_result_t	label_rtcheck(idn_resconf_t ctx, idn_action_t actions,				      labellist_t label,				      const unsigned long *original_name);static int initialized;static int enabled;voididn_res_enable(int on_off) {	if (!initialized) {		idn_res_initialize();	}	if (on_off == 0) {		enabled = 0;	} else {		enabled = 1;	}}static voididn_res_initialize(void) {	if (!initialized) {		char *value = getenv("IDN_DISABLE");		if (value == NULL) {			enabled = 1;		} else {			enabled = 0;		}		initialized = 1;	}}idn_result_tidn_res_encodename(idn_resconf_t ctx, idn_action_t actions, const char *from,		    char *to, size_t tolen) {	idn_converter_t local_converter = NULL;	idn_converter_t idn_converter = NULL;	idn_delimitermap_t delimiter_mapper;	idn_result_t r;	labellist_t labels = NULL, l;	unsigned long *buffer = NULL;	size_t buffer_length;	int from_is_root;	int idn_is_ace;	assert(ctx != NULL && from != NULL && to != NULL);	TRACE(("idn_res_encodename(actions=%s, from=\"%s\", tolen=%d)\n",		idn__res_actionstostring(actions),		idn__debug_xstring(from, 50), (int)tolen));	if (actions & ~ENCODE_MASK) {		WARNING(("idn_res_encodename: invalid actions 0x%x\n",			 actions));		r = idn_invalid_action;		goto ret;	}	if (!initialized)		idn_res_initialize();	if (!enabled || actions == 0) {		r = copy_verbatim(from, to, tolen);		goto ret;	} else if (tolen <= 0) {		r = idn_buffer_overflow;		goto ret;	}	if (actions & IDN_ENCODE_QUERY) {#ifndef WITHOUT_ICONV		actions |= (IDN_LOCALCONV | IDN_DELIMMAP | IDN_LOCALMAP | \			    IDN_MAP | IDN_NORMALIZE | IDN_PROHCHECK | \			    IDN_BIDICHECK | IDN_IDNCONV | IDN_LENCHECK);#else		actions |= (IDN_DELIMMAP | IDN_LOCALMAP | IDN_MAP | \			    IDN_NORMALIZE | IDN_PROHCHECK | IDN_BIDICHECK | \			    IDN_IDNCONV | IDN_LENCHECK);#endif	}	/*	 * Convert `from' to UCS4.	 */	local_converter = idn_resconf_getlocalconverter(ctx);#ifndef WITHOUT_ICONV	if (local_converter == NULL) {		r = idn_invalid_name;		goto ret;	}#endif	idn_converter = idn_resconf_getidnconverter(ctx);	if (idn_converter != NULL &&	    idn_converter_isasciicompatible(idn_converter))		idn_is_ace = 1;	else		idn_is_ace = 0;	buffer_length = tolen * 2;	for (;;) {		void *new_buffer;		new_buffer = realloc(buffer, sizeof(*buffer) * buffer_length);		if (new_buffer == NULL) {			r = idn_nomemory;			goto ret;		}		buffer = (unsigned long *)new_buffer;		if (actions & IDN_LOCALCONV) {			r = idn_converter_convtoucs4(local_converter, from,						     buffer, buffer_length);		} else {			r = idn_ucs4_utf8toucs4(from, buffer, buffer_length);		}		if (r == idn_success)			break;		else if (r != idn_buffer_overflow)			goto ret;		buffer_length *= 2;	}	if (*buffer == '\0') {		if (tolen <= 0) {			r = idn_buffer_overflow;			goto ret;		}		*to = '\0';		r = idn_success;		goto ret;	}	/*	 * Delimiter map.	 */	if (actions & IDN_DELIMMAP) {		TRACE(("res delimitermap(name=\"%s\")\n",		       idn__debug_ucs4xstring(buffer, 50)));		delimiter_mapper = idn_resconf_getdelimitermap(ctx);		if (delimiter_mapper != NULL) {			r = idn_delimitermap_map(delimiter_mapper, buffer,						 buffer, buffer_length);			idn_delimitermap_destroy(delimiter_mapper);			if (r != idn_success)				goto ret;		}		TRACE(("res delimitermap(): success (name=\"%s\")\n",		       idn__debug_ucs4xstring(buffer, 50)));	}	from_is_root = (buffer[0] == '.' && buffer[1] == '\0');	/*	 * Split the name into a list of labels.	 */	r = labellist_create(buffer, &labels);	if (r != idn_success)		goto ret;	/*	 * Perform conversions and tests.	 */	for (l = labellist_tail(labels); l != NULL;	     l = labellist_previous(l)) {		if (!idn__util_ucs4isasciirange(labellist_getname(l))) {			if (actions & IDN_LOCALMAP) {				r = label_localmap(ctx, l);				if (r != idn_success)					goto ret;			}		}		if (!idn__util_ucs4isasciirange(labellist_getname(l))) {			if (actions & IDN_MAP) {				r = label_map(ctx, l);				if (r != idn_success)					goto ret;			}			if (actions & IDN_NORMALIZE) {				r = label_normalize(ctx, l);				if (r != idn_success)					goto ret;			}			if (actions & IDN_PROHCHECK) {				r = label_prohcheck(ctx, l);				if (r == idn_prohibited && 				    (actions & IDN_UNDOIFERR)) {					labellist_undo(l);					continue;				} else if (r != idn_success) {					goto ret;				}			}			if (actions & IDN_UNASCHECK) {				r = label_unascheck(ctx, l);				if (r == idn_prohibited && 				    (actions & IDN_UNDOIFERR)) {					labellist_undo(l);					continue;				} else if (r != idn_success) {					goto ret;				}			}			if (actions & IDN_BIDICHECK) {				r = label_bidicheck(ctx, l);				if (r == idn_prohibited && 				    (actions & IDN_UNDOIFERR)) {					labellist_undo(l);					continue;				} else if (r != idn_success) {					goto ret;				}			}		}		if (actions & IDN_ASCCHECK) {			r = label_asccheck(ctx, l);			if (r == idn_prohibited && (actions & IDN_UNDOIFERR)) {				labellist_undo(l);				continue;			} else if (r != idn_success) {				goto ret;			}		}		if (!idn__util_ucs4isasciirange(labellist_getname(l))) {			if ((actions & IDN_IDNCONV) && idn_is_ace) {				r = label_idnencode_ace(ctx, l);				if (r != idn_success)					goto ret;			}		}		if (!from_is_root && (actions & IDN_LENCHECK)) {			if (idn_is_ace)				r = label_lencheck_ace(ctx, l);			else				r = label_lencheck_nonace(ctx, l);			if (r == idn_invalid_length &&			    (actions & IDN_UNDOIFERR)) {				labellist_undo(l);				continue;			} else if (r != idn_success) {				goto ret;			}		}	}	/*	 * Concat a list of labels to a name.	 */	for (;;) {		void *new_buffer;		new_buffer = realloc(buffer, sizeof(*buffer) * buffer_length);		if (new_buffer == NULL) {			r = idn_nomemory;			goto ret;		}		buffer = (unsigned long *)new_buffer;		r = labellist_getnamelist(labels, buffer, buffer_length);		if (r == idn_success)			break;		else if (r != idn_buffer_overflow)			goto ret;		buffer_length *= 2;	}	if ((actions & IDN_IDNCONV) && idn_converter != NULL && !idn_is_ace) {		r = idn_converter_convfromucs4(idn_converter, buffer, to,					       tolen);	} else {		r = idn_ucs4_ucs4toutf8(buffer, to, tolen);	}ret:	if (r == idn_success) {		TRACE(("idn_res_encodename(): success (to=\"%s\")\n",		       idn__debug_xstring(to, 50)));	} else {		TRACE(("idn_res_encodename(): %s\n", idn_result_tostring(r)));	}	free(buffer);	if (local_converter != NULL)		idn_converter_destroy(local_converter);	if (idn_converter != NULL)		idn_converter_destroy(idn_converter);	if (labels != NULL)		labellist_destroy(labels);	return (r);}idn_result_tidn_res_decodename(idn_resconf_t ctx, idn_action_t actions, const char *from,		    char *to, size_t tolen) {	idn_converter_t local_converter = NULL;	idn_converter_t idn_converter = NULL;	idn_delimitermap_t delimiter_mapper;	idn_result_t r;	labellist_t labels = NULL, l;	unsigned long *buffer = NULL;	unsigned long *saved_name = NULL;	size_t buffer_length;	int idn_is_ace;	assert(ctx != NULL && from != NULL && to != NULL);	TRACE(("idn_res_decodename(actions=%s, from=\"%s\", tolen=%d)\n",		idn__res_actionstostring(actions),		idn__debug_xstring(from, 50), (int)tolen));	if (actions & ~DECODE_MASK) {		WARNING(("idn_res_decodename: invalid actions 0x%x\n",			 actions));		r = idn_invalid_action;		goto ret;	}	if (!initialized)		idn_res_initialize();	if (!enabled || actions == 0) {		r = copy_verbatim(from, to, tolen);		goto ret;	} else if (tolen <= 0) {		r = idn_buffer_overflow;		goto ret;	}	if (actions & IDN_DECODE_QUERY) {#ifndef WITHOUT_ICONV		actions |= (IDN_DELIMMAP | IDN_MAP | IDN_NORMALIZE | \			    IDN_PROHCHECK | IDN_BIDICHECK | IDN_IDNCONV | \			    IDN_RTCHECK | IDN_LOCALCONV);#else		actions |= (IDN_DELIMMAP | IDN_MAP | IDN_NORMALIZE | \			    IDN_PROHCHECK | IDN_BIDICHECK | IDN_IDNCONV | \			    IDN_RTCHECK);#endif	}	/*	 * Convert `from' to UCS4.	 */	local_converter = idn_resconf_getlocalconverter(ctx);#ifndef WITHOUT_ICONV	if (local_converter == NULL) {		r = idn_invalid_name;		goto ret;	}#endif	idn_converter = idn_resconf_getidnconverter(ctx);	if (idn_converter != NULL &&	    idn_converter_isasciicompatible(idn_converter))		idn_is_ace = 1;	else		idn_is_ace = 0;	buffer_length = tolen * 2;	TRACE(("res idndecode(name=\"%s\")\n", idn__debug_xstring(from, 50)));	for (;;) {		void *new_buffer;		new_buffer = realloc(buffer, sizeof(*buffer) * buffer_length);		if (new_buffer == NULL) {			r = idn_nomemory;			goto ret;		}		buffer = (unsigned long *)new_buffer;		if ((actions & IDN_IDNCONV) &&		     idn_converter != NULL && !idn_is_ace) {			r = idn_converter_convtoucs4(idn_converter, from,						     buffer, buffer_length);		} else {			r = idn_ucs4_utf8toucs4(from, buffer, buffer_length);		}		if (r == idn_success)			break;		else if (r != idn_buffer_overflow)			goto ret;		buffer_length *= 2;	}	if (*buffer == '\0') {		if (tolen <= 0) {			r = idn_buffer_overflow;			goto ret;		}		*to = '\0';		r = idn_success;		goto ret;	}	/*	 * Delimiter map.	 */	if (actions & IDN_DELIMMAP) {		TRACE(("res delimitermap(name=\"%s\")\n",		       idn__debug_ucs4xstring(buffer, 50)));		delimiter_mapper = idn_resconf_getdelimitermap(ctx);		if (delimiter_mapper != NULL) {			r = idn_delimitermap_map(delimiter_mapper, buffer,						 buffer, buffer_length);			idn_delimitermap_destroy(delimiter_mapper);			if (r != idn_success)				goto ret;		}		TRACE(("res delimitermap(): success (name=\"%s\")\n",		       idn__debug_ucs4xstring(buffer, 50)));	}	/*	 * Split the name into a list of labels.	 */	r = labellist_create(buffer, &labels);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -