Merge branch 'as/api-allocation-doc'
[git] / archive-zip.c
1 /*
2  * Copyright (c) 2006 Rene Scharfe
3  */
4 #include "cache.h"
5 #include "archive.h"
6 #include "streaming.h"
7 #include "utf8.h"
8
9 static int zip_date;
10 static int zip_time;
11
12 static unsigned char *zip_dir;
13 static unsigned int zip_dir_size;
14
15 static unsigned int zip_offset;
16 static unsigned int zip_dir_offset;
17 static unsigned int zip_dir_entries;
18
19 #define ZIP_DIRECTORY_MIN_SIZE  (1024 * 1024)
20 #define ZIP_STREAM      (1 <<  3)
21 #define ZIP_UTF8        (1 << 11)
22
23 struct zip_local_header {
24         unsigned char magic[4];
25         unsigned char version[2];
26         unsigned char flags[2];
27         unsigned char compression_method[2];
28         unsigned char mtime[2];
29         unsigned char mdate[2];
30         unsigned char crc32[4];
31         unsigned char compressed_size[4];
32         unsigned char size[4];
33         unsigned char filename_length[2];
34         unsigned char extra_length[2];
35         unsigned char _end[1];
36 };
37
38 struct zip_data_desc {
39         unsigned char magic[4];
40         unsigned char crc32[4];
41         unsigned char compressed_size[4];
42         unsigned char size[4];
43         unsigned char _end[1];
44 };
45
46 struct zip_dir_header {
47         unsigned char magic[4];
48         unsigned char creator_version[2];
49         unsigned char version[2];
50         unsigned char flags[2];
51         unsigned char compression_method[2];
52         unsigned char mtime[2];
53         unsigned char mdate[2];
54         unsigned char crc32[4];
55         unsigned char compressed_size[4];
56         unsigned char size[4];
57         unsigned char filename_length[2];
58         unsigned char extra_length[2];
59         unsigned char comment_length[2];
60         unsigned char disk[2];
61         unsigned char attr1[2];
62         unsigned char attr2[4];
63         unsigned char offset[4];
64         unsigned char _end[1];
65 };
66
67 struct zip_dir_trailer {
68         unsigned char magic[4];
69         unsigned char disk[2];
70         unsigned char directory_start_disk[2];
71         unsigned char entries_on_this_disk[2];
72         unsigned char entries[2];
73         unsigned char size[4];
74         unsigned char offset[4];
75         unsigned char comment_length[2];
76         unsigned char _end[1];
77 };
78
79 struct zip_extra_mtime {
80         unsigned char magic[2];
81         unsigned char extra_size[2];
82         unsigned char flags[1];
83         unsigned char mtime[4];
84         unsigned char _end[1];
85 };
86
87 /*
88  * On ARM, padding is added at the end of the struct, so a simple
89  * sizeof(struct ...) reports two bytes more than the payload size
90  * we're interested in.
91  */
92 #define ZIP_LOCAL_HEADER_SIZE   offsetof(struct zip_local_header, _end)
93 #define ZIP_DATA_DESC_SIZE      offsetof(struct zip_data_desc, _end)
94 #define ZIP_DIR_HEADER_SIZE     offsetof(struct zip_dir_header, _end)
95 #define ZIP_DIR_TRAILER_SIZE    offsetof(struct zip_dir_trailer, _end)
96 #define ZIP_EXTRA_MTIME_SIZE    offsetof(struct zip_extra_mtime, _end)
97 #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
98         (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
99
100 static void copy_le16(unsigned char *dest, unsigned int n)
101 {
102         dest[0] = 0xff & n;
103         dest[1] = 0xff & (n >> 010);
104 }
105
106 static void copy_le32(unsigned char *dest, unsigned int n)
107 {
108         dest[0] = 0xff & n;
109         dest[1] = 0xff & (n >> 010);
110         dest[2] = 0xff & (n >> 020);
111         dest[3] = 0xff & (n >> 030);
112 }
113
114 static void *zlib_deflate(void *data, unsigned long size,
115                 int compression_level, unsigned long *compressed_size)
116 {
117         git_zstream stream;
118         unsigned long maxsize;
119         void *buffer;
120         int result;
121
122         memset(&stream, 0, sizeof(stream));
123         git_deflate_init(&stream, compression_level);
124         maxsize = git_deflate_bound(&stream, size);
125         buffer = xmalloc(maxsize);
126
127         stream.next_in = data;
128         stream.avail_in = size;
129         stream.next_out = buffer;
130         stream.avail_out = maxsize;
131
132         do {
133                 result = git_deflate(&stream, Z_FINISH);
134         } while (result == Z_OK);
135
136         if (result != Z_STREAM_END) {
137                 free(buffer);
138                 return NULL;
139         }
140
141         git_deflate_end(&stream);
142         *compressed_size = stream.total_out;
143
144         return buffer;
145 }
146
147 static void write_zip_data_desc(unsigned long size,
148                                 unsigned long compressed_size,
149                                 unsigned long crc)
150 {
151         struct zip_data_desc trailer;
152
153         copy_le32(trailer.magic, 0x08074b50);
154         copy_le32(trailer.crc32, crc);
155         copy_le32(trailer.compressed_size, compressed_size);
156         copy_le32(trailer.size, size);
157         write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
158 }
159
160 static void set_zip_dir_data_desc(struct zip_dir_header *header,
161                                   unsigned long size,
162                                   unsigned long compressed_size,
163                                   unsigned long crc)
164 {
165         copy_le32(header->crc32, crc);
166         copy_le32(header->compressed_size, compressed_size);
167         copy_le32(header->size, size);
168 }
169
170 static void set_zip_header_data_desc(struct zip_local_header *header,
171                                      unsigned long size,
172                                      unsigned long compressed_size,
173                                      unsigned long crc)
174 {
175         copy_le32(header->crc32, crc);
176         copy_le32(header->compressed_size, compressed_size);
177         copy_le32(header->size, size);
178 }
179
180 static int has_only_ascii(const char *s)
181 {
182         for (;;) {
183                 int c = *s++;
184                 if (c == '\0')
185                         return 1;
186                 if (!isascii(c))
187                         return 0;
188         }
189 }
190
191 #define STREAM_BUFFER_SIZE (1024 * 16)
192
193 static int write_zip_entry(struct archiver_args *args,
194                            const unsigned char *sha1,
195                            const char *path, size_t pathlen,
196                            unsigned int mode)
197 {
198         struct zip_local_header header;
199         struct zip_dir_header dirent;
200         struct zip_extra_mtime extra;
201         unsigned long attr2;
202         unsigned long compressed_size;
203         unsigned long crc;
204         unsigned long direntsize;
205         int method;
206         unsigned char *out;
207         void *deflated = NULL;
208         void *buffer;
209         struct git_istream *stream = NULL;
210         unsigned long flags = 0;
211         unsigned long size;
212
213         crc = crc32(0, NULL, 0);
214
215         if (!has_only_ascii(path)) {
216                 if (is_utf8(path))
217                         flags |= ZIP_UTF8;
218                 else
219                         warning("Path is not valid UTF-8: %s", path);
220         }
221
222         if (pathlen > 0xffff) {
223                 return error("path too long (%d chars, SHA1: %s): %s",
224                                 (int)pathlen, sha1_to_hex(sha1), path);
225         }
226
227         if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
228                 method = 0;
229                 attr2 = 16;
230                 out = NULL;
231                 size = 0;
232                 compressed_size = 0;
233                 buffer = NULL;
234                 size = 0;
235         } else if (S_ISREG(mode) || S_ISLNK(mode)) {
236                 enum object_type type = sha1_object_info(sha1, &size);
237
238                 method = 0;
239                 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
240                         (mode & 0111) ? ((mode) << 16) : 0;
241                 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
242                         method = 8;
243                 compressed_size = size;
244
245                 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
246                     size > big_file_threshold) {
247                         stream = open_istream(sha1, &type, &size, NULL);
248                         if (!stream)
249                                 return error("cannot stream blob %s",
250                                              sha1_to_hex(sha1));
251                         flags |= ZIP_STREAM;
252                         out = buffer = NULL;
253                 } else {
254                         buffer = sha1_file_to_archive(args, path, sha1, mode,
255                                                       &type, &size);
256                         if (!buffer)
257                                 return error("cannot read %s",
258                                              sha1_to_hex(sha1));
259                         crc = crc32(crc, buffer, size);
260                         out = buffer;
261                 }
262         } else {
263                 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
264                                 sha1_to_hex(sha1));
265         }
266
267         if (buffer && method == 8) {
268                 deflated = zlib_deflate(buffer, size, args->compression_level,
269                                 &compressed_size);
270                 if (deflated && compressed_size - 6 < size) {
271                         /* ZLIB --> raw compressed data (see RFC 1950) */
272                         /* CMF and FLG ... */
273                         out = (unsigned char *)deflated + 2;
274                         compressed_size -= 6;   /* ... and ADLER32 */
275                 } else {
276                         method = 0;
277                         compressed_size = size;
278                 }
279         }
280
281         copy_le16(extra.magic, 0x5455);
282         copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
283         extra.flags[0] = 1;     /* just mtime */
284         copy_le32(extra.mtime, args->time);
285
286         /* make sure we have enough free space in the dictionary */
287         direntsize = ZIP_DIR_HEADER_SIZE + pathlen + ZIP_EXTRA_MTIME_SIZE;
288         while (zip_dir_size < zip_dir_offset + direntsize) {
289                 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
290                 zip_dir = xrealloc(zip_dir, zip_dir_size);
291         }
292
293         copy_le32(dirent.magic, 0x02014b50);
294         copy_le16(dirent.creator_version,
295                 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
296         copy_le16(dirent.version, 10);
297         copy_le16(dirent.flags, flags);
298         copy_le16(dirent.compression_method, method);
299         copy_le16(dirent.mtime, zip_time);
300         copy_le16(dirent.mdate, zip_date);
301         set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
302         copy_le16(dirent.filename_length, pathlen);
303         copy_le16(dirent.extra_length, ZIP_EXTRA_MTIME_SIZE);
304         copy_le16(dirent.comment_length, 0);
305         copy_le16(dirent.disk, 0);
306         copy_le16(dirent.attr1, 0);
307         copy_le32(dirent.attr2, attr2);
308         copy_le32(dirent.offset, zip_offset);
309
310         copy_le32(header.magic, 0x04034b50);
311         copy_le16(header.version, 10);
312         copy_le16(header.flags, flags);
313         copy_le16(header.compression_method, method);
314         copy_le16(header.mtime, zip_time);
315         copy_le16(header.mdate, zip_date);
316         if (flags & ZIP_STREAM)
317                 set_zip_header_data_desc(&header, 0, 0, 0);
318         else
319                 set_zip_header_data_desc(&header, size, compressed_size, crc);
320         copy_le16(header.filename_length, pathlen);
321         copy_le16(header.extra_length, ZIP_EXTRA_MTIME_SIZE);
322         write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
323         zip_offset += ZIP_LOCAL_HEADER_SIZE;
324         write_or_die(1, path, pathlen);
325         zip_offset += pathlen;
326         write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
327         zip_offset += ZIP_EXTRA_MTIME_SIZE;
328         if (stream && method == 0) {
329                 unsigned char buf[STREAM_BUFFER_SIZE];
330                 ssize_t readlen;
331
332                 for (;;) {
333                         readlen = read_istream(stream, buf, sizeof(buf));
334                         if (readlen <= 0)
335                                 break;
336                         crc = crc32(crc, buf, readlen);
337                         write_or_die(1, buf, readlen);
338                 }
339                 close_istream(stream);
340                 if (readlen)
341                         return readlen;
342
343                 compressed_size = size;
344                 zip_offset += compressed_size;
345
346                 write_zip_data_desc(size, compressed_size, crc);
347                 zip_offset += ZIP_DATA_DESC_SIZE;
348
349                 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
350         } else if (stream && method == 8) {
351                 unsigned char buf[STREAM_BUFFER_SIZE];
352                 ssize_t readlen;
353                 git_zstream zstream;
354                 int result;
355                 size_t out_len;
356                 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
357
358                 memset(&zstream, 0, sizeof(zstream));
359                 git_deflate_init(&zstream, args->compression_level);
360
361                 compressed_size = 0;
362                 zstream.next_out = compressed;
363                 zstream.avail_out = sizeof(compressed);
364
365                 for (;;) {
366                         readlen = read_istream(stream, buf, sizeof(buf));
367                         if (readlen <= 0)
368                                 break;
369                         crc = crc32(crc, buf, readlen);
370
371                         zstream.next_in = buf;
372                         zstream.avail_in = readlen;
373                         result = git_deflate(&zstream, 0);
374                         if (result != Z_OK)
375                                 die("deflate error (%d)", result);
376                         out = compressed;
377                         if (!compressed_size)
378                                 out += 2;
379                         out_len = zstream.next_out - out;
380
381                         if (out_len > 0) {
382                                 write_or_die(1, out, out_len);
383                                 compressed_size += out_len;
384                                 zstream.next_out = compressed;
385                                 zstream.avail_out = sizeof(compressed);
386                         }
387
388                 }
389                 close_istream(stream);
390                 if (readlen)
391                         return readlen;
392
393                 zstream.next_in = buf;
394                 zstream.avail_in = 0;
395                 result = git_deflate(&zstream, Z_FINISH);
396                 if (result != Z_STREAM_END)
397                         die("deflate error (%d)", result);
398
399                 git_deflate_end(&zstream);
400                 out = compressed;
401                 if (!compressed_size)
402                         out += 2;
403                 out_len = zstream.next_out - out - 4;
404                 write_or_die(1, out, out_len);
405                 compressed_size += out_len;
406                 zip_offset += compressed_size;
407
408                 write_zip_data_desc(size, compressed_size, crc);
409                 zip_offset += ZIP_DATA_DESC_SIZE;
410
411                 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
412         } else if (compressed_size > 0) {
413                 write_or_die(1, out, compressed_size);
414                 zip_offset += compressed_size;
415         }
416
417         free(deflated);
418         free(buffer);
419
420         memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
421         zip_dir_offset += ZIP_DIR_HEADER_SIZE;
422         memcpy(zip_dir + zip_dir_offset, path, pathlen);
423         zip_dir_offset += pathlen;
424         memcpy(zip_dir + zip_dir_offset, &extra, ZIP_EXTRA_MTIME_SIZE);
425         zip_dir_offset += ZIP_EXTRA_MTIME_SIZE;
426         zip_dir_entries++;
427
428         return 0;
429 }
430
431 static void write_zip_trailer(const unsigned char *sha1)
432 {
433         struct zip_dir_trailer trailer;
434
435         copy_le32(trailer.magic, 0x06054b50);
436         copy_le16(trailer.disk, 0);
437         copy_le16(trailer.directory_start_disk, 0);
438         copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
439         copy_le16(trailer.entries, zip_dir_entries);
440         copy_le32(trailer.size, zip_dir_offset);
441         copy_le32(trailer.offset, zip_offset);
442         copy_le16(trailer.comment_length, sha1 ? 40 : 0);
443
444         write_or_die(1, zip_dir, zip_dir_offset);
445         write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
446         if (sha1)
447                 write_or_die(1, sha1_to_hex(sha1), 40);
448 }
449
450 static void dos_time(time_t *time, int *dos_date, int *dos_time)
451 {
452         struct tm *t = localtime(time);
453
454         *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
455                     (t->tm_year + 1900 - 1980) * 512;
456         *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
457 }
458
459 static int write_zip_archive(const struct archiver *ar,
460                              struct archiver_args *args)
461 {
462         int err;
463
464         dos_time(&args->time, &zip_date, &zip_time);
465
466         zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
467         zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
468
469         err = write_archive_entries(args, write_zip_entry);
470         if (!err)
471                 write_zip_trailer(args->commit_sha1);
472
473         free(zip_dir);
474
475         return err;
476 }
477
478 static struct archiver zip_archiver = {
479         "zip",
480         write_zip_archive,
481         ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
482 };
483
484 void init_zip_archiver(void)
485 {
486         register_archiver(&zip_archiver);
487 }