2 * Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
3 * 2006-2009 Bjorn Andersson <flex@kryo.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 build_hostname(char *buf, size_t buflen,
24 const char *data, const size_t datalen,
25 const char *topdomain, struct encoder *encoder, int maxlen)
30 space = MIN((size_t)maxlen, buflen) - strlen(topdomain) - 8;
31 /* 8 = 5 max header length + 1 dot before topdomain + 2 safety */
33 if (!encoder->places_dots())
34 space -= (space / 57); /* space for dots */
36 memset(buf, 0, buflen);
38 encoder->encode(buf, &space, data, datalen);
40 if (!encoder->places_dots())
41 inline_dotify(buf, buflen);
46 /* move b back one step to see if the dot is there */
51 /* move b ahead of the string so we can copy to it */
53 strncpy(b, topdomain, strlen(topdomain)+1);
59 unpack_data(char *buf, size_t buflen, char *data, size_t datalen, struct encoder *enc)
61 if (!enc->eats_dots())
62 datalen = inline_undotify(data, datalen);
63 return enc->decode(buf, &buflen, data, datalen);
67 inline_dotify(char *buf, size_t buflen)
72 char *reader, *writer;
82 if (strlen(buf) + dots > buflen) {
88 reader = writer - dots;
89 pos = (unsigned) (reader - buf) + 1;
92 *writer-- = *reader--;
100 /* return new length of string */
105 inline_undotify(char *buf, size_t len)
109 char *reader, *writer;
118 if (*reader == '.') {
124 *writer++ = *reader++;
128 /* return new length of string */