1219820Sjeff/*	$NetBSD$	*/
2219820Sjeff
3219820Sjeff/*
4219820Sjeff * Copyright (c) 2004 Kungliga Tekniska H��gskolan
5219820Sjeff * (Royal Institute of Technology, Stockholm, Sweden).
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff *
12219820Sjeff * 1. Redistributions of source code must retain the above copyright
13219820Sjeff *    notice, this list of conditions and the following disclaimer.
14219820Sjeff *
15219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
16219820Sjeff *    notice, this list of conditions and the following disclaimer in the
17219820Sjeff *    documentation and/or other materials provided with the distribution.
18219820Sjeff *
19219820Sjeff * 3. Neither the name of the Institute nor the names of its contributors
20219820Sjeff *    may be used to endorse or promote products derived from this software
21219820Sjeff *    without specific prior written permission.
22219820Sjeff *
23219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24219820Sjeff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26219820Sjeff * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27219820Sjeff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28219820Sjeff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29219820Sjeff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31219820Sjeff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32219820Sjeff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33219820Sjeff * SUCH DAMAGE.
34219820Sjeff */
35219820Sjeff
36219820Sjeff#include "windlocl.h"
37219820Sjeff
38219820Sjeff#include <stdlib.h>
39219820Sjeff
40219820Sjeff#include "bidi_table.h"
41219820Sjeff
42219820Sjeffstatic int
43219820Sjeffrange_entry_cmp(const void *a, const void *b)
44219820Sjeff{
45219820Sjeff    const struct range_entry *ea = (const struct range_entry*)a;
46219820Sjeff    const struct range_entry *eb = (const struct range_entry*)b;
47219820Sjeff
48219820Sjeff    if (ea->start >= eb->start && ea->start < eb->start + eb->len)
49219820Sjeff	return 0;
50219820Sjeff    return ea->start - eb->start;
51219820Sjeff}
52219820Sjeff
53219820Sjeffstatic int
54219820Sjeffis_ral(uint32_t cp)
55219820Sjeff{
56219820Sjeff    struct range_entry ee = {cp};
57219820Sjeff    void *s = bsearch(&ee, _wind_ral_table, _wind_ral_table_size,
58219820Sjeff		      sizeof(_wind_ral_table[0]),
59219820Sjeff		      range_entry_cmp);
60219820Sjeff    return s != NULL;
61219820Sjeff}
62219820Sjeff
63219820Sjeffstatic int
64219820Sjeffis_l(uint32_t cp)
65219820Sjeff{
66219820Sjeff    struct range_entry ee = {cp};
67219820Sjeff    void *s = bsearch(&ee, _wind_l_table, _wind_l_table_size,
68219820Sjeff		      sizeof(_wind_l_table[0]),
69219820Sjeff		      range_entry_cmp);
70254123Sjeff    return s != NULL;
71254123Sjeff}
72254123Sjeff
73254123Sjeffint
74219820Sjeff_wind_stringprep_testbidi(const uint32_t *in, size_t in_len, wind_profile_flags flags)
75219820Sjeff{
76219820Sjeff    size_t i;
77219820Sjeff    unsigned ral = 0;
78219820Sjeff    unsigned l   = 0;
79219820Sjeff
80219820Sjeff    if ((flags & (WIND_PROFILE_NAME|WIND_PROFILE_SASL)) == 0)
81219820Sjeff	return 0;
82219820Sjeff
83219820Sjeff    for (i = 0; i < in_len; ++i) {
84219820Sjeff	ral |= is_ral(in[i]);
85219820Sjeff	l   |= is_l(in[i]);
86219820Sjeff    }
87219820Sjeff    if (ral) {
88219820Sjeff	if (l)
89219820Sjeff	    return 1;
90219820Sjeff	if (!is_ral(in[0]) || !is_ral(in[in_len - 1]))
91219820Sjeff	    return 1;
92219820Sjeff    }
93219820Sjeff    return 0;
94219820Sjeff}
95219820Sjeff