2006-08-12 23:24:59 +00:00
|
|
|
/*
|
2014-06-01 08:46:42 +02:00
|
|
|
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
|
|
|
|
* 2006-2009 Bjorn Andersson <flex@kryo.se>
|
2006-08-12 23:24:59 +00:00
|
|
|
*
|
2014-08-07 21:14:10 +02:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
2006-08-12 23:24:59 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2006-08-13 17:15:24 +00:00
|
|
|
#include <string.h>
|
2009-09-20 08:43:48 +00:00
|
|
|
#include "common.h"
|
2007-06-09 16:18:59 +00:00
|
|
|
#include "encoding.h"
|
2006-08-12 23:24:59 +00:00
|
|
|
|
2017-03-11 15:17:11 -08:00
|
|
|
int build_hostname(char *buf, size_t buflen, const char *data,
|
|
|
|
const size_t datalen, const char *topdomain,
|
|
|
|
const struct encoder *encoder, int maxlen)
|
2009-09-20 08:43:48 +00:00
|
|
|
{
|
|
|
|
size_t space;
|
|
|
|
char *b;
|
|
|
|
|
2014-04-02 12:53:36 +01:00
|
|
|
space = MIN((size_t)maxlen, buflen) - strlen(topdomain) - 8;
|
2009-12-29 20:00:57 +00:00
|
|
|
/* 8 = 5 max header length + 1 dot before topdomain + 2 safety */
|
|
|
|
|
2017-03-11 15:37:56 -08:00
|
|
|
if (!encoder->places_dots)
|
2009-09-20 08:43:48 +00:00
|
|
|
space -= (space / 57); /* space for dots */
|
|
|
|
|
|
|
|
memset(buf, 0, buflen);
|
2014-06-01 08:34:18 +02:00
|
|
|
|
2014-01-29 19:10:52 +01:00
|
|
|
encoder->encode(buf, &space, data, datalen);
|
2009-09-20 08:43:48 +00:00
|
|
|
|
2017-03-11 15:37:56 -08:00
|
|
|
if (!encoder->places_dots)
|
2009-09-20 08:43:48 +00:00
|
|
|
inline_dotify(buf, buflen);
|
|
|
|
|
|
|
|
b = buf;
|
|
|
|
b += strlen(buf);
|
|
|
|
|
2009-09-20 08:43:49 +00:00
|
|
|
/* move b back one step to see if the dot is there */
|
|
|
|
b--;
|
2014-06-01 08:34:18 +02:00
|
|
|
if (*b != '.')
|
2009-09-20 08:43:49 +00:00
|
|
|
*++b = '.';
|
|
|
|
b++;
|
|
|
|
/* move b ahead of the string so we can copy to it */
|
2009-09-20 08:43:48 +00:00
|
|
|
|
|
|
|
strncpy(b, topdomain, strlen(topdomain)+1);
|
|
|
|
|
|
|
|
return space;
|
|
|
|
}
|
2007-06-09 16:38:31 +00:00
|
|
|
|
2017-03-11 15:17:11 -08:00
|
|
|
int unpack_data(char *buf, size_t buflen, char *data, size_t datalen,
|
|
|
|
const struct encoder *enc)
|
2007-06-09 16:38:31 +00:00
|
|
|
{
|
2017-03-11 15:37:56 -08:00
|
|
|
if (!enc->eats_dots)
|
2007-06-09 16:38:31 +00:00
|
|
|
datalen = inline_undotify(data, datalen);
|
|
|
|
return enc->decode(buf, &buflen, data, datalen);
|
|
|
|
}
|
|
|
|
|
2017-03-11 15:17:11 -08:00
|
|
|
int inline_dotify(char *buf, size_t buflen)
|
2006-08-13 17:15:24 +00:00
|
|
|
{
|
2007-06-09 16:18:59 +00:00
|
|
|
unsigned dots;
|
|
|
|
unsigned pos;
|
|
|
|
unsigned total;
|
|
|
|
char *reader, *writer;
|
|
|
|
|
|
|
|
total = strlen(buf);
|
2008-12-11 22:39:06 +00:00
|
|
|
dots = total / 57;
|
2007-06-09 16:18:59 +00:00
|
|
|
|
|
|
|
writer = buf;
|
|
|
|
writer += total;
|
|
|
|
writer += dots;
|
|
|
|
|
|
|
|
total += dots;
|
|
|
|
if (strlen(buf) + dots > buflen) {
|
|
|
|
writer = buf;
|
|
|
|
writer += buflen;
|
|
|
|
total = buflen;
|
2006-08-13 17:15:24 +00:00
|
|
|
}
|
|
|
|
|
2007-06-09 16:18:59 +00:00
|
|
|
reader = writer - dots;
|
2008-07-12 11:36:39 +00:00
|
|
|
pos = (unsigned) (reader - buf) + 1;
|
2006-08-12 23:24:59 +00:00
|
|
|
|
2007-06-09 16:18:59 +00:00
|
|
|
while (dots) {
|
2009-01-16 18:51:54 +00:00
|
|
|
*writer-- = *reader--;
|
|
|
|
pos--;
|
2008-12-11 22:39:06 +00:00
|
|
|
if (pos % 57 == 0) {
|
2007-06-09 16:18:59 +00:00
|
|
|
*writer-- = '.';
|
|
|
|
dots--;
|
2006-08-12 23:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-09 16:18:59 +00:00
|
|
|
|
|
|
|
/* return new length of string */
|
|
|
|
return total;
|
2006-08-12 23:24:59 +00:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:17:11 -08:00
|
|
|
int inline_undotify(char *buf, size_t len)
|
2006-08-12 23:24:59 +00:00
|
|
|
{
|
2007-06-09 16:18:59 +00:00
|
|
|
unsigned pos;
|
|
|
|
unsigned dots;
|
|
|
|
char *reader, *writer;
|
2006-08-12 23:24:59 +00:00
|
|
|
|
2007-06-09 16:18:59 +00:00
|
|
|
writer = buf;
|
|
|
|
reader = writer;
|
2007-02-04 23:08:09 +00:00
|
|
|
|
2007-06-09 16:18:59 +00:00
|
|
|
pos = 0;
|
|
|
|
dots = 0;
|
2007-02-08 20:32:17 +00:00
|
|
|
|
2007-06-09 16:18:59 +00:00
|
|
|
while (pos < len) {
|
|
|
|
if (*reader == '.') {
|
|
|
|
reader++;
|
|
|
|
pos++;
|
|
|
|
dots++;
|
2006-08-12 23:24:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-06-09 16:18:59 +00:00
|
|
|
*writer++ = *reader++;
|
|
|
|
pos++;
|
2006-08-13 17:15:24 +00:00
|
|
|
}
|
2014-06-01 08:34:18 +02:00
|
|
|
|
2007-06-09 16:18:59 +00:00
|
|
|
/* return new length of string */
|
|
|
|
return len - dots;
|
2006-08-12 23:24:59 +00:00
|
|
|
}
|