fixed decoding and cached read

This commit is contained in:
TLINDEN
2014-02-23 11:03:49 +01:00
parent 33d80dab29
commit 5dd40a1779
4 changed files with 36 additions and 8 deletions

View File

@@ -48,6 +48,7 @@
wrapper around file i/o or buffer i/o. It's used wrapper around file i/o or buffer i/o. It's used
in libpcp/crypto.c (more to follow), so it depends in libpcp/crypto.c (more to follow), so it depends
on the caller if it works on files or on buffers. 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 Lots of refactoring have been done to clear things
out and make the system work with the changes out and make the system work with the changes

View File

@@ -24,7 +24,8 @@
Pcpstream *ps_init(void) { Pcpstream *ps_init(void) {
Pcpstream *stream = ucmalloc(sizeof(Pcpstream)); Pcpstream *stream = ucmalloc(sizeof(Pcpstream));
stream->b = NULL; stream->b = NULL;
stream->cache = buffer_new(32, "Pcpstreamcache"); stream->cache = NULL;
stream->next = NULL;
stream->fd = NULL; stream->fd = NULL;
stream->is_buffer = 0; stream->is_buffer = 0;
stream->eof = 0; stream->eof = 0;
@@ -57,11 +58,19 @@ Pcpstream *ps_new_outbuffer() {
void ps_setdetermine(Pcpstream *stream, size_t blocksize) { void ps_setdetermine(Pcpstream *stream, size_t blocksize) {
stream->determine = 1; stream->determine = 1;
stream->blocksize = blocksize; 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) { void ps_armor(Pcpstream *stream, size_t blocksize) {
stream->armor = 1; stream->armor = 1;
stream->blocksize = blocksize; 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) { 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 fetch (and decode) the next chunk, append it to cache and return from
that */ that */
size_t ps_read_cached(Pcpstream *stream, void *buf, size_t readbytes) { 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 */ /* enough left in current cache */
return buffer_get_chunk(stream->cache, buf, readbytes); 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); size_t got = ps_read_raw(stream, buf, stream->blocksize);
/* check if it's binary or not */ /* 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 */ /* no, it's armored */
stream->armor = 1; stream->armor = 1;
ps_read_decode(stream, stream->cache, buf, got); 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) { 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) { if(stream->is_buffer) {
buffer_clear(stream->b); buffer_clear(stream->b);
free(stream); free(stream);

View File

@@ -20,7 +20,7 @@
# #
AM_CFLAGS = -I../include/pcp -I../src -I../libpcp/scrypt/crypto -Wall -g 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_LDADD = ../libpcp/.libs/libpcp1.a
gencheader_SOURCES = gencheader.c gencheader_SOURCES = gencheader.c

View File

@@ -10,30 +10,41 @@ int main() {
unsigned char clear[8] = "ABCDEFGH"; unsigned char clear[8] = "ABCDEFGH";
unsigned char key[8] = "IxD8Lq1K"; unsigned char key[8] = "IxD8Lq1K";
unsigned char crypt[8] = {0}; unsigned char crypt[8] = {0};
int blocks = 8; int blocks = 48;
int i = 0; int i = 0;
if((out = fopen("teststream.out", "wb+")) == NULL) { if((out = fopen("teststream.out", "wb+")) == NULL) {
fprintf(stderr, "oops, could not open file!\n"); fprintf(stderr, "oops, could not open file!\n");
return 1; 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<blocks; i++) { for(i=0; i<blocks; i++) {
memcpy(crypt, clear, 8); memcpy(crypt, clear, 8);
_xorbuf(key, crypt, 8); _xorbuf(key, crypt, 8);
ps_write(pout, crypt, 8); ps_write(pout, crypt, 8);
} }
/* done, put cached buffers out and close */
ps_finish(pout);
ps_close(pout); ps_close(pout);
fclose(out); fclose(out);
/* read it in again using an in and an out stream */ /* read it in again using an input stream */
if((in = fopen("teststream.out", "rb")) == NULL) { if((in = fopen("teststream.out", "rb")) == NULL) {
fprintf(stderr, "oops, could not open file!\n"); fprintf(stderr, "oops, could not open file!\n");
return 1; return 1;
} }
Pcpstream *pin = ps_new_file(in); Pcpstream *pin = ps_new_file(in);
/* enable autmatically encoding detection.
set blocksize to 10, because:
8 + (5 - (8 % 5)) == 10 */
ps_setdetermine(pin, 10);
/* we'll use this stream to put the "decrypted" data in. /* we'll use this stream to put the "decrypted" data in.
note, that this could be a file as well. */ note, that this could be a file as well. */
@@ -44,6 +55,7 @@ int main() {
ps_read(pin, crypt, 8); ps_read(pin, crypt, 8);
_xorbuf(key, crypt, 8); _xorbuf(key, crypt, 8);
ps_write(pclear, crypt, 8); ps_write(pclear, crypt, 8);
memset(crypt,0,8);
} }
ps_close(pin); ps_close(pin);
fclose(in); fclose(in);