cache.h: remove expand_user_path()
[git/debian.git] / archive-zip.c
blobc02dc33e4066a39121425e275ed6f3173a6b440a
1 /*
2 * Copyright (c) 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "config.h"
6 #include "archive.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "streaming.h"
10 #include "utf8.h"
11 #include "object-store.h"
12 #include "userdiff.h"
13 #include "xdiff-interface.h"
14 #include "date.h"
16 static int zip_date;
17 static int zip_time;
19 /* We only care about the "buf" part here. */
20 static struct strbuf zip_dir;
22 static uintmax_t zip_offset;
23 static uint64_t zip_dir_entries;
25 static unsigned int max_creator_version;
27 #define ZIP_STREAM (1 << 3)
28 #define ZIP_UTF8 (1 << 11)
30 enum zip_method {
31 ZIP_METHOD_STORE = 0,
32 ZIP_METHOD_DEFLATE = 8
35 struct zip_local_header {
36 unsigned char magic[4];
37 unsigned char version[2];
38 unsigned char flags[2];
39 unsigned char compression_method[2];
40 unsigned char mtime[2];
41 unsigned char mdate[2];
42 unsigned char crc32[4];
43 unsigned char compressed_size[4];
44 unsigned char size[4];
45 unsigned char filename_length[2];
46 unsigned char extra_length[2];
47 unsigned char _end[1];
50 struct zip_data_desc {
51 unsigned char magic[4];
52 unsigned char crc32[4];
53 unsigned char compressed_size[4];
54 unsigned char size[4];
55 unsigned char _end[1];
58 struct zip64_data_desc {
59 unsigned char magic[4];
60 unsigned char crc32[4];
61 unsigned char compressed_size[8];
62 unsigned char size[8];
63 unsigned char _end[1];
66 struct zip_dir_trailer {
67 unsigned char magic[4];
68 unsigned char disk[2];
69 unsigned char directory_start_disk[2];
70 unsigned char entries_on_this_disk[2];
71 unsigned char entries[2];
72 unsigned char size[4];
73 unsigned char offset[4];
74 unsigned char comment_length[2];
75 unsigned char _end[1];
78 struct zip_extra_mtime {
79 unsigned char magic[2];
80 unsigned char extra_size[2];
81 unsigned char flags[1];
82 unsigned char mtime[4];
83 unsigned char _end[1];
86 struct zip64_extra {
87 unsigned char magic[2];
88 unsigned char extra_size[2];
89 unsigned char size[8];
90 unsigned char compressed_size[8];
91 unsigned char _end[1];
94 struct zip64_dir_trailer {
95 unsigned char magic[4];
96 unsigned char record_size[8];
97 unsigned char creator_version[2];
98 unsigned char version[2];
99 unsigned char disk[4];
100 unsigned char directory_start_disk[4];
101 unsigned char entries_on_this_disk[8];
102 unsigned char entries[8];
103 unsigned char size[8];
104 unsigned char offset[8];
105 unsigned char _end[1];
108 struct zip64_dir_trailer_locator {
109 unsigned char magic[4];
110 unsigned char disk[4];
111 unsigned char offset[8];
112 unsigned char number_of_disks[4];
113 unsigned char _end[1];
117 * On ARM, padding is added at the end of the struct, so a simple
118 * sizeof(struct ...) reports two bytes more than the payload size
119 * we're interested in.
121 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
122 #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
123 #define ZIP64_DATA_DESC_SIZE offsetof(struct zip64_data_desc, _end)
124 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
125 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
126 #define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end)
127 #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
128 (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
129 #define ZIP64_EXTRA_SIZE offsetof(struct zip64_extra, _end)
130 #define ZIP64_EXTRA_PAYLOAD_SIZE \
131 (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size))
132 #define ZIP64_DIR_TRAILER_SIZE offsetof(struct zip64_dir_trailer, _end)
133 #define ZIP64_DIR_TRAILER_RECORD_SIZE \
134 (ZIP64_DIR_TRAILER_SIZE - \
135 offsetof(struct zip64_dir_trailer, creator_version))
136 #define ZIP64_DIR_TRAILER_LOCATOR_SIZE \
137 offsetof(struct zip64_dir_trailer_locator, _end)
139 static void copy_le16(unsigned char *dest, unsigned int n)
141 dest[0] = 0xff & n;
142 dest[1] = 0xff & (n >> 010);
145 static void copy_le32(unsigned char *dest, unsigned int n)
147 dest[0] = 0xff & n;
148 dest[1] = 0xff & (n >> 010);
149 dest[2] = 0xff & (n >> 020);
150 dest[3] = 0xff & (n >> 030);
153 static void copy_le64(unsigned char *dest, uint64_t n)
155 dest[0] = 0xff & n;
156 dest[1] = 0xff & (n >> 010);
157 dest[2] = 0xff & (n >> 020);
158 dest[3] = 0xff & (n >> 030);
159 dest[4] = 0xff & (n >> 040);
160 dest[5] = 0xff & (n >> 050);
161 dest[6] = 0xff & (n >> 060);
162 dest[7] = 0xff & (n >> 070);
165 static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped)
167 if (n <= max)
168 return n;
169 *clamped = 1;
170 return max;
173 static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
175 copy_le16(dest, clamp_max(n, 0xffff, clamped));
178 static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped)
180 copy_le32(dest, clamp_max(n, 0xffffffff, clamped));
183 static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n)
185 while (size-- > 0) {
186 strbuf_addch(sb, n & 0xff);
187 n >>= 8;
189 return -!!n;
192 static uint32_t clamp32(uintmax_t n)
194 const uintmax_t max = 0xffffffff;
195 return (n < max) ? n : max;
198 static void *zlib_deflate_raw(void *data, unsigned long size,
199 int compression_level,
200 unsigned long *compressed_size)
202 git_zstream stream;
203 unsigned long maxsize;
204 void *buffer;
205 int result;
207 git_deflate_init_raw(&stream, compression_level);
208 maxsize = git_deflate_bound(&stream, size);
209 buffer = xmalloc(maxsize);
211 stream.next_in = data;
212 stream.avail_in = size;
213 stream.next_out = buffer;
214 stream.avail_out = maxsize;
216 do {
217 result = git_deflate(&stream, Z_FINISH);
218 } while (result == Z_OK);
220 if (result != Z_STREAM_END) {
221 free(buffer);
222 return NULL;
225 git_deflate_end(&stream);
226 *compressed_size = stream.total_out;
228 return buffer;
231 static void write_zip_data_desc(unsigned long size,
232 unsigned long compressed_size,
233 unsigned long crc)
235 if (size >= 0xffffffff || compressed_size >= 0xffffffff) {
236 struct zip64_data_desc trailer;
237 copy_le32(trailer.magic, 0x08074b50);
238 copy_le32(trailer.crc32, crc);
239 copy_le64(trailer.compressed_size, compressed_size);
240 copy_le64(trailer.size, size);
241 write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE);
242 zip_offset += ZIP64_DATA_DESC_SIZE;
243 } else {
244 struct zip_data_desc trailer;
245 copy_le32(trailer.magic, 0x08074b50);
246 copy_le32(trailer.crc32, crc);
247 copy_le32(trailer.compressed_size, compressed_size);
248 copy_le32(trailer.size, size);
249 write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
250 zip_offset += ZIP_DATA_DESC_SIZE;
254 static void set_zip_header_data_desc(struct zip_local_header *header,
255 unsigned long size,
256 unsigned long compressed_size,
257 unsigned long crc)
259 copy_le32(header->crc32, crc);
260 copy_le32(header->compressed_size, compressed_size);
261 copy_le32(header->size, size);
264 static int has_only_ascii(const char *s)
266 for (;;) {
267 int c = *s++;
268 if (c == '\0')
269 return 1;
270 if (!isascii(c))
271 return 0;
275 static int entry_is_binary(struct index_state *istate, const char *path,
276 const void *buffer, size_t size)
278 struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
279 if (!driver)
280 driver = userdiff_find_by_name("default");
281 if (driver->binary != -1)
282 return driver->binary;
283 return buffer_is_binary(buffer, size);
286 #define STREAM_BUFFER_SIZE (1024 * 16)
288 static int write_zip_entry(struct archiver_args *args,
289 const struct object_id *oid,
290 const char *path, size_t pathlen,
291 unsigned int mode,
292 void *buffer, unsigned long size)
294 struct zip_local_header header;
295 uintmax_t offset = zip_offset;
296 struct zip_extra_mtime extra;
297 struct zip64_extra extra64;
298 size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE;
299 int need_zip64_extra = 0;
300 unsigned long attr2;
301 unsigned long compressed_size;
302 unsigned long crc;
303 enum zip_method method;
304 unsigned char *out;
305 void *deflated = NULL;
306 struct git_istream *stream = NULL;
307 unsigned long flags = 0;
308 int is_binary = -1;
309 const char *path_without_prefix = path + args->baselen;
310 unsigned int creator_version = 0;
311 unsigned int version_needed = 10;
312 size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE;
313 size_t zip64_dir_extra_payload_size = 0;
315 crc = crc32(0, NULL, 0);
317 if (!has_only_ascii(path)) {
318 if (is_utf8(path))
319 flags |= ZIP_UTF8;
320 else
321 warning(_("path is not valid UTF-8: %s"), path);
324 if (pathlen > 0xffff) {
325 return error(_("path too long (%d chars, SHA1: %s): %s"),
326 (int)pathlen, oid_to_hex(oid), path);
329 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
330 method = ZIP_METHOD_STORE;
331 attr2 = 16;
332 out = NULL;
333 compressed_size = 0;
334 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
335 method = ZIP_METHOD_STORE;
336 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
337 (mode & 0111) ? ((mode) << 16) : 0;
338 if (S_ISLNK(mode) || (mode & 0111))
339 creator_version = 0x0317;
340 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
341 method = ZIP_METHOD_DEFLATE;
343 if (!buffer) {
344 enum object_type type;
345 stream = open_istream(args->repo, oid, &type, &size,
346 NULL);
347 if (!stream)
348 return error(_("cannot stream blob %s"),
349 oid_to_hex(oid));
350 flags |= ZIP_STREAM;
351 out = NULL;
352 } else {
353 crc = crc32(crc, buffer, size);
354 is_binary = entry_is_binary(args->repo->index,
355 path_without_prefix,
356 buffer, size);
357 out = buffer;
359 compressed_size = (method == ZIP_METHOD_STORE) ? size : 0;
360 } else {
361 return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
362 oid_to_hex(oid));
365 if (creator_version > max_creator_version)
366 max_creator_version = creator_version;
368 if (buffer && method == ZIP_METHOD_DEFLATE) {
369 out = deflated = zlib_deflate_raw(buffer, size,
370 args->compression_level,
371 &compressed_size);
372 if (!out || compressed_size >= size) {
373 out = buffer;
374 method = ZIP_METHOD_STORE;
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)
390 version_needed = 45;
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;
401 } else {
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 == ZIP_METHOD_STORE) {
422 unsigned char buf[STREAM_BUFFER_SIZE];
423 ssize_t readlen;
425 for (;;) {
426 readlen = read_istream(stream, buf, sizeof(buf));
427 if (readlen <= 0)
428 break;
429 crc = crc32(crc, buf, readlen);
430 if (is_binary == -1)
431 is_binary = entry_is_binary(args->repo->index,
432 path_without_prefix,
433 buf, readlen);
434 write_or_die(1, buf, readlen);
436 close_istream(stream);
437 if (readlen)
438 return readlen;
440 compressed_size = size;
441 zip_offset += compressed_size;
443 write_zip_data_desc(size, compressed_size, crc);
444 } else if (stream && method == ZIP_METHOD_DEFLATE) {
445 unsigned char buf[STREAM_BUFFER_SIZE];
446 ssize_t readlen;
447 git_zstream zstream;
448 int result;
449 size_t out_len;
450 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
452 git_deflate_init_raw(&zstream, args->compression_level);
454 compressed_size = 0;
455 zstream.next_out = compressed;
456 zstream.avail_out = sizeof(compressed);
458 for (;;) {
459 readlen = read_istream(stream, buf, sizeof(buf));
460 if (readlen <= 0)
461 break;
462 crc = crc32(crc, buf, readlen);
463 if (is_binary == -1)
464 is_binary = entry_is_binary(args->repo->index,
465 path_without_prefix,
466 buf, readlen);
468 zstream.next_in = buf;
469 zstream.avail_in = readlen;
470 result = git_deflate(&zstream, 0);
471 if (result != Z_OK)
472 die(_("deflate error (%d)"), result);
473 out_len = zstream.next_out - compressed;
475 if (out_len > 0) {
476 write_or_die(1, compressed, out_len);
477 compressed_size += out_len;
478 zstream.next_out = compressed;
479 zstream.avail_out = sizeof(compressed);
483 close_istream(stream);
484 if (readlen)
485 return readlen;
487 zstream.next_in = buf;
488 zstream.avail_in = 0;
489 result = git_deflate(&zstream, Z_FINISH);
490 if (result != Z_STREAM_END)
491 die("deflate error (%d)", result);
493 git_deflate_end(&zstream);
494 out_len = zstream.next_out - compressed;
495 write_or_die(1, compressed, out_len);
496 compressed_size += out_len;
497 zip_offset += compressed_size;
499 write_zip_data_desc(size, compressed_size, crc);
500 } else if (compressed_size > 0) {
501 write_or_die(1, out, compressed_size);
502 zip_offset += compressed_size;
505 free(deflated);
507 if (compressed_size > 0xffffffff || size > 0xffffffff ||
508 offset > 0xffffffff) {
509 if (compressed_size >= 0xffffffff)
510 zip64_dir_extra_payload_size += 8;
511 if (size >= 0xffffffff)
512 zip64_dir_extra_payload_size += 8;
513 if (offset >= 0xffffffff)
514 zip64_dir_extra_payload_size += 8;
515 zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
518 strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
519 strbuf_add_le(&zip_dir, 2, creator_version);
520 strbuf_add_le(&zip_dir, 2, version_needed);
521 strbuf_add_le(&zip_dir, 2, flags);
522 strbuf_add_le(&zip_dir, 2, method);
523 strbuf_add_le(&zip_dir, 2, zip_time);
524 strbuf_add_le(&zip_dir, 2, zip_date);
525 strbuf_add_le(&zip_dir, 4, crc);
526 strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
527 strbuf_add_le(&zip_dir, 4, clamp32(size));
528 strbuf_add_le(&zip_dir, 2, pathlen);
529 strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
530 strbuf_add_le(&zip_dir, 2, 0); /* comment length */
531 strbuf_add_le(&zip_dir, 2, 0); /* disk */
532 strbuf_add_le(&zip_dir, 2, !is_binary);
533 strbuf_add_le(&zip_dir, 4, attr2);
534 strbuf_add_le(&zip_dir, 4, clamp32(offset));
535 strbuf_add(&zip_dir, path, pathlen);
536 strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
537 if (zip64_dir_extra_payload_size) {
538 strbuf_add_le(&zip_dir, 2, 0x0001); /* magic */
539 strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size);
540 if (size >= 0xffffffff)
541 strbuf_add_le(&zip_dir, 8, size);
542 if (compressed_size >= 0xffffffff)
543 strbuf_add_le(&zip_dir, 8, compressed_size);
544 if (offset >= 0xffffffff)
545 strbuf_add_le(&zip_dir, 8, offset);
547 zip_dir_entries++;
549 return 0;
552 static void write_zip64_trailer(void)
554 struct zip64_dir_trailer trailer64;
555 struct zip64_dir_trailer_locator locator64;
557 copy_le32(trailer64.magic, 0x06064b50);
558 copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
559 copy_le16(trailer64.creator_version, max_creator_version);
560 copy_le16(trailer64.version, 45);
561 copy_le32(trailer64.disk, 0);
562 copy_le32(trailer64.directory_start_disk, 0);
563 copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
564 copy_le64(trailer64.entries, zip_dir_entries);
565 copy_le64(trailer64.size, zip_dir.len);
566 copy_le64(trailer64.offset, zip_offset);
568 copy_le32(locator64.magic, 0x07064b50);
569 copy_le32(locator64.disk, 0);
570 copy_le64(locator64.offset, zip_offset + zip_dir.len);
571 copy_le32(locator64.number_of_disks, 1);
573 write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
574 write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
577 static void write_zip_trailer(const struct object_id *oid)
579 struct zip_dir_trailer trailer;
580 int clamped = 0;
582 copy_le32(trailer.magic, 0x06054b50);
583 copy_le16(trailer.disk, 0);
584 copy_le16(trailer.directory_start_disk, 0);
585 copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
586 &clamped);
587 copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
588 copy_le32(trailer.size, zip_dir.len);
589 copy_le32_clamp(trailer.offset, zip_offset, &clamped);
590 copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);
592 write_or_die(1, zip_dir.buf, zip_dir.len);
593 if (clamped)
594 write_zip64_trailer();
595 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
596 if (oid)
597 write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
600 static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
602 time_t time;
603 struct tm tm;
605 if (date_overflows(*timestamp))
606 die(_("timestamp too large for this system: %"PRItime),
607 *timestamp);
608 time = (time_t)*timestamp;
609 localtime_r(&time, &tm);
610 *timestamp = time;
612 *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 +
613 (tm.tm_year + 1900 - 1980) * 512;
614 *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048;
617 static int archive_zip_config(const char *var, const char *value,
618 void *data UNUSED)
620 return userdiff_config(var, value);
623 static int write_zip_archive(const struct archiver *ar UNUSED,
624 struct archiver_args *args)
626 int err;
628 git_config(archive_zip_config, NULL);
630 dos_time(&args->time, &zip_date, &zip_time);
632 strbuf_init(&zip_dir, 0);
634 err = write_archive_entries(args, write_zip_entry);
635 if (!err)
636 write_zip_trailer(args->commit_oid);
638 strbuf_release(&zip_dir);
640 return err;
643 static struct archiver zip_archiver = {
644 .name = "zip",
645 .write_archive = write_zip_archive,
646 .flags = ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE,
649 void init_zip_archiver(void)
651 register_archiver(&zip_archiver);