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