zip-writer: Write size and CRC in local directory when possible.
[pspp] / src / libpspp / zip-writer.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010, 2012, 2014 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 #include <config.h>
18
19 #include "libpspp/zip-writer.h"
20 #include "libpspp/zip-private.h"
21
22 #include <byteswap.h>
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <time.h>
26
27 #include "gl/crc.h"
28 #include "gl/fwriteerror.h"
29 #include "gl/xalloc.h"
30
31 #include "libpspp/message.h"
32
33 #include "gettext.h"
34 #define _(msgid) gettext (msgid)
35
36 struct zip_writer
37   {
38     char *file_name;            /* File name, for use in error mesages. */
39     FILE *file;                 /* Output stream. */
40
41     uint16_t date, time;        /* Date and time in MS-DOS format. */
42
43     bool ok;
44
45     /* Members already added to the file, so that we can summarize them to the
46        central directory at the end of the ZIP file. */
47     struct zip_member *members;
48     size_t n_members, allocated_members;
49   };
50
51 struct zip_member
52   {
53     uint32_t offset;            /* Starting offset in file. */
54     uint32_t size;              /* Length of member file data, in bytes. */
55     uint32_t crc;               /* CRC-32 of member file data.. */
56     char *name;                 /* Name of member file. */
57   };
58
59 static void
60 put_bytes (struct zip_writer *zw, const void *p, size_t n)
61 {
62   fwrite (p, 1, n, zw->file);
63 }
64
65 static void
66 put_u16 (struct zip_writer *zw, uint16_t x)
67 {
68 #ifdef WORDS_BIGENDIAN
69   x = bswap_16 (x);
70 #endif
71   put_bytes (zw, &x, sizeof x);
72 }
73
74 static void
75 put_u32 (struct zip_writer *zw, uint32_t x)
76 {
77 #ifdef WORDS_BIGENDIAN
78   x = bswap_32 (x);
79 #endif
80   put_bytes (zw, &x, sizeof x);
81 }
82
83 /* Starts writing a new ZIP file named FILE_NAME.  Returns a new zip_writer if
84    successful, otherwise a null pointer. */
85 struct zip_writer *
86 zip_writer_create (const char *file_name)
87 {
88   struct zip_writer *zw;
89   struct tm *tm;
90   time_t now;
91   FILE *file;
92
93   file = fopen (file_name, "wb");
94   if (file == NULL)
95     {
96       msg_error (errno, _("%s: error opening output file"), file_name);
97       return NULL;
98     }
99
100   zw = xmalloc (sizeof *zw);
101   zw->file_name = xstrdup (file_name);
102   zw->file = file;
103
104   zw->ok = true;
105
106   now = time (NULL);
107   tm = localtime (&now);
108   zw->date = tm->tm_mday + ((tm->tm_mon + 1) << 5) + ((tm->tm_year - 80) << 9);
109   zw->time = tm->tm_sec / 2 + (tm->tm_min << 5) + (tm->tm_hour << 11);
110
111   zw->members = NULL;
112   zw->n_members = 0;
113   zw->allocated_members = 0;
114
115   return zw;
116 }
117
118 static void
119 put_local_header (struct zip_writer *zw, const char *member_name, uint32_t crc,
120                   uint32_t size, int flag)
121 {
122   put_u32 (zw, MAGIC_LHDR);     /* local file header signature */
123   put_u16 (zw, 10);             /* version needed to extract */
124   put_u16 (zw, flag);           /* general purpose bit flag */
125   put_u16 (zw, 0);              /* compression method */
126   put_u16 (zw, zw->time);       /* last mod file time */
127   put_u16 (zw, zw->date);       /* last mod file date */
128   put_u32 (zw, crc);            /* crc-32 */
129   put_u32 (zw, size);           /* compressed size */
130   put_u32 (zw, size);           /* uncompressed size */
131   put_u16 (zw, strlen (member_name)); /* file name length */
132   put_u16 (zw, 0);                    /* extra field length */
133   put_bytes (zw, member_name, strlen (member_name));
134 }
135
136 /* Adds the contents of FILE, with name MEMBER_NAME, to ZW. */
137 void
138 zip_writer_add (struct zip_writer *zw, FILE *file, const char *member_name)
139 {
140   struct zip_member *member;
141   uint32_t offset, size;
142   size_t bytes_read;
143   uint32_t crc;
144   char buf[4096];
145
146   /* Local file header. */
147   offset = ftello (zw->file);
148   put_local_header (zw, member_name, 0, 0, 1 << 3);
149
150   /* File data. */
151   size = crc = 0;
152   fseeko (file, 0, SEEK_SET);
153   while ((bytes_read = fread (buf, 1, sizeof buf, file)) > 0)
154     {
155       put_bytes (zw, buf, bytes_read);
156       size += bytes_read;
157       crc = crc32_update (crc, buf, bytes_read);
158     }
159
160   /* Try to seek back to the local file header.  If successful, overwrite it
161      with the correct file size and CRC.  Otherwise, write data descriptor. */
162   if (fseeko (zw->file, offset, SEEK_SET) == 0)
163     {
164       put_local_header (zw, member_name, crc, size, 0);
165       if (fseeko (zw->file, size, SEEK_CUR)
166           && zw->ok)
167         {
168           msg_error (errno, _("%s: error seeking in output file"), zw->file_name);
169           zw->ok = false;
170         }
171     }
172   else
173     {
174       put_u32 (zw, MAGIC_DDHD);
175       put_u32 (zw, crc);
176       put_u32 (zw, size);
177       put_u32 (zw, size);
178     }
179
180   /* Add to set of members. */
181   if (zw->n_members >= zw->allocated_members)
182     zw->members = x2nrealloc (zw->members, &zw->allocated_members,
183                               sizeof *zw->members);
184   member = &zw->members[zw->n_members++];
185   member->offset = offset;
186   member->size = size;
187   member->crc = crc;
188   member->name = xstrdup (member_name);
189 }
190
191 /* Finalizes the contents of ZW and closes it.  Returns true if successful,
192    false if a write error occurred while finalizing the file or at any earlier
193    time. */
194 bool
195 zip_writer_close (struct zip_writer *zw)
196 {
197   uint32_t dir_start, dir_end;
198   size_t i;
199   bool ok;
200
201   if (zw == NULL)
202     return true;
203
204   dir_start = ftello (zw->file);
205   for (i = 0; i < zw->n_members; i++)
206     {
207       struct zip_member *m = &zw->members[i];
208
209       /* Central directory file header. */
210       put_u32 (zw, MAGIC_SOCD);       /* central file header signature */
211       put_u16 (zw, 63);               /* version made by */
212       put_u16 (zw, 10);               /* version needed to extract */
213       put_u16 (zw, 1 << 3);           /* general purpose bit flag */
214       put_u16 (zw, 0);                /* compression method */
215       put_u16 (zw, zw->time);         /* last mod file time */
216       put_u16 (zw, zw->date);         /* last mod file date */
217       put_u32 (zw, m->crc);           /* crc-32 */
218       put_u32 (zw, m->size);          /* compressed size */
219       put_u32 (zw, m->size);          /* uncompressed size */
220       put_u16 (zw, strlen (m->name)); /* file name length */
221       put_u16 (zw, 0);                /* extra field length */
222       put_u16 (zw, 0);                /* file comment length */
223       put_u16 (zw, 0);                /* disk number start */
224       put_u16 (zw, 0);                /* internal file attributes */
225       put_u32 (zw, 0);                /* external file attributes */
226       put_u32 (zw, m->offset);        /* relative offset of local header */
227       put_bytes (zw, m->name, strlen (m->name));
228       free (m->name);
229     }
230   free (zw->members);
231   dir_end = ftello (zw->file);
232
233   /* End of central directory record. */
234   put_u32 (zw, MAGIC_EOCD);     /* end of central dir signature */
235   put_u16 (zw, 0);              /* number of this disk */
236   put_u16 (zw, 0);              /* number of the disk with the
237                                    start of the central directory */
238   put_u16 (zw, zw->n_members);  /* total number of entries in the
239                                    central directory on this disk */
240   put_u16 (zw, zw->n_members);  /* total number of entries in
241                                    the central directory */
242   put_u32 (zw, dir_end - dir_start); /* size of the central directory */
243   put_u32 (zw, dir_start);      /* offset of start of central
244                                    directory with respect to
245                                    the starting disk number */
246   put_u16 (zw, 0);              /* .ZIP file comment length */
247
248   ok = zw->ok;
249   if (ok && fwriteerror (zw->file))
250     {
251       msg_error (errno, _("%s: write failed"), zw->file_name);
252       ok = false;
253     }
254
255   free (zw->file_name);
256   free (zw);
257
258   return ok;
259 }