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