Merge branch 'sb/blame-color'
[git] / archive-zip.c
1 /*
2  * Copyright (c) 2006 Rene Scharfe
3  */
4 #include "cache.h"
5 #include "config.h"
6 #include "archive.h"
7 #include "streaming.h"
8 #include "utf8.h"
9 #include "object-store.h"
10 #include "userdiff.h"
11 #include "xdiff-interface.h"
12
13 static int zip_date;
14 static int zip_time;
15
16 /* We only care about the "buf" part here. */
17 static struct strbuf zip_dir;
18
19 static uintmax_t zip_offset;
20 static uint64_t zip_dir_entries;
21
22 static unsigned int max_creator_version;
23
24 #define ZIP_STREAM      (1 <<  3)
25 #define ZIP_UTF8        (1 << 11)
26
27 struct zip_local_header {
28         unsigned char magic[4];
29         unsigned char version[2];
30         unsigned char flags[2];
31         unsigned char compression_method[2];
32         unsigned char mtime[2];
33         unsigned char mdate[2];
34         unsigned char crc32[4];
35         unsigned char compressed_size[4];
36         unsigned char size[4];
37         unsigned char filename_length[2];
38         unsigned char extra_length[2];
39         unsigned char _end[1];
40 };
41
42 struct zip_data_desc {
43         unsigned char magic[4];
44         unsigned char crc32[4];
45         unsigned char compressed_size[4];
46         unsigned char size[4];
47         unsigned char _end[1];
48 };
49
50 struct zip64_data_desc {
51         unsigned char magic[4];
52         unsigned char crc32[4];
53         unsigned char compressed_size[8];
54         unsigned char size[8];
55         unsigned char _end[1];
56 };
57
58 struct zip_dir_trailer {
59         unsigned char magic[4];
60         unsigned char disk[2];
61         unsigned char directory_start_disk[2];
62         unsigned char entries_on_this_disk[2];
63         unsigned char entries[2];
64         unsigned char size[4];
65         unsigned char offset[4];
66         unsigned char comment_length[2];
67         unsigned char _end[1];
68 };
69
70 struct zip_extra_mtime {
71         unsigned char magic[2];
72         unsigned char extra_size[2];
73         unsigned char flags[1];
74         unsigned char mtime[4];
75         unsigned char _end[1];
76 };
77
78 struct zip64_extra {
79         unsigned char magic[2];
80         unsigned char extra_size[2];
81         unsigned char size[8];
82         unsigned char compressed_size[8];
83         unsigned char _end[1];
84 };
85
86 struct zip64_dir_trailer {
87         unsigned char magic[4];
88         unsigned char record_size[8];
89         unsigned char creator_version[2];
90         unsigned char version[2];
91         unsigned char disk[4];
92         unsigned char directory_start_disk[4];
93         unsigned char entries_on_this_disk[8];
94         unsigned char entries[8];
95         unsigned char size[8];
96         unsigned char offset[8];
97         unsigned char _end[1];
98 };
99
100 struct zip64_dir_trailer_locator {
101         unsigned char magic[4];
102         unsigned char disk[4];
103         unsigned char offset[8];
104         unsigned char number_of_disks[4];
105         unsigned char _end[1];
106 };
107
108 /*
109  * On ARM, padding is added at the end of the struct, so a simple
110  * sizeof(struct ...) reports two bytes more than the payload size
111  * we're interested in.
112  */
113 #define ZIP_LOCAL_HEADER_SIZE   offsetof(struct zip_local_header, _end)
114 #define ZIP_DATA_DESC_SIZE      offsetof(struct zip_data_desc, _end)
115 #define ZIP64_DATA_DESC_SIZE    offsetof(struct zip64_data_desc, _end)
116 #define ZIP_DIR_HEADER_SIZE     offsetof(struct zip_dir_header, _end)
117 #define ZIP_DIR_TRAILER_SIZE    offsetof(struct zip_dir_trailer, _end)
118 #define ZIP_EXTRA_MTIME_SIZE    offsetof(struct zip_extra_mtime, _end)
119 #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
120         (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
121 #define ZIP64_EXTRA_SIZE        offsetof(struct zip64_extra, _end)
122 #define ZIP64_EXTRA_PAYLOAD_SIZE \
123         (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size))
124 #define ZIP64_DIR_TRAILER_SIZE  offsetof(struct zip64_dir_trailer, _end)
125 #define ZIP64_DIR_TRAILER_RECORD_SIZE \
126         (ZIP64_DIR_TRAILER_SIZE - \
127          offsetof(struct zip64_dir_trailer, creator_version))
128 #define ZIP64_DIR_TRAILER_LOCATOR_SIZE \
129         offsetof(struct zip64_dir_trailer_locator, _end)
130
131 static void copy_le16(unsigned char *dest, unsigned int n)
132 {
133         dest[0] = 0xff & n;
134         dest[1] = 0xff & (n >> 010);
135 }
136
137 static void copy_le32(unsigned char *dest, unsigned int n)
138 {
139         dest[0] = 0xff & n;
140         dest[1] = 0xff & (n >> 010);
141         dest[2] = 0xff & (n >> 020);
142         dest[3] = 0xff & (n >> 030);
143 }
144
145 static void copy_le64(unsigned char *dest, uint64_t n)
146 {
147         dest[0] = 0xff & n;
148         dest[1] = 0xff & (n >> 010);
149         dest[2] = 0xff & (n >> 020);
150         dest[3] = 0xff & (n >> 030);
151         dest[4] = 0xff & (n >> 040);
152         dest[5] = 0xff & (n >> 050);
153         dest[6] = 0xff & (n >> 060);
154         dest[7] = 0xff & (n >> 070);
155 }
156
157 static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped)
158 {
159         if (n <= max)
160                 return n;
161         *clamped = 1;
162         return max;
163 }
164
165 static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
166 {
167         copy_le16(dest, clamp_max(n, 0xffff, clamped));
168 }
169
170 static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped)
171 {
172         copy_le32(dest, clamp_max(n, 0xffffffff, clamped));
173 }
174
175 static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n)
176 {
177         while (size-- > 0) {
178                 strbuf_addch(sb, n & 0xff);
179                 n >>= 8;
180         }
181         return -!!n;
182 }
183
184 static uint32_t clamp32(uintmax_t n)
185 {
186         const uintmax_t max = 0xffffffff;
187         return (n < max) ? n : max;
188 }
189
190 static void *zlib_deflate_raw(void *data, unsigned long size,
191                               int compression_level,
192                               unsigned long *compressed_size)
193 {
194         git_zstream stream;
195         unsigned long maxsize;
196         void *buffer;
197         int result;
198
199         git_deflate_init_raw(&stream, compression_level);
200         maxsize = git_deflate_bound(&stream, size);
201         buffer = xmalloc(maxsize);
202
203         stream.next_in = data;
204         stream.avail_in = size;
205         stream.next_out = buffer;
206         stream.avail_out = maxsize;
207
208         do {
209                 result = git_deflate(&stream, Z_FINISH);
210         } while (result == Z_OK);
211
212         if (result != Z_STREAM_END) {
213                 free(buffer);
214                 return NULL;
215         }
216
217         git_deflate_end(&stream);
218         *compressed_size = stream.total_out;
219
220         return buffer;
221 }
222
223 static void write_zip_data_desc(unsigned long size,
224                                 unsigned long compressed_size,
225                                 unsigned long crc)
226 {
227         if (size >= 0xffffffff || compressed_size >= 0xffffffff) {
228                 struct zip64_data_desc trailer;
229                 copy_le32(trailer.magic, 0x08074b50);
230                 copy_le32(trailer.crc32, crc);
231                 copy_le64(trailer.compressed_size, compressed_size);
232                 copy_le64(trailer.size, size);
233                 write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE);
234                 zip_offset += ZIP64_DATA_DESC_SIZE;
235         } else {
236                 struct zip_data_desc trailer;
237                 copy_le32(trailer.magic, 0x08074b50);
238                 copy_le32(trailer.crc32, crc);
239                 copy_le32(trailer.compressed_size, compressed_size);
240                 copy_le32(trailer.size, size);
241                 write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
242                 zip_offset += ZIP_DATA_DESC_SIZE;
243         }
244 }
245
246 static void set_zip_header_data_desc(struct zip_local_header *header,
247                                      unsigned long size,
248                                      unsigned long compressed_size,
249                                      unsigned long crc)
250 {
251         copy_le32(header->crc32, crc);
252         copy_le32(header->compressed_size, compressed_size);
253         copy_le32(header->size, size);
254 }
255
256 static int has_only_ascii(const char *s)
257 {
258         for (;;) {
259                 int c = *s++;
260                 if (c == '\0')
261                         return 1;
262                 if (!isascii(c))
263                         return 0;
264         }
265 }
266
267 static int entry_is_binary(const char *path, const void *buffer, size_t size)
268 {
269         struct userdiff_driver *driver = userdiff_find_by_path(path);
270         if (!driver)
271                 driver = userdiff_find_by_name("default");
272         if (driver->binary != -1)
273                 return driver->binary;
274         return buffer_is_binary(buffer, size);
275 }
276
277 #define STREAM_BUFFER_SIZE (1024 * 16)
278
279 static int write_zip_entry(struct archiver_args *args,
280                            const struct object_id *oid,
281                            const char *path, size_t pathlen,
282                            unsigned int mode)
283 {
284         struct zip_local_header header;
285         uintmax_t offset = zip_offset;
286         struct zip_extra_mtime extra;
287         struct zip64_extra extra64;
288         size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE;
289         int need_zip64_extra = 0;
290         unsigned long attr2;
291         unsigned long compressed_size;
292         unsigned long crc;
293         int method;
294         unsigned char *out;
295         void *deflated = NULL;
296         void *buffer;
297         struct git_istream *stream = NULL;
298         unsigned long flags = 0;
299         unsigned long size;
300         int is_binary = -1;
301         const char *path_without_prefix = path + args->baselen;
302         unsigned int creator_version = 0;
303         unsigned int version_needed = 10;
304         size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE;
305         size_t zip64_dir_extra_payload_size = 0;
306
307         crc = crc32(0, NULL, 0);
308
309         if (!has_only_ascii(path)) {
310                 if (is_utf8(path))
311                         flags |= ZIP_UTF8;
312                 else
313                         warning("Path is not valid UTF-8: %s", path);
314         }
315
316         if (pathlen > 0xffff) {
317                 return error("path too long (%d chars, SHA1: %s): %s",
318                                 (int)pathlen, oid_to_hex(oid), path);
319         }
320
321         if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
322                 method = 0;
323                 attr2 = 16;
324                 out = NULL;
325                 size = 0;
326                 compressed_size = 0;
327                 buffer = NULL;
328         } else if (S_ISREG(mode) || S_ISLNK(mode)) {
329                 enum object_type type = oid_object_info(the_repository, oid,
330                                                         &size);
331
332                 method = 0;
333                 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
334                         (mode & 0111) ? ((mode) << 16) : 0;
335                 if (S_ISLNK(mode) || (mode & 0111))
336                         creator_version = 0x0317;
337                 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
338                         method = 8;
339
340                 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
341                     size > big_file_threshold) {
342                         stream = open_istream(oid, &type, &size, NULL);
343                         if (!stream)
344                                 return error("cannot stream blob %s",
345                                              oid_to_hex(oid));
346                         flags |= ZIP_STREAM;
347                         out = buffer = NULL;
348                 } else {
349                         buffer = object_file_to_archive(args, path, oid, mode,
350                                                         &type, &size);
351                         if (!buffer)
352                                 return error("cannot read %s",
353                                              oid_to_hex(oid));
354                         crc = crc32(crc, buffer, size);
355                         is_binary = entry_is_binary(path_without_prefix,
356                                                     buffer, size);
357                         out = buffer;
358                 }
359                 compressed_size = (method == 0) ? size : 0;
360         } else {
361                 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
362                                 oid_to_hex(oid));
363         }
364
365         if (creator_version > max_creator_version)
366                 max_creator_version = creator_version;
367
368         if (buffer && method == 8) {
369                 out = deflated = zlib_deflate_raw(buffer, size,
370                                                   args->compression_level,
371                                                   &compressed_size);
372                 if (!out || compressed_size >= size) {
373                         out = buffer;
374                         method = 0;
375                         compressed_size = size;
376                 }
377         }
378
379         copy_le16(extra.magic, 0x5455);
380         copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
381         extra.flags[0] = 1;     /* just mtime */
382         copy_le32(extra.mtime, args->time);
383
384         if (size > 0xffffffff || compressed_size > 0xffffffff)
385                 need_zip64_extra = 1;
386         if (stream && size > 0x7fffffff)
387                 need_zip64_extra = 1;
388
389         if (need_zip64_extra)
390                 version_needed = 45;
391
392         copy_le32(header.magic, 0x04034b50);
393         copy_le16(header.version, version_needed);
394         copy_le16(header.flags, flags);
395         copy_le16(header.compression_method, method);
396         copy_le16(header.mtime, zip_time);
397         copy_le16(header.mdate, zip_date);
398         if (need_zip64_extra) {
399                 set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc);
400                 header_extra_size += ZIP64_EXTRA_SIZE;
401         } else {
402                 set_zip_header_data_desc(&header, size, compressed_size, crc);
403         }
404         copy_le16(header.filename_length, pathlen);
405         copy_le16(header.extra_length, header_extra_size);
406         write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
407         zip_offset += ZIP_LOCAL_HEADER_SIZE;
408         write_or_die(1, path, pathlen);
409         zip_offset += pathlen;
410         write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
411         zip_offset += ZIP_EXTRA_MTIME_SIZE;
412         if (need_zip64_extra) {
413                 copy_le16(extra64.magic, 0x0001);
414                 copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE);
415                 copy_le64(extra64.size, size);
416                 copy_le64(extra64.compressed_size, compressed_size);
417                 write_or_die(1, &extra64, ZIP64_EXTRA_SIZE);
418                 zip_offset += ZIP64_EXTRA_SIZE;
419         }
420
421         if (stream && method == 0) {
422                 unsigned char buf[STREAM_BUFFER_SIZE];
423                 ssize_t readlen;
424
425                 for (;;) {
426                         readlen = read_istream(stream, buf, sizeof(buf));
427                         if (readlen <= 0)
428                                 break;
429                         crc = crc32(crc, buf, readlen);
430                         if (is_binary == -1)
431                                 is_binary = entry_is_binary(path_without_prefix,
432                                                             buf, readlen);
433                         write_or_die(1, buf, readlen);
434                 }
435                 close_istream(stream);
436                 if (readlen)
437                         return readlen;
438
439                 compressed_size = size;
440                 zip_offset += compressed_size;
441
442                 write_zip_data_desc(size, compressed_size, crc);
443         } else if (stream && method == 8) {
444                 unsigned char buf[STREAM_BUFFER_SIZE];
445                 ssize_t readlen;
446                 git_zstream zstream;
447                 int result;
448                 size_t out_len;
449                 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
450
451                 git_deflate_init_raw(&zstream, args->compression_level);
452
453                 compressed_size = 0;
454                 zstream.next_out = compressed;
455                 zstream.avail_out = sizeof(compressed);
456
457                 for (;;) {
458                         readlen = read_istream(stream, buf, sizeof(buf));
459                         if (readlen <= 0)
460                                 break;
461                         crc = crc32(crc, buf, readlen);
462                         if (is_binary == -1)
463                                 is_binary = entry_is_binary(path_without_prefix,
464                                                             buf, readlen);
465
466                         zstream.next_in = buf;
467                         zstream.avail_in = readlen;
468                         result = git_deflate(&zstream, 0);
469                         if (result != Z_OK)
470                                 die("deflate error (%d)", result);
471                         out_len = zstream.next_out - compressed;
472
473                         if (out_len > 0) {
474                                 write_or_die(1, compressed, out_len);
475                                 compressed_size += out_len;
476                                 zstream.next_out = compressed;
477                                 zstream.avail_out = sizeof(compressed);
478                         }
479
480                 }
481                 close_istream(stream);
482                 if (readlen)
483                         return readlen;
484
485                 zstream.next_in = buf;
486                 zstream.avail_in = 0;
487                 result = git_deflate(&zstream, Z_FINISH);
488                 if (result != Z_STREAM_END)
489                         die("deflate error (%d)", result);
490
491                 git_deflate_end(&zstream);
492                 out_len = zstream.next_out - compressed;
493                 write_or_die(1, compressed, out_len);
494                 compressed_size += out_len;
495                 zip_offset += compressed_size;
496
497                 write_zip_data_desc(size, compressed_size, crc);
498         } else if (compressed_size > 0) {
499                 write_or_die(1, out, compressed_size);
500                 zip_offset += compressed_size;
501         }
502
503         free(deflated);
504         free(buffer);
505
506         if (compressed_size > 0xffffffff || size > 0xffffffff ||
507             offset > 0xffffffff) {
508                 if (compressed_size >= 0xffffffff)
509                         zip64_dir_extra_payload_size += 8;
510                 if (size >= 0xffffffff)
511                         zip64_dir_extra_payload_size += 8;
512                 if (offset >= 0xffffffff)
513                         zip64_dir_extra_payload_size += 8;
514                 zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
515         }
516
517         strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
518         strbuf_add_le(&zip_dir, 2, creator_version);
519         strbuf_add_le(&zip_dir, 2, version_needed);
520         strbuf_add_le(&zip_dir, 2, flags);
521         strbuf_add_le(&zip_dir, 2, method);
522         strbuf_add_le(&zip_dir, 2, zip_time);
523         strbuf_add_le(&zip_dir, 2, zip_date);
524         strbuf_add_le(&zip_dir, 4, crc);
525         strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
526         strbuf_add_le(&zip_dir, 4, clamp32(size));
527         strbuf_add_le(&zip_dir, 2, pathlen);
528         strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
529         strbuf_add_le(&zip_dir, 2, 0);          /* comment length */
530         strbuf_add_le(&zip_dir, 2, 0);          /* disk */
531         strbuf_add_le(&zip_dir, 2, !is_binary);
532         strbuf_add_le(&zip_dir, 4, attr2);
533         strbuf_add_le(&zip_dir, 4, clamp32(offset));
534         strbuf_add(&zip_dir, path, pathlen);
535         strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
536         if (zip64_dir_extra_payload_size) {
537                 strbuf_add_le(&zip_dir, 2, 0x0001);     /* magic */
538                 strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size);
539                 if (size >= 0xffffffff)
540                         strbuf_add_le(&zip_dir, 8, size);
541                 if (compressed_size >= 0xffffffff)
542                         strbuf_add_le(&zip_dir, 8, compressed_size);
543                 if (offset >= 0xffffffff)
544                         strbuf_add_le(&zip_dir, 8, offset);
545         }
546         zip_dir_entries++;
547
548         return 0;
549 }
550
551 static void write_zip64_trailer(void)
552 {
553         struct zip64_dir_trailer trailer64;
554         struct zip64_dir_trailer_locator locator64;
555
556         copy_le32(trailer64.magic, 0x06064b50);
557         copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
558         copy_le16(trailer64.creator_version, max_creator_version);
559         copy_le16(trailer64.version, 45);
560         copy_le32(trailer64.disk, 0);
561         copy_le32(trailer64.directory_start_disk, 0);
562         copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
563         copy_le64(trailer64.entries, zip_dir_entries);
564         copy_le64(trailer64.size, zip_dir.len);
565         copy_le64(trailer64.offset, zip_offset);
566
567         copy_le32(locator64.magic, 0x07064b50);
568         copy_le32(locator64.disk, 0);
569         copy_le64(locator64.offset, zip_offset + zip_dir.len);
570         copy_le32(locator64.number_of_disks, 1);
571
572         write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
573         write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
574 }
575
576 static void write_zip_trailer(const unsigned char *sha1)
577 {
578         struct zip_dir_trailer trailer;
579         int clamped = 0;
580
581         copy_le32(trailer.magic, 0x06054b50);
582         copy_le16(trailer.disk, 0);
583         copy_le16(trailer.directory_start_disk, 0);
584         copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
585                         &clamped);
586         copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
587         copy_le32(trailer.size, zip_dir.len);
588         copy_le32_clamp(trailer.offset, zip_offset, &clamped);
589         copy_le16(trailer.comment_length, sha1 ? GIT_SHA1_HEXSZ : 0);
590
591         write_or_die(1, zip_dir.buf, zip_dir.len);
592         if (clamped)
593                 write_zip64_trailer();
594         write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
595         if (sha1)
596                 write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ);
597 }
598
599 static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
600 {
601         time_t time;
602         struct tm *t;
603
604         if (date_overflows(*timestamp))
605                 die("timestamp too large for this system: %"PRItime,
606                     *timestamp);
607         time = (time_t)*timestamp;
608         t = localtime(&time);
609         *timestamp = time;
610
611         *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
612                     (t->tm_year + 1900 - 1980) * 512;
613         *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
614 }
615
616 static int archive_zip_config(const char *var, const char *value, void *data)
617 {
618         return userdiff_config(var, value);
619 }
620
621 static int write_zip_archive(const struct archiver *ar,
622                              struct archiver_args *args)
623 {
624         int err;
625
626         git_config(archive_zip_config, NULL);
627
628         dos_time(&args->time, &zip_date, &zip_time);
629
630         strbuf_init(&zip_dir, 0);
631
632         err = write_archive_entries(args, write_zip_entry);
633         if (!err)
634                 write_zip_trailer(args->commit_sha1);
635
636         strbuf_release(&zip_dir);
637
638         return err;
639 }
640
641 static struct archiver zip_archiver = {
642         "zip",
643         write_zip_archive,
644         ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
645 };
646
647 void init_zip_archiver(void)
648 {
649         register_archiver(&zip_archiver);
650 }