2 * Copyright (c) 2006 Rene Scharfe
15 static const struct commit *commit;
17 static unsigned char *zip_dir;
18 static unsigned int zip_dir_size;
20 static unsigned int zip_offset;
21 static unsigned int zip_dir_offset;
22 static unsigned int zip_dir_entries;
24 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
26 struct zip_local_header {
27 unsigned char magic[4];
28 unsigned char version[2];
29 unsigned char flags[2];
30 unsigned char compression_method[2];
31 unsigned char mtime[2];
32 unsigned char mdate[2];
33 unsigned char crc32[4];
34 unsigned char compressed_size[4];
35 unsigned char size[4];
36 unsigned char filename_length[2];
37 unsigned char extra_length[2];
38 unsigned char _end[1];
41 struct zip_dir_header {
42 unsigned char magic[4];
43 unsigned char creator_version[2];
44 unsigned char version[2];
45 unsigned char flags[2];
46 unsigned char compression_method[2];
47 unsigned char mtime[2];
48 unsigned char mdate[2];
49 unsigned char crc32[4];
50 unsigned char compressed_size[4];
51 unsigned char size[4];
52 unsigned char filename_length[2];
53 unsigned char extra_length[2];
54 unsigned char comment_length[2];
55 unsigned char disk[2];
56 unsigned char attr1[2];
57 unsigned char attr2[4];
58 unsigned char offset[4];
59 unsigned char _end[1];
62 struct zip_dir_trailer {
63 unsigned char magic[4];
64 unsigned char disk[2];
65 unsigned char directory_start_disk[2];
66 unsigned char entries_on_this_disk[2];
67 unsigned char entries[2];
68 unsigned char size[4];
69 unsigned char offset[4];
70 unsigned char comment_length[2];
71 unsigned char _end[1];
75 * On ARM, padding is added at the end of the struct, so a simple
76 * sizeof(struct ...) reports two bytes more than the payload size
77 * we're interested in.
79 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
80 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
81 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
83 static void copy_le16(unsigned char *dest, unsigned int n)
86 dest[1] = 0xff & (n >> 010);
89 static void copy_le32(unsigned char *dest, unsigned int n)
92 dest[1] = 0xff & (n >> 010);
93 dest[2] = 0xff & (n >> 020);
94 dest[3] = 0xff & (n >> 030);
97 static void *zlib_deflate(void *data, unsigned long size,
98 unsigned long *compressed_size)
101 unsigned long maxsize;
105 memset(&stream, 0, sizeof(stream));
106 deflateInit(&stream, zlib_compression_level);
107 maxsize = deflateBound(&stream, size);
108 buffer = xmalloc(maxsize);
110 stream.next_in = data;
111 stream.avail_in = size;
112 stream.next_out = buffer;
113 stream.avail_out = maxsize;
116 result = deflate(&stream, Z_FINISH);
117 } while (result == Z_OK);
119 if (result != Z_STREAM_END) {
125 *compressed_size = stream.total_out;
130 static char *construct_path(const char *base, int baselen,
131 const char *filename, int isdir, int *pathlen)
133 int filenamelen = strlen(filename);
134 int len = baselen + filenamelen;
139 p = path = xmalloc(len + 1);
141 memcpy(p, base, baselen);
143 memcpy(p, filename, filenamelen);
154 static int write_zip_entry(const unsigned char *sha1,
155 const char *base, int baselen,
156 const char *filename, unsigned mode, int stage)
158 struct zip_local_header header;
159 struct zip_dir_header dirent;
161 unsigned long compressed_size;
162 unsigned long uncompressed_size;
164 unsigned long direntsize;
171 enum object_type type;
173 void *deflated = NULL;
175 crc = crc32(0, NULL, 0);
177 path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
179 fprintf(stderr, "%s\n", path);
180 if (pathlen > 0xffff) {
181 error("path too long (%d chars, SHA1: %s): %s", pathlen,
182 sha1_to_hex(sha1), path);
186 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
189 result = (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
191 uncompressed_size = 0;
193 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
195 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : 0;
196 if (S_ISREG(mode) && zlib_compression_level != 0)
199 buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
202 die("cannot read %s", sha1_to_hex(sha1));
203 crc = crc32(crc, buffer, size);
205 uncompressed_size = size;
206 compressed_size = size;
208 error("unsupported file mode: 0%o (SHA1: %s)", mode,
214 deflated = zlib_deflate(buffer, size, &compressed_size);
215 if (deflated && compressed_size - 6 < size) {
216 /* ZLIB --> raw compressed data (see RFC 1950) */
217 /* CMF and FLG ... */
218 out = (unsigned char *)deflated + 2;
219 compressed_size -= 6; /* ... and ADLER32 */
222 compressed_size = size;
226 /* make sure we have enough free space in the dictionary */
227 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
228 while (zip_dir_size < zip_dir_offset + direntsize) {
229 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
230 zip_dir = xrealloc(zip_dir, zip_dir_size);
233 copy_le32(dirent.magic, 0x02014b50);
234 copy_le16(dirent.creator_version, S_ISLNK(mode) ? 0x0317 : 0);
235 copy_le16(dirent.version, 10);
236 copy_le16(dirent.flags, 0);
237 copy_le16(dirent.compression_method, method);
238 copy_le16(dirent.mtime, zip_time);
239 copy_le16(dirent.mdate, zip_date);
240 copy_le32(dirent.crc32, crc);
241 copy_le32(dirent.compressed_size, compressed_size);
242 copy_le32(dirent.size, uncompressed_size);
243 copy_le16(dirent.filename_length, pathlen);
244 copy_le16(dirent.extra_length, 0);
245 copy_le16(dirent.comment_length, 0);
246 copy_le16(dirent.disk, 0);
247 copy_le16(dirent.attr1, 0);
248 copy_le32(dirent.attr2, attr2);
249 copy_le32(dirent.offset, zip_offset);
250 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
251 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
252 memcpy(zip_dir + zip_dir_offset, path, pathlen);
253 zip_dir_offset += pathlen;
256 copy_le32(header.magic, 0x04034b50);
257 copy_le16(header.version, 10);
258 copy_le16(header.flags, 0);
259 copy_le16(header.compression_method, method);
260 copy_le16(header.mtime, zip_time);
261 copy_le16(header.mdate, zip_date);
262 copy_le32(header.crc32, crc);
263 copy_le32(header.compressed_size, compressed_size);
264 copy_le32(header.size, uncompressed_size);
265 copy_le16(header.filename_length, pathlen);
266 copy_le16(header.extra_length, 0);
267 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
268 zip_offset += ZIP_LOCAL_HEADER_SIZE;
269 write_or_die(1, path, pathlen);
270 zip_offset += pathlen;
271 if (compressed_size > 0) {
272 write_or_die(1, out, compressed_size);
273 zip_offset += compressed_size;
284 static void write_zip_trailer(const unsigned char *sha1)
286 struct zip_dir_trailer trailer;
288 copy_le32(trailer.magic, 0x06054b50);
289 copy_le16(trailer.disk, 0);
290 copy_le16(trailer.directory_start_disk, 0);
291 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
292 copy_le16(trailer.entries, zip_dir_entries);
293 copy_le32(trailer.size, zip_dir_offset);
294 copy_le32(trailer.offset, zip_offset);
295 copy_le16(trailer.comment_length, sha1 ? 40 : 0);
297 write_or_die(1, zip_dir, zip_dir_offset);
298 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
300 write_or_die(1, sha1_to_hex(sha1), 40);
303 static void dos_time(time_t *time, int *dos_date, int *dos_time)
305 struct tm *t = localtime(time);
307 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
308 (t->tm_year + 1900 - 1980) * 512;
309 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
312 int write_zip_archive(struct archiver_args *args)
314 int plen = strlen(args->base);
316 dos_time(&args->time, &zip_date, &zip_time);
318 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
319 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
320 verbose = args->verbose;
321 commit = args->commit;
323 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
324 char *base = xstrdup(args->base);
325 int baselen = strlen(base);
327 while (baselen > 0 && base[baselen - 1] == '/')
328 base[--baselen] = '\0';
329 write_zip_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
332 read_tree_recursive(args->tree, args->base, plen, 0,
333 args->pathspec, write_zip_entry);
334 write_zip_trailer(args->commit_sha1);
341 void *parse_extra_zip_args(int argc, const char **argv)
343 for (; argc > 0; argc--, argv++) {
344 const char *arg = argv[0];
346 if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0')
347 zlib_compression_level = arg[1] - '0';
349 die("Unknown argument for zip format: %s", arg);