interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / archive-zip.c

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