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