Skip to content

Instantly share code, notes, and snippets.

@dstndstn
Created March 7, 2014 01:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstndstn/9403603 to your computer and use it in GitHub Desktop.
Save dstndstn/9403603 to your computer and use it in GitHub Desktop.
diff --git a/drvrfile.c b/drvrfile.c
index fd89233..2e9890f 100644
--- a/drvrfile.c
+++ b/drvrfile.c
@@ -772,28 +772,33 @@ int file_is_compressed(char *filename) /* I - FITS file name */
strcat(filename,".gz");
if (file_openfile(filename, 0, &diskfile))
{
- strcpy(filename, tmpfilename);
- strcat(filename,".Z");
+ strcpy(tmpfilename,filename);
+ strcat(filename,".bz2");
if (file_openfile(filename, 0, &diskfile))
{
strcpy(filename, tmpfilename);
- strcat(filename,".z"); /* it's often lower case on CDROMs */
+ strcat(filename,".Z");
if (file_openfile(filename, 0, &diskfile))
{
strcpy(filename, tmpfilename);
- strcat(filename,".zip");
+ strcat(filename,".z"); /* it's often lower case on CDROMs */
if (file_openfile(filename, 0, &diskfile))
{
strcpy(filename, tmpfilename);
- strcat(filename,"-z"); /* VMS suffix */
+ strcat(filename,".zip");
if (file_openfile(filename, 0, &diskfile))
{
strcpy(filename, tmpfilename);
- strcat(filename,"-gz"); /* VMS suffix */
+ strcat(filename,"-z"); /* VMS suffix */
if (file_openfile(filename, 0, &diskfile))
{
- strcpy(filename,tmpfilename); /* restore original name */
- return(0); /* file not found */
+ strcpy(filename, tmpfilename);
+ strcat(filename,"-gz"); /* VMS suffix */
+ if (file_openfile(filename, 0, &diskfile))
+ {
+ strcpy(filename,tmpfilename); /* restore original name */
+ return(0); /* file not found */
+ }
}
}
}
@@ -815,7 +820,8 @@ int file_is_compressed(char *filename) /* I - FITS file name */
(memcmp(buffer, "\120\113", 2) == 0) || /* PKZIP */
(memcmp(buffer, "\037\036", 2) == 0) || /* PACK */
(memcmp(buffer, "\037\235", 2) == 0) || /* LZW */
- (memcmp(buffer, "\037\240", 2) == 0) ) /* LZH */
+ (memcmp(buffer, "\037\240", 2) == 0) || /* LZH */
+ (memcmp(buffer, "BZ", 2) == 0) ) /* BZip2 */
{
return(1); /* this is a compressed file */
}
diff --git a/drvrmem.c b/drvrmem.c
index 6bf94d2..9ede799 100644
--- a/drvrmem.c
+++ b/drvrmem.c
@@ -9,6 +9,8 @@
#include <stddef.h> /* apparently needed to define size_t */
#include "fitsio2.h"
+#include "bzlib.h"
+
/* prototype for .Z file uncompression function in zuncompress.c */
int zuncompress2mem(char *filename,
FILE *diskfile,
@@ -655,6 +657,8 @@ int mem_compress_open(char *filename, int rwmode, int *hdl)
finalsize = 0; /* for most methods we can't determine final size */
else if (memcmp(buffer, "\037\240", 2) == 0) /* LZH */
finalsize = 0; /* for most methods we can't determine final size */
+ else if (memcmp(buffer, "BZ", 2) == 0) /* BZip2 */
+ finalsize = 0; /* for most methods we can't determine final size */
else
{
/* not a compressed file; this should never happen */
@@ -1060,6 +1064,50 @@ int mem_uncompress2mem(char *filename, FILE *diskfile, int hdl)
memTable[hdl].memsizeptr, /* pointer to size of memory */
realloc, /* reallocation function */
&finalsize, &status); /* returned file size nd status*/
+ } else if (strstr(filename, ".bz2")) {
+ BZFILE* b;
+ char buf[8192];
+ int bzerror;
+ // we read from the bzip stream into "buf" and then copy into
+ // the real memory-file buffer at this "offset". Could do
+ // this in one shot, with somewhat more memory-file
+ // book-keeping. Do it the easy way instead, for now.
+ size_t offset = 0;
+
+ b = BZ2_bzReadOpen(&bzerror, diskfile, 0, 0, NULL, 0);
+ if (bzerror != BZ_OK) {
+ BZ2_bzReadClose(&bzerror, b);
+ ffpmsg("failed to bzReadOpen a bzip2 file\n");
+ return 1;
+ }
+ bzerror = BZ_OK;
+ while (bzerror == BZ_OK) {
+ int nread;
+ nread = BZ2_bzRead(&bzerror, b, buf, sizeof(buf));
+ if (bzerror == BZ_OK || bzerror == BZ_STREAM_END) {
+ char** ptrptr = memTable[hdl].memaddrptr;
+ size_t sz = *(memTable[hdl].memsizeptr);
+ if (offset + nread > sz) {
+ // realloc the memory file 50% larger
+ size_t newsize = sz + sz/2;
+ *ptrptr = realloc(*ptrptr, newsize);
+ if (*ptrptr == NULL) {
+ BZ2_bzReadClose(&bzerror, b);
+ ffpmsg("failed to realloc uncompressing bzip2");
+ return 1;
+ }
+ *(memTable[hdl].memsizeptr) = newsize;
+ }
+ memcpy(*ptrptr + offset, buf, nread);
+ offset += nread;
+ }
+ }
+ BZ2_bzReadClose(&bzerror, b);
+ if (bzerror != BZ_STREAM_END) {
+ ffpmsg("failure reading bz2 file\n");
+ return 1;
+ }
+ finalsize = offset;
} else {
uncompress2mem(filename, diskfile,
memTable[hdl].memaddrptr, /* pointer to memory address */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment