diff --git a/TODO b/TODO index 312887d..2af7ca5 100644 --- a/TODO +++ b/TODO @@ -25,19 +25,10 @@ Unitttests: ok 28 - check-crypto-unencrypted-secret-message public key exported. <=== ??? - - - sometimes secret key is empty -libpcp/crypto.c: the stream versions must return the output streams, not close them - - - -crypto.c: In function 'pcp_encrypt_stream': -crypto.c:379: warning: assignment makes integer from pointer without a cast -crypto.c:383: warning: assignment makes integer from pointer without a cast - +cpptest test3 fails, pcpstream backed with buffer behaves komisch Python binding, e.g.: diff --git a/include/pcp.h b/include/pcp.h index fb0d6c6..8b16eae 100644 --- a/include/pcp.h +++ b/include/pcp.h @@ -8,7 +8,6 @@ extern "C" { #include "pcp/config.h" #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/include/pcp/pcpstream.h b/include/pcp/pcpstream.h index ea1749d..d2f9f57 100644 --- a/include/pcp/pcpstream.h +++ b/include/pcp/pcpstream.h @@ -34,6 +34,7 @@ #ifndef HAVE_PCP_PCPSTEAM_H #define HAVE_PCP_PCPSTEAM_H +#include #include #include "mem.h" #include "util.h" @@ -74,9 +75,15 @@ size_t ps_read(Pcpstream *stream, void *buf, size_t readbytes); sets eof and err respectively as ps_read() does. */ size_t ps_write(Pcpstream *stream, void *buf, size_t writebytes); +/* use fprintf() style format string to print something out */ +size_t ps_print(Pcpstream *stream, const char * fmt, ...); + /* return the current read or write offset */ size_t ps_tell(Pcpstream *stream); +/* return a pointer to the internal buffer object */ +Buffer *ps_buffer(Pcpstream *stream); + /* closes the stream and frees allocated memory, if present */ void ps_close(Pcpstream *stream); diff --git a/libpcp/buffer.c b/libpcp/buffer.c index 59955c5..571ab35 100644 --- a/libpcp/buffer.c +++ b/libpcp/buffer.c @@ -21,8 +21,6 @@ #include "buffer.h" - - Buffer *buffer_new(size_t blocksize, char *name) { Buffer *b = ucmalloc(sizeof(Buffer)); b->buf = ucmalloc(blocksize); diff --git a/libpcp/pcpstream.c b/libpcp/pcpstream.c index 65fb770..436919a 100644 --- a/libpcp/pcpstream.c +++ b/libpcp/pcpstream.c @@ -55,6 +55,10 @@ size_t ps_read(Pcpstream *stream, void *buf, size_t readbytes) { size_t gotbytes = 0; if(stream->is_buffer) { + /* check if there's enough space in our buffer */ + if(buffer_left(stream->b) < readbytes) + readbytes = buffer_left(stream->b); + gotbytes = buffer_get_chunk(stream->b, buf, readbytes); if(gotbytes == 0) { /* this should not happen with buffers */ @@ -91,6 +95,23 @@ size_t ps_write(Pcpstream *stream, void *buf, size_t writebytes) { return writebytes; } +size_t ps_print(Pcpstream *stream, const char * fmt, ...) { + va_list ap; + char *dst; + va_start(ap, fmt); + vasprintf(&dst, fmt, ap); + va_end(ap); + size_t len = strlen(dst); + + if(stream->is_buffer) { + buffer_add(stream->b, dst, len); + return len; + } + else { + return ps_write(stream, dst, len); + } +} + void ps_close(Pcpstream *stream) { if(stream->is_buffer) { buffer_clear(stream->b); @@ -123,3 +144,7 @@ size_t ps_tell(Pcpstream *stream) { return ftell(stream->fd); } } + +Buffer *ps_buffer(Pcpstream *stream) { + return stream->b; +}