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