5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *rcsid = "$Id: converter.c,v 1.1 2003/06/04 00:25:51 marka Exp $";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (c) 2000,2002 Japan Network Information Center.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * All rights reserved.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * By using this file, you agree to the terms and conditions set forth bellow.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * LICENSE TERMS AND CONDITIONS
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 * 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 * 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 * 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 * 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 * 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#define IDN_UTF8_ENCODING_NAME "UTF-8" /* by IANA */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#else /* WIN32 */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define IDN_ALIAS_FILE IDN_RESCONF_DIR "/idnalias.conf"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* WIN32 */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewstypedef struct {
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 converter_none_open(idn_converter_t ctx, void **privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_close(idn_converter_t ctx, void *privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_none_convfromucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_openfromucs4(idn_converter_t ctx, void **privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_opentoucs4(idn_converter_t ctx, void **privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_close(idn_converter_t ctx, void *privdata);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_convfromucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_iconv_convtoucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char * get_system_aliasfile(void);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* !WITHOUT_ICONV */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_uescape_convfromucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews converter_uescape_convtoucs4(idn_converter_t ctx,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* DEBUG */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Initialize.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((r = idn__strhash_create(&hash)) != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((r = idn__aliaslist_create(&list)) != idn_success)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_initialize(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews static char alias_path[500]; /* a good longer than MAX_PATH */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (idn__util_getregistrystring(idn__util_hkey_localmachine,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_create(const char *name, idn_converter_t *ctxp, int flags) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_create: realname=%s\n", realname));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Allocate memory for a converter context and the name. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx = malloc(sizeof(struct idn_converter) + strlen(realname) + 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)strcpy(ctx->local_encoding_name, realname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcmp(realname, IDN_UTF8_ENCODING_NAME) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* No conversion needed */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if ((r = idn__strhash_get(encoding_name_hash, realname, &v))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Special converter found */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* General case */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((flags & IDN_CONVERTER_DELAYEDOPEN) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (ctx->ops->openfromucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_create(): open failed "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "(ucs4->local)\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->opentoucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_create(): open failed "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "(local->ucs4)\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_create(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_destroy(ctx=%s)\n", ctx->local_encoding_name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_destroy(): the object is destroyed\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)(*ctx->ops->close)(ctx, ctx->private_data);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "update reference count (%d->%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ctx->reference_count + 1, ctx->reference_count));
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 Andrewsidn_converter_localencoding(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_localencoding(ctx=%s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_encodingtype(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_encodingtype(): %d\n", encoding_type));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_isasciicompatible(idn_converter_t ctx) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_isasciicompatible(ctx=%s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews iscompat = (ctx->ops->encoding_type != IDN_NONACE);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_isasciicompatible(): %d\n", iscompat));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_convfromucs4(idn_converter_t ctx, const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
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 r = (*ctx->ops->openfromucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->convfromucs4)(ctx, ctx->private_data, from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if ((ctx->flags & IDN_CONVERTER_RTCHECK) != 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convfromucs4(): success (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_convtoucs4(idn_converter_t ctx, const char *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
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 r = (*ctx->ops->opentoucs4)(ctx, &(ctx->private_data));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = (*ctx->ops->convtoucs4)(ctx, ctx->private_data, from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_convtoucs4(): success (to=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Encoding registration.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(name != NULL && convfromucs4 != NULL && convtoucs4 != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_register(name=%s)\n", name));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__strhash_put(encoding_name_hash, name, ops);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_register(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_register(IDN_PUNYCODE_ENCODING_NAME,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_register(IDN_AMCACEZ_ENCODING_NAME,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_register(IDN_RACE_ENCODING_NAME,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* IDN_EXTRA_ACE */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* This is convenient for debug. Not useful for other purposes. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* DEBUG */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Encoding alias support.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_converter_addalias(const char *alias_name, const char *real_name,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(alias_name != NULL && real_name != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_addalias(alias_name=%s,real_name=%s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strlen(alias_name) == 0 || strlen(real_name) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_addalias(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__aliaslist_additem(encoding_alias_list, alias_name, real_name,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_addalias(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_aliasfile(path=%s)\n", path));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_aliasfile(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__aliaslist_aliasfile(encoding_alias_list, path);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_aliasfile(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_resetalias(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_converter_resetalias(): %s\n", idn_result_tostring(r)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("idn_converter_getrealname(): the module is not "
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews "initialized\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn__aliaslist_find(encoding_alias_list, name, &realname);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Round trip check.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsroundtrip_check(idn_converter_t ctx, const unsigned long *from, const char *to)
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 TRACE(("idn_converter_convert: round-trip checking (from=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Allocate enough buffer. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (fromlen * sizeof(*back) <= sizeof(backbuf)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews back = (unsigned long *)malloc(backlen * sizeof(*back));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Perform backward conversion.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_converter_convtoucs4(ctx, to, back, backlen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (r) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (memcmp(back, from, sizeof(*from) * fromlen) != 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Identity conversion (or, no conversion at all).
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_none_open(idn_converter_t ctx, void **privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_none_close(idn_converter_t ctx, void *privdata) {
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 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 * Conversion using iconv() interface.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_openfromucs4(idn_converter_t ctx, void **privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ictxp = iconv_open(ctx->local_encoding_name, IDN_UTF8_ENCODING_NAME);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv_open failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_opentoucs4(idn_converter_t ctx, void **privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *ictxp = iconv_open(IDN_UTF8_ENCODING_NAME, ctx->local_encoding_name);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv_open failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_close(idn_converter_t ctx, void *privdata) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_convfromucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(ctx != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews r = idn_buffer_overflow; /* need space for NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * UCS4 -> UTF-8 conversion.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (r != idn_success) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Reset internal state.
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 * (void)iconv(ictx, (const char **)NULL, (size_t *)NULL,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * (char **)NULL, (size_t *)NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)iconv(ictx, (const char **)NULL, &inleft, &outbuf, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outleft = tolen - 1; /* reserve space for terminating NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sz = iconv(ictx, (const char **)&inbuf, &inleft, &to, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * We already checked the validity of the input
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * string. So we assume a mapping error.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * For UTF-8 -> local conversion, append a sequence of
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * state reset.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sz = iconv(ictx, (const char **)NULL, &inleft, &to, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews WARNING(("iconv failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_iconv_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 r = idn_buffer_overflow; /* need space for NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Reset internal state.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (void)iconv(ictx, (const char **)NULL, &inleft, &outbuf, &outleft);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews outleft = utf8size - 1; /* reserve space for terminating NUL */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews sz = iconv(ictx, (const char **)&from_ptr, &inleft, &outbuf, &outleft);
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 WARNING(("iconv failed with errno %d\n", errno));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * UTF-8 -> UCS4 conversion.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif /* !WITHOUT_ICONV */
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 Andrewsconverter_uescape_convfromucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (v <= 0x7f) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v <= 0xffffffff) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsconverter_uescape_convtoucs4(idn_converter_t ctx, void *privdata,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *from, unsigned long *to, size_t tolen)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int c = *(unsigned char *)from;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (c < 0x80)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xc0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xe0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xf0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xf8)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xfc)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews else if (c < 0xfe)