1 #ifndef lint
2 static char *rcsid = "$Id: util.c,v 1.1 2003/06/04 00:26:45 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 #ifdef WIN32
51 #include <windows.h>
52 #undef ERROR
53 #endif
54 
55 #include <stddef.h>
56 
57 #include <idn/assert.h>
58 #include <idn/result.h>
59 #include <idn/logmacro.h>
60 #include <idn/util.h>
61 #include <idn/ucs4.h>
62 
63 #ifdef WIN32
64 #define IDNKEY_IDNKIT		"Software\\JPNIC\\IDN"
65 #endif
66 
67 /*
68  * ASCII ctype macros.
69  * Note that these macros evaluate the argument multiple times.  Be careful.
70  */
71 #define ASCII_ISDIGIT(c) \
72 	('0' <= (c) && (c) <= '9')
73 #define ASCII_ISUPPER(c) \
74 	('A' <= (c) && (c) <= 'Z')
75 #define ASCII_ISLOWER(c) \
76 	('a' <= (c) && (c) <= 'z')
77 #define ASCII_ISALPHA(c) \
78 	(ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
79 #define ASCII_ISALNUM(c) \
80 	(ASCII_ISDIGIT(c) || ASCII_ISUPPER(c) || ASCII_ISLOWER(c))
81 
82 #define ASCII_TOUPPER(c) \
83 	(('a' <= (c) && (c) <= 'z') ? ((c) - 'a' + 'A') : (c))
84 #define ASCII_TOLOWER(c) \
85 	(('A' <= (c) && (c) <= 'Z') ? ((c) - 'A' + 'a') : (c))
86 
87 int
idn__util_asciihaveaceprefix(const char * str,const char * prefix)88 idn__util_asciihaveaceprefix(const char *str, const char *prefix) {
89 	assert(str != NULL && prefix != NULL);
90 
91 	while (*prefix != '\0') {
92 		if (ASCII_TOLOWER(*str) != ASCII_TOLOWER(*prefix))
93 			return 0;
94 		str++;
95 		prefix++;
96 	}
97 
98 	return (1);
99 }
100 
101 int
idn__util_ucs4haveaceprefix(const unsigned long * str,const char * prefix)102 idn__util_ucs4haveaceprefix(const unsigned long *str, const char *prefix) {
103 	assert(str != NULL && prefix != NULL);
104 
105 	while (*prefix != '\0') {
106 		if (ASCII_TOLOWER(*str) != ASCII_TOLOWER(*prefix))
107 			return 0;
108 		str++;
109 		prefix++;
110 	}
111 
112 	return (1);
113 }
114 
115 int
idn__util_ucs4isasciirange(const unsigned long * str)116 idn__util_ucs4isasciirange(const unsigned long *str) {
117 	while (*str != '\0') {
118 		if (*str > 0x7f)
119 			return (0);
120 		str++;
121 	}
122 
123 	return (1);
124 }
125 
126 #ifdef WIN32
127 int
idn__util_getregistrystring(idn__util_hkey_t topkey,const char * name,char * str,size_t length)128 idn__util_getregistrystring(idn__util_hkey_t topkey, const char *name,
129 			    char *str, size_t length)
130 {
131 	HKEY top;
132 	LONG stat;
133 	HKEY hk;
134 	DWORD len, type;
135 
136 	assert((topkey == idn__util_hkey_currentuser ||
137 		topkey == idn__util_hkey_localmachine) &&
138 	       name != NULL && str != NULL);
139 
140 	if (topkey == idn__util_hkey_currentuser) {
141 		top= HKEY_CURRENT_USER;
142 	} else {	/* idn__util_hkey_localmachine */
143 		top = HKEY_LOCAL_MACHINE;
144 	}
145 
146 	stat = RegOpenKeyEx(top, IDNKEY_IDNKIT, 0, KEY_READ, &hk);
147 	if (stat != ERROR_SUCCESS) {
148 		return (0);
149 	}
150 
151 	len = (DWORD)length;
152 	stat = RegQueryValueEx(hk, (LPCTSTR)name, NULL,
153 			       &type, (LPBYTE)str, &len);
154 	RegCloseKey(hk);
155 
156 	if (stat != ERROR_SUCCESS || type != REG_SZ) {
157 		return (0);
158 	}
159 
160 	return (1);
161 }
162 #endif /* WIN32 */
163