archive-zip: support UTF-8 paths
[git] / archive-zip.c
1 /*
2  * Copyright (c) 2006 Rene Scharfe
3  */
4 #include "cache.h"
5 #include "archive.h"
6 #include "streaming.h"
7 #include "utf8.h"
8
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)
20 #define ZIP_STREAM      (1 <<  3)
21 #define ZIP_UTF8        (1 << 11)
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];
35         unsigned char _end[1];
36 };
37
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
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];
64         unsigned char _end[1];
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];
76         unsigned char _end[1];
77 };
78
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)
85 #define ZIP_DATA_DESC_SIZE      offsetof(struct zip_data_desc, _end)
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
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,
104                 int compression_level, unsigned long *compressed_size)
105 {
106         git_zstream stream;
107         unsigned long maxsize;
108         void *buffer;
109         int result;
110
111         memset(&stream, 0, sizeof(stream));
112         git_deflate_init(&stream, compression_level);
113         maxsize = git_deflate_bound(&stream, size);
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 {
122                 result = git_deflate(&stream, Z_FINISH);
123         } while (result == Z_OK);
124
125         if (result != Z_STREAM_END) {
126                 free(buffer);
127                 return NULL;
128         }
129
130         git_deflate_end(&stream);
131         *compressed_size = stream.total_out;
132
133         return buffer;
134 }
135
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
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
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
180 #define STREAM_BUFFER_SIZE (1024 * 16)
181
182 static int write_zip_entry(struct archiver_args *args,
183                            const unsigned char *sha1,
184                            const char *path, size_t pathlen,
185                            unsigned int mode)
186 {
187         struct zip_local_header header;
188         struct zip_dir_header dirent;
189         unsigned long attr2;
190         unsigned long compressed_size;
191         unsigned long crc;
192         unsigned long direntsize;
193         int method;
194         unsigned char *out;
195         void *deflated = NULL;
196         void *buffer;
197         struct git_istream *stream = NULL;
198         unsigned long flags = 0;
199         unsigned long size;
200
201         crc = crc32(0, NULL, 0);
202
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
210         if (pathlen > 0xffff) {
211                 return error("path too long (%d chars, SHA1: %s): %s",
212                                 (int)pathlen, sha1_to_hex(sha1), path);
213         }
214
215         if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
216                 method = 0;
217                 attr2 = 16;
218                 out = NULL;
219                 size = 0;
220                 compressed_size = 0;
221                 buffer = NULL;
222                 size = 0;
223         } else if (S_ISREG(mode) || S_ISLNK(mode)) {
224                 enum object_type type = sha1_object_info(sha1, &size);
225
226                 method = 0;
227                 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
228                         (mode & 0111) ? ((mode) << 16) : 0;
229                 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
230                         method = 8;
231                 compressed_size = size;
232
233                 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
234                     size > big_file_threshold) {
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                 }
250         } else {
251                 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
252                                 sha1_to_hex(sha1));
253         }
254
255         if (buffer && method == 8) {
256                 deflated = zlib_deflate(buffer, size, args->compression_level,
257                                 &compressed_size);
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 */
270         direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
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);
277         copy_le16(dirent.creator_version,
278                 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
279         copy_le16(dirent.version, 10);
280         copy_le16(dirent.flags, flags);
281         copy_le16(dirent.compression_method, method);
282         copy_le16(dirent.mtime, zip_time);
283         copy_le16(dirent.mdate, zip_date);
284         set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
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);
290         copy_le32(dirent.attr2, attr2);
291         copy_le32(dirent.offset, zip_offset);
292
293         copy_le32(header.magic, 0x04034b50);
294         copy_le16(header.version, 10);
295         copy_le16(header.flags, flags);
296         copy_le16(header.compression_method, method);
297         copy_le16(header.mtime, zip_time);
298         copy_le16(header.mdate, zip_date);
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);
303         copy_le16(header.filename_length, pathlen);
304         copy_le16(header.extra_length, 0);
305         write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
306         zip_offset += ZIP_LOCAL_HEADER_SIZE;
307         write_or_die(1, path, pathlen);
308         zip_offset += pathlen;
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
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
392                 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
393         } else if (compressed_size > 0) {
394                 write_or_die(1, out, compressed_size);
395                 zip_offset += compressed_size;
396         }
397
398         free(deflated);
399         free(buffer);
400
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
407         return 0;
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);
424         write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
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
438 static int write_zip_archive(const struct archiver *ar,
439                              struct archiver_args *args)
440 {
441         int err;
442
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;
447
448         err = write_archive_entries(args, write_zip_entry);
449         if (!err)
450                 write_zip_trailer(args->commit_sha1);
451
452         free(zip_dir);
453
454         return err;
455 }
456
457 static struct archiver zip_archiver = {
458         "zip",
459         write_zip_archive,
460         ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
461 };
462
463 void init_zip_archiver(void)
464 {
465         register_archiver(&zip_archiver);
466 }