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