From ffe31a3582c4b65084ad7bfb0a5833b5ac83d58d Mon Sep 17 00:00:00 2001 From: Thomas White Date: Thu, 20 Sep 2018 16:36:03 +0200 Subject: Allow cbf.gz buffer to grow if the file is large --- libcrystfel/src/image.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/libcrystfel/src/image.c b/libcrystfel/src/image.c index a51f415a..cbefae4f 100644 --- a/libcrystfel/src/image.c +++ b/libcrystfel/src/image.c @@ -51,9 +51,6 @@ #include "hdf5-file.h" #include "detector.h" -/* Maximum size of uncompressed CBF file in gzip format */ -#define GZIP_BUFFER_SIZE (16*1024*1024) - /** * SECTION:image * @short_description: Data structure representing an image @@ -647,7 +644,9 @@ static float *read_cbf_data(struct imagefile *f, int *w, int *h, cbf_handle *pcb } else if ( f->type == IMAGEFILE_CBFGZ ) { gzFile gzfh; - size_t len; + size_t len, len_read; + const size_t bufinc = 8*1024*1024; /* Allocate buffer in 8Mb chunks */ + size_t bufsz = bufinc; gzfh = gzopen(f->filename, "rb"); if ( gzfh == NULL ) return NULL; @@ -655,11 +654,23 @@ static float *read_cbf_data(struct imagefile *f, int *w, int *h, cbf_handle *pcb /* Set larger buffer size for hopefully faster uncompression */ gzbuffer(gzfh, 128*1024); - buf = malloc(GZIP_BUFFER_SIZE); + buf = malloc(bufsz); if ( buf == NULL ) return NULL; - len = gzread(gzfh, buf, GZIP_BUFFER_SIZE); - if ( len == -1 ) return NULL; + len = 0; + do { + + len_read = gzread(gzfh, buf+len, bufinc); + if ( len_read == -1 ) return NULL; + len += len_read; + + if ( len_read == bufinc ) { + bufsz += bufinc; + buf = realloc(buf, bufsz); + if ( buf == NULL ) return NULL; + } + + } while ( len_read == bufinc ); fh = fmemopen(buf, len, "rb"); if ( fh == NULL ) return NULL; -- cgit v1.2.3