Git fork
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 "streaming.h"
14#include "utf8.h"
15#include "odb.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
22static int zip_date;
23static int zip_time;
24
25/* We only care about the "buf" part here. */
26static struct strbuf zip_dir;
27
28static uintmax_t zip_offset;
29static uint64_t zip_dir_entries;
30
31static unsigned int max_creator_version;
32
33#define ZIP_STREAM (1 << 3)
34#define ZIP_UTF8 (1 << 11)
35
36enum zip_method {
37 ZIP_METHOD_STORE = 0,
38 ZIP_METHOD_DEFLATE = 8
39};
40
41struct 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
56struct 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
64struct 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
72struct 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
84struct 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
92struct 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
100struct 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
114struct 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
145static void copy_le16(unsigned char *dest, unsigned int n)
146{
147 dest[0] = 0xff & n;
148 dest[1] = 0xff & (n >> 010);
149}
150
151static 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
159static 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
171static 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
179static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
180{
181 copy_le16(dest, clamp_max(n, 0xffff, clamped));
182}
183
184static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped)
185{
186 copy_le32(dest, clamp_max(n, 0xffffffff, clamped));
187}
188
189static 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
198static uint32_t clamp32(uintmax_t n)
199{
200 const uintmax_t max = 0xffffffff;
201 return (n < max) ? n : max;
202}
203
204static void *zlib_deflate_raw(void *data, unsigned long size,
205 int compression_level,
206 unsigned long *compressed_size)
207{
208 git_zstream stream;
209 unsigned long 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
237static 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
260static 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
270static 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
281static 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
294static 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 git_istream *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 enum object_type type;
351 stream = open_istream(args->repo, oid, &type, &size,
352 NULL);
353 if (!stream)
354 return error(_("cannot stream blob %s"),
355 oid_to_hex(oid));
356 flags |= ZIP_STREAM;
357 out = NULL;
358 } else {
359 crc = crc32(crc, buffer, size);
360 is_binary = entry_is_binary(args->repo->index,
361 path_without_prefix,
362 buffer, size);
363 out = buffer;
364 }
365 compressed_size = (method == ZIP_METHOD_STORE) ? size : 0;
366 } else {
367 return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
368 oid_to_hex(oid));
369 }
370
371 if (creator_version > max_creator_version)
372 max_creator_version = creator_version;
373
374 if (buffer && method == ZIP_METHOD_DEFLATE) {
375 out = deflated = zlib_deflate_raw(buffer, size,
376 args->compression_level,
377 &compressed_size);
378 if (!out || compressed_size >= size) {
379 out = buffer;
380 method = ZIP_METHOD_STORE;
381 compressed_size = size;
382 }
383 }
384
385 copy_le16(extra.magic, 0x5455);
386 copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
387 extra.flags[0] = 1; /* just mtime */
388 copy_le32(extra.mtime, args->time);
389
390 if (size > 0xffffffff || compressed_size > 0xffffffff)
391 need_zip64_extra = 1;
392 if (stream && size > 0x7fffffff)
393 need_zip64_extra = 1;
394
395 if (need_zip64_extra)
396 version_needed = 45;
397
398 copy_le32(header.magic, 0x04034b50);
399 copy_le16(header.version, version_needed);
400 copy_le16(header.flags, flags);
401 copy_le16(header.compression_method, method);
402 copy_le16(header.mtime, zip_time);
403 copy_le16(header.mdate, zip_date);
404 if (need_zip64_extra) {
405 set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc);
406 header_extra_size += ZIP64_EXTRA_SIZE;
407 } else {
408 set_zip_header_data_desc(&header, size, compressed_size, crc);
409 }
410 copy_le16(header.filename_length, pathlen);
411 copy_le16(header.extra_length, header_extra_size);
412 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
413 zip_offset += ZIP_LOCAL_HEADER_SIZE;
414 write_or_die(1, path, pathlen);
415 zip_offset += pathlen;
416 write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
417 zip_offset += ZIP_EXTRA_MTIME_SIZE;
418 if (need_zip64_extra) {
419 copy_le16(extra64.magic, 0x0001);
420 copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE);
421 copy_le64(extra64.size, size);
422 copy_le64(extra64.compressed_size, compressed_size);
423 write_or_die(1, &extra64, ZIP64_EXTRA_SIZE);
424 zip_offset += ZIP64_EXTRA_SIZE;
425 }
426
427 if (stream && method == ZIP_METHOD_STORE) {
428 unsigned char buf[STREAM_BUFFER_SIZE];
429 ssize_t readlen;
430
431 for (;;) {
432 readlen = read_istream(stream, buf, sizeof(buf));
433 if (readlen <= 0)
434 break;
435 crc = crc32(crc, buf, readlen);
436 if (is_binary == -1)
437 is_binary = entry_is_binary(args->repo->index,
438 path_without_prefix,
439 buf, readlen);
440 write_or_die(1, buf, readlen);
441 }
442 close_istream(stream);
443 if (readlen)
444 return readlen;
445
446 compressed_size = size;
447 zip_offset += compressed_size;
448
449 write_zip_data_desc(size, compressed_size, crc);
450 } else if (stream && method == ZIP_METHOD_DEFLATE) {
451 unsigned char buf[STREAM_BUFFER_SIZE];
452 ssize_t readlen;
453 git_zstream zstream;
454 int result;
455 size_t out_len;
456 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
457
458 git_deflate_init_raw(&zstream, args->compression_level);
459
460 compressed_size = 0;
461 zstream.next_out = compressed;
462 zstream.avail_out = sizeof(compressed);
463
464 for (;;) {
465 readlen = read_istream(stream, buf, sizeof(buf));
466 if (readlen <= 0)
467 break;
468 crc = crc32(crc, buf, readlen);
469 if (is_binary == -1)
470 is_binary = entry_is_binary(args->repo->index,
471 path_without_prefix,
472 buf, readlen);
473
474 zstream.next_in = buf;
475 zstream.avail_in = readlen;
476 result = git_deflate(&zstream, 0);
477 if (result != Z_OK)
478 die(_("deflate error (%d)"), result);
479 out_len = zstream.next_out - compressed;
480
481 if (out_len > 0) {
482 write_or_die(1, compressed, out_len);
483 compressed_size += out_len;
484 zstream.next_out = compressed;
485 zstream.avail_out = sizeof(compressed);
486 }
487
488 }
489 close_istream(stream);
490 if (readlen)
491 return readlen;
492
493 zstream.next_in = buf;
494 zstream.avail_in = 0;
495
496 do {
497 result = git_deflate(&zstream, Z_FINISH);
498 if (result != Z_OK && result != Z_STREAM_END)
499 die("deflate error (%d)", result);
500
501 out_len = zstream.next_out - compressed;
502 if (out_len > 0) {
503 write_or_die(1, compressed, out_len);
504 compressed_size += out_len;
505 zstream.next_out = compressed;
506 zstream.avail_out = sizeof(compressed);
507 }
508 } while (result != Z_STREAM_END);
509
510 git_deflate_end(&zstream);
511 zip_offset += compressed_size;
512
513 write_zip_data_desc(size, compressed_size, crc);
514 } else if (compressed_size > 0) {
515 write_or_die(1, out, compressed_size);
516 zip_offset += compressed_size;
517 }
518
519 free(deflated);
520
521 if (compressed_size > 0xffffffff || size > 0xffffffff ||
522 offset > 0xffffffff) {
523 if (compressed_size >= 0xffffffff)
524 zip64_dir_extra_payload_size += 8;
525 if (size >= 0xffffffff)
526 zip64_dir_extra_payload_size += 8;
527 if (offset >= 0xffffffff)
528 zip64_dir_extra_payload_size += 8;
529 zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
530 }
531
532 strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
533 strbuf_add_le(&zip_dir, 2, creator_version);
534 strbuf_add_le(&zip_dir, 2, version_needed);
535 strbuf_add_le(&zip_dir, 2, flags);
536 strbuf_add_le(&zip_dir, 2, method);
537 strbuf_add_le(&zip_dir, 2, zip_time);
538 strbuf_add_le(&zip_dir, 2, zip_date);
539 strbuf_add_le(&zip_dir, 4, crc);
540 strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
541 strbuf_add_le(&zip_dir, 4, clamp32(size));
542 strbuf_add_le(&zip_dir, 2, pathlen);
543 strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
544 strbuf_add_le(&zip_dir, 2, 0); /* comment length */
545 strbuf_add_le(&zip_dir, 2, 0); /* disk */
546 strbuf_add_le(&zip_dir, 2, !is_binary);
547 strbuf_add_le(&zip_dir, 4, attr2);
548 strbuf_add_le(&zip_dir, 4, clamp32(offset));
549 strbuf_add(&zip_dir, path, pathlen);
550 strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
551 if (zip64_dir_extra_payload_size) {
552 strbuf_add_le(&zip_dir, 2, 0x0001); /* magic */
553 strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size);
554 if (size >= 0xffffffff)
555 strbuf_add_le(&zip_dir, 8, size);
556 if (compressed_size >= 0xffffffff)
557 strbuf_add_le(&zip_dir, 8, compressed_size);
558 if (offset >= 0xffffffff)
559 strbuf_add_le(&zip_dir, 8, offset);
560 }
561 zip_dir_entries++;
562
563 return 0;
564}
565
566static void write_zip64_trailer(void)
567{
568 struct zip64_dir_trailer trailer64;
569 struct zip64_dir_trailer_locator locator64;
570
571 copy_le32(trailer64.magic, 0x06064b50);
572 copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
573 copy_le16(trailer64.creator_version, max_creator_version);
574 copy_le16(trailer64.version, 45);
575 copy_le32(trailer64.disk, 0);
576 copy_le32(trailer64.directory_start_disk, 0);
577 copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
578 copy_le64(trailer64.entries, zip_dir_entries);
579 copy_le64(trailer64.size, zip_dir.len);
580 copy_le64(trailer64.offset, zip_offset);
581
582 copy_le32(locator64.magic, 0x07064b50);
583 copy_le32(locator64.disk, 0);
584 copy_le64(locator64.offset, zip_offset + zip_dir.len);
585 copy_le32(locator64.number_of_disks, 1);
586
587 write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
588 write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
589}
590
591static void write_zip_trailer(const struct object_id *oid)
592{
593 struct zip_dir_trailer trailer;
594 int clamped = 0;
595
596 copy_le32(trailer.magic, 0x06054b50);
597 copy_le16(trailer.disk, 0);
598 copy_le16(trailer.directory_start_disk, 0);
599 copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
600 &clamped);
601 copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
602 copy_le32(trailer.size, zip_dir.len);
603 copy_le32_clamp(trailer.offset, zip_offset, &clamped);
604 copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);
605
606 write_or_die(1, zip_dir.buf, zip_dir.len);
607 if (clamped)
608 write_zip64_trailer();
609 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
610 if (oid)
611 write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
612}
613
614static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
615{
616 time_t time;
617 struct tm tm;
618
619 if (date_overflows(*timestamp))
620 die(_("timestamp too large for this system: %"PRItime),
621 *timestamp);
622 time = (time_t)*timestamp;
623 localtime_r(&time, &tm);
624 *timestamp = time;
625
626 *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 +
627 (tm.tm_year + 1900 - 1980) * 512;
628 *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048;
629}
630
631static int archive_zip_config(const char *var, const char *value,
632 const struct config_context *ctx UNUSED,
633 void *data UNUSED)
634{
635 return userdiff_config(var, value);
636}
637
638static int write_zip_archive(const struct archiver *ar UNUSED,
639 struct archiver_args *args)
640{
641 int err;
642
643 repo_config(the_repository, archive_zip_config, NULL);
644
645 dos_time(&args->time, &zip_date, &zip_time);
646
647 strbuf_init(&zip_dir, 0);
648
649 err = write_archive_entries(args, write_zip_entry);
650 if (!err)
651 write_zip_trailer(args->commit_oid);
652
653 strbuf_release(&zip_dir);
654
655 return err;
656}
657
658static struct archiver zip_archiver = {
659 .name = "zip",
660 .write_archive = write_zip_archive,
661 .flags = ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE,
662};
663
664void init_zip_archiver(void)
665{
666 register_archiver(&zip_archiver);
667}