/src/avahi/avahi-common/alternative.c
Line | Count | Source |
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 | 1.15k | static void drop_incomplete_utf8(char *c) { |
36 | 1.15k | char *e; |
37 | | |
38 | 1.15k | e = strchr(c, 0) - 1; |
39 | | |
40 | 1.22k | while (e >= c) { |
41 | | |
42 | 1.09k | if (avahi_utf8_valid(c)) |
43 | 1.01k | break; |
44 | | |
45 | 1.09k | assert(*e & 128); |
46 | 71 | *e = 0; |
47 | | |
48 | 71 | e--; |
49 | 71 | } |
50 | 1.15k | } |
51 | | |
52 | 954 | char *avahi_alternative_host_name(const char *s) { |
53 | 954 | char label[AVAHI_LABEL_MAX], alternative[AVAHI_LABEL_MAX*4+1]; |
54 | 954 | char *alt, *r, *ret; |
55 | 954 | const char *e; |
56 | 954 | size_t len; |
57 | | |
58 | 954 | assert(s); |
59 | | |
60 | 954 | if (!avahi_is_valid_host_name(s)) |
61 | 521 | return NULL; |
62 | | |
63 | 433 | if (!avahi_unescape_label(&s, label, sizeof(label))) |
64 | 0 | return NULL; |
65 | | |
66 | 433 | if ((e = strrchr(label, '-'))) { |
67 | 118 | const char *p; |
68 | | |
69 | 118 | e++; |
70 | | |
71 | 805 | for (p = e; *p; p++) |
72 | 708 | if (!isdigit((unsigned char)*p)) { |
73 | 21 | e = NULL; |
74 | 21 | break; |
75 | 21 | } |
76 | | |
77 | 118 | if (e && (*e == '0' || *e == 0)) |
78 | 8 | e = NULL; |
79 | 118 | } |
80 | | |
81 | 433 | if (e) { |
82 | 89 | char *c, *m; |
83 | 89 | int n; |
84 | | |
85 | 89 | n = atoi(e); |
86 | 89 | if (n == INT_MAX) |
87 | 7 | n = 1; |
88 | 82 | else |
89 | 82 | n++; |
90 | 89 | if (!(m = avahi_strdup_printf("%i", n))) |
91 | 0 | return NULL; |
92 | | |
93 | 89 | len = e-label-1; |
94 | | |
95 | 89 | if (len >= AVAHI_LABEL_MAX-1-strlen(m)-1) |
96 | 5 | len = AVAHI_LABEL_MAX-1-strlen(m)-1; |
97 | | |
98 | 89 | if (!(c = avahi_strndup(label, len))) { |
99 | 0 | avahi_free(m); |
100 | 0 | return NULL; |
101 | 0 | } |
102 | | |
103 | 89 | drop_incomplete_utf8(c); |
104 | | |
105 | 89 | r = avahi_strdup_printf("%s-%s", c, m); |
106 | 89 | avahi_free(c); |
107 | 89 | avahi_free(m); |
108 | | |
109 | 344 | } else { |
110 | 344 | char *c; |
111 | | |
112 | 344 | if (!(c = avahi_strndup(label, AVAHI_LABEL_MAX-1-2))) |
113 | 0 | return NULL; |
114 | | |
115 | 344 | drop_incomplete_utf8(c); |
116 | | |
117 | 344 | r = avahi_strdup_printf("%s-2", c); |
118 | 344 | avahi_free(c); |
119 | 344 | } |
120 | | |
121 | 433 | if (!r) |
122 | 0 | return NULL; |
123 | | |
124 | 433 | alt = alternative; |
125 | 433 | len = sizeof(alternative); |
126 | 433 | ret = avahi_escape_label(r, strlen(r), &alt, &len); |
127 | | |
128 | 433 | avahi_free(r); |
129 | 433 | if (!(r = avahi_strdup(ret))) |
130 | 0 | return NULL; |
131 | | |
132 | 433 | assert(avahi_is_valid_host_name(r)); |
133 | | |
134 | 433 | return r; |
135 | 433 | } |
136 | | |
137 | 954 | char *avahi_alternative_service_name(const char *s) { |
138 | 954 | const char *e; |
139 | 954 | char *r; |
140 | | |
141 | 954 | assert(s); |
142 | | |
143 | 954 | if (!avahi_is_valid_service_name(s)) |
144 | 230 | return NULL; |
145 | | |
146 | 724 | if ((e = strstr(s, " #"))) { |
147 | 117 | const char *n, *p; |
148 | 117 | e += 2; |
149 | | |
150 | 195 | while ((n = strstr(e, " #"))) |
151 | 78 | e = n + 2; |
152 | | |
153 | 751 | for (p = e; *p; p++) |
154 | 657 | if (!isdigit((unsigned char)*p)) { |
155 | 23 | e = NULL; |
156 | 23 | break; |
157 | 23 | } |
158 | | |
159 | 117 | if (e && (*e == '0' || *e == 0)) |
160 | 9 | e = NULL; |
161 | 117 | } |
162 | | |
163 | 724 | if (e) { |
164 | 85 | char *c, *m; |
165 | 85 | size_t l; |
166 | 85 | int n; |
167 | | |
168 | 85 | n = atoi(e); |
169 | 85 | if (n == INT_MAX) |
170 | 2 | n = 1; |
171 | 83 | else |
172 | 83 | n++; |
173 | 85 | if (!(m = avahi_strdup_printf("%i", n))) |
174 | 0 | return NULL; |
175 | | |
176 | 85 | l = e-s-2; |
177 | | |
178 | 85 | if (l >= AVAHI_LABEL_MAX-1-strlen(m)-2) |
179 | 6 | l = AVAHI_LABEL_MAX-1-strlen(m)-2; |
180 | | |
181 | 85 | if (!(c = avahi_strndup(s, l))) { |
182 | 0 | avahi_free(m); |
183 | 0 | return NULL; |
184 | 0 | } |
185 | | |
186 | 85 | drop_incomplete_utf8(c); |
187 | | |
188 | 85 | r = avahi_strdup_printf("%s #%s", c, m); |
189 | 85 | avahi_free(c); |
190 | 85 | avahi_free(m); |
191 | 639 | } else { |
192 | 639 | char *c; |
193 | | |
194 | 639 | if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-3))) |
195 | 0 | return NULL; |
196 | | |
197 | 639 | drop_incomplete_utf8(c); |
198 | | |
199 | 639 | r = avahi_strdup_printf("%s #2", c); |
200 | 639 | avahi_free(c); |
201 | 639 | } |
202 | | |
203 | 724 | if (!r) |
204 | 0 | return NULL; |
205 | | |
206 | 724 | assert(avahi_is_valid_service_name(r)); |
207 | | |
208 | 724 | return r; |
209 | 724 | } |