From 5dd40a1779cf6ec458ca2cb2fed8300ebb404e3b Mon Sep 17 00:00:00 2001 From: TLINDEN Date: Sun, 23 Feb 2014 11:03:49 +0100 Subject: [PATCH] fixed decoding and cached read --- ChangeLog | 1 + libpcp/pcpstream.c | 21 ++++++++++++++++++--- tests/Makefile.am | 2 +- tests/streamtest.c | 20 ++++++++++++++++---- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index f271d19..7e39ba8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -48,6 +48,7 @@ wrapper around file i/o or buffer i/o. It's used in libpcp/crypto.c (more to follow), so it depends on the caller if it works on files or on buffers. + Pcpstreams also automatically encode/decode Z85. Lots of refactoring have been done to clear things out and make the system work with the changes diff --git a/libpcp/pcpstream.c b/libpcp/pcpstream.c index eefbce6..7fcecbd 100644 --- a/libpcp/pcpstream.c +++ b/libpcp/pcpstream.c @@ -24,7 +24,8 @@ Pcpstream *ps_init(void) { Pcpstream *stream = ucmalloc(sizeof(Pcpstream)); stream->b = NULL; - stream->cache = buffer_new(32, "Pcpstreamcache"); + stream->cache = NULL; + stream->next = NULL; stream->fd = NULL; stream->is_buffer = 0; stream->eof = 0; @@ -57,11 +58,19 @@ Pcpstream *ps_new_outbuffer() { void ps_setdetermine(Pcpstream *stream, size_t blocksize) { stream->determine = 1; stream->blocksize = blocksize; + if(stream->cache == NULL) { + stream->cache = buffer_new(32, "Pcpstreamcache"); + stream->next = buffer_new(32, "Pcpstreamcachenext"); + } } void ps_armor(Pcpstream *stream, size_t blocksize) { stream->armor = 1; stream->blocksize = blocksize; + if(stream->cache == NULL) { + stream->cache = buffer_new(32, "Pcpstreamcache"); + stream->next = buffer_new(32, "Pcpstreamcachenext"); + } } size_t ps_read_raw(Pcpstream *stream, void *buf, size_t readbytes) { @@ -97,7 +106,7 @@ size_t ps_read_raw(Pcpstream *stream, void *buf, size_t readbytes) { fetch (and decode) the next chunk, append it to cache and return from that */ size_t ps_read_cached(Pcpstream *stream, void *buf, size_t readbytes) { - if(buffer_left(stream->cache) <= readbytes) { + if(buffer_left(stream->cache) <= readbytes && buffer_left(stream->cache) > 0) { /* enough left in current cache */ return buffer_get_chunk(stream->cache, buf, readbytes); } @@ -175,7 +184,7 @@ void ps_determine(Pcpstream *stream) { size_t got = ps_read_raw(stream, buf, stream->blocksize); /* check if it's binary or not */ - if(_buffer_is_binary(buf, got) != 0) { + if(_buffer_is_binary(buf, got) == 0) { /* no, it's armored */ stream->armor = 1; ps_read_decode(stream, stream->cache, buf, got); @@ -364,6 +373,12 @@ size_t ps_print(Pcpstream *stream, const char * fmt, ...) { } void ps_close(Pcpstream *stream) { + if(stream->cache != NULL) + buffer_free(stream->cache); + + if(stream->next != NULL) + buffer_free(stream->next); + if(stream->is_buffer) { buffer_clear(stream->b); free(stream); diff --git a/tests/Makefile.am b/tests/Makefile.am index ddad80d..80bc842 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -20,7 +20,7 @@ # AM_CFLAGS = -I../include/pcp -I../src -I../libpcp/scrypt/crypto -Wall -g -check_PROGRAMS = col invalidkeys pwhashes gencheader statictest cpptest buffertest sample +check_PROGRAMS = col invalidkeys pwhashes gencheader statictest cpptest buffertest sample streamtest gencheader_LDADD = ../libpcp/.libs/libpcp1.a gencheader_SOURCES = gencheader.c diff --git a/tests/streamtest.c b/tests/streamtest.c index ff8a694..36fcb62 100644 --- a/tests/streamtest.c +++ b/tests/streamtest.c @@ -10,30 +10,41 @@ int main() { unsigned char clear[8] = "ABCDEFGH"; unsigned char key[8] = "IxD8Lq1K"; unsigned char crypt[8] = {0}; - int blocks = 8; + int blocks = 48; int i = 0; if((out = fopen("teststream.out", "wb+")) == NULL) { fprintf(stderr, "oops, could not open file!\n"); return 1; } - Pcpstream *pout = ps_new_file(out); - /* "encrypt" a couple of times into the file */ + /* out output stream, z85 encoded, use z85 blocksize 8 */ + Pcpstream *pout = ps_new_file(out); + ps_armor(pout, 8); + + /* "encrypt" a couple of times into the output stream */ for(i=0; i