1226031Sstas/*
2226031Sstas * Copyright (c) 2004 Kungliga Tekniska H��gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Redistribution and use in source and binary forms, with or without
7226031Sstas * modification, are permitted provided that the following conditions
8226031Sstas * are met:
9226031Sstas *
10226031Sstas * 1. Redistributions of source code must retain the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer.
12226031Sstas *
13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
14226031Sstas *    notice, this list of conditions and the following disclaimer in the
15226031Sstas *    documentation and/or other materials provided with the distribution.
16226031Sstas *
17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas */
33226031Sstas
34226031Sstas#include "windlocl.h"
35226031Sstas
36226031Sstas#include <stdlib.h>
37226031Sstas
38226031Sstas#include "bidi_table.h"
39226031Sstas
40226031Sstasstatic int
41226031Sstasrange_entry_cmp(const void *a, const void *b)
42226031Sstas{
43226031Sstas    const struct range_entry *ea = (const struct range_entry*)a;
44226031Sstas    const struct range_entry *eb = (const struct range_entry*)b;
45226031Sstas
46226031Sstas    if (ea->start >= eb->start && ea->start < eb->start + eb->len)
47226031Sstas	return 0;
48226031Sstas    return ea->start - eb->start;
49226031Sstas}
50226031Sstas
51226031Sstasstatic int
52226031Sstasis_ral(uint32_t cp)
53226031Sstas{
54226031Sstas    struct range_entry ee = {cp};
55226031Sstas    void *s = bsearch(&ee, _wind_ral_table, _wind_ral_table_size,
56226031Sstas		      sizeof(_wind_ral_table[0]),
57226031Sstas		      range_entry_cmp);
58226031Sstas    return s != NULL;
59226031Sstas}
60226031Sstas
61226031Sstasstatic int
62226031Sstasis_l(uint32_t cp)
63226031Sstas{
64226031Sstas    struct range_entry ee = {cp};
65226031Sstas    void *s = bsearch(&ee, _wind_l_table, _wind_l_table_size,
66226031Sstas		      sizeof(_wind_l_table[0]),
67226031Sstas		      range_entry_cmp);
68226031Sstas    return s != NULL;
69226031Sstas}
70226031Sstas
71226031Sstasint
72226031Sstas_wind_stringprep_testbidi(const uint32_t *in, size_t in_len, wind_profile_flags flags)
73226031Sstas{
74226031Sstas    size_t i;
75226031Sstas    unsigned ral = 0;
76226031Sstas    unsigned l   = 0;
77226031Sstas
78226031Sstas    if ((flags & (WIND_PROFILE_NAME|WIND_PROFILE_SASL)) == 0)
79226031Sstas	return 0;
80226031Sstas
81226031Sstas    for (i = 0; i < in_len; ++i) {
82226031Sstas	ral |= is_ral(in[i]);
83226031Sstas	l   |= is_l(in[i]);
84226031Sstas    }
85226031Sstas    if (ral) {
86226031Sstas	if (l)
87226031Sstas	    return 1;
88226031Sstas	if (!is_ral(in[0]) || !is_ral(in[in_len - 1]))
89226031Sstas	    return 1;
90226031Sstas    }
91226031Sstas    return 0;
92226031Sstas}
93