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 attr2;
149 unsigned long compressed_size;
150 unsigned long uncompressed_size;
151 unsigned long crc;
152 unsigned long direntsize;
153 unsigned long size;
154 int method;
155 int result = -1;
156 int pathlen;
157 unsigned char *out;
158 char *path;
159 char type[20];
160 void *buffer = NULL;
161 void *deflated = NULL;
162
163 crc = crc32(0, Z_NULL, 0);
164
165 path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
166 if (verbose)
167 fprintf(stderr, "%s\n", path);
168 if (pathlen > 0xffff) {
169 error("path too long (%d chars, SHA1: %s): %s", pathlen,
170 sha1_to_hex(sha1), path);
171 goto out;
172 }
173
174 if (S_ISDIR(mode)) {
175 method = 0;
176 attr2 = 16;
177 result = READ_TREE_RECURSIVE;
178 out = NULL;
179 uncompressed_size = 0;
180 compressed_size = 0;
181 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
182 method = 0;
183 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : 0;
184 if (S_ISREG(mode) && zlib_compression_level != 0)
185 method = 8;
186 result = 0;
187 buffer = read_sha1_file(sha1, type, &size);
188 if (!buffer)
189 die("cannot read %s", sha1_to_hex(sha1));
190 crc = crc32(crc, buffer, size);
191 out = buffer;
192 uncompressed_size = size;
193 compressed_size = size;
194 } else {
195 error("unsupported file mode: 0%o (SHA1: %s)", mode,
196 sha1_to_hex(sha1));
197 goto out;
198 }
199
200 if (method == 8) {
201 deflated = zlib_deflate(buffer, size, &compressed_size);
202 if (deflated && compressed_size - 6 < size) {
203 /* ZLIB --> raw compressed data (see RFC 1950) */
204 /* CMF and FLG ... */
205 out = (unsigned char *)deflated + 2;
206 compressed_size -= 6; /* ... and ADLER32 */
207 } else {
208 method = 0;
209 compressed_size = size;
210 }
211 }
212
213 /* make sure we have enough free space in the dictionary */
214 direntsize = sizeof(struct zip_dir_header) + pathlen;
215 while (zip_dir_size < zip_dir_offset + direntsize) {
216 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
217 zip_dir = xrealloc(zip_dir, zip_dir_size);
218 }
219
220 copy_le32(dirent.magic, 0x02014b50);
221 copy_le16(dirent.creator_version, S_ISLNK(mode) ? 0x0317 : 0);
222 copy_le16(dirent.version, 10);
223 copy_le16(dirent.flags, 0);
224 copy_le16(dirent.compression_method, method);
225 copy_le16(dirent.mtime, zip_time);
226 copy_le16(dirent.mdate, zip_date);
227 copy_le32(dirent.crc32, crc);
228 copy_le32(dirent.compressed_size, compressed_size);
229 copy_le32(dirent.size, uncompressed_size);
230 copy_le16(dirent.filename_length, pathlen);
231 copy_le16(dirent.extra_length, 0);
232 copy_le16(dirent.comment_length, 0);
233 copy_le16(dirent.disk, 0);
234 copy_le16(dirent.attr1, 0);
235 copy_le32(dirent.attr2, attr2);
236 copy_le32(dirent.offset, zip_offset);
237 memcpy(zip_dir + zip_dir_offset, &dirent, sizeof(struct zip_dir_header));
238 zip_dir_offset += sizeof(struct zip_dir_header);
239 memcpy(zip_dir + zip_dir_offset, path, pathlen);
240 zip_dir_offset += pathlen;
241 zip_dir_entries++;
242
243 copy_le32(header.magic, 0x04034b50);
244 copy_le16(header.version, 10);
245 copy_le16(header.flags, 0);
246 copy_le16(header.compression_method, method);
247 copy_le16(header.mtime, zip_time);
248 copy_le16(header.mdate, zip_date);
249 copy_le32(header.crc32, crc);
250 copy_le32(header.compressed_size, compressed_size);
251 copy_le32(header.size, uncompressed_size);
252 copy_le16(header.filename_length, pathlen);
253 copy_le16(header.extra_length, 0);
254 write_or_die(1, &header, sizeof(struct zip_local_header));
255 zip_offset += sizeof(struct zip_local_header);
256 write_or_die(1, path, pathlen);
257 zip_offset += pathlen;
258 if (compressed_size > 0) {
259 write_or_die(1, out, compressed_size);
260 zip_offset += compressed_size;
261 }
262
263out:
264 free(buffer);
265 free(deflated);
266 free(path);
267
268 return result;
269}
270
271static void write_zip_trailer(const unsigned char *sha1)
272{
273 struct zip_dir_trailer trailer;
274
275 copy_le32(trailer.magic, 0x06054b50);
276 copy_le16(trailer.disk, 0);
277 copy_le16(trailer.directory_start_disk, 0);
278 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
279 copy_le16(trailer.entries, zip_dir_entries);
280 copy_le32(trailer.size, zip_dir_offset);
281 copy_le32(trailer.offset, zip_offset);
282 copy_le16(trailer.comment_length, sha1 ? 40 : 0);
283
284 write_or_die(1, zip_dir, zip_dir_offset);
285 write_or_die(1, &trailer, sizeof(struct zip_dir_trailer));
286 if (sha1)
287 write_or_die(1, sha1_to_hex(sha1), 40);
288}
289
290static void dos_time(time_t *time, int *dos_date, int *dos_time)
291{
292 struct tm *t = localtime(time);
293
294 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
295 (t->tm_year + 1900 - 1980) * 512;
296 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
297}
298
299int write_zip_archive(struct archiver_args *args)
300{
301 int plen = strlen(args->base);
302
303 dos_time(&args->time, &zip_date, &zip_time);
304
305 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
306 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
307 verbose = args->verbose;
308
309 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
310 char *base = xstrdup(args->base);
311 int baselen = strlen(base);
312
313 while (baselen > 0 && base[baselen - 1] == '/')
314 base[--baselen] = '\0';
315 write_zip_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
316 free(base);
317 }
318 read_tree_recursive(args->tree, args->base, plen, 0,
319 args->pathspec, write_zip_entry);
320 write_zip_trailer(args->commit_sha1);
321
322 free(zip_dir);
323
324 return 0;
325}
326
327void *parse_extra_zip_args(int argc, const char **argv)
328{
329 for (; argc > 0; argc--, argv++) {
330 const char *arg = argv[0];
331
332 if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0')
333 zlib_compression_level = arg[1] - '0';
334 else
335 die("Unknown argument for zip format: %s", arg);
336 }
337 return NULL;
338}