X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fzip-reader.c;h=00758c5faae19bf957343af185466b89c000a1dd;hb=00aad3b983774328140a04436d7d6ae7925fec97;hp=bee7e2f240ff2affad679c5a50f1bef916d638a6;hpb=32ee0e0402d6d56674f53a47d879ec5c07dabe09;p=pspp diff --git a/src/libpspp/zip-reader.c b/src/libpspp/zip-reader.c index bee7e2f240..00758c5faa 100644 --- a/src/libpspp/zip-reader.c +++ b/src/libpspp/zip-reader.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2011 Free Software Foundation, Inc. + Copyright (C) 2011, 2013, 2014 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -25,7 +25,9 @@ #include #include #include +#include +#include #include #include "inflate.h" @@ -64,9 +66,7 @@ stored_finish (struct zip_member *zm UNUSED) static struct decompressor decompressors[n_COMPRESSION] = { {stored_init, stored_read, stored_finish}, -#if HAVE_ZLIB_H {inflate_init, inflate_read, inflate_finish} -#endif }; static enum compression @@ -78,11 +78,9 @@ comp_code (struct zip_member *zm, uint16_t c) case 0: which = COMPRESSION_STORED; break; -#if HAVE_ZLIB_H case 8: which = COMPRESSION_INFLATE; break; -#endif default: ds_put_format (zm->errs, _("Unsupported compression type (%d)"), c); which = n_COMPRESSION; @@ -151,18 +149,50 @@ skip_bytes (FILE *f, size_t n) fseeko (f, n, SEEK_CUR); } +static bool get_bytes (FILE *f, void *x, size_t n) WARN_UNUSED_RESULT; + + /* Read N bytes from F, storing the result in X */ -static void -get_bytes (FILE *f, void *x, size_t n) +static bool +get_bytes (FILE *f, void *x, size_t n) { - fread (x, 1, n, f); + return (n == fread (x, 1, n, f)); } +static bool get_u32 (FILE *f, uint32_t *v) WARN_UNUSED_RESULT; + + /* Read a 32 bit value from F */ -static void -get_u32 (FILE *f, uint32_t *x) +static bool +get_u32 (FILE *f, uint32_t *v) { - get_bytes (f, x, sizeof *x); + uint32_t x; + if (!get_bytes (f, &x, sizeof x)) + return false; +#ifdef WORDS_BIGENDIAN + *v = bswap_32 (x); +#else + *v = x; +#endif + return true; +} + +static bool get_u16 (FILE *f, uint16_t *v) WARN_UNUSED_RESULT; + + +/* Read a 16 bit value from F */ +static bool +get_u16 (FILE *f, uint16_t *v) +{ + uint16_t x; + if (!get_bytes (f, &x, sizeof x)) + return false; +#ifdef WORDS_BIGENDIAN + *v = bswap_16 (x); +#else + *v = x; +#endif + return true; } @@ -173,7 +203,7 @@ check_magic (FILE *f, uint32_t expected, struct string *err) { uint32_t magic; - get_u32 (f, &magic); + if (! get_u32 (f, &magic)) return false; if ((expected != magic)) { @@ -187,13 +217,6 @@ check_magic (FILE *f, uint32_t expected, struct string *err) } -/* Read a 16 bit value from F */ -static void -get_u16 (FILE *f, uint16_t *x) -{ - get_bytes (f, x, sizeof *x); -} - /* Reads upto BYTES bytes from ZM and puts them in BUF. Returns the number of bytes read, or -1 on error */ int @@ -241,35 +264,35 @@ zip_header_read_next (struct zip_reader *zr) if ( ! check_magic (zr->fr, MAGIC_SOCD, zr->errs)) return NULL; - get_u16 (zr->fr, &v); + if (! get_u16 (zr->fr, &v)) return NULL; - get_u16 (zr->fr, &v); - get_u16 (zr->fr, &gp); - get_u16 (zr->fr, &comp_type); + if (! get_u16 (zr->fr, &v)) return NULL; + if (! get_u16 (zr->fr, &gp)) return NULL; + if (! get_u16 (zr->fr, &comp_type)) return NULL; zm->compression = comp_code (zm, comp_type); - get_u16 (zr->fr, &time); - get_u16 (zr->fr, &date); - get_u32 (zr->fr, &zm->expected_crc); - get_u32 (zr->fr, &zm->comp_size); - get_u32 (zr->fr, &zm->ucomp_size); - get_u16 (zr->fr, &nlen); - get_u16 (zr->fr, &extralen); - get_u16 (zr->fr, &clen); - get_u16 (zr->fr, &diskstart); - get_u16 (zr->fr, &iattr); - get_u32 (zr->fr, &eattr); - get_u32 (zr->fr, &zm->offset); - - zm->name = calloc (nlen + 1, 1); - get_bytes (zr->fr, zm->name, nlen); + if (! get_u16 (zr->fr, &time)) return NULL; + if (! get_u16 (zr->fr, &date)) return NULL; + if (! get_u32 (zr->fr, &zm->expected_crc)) return NULL; + if (! get_u32 (zr->fr, &zm->comp_size)) return NULL; + if (! get_u32 (zr->fr, &zm->ucomp_size)) return NULL; + if (! get_u16 (zr->fr, &nlen)) return NULL; + if (! get_u16 (zr->fr, &extralen)) return NULL; + if (! get_u16 (zr->fr, &clen)) return NULL; + if (! get_u16 (zr->fr, &diskstart)) return NULL; + if (! get_u16 (zr->fr, &iattr)) return NULL; + if (! get_u32 (zr->fr, &eattr)) return NULL; + if (! get_u32 (zr->fr, &zm->offset)) return NULL; + + zm->name = xzalloc (nlen + 1); + if (! get_bytes (zr->fr, zm->name, nlen)) return NULL; skip_bytes (zr->fr, extralen); zr->members[zr->nm++] = zm; - zm->fp = fopen (zr->filename, "r"); + zm->fp = fopen (zr->filename, "rb"); zm->ref_cnt = 1; zm->errs = zr->errs; @@ -285,14 +308,14 @@ zip_reader_create (const char *filename, struct string *errs) off_t offset = 0; uint32_t central_dir_start, central_dir_length; - struct zip_reader *zr = malloc (sizeof *zr); + struct zip_reader *zr = xzalloc (sizeof *zr); zr->errs = errs; if ( zr->errs) ds_init_empty (zr->errs); zr->nm = 0; - zr->fr = fopen (filename, "r"); + zr->fr = fopen (filename, "rb"); if (NULL == zr->fr) { ds_put_cstr (zr->errs, strerror (errno)); @@ -332,14 +355,14 @@ zip_reader_create (const char *filename, struct string *errs) return NULL; } - get_u16 (zr->fr, &disknum); - get_u16 (zr->fr, &disknum); + if (! get_u16 (zr->fr, &disknum)) return NULL; + if (! get_u16 (zr->fr, &disknum)) return NULL; - get_u16 (zr->fr, &zr->n_members); - get_u16 (zr->fr, &total_members); + if (! get_u16 (zr->fr, &zr->n_members)) return NULL; + if (! get_u16 (zr->fr, &total_members)) return NULL; - get_u32 (zr->fr, ¢ral_dir_length); - get_u32 (zr->fr, ¢ral_dir_start); + if (! get_u32 (zr->fr, ¢ral_dir_length)) return NULL; + if (! get_u32 (zr->fr, ¢ral_dir_start)) return NULL; if ( 0 != fseeko (zr->fr, central_dir_start, SEEK_SET)) { @@ -350,7 +373,8 @@ zip_reader_create (const char *filename, struct string *errs) return NULL; } - zr->members = calloc (zr->n_members, sizeof (*zr->members)); + zr->members = xcalloc (zr->n_members, sizeof (*zr->members)); + memset (zr->members, 0, zr->n_members * sizeof (*zr->members)); zr->filename = strdup (filename); @@ -368,7 +392,7 @@ zip_member_open (struct zip_reader *zr, const char *member) uint32_t ucomp_size, comp_size; uint32_t crc; - + bool new_member = false; char *name = NULL; int i; @@ -377,17 +401,19 @@ zip_member_open (struct zip_reader *zr, const char *member) if ( zr == NULL) return NULL; - for (i = 0 ; i < zr->n_members; ++i) + for (i = 0; i < zr->n_members; ++i) { - zm = zr->members[i] = zip_header_read_next (zr); - if (zm && 0 == strcmp (zm->name, member)) + zm = zr->members[i]; + + if (zm == NULL) { - break; + zm = zr->members[i] = zip_header_read_next (zr); + new_member = true; } + if (zm && 0 == strcmp (zm->name, member)) + break; else - { - zm = NULL; - } + zm = NULL; } if ( zm == NULL) @@ -405,22 +431,22 @@ zip_member_open (struct zip_reader *zr, const char *member) return NULL; } - get_u16 (zm->fp, &v); - get_u16 (zm->fp, &gp); - get_u16 (zm->fp, &comp_type); + if (! get_u16 (zm->fp, &v)) return NULL; + if (! get_u16 (zm->fp, &gp)) return NULL; + if (! get_u16 (zm->fp, &comp_type)) return NULL; zm->compression = comp_code (zm, comp_type); - get_u16 (zm->fp, &time); - get_u16 (zm->fp, &date); - get_u32 (zm->fp, &crc); - get_u32 (zm->fp, &comp_size); + if (! get_u16 (zm->fp, &time)) return NULL; + if (! get_u16 (zm->fp, &date)) return NULL; + if (! get_u32 (zm->fp, &crc)) return NULL; + if (! get_u32 (zm->fp, &comp_size)) return NULL; - get_u32 (zm->fp, &ucomp_size); - get_u16 (zm->fp, &nlen); - get_u16 (zm->fp, &extra_len); + if (! get_u32 (zm->fp, &ucomp_size)) return NULL; + if (! get_u16 (zm->fp, &nlen)) return NULL; + if (! get_u16 (zm->fp, &extra_len)) return NULL; - name = calloc (nlen + 1, sizeof (char)); + name = xzalloc (nlen + 1); - get_bytes (zm->fp, name, nlen); + if (! get_bytes (zm->fp, name, nlen)) return NULL; skip_bytes (zm->fp, extra_len); @@ -437,8 +463,11 @@ zip_member_open (struct zip_reader *zr, const char *member) free (name); zm->bytes_unread = zm->ucomp_size; + + if ( !new_member) + decompressors[zm->compression].finish (zm); - if ( ! decompressors[zm->compression].init (zm) ) + if (!decompressors[zm->compression].init (zm) ) return NULL; return zm; @@ -536,7 +565,8 @@ probe_magic (FILE *fp, uint32_t magic, off_t start, off_t stop, off_t *off) do { - fread (&byte, 1, 1, fp); + if (1 != fread (&byte, 1, 1, fp)) + break; if ( byte == seq[state]) state++;