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