iodine/src/encoding.c

94 lines
1.9 KiB
C
Raw Normal View History

2006-08-13 02:24:59 +03:00
/*
2009-01-04 01:27:21 +02:00
* Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
2006-08-13 02:24:59 +03:00
*
* Permission to use, copy, modify, and distribute this software for any
* 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 20:15:24 +03:00
#include <string.h>
2007-06-09 19:18:59 +03:00
#include "encoding.h"
2006-08-13 02:24:59 +03:00
2007-06-09 19:38:31 +03:00
int
unpack_data(char *buf, size_t buflen, char *data, size_t datalen, struct encoder *enc)
{
if (!enc->eats_dots())
datalen = inline_undotify(data, datalen);
return enc->decode(buf, &buflen, data, datalen);
}
2007-06-09 19:18:59 +03:00
int
inline_dotify(char *buf, size_t buflen)
2006-08-13 20:15:24 +03:00
{
2007-06-09 19:18:59 +03:00
unsigned dots;
unsigned pos;
unsigned total;
char *reader, *writer;
total = strlen(buf);
dots = total / 57;
2007-06-09 19:18:59 +03:00
writer = buf;
writer += total;
writer += dots;
total += dots;
if (strlen(buf) + dots > buflen) {
writer = buf;
writer += buflen;
total = buflen;
2006-08-13 20:15:24 +03:00
}
2007-06-09 19:18:59 +03:00
reader = writer - dots;
pos = (unsigned) (reader - buf) + 1;
2006-08-13 02:24:59 +03:00
2007-06-09 19:18:59 +03:00
while (dots) {
*writer-- = *reader--;
pos--;
if (pos % 57 == 0) {
2007-06-09 19:18:59 +03:00
*writer-- = '.';
dots--;
2006-08-13 02:24:59 +03:00
}
}
2007-06-09 19:18:59 +03:00
/* return new length of string */
return total;
2006-08-13 02:24:59 +03:00
}
2007-06-09 19:18:59 +03:00
int
inline_undotify(char *buf, size_t len)
2006-08-13 02:24:59 +03:00
{
2007-06-09 19:18:59 +03:00
unsigned pos;
unsigned dots;
char *reader, *writer;
2006-08-13 02:24:59 +03:00
2007-06-09 19:18:59 +03:00
writer = buf;
reader = writer;
2007-06-09 19:18:59 +03:00
pos = 0;
dots = 0;
2007-02-08 22:32:17 +02:00
2007-06-09 19:18:59 +03:00
while (pos < len) {
if (*reader == '.') {
reader++;
pos++;
dots++;
2006-08-13 02:24:59 +03:00
continue;
}
2007-06-09 19:18:59 +03:00
*writer++ = *reader++;
pos++;
2006-08-13 20:15:24 +03:00
}
2007-06-09 19:18:59 +03:00
/* return new length of string */
return len - dots;
2006-08-13 02:24:59 +03:00
}