5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef lint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *rcsid = "$Id: utf8.c,v 1.1 2003/06/04 00:26:44 marka Exp $";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (c) 2000 Japan Network Information Center. 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
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/assert.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/logmacro.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/utf8.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/debug.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define UTF8_WIDTH(c) \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews (((c) < 0x80) ? 1 : \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ((c) < 0xc0) ? 0 : \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ((c) < 0xe0) ? 2 : \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ((c) < 0xf0) ? 3 : \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ((c) < 0xf8) ? 4 : \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ((c) < 0xfc) ? 5 : \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews ((c) < 0xfe) ? 6 : 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define VALID_CONT_BYTE(c) (0x80 <= (c) && (c) < 0xc0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_utf8_mblen(const char *s) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int c = *(unsigned char *)s;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(s != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#if 0
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_utf8_mblen(s=<%s>)\n", idn__debug_hexstring(s, 6)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return UTF8_WIDTH(c);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_utf8_getmb(const char *s, size_t len, char *buf) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* buf must be at least 7-bytes long */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned char *p = (const unsigned char *)s;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned char *q = (unsigned char *)buf;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int width = UTF8_WIDTH(*p);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int w;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(s != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#if 0
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_utf8_getmb(s=<%s>,len=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_hexstring(s, 6), len));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (width == 0 || len < width)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Copy the first byte. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *q++ = *p++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* .. and the rest. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews w = width;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (--w > 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (!VALID_CONT_BYTE(*p))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *q++ = *p++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (width);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_utf8_getwc(const char *s, size_t len, unsigned long *vp) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long min;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned char *p = (const unsigned char *)s;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int c;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int width;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int rest;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(s != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#if 0
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_utf8_getwc(s=<%s>,len=%d)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_hexstring(s, 10), len));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews c = *p++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = UTF8_WIDTH(c);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews switch (width) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 0:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 1:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = c;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews min = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 2:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = c & 0x1f;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews min = 0x80;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 3:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = c & 0xf;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews min = 0x800;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 4:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = c & 0x7;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews min = 0x10000;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 5:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = c & 3;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews min = 0x200000;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews case 6:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = c & 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews min = 0x4000000;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews default:
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews FATAL(("idn_utf8_getint: internal error\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (len < width)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews rest = width - 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (rest-- > 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (!VALID_CONT_BYTE(*p))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = (v << 6) | (*p & 0x3f);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews p++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (v < min)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *vp = v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (width);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_utf8_putwc(char *s, size_t len, unsigned long v) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned char *p = (unsigned char *)s;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int mask;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int off;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int l;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(s != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#if 0
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_utf8_putwc(v=%lx)\n", v));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (v < 0x80) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mask = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews l = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v < 0x800) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mask = 0xc0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews l = 2;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v < 0x10000) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mask = 0xe0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews l = 3;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v < 0x200000) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mask = 0xf0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews l = 4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v < 0x4000000) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mask = 0xf8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews l = 5;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v < 0x80000000) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mask = 0xfc;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews l = 6;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (len < l)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews off = 6 * (l - 1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *p++ = (v >> off) | mask;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mask = 0x80;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (off > 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews off -= 6;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *p++ = ((v >> off) & 0x3f) | mask;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return l;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_utf8_isvalidchar(const char *s) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long dummy;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_utf8_isvalidchar(s=<%s>)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_hexstring(s, 6)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_utf8_getwc(s, 6, &dummy) > 0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_utf8_isvalidstring(const char *s) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long dummy;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int width;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(s != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_utf8_isvalidstring(s=<%s>)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_hexstring(s, 20)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (*s != '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews width = idn_utf8_getwc(s, 6, &dummy);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (width == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (0);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews s += width;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (1);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewschar *
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_utf8_findfirstbyte(const char *s, const char *known_top) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned char *p = (const unsigned char *)s;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned char *t = (const unsigned char *)known_top;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(s != NULL && known_top != NULL && known_top <= s);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_utf8_findfirstbyte(s=<%s>)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn__debug_hexstring(s, 8)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (p >= t) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (!VALID_CONT_BYTE(*p))
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews break;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews p--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (p < t || UTF8_WIDTH(*p) == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return ((char *)p);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}