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