mirror of
https://github.com/yarrick/iodine.git
synced 2024-09-06 16:29:20 +03:00
94 lines
1.9 KiB
C
94 lines
1.9 KiB
C
/*
|
|
* Copyright (c) 2006-2008 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include "encoding.h"
|
|
|
|
|
|
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);
|
|
}
|
|
|
|
int
|
|
inline_dotify(char *buf, size_t buflen)
|
|
{
|
|
unsigned dots;
|
|
unsigned pos;
|
|
unsigned total;
|
|
char *reader, *writer;
|
|
|
|
total = strlen(buf);
|
|
dots = total / 62;
|
|
|
|
writer = buf;
|
|
writer += total;
|
|
writer += dots;
|
|
|
|
total += dots;
|
|
if (strlen(buf) + dots > buflen) {
|
|
writer = buf;
|
|
writer += buflen;
|
|
total = buflen;
|
|
}
|
|
|
|
reader = writer - dots;
|
|
pos = (unsigned) (reader - buf) + 1;
|
|
|
|
while (dots) {
|
|
if (pos % 62 == 0) {
|
|
*writer-- = '.';
|
|
dots--;
|
|
}
|
|
*writer-- = *reader--;
|
|
pos--;
|
|
}
|
|
|
|
/* return new length of string */
|
|
return total;
|
|
}
|
|
|
|
int
|
|
inline_undotify(char *buf, size_t len)
|
|
{
|
|
unsigned pos;
|
|
unsigned dots;
|
|
char *reader, *writer;
|
|
|
|
writer = buf;
|
|
reader = writer;
|
|
|
|
pos = 0;
|
|
dots = 0;
|
|
|
|
while (pos < len) {
|
|
if (*reader == '.') {
|
|
reader++;
|
|
pos++;
|
|
dots++;
|
|
continue;
|
|
}
|
|
*writer++ = *reader++;
|
|
pos++;
|
|
}
|
|
|
|
/* return new length of string */
|
|
return len - dots;
|
|
}
|