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