diff options
author | tlatorre <tlatorre@uchicago.edu> | 2019-06-25 14:56:35 -0500 |
---|---|---|
committer | tlatorre <tlatorre@uchicago.edu> | 2019-06-25 14:56:35 -0500 |
commit | d4cb7e14b54e4f4510fd73d1b3e219eaaffbe081 (patch) | |
tree | 595428a39e909066dcb5186fb8b2b8207ce2ab7e /src/zebra.c | |
parent | 42782c7e5bd4f1a0475cbdc455bd2f7917e066a0 (diff) | |
download | sddm-d4cb7e14b54e4f4510fd73d1b3e219eaaffbe081.tar.gz sddm-d4cb7e14b54e4f4510fd73d1b3e219eaaffbe081.tar.bz2 sddm-d4cb7e14b54e4f4510fd73d1b3e219eaaffbe081.zip |
add ability to read gzipped zdab files
Diffstat (limited to 'src/zebra.c')
-rw-r--r-- | src/zebra.c | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/src/zebra.c b/src/zebra.c index 690294b..1289318 100644 --- a/src/zebra.c +++ b/src/zebra.c @@ -21,16 +21,25 @@ #include <string.h> /* for memmove() */ #include <stdint.h> /* for uint8_t, etc. */ #include <errno.h> /* for strerror(), etc. */ +#include <zlib.h> char zebra_err[256]; +/* Returns a pointer to a new zebra file object. + * + * Returns the pointer to the zebraFile object on success, or NULL on error. */ zebraFile *zebra_open(const char *filename) { - /* Returns a pointer to a new zebra file object. + /* We use gzopen() here even though the file may not be gzipped. According + * to the docs: + * + * gzopen can be used to read a file which is not in gzip format; in + * this case gzread will directly read from the file without + * decompression. When reading, this will be detected automatically + * by looking for the magic two-byte gzip header. * - * Returns the pointer to the zebraFile object on success, or NULL on - * error. */ - FILE *f = fopen(filename, "r"); + * See https://zlib.net/manual.html. */ + gzFile f = gzopen(filename, "r"); if (!f) { sprintf(zebra_err, "failed to open '%s': %s", filename, strerror(errno)); @@ -41,7 +50,7 @@ zebraFile *zebra_open(const char *filename) if (!z) { sprintf(zebra_err, "failed to allocate space for zebraFile struct: %s", strerror(errno)); - fclose(f); + gzclose(f); return NULL; } @@ -64,12 +73,12 @@ static int read_next_physical_record(zebraFile *z) size_t nbytes; uint32_t s0, s1, s2, s3, nwphr, nfast; - nbytes = fread(buf,4,8,z->f); + nbytes = gzread(z->f,buf,4*8); if (nbytes == 0) return 1; - if (nbytes != 8) { - sprintf(zebra_err, "expected %i words but only read %zu", 8, nbytes); + if (nbytes != 8*4) { + sprintf(zebra_err, "expected %i bytes but only read %zu", 8*4, nbytes); return -1; } @@ -109,12 +118,12 @@ static int read_next_physical_record(zebraFile *z) z->buf = realloc(z->buf, z->buf_size+words*4); - nbytes = fread(z->buf+z->buf_size, 4, words, z->f); + nbytes = gzread(z->f, z->buf+z->buf_size, 4*words); z->buf_size += words*4; - if (nbytes != words) { - sprintf(zebra_err, "expected %i words but only read %zu", words, nbytes); + if (nbytes != words*4) { + sprintf(zebra_err, "expected %i bytes but only read %zu", words*4, nbytes); return -1; } @@ -383,7 +392,7 @@ int zebra_get_bank(zebraFile *z, zebraBank *b, uint32_t link) void zebra_close(zebraFile *z) { - fclose(z->f); + gzclose(z->f); if (z->tab) free(z->tab); if (z->buf) free(z->buf); free(z); |