Merge branch 'ma/user-manual-markup-update'
[git/git.git] / archive-zip.c
CommitLineData
e4fbbfe9
RS
1/*
2 * Copyright (c) 2006 Rene Scharfe
3 */
e4fbbfe9 4#include "cache.h"
b2141fc1 5#include "config.h"
ec06bff5 6#include "archive.h"
2158f883 7#include "streaming.h"
88182bab 8#include "utf8.h"
cbd53a21 9#include "object-store.h"
4aff646d
RS
10#include "userdiff.h"
11#include "xdiff-interface.h"
e4fbbfe9 12
e4fbbfe9
RS
13static int zip_date;
14static int zip_time;
15
c061a149
RS
16/* We only care about the "buf" part here. */
17static struct strbuf zip_dir;
e4fbbfe9 18
af95749f 19static uintmax_t zip_offset;
88329ca8
RS
20static uint64_t zip_dir_entries;
21
22static unsigned int max_creator_version;
e4fbbfe9 23
88182bab
RS
24#define ZIP_STREAM (1 << 3)
25#define ZIP_UTF8 (1 << 11)
e4fbbfe9
RS
26
27struct zip_local_header {
28 unsigned char magic[4];
29 unsigned char version[2];
30 unsigned char flags[2];
31 unsigned char compression_method[2];
32 unsigned char mtime[2];
33 unsigned char mdate[2];
34 unsigned char crc32[4];
35 unsigned char compressed_size[4];
36 unsigned char size[4];
37 unsigned char filename_length[2];
38 unsigned char extra_length[2];
0ea865ce 39 unsigned char _end[1];
e4fbbfe9
RS
40};
41
2158f883
RS
42struct zip_data_desc {
43 unsigned char magic[4];
44 unsigned char crc32[4];
45 unsigned char compressed_size[4];
46 unsigned char size[4];
47 unsigned char _end[1];
48};
49
4cdf3f9d
RS
50struct zip64_data_desc {
51 unsigned char magic[4];
52 unsigned char crc32[4];
53 unsigned char compressed_size[8];
54 unsigned char size[8];
55 unsigned char _end[1];
56};
57
e4fbbfe9
RS
58struct zip_dir_trailer {
59 unsigned char magic[4];
60 unsigned char disk[2];
61 unsigned char directory_start_disk[2];
62 unsigned char entries_on_this_disk[2];
63 unsigned char entries[2];
64 unsigned char size[4];
65 unsigned char offset[4];
66 unsigned char comment_length[2];
0ea865ce 67 unsigned char _end[1];
e4fbbfe9
RS
68};
69
227bf598
RS
70struct zip_extra_mtime {
71 unsigned char magic[2];
72 unsigned char extra_size[2];
73 unsigned char flags[1];
74 unsigned char mtime[4];
75 unsigned char _end[1];
76};
77
4cdf3f9d
RS
78struct zip64_extra {
79 unsigned char magic[2];
80 unsigned char extra_size[2];
81 unsigned char size[8];
82 unsigned char compressed_size[8];
83 unsigned char _end[1];
84};
85
88329ca8
RS
86struct zip64_dir_trailer {
87 unsigned char magic[4];
88 unsigned char record_size[8];
89 unsigned char creator_version[2];
90 unsigned char version[2];
91 unsigned char disk[4];
92 unsigned char directory_start_disk[4];
93 unsigned char entries_on_this_disk[8];
94 unsigned char entries[8];
95 unsigned char size[8];
96 unsigned char offset[8];
97 unsigned char _end[1];
98};
99
100struct zip64_dir_trailer_locator {
101 unsigned char magic[4];
102 unsigned char disk[4];
103 unsigned char offset[8];
104 unsigned char number_of_disks[4];
105 unsigned char _end[1];
106};
107
0ea865ce
RS
108/*
109 * On ARM, padding is added at the end of the struct, so a simple
110 * sizeof(struct ...) reports two bytes more than the payload size
111 * we're interested in.
112 */
113#define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
2158f883 114#define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
4cdf3f9d 115#define ZIP64_DATA_DESC_SIZE offsetof(struct zip64_data_desc, _end)
0ea865ce
RS
116#define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
117#define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
227bf598
RS
118#define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end)
119#define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
120 (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
4cdf3f9d
RS
121#define ZIP64_EXTRA_SIZE offsetof(struct zip64_extra, _end)
122#define ZIP64_EXTRA_PAYLOAD_SIZE \
123 (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size))
88329ca8
RS
124#define ZIP64_DIR_TRAILER_SIZE offsetof(struct zip64_dir_trailer, _end)
125#define ZIP64_DIR_TRAILER_RECORD_SIZE \
126 (ZIP64_DIR_TRAILER_SIZE - \
127 offsetof(struct zip64_dir_trailer, creator_version))
128#define ZIP64_DIR_TRAILER_LOCATOR_SIZE \
129 offsetof(struct zip64_dir_trailer_locator, _end)
0ea865ce 130
e4fbbfe9
RS
131static void copy_le16(unsigned char *dest, unsigned int n)
132{
133 dest[0] = 0xff & n;
134 dest[1] = 0xff & (n >> 010);
135}
136
137static void copy_le32(unsigned char *dest, unsigned int n)
138{
139 dest[0] = 0xff & n;
140 dest[1] = 0xff & (n >> 010);
141 dest[2] = 0xff & (n >> 020);
142 dest[3] = 0xff & (n >> 030);
143}
144
88329ca8
RS
145static void copy_le64(unsigned char *dest, uint64_t n)
146{
147 dest[0] = 0xff & n;
148 dest[1] = 0xff & (n >> 010);
149 dest[2] = 0xff & (n >> 020);
150 dest[3] = 0xff & (n >> 030);
151 dest[4] = 0xff & (n >> 040);
152 dest[5] = 0xff & (n >> 050);
153 dest[6] = 0xff & (n >> 060);
154 dest[7] = 0xff & (n >> 070);
155}
156
157static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped)
158{
159 if (n <= max)
160 return n;
161 *clamped = 1;
162 return max;
163}
164
165static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
166{
167 copy_le16(dest, clamp_max(n, 0xffff, clamped));
168}
169
af95749f
RS
170static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped)
171{
172 copy_le32(dest, clamp_max(n, 0xffffffff, clamped));
173}
174
3c78fd80
RS
175static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n)
176{
177 while (size-- > 0) {
178 strbuf_addch(sb, n & 0xff);
179 n >>= 8;
180 }
181 return -!!n;
182}
183
af95749f
RS
184static uint32_t clamp32(uintmax_t n)
185{
186 const uintmax_t max = 0xffffffff;
187 return (n < max) ? n : max;
188}
189
c3c2e1a0
RS
190static void *zlib_deflate_raw(void *data, unsigned long size,
191 int compression_level,
192 unsigned long *compressed_size)
e4fbbfe9 193{
ef49a7a0 194 git_zstream stream;
e4fbbfe9
RS
195 unsigned long maxsize;
196 void *buffer;
197 int result;
198
c3c2e1a0 199 git_deflate_init_raw(&stream, compression_level);
225a6f10 200 maxsize = git_deflate_bound(&stream, size);
e4fbbfe9
RS
201 buffer = xmalloc(maxsize);
202
203 stream.next_in = data;
204 stream.avail_in = size;
205 stream.next_out = buffer;
206 stream.avail_out = maxsize;
207
208 do {
55bb5c91 209 result = git_deflate(&stream, Z_FINISH);
e4fbbfe9
RS
210 } while (result == Z_OK);
211
212 if (result != Z_STREAM_END) {
213 free(buffer);
214 return NULL;
215 }
216
55bb5c91 217 git_deflate_end(&stream);
e4fbbfe9
RS
218 *compressed_size = stream.total_out;
219
220 return buffer;
221}
222
2158f883
RS
223static void write_zip_data_desc(unsigned long size,
224 unsigned long compressed_size,
225 unsigned long crc)
226{
4cdf3f9d
RS
227 if (size >= 0xffffffff || compressed_size >= 0xffffffff) {
228 struct zip64_data_desc trailer;
229 copy_le32(trailer.magic, 0x08074b50);
230 copy_le32(trailer.crc32, crc);
231 copy_le64(trailer.compressed_size, compressed_size);
232 copy_le64(trailer.size, size);
233 write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE);
234 zip_offset += ZIP64_DATA_DESC_SIZE;
235 } else {
236 struct zip_data_desc trailer;
237 copy_le32(trailer.magic, 0x08074b50);
238 copy_le32(trailer.crc32, crc);
239 copy_le32(trailer.compressed_size, compressed_size);
240 copy_le32(trailer.size, size);
241 write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
242 zip_offset += ZIP_DATA_DESC_SIZE;
243 }
2158f883
RS
244}
245
ebf5374a
RS
246static void set_zip_header_data_desc(struct zip_local_header *header,
247 unsigned long size,
248 unsigned long compressed_size,
249 unsigned long crc)
250{
251 copy_le32(header->crc32, crc);
252 copy_le32(header->compressed_size, compressed_size);
253 copy_le32(header->size, size);
254}
255
88182bab
RS
256static int has_only_ascii(const char *s)
257{
258 for (;;) {
259 int c = *s++;
260 if (c == '\0')
261 return 1;
262 if (!isascii(c))
263 return 0;
264 }
265}
266
acd00ea0
NTND
267static int entry_is_binary(struct index_state *istate, const char *path,
268 const void *buffer, size_t size)
4aff646d 269{
acd00ea0 270 struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
4aff646d
RS
271 if (!driver)
272 driver = userdiff_find_by_name("default");
273 if (driver->binary != -1)
274 return driver->binary;
275 return buffer_is_binary(buffer, size);
276}
277
2158f883
RS
278#define STREAM_BUFFER_SIZE (1024 * 16)
279
562e25ab 280static int write_zip_entry(struct archiver_args *args,
015ff4f8 281 const struct object_id *oid,
9cb513b7
NTND
282 const char *path, size_t pathlen,
283 unsigned int mode)
e4fbbfe9
RS
284{
285 struct zip_local_header header;
3c78fd80 286 uintmax_t offset = zip_offset;
227bf598 287 struct zip_extra_mtime extra;
4cdf3f9d
RS
288 struct zip64_extra extra64;
289 size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE;
290 int need_zip64_extra = 0;
bb52d22e 291 unsigned long attr2;
e4fbbfe9 292 unsigned long compressed_size;
e4fbbfe9 293 unsigned long crc;
e4fbbfe9 294 int method;
e4fbbfe9 295 unsigned char *out;
e4fbbfe9 296 void *deflated = NULL;
9cb513b7 297 void *buffer;
2158f883
RS
298 struct git_istream *stream = NULL;
299 unsigned long flags = 0;
9cb513b7 300 unsigned long size;
4aff646d
RS
301 int is_binary = -1;
302 const char *path_without_prefix = path + args->baselen;
0f747f9d 303 unsigned int creator_version = 0;
ebdfa294 304 unsigned int version_needed = 10;
af95749f
RS
305 size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE;
306 size_t zip64_dir_extra_payload_size = 0;
e4fbbfe9 307
38f4d138 308 crc = crc32(0, NULL, 0);
e4fbbfe9 309
88182bab
RS
310 if (!has_only_ascii(path)) {
311 if (is_utf8(path))
312 flags |= ZIP_UTF8;
313 else
02f3fe5a 314 warning(_("path is not valid UTF-8: %s"), path);
88182bab
RS
315 }
316
e4fbbfe9 317 if (pathlen > 0xffff) {
02f3fe5a 318 return error(_("path too long (%d chars, SHA1: %s): %s"),
015ff4f8 319 (int)pathlen, oid_to_hex(oid), path);
e4fbbfe9
RS
320 }
321
302b9282 322 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
e4fbbfe9 323 method = 0;
62cdce17 324 attr2 = 16;
e4fbbfe9 325 out = NULL;
60df6bd1 326 size = 0;
e4fbbfe9 327 compressed_size = 0;
9cb513b7 328 buffer = NULL;
62cdce17 329 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
b67b5512 330 enum object_type type = oid_object_info(args->repo, oid,
0df8e965 331 &size);
9cb513b7 332
62cdce17 333 method = 0;
bb52d22e
JH
334 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
335 (mode & 0111) ? ((mode) << 16) : 0;
0f747f9d
RS
336 if (S_ISLNK(mode) || (mode & 0111))
337 creator_version = 0x0317;
2158f883 338 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
62cdce17 339 method = 8;
2158f883
RS
340
341 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
c743c215 342 size > big_file_threshold) {
ef7b5195 343 stream = open_istream(oid, &type, &size, NULL);
2158f883 344 if (!stream)
02f3fe5a 345 return error(_("cannot stream blob %s"),
015ff4f8 346 oid_to_hex(oid));
2158f883
RS
347 flags |= ZIP_STREAM;
348 out = buffer = NULL;
349 } else {
e5ec981a 350 buffer = object_file_to_archive(args, path, oid, mode,
351 &type, &size);
2158f883 352 if (!buffer)
02f3fe5a 353 return error(_("cannot read %s"),
015ff4f8 354 oid_to_hex(oid));
2158f883 355 crc = crc32(crc, buffer, size);
acd00ea0
NTND
356 is_binary = entry_is_binary(args->repo->index,
357 path_without_prefix,
4aff646d 358 buffer, size);
2158f883
RS
359 out = buffer;
360 }
d3c1472f 361 compressed_size = (method == 0) ? size : 0;
e4fbbfe9 362 } else {
02f3fe5a 363 return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
015ff4f8 364 oid_to_hex(oid));
e4fbbfe9
RS
365 }
366
88329ca8
RS
367 if (creator_version > max_creator_version)
368 max_creator_version = creator_version;
369
2158f883 370 if (buffer && method == 8) {
c3c2e1a0
RS
371 out = deflated = zlib_deflate_raw(buffer, size,
372 args->compression_level,
373 &compressed_size);
374 if (!out || compressed_size >= size) {
375 out = buffer;
e4fbbfe9
RS
376 method = 0;
377 compressed_size = size;
378 }
379 }
380
227bf598
RS
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);
385
4cdf3f9d
RS
386 if (size > 0xffffffff || compressed_size > 0xffffffff)
387 need_zip64_extra = 1;
388 if (stream && size > 0x7fffffff)
389 need_zip64_extra = 1;
390
ebdfa294
RS
391 if (need_zip64_extra)
392 version_needed = 45;
393
e4fbbfe9 394 copy_le32(header.magic, 0x04034b50);
ebdfa294 395 copy_le16(header.version, version_needed);
2158f883 396 copy_le16(header.flags, flags);
e4fbbfe9
RS
397 copy_le16(header.compression_method, method);
398 copy_le16(header.mtime, zip_time);
399 copy_le16(header.mdate, zip_date);
4cdf3f9d
RS
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);
405 }
e4fbbfe9 406 copy_le16(header.filename_length, pathlen);
4cdf3f9d 407 copy_le16(header.extra_length, header_extra_size);
0ea865ce
RS
408 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
409 zip_offset += ZIP_LOCAL_HEADER_SIZE;
e4fbbfe9
RS
410 write_or_die(1, path, pathlen);
411 zip_offset += pathlen;
227bf598
RS
412 write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
413 zip_offset += ZIP_EXTRA_MTIME_SIZE;
4cdf3f9d
RS
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;
421 }
422
2158f883
RS
423 if (stream && method == 0) {
424 unsigned char buf[STREAM_BUFFER_SIZE];
425 ssize_t readlen;
426
427 for (;;) {
428 readlen = read_istream(stream, buf, sizeof(buf));
429 if (readlen <= 0)
430 break;
431 crc = crc32(crc, buf, readlen);
4aff646d 432 if (is_binary == -1)
acd00ea0
NTND
433 is_binary = entry_is_binary(args->repo->index,
434 path_without_prefix,
4aff646d 435 buf, readlen);
2158f883
RS
436 write_or_die(1, buf, readlen);
437 }
438 close_istream(stream);
439 if (readlen)
440 return readlen;
441
442 compressed_size = size;
443 zip_offset += compressed_size;
444
445 write_zip_data_desc(size, compressed_size, crc);
c743c215
RS
446 } else if (stream && method == 8) {
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];
453
c3c2e1a0 454 git_deflate_init_raw(&zstream, args->compression_level);
c743c215
RS
455
456 compressed_size = 0;
457 zstream.next_out = compressed;
458 zstream.avail_out = sizeof(compressed);
459
460 for (;;) {
461 readlen = read_istream(stream, buf, sizeof(buf));
462 if (readlen <= 0)
463 break;
464 crc = crc32(crc, buf, readlen);
4aff646d 465 if (is_binary == -1)
acd00ea0
NTND
466 is_binary = entry_is_binary(args->repo->index,
467 path_without_prefix,
4aff646d 468 buf, readlen);
c743c215
RS
469
470 zstream.next_in = buf;
471 zstream.avail_in = readlen;
472 result = git_deflate(&zstream, 0);
473 if (result != Z_OK)
02f3fe5a 474 die(_("deflate error (%d)"), result);
c3c2e1a0 475 out_len = zstream.next_out - compressed;
c743c215
RS
476
477 if (out_len > 0) {
c3c2e1a0 478 write_or_die(1, compressed, out_len);
c743c215
RS
479 compressed_size += out_len;
480 zstream.next_out = compressed;
481 zstream.avail_out = sizeof(compressed);
482 }
483
484 }
485 close_istream(stream);
486 if (readlen)
487 return readlen;
488
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);
494
495 git_deflate_end(&zstream);
c3c2e1a0
RS
496 out_len = zstream.next_out - compressed;
497 write_or_die(1, compressed, out_len);
c743c215
RS
498 compressed_size += out_len;
499 zip_offset += compressed_size;
500
501 write_zip_data_desc(size, compressed_size, crc);
2158f883 502 } else if (compressed_size > 0) {
e4fbbfe9
RS
503 write_or_die(1, out, compressed_size);
504 zip_offset += compressed_size;
505 }
506
e4fbbfe9 507 free(deflated);
9cb513b7 508 free(buffer);
e4fbbfe9 509
4cdf3f9d
RS
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;
af95749f
RS
518 zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
519 }
520
3c78fd80
RS
521 strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
522 strbuf_add_le(&zip_dir, 2, creator_version);
ebdfa294 523 strbuf_add_le(&zip_dir, 2, version_needed);
3c78fd80
RS
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);
4cdf3f9d
RS
529 strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
530 strbuf_add_le(&zip_dir, 4, clamp32(size));
3c78fd80 531 strbuf_add_le(&zip_dir, 2, pathlen);
af95749f 532 strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
3c78fd80
RS
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);
af95749f 537 strbuf_add_le(&zip_dir, 4, clamp32(offset));
c061a149
RS
538 strbuf_add(&zip_dir, path, pathlen);
539 strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
af95749f
RS
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);
4cdf3f9d
RS
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);
af95749f
RS
547 if (offset >= 0xffffffff)
548 strbuf_add_le(&zip_dir, 8, offset);
549 }
ebf5374a
RS
550 zip_dir_entries++;
551
562e25ab 552 return 0;
e4fbbfe9
RS
553}
554
88329ca8
RS
555static void write_zip64_trailer(void)
556{
557 struct zip64_dir_trailer trailer64;
558 struct zip64_dir_trailer_locator locator64;
559
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);
c061a149 568 copy_le64(trailer64.size, zip_dir.len);
88329ca8
RS
569 copy_le64(trailer64.offset, zip_offset);
570
571 copy_le32(locator64.magic, 0x07064b50);
572 copy_le32(locator64.disk, 0);
c061a149 573 copy_le64(locator64.offset, zip_offset + zip_dir.len);
88329ca8
RS
574 copy_le32(locator64.number_of_disks, 1);
575
576 write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
577 write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
578}
579
bbf05cf7 580static void write_zip_trailer(const struct object_id *oid)
e4fbbfe9
RS
581{
582 struct zip_dir_trailer trailer;
88329ca8 583 int clamped = 0;
e4fbbfe9
RS
584
585 copy_le32(trailer.magic, 0x06054b50);
586 copy_le16(trailer.disk, 0);
587 copy_le16(trailer.directory_start_disk, 0);
88329ca8
RS
588 copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
589 &clamped);
590 copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
c061a149 591 copy_le32(trailer.size, zip_dir.len);
af95749f 592 copy_le32_clamp(trailer.offset, zip_offset, &clamped);
bbf05cf7 593 copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);
e4fbbfe9 594
c061a149 595 write_or_die(1, zip_dir.buf, zip_dir.len);
88329ca8
RS
596 if (clamped)
597 write_zip64_trailer();
0ea865ce 598 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
bbf05cf7 599 if (oid)
600 write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
e4fbbfe9
RS
601}
602
dddbad72 603static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
e4fbbfe9 604{
dddbad72
JS
605 time_t time;
606 struct tm *t;
607
608 if (date_overflows(*timestamp))
02f3fe5a 609 die(_("timestamp too large for this system: %"PRItime),
dddbad72
JS
610 *timestamp);
611 time = (time_t)*timestamp;
612 t = localtime(&time);
613 *timestamp = time;
e4fbbfe9
RS
614
615 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
616 (t->tm_year + 1900 - 1980) * 512;
617 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
618}
619
965cba2e
JK
620static int archive_zip_config(const char *var, const char *value, void *data)
621{
622 return userdiff_config(var, value);
623}
624
4d7c9898
JK
625static int write_zip_archive(const struct archiver *ar,
626 struct archiver_args *args)
ec06bff5 627{
562e25ab
RS
628 int err;
629
965cba2e
JK
630 git_config(archive_zip_config, NULL);
631
ec06bff5
FBH
632 dos_time(&args->time, &zip_date, &zip_time);
633
c061a149 634 strbuf_init(&zip_dir, 0);
562e25ab
RS
635
636 err = write_archive_entries(args, write_zip_entry);
637 if (!err)
bbf05cf7 638 write_zip_trailer(args->commit_oid);
ec06bff5 639
c061a149 640 strbuf_release(&zip_dir);
ec06bff5 641
562e25ab 642 return err;
ec06bff5 643}
13e0f88d
JK
644
645static struct archiver zip_archiver = {
646 "zip",
647 write_zip_archive,
7b97730b 648 ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
13e0f88d
JK
649};
650
651void init_zip_archiver(void)
652{
653 register_archiver(&zip_archiver);
654}