1 /*	$NetBSD: util.c,v 1.4 2014/12/10 04:37:55 christos Exp $	*/
2 
3 #ifndef lint
4 static char *rcsid = "Id: util.c,v 1.1 2003/06/04 00:26:45 marka Exp ";
5 #endif
6 
7 /*
8  * Copyright (c) 2000,2002 Japan Network Information Center.
9  * All rights reserved.
10  *
11  * By using this file, you agree to the terms and conditions set forth bellow.
12  *
13  * 			LICENSE TERMS AND CONDITIONS
14  *
15  * The following License Terms and Conditions apply, unless a different
16  * license is obtained from Japan Network Information Center ("JPNIC"),
17  * a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
18  * Chiyoda-ku, Tokyo 101-0047, Japan.
19  *
20  * 1. Use, Modification and Redistribution (including distribution of any
21  *    modified or derived work) in source and/or binary forms is permitted
22  *    under this License Terms and Conditions.
23  *
24  * 2. Redistribution of source code must retain the copyright notices as they
25  *    appear in each source code file, this License Terms and Conditions.
26  *
27  * 3. Redistribution in binary form must reproduce the Copyright Notice,
28  *    this License Terms and Conditions, in the documentation and/or other
29  *    materials provided with the distribution.  For the purposes of binary
30  *    distribution the "Copyright Notice" refers to the following language:
31  *    "Copyright (c) 2000-2002 Japan Network Information Center.  All rights reserved."
32  *
33  * 4. The name of JPNIC may not be used to endorse or promote products
34  *    derived from this Software without specific prior written approval of
35  *    JPNIC.
36  *
37  * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
38  *    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  *    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
40  *    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JPNIC BE LIABLE
41  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
44  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
45  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46  *    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
47  *    ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
48  */
49 
50 #include <config.h>
51 
52 #ifdef WIN32
53 #include <windows.h>
54 #undef ERROR
55 #endif
56 
57 #include <stddef.h>
58 
59 #include <idn/assert.h>
60 #include <idn/result.h>
61 #include <idn/logmacro.h>
62 #include <idn/util.h>
63 #include <idn/ucs4.h>
64 
65 #ifdef WIN32
66 #define IDNKEY_IDNKIT		"Software\\JPNIC\\IDN"
67 #endif
68 
69 /*
70  * ASCII ctype macros.
71  * Note that these macros evaluate the argument multiple times.  Be careful.
72  */
73 #define ASCII_ISDIGIT(c) \
74 	('0' <= (c) && (c) <= '9')
75 #define ASCII_ISUPPER(c) \
76 	('A' <= (c) && (c) <= 'Z')
77 #define ASCII_ISLOWER(c) \
78 	('a' <= (c) && (c) <= 'z')
79 #define ASCII_ISALPHA(c) \
80 	(ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
81 #define ASCII_ISALNUM(c) \
82 	(ASCII_ISDIGIT(c) || ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
83 
84 #define ASCII_TOUPPER(c) \
85 	(('a' <= (c) && (c) <= 'z') ? ((c) - 'a' + 'A') : (c))
86 #define ASCII_TOLOWER(c) \
87 	(('A' <= (c) && (c) <= 'Z') ? ((c) - 'A' + 'a') : (c))
88 
89 int
idn__util_asciihaveaceprefix(const char * str,const char * prefix)90 idn__util_asciihaveaceprefix(const char *str, const char *prefix) {
91 	assert(str != NULL && prefix != NULL);
92 
93 	while (*prefix != '\0') {
94 		if (ASCII_TOLOWER(*str) != ASCII_TOLOWER(*prefix))
95 			return 0;
96 		str++;
97 		prefix++;
98 	}
99 
100 	return (1);
101 }
102 
103 int
idn__util_ucs4haveaceprefix(const unsigned long * str,const char * prefix)104 idn__util_ucs4haveaceprefix(const unsigned long *str, const char *prefix) {
105 	assert(str != NULL && prefix != NULL);
106 
107 	while (*prefix != '\0') {
108 		if (ASCII_TOLOWER(*str) != ASCII_TOLOWER(*prefix))
109 			return 0;
110 		str++;
111 		prefix++;
112 	}
113 
114 	return (1);
115 }
116 
117 int
idn__util_ucs4isasciirange(const unsigned long * str)118 idn__util_ucs4isasciirange(const unsigned long *str) {
119 	while (*str != '\0') {
120 		if (*str > 0x7f)
121 			return (0);
122 		str++;
123 	}
124 
125 	return (1);
126 }
127 
128 #ifdef WIN32
129 int
idn__util_getregistrystring(idn__util_hkey_t topkey,const char * name,char * str,size_t length)130 idn__util_getregistrystring(idn__util_hkey_t topkey, const char *name,
131 			    char *str, size_t length)
132 {
133 	HKEY top;
134 	LONG stat;
135 	HKEY hk;
136 	DWORD len, type;
137 
138 	assert((topkey == idn__util_hkey_currentuser ||
139 		topkey == idn__util_hkey_localmachine) &&
140 	       name != NULL && str != NULL);
141 
142 	if (topkey == idn__util_hkey_currentuser) {
143 		top= HKEY_CURRENT_USER;
144 	} else {	/* idn__util_hkey_localmachine */
145 		top = HKEY_LOCAL_MACHINE;
146 	}
147 
148 	stat = RegOpenKeyEx(top, IDNKEY_IDNKIT, 0, KEY_READ, &hk);
149 	if (stat != ERROR_SUCCESS) {
150 		return (0);
151 	}
152 
153 	len = (DWORD)length;
154 	stat = RegQueryValueEx(hk, (LPCTSTR)name, NULL,
155 			       &type, (LPBYTE)str, &len);
156 	RegCloseKey(hk);
157 
158 	if (stat != ERROR_SUCCESS || type != REG_SZ) {
159 		return (0);
160 	}
161 
162 	return (1);
163 }
164 #endif /* WIN32 */
165