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