Files
pcp/libpcp/z85.c

241 lines
5.0 KiB
C
Raw Normal View History

2013-11-04 17:43:22 +01:00
/*
This file is part of Pretty Curved Privacy (pcp1).
Copyright (C) 2013-2014 T.v.Dein.
2013-11-04 17:43:22 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
You can contact me by mail: <tom AT vondein DOT org>.
2013-11-04 17:43:22 +01:00
*/
2013-10-28 22:50:05 +01:00
#include "z85.h"
2014-02-13 15:30:23 +01:00
size_t _buffer_is_binary(unsigned char *buf, size_t len) {
size_t pos;
for (pos=0; pos<len; ++pos) {
if(buf[pos] == '\0' || (buf[pos] != '\r' && buf[pos] != '\n' && isprint(buf[pos]) == 0)) {
break;
}
}
if(pos < len)
return pos;
else
return 0;
}
2013-10-28 22:50:05 +01:00
unsigned char *pcp_padfour(unsigned char *src, size_t srclen, size_t *dstlen) {
size_t outlen, zerolen;
unsigned char *dst;
outlen = srclen;
2013-10-28 22:50:05 +01:00
while (outlen % 4 != 0) outlen++;
zerolen = outlen - srclen;
2013-10-28 22:50:05 +01:00
dst = (unsigned char*)ucmalloc(outlen);
memcpy(dst, src, srclen); /* add the original */
memset(&dst[srclen], 0, zerolen); /* pad with zeroes */
2013-10-28 22:50:05 +01:00
*dstlen = outlen;
return dst;
}
size_t pcp_unpadfour(unsigned char *src, size_t srclen) {
2013-10-28 22:50:05 +01:00
size_t outlen;
size_t i;
2013-10-28 22:50:05 +01:00
outlen = srclen;
2013-10-28 22:50:05 +01:00
for(i=srclen-1; i>0; --i) {
if(src[i] != '\0') {
outlen = i + 1;
break;
}
}
2013-10-28 22:50:05 +01:00
return outlen;
2013-10-28 22:50:05 +01:00
}
unsigned char *pcp_z85_decode(char *z85block, size_t *dstlen) {
unsigned char *bin = NULL;
size_t binlen, outlen;
2014-02-09 15:49:52 +01:00
binlen = strlen(z85block) * 4 / 5;
2013-10-28 22:50:05 +01:00
bin = ucmalloc(binlen);
if(zmq_z85_decode(bin, z85block) == NULL) {
fatal("zmq_z85_decode() failed, input size ! mod 5 (got %ld)", strlen(z85block));
2014-02-09 15:49:52 +01:00
return NULL;
}
outlen = pcp_unpadfour(bin, binlen);
2013-10-28 22:50:05 +01:00
*dstlen = outlen;
return bin;
2013-10-28 22:50:05 +01:00
}
char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen) {
int pos = 0;
2013-10-28 22:50:05 +01:00
size_t outlen, blocklen, zlen;
/* make z85 happy (size % 4) */
2013-10-28 22:50:05 +01:00
unsigned char *padded = pcp_padfour(raw, srclen, &outlen);
/* encode to z85 */
2013-10-28 22:50:05 +01:00
zlen = (outlen * 5 / 4) + 1;
char *z85 = ucmalloc(zlen);
z85 = zmq_z85_encode(z85, padded, outlen);
/* make it a 72 chars wide block */
blocklen = (zlen + ((zlen / 72) * 2)) + 1;
2013-10-28 22:50:05 +01:00
char *z85block = ucmalloc(blocklen);
char *z = &z85[0];
char *B = &z85block[0];
while(*z != '\0') {
if(pos >= 71) {
*B++ = '\r';
*B++ = '\n';
2013-10-28 22:50:05 +01:00
pos = 1;
}
else {
pos++;
}
*B++ = *z++;
2013-10-28 22:50:05 +01:00
}
*B = '\0';
2013-10-28 22:50:05 +01:00
*dstlen = blocklen;
free(z85);
2013-10-28 22:50:05 +01:00
free(padded);
return z85block;
}
char *pcp_readz85file(FILE *infile) {
unsigned char *input = NULL;
unsigned char *tmp = NULL;
size_t bufsize = 0;
unsigned char byte[1];
2013-10-28 22:50:05 +01:00
while(!feof(infile)) {
if(!fread(&byte, 1, 1, infile))
break;
2013-12-19 16:54:16 +01:00
if(ferror(infile) != 0)
break;
2013-10-28 22:50:05 +01:00
tmp = realloc(input, bufsize + 1);
input = tmp;
memmove(&input[bufsize], byte, 1);
bufsize ++;
}
if(bufsize == 0) {
fatal("Input file is empty!\n");
free(tmp);
return NULL;
2013-10-28 22:50:05 +01:00
}
return pcp_readz85string(input, bufsize);
}
char *pcp_readz85string(unsigned char *input, size_t bufsize) {
int i;
size_t MAXLINE = 1024;
2014-02-13 15:30:23 +01:00
if(bufsize == 0) {
fatal("Input file is empty!\n");
return NULL;
}
if(_buffer_is_binary(input, bufsize) > 0) {
fatal("input is not z85 encoded and contains pure binary data");
return NULL;
}
Buffer *z = buffer_new(MAXLINE, "z");
Buffer *line = buffer_new(MAXLINE, "line");
char *oneline;
int begin, end;
begin = end = 0;
char *out = NULL;
2013-10-28 22:50:05 +01:00
for(i=0; i<bufsize; ++i) {
if(input[i] == '\r')
continue;
else if(input[i] == '\n') {
/* a line is complete */
oneline = buffer_get_str(line);
if(strncmp(oneline, "-----", 5) == 0 ) {
if(begin == 0) {
/* a begin header, reset whatever we've got so far in z buffer */
begin = 1;
buffer_clear(line);
buffer_clear(z);
continue;
2013-10-28 22:50:05 +01:00
}
else {
/* an end header */
end = 1;
break;
}
}
else if(strchr(oneline, ' ') != NULL) {
/* a comment */
buffer_clear(line);
continue;
2013-10-28 22:50:05 +01:00
}
else {
/* regular z85 encoded content */
buffer_add_buf(z, line);
buffer_clear(line);
2013-10-28 22:50:05 +01:00
}
}
else {
/* regular line content */
buffer_add8(line, input[i]);
}
}
2014-02-09 15:49:52 +01:00
if(buffer_size(line) > 0 && end != 1) {
/* something left in line buffer, probably
newline at eof missing or no multiline input */
buffer_add_buf(z, line);
}
if(buffer_size(z) == 0) {
fatal("empty z85 encoded string");
goto rferr;
2013-10-28 22:50:05 +01:00
}
out = ucmalloc(buffer_size(z)+1);
strncpy(out, buffer_get_str(z), buffer_size(z)+1);
2013-10-28 22:50:05 +01:00
buffer_free(z);
buffer_free(line);
2013-10-28 22:50:05 +01:00
return out;
2013-10-28 22:50:05 +01:00
rferr:
buffer_free(z);
buffer_free(line);
2013-10-28 22:50:05 +01:00
return NULL;
}