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