userdiff: fix some corner cases in dts regex
[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(struct index_state *istate, const char *path,
268                            const void *buffer, size_t size)
269 {
270         struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
271         if (!driver)
272                 driver = userdiff_find_by_name("default");
273         if (driver->binary != -1)
274                 return driver->binary;
275         return buffer_is_binary(buffer, size);
276 }
277
278 #define STREAM_BUFFER_SIZE (1024 * 16)
279
280 static int write_zip_entry(struct archiver_args *args,
281                            const struct object_id *oid,
282                            const char *path, size_t pathlen,
283                            unsigned int mode)
284 {
285         struct zip_local_header header;
286         uintmax_t offset = zip_offset;
287         struct zip_extra_mtime extra;
288         struct zip64_extra extra64;
289         size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE;
290         int need_zip64_extra = 0;
291         unsigned long attr2;
292         unsigned long compressed_size;
293         unsigned long crc;
294         int method;
295         unsigned char *out;
296         void *deflated = NULL;
297         void *buffer;
298         struct git_istream *stream = NULL;
299         unsigned long flags = 0;
300         unsigned long size;
301         int is_binary = -1;
302         const char *path_without_prefix = path + args->baselen;
303         unsigned int creator_version = 0;
304         unsigned int version_needed = 10;
305         size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE;
306         size_t zip64_dir_extra_payload_size = 0;
307
308         crc = crc32(0, NULL, 0);
309
310         if (!has_only_ascii(path)) {
311                 if (is_utf8(path))
312                         flags |= ZIP_UTF8;
313                 else
314                         warning(_("path is not valid UTF-8: %s"), path);
315         }
316
317         if (pathlen > 0xffff) {
318                 return error(_("path too long (%d chars, SHA1: %s): %s"),
319                                 (int)pathlen, oid_to_hex(oid), path);
320         }
321
322         if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
323                 method = 0;
324                 attr2 = 16;
325                 out = NULL;
326                 size = 0;
327                 compressed_size = 0;
328                 buffer = NULL;
329         } else if (S_ISREG(mode) || S_ISLNK(mode)) {
330                 enum object_type type = oid_object_info(args->repo, oid,
331                                                         &size);
332
333                 method = 0;
334                 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
335                         (mode & 0111) ? ((mode) << 16) : 0;
336                 if (S_ISLNK(mode) || (mode & 0111))
337                         creator_version = 0x0317;
338                 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
339                         method = 8;
340
341                 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
342                     size > big_file_threshold) {
343                         stream = open_istream(oid, &type, &size, NULL);
344                         if (!stream)
345                                 return error(_("cannot stream blob %s"),
346                                              oid_to_hex(oid));
347                         flags |= ZIP_STREAM;
348                         out = buffer = NULL;
349                 } else {
350                         buffer = object_file_to_archive(args, path, oid, mode,
351                                                         &type, &size);
352                         if (!buffer)
353                                 return error(_("cannot read %s"),
354                                              oid_to_hex(oid));
355                         crc = crc32(crc, buffer, size);
356                         is_binary = entry_is_binary(args->repo->index,
357                                                     path_without_prefix,
358                                                     buffer, size);
359                         out = buffer;
360                 }
361                 compressed_size = (method == 0) ? size : 0;
362         } else {
363                 return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
364                                 oid_to_hex(oid));
365         }
366
367         if (creator_version > max_creator_version)
368                 max_creator_version = creator_version;
369
370         if (buffer && method == 8) {
371                 out = deflated = zlib_deflate_raw(buffer, size,
372                                                   args->compression_level,
373                                                   &compressed_size);
374                 if (!out || compressed_size >= size) {
375                         out = buffer;
376                         method = 0;
377                         compressed_size = size;
378                 }
379         }
380
381         copy_le16(extra.magic, 0x5455);
382         copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
383         extra.flags[0] = 1;     /* just mtime */
384         copy_le32(extra.mtime, args->time);
385
386         if (size > 0xffffffff || compressed_size > 0xffffffff)
387                 need_zip64_extra = 1;
388         if (stream && size > 0x7fffffff)
389                 need_zip64_extra = 1;
390
391         if (need_zip64_extra)
392                 version_needed = 45;
393
394         copy_le32(header.magic, 0x04034b50);
395         copy_le16(header.version, version_needed);
396         copy_le16(header.flags, flags);
397         copy_le16(header.compression_method, method);
398         copy_le16(header.mtime, zip_time);
399         copy_le16(header.mdate, zip_date);
400         if (need_zip64_extra) {
401                 set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc);
402                 header_extra_size += ZIP64_EXTRA_SIZE;
403         } else {
404                 set_zip_header_data_desc(&header, size, compressed_size, crc);
405         }
406         copy_le16(header.filename_length, pathlen);
407         copy_le16(header.extra_length, header_extra_size);
408         write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
409         zip_offset += ZIP_LOCAL_HEADER_SIZE;
410         write_or_die(1, path, pathlen);
411         zip_offset += pathlen;
412         write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
413         zip_offset += ZIP_EXTRA_MTIME_SIZE;
414         if (need_zip64_extra) {
415                 copy_le16(extra64.magic, 0x0001);
416                 copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE);
417                 copy_le64(extra64.size, size);
418                 copy_le64(extra64.compressed_size, compressed_size);
419                 write_or_die(1, &extra64, ZIP64_EXTRA_SIZE);
420                 zip_offset += ZIP64_EXTRA_SIZE;
421         }
422
423         if (stream && method == 0) {
424                 unsigned char buf[STREAM_BUFFER_SIZE];
425                 ssize_t readlen;
426
427                 for (;;) {
428                         readlen = read_istream(stream, buf, sizeof(buf));
429                         if (readlen <= 0)
430                                 break;
431                         crc = crc32(crc, buf, readlen);
432                         if (is_binary == -1)
433                                 is_binary = entry_is_binary(args->repo->index,
434                                                             path_without_prefix,
435                                                             buf, readlen);
436                         write_or_die(1, buf, readlen);
437                 }
438                 close_istream(stream);
439                 if (readlen)
440                         return readlen;
441
442                 compressed_size = size;
443                 zip_offset += compressed_size;
444
445                 write_zip_data_desc(size, compressed_size, crc);
446         } else if (stream && method == 8) {
447                 unsigned char buf[STREAM_BUFFER_SIZE];
448                 ssize_t readlen;
449                 git_zstream zstream;
450                 int result;
451                 size_t out_len;
452                 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
453
454                 git_deflate_init_raw(&zstream, args->compression_level);
455
456                 compressed_size = 0;
457                 zstream.next_out = compressed;
458                 zstream.avail_out = sizeof(compressed);
459
460                 for (;;) {
461                         readlen = read_istream(stream, buf, sizeof(buf));
462                         if (readlen <= 0)
463                                 break;
464                         crc = crc32(crc, buf, readlen);
465                         if (is_binary == -1)
466                                 is_binary = entry_is_binary(args->repo->index,
467                                                             path_without_prefix,
468                                                             buf, readlen);
469
470                         zstream.next_in = buf;
471                         zstream.avail_in = readlen;
472                         result = git_deflate(&zstream, 0);
473                         if (result != Z_OK)
474                                 die(_("deflate error (%d)"), result);
475                         out_len = zstream.next_out - compressed;
476
477                         if (out_len > 0) {
478                                 write_or_die(1, compressed, out_len);
479                                 compressed_size += out_len;
480                                 zstream.next_out = compressed;
481                                 zstream.avail_out = sizeof(compressed);
482                         }
483
484                 }
485                 close_istream(stream);
486                 if (readlen)
487                         return readlen;
488
489                 zstream.next_in = buf;
490                 zstream.avail_in = 0;
491                 result = git_deflate(&zstream, Z_FINISH);
492                 if (result != Z_STREAM_END)
493                         die("deflate error (%d)", result);
494
495                 git_deflate_end(&zstream);
496                 out_len = zstream.next_out - compressed;
497                 write_or_die(1, compressed, out_len);
498                 compressed_size += out_len;
499                 zip_offset += compressed_size;
500
501                 write_zip_data_desc(size, compressed_size, crc);
502         } else if (compressed_size > 0) {
503                 write_or_die(1, out, compressed_size);
504                 zip_offset += compressed_size;
505         }
506
507         free(deflated);
508         free(buffer);
509
510         if (compressed_size > 0xffffffff || size > 0xffffffff ||
511             offset > 0xffffffff) {
512                 if (compressed_size >= 0xffffffff)
513                         zip64_dir_extra_payload_size += 8;
514                 if (size >= 0xffffffff)
515                         zip64_dir_extra_payload_size += 8;
516                 if (offset >= 0xffffffff)
517                         zip64_dir_extra_payload_size += 8;
518                 zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
519         }
520
521         strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
522         strbuf_add_le(&zip_dir, 2, creator_version);
523         strbuf_add_le(&zip_dir, 2, version_needed);
524         strbuf_add_le(&zip_dir, 2, flags);
525         strbuf_add_le(&zip_dir, 2, method);
526         strbuf_add_le(&zip_dir, 2, zip_time);
527         strbuf_add_le(&zip_dir, 2, zip_date);
528         strbuf_add_le(&zip_dir, 4, crc);
529         strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
530         strbuf_add_le(&zip_dir, 4, clamp32(size));
531         strbuf_add_le(&zip_dir, 2, pathlen);
532         strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
533         strbuf_add_le(&zip_dir, 2, 0);          /* comment length */
534         strbuf_add_le(&zip_dir, 2, 0);          /* disk */
535         strbuf_add_le(&zip_dir, 2, !is_binary);
536         strbuf_add_le(&zip_dir, 4, attr2);
537         strbuf_add_le(&zip_dir, 4, clamp32(offset));
538         strbuf_add(&zip_dir, path, pathlen);
539         strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
540         if (zip64_dir_extra_payload_size) {
541                 strbuf_add_le(&zip_dir, 2, 0x0001);     /* magic */
542                 strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size);
543                 if (size >= 0xffffffff)
544                         strbuf_add_le(&zip_dir, 8, size);
545                 if (compressed_size >= 0xffffffff)
546                         strbuf_add_le(&zip_dir, 8, compressed_size);
547                 if (offset >= 0xffffffff)
548                         strbuf_add_le(&zip_dir, 8, offset);
549         }
550         zip_dir_entries++;
551
552         return 0;
553 }
554
555 static void write_zip64_trailer(void)
556 {
557         struct zip64_dir_trailer trailer64;
558         struct zip64_dir_trailer_locator locator64;
559
560         copy_le32(trailer64.magic, 0x06064b50);
561         copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
562         copy_le16(trailer64.creator_version, max_creator_version);
563         copy_le16(trailer64.version, 45);
564         copy_le32(trailer64.disk, 0);
565         copy_le32(trailer64.directory_start_disk, 0);
566         copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
567         copy_le64(trailer64.entries, zip_dir_entries);
568         copy_le64(trailer64.size, zip_dir.len);
569         copy_le64(trailer64.offset, zip_offset);
570
571         copy_le32(locator64.magic, 0x07064b50);
572         copy_le32(locator64.disk, 0);
573         copy_le64(locator64.offset, zip_offset + zip_dir.len);
574         copy_le32(locator64.number_of_disks, 1);
575
576         write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
577         write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
578 }
579
580 static void write_zip_trailer(const struct object_id *oid)
581 {
582         struct zip_dir_trailer trailer;
583         int clamped = 0;
584
585         copy_le32(trailer.magic, 0x06054b50);
586         copy_le16(trailer.disk, 0);
587         copy_le16(trailer.directory_start_disk, 0);
588         copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
589                         &clamped);
590         copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
591         copy_le32(trailer.size, zip_dir.len);
592         copy_le32_clamp(trailer.offset, zip_offset, &clamped);
593         copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);
594
595         write_or_die(1, zip_dir.buf, zip_dir.len);
596         if (clamped)
597                 write_zip64_trailer();
598         write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
599         if (oid)
600                 write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
601 }
602
603 static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
604 {
605         time_t time;
606         struct tm *t;
607
608         if (date_overflows(*timestamp))
609                 die(_("timestamp too large for this system: %"PRItime),
610                     *timestamp);
611         time = (time_t)*timestamp;
612         t = localtime(&time);
613         *timestamp = time;
614
615         *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
616                     (t->tm_year + 1900 - 1980) * 512;
617         *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
618 }
619
620 static int archive_zip_config(const char *var, const char *value, void *data)
621 {
622         return userdiff_config(var, value);
623 }
624
625 static int write_zip_archive(const struct archiver *ar,
626                              struct archiver_args *args)
627 {
628         int err;
629
630         git_config(archive_zip_config, NULL);
631
632         dos_time(&args->time, &zip_date, &zip_time);
633
634         strbuf_init(&zip_dir, 0);
635
636         err = write_archive_entries(args, write_zip_entry);
637         if (!err)
638                 write_zip_trailer(args->commit_oid);
639
640         strbuf_release(&zip_dir);
641
642         return err;
643 }
644
645 static struct archiver zip_archiver = {
646         "zip",
647         write_zip_archive,
648         ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
649 };
650
651 void init_zip_archiver(void)
652 {
653         register_archiver(&zip_archiver);
654 }