5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef lint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *rcsid = "$Id: converter.c,v 1.1 2003/06/04 00:25:51 marka Exp $";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (c) 2000,2002 Japan Network Information Center.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * All rights reserved.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * By using this file, you agree to the terms and conditions set forth bellow.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LICENSE TERMS AND CONDITIONS
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * The following License Terms and Conditions apply, unless a different
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * license is obtained from Japan Network Information Center ("JPNIC"),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Chiyoda-ku, Tokyo 101-0047, Japan.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 1. Use, Modification and Redistribution (including distribution of any
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * modified or derived work) in source and/or binary forms is permitted
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * under this License Terms and Conditions.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 2. Redistribution of source code must retain the copyright notices as they
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * appear in each source code file, this License Terms and Conditions.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 3. Redistribution in binary form must reproduce the Copyright Notice,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * this License Terms and Conditions, in the documentation and/or other
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * materials provided with the distribution. For the purposes of binary
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * distribution the "Copyright Notice" refers to the following language:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * "Copyright (c) 2000-2002 Japan Network Information Center. All rights reserved."
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 4. The name of JPNIC may not be used to endorse or promote products
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * derived from this Software without specific prior written approval of
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * JPNIC.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <config.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <stddef.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <stdlib.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <stdio.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <string.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <ctype.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <errno.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <iconv.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/result.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/assert.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/logmacro.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/converter.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/aliaslist.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/strhash.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/debug.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/ucs4.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/punycode.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/race.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/util.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef IDN_UTF8_ENCODING_NAME
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_UTF8_ENCODING_NAME "UTF-8" /* by IANA */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef IDN_RACE_ENCODING_NAME
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_RACE_ENCODING_NAME "RACE"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef IDN_AMCACEZ_ENCODING_NAME
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_AMCACEZ_ENCODING_NAME "AMC-ACE-Z"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef IDN_PUNYCODE_ENCODING_NAME
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_PUNYCODE_ENCODING_NAME "Punycode"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define MAX_RECURSE 20
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef WIN32
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDNKEY_IDNKIT "Software\\JPNIC\\IDN"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDNVAL_ALIASFILE "AliasFile"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#else /* WIN32 */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef IDN_RESCONF_DIR
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_RESCONF_DIR "/etc"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_ALIAS_FILE IDN_RESCONF_DIR "/idnalias.conf"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* WIN32 */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewstypedef struct {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_openproc_t openfromucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_openproc_t opentoucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_convfromucs4proc_t convfromucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_convtoucs4proc_t convtoucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_closeproc_t close;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int encoding_type;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews} converter_ops_t;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstruct idn_converter {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *local_encoding_name;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_ops_t *ops;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int flags;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int opened_convfromucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int opened_convtoucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int reference_count;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *private_data;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn__strhash_t encoding_name_hash;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn__aliaslist_t encoding_alias_list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t register_standard_encoding(void);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t roundtrip_check(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *to);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_open(idn_converter_t ctx, void **privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_close(idn_converter_t ctx, void *privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_convfromucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *to, size_t tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_convtoucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *privdata, const char *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to, size_t tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_openfromucs4(idn_converter_t ctx, void **privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_opentoucs4(idn_converter_t ctx, void **privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_close(idn_converter_t ctx, void *privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_convfromucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *to, size_t tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_convtoucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to, size_t tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsiconv_initialize_privdata(void **privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic void
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsiconv_finalize_privdata(void *privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char * get_system_aliasfile(void);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int file_exist(const char *filename);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* !WITHOUT_ICONV */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef DEBUG
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_uescape_convfromucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *to, size_t tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_uescape_convtoucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* DEBUG */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic converter_ops_t none_converter_ops = {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_open,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_open,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_convfromucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_convtoucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_close,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews IDN_NONACE,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic converter_ops_t iconv_converter_ops = {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_openfromucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_opentoucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_convfromucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_convtoucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_close,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews IDN_NONACE,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Initialize.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_initialize(void) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__strhash_t hash;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__aliaslist_t list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *fname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_initialize()\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (encoding_name_hash == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((r = idn__strhash_create(&hash)) != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews encoding_name_hash = hash;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = register_standard_encoding();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (encoding_alias_list == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((r = idn__aliaslist_create(&list)) != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews encoding_alias_list = list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fname = get_system_aliasfile();
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (fname != NULL && file_exist(fname))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_aliasfile(fname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_initialize(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsget_system_aliasfile() {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef WIN32
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews static char alias_path[500]; /* a good longer than MAX_PATH */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (idn__util_getregistrystring(idn__util_hkey_localmachine,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews IDNVAL_ALIASFILE,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews alias_path, sizeof(alias_path))) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (alias_path);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (IDN_ALIAS_FILE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic int
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsfile_exist(const char *filename) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews FILE *fp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((fp = fopen(filename, "r")) == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fclose(fp);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_create(const char *name, idn_converter_t *ctxp, int flags) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *realname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_t ctx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews void *v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(name != NULL && ctxp != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_create(%s)\n", name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews realname = idn_converter_getrealname(name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef DEBUG
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcmp(name, realname) != 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_create: realname=%s\n", realname));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ctxp = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Allocate memory for a converter context and the name. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx = malloc(sizeof(struct idn_converter) + strlen(realname) + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ctx == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomemory;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->local_encoding_name = (char *)(ctx + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)strcpy(ctx->local_encoding_name, realname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->flags = flags;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->reference_count = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->opened_convfromucs4 = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->opened_convtoucs4 = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->private_data = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(encoding_name_hash != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcmp(realname, IDN_UTF8_ENCODING_NAME) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* No conversion needed */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->ops = &none_converter_ops;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if ((r = idn__strhash_get(encoding_name_hash, realname, &v))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews == idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Special converter found */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->ops = (converter_ops_t *)v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* General case */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(ctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ctxp = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_name;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->ops = &iconv_converter_ops;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & IDN_CONVERTER_DELAYEDOPEN) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (ctx->ops->openfromucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_create(): open failed "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "(ucs4->local)\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(ctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ctxp = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->opened_convfromucs4 = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->opentoucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_create(): open failed "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "(local->ucs4)\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(ctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ctxp = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->opened_convtoucs4 = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ctxp = ctx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_create(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_destroy(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_destroy(ctx=%s)\n", ctx->local_encoding_name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->reference_count--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (ctx->reference_count <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_destroy(): the object is destroyed\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)(*ctx->ops->close)(ctx, ctx->private_data);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(ctx);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_destroy(): "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "update reference count (%d->%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->reference_count + 1, ctx->reference_count));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_incrref(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_incrref(ctx=%s)\n", ctx->local_encoding_name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_incrref: update reference count (%d->%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->reference_count, ctx->reference_count + 1));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->reference_count++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewschar *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_localencoding(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_localencoding(ctx=%s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->local_encoding_name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (ctx->local_encoding_name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_encodingtype(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int encoding_type;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_encodingtype(ctx=%s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->local_encoding_name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews encoding_type = ctx->ops->encoding_type;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_encodingtype(): %d\n", encoding_type));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (encoding_type);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_isasciicompatible(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int iscompat;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_isasciicompatible(ctx=%s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->local_encoding_name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iscompat = (ctx->ops->encoding_type != IDN_NONACE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_isasciicompatible(): %d\n", iscompat));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (iscompat);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_convfromucs4(idn_converter_t ctx, const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convfromucs4(ctx=%s, from=\"%s\", tolen=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->local_encoding_name, idn__debug_ucs4xstring(from, 50),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (int)tolen));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (!ctx->opened_convfromucs4) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->openfromucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->opened_convfromucs4 = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->convfromucs4)(ctx, ctx->private_data, from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((ctx->flags & IDN_CONVERTER_RTCHECK) != 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = roundtrip_check(ctx, from, to);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r == idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convfromucs4(): success (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_xstring(to, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convfromucs4(): %s\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_convtoucs4(idn_converter_t ctx, const char *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convtoucs4(ctx=%s, from=\"%s\", tolen=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->local_encoding_name, idn__debug_xstring(from, 50),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (int)tolen));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (!ctx->opened_convtoucs4) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->opentoucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->opened_convtoucs4 = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->convtoucs4)(ctx, ctx->private_data, from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r == idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convtoucs4(): success (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_ucs4xstring(to, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convtoucs4(): %s\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Encoding registration.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_register(const char *name,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_openproc_t openfromucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_openproc_t opentoucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_convfromucs4proc_t convfromucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_convtoucs4proc_t convtoucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_converter_closeproc_t close,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int encoding_type) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_ops_t *ops;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(name != NULL && convfromucs4 != NULL && convtoucs4 != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_register(name=%s)\n", name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((ops = malloc(sizeof(*ops))) == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomemory;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (openfromucs4 == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews openfromucs4 = converter_none_open;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (opentoucs4 == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews opentoucs4 = converter_none_open;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (close == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews close = converter_none_close;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ops->openfromucs4 = openfromucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ops->opentoucs4 = opentoucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ops->convfromucs4 = convfromucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ops->convtoucs4 = convtoucs4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ops->close = close;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ops->encoding_type = encoding_type;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__strhash_put(encoding_name_hash, name, ops);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(ops);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_register(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsregister_standard_encoding(void) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_register(IDN_PUNYCODE_ENCODING_NAME,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__punycode_encode,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__punycode_decode,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_close,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews IDN_ACE_STRICTCASE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef IDN_EXTRA_ACE
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_register(IDN_AMCACEZ_ENCODING_NAME,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__punycode_encode,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__punycode_decode,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_close,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews IDN_ACE_STRICTCASE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_register(IDN_RACE_ENCODING_NAME,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__race_encode,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__race_decode,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_close,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews IDN_ACE_LOOSECASE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* IDN_EXTRA_ACE */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef DEBUG
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* This is convenient for debug. Not useful for other purposes. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_register("U-escape",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_uescape_convfromucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_uescape_convtoucs4,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews IDN_NONACE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* DEBUG */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Encoding alias support.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_addalias(const char *alias_name, const char *real_name,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int first_item) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(alias_name != NULL && real_name != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_addalias(alias_name=%s,real_name=%s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews alias_name, real_name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strlen(alias_name) == 0 || strlen(real_name) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_invalid_syntax;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcmp(alias_name, real_name) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (encoding_alias_list == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_addalias(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__aliaslist_additem(encoding_alias_list, alias_name, real_name,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews first_item);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_addalias(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_aliasfile(const char *path) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(path != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_aliasfile(path=%s)\n", path));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (encoding_alias_list == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_aliasfile(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_failure);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__aliaslist_aliasfile(encoding_alias_list, path);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_aliasfile(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_resetalias(void) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__aliaslist_t list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_resetalias()\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (encoding_alias_list == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_resetalias(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_failure);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list = encoding_alias_list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews encoding_alias_list = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__aliaslist_destroy(list);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews list = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__aliaslist_create(&list);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews encoding_alias_list = list;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_resetalias(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconst char *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_getrealname(const char *name) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *realname;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_getrealname()\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(name != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (encoding_alias_list == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_getrealname(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__aliaslist_find(encoding_alias_list, name, &realname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (realname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Round trip check.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsroundtrip_check(idn_converter_t ctx, const unsigned long *from, const char *to)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews{
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * One problem with iconv() convertion is that
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * iconv() doesn't signal an error if the input
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * string contains characters which are valid but
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * do not have mapping to the output codeset.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * (the behavior of iconv() for that case is defined as
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * `implementation dependent')
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * One way to check this case is to perform round-trip
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * conversion and see if it is same as the original string.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *back;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long backbuf[256];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t fromlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t backlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convert: round-trip checking (from=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_ucs4xstring(from, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Allocate enough buffer. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fromlen = idn_ucs4_strlen(from) + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (fromlen * sizeof(*back) <= sizeof(backbuf)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews backlen = sizeof(backbuf);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews back = backbuf;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews backlen = fromlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews back = (unsigned long *)malloc(backlen * sizeof(*back));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (back == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_nomemory);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Perform backward conversion.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_convtoucs4(ctx, to, back, backlen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (r) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case idn_success:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (memcmp(back, from, sizeof(*from) * fromlen) != 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomapping;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case idn_invalid_encoding:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case idn_buffer_overflow:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomapping;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (back != backbuf)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(back);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("round-trip check failed: %s\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Identity conversion (or, no conversion at all).
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_none_open(idn_converter_t ctx, void **privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_none_close(idn_converter_t ctx, void *privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_none_convfromucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from, char *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_ucs4_ucs4toutf8(from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_none_convtoucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from, unsigned long *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_ucs4_utf8toucs4(from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef WITHOUT_ICONV
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Conversion using iconv() interface.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_openfromucs4(idn_converter_t ctx, void **privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_t *ictxp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = iconv_initialize_privdata(privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ictxp = (iconv_t *)*privdata;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ictxp = iconv_open(ctx->local_encoding_name, IDN_UTF8_ENCODING_NAME);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*ictxp == (iconv_t)(-1)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(*privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *privdata = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (errno) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case ENOMEM:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_nomemory);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EINVAL:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_invalid_name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv_open failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_failure);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_opentoucs4(idn_converter_t ctx, void **privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_t *ictxp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = iconv_initialize_privdata(privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ictxp = (iconv_t *)*privdata + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ictxp = iconv_open(IDN_UTF8_ENCODING_NAME, ctx->local_encoding_name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*ictxp == (iconv_t)(-1)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(*privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *privdata = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (errno) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case ENOMEM:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_nomemory);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EINVAL:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_invalid_name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv_open failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_failure);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsiconv_initialize_privdata(void **privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*privdata == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *privdata = malloc(sizeof(iconv_t) * 2);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*privdata == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_nomemory);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *((iconv_t *)*privdata) = (iconv_t)(-1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *((iconv_t *)*privdata + 1) = (iconv_t)(-1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic void
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsiconv_finalize_privdata(void *privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_t *ictxp;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (privdata != NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ictxp = (iconv_t *)privdata;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*ictxp != (iconv_t)(-1))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_close(*ictxp);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ictxp++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*ictxp != (iconv_t)(-1))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_close(*ictxp);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_close(idn_converter_t ctx, void *privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_finalize_privdata(privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_convfromucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from, char *to,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_t ictx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *utf8 = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t utf8size = 256; /* large enough */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t sz;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t inleft;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t outleft;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *inbuf, *outbuf;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow; /* need space for NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * UCS4 -> UTF-8 conversion.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews utf8 = (char *)malloc(utf8size);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (utf8 == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomemory;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewstry_again:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_ucs4toutf8(from, utf8, utf8size);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r == idn_buffer_overflow) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *new_utf8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews utf8size *= 2;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews new_utf8 = (char *)realloc(utf8, utf8size);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (new_utf8 == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomemory;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews utf8 = new_utf8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto try_again;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ictx = ((iconv_t *)privdata)[0];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Reset internal state.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * The following code should work according to the SUSv2 spec,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * but causes segmentation fault with Solaris 2.6.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * So.. a work-around.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * (void)iconv(ictx, (const char **)NULL, (size_t *)NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * (char **)NULL, (size_t *)NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews inleft = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outbuf = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outleft = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)iconv(ictx, (const char **)NULL, &inleft, &outbuf, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews inleft = strlen(utf8);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews inbuf = utf8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outleft = tolen - 1; /* reserve space for terminating NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sz = iconv(ictx, (const char **)&inbuf, &inleft, &to, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sz == (size_t)(-1) || inleft > 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (errno) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EILSEQ:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EINVAL:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * We already checked the validity of the input
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * string. So we assume a mapping error.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomapping;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case E2BIG:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * For UTF-8 -> local conversion, append a sequence of
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * state reset.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews inleft = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sz = iconv(ictx, (const char **)NULL, &inleft, &to, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sz == (size_t)(-1)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (errno) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EILSEQ:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EINVAL:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_encoding;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case E2BIG:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_success;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(utf8);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_convtoucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from, unsigned long *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iconv_t ictx;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *utf8 = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t utf8size = 256; /* large enough */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t sz;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t inleft;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t outleft;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from_ptr;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *outbuf;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow; /* need space for NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ictx = ((iconv_t *)privdata)[1];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews utf8 = (char *)malloc(utf8size);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (utf8 == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomemory;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewstry_again:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Reset internal state.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews inleft = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outbuf = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outleft = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)iconv(ictx, (const char **)NULL, &inleft, &outbuf, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews from_ptr = from;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews inleft = strlen(from);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outbuf = utf8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outleft = utf8size - 1; /* reserve space for terminating NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sz = iconv(ictx, (const char **)&from_ptr, &inleft, &outbuf, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (sz == (size_t)(-1) || inleft > 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *new_utf8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (errno) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EILSEQ:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case EINVAL:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * We assume all the characters in the local
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * codeset are included in UCS. This means mapping
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * error is not possible, so the input string must
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * have some problem.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_encoding;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case E2BIG:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews utf8size *= 2;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews new_utf8 = (char *)realloc(utf8, utf8size);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (new_utf8 == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_nomemory;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews utf8 = new_utf8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto try_again;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto ret;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *outbuf = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * UTF-8 -> UCS4 conversion.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4(utf8, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsret:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews free(utf8);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* !WITHOUT_ICONV */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifdef DEBUG
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Conversion to/from unicode escape string.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Arbitrary UCS-4 character can be specified by a special sequence
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * \u{XXXXXX}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * where XXXXX denotes any hexadecimal string up to FFFFFFFF.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * This is designed for debugging.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_uescape_convfromucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from, char *to,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (*from != '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = *from++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (v <= 0x7f) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen < 1) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to++ = v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v <= 0xffffffff) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char tmp[20];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int len;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)sprintf(tmp, "\\u{%lx}", v);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews len = strlen(tmp);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen < len) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)memcpy(to, tmp, len);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to += len;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen -= len;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_encoding;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsfailure:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_buffer_overflow) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_uescape_convfromucs4(): %s\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_uescape_convtoucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from, unsigned long *to, size_t tolen)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews{
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_t r;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t fromlen = strlen(from);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (*from != '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strncmp(from, "\\u{", 3) == 0 ||
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews strncmp(from, "\\U{", 3) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t ullen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *end;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = strtoul(from + 3, &end, 16);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ullen = end - (from + 3);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*end == '}' && ullen > 1 && ullen < 8) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews from = end + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fromlen -= ullen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = '\\';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews from++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fromlen--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int c = *(unsigned char *)from;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t width;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char buf[8];
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (c < 0x80)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xc0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xe0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 2;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xf0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 3;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xf8)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xfc)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 5;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xfe)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 6;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (width == 0 || width > fromlen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_encoding;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews memcpy(buf, from, width);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews buf[width] = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_ucs4_utf8toucs4(buf, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_invalid_encoding;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews from += width;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews fromlen -= width;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen <= 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews goto failure;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsfailure:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (r != idn_buffer_overflow) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_uescape_convtoucs4(): %s\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (r);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif