mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-18 12:20:58 +01:00
changed z85 header and comment syntax and parser
This commit is contained in:
@@ -56,8 +56,9 @@ Pcpstream *ps_new_outbuffer() {
|
||||
}
|
||||
|
||||
void ps_setdetermine(Pcpstream *stream, size_t blocksize) {
|
||||
assert(blocksize % 4 == 0);
|
||||
stream->determine = 1;
|
||||
stream->blocksize = blocksize;
|
||||
stream->blocksize = blocksize + (5 - (blocksize % 5));
|
||||
if(stream->cache == NULL) {
|
||||
stream->cache = buffer_new(32, "Pcpstreamcache");
|
||||
stream->next = buffer_new(32, "Pcpstreamcachenext");
|
||||
@@ -65,6 +66,7 @@ void ps_setdetermine(Pcpstream *stream, size_t blocksize) {
|
||||
}
|
||||
|
||||
void ps_armor(Pcpstream *stream, size_t blocksize) {
|
||||
assert(blocksize % 4 == 0);
|
||||
stream->armor = 1;
|
||||
stream->blocksize = blocksize;
|
||||
if(stream->cache == NULL) {
|
||||
@@ -196,6 +198,53 @@ void ps_determine(Pcpstream *stream) {
|
||||
}
|
||||
|
||||
size_t ps_read_decode(Pcpstream *stream, Buffer *cache, void *buf, size_t bufsize) {
|
||||
size_t zdiff = 1;
|
||||
size_t i = 0;
|
||||
uint8_t is_comment = 0;
|
||||
uint8_t c;
|
||||
Buffer *z = buffer_new(32, "ztemp");
|
||||
byte *_buf = buf;
|
||||
|
||||
if(bufsize > 0) {
|
||||
for(i=0; i<bufsize; ++i) {
|
||||
c = _buf[i];
|
||||
is_comment = _parse_zchar(z, c, is_comment);
|
||||
}
|
||||
}
|
||||
|
||||
if(buffer_size(z) < stream->blocksize) {
|
||||
/* blocksize not full, continue with stream source */
|
||||
/* read in bytewise, ignore newlines and add until the block is full */
|
||||
while (buffer_size(z) < stream->blocksize) {
|
||||
if (ps_read_raw(stream, &c, 1) == 1) {
|
||||
is_comment = _parse_zchar(z, c, is_comment);
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* finally, decode it and put into cache */
|
||||
size_t binlen, outlen;
|
||||
unsigned char *bin = pcp_z85_decode(buffer_get_str(z), &binlen);
|
||||
if(bin == NULL) {
|
||||
/* it's not z85 encoded, so threat it as binary */
|
||||
stream->armor = 1;
|
||||
buffer_add_buf(stream->cache, z);
|
||||
outlen = buffer_size(stream->cache);
|
||||
}
|
||||
else {
|
||||
/* yes, successfully decoded it, put into cache */
|
||||
buffer_add(stream->cache, bin, binlen);
|
||||
outlen = binlen;
|
||||
}
|
||||
|
||||
buffer_free(z);
|
||||
|
||||
return outlen;
|
||||
}
|
||||
|
||||
size_t ps_read_decodeOLD(Pcpstream *stream, Buffer *cache, void *buf, size_t bufsize) {
|
||||
size_t zdiff = 1;
|
||||
size_t i = 0;
|
||||
Buffer *z = buffer_new(32, "ztemp");
|
||||
|
||||
66
libpcp/z85.c
66
libpcp/z85.c
@@ -37,6 +37,20 @@ size_t _buffer_is_binary(unsigned char *buf, size_t len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t _parse_zchar(Buffer *z, uint8_t c, uint8_t is_comment) {
|
||||
if(is_comment == 1) {
|
||||
if(c == '~')
|
||||
is_comment = 0;
|
||||
}
|
||||
else {
|
||||
if(c == '~')
|
||||
is_comment = 1;
|
||||
else if(c != '\r' && c != '\n') {
|
||||
buffer_add8(z, c);
|
||||
}
|
||||
}
|
||||
return is_comment;
|
||||
}
|
||||
|
||||
unsigned char *pcp_padfour(unsigned char *src, size_t srclen, size_t *dstlen) {
|
||||
size_t outlen, zerolen;
|
||||
@@ -172,54 +186,12 @@ char *pcp_readz85string(unsigned char *input, size_t bufsize) {
|
||||
}
|
||||
|
||||
Buffer *z = buffer_new(MAXLINE, "z");
|
||||
Buffer *line = buffer_new(MAXLINE, "line");
|
||||
char *oneline;
|
||||
int begin, end;
|
||||
begin = end = 0;
|
||||
uint8_t is_comment = 0;
|
||||
char *out = NULL;
|
||||
|
||||
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;
|
||||
}
|
||||
else {
|
||||
/* an end header */
|
||||
end = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if(strchr(oneline, ' ') != NULL) {
|
||||
/* a comment */
|
||||
buffer_clear(line);
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
/* regular z85 encoded content */
|
||||
buffer_add_buf(z, line);
|
||||
buffer_clear(line);
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* regular line content */
|
||||
buffer_add8(line, input[i]);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
for(i=0; i<bufsize; ++i)
|
||||
is_comment = _parse_zchar(z, input[i], is_comment);
|
||||
|
||||
|
||||
if(buffer_size(z) == 0) {
|
||||
fatal("empty z85 encoded string");
|
||||
@@ -230,13 +202,11 @@ char *pcp_readz85string(unsigned char *input, size_t bufsize) {
|
||||
strncpy(out, buffer_get_str(z), buffer_size(z)+1);
|
||||
|
||||
buffer_free(z);
|
||||
buffer_free(line);
|
||||
|
||||
return out;
|
||||
|
||||
rferr:
|
||||
buffer_free(z);
|
||||
buffer_free(line);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user