pack-bitmap-write: use hashwrite_be32() in write_hash_cache()
[git.git] / archive-zip.c
blobe9f426298b6d1c4ce64a1ef537a97375a8855d3c
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"
13 static int zip_date;
14 static int zip_time;
16 /* We only care about the "buf" part here. */
17 static struct strbuf zip_dir;
19 static uintmax_t zip_offset;
20 static uint64_t zip_dir_entries;
22 static unsigned int max_creator_version;
24 #define ZIP_STREAM (1 << 3)
25 #define ZIP_UTF8 (1 << 11)
27 enum zip_method {
28 ZIP_METHOD_STORE = 0,
29 ZIP_METHOD_DEFLATE = 8
32 struct zip_local_header {
33 unsigned char magic[4];
34 unsigned char version[2];
35 unsigned char flags[2];
36 unsigned char compression_method[2];
37 unsigned char mtime[2];
38 unsigned char mdate[2];
39 unsigned char crc32[4];
40 unsigned char compressed_size[4];
41 unsigned char size[4];
42 unsigned char filename_length[2];
43 unsigned char extra_length[2];
44 unsigned char _end[1];
47 struct zip_data_desc {
48 unsigned char magic[4];
49 unsigned char crc32[4];
50 unsigned char compressed_size[4];
51 unsigned char size[4];
52 unsigned char _end[1];
55 struct zip64_data_desc {
56 unsigned char magic[4];
57 unsigned char crc32[4];
58 unsigned char compressed_size[8];
59 unsigned char size[8];
60 unsigned char _end[1];
63 struct zip_dir_trailer {
64 unsigned char magic[4];
65 unsigned char disk[2];
66 unsigned char directory_start_disk[2];
67 unsigned char entries_on_this_disk[2];
68 unsigned char entries[2];
69 unsigned char size[4];
70 unsigned char offset[4];
71 unsigned char comment_length[2];
72 unsigned char _end[1];
75 struct zip_extra_mtime {
76 unsigned char magic[2];
77 unsigned char extra_size[2];
78 unsigned char flags[1];
79 unsigned char mtime[4];
80 unsigned char _end[1];
83 struct zip64_extra {
84 unsigned char magic[2];
85 unsigned char extra_size[2];
86 unsigned char size[8];
87 unsigned char compressed_size[8];
88 unsigned char _end[1];
91 struct zip64_dir_trailer {
92 unsigned char magic[4];
93 unsigned char record_size[8];
94 unsigned char creator_version[2];
95 unsigned char version[2];
96 unsigned char disk[4];
97 unsigned char directory_start_disk[4];
98 unsigned char entries_on_this_disk[8];
99 unsigned char entries[8];
100 unsigned char size[8];
101 unsigned char offset[8];
102 unsigned char _end[1];
105 struct zip64_dir_trailer_locator {
106 unsigned char magic[4];
107 unsigned char disk[4];
108 unsigned char offset[8];
109 unsigned char number_of_disks[4];
110 unsigned char _end[1];
114 * On ARM, padding is added at the end of the struct, so a simple
115 * sizeof(struct ...) reports two bytes more than the payload size
116 * we're interested in.
118 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
119 #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
120 #define ZIP64_DATA_DESC_SIZE offsetof(struct zip64_data_desc, _end)
121 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
122 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
123 #define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end)
124 #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
125 (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
126 #define ZIP64_EXTRA_SIZE offsetof(struct zip64_extra, _end)
127 #define ZIP64_EXTRA_PAYLOAD_SIZE \
128 (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size))
129 #define ZIP64_DIR_TRAILER_SIZE offsetof(struct zip64_dir_trailer, _end)
130 #define ZIP64_DIR_TRAILER_RECORD_SIZE \
131 (ZIP64_DIR_TRAILER_SIZE - \
132 offsetof(struct zip64_dir_trailer, creator_version))
133 #define ZIP64_DIR_TRAILER_LOCATOR_SIZE \
134 offsetof(struct zip64_dir_trailer_locator, _end)
136 static void copy_le16(unsigned char *dest, unsigned int n)
138 dest[0] = 0xff & n;
139 dest[1] = 0xff & (n >> 010);
142 static void copy_le32(unsigned char *dest, unsigned int n)
144 dest[0] = 0xff & n;
145 dest[1] = 0xff & (n >> 010);
146 dest[2] = 0xff & (n >> 020);
147 dest[3] = 0xff & (n >> 030);
150 static void copy_le64(unsigned char *dest, uint64_t n)
152 dest[0] = 0xff & n;
153 dest[1] = 0xff & (n >> 010);
154 dest[2] = 0xff & (n >> 020);
155 dest[3] = 0xff & (n >> 030);
156 dest[4] = 0xff & (n >> 040);
157 dest[5] = 0xff & (n >> 050);
158 dest[6] = 0xff & (n >> 060);
159 dest[7] = 0xff & (n >> 070);
162 static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped)
164 if (n <= max)
165 return n;
166 *clamped = 1;
167 return max;
170 static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
172 copy_le16(dest, clamp_max(n, 0xffff, clamped));
175 static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped)
177 copy_le32(dest, clamp_max(n, 0xffffffff, clamped));
180 static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n)
182 while (size-- > 0) {
183 strbuf_addch(sb, n & 0xff);
184 n >>= 8;
186 return -!!n;
189 static uint32_t clamp32(uintmax_t n)
191 const uintmax_t max = 0xffffffff;
192 return (n < max) ? n : max;
195 static void *zlib_deflate_raw(void *data, unsigned long size,
196 int compression_level,
197 unsigned long *compressed_size)
199 git_zstream stream;
200 unsigned long maxsize;
201 void *buffer;
202 int result;
204 git_deflate_init_raw(&stream, compression_level);
205 maxsize = git_deflate_bound(&stream, size);
206 buffer = xmalloc(maxsize);
208 stream.next_in = data;
209 stream.avail_in = size;
210 stream.next_out = buffer;
211 stream.avail_out = maxsize;
213 do {
214 result = git_deflate(&stream, Z_FINISH);
215 } while (result == Z_OK);
217 if (result != Z_STREAM_END) {
218 free(buffer);
219 return NULL;
222 git_deflate_end(&stream);
223 *compressed_size = stream.total_out;
225 return buffer;
228 static void write_zip_data_desc(unsigned long size,
229 unsigned long compressed_size,
230 unsigned long crc)
232 if (size >= 0xffffffff || compressed_size >= 0xffffffff) {
233 struct zip64_data_desc trailer;
234 copy_le32(trailer.magic, 0x08074b50);
235 copy_le32(trailer.crc32, crc);
236 copy_le64(trailer.compressed_size, compressed_size);
237 copy_le64(trailer.size, size);
238 write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE);
239 zip_offset += ZIP64_DATA_DESC_SIZE;
240 } else {
241 struct zip_data_desc trailer;
242 copy_le32(trailer.magic, 0x08074b50);
243 copy_le32(trailer.crc32, crc);
244 copy_le32(trailer.compressed_size, compressed_size);
245 copy_le32(trailer.size, size);
246 write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
247 zip_offset += ZIP_DATA_DESC_SIZE;
251 static void set_zip_header_data_desc(struct zip_local_header *header,
252 unsigned long size,
253 unsigned long compressed_size,
254 unsigned long crc)
256 copy_le32(header->crc32, crc);
257 copy_le32(header->compressed_size, compressed_size);
258 copy_le32(header->size, size);
261 static int has_only_ascii(const char *s)
263 for (;;) {
264 int c = *s++;
265 if (c == '\0')
266 return 1;
267 if (!isascii(c))
268 return 0;
272 static int entry_is_binary(struct index_state *istate, const char *path,
273 const void *buffer, size_t size)
275 struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
276 if (!driver)
277 driver = userdiff_find_by_name("default");
278 if (driver->binary != -1)
279 return driver->binary;
280 return buffer_is_binary(buffer, size);
283 #define STREAM_BUFFER_SIZE (1024 * 16)
285 static int write_zip_entry(struct archiver_args *args,
286 const struct object_id *oid,
287 const char *path, size_t pathlen,
288 unsigned int mode)
290 struct zip_local_header header;
291 uintmax_t offset = zip_offset;
292 struct zip_extra_mtime extra;
293 struct zip64_extra extra64;
294 size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE;
295 int need_zip64_extra = 0;
296 unsigned long attr2;
297 unsigned long compressed_size;
298 unsigned long crc;
299 enum zip_method method;
300 unsigned char *out;
301 void *deflated = NULL;
302 void *buffer;
303 struct git_istream *stream = NULL;
304 unsigned long flags = 0;
305 unsigned long size;
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 size = 0;
332 compressed_size = 0;
333 buffer = NULL;
334 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
335 enum object_type type = oid_object_info(args->repo, oid,
336 &size);
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 (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
347 size > big_file_threshold) {
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 = buffer = NULL;
355 } else {
356 buffer = object_file_to_archive(args, path, oid, mode,
357 &type, &size);
358 if (!buffer)
359 return error(_("cannot read %s"),
360 oid_to_hex(oid));
361 crc = crc32(crc, buffer, size);
362 is_binary = entry_is_binary(args->repo->index,
363 path_without_prefix,
364 buffer, size);
365 out = buffer;
367 compressed_size = (method == ZIP_METHOD_STORE) ? size : 0;
368 } else {
369 return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
370 oid_to_hex(oid));
373 if (creator_version > max_creator_version)
374 max_creator_version = creator_version;
376 if (buffer && method == ZIP_METHOD_DEFLATE) {
377 out = deflated = zlib_deflate_raw(buffer, size,
378 args->compression_level,
379 &compressed_size);
380 if (!out || compressed_size >= size) {
381 out = buffer;
382 method = ZIP_METHOD_STORE;
383 compressed_size = size;
387 copy_le16(extra.magic, 0x5455);
388 copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
389 extra.flags[0] = 1; /* just mtime */
390 copy_le32(extra.mtime, args->time);
392 if (size > 0xffffffff || compressed_size > 0xffffffff)
393 need_zip64_extra = 1;
394 if (stream && size > 0x7fffffff)
395 need_zip64_extra = 1;
397 if (need_zip64_extra)
398 version_needed = 45;
400 copy_le32(header.magic, 0x04034b50);
401 copy_le16(header.version, version_needed);
402 copy_le16(header.flags, flags);
403 copy_le16(header.compression_method, method);
404 copy_le16(header.mtime, zip_time);
405 copy_le16(header.mdate, zip_date);
406 if (need_zip64_extra) {
407 set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc);
408 header_extra_size += ZIP64_EXTRA_SIZE;
409 } else {
410 set_zip_header_data_desc(&header, size, compressed_size, crc);
412 copy_le16(header.filename_length, pathlen);
413 copy_le16(header.extra_length, header_extra_size);
414 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
415 zip_offset += ZIP_LOCAL_HEADER_SIZE;
416 write_or_die(1, path, pathlen);
417 zip_offset += pathlen;
418 write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
419 zip_offset += ZIP_EXTRA_MTIME_SIZE;
420 if (need_zip64_extra) {
421 copy_le16(extra64.magic, 0x0001);
422 copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE);
423 copy_le64(extra64.size, size);
424 copy_le64(extra64.compressed_size, compressed_size);
425 write_or_die(1, &extra64, ZIP64_EXTRA_SIZE);
426 zip_offset += ZIP64_EXTRA_SIZE;
429 if (stream && method == ZIP_METHOD_STORE) {
430 unsigned char buf[STREAM_BUFFER_SIZE];
431 ssize_t readlen;
433 for (;;) {
434 readlen = read_istream(stream, buf, sizeof(buf));
435 if (readlen <= 0)
436 break;
437 crc = crc32(crc, buf, readlen);
438 if (is_binary == -1)
439 is_binary = entry_is_binary(args->repo->index,
440 path_without_prefix,
441 buf, readlen);
442 write_or_die(1, buf, readlen);
444 close_istream(stream);
445 if (readlen)
446 return readlen;
448 compressed_size = size;
449 zip_offset += compressed_size;
451 write_zip_data_desc(size, compressed_size, crc);
452 } else if (stream && method == ZIP_METHOD_DEFLATE) {
453 unsigned char buf[STREAM_BUFFER_SIZE];
454 ssize_t readlen;
455 git_zstream zstream;
456 int result;
457 size_t out_len;
458 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
460 git_deflate_init_raw(&zstream, args->compression_level);
462 compressed_size = 0;
463 zstream.next_out = compressed;
464 zstream.avail_out = sizeof(compressed);
466 for (;;) {
467 readlen = read_istream(stream, buf, sizeof(buf));
468 if (readlen <= 0)
469 break;
470 crc = crc32(crc, buf, readlen);
471 if (is_binary == -1)
472 is_binary = entry_is_binary(args->repo->index,
473 path_without_prefix,
474 buf, readlen);
476 zstream.next_in = buf;
477 zstream.avail_in = readlen;
478 result = git_deflate(&zstream, 0);
479 if (result != Z_OK)
480 die(_("deflate error (%d)"), result);
481 out_len = zstream.next_out - compressed;
483 if (out_len > 0) {
484 write_or_die(1, compressed, out_len);
485 compressed_size += out_len;
486 zstream.next_out = compressed;
487 zstream.avail_out = sizeof(compressed);
491 close_istream(stream);
492 if (readlen)
493 return readlen;
495 zstream.next_in = buf;
496 zstream.avail_in = 0;
497 result = git_deflate(&zstream, Z_FINISH);
498 if (result != Z_STREAM_END)
499 die("deflate error (%d)", result);
501 git_deflate_end(&zstream);
502 out_len = zstream.next_out - compressed;
503 write_or_die(1, compressed, out_len);
504 compressed_size += out_len;
505 zip_offset += compressed_size;
507 write_zip_data_desc(size, compressed_size, crc);
508 } else if (compressed_size > 0) {
509 write_or_die(1, out, compressed_size);
510 zip_offset += compressed_size;
513 free(deflated);
514 free(buffer);
516 if (compressed_size > 0xffffffff || size > 0xffffffff ||
517 offset > 0xffffffff) {
518 if (compressed_size >= 0xffffffff)
519 zip64_dir_extra_payload_size += 8;
520 if (size >= 0xffffffff)
521 zip64_dir_extra_payload_size += 8;
522 if (offset >= 0xffffffff)
523 zip64_dir_extra_payload_size += 8;
524 zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
527 strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
528 strbuf_add_le(&zip_dir, 2, creator_version);
529 strbuf_add_le(&zip_dir, 2, version_needed);
530 strbuf_add_le(&zip_dir, 2, flags);
531 strbuf_add_le(&zip_dir, 2, method);
532 strbuf_add_le(&zip_dir, 2, zip_time);
533 strbuf_add_le(&zip_dir, 2, zip_date);
534 strbuf_add_le(&zip_dir, 4, crc);
535 strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
536 strbuf_add_le(&zip_dir, 4, clamp32(size));
537 strbuf_add_le(&zip_dir, 2, pathlen);
538 strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
539 strbuf_add_le(&zip_dir, 2, 0); /* comment length */
540 strbuf_add_le(&zip_dir, 2, 0); /* disk */
541 strbuf_add_le(&zip_dir, 2, !is_binary);
542 strbuf_add_le(&zip_dir, 4, attr2);
543 strbuf_add_le(&zip_dir, 4, clamp32(offset));
544 strbuf_add(&zip_dir, path, pathlen);
545 strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
546 if (zip64_dir_extra_payload_size) {
547 strbuf_add_le(&zip_dir, 2, 0x0001); /* magic */
548 strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size);
549 if (size >= 0xffffffff)
550 strbuf_add_le(&zip_dir, 8, size);
551 if (compressed_size >= 0xffffffff)
552 strbuf_add_le(&zip_dir, 8, compressed_size);
553 if (offset >= 0xffffffff)
554 strbuf_add_le(&zip_dir, 8, offset);
556 zip_dir_entries++;
558 return 0;
561 static void write_zip64_trailer(void)
563 struct zip64_dir_trailer trailer64;
564 struct zip64_dir_trailer_locator locator64;
566 copy_le32(trailer64.magic, 0x06064b50);
567 copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
568 copy_le16(trailer64.creator_version, max_creator_version);
569 copy_le16(trailer64.version, 45);
570 copy_le32(trailer64.disk, 0);
571 copy_le32(trailer64.directory_start_disk, 0);
572 copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
573 copy_le64(trailer64.entries, zip_dir_entries);
574 copy_le64(trailer64.size, zip_dir.len);
575 copy_le64(trailer64.offset, zip_offset);
577 copy_le32(locator64.magic, 0x07064b50);
578 copy_le32(locator64.disk, 0);
579 copy_le64(locator64.offset, zip_offset + zip_dir.len);
580 copy_le32(locator64.number_of_disks, 1);
582 write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
583 write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
586 static void write_zip_trailer(const struct object_id *oid)
588 struct zip_dir_trailer trailer;
589 int clamped = 0;
591 copy_le32(trailer.magic, 0x06054b50);
592 copy_le16(trailer.disk, 0);
593 copy_le16(trailer.directory_start_disk, 0);
594 copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
595 &clamped);
596 copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
597 copy_le32(trailer.size, zip_dir.len);
598 copy_le32_clamp(trailer.offset, zip_offset, &clamped);
599 copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);
601 write_or_die(1, zip_dir.buf, zip_dir.len);
602 if (clamped)
603 write_zip64_trailer();
604 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
605 if (oid)
606 write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
609 static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
611 time_t time;
612 struct tm tm;
614 if (date_overflows(*timestamp))
615 die(_("timestamp too large for this system: %"PRItime),
616 *timestamp);
617 time = (time_t)*timestamp;
618 localtime_r(&time, &tm);
619 *timestamp = time;
621 *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 +
622 (tm.tm_year + 1900 - 1980) * 512;
623 *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048;
626 static int archive_zip_config(const char *var, const char *value, void *data)
628 return userdiff_config(var, value);
631 static int write_zip_archive(const struct archiver *ar,
632 struct archiver_args *args)
634 int err;
636 git_config(archive_zip_config, NULL);
638 dos_time(&args->time, &zip_date, &zip_time);
640 strbuf_init(&zip_dir, 0);
642 err = write_archive_entries(args, write_zip_entry);
643 if (!err)
644 write_zip_trailer(args->commit_oid);
646 strbuf_release(&zip_dir);
648 return err;
651 static struct archiver zip_archiver = {
652 "zip",
653 write_zip_archive,
654 ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
657 void init_zip_archiver(void)
659 register_archiver(&zip_archiver);