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