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