mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
splitted pcp_readz85file() and added pcp_readz85string() which makes a better interface
This commit is contained in:
@@ -41,5 +41,6 @@ unsigned char *pcp_z85_decode(char *z85block, size_t *dstlen);
|
|||||||
char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen);
|
char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen);
|
||||||
|
|
||||||
char *pcp_readz85file(FILE *infile);
|
char *pcp_readz85file(FILE *infile);
|
||||||
|
char *pcp_readz85string(unsigned char *input, size_t bufsize);
|
||||||
|
|
||||||
#endif // _HAVE_PCP_Z85_H
|
#endif // _HAVE_PCP_Z85_H
|
||||||
|
|||||||
30
libpcp/z85.c
30
libpcp/z85.c
@@ -91,7 +91,7 @@ unsigned char *pcp_z85_decode(char *z85block, size_t *dstlen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen) {
|
char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen) {
|
||||||
int i, pos, b;
|
int pos, b;
|
||||||
size_t outlen, blocklen, zlen;
|
size_t outlen, blocklen, zlen;
|
||||||
|
|
||||||
// make z85 happy (size % 4)
|
// make z85 happy (size % 4)
|
||||||
@@ -154,15 +154,9 @@ char *pcp_z85_encode(unsigned char *raw, size_t srclen, size_t *dstlen) {
|
|||||||
|
|
||||||
char *pcp_readz85file(FILE *infile) {
|
char *pcp_readz85file(FILE *infile) {
|
||||||
unsigned char *input = NULL;
|
unsigned char *input = NULL;
|
||||||
unsigned char *out = NULL;
|
|
||||||
unsigned char *tmp = NULL;
|
unsigned char *tmp = NULL;
|
||||||
char *ret;
|
|
||||||
char *line;
|
|
||||||
unsigned char byte[1];
|
|
||||||
int i, outsize, lpos, x;
|
|
||||||
size_t bufsize = 0;
|
size_t bufsize = 0;
|
||||||
lpos = outsize = 0;
|
unsigned char byte[1];
|
||||||
size_t MAXLINE = 1024;
|
|
||||||
|
|
||||||
while(!feof(infile)) {
|
while(!feof(infile)) {
|
||||||
if(!fread(&byte, 1, 1, infile))
|
if(!fread(&byte, 1, 1, infile))
|
||||||
@@ -175,11 +169,21 @@ char *pcp_readz85file(FILE *infile) {
|
|||||||
|
|
||||||
if(bufsize == 0) {
|
if(bufsize == 0) {
|
||||||
fatal("Input file is empty!\n");
|
fatal("Input file is empty!\n");
|
||||||
goto rferrx;
|
free(tmp);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
out = ucmalloc(bufsize);
|
return pcp_readz85string(input, bufsize);
|
||||||
line = ucmalloc(MAXLINE);
|
}
|
||||||
|
|
||||||
|
char *pcp_readz85string(unsigned char *input, size_t bufsize) {
|
||||||
|
char *ret;
|
||||||
|
int i, outsize, lpos, x;
|
||||||
|
lpos = outsize = 0;
|
||||||
|
size_t MAXLINE = 1024;
|
||||||
|
|
||||||
|
unsigned char *out = ucmalloc(bufsize);
|
||||||
|
char *line = ucmalloc(MAXLINE);
|
||||||
|
|
||||||
for(i=0; i<bufsize; ++i) {
|
for(i=0; i<bufsize; ++i) {
|
||||||
if(lpos > MAXLINE) {
|
if(lpos > MAXLINE) {
|
||||||
@@ -210,7 +214,6 @@ char *pcp_readz85file(FILE *infile) {
|
|||||||
ret = ucmalloc(outsize+1);
|
ret = ucmalloc(outsize+1);
|
||||||
memcpy(ret, out, outsize+1);
|
memcpy(ret, out, outsize+1);
|
||||||
|
|
||||||
free(tmp);
|
|
||||||
free(out);
|
free(out);
|
||||||
free(line);
|
free(line);
|
||||||
|
|
||||||
@@ -219,7 +222,6 @@ char *pcp_readz85file(FILE *infile) {
|
|||||||
rferr:
|
rferr:
|
||||||
free(out);
|
free(out);
|
||||||
free(line);
|
free(line);
|
||||||
rferrx:
|
|
||||||
free(tmp);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user