cocci: remove 'unused.cocci'
[git.git] / archive-zip.c
blobe6f5c10a14f20a105de81224aae0144610f5a312
1 /*
2 * Copyright (c) 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "config.h"
6 #include "archive.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "streaming.h"
10 #include "utf8.h"
11 #include "object-store.h"
12 #include "userdiff.h"
13 #include "write-or-die.h"
14 #include "xdiff-interface.h"
15 #include "date.h"
17 static int zip_date;
18 static int zip_time;
20 /* We only care about the "buf" part here. */
21 static struct strbuf zip_dir;
23 static uintmax_t zip_offset;
24 static uint64_t zip_dir_entries;
26 static unsigned int max_creator_version;
28 #define ZIP_STREAM (1 << 3)
29 #define ZIP_UTF8 (1 << 11)
31 enum zip_method {
32 ZIP_METHOD_STORE = 0,
33 ZIP_METHOD_DEFLATE = 8
36 struct zip_local_header {
37 unsigned char magic[4];
38 unsigned char version[2];
39 unsigned char flags[2];
40 unsigned char compression_method[2];
41 unsigned char mtime[2];
42 unsigned char mdate[2];
43 unsigned char crc32[4];
44 unsigned char compressed_size[4];
45 unsigned char size[4];
46 unsigned char filename_length[2];
47 unsigned char extra_length[2];
48 unsigned char _end[1];
51 struct zip_data_desc {
52 unsigned char magic[4];
53 unsigned char crc32[4];
54 unsigned char compressed_size[4];
55 unsigned char size[4];
56 unsigned char _end[1];
59 struct zip64_data_desc {
60 unsigned char magic[4];
61 unsigned char crc32[4];
62 unsigned char compressed_size[8];
63 unsigned char size[8];
64 unsigned char _end[1];
67 struct zip_dir_trailer {
68 unsigned char magic[4];
69 unsigned char disk[2];
70 unsigned char directory_start_disk[2];
71 unsigned char entries_on_this_disk[2];
72 unsigned char entries[2];
73 unsigned char size[4];
74 unsigned char offset[4];
75 unsigned char comment_length[2];
76 unsigned char _end[1];
79 struct zip_extra_mtime {
80 unsigned char magic[2];
81 unsigned char extra_size[2];
82 unsigned char flags[1];
83 unsigned char mtime[4];
84 unsigned char _end[1];
87 struct zip64_extra {
88 unsigned char magic[2];
89 unsigned char extra_size[2];
90 unsigned char size[8];
91 unsigned char compressed_size[8];
92 unsigned char _end[1];
95 struct zip64_dir_trailer {
96 unsigned char magic[4];
97 unsigned char record_size[8];
98 unsigned char creator_version[2];
99 unsigned char version[2];
100 unsigned char disk[4];
101 unsigned char directory_start_disk[4];
102 unsigned char entries_on_this_disk[8];
103 unsigned char entries[8];
104 unsigned char size[8];
105 unsigned char offset[8];
106 unsigned char _end[1];
109 struct zip64_dir_trailer_locator {
110 unsigned char magic[4];
111 unsigned char disk[4];
112 unsigned char offset[8];
113 unsigned char number_of_disks[4];
114 unsigned char _end[1];
118 * On ARM, padding is added at the end of the struct, so a simple
119 * sizeof(struct ...) reports two bytes more than the payload size
120 * we're interested in.
122 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
123 #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
124 #define ZIP64_DATA_DESC_SIZE offsetof(struct zip64_data_desc, _end)
125 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
126 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
127 #define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end)
128 #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
129 (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
130 #define ZIP64_EXTRA_SIZE offsetof(struct zip64_extra, _end)
131 #define ZIP64_EXTRA_PAYLOAD_SIZE \
132 (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size))
133 #define ZIP64_DIR_TRAILER_SIZE offsetof(struct zip64_dir_trailer, _end)
134 #define ZIP64_DIR_TRAILER_RECORD_SIZE \
135 (ZIP64_DIR_TRAILER_SIZE - \
136 offsetof(struct zip64_dir_trailer, creator_version))
137 #define ZIP64_DIR_TRAILER_LOCATOR_SIZE \
138 offsetof(struct zip64_dir_trailer_locator, _end)
140 static void copy_le16(unsigned char *dest, unsigned int n)
142 dest[0] = 0xff & n;
143 dest[1] = 0xff & (n >> 010);
146 static void copy_le32(unsigned char *dest, unsigned int n)
148 dest[0] = 0xff & n;
149 dest[1] = 0xff & (n >> 010);
150 dest[2] = 0xff & (n >> 020);
151 dest[3] = 0xff & (n >> 030);
154 static void copy_le64(unsigned char *dest, uint64_t n)
156 dest[0] = 0xff & n;
157 dest[1] = 0xff & (n >> 010);
158 dest[2] = 0xff & (n >> 020);
159 dest[3] = 0xff & (n >> 030);
160 dest[4] = 0xff & (n >> 040);
161 dest[5] = 0xff & (n >> 050);
162 dest[6] = 0xff & (n >> 060);
163 dest[7] = 0xff & (n >> 070);
166 static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped)
168 if (n <= max)
169 return n;
170 *clamped = 1;
171 return max;
174 static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
176 copy_le16(dest, clamp_max(n, 0xffff, clamped));
179 static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped)
181 copy_le32(dest, clamp_max(n, 0xffffffff, clamped));
184 static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n)
186 while (size-- > 0) {
187 strbuf_addch(sb, n & 0xff);
188 n >>= 8;
190 return -!!n;
193 static uint32_t clamp32(uintmax_t n)
195 const uintmax_t max = 0xffffffff;
196 return (n < max) ? n : max;
199 static void *zlib_deflate_raw(void *data, unsigned long size,
200 int compression_level,
201 unsigned long *compressed_size)
203 git_zstream stream;
204 unsigned long maxsize;
205 void *buffer;
206 int result;
208 git_deflate_init_raw(&stream, compression_level);
209 maxsize = git_deflate_bound(&stream, size);
210 buffer = xmalloc(maxsize);
212 stream.next_in = data;
213 stream.avail_in = size;
214 stream.next_out = buffer;
215 stream.avail_out = maxsize;
217 do {
218 result = git_deflate(&stream, Z_FINISH);
219 } while (result == Z_OK);
221 if (result != Z_STREAM_END) {
222 free(buffer);
223 return NULL;
226 git_deflate_end(&stream);
227 *compressed_size = stream.total_out;
229 return buffer;
232 static void write_zip_data_desc(unsigned long size,
233 unsigned long compressed_size,
234 unsigned long crc)
236 if (size >= 0xffffffff || compressed_size >= 0xffffffff) {
237 struct zip64_data_desc trailer;
238 copy_le32(trailer.magic, 0x08074b50);
239 copy_le32(trailer.crc32, crc);
240 copy_le64(trailer.compressed_size, compressed_size);
241 copy_le64(trailer.size, size);
242 write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE);
243 zip_offset += ZIP64_DATA_DESC_SIZE;
244 } else {
245 struct zip_data_desc trailer;
246 copy_le32(trailer.magic, 0x08074b50);
247 copy_le32(trailer.crc32, crc);
248 copy_le32(trailer.compressed_size, compressed_size);
249 copy_le32(trailer.size, size);
250 write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
251 zip_offset += ZIP_DATA_DESC_SIZE;
255 static void set_zip_header_data_desc(struct zip_local_header *header,
256 unsigned long size,
257 unsigned long compressed_size,
258 unsigned long crc)
260 copy_le32(header->crc32, crc);
261 copy_le32(header->compressed_size, compressed_size);
262 copy_le32(header->size, size);
265 static int has_only_ascii(const char *s)
267 for (;;) {
268 int c = *s++;
269 if (c == '\0')
270 return 1;
271 if (!isascii(c))
272 return 0;
276 static int entry_is_binary(struct index_state *istate, const char *path,
277 const void *buffer, size_t size)
279 struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
280 if (!driver)
281 driver = userdiff_find_by_name("default");
282 if (driver->binary != -1)
283 return driver->binary;
284 return buffer_is_binary(buffer, size);
287 #define STREAM_BUFFER_SIZE (1024 * 16)
289 static int write_zip_entry(struct archiver_args *args,
290 const struct object_id *oid,
291 const char *path, size_t pathlen,
292 unsigned int mode,
293 void *buffer, unsigned long size)
295 struct zip_local_header header;
296 uintmax_t offset = zip_offset;
297 struct zip_extra_mtime extra;
298 struct zip64_extra extra64;
299 size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE;
300 int need_zip64_extra = 0;
301 unsigned long attr2;
302 unsigned long compressed_size;
303 unsigned long crc;
304 enum zip_method method;
305 unsigned char *out;
306 void *deflated = NULL;
307 struct git_istream *stream = NULL;
308 unsigned long flags = 0;
309 int is_binary = -1;
310 const char *path_without_prefix = path + args->baselen;
311 unsigned int creator_version = 0;
312 unsigned int version_needed = 10;
313 size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE;
314 size_t zip64_dir_extra_payload_size = 0;
316 crc = crc32(0, NULL, 0);
318 if (!has_only_ascii(path)) {
319 if (is_utf8(path))
320 flags |= ZIP_UTF8;
321 else
322 warning(_("path is not valid UTF-8: %s"), path);
325 if (pathlen > 0xffff) {
326 return error(_("path too long (%d chars, SHA1: %s): %s"),
327 (int)pathlen, oid_to_hex(oid), path);
330 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
331 method = ZIP_METHOD_STORE;
332 attr2 = 16;
333 out = NULL;
334 compressed_size = 0;
335 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
336 method = ZIP_METHOD_STORE;
337 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
338 (mode & 0111) ? ((mode) << 16) : 0;
339 if (S_ISLNK(mode) || (mode & 0111))
340 creator_version = 0x0317;
341 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
342 method = ZIP_METHOD_DEFLATE;
344 if (!buffer) {
345 enum object_type type;
346 stream = open_istream(args->repo, oid, &type, &size,
347 NULL);
348 if (!stream)
349 return error(_("cannot stream blob %s"),
350 oid_to_hex(oid));
351 flags |= ZIP_STREAM;
352 out = NULL;
353 } else {
354 crc = crc32(crc, buffer, size);
355 is_binary = entry_is_binary(args->repo->index,
356 path_without_prefix,
357 buffer, size);
358 out = buffer;
360 compressed_size = (method == ZIP_METHOD_STORE) ? size : 0;
361 } else {
362 return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
363 oid_to_hex(oid));
366 if (creator_version > max_creator_version)
367 max_creator_version = creator_version;
369 if (buffer && method == ZIP_METHOD_DEFLATE) {
370 out = deflated = zlib_deflate_raw(buffer, size,
371 args->compression_level,
372 &compressed_size);
373 if (!out || compressed_size >= size) {
374 out = buffer;
375 method = ZIP_METHOD_STORE;
376 compressed_size = size;
380 copy_le16(extra.magic, 0x5455);
381 copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
382 extra.flags[0] = 1; /* just mtime */
383 copy_le32(extra.mtime, args->time);
385 if (size > 0xffffffff || compressed_size > 0xffffffff)
386 need_zip64_extra = 1;
387 if (stream && size > 0x7fffffff)
388 need_zip64_extra = 1;
390 if (need_zip64_extra)
391 version_needed = 45;
393 copy_le32(header.magic, 0x04034b50);
394 copy_le16(header.version, version_needed);
395 copy_le16(header.flags, flags);
396 copy_le16(header.compression_method, method);
397 copy_le16(header.mtime, zip_time);
398 copy_le16(header.mdate, zip_date);
399 if (need_zip64_extra) {
400 set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc);
401 header_extra_size += ZIP64_EXTRA_SIZE;
402 } else {
403 set_zip_header_data_desc(&header, size, compressed_size, crc);
405 copy_le16(header.filename_length, pathlen);
406 copy_le16(header.extra_length, header_extra_size);
407 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
408 zip_offset += ZIP_LOCAL_HEADER_SIZE;
409 write_or_die(1, path, pathlen);
410 zip_offset += pathlen;
411 write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
412 zip_offset += ZIP_EXTRA_MTIME_SIZE;
413 if (need_zip64_extra) {
414 copy_le16(extra64.magic, 0x0001);
415 copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE);
416 copy_le64(extra64.size, size);
417 copy_le64(extra64.compressed_size, compressed_size);
418 write_or_die(1, &extra64, ZIP64_EXTRA_SIZE);
419 zip_offset += ZIP64_EXTRA_SIZE;
422 if (stream && method == ZIP_METHOD_STORE) {
423 unsigned char buf[STREAM_BUFFER_SIZE];
424 ssize_t readlen;
426 for (;;) {
427 readlen = read_istream(stream, buf, sizeof(buf));
428 if (readlen <= 0)
429 break;
430 crc = crc32(crc, buf, readlen);
431 if (is_binary == -1)
432 is_binary = entry_is_binary(args->repo->index,
433 path_without_prefix,
434 buf, readlen);
435 write_or_die(1, buf, readlen);
437 close_istream(stream);
438 if (readlen)
439 return readlen;
441 compressed_size = size;
442 zip_offset += compressed_size;
444 write_zip_data_desc(size, compressed_size, crc);
445 } else if (stream && method == ZIP_METHOD_DEFLATE) {
446 unsigned char buf[STREAM_BUFFER_SIZE];
447 ssize_t readlen;
448 git_zstream zstream;
449 int result;
450 size_t out_len;
451 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
453 git_deflate_init_raw(&zstream, args->compression_level);
455 compressed_size = 0;
456 zstream.next_out = compressed;
457 zstream.avail_out = sizeof(compressed);
459 for (;;) {
460 readlen = read_istream(stream, buf, sizeof(buf));
461 if (readlen <= 0)
462 break;
463 crc = crc32(crc, buf, readlen);
464 if (is_binary == -1)
465 is_binary = entry_is_binary(args->repo->index,
466 path_without_prefix,
467 buf, readlen);
469 zstream.next_in = buf;
470 zstream.avail_in = readlen;
471 result = git_deflate(&zstream, 0);
472 if (result != Z_OK)
473 die(_("deflate error (%d)"), result);
474 out_len = zstream.next_out - compressed;
476 if (out_len > 0) {
477 write_or_die(1, compressed, out_len);
478 compressed_size += out_len;
479 zstream.next_out = compressed;
480 zstream.avail_out = sizeof(compressed);
484 close_istream(stream);
485 if (readlen)
486 return readlen;
488 zstream.next_in = buf;
489 zstream.avail_in = 0;
490 result = git_deflate(&zstream, Z_FINISH);
491 if (result != Z_STREAM_END)
492 die("deflate error (%d)", result);
494 git_deflate_end(&zstream);
495 out_len = zstream.next_out - compressed;
496 write_or_die(1, compressed, out_len);
497 compressed_size += out_len;
498 zip_offset += compressed_size;
500 write_zip_data_desc(size, compressed_size, crc);
501 } else if (compressed_size > 0) {
502 write_or_die(1, out, compressed_size);
503 zip_offset += compressed_size;
506 free(deflated);
508 if (compressed_size > 0xffffffff || size > 0xffffffff ||
509 offset > 0xffffffff) {
510 if (compressed_size >= 0xffffffff)
511 zip64_dir_extra_payload_size += 8;
512 if (size >= 0xffffffff)
513 zip64_dir_extra_payload_size += 8;
514 if (offset >= 0xffffffff)
515 zip64_dir_extra_payload_size += 8;
516 zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
519 strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
520 strbuf_add_le(&zip_dir, 2, creator_version);
521 strbuf_add_le(&zip_dir, 2, version_needed);
522 strbuf_add_le(&zip_dir, 2, flags);
523 strbuf_add_le(&zip_dir, 2, method);
524 strbuf_add_le(&zip_dir, 2, zip_time);
525 strbuf_add_le(&zip_dir, 2, zip_date);
526 strbuf_add_le(&zip_dir, 4, crc);
527 strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
528 strbuf_add_le(&zip_dir, 4, clamp32(size));
529 strbuf_add_le(&zip_dir, 2, pathlen);
530 strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
531 strbuf_add_le(&zip_dir, 2, 0); /* comment length */
532 strbuf_add_le(&zip_dir, 2, 0); /* disk */
533 strbuf_add_le(&zip_dir, 2, !is_binary);
534 strbuf_add_le(&zip_dir, 4, attr2);
535 strbuf_add_le(&zip_dir, 4, clamp32(offset));
536 strbuf_add(&zip_dir, path, pathlen);
537 strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
538 if (zip64_dir_extra_payload_size) {
539 strbuf_add_le(&zip_dir, 2, 0x0001); /* magic */
540 strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size);
541 if (size >= 0xffffffff)
542 strbuf_add_le(&zip_dir, 8, size);
543 if (compressed_size >= 0xffffffff)
544 strbuf_add_le(&zip_dir, 8, compressed_size);
545 if (offset >= 0xffffffff)
546 strbuf_add_le(&zip_dir, 8, offset);
548 zip_dir_entries++;
550 return 0;
553 static void write_zip64_trailer(void)
555 struct zip64_dir_trailer trailer64;
556 struct zip64_dir_trailer_locator locator64;
558 copy_le32(trailer64.magic, 0x06064b50);
559 copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
560 copy_le16(trailer64.creator_version, max_creator_version);
561 copy_le16(trailer64.version, 45);
562 copy_le32(trailer64.disk, 0);
563 copy_le32(trailer64.directory_start_disk, 0);
564 copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
565 copy_le64(trailer64.entries, zip_dir_entries);
566 copy_le64(trailer64.size, zip_dir.len);
567 copy_le64(trailer64.offset, zip_offset);
569 copy_le32(locator64.magic, 0x07064b50);
570 copy_le32(locator64.disk, 0);
571 copy_le64(locator64.offset, zip_offset + zip_dir.len);
572 copy_le32(locator64.number_of_disks, 1);
574 write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
575 write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
578 static void write_zip_trailer(const struct object_id *oid)
580 struct zip_dir_trailer trailer;
581 int clamped = 0;
583 copy_le32(trailer.magic, 0x06054b50);
584 copy_le16(trailer.disk, 0);
585 copy_le16(trailer.directory_start_disk, 0);
586 copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
587 &clamped);
588 copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
589 copy_le32(trailer.size, zip_dir.len);
590 copy_le32_clamp(trailer.offset, zip_offset, &clamped);
591 copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);
593 write_or_die(1, zip_dir.buf, zip_dir.len);
594 if (clamped)
595 write_zip64_trailer();
596 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
597 if (oid)
598 write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
601 static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
603 time_t time;
604 struct tm tm;
606 if (date_overflows(*timestamp))
607 die(_("timestamp too large for this system: %"PRItime),
608 *timestamp);
609 time = (time_t)*timestamp;
610 localtime_r(&time, &tm);
611 *timestamp = time;
613 *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 +
614 (tm.tm_year + 1900 - 1980) * 512;
615 *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048;
618 static int archive_zip_config(const char *var, const char *value,
619 void *data UNUSED)
621 return userdiff_config(var, value);
624 static int write_zip_archive(const struct archiver *ar UNUSED,
625 struct archiver_args *args)
627 int err;
629 git_config(archive_zip_config, NULL);
631 dos_time(&args->time, &zip_date, &zip_time);
633 strbuf_init(&zip_dir, 0);
635 err = write_archive_entries(args, write_zip_entry);
636 if (!err)
637 write_zip_trailer(args->commit_oid);
639 strbuf_release(&zip_dir);
641 return err;
644 static struct archiver zip_archiver = {
645 .name = "zip",
646 .write_archive = write_zip_archive,
647 .flags = ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE,
650 void init_zip_archiver(void)
652 register_archiver(&zip_archiver);