Files
pcp/src/z85util.c

138 lines
2.9 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 T.Linden.
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: <tlinden AT cpan DOT org>.
*/
2013-10-28 22:50:05 +01:00
#include "z85util.h"
int pcpz85_encode(char *infile, char *outfile) {
FILE *in;
FILE *out;
2025-11-24 23:02:13 +01:00
if (infile == NULL)
2013-10-28 22:50:05 +01:00
in = stdin;
else {
2025-11-24 23:02:13 +01:00
if ((in = fopen(infile, "rb")) == NULL) {
fatal(ptx, "Could not open input file %s\n", infile);
2013-10-28 22:50:05 +01:00
goto errz1;
}
}
2025-11-24 23:02:13 +01:00
if (outfile == NULL)
2013-10-28 22:50:05 +01:00
out = stdout;
else {
2025-11-24 23:02:13 +01:00
if ((out = fopen(outfile, "wb+")) == NULL) {
fatal(ptx, "Could not open output file %s\n", outfile);
2013-10-28 22:50:05 +01:00
goto errz1;
}
}
byte *input = NULL;
2013-10-28 22:50:05 +01:00
size_t inputBufSize = 0;
byte onebyte[1];
2025-11-24 23:02:13 +01:00
while (!feof(in)) {
if (!fread(&onebyte, 1, 1, in))
2013-10-28 22:50:05 +01:00
break;
byte *tmp = realloc(input, inputBufSize + 1);
2013-10-28 22:50:05 +01:00
input = tmp;
memmove(&input[inputBufSize], onebyte, 1);
2025-11-24 23:02:13 +01:00
inputBufSize++;
2013-10-28 22:50:05 +01:00
}
fclose(in);
2025-11-24 23:02:13 +01:00
if (inputBufSize == 0) {
fatal(ptx, "Input file is empty!\n");
2013-10-28 22:50:05 +01:00
goto errz2;
}
size_t zlen;
char *encoded = pcp_z85_encode(input, inputBufSize, &zlen, 1);
2013-10-28 22:50:05 +01:00
2025-11-24 23:02:13 +01:00
if (encoded != NULL) {
2013-10-28 22:50:05 +01:00
fprintf(out, "%s\n%s\n%s\n", PCP_ZFILE_HEADER, encoded, PCP_ZFILE_FOOTER);
2025-11-24 23:02:13 +01:00
if (ferror(out) != 0) {
fatal(ptx, "Failed to write z85 output!\n");
2013-10-28 22:50:05 +01:00
}
free(encoded);
goto errz2;
}
return 0;
2025-11-24 23:02:13 +01:00
errz2:
2013-10-28 22:50:05 +01:00
free(input);
2025-11-24 23:02:13 +01:00
errz1:
2013-10-28 22:50:05 +01:00
return 1;
}
int pcpz85_decode(char *infile, char *outfile) {
FILE *in;
FILE *out;
2025-11-24 23:02:13 +01:00
if (infile == NULL)
2013-10-28 22:50:05 +01:00
in = stdin;
else {
2025-11-24 23:02:13 +01:00
if ((in = fopen(infile, "rb")) == NULL) {
fatal(ptx, "Could not open input file %s\n", infile);
2013-10-28 22:50:05 +01:00
goto errdz1;
}
}
2025-11-24 23:02:13 +01:00
if (outfile == NULL)
2013-10-28 22:50:05 +01:00
out = stdout;
else {
2025-11-24 23:02:13 +01:00
if ((out = fopen(outfile, "wb+")) == NULL) {
fatal(ptx, "Could not open output file %s\n", outfile);
2013-10-28 22:50:05 +01:00
goto errdz1;
}
}
char *encoded = pcp_readz85file(ptx, in);
2013-10-28 22:50:05 +01:00
2025-11-24 23:02:13 +01:00
if (encoded == NULL)
2013-10-28 22:50:05 +01:00
goto errdz1;
size_t clen;
byte *decoded = pcp_z85_decode(ptx, encoded, &clen);
2013-10-28 22:50:05 +01:00
2025-11-24 23:02:13 +01:00
if (decoded == NULL)
2013-10-28 22:50:05 +01:00
goto errdz2;
2025-11-24 23:02:13 +01:00
2013-10-28 22:50:05 +01:00
fwrite(decoded, clen, 1, out);
2025-11-24 23:02:13 +01:00
if (fclose(out) != 0) {
fatal(ptx, "Failed to write decoded output!\n");
2013-10-28 22:50:05 +01:00
goto errdz3;
}
free(encoded);
2013-10-28 22:50:05 +01:00
free(decoded);
return 0;
2025-11-24 23:02:13 +01:00
errdz3:
2013-10-28 22:50:05 +01:00
free(decoded);
2025-11-24 23:02:13 +01:00
errdz2:
2013-10-28 22:50:05 +01:00
free(encoded);
2025-11-24 23:02:13 +01:00
errdz1:
2013-10-28 22:50:05 +01:00
return 1;
}