winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / dnsapi / name.c
blobc14c8a0e49511375114f193f353ab806d669469e
1 /*
2 * DNS support
4 * Copyright (C) 2006 Matthew Kehrer
5 * Copyright (C) 2006 Hans Leidekker
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
26 #include <stdarg.h>
27 #include <sys/types.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_H
33 # include <arpa/nameser.h>
34 # undef NOERROR
35 #endif
36 #ifdef HAVE_RESOLV_H
37 # include <resolv.h>
38 #endif
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winerror.h"
43 #include "winnls.h"
44 #include "windns.h"
46 #include "dnsapi.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(dnsapi);
50 /******************************************************************************
51 * DnsNameCompare_A [DNSAPI.@]
54 BOOL WINAPI DnsNameCompare_A( PCSTR name1, PCSTR name2 )
56 BOOL ret;
57 PWSTR name1W, name2W;
59 TRACE( "(%s,%s)\n", debugstr_a(name1), debugstr_a(name2) );
61 name1W = dns_strdup_aw( name1 );
62 name2W = dns_strdup_aw( name2 );
64 ret = DnsNameCompare_W( name1W, name2W );
66 heap_free( name1W );
67 heap_free( name2W );
69 return ret;
72 /******************************************************************************
73 * DnsNameCompare_W [DNSAPI.@]
76 BOOL WINAPI DnsNameCompare_W( PCWSTR name1, PCWSTR name2 )
78 PCWSTR p, q;
80 TRACE( "(%s,%s)\n", debugstr_w(name1), debugstr_w(name2) );
82 if (!name1 && !name2) return TRUE;
83 if (!name1 || !name2) return FALSE;
85 p = name1 + lstrlenW( name1 ) - 1;
86 q = name2 + lstrlenW( name2 ) - 1;
88 while (*p == '.' && p >= name1) p--;
89 while (*q == '.' && q >= name2) q--;
91 if (p - name1 != q - name2) return FALSE;
93 while (name1 <= p)
95 if (toupperW( *name1 ) != toupperW( *name2 ))
96 return FALSE;
98 name1++;
99 name2++;
101 return TRUE;
104 /******************************************************************************
105 * DnsValidateName_A [DNSAPI.@]
108 DNS_STATUS WINAPI DnsValidateName_A( PCSTR name, DNS_NAME_FORMAT format )
110 PWSTR nameW;
111 DNS_STATUS ret;
113 TRACE( "(%s, %d)\n", debugstr_a(name), format );
115 nameW = dns_strdup_aw( name );
116 ret = DnsValidateName_W( nameW, format );
118 heap_free( nameW );
119 return ret;
122 /******************************************************************************
123 * DnsValidateName_UTF8 [DNSAPI.@]
126 DNS_STATUS WINAPI DnsValidateName_UTF8( PCSTR name, DNS_NAME_FORMAT format )
128 PWSTR nameW;
129 DNS_STATUS ret;
131 TRACE( "(%s, %d)\n", debugstr_a(name), format );
133 nameW = dns_strdup_uw( name );
134 ret = DnsValidateName_W( nameW, format );
136 heap_free( nameW );
137 return ret;
140 #define HAS_EXTENDED 0x0001
141 #define HAS_NUMERIC 0x0002
142 #define HAS_NON_NUMERIC 0x0004
143 #define HAS_DOT 0x0008
144 #define HAS_DOT_DOT 0x0010
145 #define HAS_SPACE 0x0020
146 #define HAS_INVALID 0x0040
147 #define HAS_ASTERISK 0x0080
148 #define HAS_UNDERSCORE 0x0100
149 #define HAS_LONG_LABEL 0x0200
151 /******************************************************************************
152 * DnsValidateName_W [DNSAPI.@]
155 DNS_STATUS WINAPI DnsValidateName_W( PCWSTR name, DNS_NAME_FORMAT format )
157 PCWSTR p;
158 unsigned int i, j, state = 0;
159 static const WCHAR invalid[] = {
160 '{','|','}','~','[','\\',']','^','\'',':',';','<','=','>',
161 '?','@','!','\"','#','$','%','^','`','(',')','+','/',',',0 };
163 TRACE( "(%s, %d)\n", debugstr_w(name), format );
165 if (!name) return ERROR_INVALID_NAME;
167 for (p = name, i = 0, j = 0; *p; p++, i++, j++)
169 if (*p == '.')
171 j = 0;
172 state |= HAS_DOT;
173 if (p[1] == '.') state |= HAS_DOT_DOT;
175 else if (*p < '0' || *p > '9') state |= HAS_NON_NUMERIC;
176 else state |= HAS_NUMERIC;
178 if (j > 62) state |= HAS_LONG_LABEL;
180 if (strchrW( invalid, *p )) state |= HAS_INVALID;
181 else if ((unsigned)*p > 127) state |= HAS_EXTENDED;
182 else if (*p == ' ') state |= HAS_SPACE;
183 else if (*p == '_') state |= HAS_UNDERSCORE;
184 else if (*p == '*') state |= HAS_ASTERISK;
187 if (i == 0 || i > 255 ||
188 (state & HAS_LONG_LABEL) ||
189 (state & HAS_DOT_DOT) ||
190 (name[0] == '.' && name[1])) return ERROR_INVALID_NAME;
192 switch (format)
194 case DnsNameDomain:
196 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
197 return DNS_ERROR_NON_RFC_NAME;
198 if ((state & HAS_SPACE) ||
199 (state & HAS_INVALID) ||
200 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
201 break;
203 case DnsNameDomainLabel:
205 if (state & HAS_DOT) return ERROR_INVALID_NAME;
206 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
207 return DNS_ERROR_NON_RFC_NAME;
208 if ((state & HAS_SPACE) ||
209 (state & HAS_INVALID) ||
210 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
211 break;
213 case DnsNameHostnameFull:
215 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
216 return DNS_ERROR_NON_RFC_NAME;
217 if ((state & HAS_SPACE) ||
218 (state & HAS_INVALID) ||
219 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
220 break;
222 case DnsNameHostnameLabel:
224 if (state & HAS_DOT) return ERROR_INVALID_NAME;
225 if ((state & HAS_EXTENDED) || (state & HAS_UNDERSCORE))
226 return DNS_ERROR_NON_RFC_NAME;
227 if ((state & HAS_SPACE) ||
228 (state & HAS_INVALID) ||
229 (state & HAS_ASTERISK)) return DNS_ERROR_INVALID_NAME_CHAR;
230 break;
232 case DnsNameWildcard:
234 if (!(state & HAS_NON_NUMERIC) && (state & HAS_NUMERIC))
235 return ERROR_INVALID_NAME;
236 if (name[0] != '*') return ERROR_INVALID_NAME;
237 if (name[1] && name[1] != '.')
238 return DNS_ERROR_INVALID_NAME_CHAR;
239 if ((state & HAS_EXTENDED) ||
240 (state & HAS_SPACE) ||
241 (state & HAS_INVALID)) return ERROR_INVALID_NAME;
242 break;
244 case DnsNameSrvRecord:
246 if (!(state & HAS_NON_NUMERIC) && (state & HAS_NUMERIC))
247 return ERROR_INVALID_NAME;
248 if (name[0] != '_') return ERROR_INVALID_NAME;
249 if ((state & HAS_UNDERSCORE) && !name[1])
250 return DNS_ERROR_NON_RFC_NAME;
251 if ((state & HAS_EXTENDED) ||
252 (state & HAS_SPACE) ||
253 (state & HAS_INVALID)) return ERROR_INVALID_NAME;
254 break;
256 default:
257 WARN( "unknown format: %d\n", format );
258 break;
260 return ERROR_SUCCESS;