2 * Copyright (c) 2006 Rene Scharfe
9 #include "xdiff-interface.h"
14 static unsigned char *zip_dir;
15 static unsigned int zip_dir_size;
17 static unsigned int zip_offset;
18 static unsigned int zip_dir_offset;
19 static unsigned int zip_dir_entries;
21 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
22 #define ZIP_STREAM (1 << 3)
23 #define ZIP_UTF8 (1 << 11)
25 struct 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];
37 unsigned char _end[1];
40 struct 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];
48 struct 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];
66 unsigned char _end[1];
69 struct 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];
78 unsigned char _end[1];
81 struct 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];
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.
94 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
95 #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
96 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
97 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
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))
102 static void copy_le16(unsigned char *dest, unsigned int n)
105 dest[1] = 0xff & (n >> 010);
108 static void copy_le32(unsigned char *dest, unsigned int n)
111 dest[1] = 0xff & (n >> 010);
112 dest[2] = 0xff & (n >> 020);
113 dest[3] = 0xff & (n >> 030);
116 static void *zlib_deflate_raw(void *data, unsigned long size,
117 int compression_level,
118 unsigned long *compressed_size)
121 unsigned long maxsize;
125 memset(&stream, 0, sizeof(stream));
126 git_deflate_init_raw(&stream, compression_level);
127 maxsize = git_deflate_bound(&stream, size);
128 buffer = xmalloc(maxsize);
130 stream.next_in = data;
131 stream.avail_in = size;
132 stream.next_out = buffer;
133 stream.avail_out = maxsize;
136 result = git_deflate(&stream, Z_FINISH);
137 } while (result == Z_OK);
139 if (result != Z_STREAM_END) {
144 git_deflate_end(&stream);
145 *compressed_size = stream.total_out;
150 static void write_zip_data_desc(unsigned long size,
151 unsigned long compressed_size,
154 struct zip_data_desc trailer;
156 copy_le32(trailer.magic, 0x08074b50);
157 copy_le32(trailer.crc32, crc);
158 copy_le32(trailer.compressed_size, compressed_size);
159 copy_le32(trailer.size, size);
160 write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
163 static void set_zip_dir_data_desc(struct zip_dir_header *header,
165 unsigned long compressed_size,
168 copy_le32(header->crc32, crc);
169 copy_le32(header->compressed_size, compressed_size);
170 copy_le32(header->size, size);
173 static void set_zip_header_data_desc(struct zip_local_header *header,
175 unsigned long compressed_size,
178 copy_le32(header->crc32, crc);
179 copy_le32(header->compressed_size, compressed_size);
180 copy_le32(header->size, size);
183 static int has_only_ascii(const char *s)
194 static int entry_is_binary(const char *path, const void *buffer, size_t size)
196 struct userdiff_driver *driver = userdiff_find_by_path(path);
198 driver = userdiff_find_by_name("default");
199 if (driver->binary != -1)
200 return driver->binary;
201 return buffer_is_binary(buffer, size);
204 #define STREAM_BUFFER_SIZE (1024 * 16)
206 static int write_zip_entry(struct archiver_args *args,
207 const unsigned char *sha1,
208 const char *path, size_t pathlen,
211 struct zip_local_header header;
212 struct zip_dir_header dirent;
213 struct zip_extra_mtime extra;
215 unsigned long compressed_size;
217 unsigned long direntsize;
220 void *deflated = NULL;
222 struct git_istream *stream = NULL;
223 unsigned long flags = 0;
226 const char *path_without_prefix = path + args->baselen;
228 crc = crc32(0, NULL, 0);
230 if (!has_only_ascii(path)) {
234 warning("Path is not valid UTF-8: %s", path);
237 if (pathlen > 0xffff) {
238 return error("path too long (%d chars, SHA1: %s): %s",
239 (int)pathlen, sha1_to_hex(sha1), path);
242 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
249 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
250 enum object_type type = sha1_object_info(sha1, &size);
253 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
254 (mode & 0111) ? ((mode) << 16) : 0;
255 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
258 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
259 size > big_file_threshold) {
260 stream = open_istream(sha1, &type, &size, NULL);
262 return error("cannot stream blob %s",
267 buffer = sha1_file_to_archive(args, path, sha1, mode,
270 return error("cannot read %s",
272 crc = crc32(crc, buffer, size);
273 is_binary = entry_is_binary(path_without_prefix,
277 compressed_size = (method == 0) ? size : 0;
279 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
283 if (buffer && method == 8) {
284 out = deflated = zlib_deflate_raw(buffer, size,
285 args->compression_level,
287 if (!out || compressed_size >= size) {
290 compressed_size = size;
294 copy_le16(extra.magic, 0x5455);
295 copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
296 extra.flags[0] = 1; /* just mtime */
297 copy_le32(extra.mtime, args->time);
299 /* make sure we have enough free space in the dictionary */
300 direntsize = ZIP_DIR_HEADER_SIZE + pathlen + ZIP_EXTRA_MTIME_SIZE;
301 while (zip_dir_size < zip_dir_offset + direntsize) {
302 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
303 zip_dir = xrealloc(zip_dir, zip_dir_size);
306 copy_le32(dirent.magic, 0x02014b50);
307 copy_le16(dirent.creator_version,
308 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
309 copy_le16(dirent.version, 10);
310 copy_le16(dirent.flags, flags);
311 copy_le16(dirent.compression_method, method);
312 copy_le16(dirent.mtime, zip_time);
313 copy_le16(dirent.mdate, zip_date);
314 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
315 copy_le16(dirent.filename_length, pathlen);
316 copy_le16(dirent.extra_length, ZIP_EXTRA_MTIME_SIZE);
317 copy_le16(dirent.comment_length, 0);
318 copy_le16(dirent.disk, 0);
319 copy_le32(dirent.attr2, attr2);
320 copy_le32(dirent.offset, zip_offset);
322 copy_le32(header.magic, 0x04034b50);
323 copy_le16(header.version, 10);
324 copy_le16(header.flags, flags);
325 copy_le16(header.compression_method, method);
326 copy_le16(header.mtime, zip_time);
327 copy_le16(header.mdate, zip_date);
328 set_zip_header_data_desc(&header, size, compressed_size, crc);
329 copy_le16(header.filename_length, pathlen);
330 copy_le16(header.extra_length, ZIP_EXTRA_MTIME_SIZE);
331 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
332 zip_offset += ZIP_LOCAL_HEADER_SIZE;
333 write_or_die(1, path, pathlen);
334 zip_offset += pathlen;
335 write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
336 zip_offset += ZIP_EXTRA_MTIME_SIZE;
337 if (stream && method == 0) {
338 unsigned char buf[STREAM_BUFFER_SIZE];
342 readlen = read_istream(stream, buf, sizeof(buf));
345 crc = crc32(crc, buf, readlen);
347 is_binary = entry_is_binary(path_without_prefix,
349 write_or_die(1, buf, readlen);
351 close_istream(stream);
355 compressed_size = size;
356 zip_offset += compressed_size;
358 write_zip_data_desc(size, compressed_size, crc);
359 zip_offset += ZIP_DATA_DESC_SIZE;
361 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
362 } else if (stream && method == 8) {
363 unsigned char buf[STREAM_BUFFER_SIZE];
368 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
370 memset(&zstream, 0, sizeof(zstream));
371 git_deflate_init_raw(&zstream, args->compression_level);
374 zstream.next_out = compressed;
375 zstream.avail_out = sizeof(compressed);
378 readlen = read_istream(stream, buf, sizeof(buf));
381 crc = crc32(crc, buf, readlen);
383 is_binary = entry_is_binary(path_without_prefix,
386 zstream.next_in = buf;
387 zstream.avail_in = readlen;
388 result = git_deflate(&zstream, 0);
390 die("deflate error (%d)", result);
391 out_len = zstream.next_out - compressed;
394 write_or_die(1, compressed, out_len);
395 compressed_size += out_len;
396 zstream.next_out = compressed;
397 zstream.avail_out = sizeof(compressed);
401 close_istream(stream);
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);
411 git_deflate_end(&zstream);
412 out_len = zstream.next_out - compressed;
413 write_or_die(1, compressed, out_len);
414 compressed_size += out_len;
415 zip_offset += compressed_size;
417 write_zip_data_desc(size, compressed_size, crc);
418 zip_offset += ZIP_DATA_DESC_SIZE;
420 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
421 } else if (compressed_size > 0) {
422 write_or_die(1, out, compressed_size);
423 zip_offset += compressed_size;
429 copy_le16(dirent.attr1, !is_binary);
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;
435 memcpy(zip_dir + zip_dir_offset, &extra, ZIP_EXTRA_MTIME_SIZE);
436 zip_dir_offset += ZIP_EXTRA_MTIME_SIZE;
442 static void write_zip_trailer(const unsigned char *sha1)
444 struct zip_dir_trailer trailer;
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);
455 write_or_die(1, zip_dir, zip_dir_offset);
456 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
458 write_or_die(1, sha1_to_hex(sha1), 40);
461 static void dos_time(time_t *time, int *dos_date, int *dos_time)
463 struct tm *t = localtime(time);
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;
470 static int write_zip_archive(const struct archiver *ar,
471 struct archiver_args *args)
475 dos_time(&args->time, &zip_date, &zip_time);
477 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
478 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
480 err = write_archive_entries(args, write_zip_entry);
482 write_zip_trailer(args->commit_sha1);
489 static struct archiver zip_archiver = {
492 ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
495 void init_zip_archiver(void)
497 register_archiver(&zip_archiver);