1#ifndef lint
2static char *rcsid = "$Id: selectiveencode.c,v 1.1 2003/06/04 00:27:07 marka Exp $";
3#endif
4
5/*
6 * Copyright (c) 2000,2002 Japan Network Information Center.
7 * All rights reserved.
8 *
9 * By using this file, you agree to the terms and conditions set forth bellow.
10 *
11 * 			LICENSE TERMS AND CONDITIONS
12 *
13 * The following License Terms and Conditions apply, unless a different
14 * license is obtained from Japan Network Information Center ("JPNIC"),
15 * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
16 * Chiyoda-ku, Tokyo 101-0047, Japan.
17 *
18 * 1. Use, Modification and Redistribution (including distribution of any
19 *    modified or derived work) in source and/or binary forms is permitted
20 *    under this License Terms and Conditions.
21 *
22 * 2. Redistribution of source code must retain the copyright notices as they
23 *    appear in each source code file, this License Terms and Conditions.
24 *
25 * 3. Redistribution in binary form must reproduce the Copyright Notice,
26 *    this License Terms and Conditions, in the documentation and/or other
27 *    materials provided with the distribution.  For the purposes of binary
28 *    distribution the "Copyright Notice" refers to the following language:
29 *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
30 *
31 * 4. The name of JPNIC may not be used to endorse or promote products
32 *    derived from this Software without specific prior written approval of
33 *    JPNIC.
34 *
35 * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
36 *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
38 *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
39 *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
40 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
41 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42 *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
43 *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
44 *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
45 *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
46 */
47
48#include <config.h>
49
50#include <stddef.h>
51
52#include <idn/assert.h>
53#include <idn/logmacro.h>
54#include <idn/result.h>
55#include <idn/utf8.h>
56#include <idn/debug.h>
57
58#include "selectiveencode.h"
59
60static int	is_domain_delimiter(char c);
61static char	*find_nonascii(const char *s);
62
63idn_result_t
64idn_selectiveencode_findregion(const char *s,
65			       char **startp, char **endp)
66{
67	char *non_ascii;
68	char *start, *end;
69
70	assert(s != NULL && startp != NULL && endp != NULL);
71
72	TRACE(("idn_selectiveencode_findregion(s=\"%s\")\n",
73	      idn__debug_xstring(s, 20)));
74
75	/*
76	 * Scan the specified string looking for non-ascii character.
77	 */
78	if ((non_ascii = find_nonascii(s)) == NULL)
79		return (idn_notfound);
80
81	/*
82	 * Non-ascii character found.
83	 * Determine the region to encode.
84	 */
85
86	/*
87	 * First, we scan backwards to find the beginning of the region
88	 * that should be converted.
89	 */
90	start = non_ascii;
91	while (start > s) {
92		char *prev = idn_utf8_findfirstbyte(start - 1, s);
93		if (is_domain_delimiter(*prev))
94			break;			/* Found */
95		start = prev;
96	}
97	*startp = start;
98
99	/*
100	 * Next we scan forwards looking for the end of the region.
101	 */
102	end = non_ascii + idn_utf8_mblen(non_ascii);
103	while (!is_domain_delimiter(*end))
104		end += idn_utf8_mblen(end);
105	*endp = end;
106
107	return (idn_success);
108}
109
110static int
111is_domain_delimiter(char c) {
112	return ((unsigned char)c < 0x80 &&
113		!('A' <= c && c <= 'Z') &&
114		!('a' <= c && c <= 'z') &&
115		!('0' <= c && c <= '9') &&
116		c != '-' && c != '.');
117}
118
119static char *
120find_nonascii(const char *s) {
121	while (*s != '\0' && (unsigned char)*s < 0x80)
122		s++;
123	if (*s == '\0')
124		return (NULL);
125	else
126		return ((char *)s);
127}
128