1/*
2 * Copyright (c) 2006 Rene Scharfe
3 */
4#include <time.h>
5#include "cache.h"
6#include "commit.h"
7#include "blob.h"
8#include "tree.h"
9#include "quote.h"
10#include "builtin.h"
11#include "archive.h"
12
13static int verbose;
14static int zip_date;
15static int zip_time;
16
17static unsigned char *zip_dir;
18static unsigned int zip_dir_size;
19
20static unsigned int zip_offset;
21static unsigned int zip_dir_offset;
22static unsigned int zip_dir_entries;
23
24#define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
25
26struct zip_local_header {
27 unsigned char magic[4];
28 unsigned char version[2];
29 unsigned char flags[2];
30 unsigned char compression_method[2];
31 unsigned char mtime[2];
32 unsigned char mdate[2];
33 unsigned char crc32[4];
34 unsigned char compressed_size[4];
35 unsigned char size[4];
36 unsigned char filename_length[2];
37 unsigned char extra_length[2];
38};
39
40struct zip_dir_header {
41 unsigned char magic[4];
42 unsigned char creator_version[2];
43 unsigned char version[2];
44 unsigned char flags[2];
45 unsigned char compression_method[2];
46 unsigned char mtime[2];
47 unsigned char mdate[2];
48 unsigned char crc32[4];
49 unsigned char compressed_size[4];
50 unsigned char size[4];
51 unsigned char filename_length[2];
52 unsigned char extra_length[2];
53 unsigned char comment_length[2];
54 unsigned char disk[2];
55 unsigned char attr1[2];
56 unsigned char attr2[4];
57 unsigned char offset[4];
58};
59
60struct zip_dir_trailer {
61 unsigned char magic[4];
62 unsigned char disk[2];
63 unsigned char directory_start_disk[2];
64 unsigned char entries_on_this_disk[2];
65 unsigned char entries[2];
66 unsigned char size[4];
67 unsigned char offset[4];
68 unsigned char comment_length[2];
69};
70
71static void copy_le16(unsigned char *dest, unsigned int n)
72{
73 dest[0] = 0xff & n;
74 dest[1] = 0xff & (n >> 010);
75}
76
77static void copy_le32(unsigned char *dest, unsigned int n)
78{
79 dest[0] = 0xff & n;
80 dest[1] = 0xff & (n >> 010);
81 dest[2] = 0xff & (n >> 020);
82 dest[3] = 0xff & (n >> 030);
83}
84
85static void *zlib_deflate(void *data, unsigned long size,
86 unsigned long *compressed_size)
87{
88 z_stream stream;
89 unsigned long maxsize;
90 void *buffer;
91 int result;
92
93 memset(&stream, 0, sizeof(stream));
94 deflateInit(&stream, zlib_compression_level);
95 maxsize = deflateBound(&stream, size);
96 buffer = xmalloc(maxsize);
97
98 stream.next_in = data;
99 stream.avail_in = size;
100 stream.next_out = buffer;
101 stream.avail_out = maxsize;
102
103 do {
104 result = deflate(&stream, Z_FINISH);
105 } while (result == Z_OK);
106
107 if (result != Z_STREAM_END) {
108 free(buffer);
109 return NULL;
110 }
111
112 deflateEnd(&stream);
113 *compressed_size = stream.total_out;
114
115 return buffer;
116}
117
118static char *construct_path(const char *base, int baselen,
119 const char *filename, int isdir, int *pathlen)
120{
121 int filenamelen = strlen(filename);
122 int len = baselen + filenamelen;
123 char *path, *p;
124
125 if (isdir)
126 len++;
127 p = path = xmalloc(len + 1);
128
129 memcpy(p, base, baselen);
130 p += baselen;
131 memcpy(p, filename, filenamelen);
132 p += filenamelen;
133 if (isdir)
134 *p++ = '/';
135 *p = '\0';
136
137 *pathlen = len;
138
139 return path;
140}
141
142static int write_zip_entry(const unsigned char *sha1,
143 const char *base, int baselen,
144 const char *filename, unsigned mode, int stage)
145{
146 struct zip_local_header header;
147 struct zip_dir_header dirent;
148 unsigned long compressed_size;
149 unsigned long uncompressed_size;
150 unsigned long crc;
151 unsigned long direntsize;
152 unsigned long size;
153 int method;
154 int result = -1;
155 int pathlen;
156 unsigned char *out;
157 char *path;
158 char type[20];
159 void *buffer = NULL;
160 void *deflated = NULL;
161
162 crc = crc32(0, Z_NULL, 0);
163
164 path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
165 if (verbose)
166 fprintf(stderr, "%s\n", path);
167 if (pathlen > 0xffff) {
168 error("path too long (%d chars, SHA1: %s): %s", pathlen,
169 sha1_to_hex(sha1), path);
170 goto out;
171 }
172
173 if (S_ISDIR(mode)) {
174 method = 0;
175 result = READ_TREE_RECURSIVE;
176 out = NULL;
177 uncompressed_size = 0;
178 compressed_size = 0;
179 } else if (S_ISREG(mode)) {
180 method = zlib_compression_level == 0 ? 0 : 8;
181 result = 0;
182 buffer = read_sha1_file(sha1, type, &size);
183 if (!buffer)
184 die("cannot read %s", sha1_to_hex(sha1));
185 crc = crc32(crc, buffer, size);
186 out = buffer;
187 uncompressed_size = size;
188 compressed_size = size;
189 } else {
190 error("unsupported file mode: 0%o (SHA1: %s)", mode,
191 sha1_to_hex(sha1));
192 goto out;
193 }
194
195 if (method == 8) {
196 deflated = zlib_deflate(buffer, size, &compressed_size);
197 if (deflated && compressed_size - 6 < size) {
198 /* ZLIB --> raw compressed data (see RFC 1950) */
199 /* CMF and FLG ... */
200 out = (unsigned char *)deflated + 2;
201 compressed_size -= 6; /* ... and ADLER32 */
202 } else {
203 method = 0;
204 compressed_size = size;
205 }
206 }
207
208 /* make sure we have enough free space in the dictionary */
209 direntsize = sizeof(struct zip_dir_header) + pathlen;
210 while (zip_dir_size < zip_dir_offset + direntsize) {
211 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
212 zip_dir = xrealloc(zip_dir, zip_dir_size);
213 }
214
215 copy_le32(dirent.magic, 0x02014b50);
216 copy_le16(dirent.creator_version, 0);
217 copy_le16(dirent.version, 20);
218 copy_le16(dirent.flags, 0);
219 copy_le16(dirent.compression_method, method);
220 copy_le16(dirent.mtime, zip_time);
221 copy_le16(dirent.mdate, zip_date);
222 copy_le32(dirent.crc32, crc);
223 copy_le32(dirent.compressed_size, compressed_size);
224 copy_le32(dirent.size, uncompressed_size);
225 copy_le16(dirent.filename_length, pathlen);
226 copy_le16(dirent.extra_length, 0);
227 copy_le16(dirent.comment_length, 0);
228 copy_le16(dirent.disk, 0);
229 copy_le16(dirent.attr1, 0);
230 copy_le32(dirent.attr2, 0);
231 copy_le32(dirent.offset, zip_offset);
232 memcpy(zip_dir + zip_dir_offset, &dirent, sizeof(struct zip_dir_header));
233 zip_dir_offset += sizeof(struct zip_dir_header);
234 memcpy(zip_dir + zip_dir_offset, path, pathlen);
235 zip_dir_offset += pathlen;
236 zip_dir_entries++;
237
238 copy_le32(header.magic, 0x04034b50);
239 copy_le16(header.version, 20);
240 copy_le16(header.flags, 0);
241 copy_le16(header.compression_method, method);
242 copy_le16(header.mtime, zip_time);
243 copy_le16(header.mdate, zip_date);
244 copy_le32(header.crc32, crc);
245 copy_le32(header.compressed_size, compressed_size);
246 copy_le32(header.size, uncompressed_size);
247 copy_le16(header.filename_length, pathlen);
248 copy_le16(header.extra_length, 0);
249 write_or_die(1, &header, sizeof(struct zip_local_header));
250 zip_offset += sizeof(struct zip_local_header);
251 write_or_die(1, path, pathlen);
252 zip_offset += pathlen;
253 if (compressed_size > 0) {
254 write_or_die(1, out, compressed_size);
255 zip_offset += compressed_size;
256 }
257
258out:
259 free(buffer);
260 free(deflated);
261 free(path);
262
263 return result;
264}
265
266static void write_zip_trailer(const unsigned char *sha1)
267{
268 struct zip_dir_trailer trailer;
269
270 copy_le32(trailer.magic, 0x06054b50);
271 copy_le16(trailer.disk, 0);
272 copy_le16(trailer.directory_start_disk, 0);
273 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
274 copy_le16(trailer.entries, zip_dir_entries);
275 copy_le32(trailer.size, zip_dir_offset);
276 copy_le32(trailer.offset, zip_offset);
277 copy_le16(trailer.comment_length, sha1 ? 40 : 0);
278
279 write_or_die(1, zip_dir, zip_dir_offset);
280 write_or_die(1, &trailer, sizeof(struct zip_dir_trailer));
281 if (sha1)
282 write_or_die(1, sha1_to_hex(sha1), 40);
283}
284
285static void dos_time(time_t *time, int *dos_date, int *dos_time)
286{
287 struct tm *t = localtime(time);
288
289 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
290 (t->tm_year + 1900 - 1980) * 512;
291 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
292}
293
294int write_zip_archive(struct archiver_args *args)
295{
296 int plen = strlen(args->base);
297
298 dos_time(&args->time, &zip_date, &zip_time);
299
300 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
301 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
302 verbose = args->verbose;
303
304 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
305 char *base = xstrdup(args->base);
306 int baselen = strlen(base);
307
308 while (baselen > 0 && base[baselen - 1] == '/')
309 base[--baselen] = '\0';
310 write_zip_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
311 free(base);
312 }
313 read_tree_recursive(args->tree, args->base, plen, 0,
314 args->pathspec, write_zip_entry);
315 write_zip_trailer(args->commit_sha1);
316
317 free(zip_dir);
318
319 return 0;
320}
321
322void *parse_extra_zip_args(int argc, const char **argv)
323{
324 for (; argc > 0; argc--, argv++) {
325 const char *arg = argv[0];
326
327 if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0')
328 zlib_compression_level = arg[1] - '0';
329 else
330 die("Unknown argument for zip format: %s", arg);
331 }
332 return NULL;
333}