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