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