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