diff --git a/include/pcp.h b/include/pcp.h index a685285..79325d7 100644 --- a/include/pcp.h +++ b/include/pcp.h @@ -7,6 +7,7 @@ extern "C" { #include "pcp/base85.h" #include "pcp/buffer.h" +#include "pcp/config.h" #include "pcp/crypto.h" #include "pcp/defines.h" #include "pcp/digital_crc32.h" diff --git a/libpcp/buffer.c b/libpcp/buffer.c index 990a3c3..a874062 100644 --- a/libpcp/buffer.c +++ b/libpcp/buffer.c @@ -68,7 +68,6 @@ void buffer_resize(Buffer *b, size_t len) { if((b->end > 0 && b->end + len > b->size) || (b->end == 0 && len > b->size) ) { /* increase by buf blocksize */ size_t newsize = (((len / b->blocksize) +1) * b->blocksize) + b->size; - fprintf(stderr, "[buffer %s] resizing from %ld to %ld\n", b->name, b->size, newsize); b->buf = ucrealloc(b->buf, b->size, newsize); b->size = newsize; } diff --git a/libpcp/z85.c b/libpcp/z85.c index 250436b..d2b2d8b 100644 --- a/libpcp/z85.c +++ b/libpcp/z85.c @@ -73,10 +73,21 @@ unsigned char *pcp_z85_decode(char *z85block, size_t *dstlen) { } } + binlen = strlen (z85) * 4 / 5; bin = ucmalloc(binlen); bin = zmq_z85_decode(bin, z85); + if(bin == NULL) { + free(z85); + fatal("zmq_z85_decode() failed, input size ! % 5"); + return NULL; + } + + _dump("bin", bin, binlen); + + fwrite(bin, 1, binlen, stderr); + unsigned char *raw = NULL; if(bin != NULL) { raw = pcp_unpadfour(bin, binlen, &outlen); @@ -224,7 +235,7 @@ char *pcp_readz85string(unsigned char *input, size_t bufsize) { } } - if(buffer_size(line) > 0) { + 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); @@ -238,8 +249,6 @@ char *pcp_readz85string(unsigned char *input, size_t bufsize) { out = ucmalloc(buffer_size(z)+1); strncpy(out, buffer_get_str(z), buffer_size(z)+1); - fprintf(stderr, "got: \n<%s>\n", out); - buffer_free(z); buffer_free(line); diff --git a/man/details.pod b/man/details.pod index 63292a7..fc6c77b 100644 --- a/man/details.pod +++ b/man/details.pod @@ -561,35 +561,24 @@ a couple of modifications (portability, readability etc). To fulfil the requirements of the ZeroMQ Z85 functions, B does some additional preparations of raw input before actually doing the -encoding, since the input for zmq_z85_encode() must be divisible by 4: - -Expand the input so that the resulting size is divisible by 4. - -Fill the added bytes with zeroes. - -Prepend the input with a one byte value which holds the number of zeroes -added in the previous step. - -Example: - -Raw input: - - hello\0 - -Here, the input size is 6, which is insufficient, therefore it has to be expanded -to be 8. After the process the input looks like this: - - 1hello\0\0 - -So, we padded the input with 1 zero (makes 7 bytes) and preprended it with the -value 1 (the number of zeros added): makes 8 bytes total. - -After decoding Z85 input the process will be reversed. +encoding, since the input for zmq_z85_encode() must be divisible by 4. Therefore +we pad the input with zeroes and remove them after decoding. B. +Z85 encoding and decoding can be used separately as well to work with +files. Examples: + +Encode some file to Z85 encoding: + +pcp1 -z -I file -O file.z85 + +Reverse the process: + +pcp1 -Z -I file.z85 -O file + =head2 PBP COMPATIBILITY PCP tries to be fully compatible with PBP (https://github.com/stef/pbp). Encrypted diff --git a/man/options.pod b/man/options.pod index c378ae1..051726d 100644 --- a/man/options.pod +++ b/man/options.pod @@ -107,11 +107,13 @@ against using -I as well (plus -f ). Encoding Options: - -z --z85-encode Encode something to Z85 encoding. Use - -I and -O respectively, otherwise it + -z --z85-encode Encode (armor) something to Z85 encoding. + If used with encryption or singing operation + encode its output. Otherwise encode a plain + file. Use -I and -O respectively, otherwise it stdin/stdout. - -Z --z85-decode Decode something from Z85 encoding. Use - -I and -O respectively, otherwise it + -Z --z85-decode Decode (dearmor) something from Z85 encoding. + Use -I and -O respectively, otherwise it stdin/stdout diff --git a/man/pcp1.pod b/man/pcp1.pod index 5e71505..2a59a42 100644 --- a/man/pcp1.pod +++ b/man/pcp1.pod @@ -162,11 +162,13 @@ Pretty Curved Privacy - File encryption using eliptic curve cryptography. against using -I as well (plus -f ). Encoding Options: - -z --z85-encode Encode something to Z85 encoding. Use - -I and -O respectively, otherwise it + -z --z85-encode Encode (armor) something to Z85 encoding. + If used with encryption or singing operation + encode its output. Otherwise encode a plain + file. Use -I and -O respectively, otherwise it stdin/stdout. - -Z --z85-decode Decode something from Z85 encoding. Use - -I and -O respectively, otherwise it + -Z --z85-decode Decode (dearmor) something from Z85 encoding. + Use -I and -O respectively, otherwise it stdin/stdout @@ -805,35 +807,24 @@ a couple of modifications (portability, readability etc). To fulfil the requirements of the ZeroMQ Z85 functions, B does some additional preparations of raw input before actually doing the -encoding, since the input for zmq_z85_encode() must be divisible by 4: - -Expand the input so that the resulting size is divisible by 4. - -Fill the added bytes with zeroes. - -Prepend the input with a one byte value which holds the number of zeroes -added in the previous step. - -Example: - -Raw input: - - hello\0 - -Here, the input size is 6, which is insufficient, therefore it has to be expanded -to be 8. After the process the input looks like this: - - 1hello\0\0 - -So, we padded the input with 1 zero (makes 7 bytes) and preprended it with the -value 1 (the number of zeros added): makes 8 bytes total. - -After decoding Z85 input the process will be reversed. +encoding, since the input for zmq_z85_encode() must be divisible by 4. Therefore +we pad the input with zeroes and remove them after decoding. B. +Z85 encoding and decoding can be used separately as well to work with +files. Examples: + +Encode some file to Z85 encoding: + +pcp1 -z -I file -O file.z85 + +Reverse the process: + +pcp1 -Z -I file.z85 -O file + =head2 PBP COMPATIBILITY PCP tries to be fully compatible with PBP (https://github.com/stef/pbp). Encrypted diff --git a/src/pcp.c b/src/pcp.c index 489190b..46ef8a8 100644 --- a/src/pcp.c +++ b/src/pcp.c @@ -183,7 +183,7 @@ int main (int argc, char **argv) { armor = 1; break; case 'Z': - armor = 1; + armor = 2; break; case 'b': pbpcompat = 1; @@ -248,8 +248,17 @@ int main (int argc, char **argv) { if(mode == 0) { - version(); - return 1; + /* turn -z|-Z into a mode if there's nothing else specified */ + if(armor == 1) { + mode = PCP_MODE_ZENCODE; + } + else if(armor == 2) { + mode = PCP_MODE_ZDECODE; + } + else { + version(); + return 1; + } } if(mode == PCP_MODE_ENCRYPT + PCP_MODE_SIGN) { diff --git a/src/z85util.c b/src/z85util.c index f6f94db..799dd5e 100644 --- a/src/z85util.c +++ b/src/z85util.c @@ -117,6 +117,8 @@ int pcpz85_decode(char *infile, char *outfile) { size_t clen; unsigned char *decoded = pcp_z85_decode(encoded, &clen); + + if(decoded == NULL) goto errdz2;