2 * Copyright (c) 2006 Rene Scharfe
9 #include "object-store.h"
11 #include "xdiff-interface.h"
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 struct 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];
39 unsigned char _end
[1];
42 struct 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];
50 struct 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];
58 struct 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];
67 unsigned char _end
[1];
70 struct 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];
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];
86 struct 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];
100 struct 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];
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.
113 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
114 #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
115 #define ZIP64_DATA_DESC_SIZE offsetof(struct zip64_data_desc, _end)
116 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
117 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
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))
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))
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)
131 static void copy_le16(unsigned char *dest
, unsigned int n
)
134 dest
[1] = 0xff & (n
>> 010);
137 static void copy_le32(unsigned char *dest
, unsigned int n
)
140 dest
[1] = 0xff & (n
>> 010);
141 dest
[2] = 0xff & (n
>> 020);
142 dest
[3] = 0xff & (n
>> 030);
145 static void copy_le64(unsigned char *dest
, uint64_t 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);
157 static uint64_t clamp_max(uint64_t n
, uint64_t max
, int *clamped
)
165 static void copy_le16_clamp(unsigned char *dest
, uint64_t n
, int *clamped
)
167 copy_le16(dest
, clamp_max(n
, 0xffff, clamped
));
170 static void copy_le32_clamp(unsigned char *dest
, uint64_t n
, int *clamped
)
172 copy_le32(dest
, clamp_max(n
, 0xffffffff, clamped
));
175 static int strbuf_add_le(struct strbuf
*sb
, size_t size
, uintmax_t n
)
178 strbuf_addch(sb
, n
& 0xff);
184 static uint32_t clamp32(uintmax_t n
)
186 const uintmax_t max
= 0xffffffff;
187 return (n
< max
) ? n
: max
;
190 static void *zlib_deflate_raw(void *data
, unsigned long size
,
191 int compression_level
,
192 unsigned long *compressed_size
)
195 unsigned long maxsize
;
199 git_deflate_init_raw(&stream
, compression_level
);
200 maxsize
= git_deflate_bound(&stream
, size
);
201 buffer
= xmalloc(maxsize
);
203 stream
.next_in
= data
;
204 stream
.avail_in
= size
;
205 stream
.next_out
= buffer
;
206 stream
.avail_out
= maxsize
;
209 result
= git_deflate(&stream
, Z_FINISH
);
210 } while (result
== Z_OK
);
212 if (result
!= Z_STREAM_END
) {
217 git_deflate_end(&stream
);
218 *compressed_size
= stream
.total_out
;
223 static void write_zip_data_desc(unsigned long size
,
224 unsigned long compressed_size
,
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
;
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
;
246 static void set_zip_header_data_desc(struct zip_local_header
*header
,
248 unsigned long compressed_size
,
251 copy_le32(header
->crc32
, crc
);
252 copy_le32(header
->compressed_size
, compressed_size
);
253 copy_le32(header
->size
, size
);
256 static int has_only_ascii(const char *s
)
267 static int entry_is_binary(const char *path
, const void *buffer
, size_t size
)
269 struct userdiff_driver
*driver
= userdiff_find_by_path(path
);
271 driver
= userdiff_find_by_name("default");
272 if (driver
->binary
!= -1)
273 return driver
->binary
;
274 return buffer_is_binary(buffer
, size
);
277 #define STREAM_BUFFER_SIZE (1024 * 16)
279 static int write_zip_entry(struct archiver_args
*args
,
280 const struct object_id
*oid
,
281 const char *path
, size_t pathlen
,
284 struct zip_local_header header
;
285 uintmax_t offset
= zip_offset
;
286 struct zip_extra_mtime extra
;
287 struct zip64_extra extra64
;
288 size_t header_extra_size
= ZIP_EXTRA_MTIME_SIZE
;
289 int need_zip64_extra
= 0;
291 unsigned long compressed_size
;
295 void *deflated
= NULL
;
297 struct git_istream
*stream
= NULL
;
298 unsigned long flags
= 0;
301 const char *path_without_prefix
= path
+ args
->baselen
;
302 unsigned int creator_version
= 0;
303 unsigned int version_needed
= 10;
304 size_t zip_dir_extra_size
= ZIP_EXTRA_MTIME_SIZE
;
305 size_t zip64_dir_extra_payload_size
= 0;
307 crc
= crc32(0, NULL
, 0);
309 if (!has_only_ascii(path
)) {
313 warning(_("path is not valid UTF-8: %s"), path
);
316 if (pathlen
> 0xffff) {
317 return error(_("path too long (%d chars, SHA1: %s): %s"),
318 (int)pathlen
, oid_to_hex(oid
), path
);
321 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
328 } else if (S_ISREG(mode
) || S_ISLNK(mode
)) {
329 enum object_type type
= oid_object_info(args
->repo
, oid
,
333 attr2
= S_ISLNK(mode
) ? ((mode
| 0777) << 16) :
334 (mode
& 0111) ? ((mode
) << 16) : 0;
335 if (S_ISLNK(mode
) || (mode
& 0111))
336 creator_version
= 0x0317;
337 if (S_ISREG(mode
) && args
->compression_level
!= 0 && size
> 0)
340 if (S_ISREG(mode
) && type
== OBJ_BLOB
&& !args
->convert
&&
341 size
> big_file_threshold
) {
342 stream
= open_istream(oid
, &type
, &size
, NULL
);
344 return error(_("cannot stream blob %s"),
349 buffer
= object_file_to_archive(args
, path
, oid
, mode
,
352 return error(_("cannot read %s"),
354 crc
= crc32(crc
, buffer
, size
);
355 is_binary
= entry_is_binary(path_without_prefix
,
359 compressed_size
= (method
== 0) ? size
: 0;
361 return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode
,
365 if (creator_version
> max_creator_version
)
366 max_creator_version
= creator_version
;
368 if (buffer
&& method
== 8) {
369 out
= deflated
= zlib_deflate_raw(buffer
, size
,
370 args
->compression_level
,
372 if (!out
|| compressed_size
>= size
) {
375 compressed_size
= size
;
379 copy_le16(extra
.magic
, 0x5455);
380 copy_le16(extra
.extra_size
, ZIP_EXTRA_MTIME_PAYLOAD_SIZE
);
381 extra
.flags
[0] = 1; /* just mtime */
382 copy_le32(extra
.mtime
, args
->time
);
384 if (size
> 0xffffffff || compressed_size
> 0xffffffff)
385 need_zip64_extra
= 1;
386 if (stream
&& size
> 0x7fffffff)
387 need_zip64_extra
= 1;
389 if (need_zip64_extra
)
392 copy_le32(header
.magic
, 0x04034b50);
393 copy_le16(header
.version
, version_needed
);
394 copy_le16(header
.flags
, flags
);
395 copy_le16(header
.compression_method
, method
);
396 copy_le16(header
.mtime
, zip_time
);
397 copy_le16(header
.mdate
, zip_date
);
398 if (need_zip64_extra
) {
399 set_zip_header_data_desc(&header
, 0xffffffff, 0xffffffff, crc
);
400 header_extra_size
+= ZIP64_EXTRA_SIZE
;
402 set_zip_header_data_desc(&header
, size
, compressed_size
, crc
);
404 copy_le16(header
.filename_length
, pathlen
);
405 copy_le16(header
.extra_length
, header_extra_size
);
406 write_or_die(1, &header
, ZIP_LOCAL_HEADER_SIZE
);
407 zip_offset
+= ZIP_LOCAL_HEADER_SIZE
;
408 write_or_die(1, path
, pathlen
);
409 zip_offset
+= pathlen
;
410 write_or_die(1, &extra
, ZIP_EXTRA_MTIME_SIZE
);
411 zip_offset
+= ZIP_EXTRA_MTIME_SIZE
;
412 if (need_zip64_extra
) {
413 copy_le16(extra64
.magic
, 0x0001);
414 copy_le16(extra64
.extra_size
, ZIP64_EXTRA_PAYLOAD_SIZE
);
415 copy_le64(extra64
.size
, size
);
416 copy_le64(extra64
.compressed_size
, compressed_size
);
417 write_or_die(1, &extra64
, ZIP64_EXTRA_SIZE
);
418 zip_offset
+= ZIP64_EXTRA_SIZE
;
421 if (stream
&& method
== 0) {
422 unsigned char buf
[STREAM_BUFFER_SIZE
];
426 readlen
= read_istream(stream
, buf
, sizeof(buf
));
429 crc
= crc32(crc
, buf
, readlen
);
431 is_binary
= entry_is_binary(path_without_prefix
,
433 write_or_die(1, buf
, readlen
);
435 close_istream(stream
);
439 compressed_size
= size
;
440 zip_offset
+= compressed_size
;
442 write_zip_data_desc(size
, compressed_size
, crc
);
443 } else if (stream
&& method
== 8) {
444 unsigned char buf
[STREAM_BUFFER_SIZE
];
449 unsigned char compressed
[STREAM_BUFFER_SIZE
* 2];
451 git_deflate_init_raw(&zstream
, args
->compression_level
);
454 zstream
.next_out
= compressed
;
455 zstream
.avail_out
= sizeof(compressed
);
458 readlen
= read_istream(stream
, buf
, sizeof(buf
));
461 crc
= crc32(crc
, buf
, readlen
);
463 is_binary
= entry_is_binary(path_without_prefix
,
466 zstream
.next_in
= buf
;
467 zstream
.avail_in
= readlen
;
468 result
= git_deflate(&zstream
, 0);
470 die(_("deflate error (%d)"), result
);
471 out_len
= zstream
.next_out
- compressed
;
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
);
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
;
506 if (compressed_size
> 0xffffffff || size
> 0xffffffff ||
507 offset
> 0xffffffff) {
508 if (compressed_size
>= 0xffffffff)
509 zip64_dir_extra_payload_size
+= 8;
510 if (size
>= 0xffffffff)
511 zip64_dir_extra_payload_size
+= 8;
512 if (offset
>= 0xffffffff)
513 zip64_dir_extra_payload_size
+= 8;
514 zip_dir_extra_size
+= 2 + 2 + zip64_dir_extra_payload_size
;
517 strbuf_add_le(&zip_dir
, 4, 0x02014b50); /* magic */
518 strbuf_add_le(&zip_dir
, 2, creator_version
);
519 strbuf_add_le(&zip_dir
, 2, version_needed
);
520 strbuf_add_le(&zip_dir
, 2, flags
);
521 strbuf_add_le(&zip_dir
, 2, method
);
522 strbuf_add_le(&zip_dir
, 2, zip_time
);
523 strbuf_add_le(&zip_dir
, 2, zip_date
);
524 strbuf_add_le(&zip_dir
, 4, crc
);
525 strbuf_add_le(&zip_dir
, 4, clamp32(compressed_size
));
526 strbuf_add_le(&zip_dir
, 4, clamp32(size
));
527 strbuf_add_le(&zip_dir
, 2, pathlen
);
528 strbuf_add_le(&zip_dir
, 2, zip_dir_extra_size
);
529 strbuf_add_le(&zip_dir
, 2, 0); /* comment length */
530 strbuf_add_le(&zip_dir
, 2, 0); /* disk */
531 strbuf_add_le(&zip_dir
, 2, !is_binary
);
532 strbuf_add_le(&zip_dir
, 4, attr2
);
533 strbuf_add_le(&zip_dir
, 4, clamp32(offset
));
534 strbuf_add(&zip_dir
, path
, pathlen
);
535 strbuf_add(&zip_dir
, &extra
, ZIP_EXTRA_MTIME_SIZE
);
536 if (zip64_dir_extra_payload_size
) {
537 strbuf_add_le(&zip_dir
, 2, 0x0001); /* magic */
538 strbuf_add_le(&zip_dir
, 2, zip64_dir_extra_payload_size
);
539 if (size
>= 0xffffffff)
540 strbuf_add_le(&zip_dir
, 8, size
);
541 if (compressed_size
>= 0xffffffff)
542 strbuf_add_le(&zip_dir
, 8, compressed_size
);
543 if (offset
>= 0xffffffff)
544 strbuf_add_le(&zip_dir
, 8, offset
);
551 static void write_zip64_trailer(void)
553 struct zip64_dir_trailer trailer64
;
554 struct zip64_dir_trailer_locator locator64
;
556 copy_le32(trailer64
.magic
, 0x06064b50);
557 copy_le64(trailer64
.record_size
, ZIP64_DIR_TRAILER_RECORD_SIZE
);
558 copy_le16(trailer64
.creator_version
, max_creator_version
);
559 copy_le16(trailer64
.version
, 45);
560 copy_le32(trailer64
.disk
, 0);
561 copy_le32(trailer64
.directory_start_disk
, 0);
562 copy_le64(trailer64
.entries_on_this_disk
, zip_dir_entries
);
563 copy_le64(trailer64
.entries
, zip_dir_entries
);
564 copy_le64(trailer64
.size
, zip_dir
.len
);
565 copy_le64(trailer64
.offset
, zip_offset
);
567 copy_le32(locator64
.magic
, 0x07064b50);
568 copy_le32(locator64
.disk
, 0);
569 copy_le64(locator64
.offset
, zip_offset
+ zip_dir
.len
);
570 copy_le32(locator64
.number_of_disks
, 1);
572 write_or_die(1, &trailer64
, ZIP64_DIR_TRAILER_SIZE
);
573 write_or_die(1, &locator64
, ZIP64_DIR_TRAILER_LOCATOR_SIZE
);
576 static void write_zip_trailer(const unsigned char *sha1
)
578 struct zip_dir_trailer trailer
;
581 copy_le32(trailer
.magic
, 0x06054b50);
582 copy_le16(trailer
.disk
, 0);
583 copy_le16(trailer
.directory_start_disk
, 0);
584 copy_le16_clamp(trailer
.entries_on_this_disk
, zip_dir_entries
,
586 copy_le16_clamp(trailer
.entries
, zip_dir_entries
, &clamped
);
587 copy_le32(trailer
.size
, zip_dir
.len
);
588 copy_le32_clamp(trailer
.offset
, zip_offset
, &clamped
);
589 copy_le16(trailer
.comment_length
, sha1
? GIT_SHA1_HEXSZ
: 0);
591 write_or_die(1, zip_dir
.buf
, zip_dir
.len
);
593 write_zip64_trailer();
594 write_or_die(1, &trailer
, ZIP_DIR_TRAILER_SIZE
);
596 write_or_die(1, sha1_to_hex(sha1
), GIT_SHA1_HEXSZ
);
599 static void dos_time(timestamp_t
*timestamp
, int *dos_date
, int *dos_time
)
604 if (date_overflows(*timestamp
))
605 die(_("timestamp too large for this system: %"PRItime
),
607 time
= (time_t)*timestamp
;
608 t
= localtime(&time
);
611 *dos_date
= t
->tm_mday
+ (t
->tm_mon
+ 1) * 32 +
612 (t
->tm_year
+ 1900 - 1980) * 512;
613 *dos_time
= t
->tm_sec
/ 2 + t
->tm_min
* 32 + t
->tm_hour
* 2048;
616 static int archive_zip_config(const char *var
, const char *value
, void *data
)
618 return userdiff_config(var
, value
);
621 static int write_zip_archive(const struct archiver
*ar
,
622 struct archiver_args
*args
)
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
);
634 write_zip_trailer(args
->commit_sha1
);
636 strbuf_release(&zip_dir
);
641 static struct archiver zip_archiver
= {
644 ARCHIVER_WANT_COMPRESSION_LEVELS
|ARCHIVER_REMOTE
647 void init_zip_archiver(void)
649 register_archiver(&zip_archiver
);