5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#ifndef lint
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic char *rcsid = "$Id: nameprep.c,v 1.1 2003/06/04 00:25:56 marka Exp $";
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#endif
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Copyright (c) 2001,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 <stdlib.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <string.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/result.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/assert.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/log.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/logmacro.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/debug.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include <idn/nameprep.h>
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define UCS_MAX 0x7fffffff
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define UNICODE_MAX 0x10ffff
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Load NAMEPREP compiled tables.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include "nameprepdata.c"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews/*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Define mapping/checking functions for each version of the draft.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define VERSION rfc3491
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#include "nameprep_template.c"
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#undef VERSION
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewstypedef const char *(*nameprep_mapproc)(unsigned long v);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewstypedef int (*nameprep_checkproc)(unsigned long v);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewstypedef idn_biditype_t (*nameprep_biditypeproc)(unsigned long v);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic struct idn_nameprep {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews char *version;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews nameprep_mapproc map_proc;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews nameprep_checkproc prohibited_proc;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews nameprep_checkproc unassigned_proc;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews nameprep_biditypeproc biditype_proc;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews} nameprep_versions[] = {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews#define MAKE_NAMEPREP_HANDLE(version, id) \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews { version, \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews compose_sym2(nameprep_map_, id), \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews compose_sym2(nameprep_prohibited_, id), \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews compose_sym2(nameprep_unassigned_, id), \
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews compose_sym2(nameprep_biditype_, id), }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews MAKE_NAMEPREP_HANDLE("RFC3491", rfc3491),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews { NULL, NULL, NULL },
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews};
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t idn_nameprep_check(nameprep_checkproc proc,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_create(const char *version, idn_nameprep_t *handlep) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_nameprep_t handle;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(handlep != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_nameprep_create(version=%-.50s)\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews version == NULL ? "<NULL>" : version));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (version == NULL)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews version = IDN_NAMEPREP_CURRENT;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * Lookup table for the specified version. Since the number of
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * versions won't be large (I don't want see draft-23 or such :-),
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * simple linear search is OK.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews for (handle = nameprep_versions; handle->version != NULL; handle++) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (strcmp(handle->version, version) == 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *handlep = handle;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_notfound);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_destroy(idn_nameprep_t handle) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(handle != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_nameprep_destroy()\n"));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Nothing to do. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_map(idn_nameprep_t handle, const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(handle != NULL && from != NULL && to != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_nameprep_map(ctx=%s, from=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews handle->version, idn__debug_ucs4xstring(from, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (*from != '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v = *from;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const char *mapped;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (v > UCS_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* This cannot happen, but just in case.. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_invalid_codepoint);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v > UNICODE_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* No mapping is possible. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mapped = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* Try mapping. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mapped = (*handle->map_proc)(v);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (mapped == NULL) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* No mapping. Just copy verbatim. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen < 1)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_buffer_overflow);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to++ = v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen--;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned char *mappeddata;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews size_t mappedlen;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mappeddata = (const unsigned char *)mapped + 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mappedlen = *mapped;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen < (mappedlen + 3) / 4)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_buffer_overflow);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews tolen -= (mappedlen + 3) / 4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (mappedlen >= 4) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = *mappeddata++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to |= *mappeddata++ << 8;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to |= *mappeddata++ << 16;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to |= *mappeddata++ << 24;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews mappedlen -= 4;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (mappedlen > 0) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = *mappeddata++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to |= (mappedlen >= 2) ?
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *mappeddata++ << 8: 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to |= (mappedlen >= 3) ?
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *mappeddata++ << 16: 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews to++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews from++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (tolen == 0)
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_buffer_overflow);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *to = '\0';
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_isprohibited(idn_nameprep_t handle, const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(handle != NULL && str != NULL && found != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_nameprep_isprohibited(ctx=%s, str=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews handle->version, idn__debug_ucs4xstring(str, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_nameprep_check(handle->prohibited_proc, str, found));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_isunassigned(idn_nameprep_t handle, const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(handle != NULL && str != NULL && found != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_nameprep_isunassigned(handle->version, str=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews handle->version, idn__debug_ucs4xstring(str, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_nameprep_check(handle->unassigned_proc, str, found));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsstatic idn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_check(nameprep_checkproc proc, const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (*str != '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = *str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (v > UCS_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* This cannot happen, but just in case.. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_invalid_codepoint);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v > UNICODE_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* It is invalid.. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if ((*proc)(v)) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews str++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_isvalidbidi(idn_nameprep_t handle, const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long v;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_biditype_t first_char;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_biditype_t last_char;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews int found_r_al;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews assert(handle != NULL && str != NULL && found != NULL);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews TRACE(("idn_nameprep_isvalidbidi(ctx=%s, str=\"%s\")\n",
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews handle->version, idn__debug_ucs4xstring(str, 50)));
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*str == '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * check first character's type and initialize variables.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews found_r_al = 0;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (*str > UCS_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* This cannot happen, but just in case.. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_invalid_codepoint);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (*str > UNICODE_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* It is invalid.. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews first_char = last_char = (*(handle->biditype_proc))(*str);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (first_char == idn_biditype_r_al) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews found_r_al = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews str++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /*
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews * see whether string is valid or not.
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews while (*str != '\0') {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews v = *str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (v > UCS_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* This cannot happen, but just in case.. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_invalid_codepoint);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else if (v > UNICODE_MAX) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews /* It is invalid.. */
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews } else {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews last_char = (*(handle->biditype_proc))(v);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (found_r_al && last_char == idn_biditype_l) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (first_char != idn_biditype_r_al && last_char == idn_biditype_r_al) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = str;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (last_char == idn_biditype_r_al) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews found_r_al = 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews str++;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (found_r_al) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews if (last_char != idn_biditype_r_al) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = str - 1;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews }
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews *found = NULL;
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return (idn_success);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_createproc(const char *parameter, void **handlep) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_nameprep_create(parameter, (idn_nameprep_t *)handlep);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsvoid
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_destroyproc(void *handle) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews idn_nameprep_destroy((idn_nameprep_t)handle);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_mapproc(void *handle, const unsigned long *from,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews unsigned long *to, size_t tolen) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_nameprep_map((idn_nameprep_t)handle, from, to, tolen);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_prohibitproc(void *handle, const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_nameprep_isprohibited((idn_nameprep_t)handle, str, found);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_unassignedproc(void *handle, const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_nameprep_isunassigned((idn_nameprep_t)handle, str, found);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_result_t
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrewsidn_nameprep_bidiproc(void *handle, const unsigned long *str,
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews const unsigned long **found) {
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews return idn_nameprep_isvalidbidi((idn_nameprep_t)handle, str, found);
5c526acb82c882e41b655c31f5fa4425c87b671cMark Andrews}