Commit | Line | Data |
---|---|---|
dc0d77d7 CE |
1 | #include "error.h" |
2 | #include "alloc.h" | |
3 | #include "case.h" | |
4 | #include "byte.h" | |
5 | #include "dns.h" | |
6 | ||
7 | unsigned int dns_domain_length(const char *dn) | |
8 | { | |
9 | const char *x; | |
10 | unsigned char c; | |
11 | ||
12 | x = dn; | |
13 | while (c = *x++) | |
14 | x += (unsigned int) c; | |
15 | return x - dn; | |
16 | } | |
17 | ||
18 | void dns_domain_free(char **out) | |
19 | { | |
20 | if (*out) { | |
21 | alloc_free(*out); | |
22 | *out = 0; | |
23 | } | |
24 | } | |
25 | ||
26 | int dns_domain_copy(char **out,const char *in) | |
27 | { | |
28 | unsigned int len; | |
29 | char *x; | |
30 | ||
31 | len = dns_domain_length(in); | |
32 | x = alloc(len); | |
33 | if (!x) return 0; | |
34 | byte_copy(x,len,in); | |
35 | if (*out) alloc_free(*out); | |
36 | *out = x; | |
37 | return 1; | |
38 | } | |
39 | ||
40 | int dns_domain_equal(const char *dn1,const char *dn2) | |
41 | { | |
42 | unsigned int len; | |
43 | ||
44 | len = dns_domain_length(dn1); | |
45 | if (len != dns_domain_length(dn2)) return 0; | |
46 | ||
47 | if (case_diffb(dn1,len,dn2)) return 0; /* safe since 63 < 'A' */ | |
48 | return 1; | |
49 | } | |
50 | ||
51 | int dns_domain_suffix(const char *big,const char *little) | |
52 | { | |
53 | unsigned char c; | |
54 | ||
55 | for (;;) { | |
56 | if (dns_domain_equal(big,little)) return 1; | |
57 | c = *big++; | |
58 | if (!c) return 0; | |
59 | big += c; | |
60 | } | |
61 | } | |
62 | ||
63 | unsigned int dns_domain_suffixpos(const char *big,const char *little) | |
64 | { | |
65 | const char *orig = big; | |
66 | unsigned char c; | |
67 | ||
68 | for (;;) { | |
69 | if (dns_domain_equal(big,little)) return big - orig; | |
70 | c = *big++; | |
71 | if (!c) return 0; | |
72 | big += c; | |
73 | } | |
74 | } |