2 * Copyright (c) 2006 Rene Scharfe
12 static unsigned char *zip_dir
;
13 static unsigned int zip_dir_size
;
15 static unsigned int zip_offset
;
16 static unsigned int zip_dir_offset
;
17 static unsigned int zip_dir_entries
;
19 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
20 #define ZIP_STREAM (1 << 3)
21 #define ZIP_UTF8 (1 << 11)
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];
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];
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];
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];
79 struct 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];
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.
92 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
93 #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
94 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
95 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
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))
100 static void copy_le16(unsigned char *dest
, unsigned int n
)
103 dest
[1] = 0xff & (n
>> 010);
106 static void copy_le32(unsigned char *dest
, unsigned int n
)
109 dest
[1] = 0xff & (n
>> 010);
110 dest
[2] = 0xff & (n
>> 020);
111 dest
[3] = 0xff & (n
>> 030);
114 static void *zlib_deflate_raw(void *data
, unsigned long size
,
115 int compression_level
,
116 unsigned long *compressed_size
)
119 unsigned long maxsize
;
123 memset(&stream
, 0, sizeof(stream
));
124 git_deflate_init_raw(&stream
, compression_level
);
125 maxsize
= git_deflate_bound(&stream
, size
);
126 buffer
= xmalloc(maxsize
);
128 stream
.next_in
= data
;
129 stream
.avail_in
= size
;
130 stream
.next_out
= buffer
;
131 stream
.avail_out
= maxsize
;
134 result
= git_deflate(&stream
, Z_FINISH
);
135 } while (result
== Z_OK
);
137 if (result
!= Z_STREAM_END
) {
142 git_deflate_end(&stream
);
143 *compressed_size
= stream
.total_out
;
148 static void write_zip_data_desc(unsigned long size
,
149 unsigned long compressed_size
,
152 struct zip_data_desc trailer
;
154 copy_le32(trailer
.magic
, 0x08074b50);
155 copy_le32(trailer
.crc32
, crc
);
156 copy_le32(trailer
.compressed_size
, compressed_size
);
157 copy_le32(trailer
.size
, size
);
158 write_or_die(1, &trailer
, ZIP_DATA_DESC_SIZE
);
161 static void set_zip_dir_data_desc(struct zip_dir_header
*header
,
163 unsigned long compressed_size
,
166 copy_le32(header
->crc32
, crc
);
167 copy_le32(header
->compressed_size
, compressed_size
);
168 copy_le32(header
->size
, size
);
171 static void set_zip_header_data_desc(struct zip_local_header
*header
,
173 unsigned long compressed_size
,
176 copy_le32(header
->crc32
, crc
);
177 copy_le32(header
->compressed_size
, compressed_size
);
178 copy_le32(header
->size
, size
);
181 static int has_only_ascii(const char *s
)
192 #define STREAM_BUFFER_SIZE (1024 * 16)
194 static int write_zip_entry(struct archiver_args
*args
,
195 const unsigned char *sha1
,
196 const char *path
, size_t pathlen
,
199 struct zip_local_header header
;
200 struct zip_dir_header dirent
;
201 struct zip_extra_mtime extra
;
203 unsigned long compressed_size
;
205 unsigned long direntsize
;
208 void *deflated
= NULL
;
210 struct git_istream
*stream
= NULL
;
211 unsigned long flags
= 0;
214 crc
= crc32(0, NULL
, 0);
216 if (!has_only_ascii(path
)) {
220 warning("Path is not valid UTF-8: %s", path
);
223 if (pathlen
> 0xffff) {
224 return error("path too long (%d chars, SHA1: %s): %s",
225 (int)pathlen
, sha1_to_hex(sha1
), path
);
228 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
236 } else if (S_ISREG(mode
) || S_ISLNK(mode
)) {
237 enum object_type type
= sha1_object_info(sha1
, &size
);
240 attr2
= S_ISLNK(mode
) ? ((mode
| 0777) << 16) :
241 (mode
& 0111) ? ((mode
) << 16) : 0;
242 if (S_ISREG(mode
) && args
->compression_level
!= 0 && size
> 0)
245 if (S_ISREG(mode
) && type
== OBJ_BLOB
&& !args
->convert
&&
246 size
> big_file_threshold
) {
247 stream
= open_istream(sha1
, &type
, &size
, NULL
);
249 return error("cannot stream blob %s",
254 buffer
= sha1_file_to_archive(args
, path
, sha1
, mode
,
257 return error("cannot read %s",
259 crc
= crc32(crc
, buffer
, size
);
262 compressed_size
= (method
== 0) ? size
: 0;
264 return error("unsupported file mode: 0%o (SHA1: %s)", mode
,
268 if (buffer
&& method
== 8) {
269 out
= deflated
= zlib_deflate_raw(buffer
, size
,
270 args
->compression_level
,
272 if (!out
|| compressed_size
>= size
) {
275 compressed_size
= size
;
279 copy_le16(extra
.magic
, 0x5455);
280 copy_le16(extra
.extra_size
, ZIP_EXTRA_MTIME_PAYLOAD_SIZE
);
281 extra
.flags
[0] = 1; /* just mtime */
282 copy_le32(extra
.mtime
, args
->time
);
284 /* make sure we have enough free space in the dictionary */
285 direntsize
= ZIP_DIR_HEADER_SIZE
+ pathlen
+ ZIP_EXTRA_MTIME_SIZE
;
286 while (zip_dir_size
< zip_dir_offset
+ direntsize
) {
287 zip_dir_size
+= ZIP_DIRECTORY_MIN_SIZE
;
288 zip_dir
= xrealloc(zip_dir
, zip_dir_size
);
291 copy_le32(dirent
.magic
, 0x02014b50);
292 copy_le16(dirent
.creator_version
,
293 S_ISLNK(mode
) || (S_ISREG(mode
) && (mode
& 0111)) ? 0x0317 : 0);
294 copy_le16(dirent
.version
, 10);
295 copy_le16(dirent
.flags
, flags
);
296 copy_le16(dirent
.compression_method
, method
);
297 copy_le16(dirent
.mtime
, zip_time
);
298 copy_le16(dirent
.mdate
, zip_date
);
299 set_zip_dir_data_desc(&dirent
, size
, compressed_size
, crc
);
300 copy_le16(dirent
.filename_length
, pathlen
);
301 copy_le16(dirent
.extra_length
, ZIP_EXTRA_MTIME_SIZE
);
302 copy_le16(dirent
.comment_length
, 0);
303 copy_le16(dirent
.disk
, 0);
304 copy_le16(dirent
.attr1
, 0);
305 copy_le32(dirent
.attr2
, attr2
);
306 copy_le32(dirent
.offset
, zip_offset
);
308 copy_le32(header
.magic
, 0x04034b50);
309 copy_le16(header
.version
, 10);
310 copy_le16(header
.flags
, flags
);
311 copy_le16(header
.compression_method
, method
);
312 copy_le16(header
.mtime
, zip_time
);
313 copy_le16(header
.mdate
, zip_date
);
314 set_zip_header_data_desc(&header
, size
, compressed_size
, crc
);
315 copy_le16(header
.filename_length
, pathlen
);
316 copy_le16(header
.extra_length
, ZIP_EXTRA_MTIME_SIZE
);
317 write_or_die(1, &header
, ZIP_LOCAL_HEADER_SIZE
);
318 zip_offset
+= ZIP_LOCAL_HEADER_SIZE
;
319 write_or_die(1, path
, pathlen
);
320 zip_offset
+= pathlen
;
321 write_or_die(1, &extra
, ZIP_EXTRA_MTIME_SIZE
);
322 zip_offset
+= ZIP_EXTRA_MTIME_SIZE
;
323 if (stream
&& method
== 0) {
324 unsigned char buf
[STREAM_BUFFER_SIZE
];
328 readlen
= read_istream(stream
, buf
, sizeof(buf
));
331 crc
= crc32(crc
, buf
, readlen
);
332 write_or_die(1, buf
, readlen
);
334 close_istream(stream
);
338 compressed_size
= size
;
339 zip_offset
+= compressed_size
;
341 write_zip_data_desc(size
, compressed_size
, crc
);
342 zip_offset
+= ZIP_DATA_DESC_SIZE
;
344 set_zip_dir_data_desc(&dirent
, size
, compressed_size
, crc
);
345 } else if (stream
&& method
== 8) {
346 unsigned char buf
[STREAM_BUFFER_SIZE
];
351 unsigned char compressed
[STREAM_BUFFER_SIZE
* 2];
353 memset(&zstream
, 0, sizeof(zstream
));
354 git_deflate_init_raw(&zstream
, args
->compression_level
);
357 zstream
.next_out
= compressed
;
358 zstream
.avail_out
= sizeof(compressed
);
361 readlen
= read_istream(stream
, buf
, sizeof(buf
));
364 crc
= crc32(crc
, buf
, readlen
);
366 zstream
.next_in
= buf
;
367 zstream
.avail_in
= readlen
;
368 result
= git_deflate(&zstream
, 0);
370 die("deflate error (%d)", result
);
371 out_len
= zstream
.next_out
- compressed
;
374 write_or_die(1, compressed
, out_len
);
375 compressed_size
+= out_len
;
376 zstream
.next_out
= compressed
;
377 zstream
.avail_out
= sizeof(compressed
);
381 close_istream(stream
);
385 zstream
.next_in
= buf
;
386 zstream
.avail_in
= 0;
387 result
= git_deflate(&zstream
, Z_FINISH
);
388 if (result
!= Z_STREAM_END
)
389 die("deflate error (%d)", result
);
391 git_deflate_end(&zstream
);
392 out_len
= zstream
.next_out
- compressed
;
393 write_or_die(1, compressed
, out_len
);
394 compressed_size
+= out_len
;
395 zip_offset
+= compressed_size
;
397 write_zip_data_desc(size
, compressed_size
, crc
);
398 zip_offset
+= ZIP_DATA_DESC_SIZE
;
400 set_zip_dir_data_desc(&dirent
, size
, compressed_size
, crc
);
401 } else if (compressed_size
> 0) {
402 write_or_die(1, out
, compressed_size
);
403 zip_offset
+= compressed_size
;
409 memcpy(zip_dir
+ zip_dir_offset
, &dirent
, ZIP_DIR_HEADER_SIZE
);
410 zip_dir_offset
+= ZIP_DIR_HEADER_SIZE
;
411 memcpy(zip_dir
+ zip_dir_offset
, path
, pathlen
);
412 zip_dir_offset
+= pathlen
;
413 memcpy(zip_dir
+ zip_dir_offset
, &extra
, ZIP_EXTRA_MTIME_SIZE
);
414 zip_dir_offset
+= ZIP_EXTRA_MTIME_SIZE
;
420 static void write_zip_trailer(const unsigned char *sha1
)
422 struct zip_dir_trailer trailer
;
424 copy_le32(trailer
.magic
, 0x06054b50);
425 copy_le16(trailer
.disk
, 0);
426 copy_le16(trailer
.directory_start_disk
, 0);
427 copy_le16(trailer
.entries_on_this_disk
, zip_dir_entries
);
428 copy_le16(trailer
.entries
, zip_dir_entries
);
429 copy_le32(trailer
.size
, zip_dir_offset
);
430 copy_le32(trailer
.offset
, zip_offset
);
431 copy_le16(trailer
.comment_length
, sha1
? 40 : 0);
433 write_or_die(1, zip_dir
, zip_dir_offset
);
434 write_or_die(1, &trailer
, ZIP_DIR_TRAILER_SIZE
);
436 write_or_die(1, sha1_to_hex(sha1
), 40);
439 static void dos_time(time_t *time
, int *dos_date
, int *dos_time
)
441 struct tm
*t
= localtime(time
);
443 *dos_date
= t
->tm_mday
+ (t
->tm_mon
+ 1) * 32 +
444 (t
->tm_year
+ 1900 - 1980) * 512;
445 *dos_time
= t
->tm_sec
/ 2 + t
->tm_min
* 32 + t
->tm_hour
* 2048;
448 static int write_zip_archive(const struct archiver
*ar
,
449 struct archiver_args
*args
)
453 dos_time(&args
->time
, &zip_date
, &zip_time
);
455 zip_dir
= xmalloc(ZIP_DIRECTORY_MIN_SIZE
);
456 zip_dir_size
= ZIP_DIRECTORY_MIN_SIZE
;
458 err
= write_archive_entries(args
, write_zip_entry
);
460 write_zip_trailer(args
->commit_sha1
);
467 static struct archiver zip_archiver
= {
470 ARCHIVER_WANT_COMPRESSION_LEVELS
|ARCHIVER_REMOTE
473 void init_zip_archiver(void)
475 register_archiver(&zip_archiver
);