/src/avahi/avahi-common/alternative.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*** |
2 | | This file is part of avahi. |
3 | | |
4 | | avahi is free software; you can redistribute it and/or modify it |
5 | | under the terms of the GNU Lesser General Public License as |
6 | | published by the Free Software Foundation; either version 2.1 of the |
7 | | License, or (at your option) any later version. |
8 | | |
9 | | avahi is distributed in the hope that it will be useful, but WITHOUT |
10 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General |
12 | | Public License for more details. |
13 | | |
14 | | You should have received a copy of the GNU Lesser General Public |
15 | | License along with avahi; if not, write to the Free Software |
16 | | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
17 | | USA. |
18 | | ***/ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | #include <config.h> |
22 | | #endif |
23 | | |
24 | | #include <limits.h> |
25 | | #include <string.h> |
26 | | #include <stdlib.h> |
27 | | #include <ctype.h> |
28 | | #include <assert.h> |
29 | | |
30 | | #include "alternative.h" |
31 | | #include "malloc.h" |
32 | | #include "domain.h" |
33 | | #include "utf8.h" |
34 | | |
35 | 0 | static void drop_incomplete_utf8(char *c) { |
36 | 0 | char *e; |
37 | |
|
38 | 0 | e = strchr(c, 0) - 1; |
39 | |
|
40 | 0 | while (e >= c) { |
41 | |
|
42 | 0 | if (avahi_utf8_valid(c)) |
43 | 0 | break; |
44 | | |
45 | 0 | assert(*e & 128); |
46 | 0 | *e = 0; |
47 | |
|
48 | 0 | e--; |
49 | 0 | } |
50 | 0 | } |
51 | | |
52 | 0 | char *avahi_alternative_host_name(const char *s) { |
53 | 0 | char label[AVAHI_LABEL_MAX], alternative[AVAHI_LABEL_MAX*4+1]; |
54 | 0 | char *alt, *r, *ret; |
55 | 0 | const char *e; |
56 | 0 | size_t len; |
57 | |
|
58 | 0 | assert(s); |
59 | | |
60 | 0 | if (!avahi_is_valid_host_name(s)) |
61 | 0 | return NULL; |
62 | | |
63 | 0 | if (!avahi_unescape_label(&s, label, sizeof(label))) |
64 | 0 | return NULL; |
65 | | |
66 | 0 | if ((e = strrchr(label, '-'))) { |
67 | 0 | const char *p; |
68 | |
|
69 | 0 | e++; |
70 | |
|
71 | 0 | for (p = e; *p; p++) |
72 | 0 | if (!isdigit(*p)) { |
73 | 0 | e = NULL; |
74 | 0 | break; |
75 | 0 | } |
76 | |
|
77 | 0 | if (e && (*e == '0' || *e == 0)) |
78 | 0 | e = NULL; |
79 | 0 | } |
80 | |
|
81 | 0 | if (e) { |
82 | 0 | char *c, *m; |
83 | 0 | int n; |
84 | |
|
85 | 0 | n = atoi(e); |
86 | 0 | if (n == INT_MAX) |
87 | 0 | n = 1; |
88 | 0 | else |
89 | 0 | n++; |
90 | 0 | if (!(m = avahi_strdup_printf("%i", n))) |
91 | 0 | return NULL; |
92 | | |
93 | 0 | len = e-label-1; |
94 | |
|
95 | 0 | if (len >= AVAHI_LABEL_MAX-1-strlen(m)-1) |
96 | 0 | len = AVAHI_LABEL_MAX-1-strlen(m)-1; |
97 | |
|
98 | 0 | if (!(c = avahi_strndup(label, len))) { |
99 | 0 | avahi_free(m); |
100 | 0 | return NULL; |
101 | 0 | } |
102 | | |
103 | 0 | drop_incomplete_utf8(c); |
104 | |
|
105 | 0 | r = avahi_strdup_printf("%s-%s", c, m); |
106 | 0 | avahi_free(c); |
107 | 0 | avahi_free(m); |
108 | |
|
109 | 0 | } else { |
110 | 0 | char *c; |
111 | |
|
112 | 0 | if (!(c = avahi_strndup(label, AVAHI_LABEL_MAX-1-2))) |
113 | 0 | return NULL; |
114 | | |
115 | 0 | drop_incomplete_utf8(c); |
116 | |
|
117 | 0 | r = avahi_strdup_printf("%s-2", c); |
118 | 0 | avahi_free(c); |
119 | 0 | } |
120 | | |
121 | 0 | alt = alternative; |
122 | 0 | len = sizeof(alternative); |
123 | 0 | ret = avahi_escape_label(r, strlen(r), &alt, &len); |
124 | |
|
125 | 0 | avahi_free(r); |
126 | 0 | r = avahi_strdup(ret); |
127 | |
|
128 | 0 | assert(avahi_is_valid_host_name(r)); |
129 | | |
130 | 0 | return r; |
131 | 0 | } |
132 | | |
133 | 0 | char *avahi_alternative_service_name(const char *s) { |
134 | 0 | const char *e; |
135 | 0 | char *r; |
136 | |
|
137 | 0 | assert(s); |
138 | | |
139 | 0 | if (!avahi_is_valid_service_name(s)) |
140 | 0 | return NULL; |
141 | | |
142 | 0 | if ((e = strstr(s, " #"))) { |
143 | 0 | const char *n, *p; |
144 | 0 | e += 2; |
145 | |
|
146 | 0 | while ((n = strstr(e, " #"))) |
147 | 0 | e = n + 2; |
148 | |
|
149 | 0 | for (p = e; *p; p++) |
150 | 0 | if (!isdigit(*p)) { |
151 | 0 | e = NULL; |
152 | 0 | break; |
153 | 0 | } |
154 | |
|
155 | 0 | if (e && (*e == '0' || *e == 0)) |
156 | 0 | e = NULL; |
157 | 0 | } |
158 | |
|
159 | 0 | if (e) { |
160 | 0 | char *c, *m; |
161 | 0 | size_t l; |
162 | 0 | int n; |
163 | |
|
164 | 0 | n = atoi(e); |
165 | 0 | if (n == INT_MAX) |
166 | 0 | n = 1; |
167 | 0 | else |
168 | 0 | n++; |
169 | 0 | if (!(m = avahi_strdup_printf("%i", n))) |
170 | 0 | return NULL; |
171 | | |
172 | 0 | l = e-s-2; |
173 | |
|
174 | 0 | if (l >= AVAHI_LABEL_MAX-1-strlen(m)-2) |
175 | 0 | l = AVAHI_LABEL_MAX-1-strlen(m)-2; |
176 | |
|
177 | 0 | if (!(c = avahi_strndup(s, l))) { |
178 | 0 | avahi_free(m); |
179 | 0 | return NULL; |
180 | 0 | } |
181 | | |
182 | 0 | drop_incomplete_utf8(c); |
183 | |
|
184 | 0 | r = avahi_strdup_printf("%s #%s", c, m); |
185 | 0 | avahi_free(c); |
186 | 0 | avahi_free(m); |
187 | 0 | } else { |
188 | 0 | char *c; |
189 | |
|
190 | 0 | if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-3))) |
191 | 0 | return NULL; |
192 | | |
193 | 0 | drop_incomplete_utf8(c); |
194 | |
|
195 | 0 | r = avahi_strdup_printf("%s #2", c); |
196 | 0 | avahi_free(c); |
197 | 0 | } |
198 | | |
199 | 0 | assert(avahi_is_valid_service_name(r)); |
200 | | |
201 | 0 | return r; |
202 | 0 | } |