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