966bd5990f25adf1cc37c0879fad05aba3038b4e
[pspp] / src / libpspp / zip-reader.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #ifndef ZIP_READER_H
19 #define ZIP_READER_H 1
20
21 #include <inttypes.h>
22
23 struct zip_reader;
24 struct string;
25
26 enum compression
27   {
28     COMPRESSION_STORED = 0,
29 #if HAVE_ZLIB_H
30     COMPRESSION_INFLATE,
31 #endif
32     n_COMPRESSION
33   };
34
35 struct zip_member
36 {
37   FILE *fp;                   /* The stream from which the data is read */
38   uint32_t offset;            /* Starting offset in file. */
39   uint32_t comp_size;         /* Length of member file data, in bytes. */
40   uint32_t ucomp_size;        /* Uncompressed length of member file data, in bytes. */
41   uint32_t expected_crc;      /* CRC-32 of member file data.. */
42   char *name;                 /* Name of member file. */
43   uint32_t crc;
44   enum compression compression;
45
46   size_t bytes_unread;       /* Number of bytes left in the member available for reading */
47   int ref_cnt;
48   struct string *errs;
49   void *aux;
50 };
51
52 struct decompressor
53 {
54   bool (*init) (struct zip_member *);
55   int  (*read) (struct zip_member *, void *, size_t);
56   void (*finish) (struct zip_member *);
57 };
58
59
60 void zm_dump (const struct zip_member *zm);
61
62 /* Create zip reader to read the file called FILENAME.
63    If ERRS is non-null if will be used to contain any error messages
64    which the reader wishes to report.
65  */
66 struct zip_reader *zip_reader_create (const char *filename, struct string *errs);
67
68 /* Destroy the zip reader */
69 void zip_reader_destroy (struct zip_reader *zr);
70
71 /* Return the zip member in the reader ZR, called MEMBER */
72 struct zip_member *zip_member_open (struct zip_reader *zr, const char *member);
73
74 /* Read upto N bytes from ZM, storing them in BUF.
75    Returns the number of bytes read, or -1 on error */
76 int zip_member_read (struct zip_member *zm, void *buf, size_t n);
77
78 /* Unref (and possibly destroy) the zip member ZM */
79 void zip_member_unref (struct zip_member *zm);
80
81 /* Ref the zip member */
82 void zip_member_ref (struct zip_member *zm);
83
84 void zip_member_finish (struct zip_member *zm);
85
86
87 #endif