2 * Copyright (c) 2006 Rene Scharfe
10 static unsigned char *zip_dir
;
11 static unsigned int zip_dir_size
;
13 static unsigned int zip_offset
;
14 static unsigned int zip_dir_offset
;
15 static unsigned int zip_dir_entries
;
17 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
19 struct zip_local_header
{
20 unsigned char magic
[4];
21 unsigned char version
[2];
22 unsigned char flags
[2];
23 unsigned char compression_method
[2];
24 unsigned char mtime
[2];
25 unsigned char mdate
[2];
26 unsigned char crc32
[4];
27 unsigned char compressed_size
[4];
28 unsigned char size
[4];
29 unsigned char filename_length
[2];
30 unsigned char extra_length
[2];
31 unsigned char _end
[1];
34 struct zip_dir_header
{
35 unsigned char magic
[4];
36 unsigned char creator_version
[2];
37 unsigned char version
[2];
38 unsigned char flags
[2];
39 unsigned char compression_method
[2];
40 unsigned char mtime
[2];
41 unsigned char mdate
[2];
42 unsigned char crc32
[4];
43 unsigned char compressed_size
[4];
44 unsigned char size
[4];
45 unsigned char filename_length
[2];
46 unsigned char extra_length
[2];
47 unsigned char comment_length
[2];
48 unsigned char disk
[2];
49 unsigned char attr1
[2];
50 unsigned char attr2
[4];
51 unsigned char offset
[4];
52 unsigned char _end
[1];
55 struct zip_dir_trailer
{
56 unsigned char magic
[4];
57 unsigned char disk
[2];
58 unsigned char directory_start_disk
[2];
59 unsigned char entries_on_this_disk
[2];
60 unsigned char entries
[2];
61 unsigned char size
[4];
62 unsigned char offset
[4];
63 unsigned char comment_length
[2];
64 unsigned char _end
[1];
68 * On ARM, padding is added at the end of the struct, so a simple
69 * sizeof(struct ...) reports two bytes more than the payload size
70 * we're interested in.
72 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
73 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
74 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
76 static void copy_le16(unsigned char *dest
, unsigned int n
)
79 dest
[1] = 0xff & (n
>> 010);
82 static void copy_le32(unsigned char *dest
, unsigned int n
)
85 dest
[1] = 0xff & (n
>> 010);
86 dest
[2] = 0xff & (n
>> 020);
87 dest
[3] = 0xff & (n
>> 030);
90 static void *zlib_deflate(void *data
, unsigned long size
,
91 int compression_level
, unsigned long *compressed_size
)
94 unsigned long maxsize
;
98 memset(&stream
, 0, sizeof(stream
));
99 git_deflate_init(&stream
, compression_level
);
100 maxsize
= git_deflate_bound(&stream
, size
);
101 buffer
= xmalloc(maxsize
);
103 stream
.next_in
= data
;
104 stream
.avail_in
= size
;
105 stream
.next_out
= buffer
;
106 stream
.avail_out
= maxsize
;
109 result
= git_deflate(&stream
, Z_FINISH
);
110 } while (result
== Z_OK
);
112 if (result
!= Z_STREAM_END
) {
117 git_deflate_end(&stream
);
118 *compressed_size
= stream
.total_out
;
123 static void set_zip_dir_data_desc(struct zip_dir_header
*header
,
125 unsigned long compressed_size
,
128 copy_le32(header
->crc32
, crc
);
129 copy_le32(header
->compressed_size
, compressed_size
);
130 copy_le32(header
->size
, size
);
133 static void set_zip_header_data_desc(struct zip_local_header
*header
,
135 unsigned long compressed_size
,
138 copy_le32(header
->crc32
, crc
);
139 copy_le32(header
->compressed_size
, compressed_size
);
140 copy_le32(header
->size
, size
);
143 static int write_zip_entry(struct archiver_args
*args
,
144 const unsigned char *sha1
,
145 const char *path
, size_t pathlen
,
148 struct zip_local_header header
;
149 struct zip_dir_header dirent
;
151 unsigned long compressed_size
;
153 unsigned long direntsize
;
156 void *deflated
= NULL
;
160 crc
= crc32(0, NULL
, 0);
162 if (pathlen
> 0xffff) {
163 return error("path too long (%d chars, SHA1: %s): %s",
164 (int)pathlen
, sha1_to_hex(sha1
), path
);
167 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
175 } else if (S_ISREG(mode
) || S_ISLNK(mode
)) {
176 enum object_type type
;
177 buffer
= sha1_file_to_archive(args
, path
, sha1
, mode
, &type
, &size
);
179 return error("cannot read %s", sha1_to_hex(sha1
));
182 attr2
= S_ISLNK(mode
) ? ((mode
| 0777) << 16) :
183 (mode
& 0111) ? ((mode
) << 16) : 0;
184 if (S_ISREG(mode
) && args
->compression_level
!= 0)
186 crc
= crc32(crc
, buffer
, size
);
188 compressed_size
= size
;
190 return error("unsupported file mode: 0%o (SHA1: %s)", mode
,
195 deflated
= zlib_deflate(buffer
, size
, args
->compression_level
,
197 if (deflated
&& compressed_size
- 6 < size
) {
198 /* ZLIB --> raw compressed data (see RFC 1950) */
199 /* CMF and FLG ... */
200 out
= (unsigned char *)deflated
+ 2;
201 compressed_size
-= 6; /* ... and ADLER32 */
204 compressed_size
= size
;
208 /* make sure we have enough free space in the dictionary */
209 direntsize
= ZIP_DIR_HEADER_SIZE
+ pathlen
;
210 while (zip_dir_size
< zip_dir_offset
+ direntsize
) {
211 zip_dir_size
+= ZIP_DIRECTORY_MIN_SIZE
;
212 zip_dir
= xrealloc(zip_dir
, zip_dir_size
);
215 copy_le32(dirent
.magic
, 0x02014b50);
216 copy_le16(dirent
.creator_version
,
217 S_ISLNK(mode
) || (S_ISREG(mode
) && (mode
& 0111)) ? 0x0317 : 0);
218 copy_le16(dirent
.version
, 10);
219 copy_le16(dirent
.flags
, 0);
220 copy_le16(dirent
.compression_method
, method
);
221 copy_le16(dirent
.mtime
, zip_time
);
222 copy_le16(dirent
.mdate
, zip_date
);
223 set_zip_dir_data_desc(&dirent
, size
, compressed_size
, crc
);
224 copy_le16(dirent
.filename_length
, pathlen
);
225 copy_le16(dirent
.extra_length
, 0);
226 copy_le16(dirent
.comment_length
, 0);
227 copy_le16(dirent
.disk
, 0);
228 copy_le16(dirent
.attr1
, 0);
229 copy_le32(dirent
.attr2
, attr2
);
230 copy_le32(dirent
.offset
, zip_offset
);
232 copy_le32(header
.magic
, 0x04034b50);
233 copy_le16(header
.version
, 10);
234 copy_le16(header
.flags
, 0);
235 copy_le16(header
.compression_method
, method
);
236 copy_le16(header
.mtime
, zip_time
);
237 copy_le16(header
.mdate
, zip_date
);
238 set_zip_header_data_desc(&header
, size
, compressed_size
, crc
);
239 copy_le16(header
.filename_length
, pathlen
);
240 copy_le16(header
.extra_length
, 0);
241 write_or_die(1, &header
, ZIP_LOCAL_HEADER_SIZE
);
242 zip_offset
+= ZIP_LOCAL_HEADER_SIZE
;
243 write_or_die(1, path
, pathlen
);
244 zip_offset
+= pathlen
;
245 if (compressed_size
> 0) {
246 write_or_die(1, out
, compressed_size
);
247 zip_offset
+= compressed_size
;
253 memcpy(zip_dir
+ zip_dir_offset
, &dirent
, ZIP_DIR_HEADER_SIZE
);
254 zip_dir_offset
+= ZIP_DIR_HEADER_SIZE
;
255 memcpy(zip_dir
+ zip_dir_offset
, path
, pathlen
);
256 zip_dir_offset
+= pathlen
;
262 static void write_zip_trailer(const unsigned char *sha1
)
264 struct zip_dir_trailer trailer
;
266 copy_le32(trailer
.magic
, 0x06054b50);
267 copy_le16(trailer
.disk
, 0);
268 copy_le16(trailer
.directory_start_disk
, 0);
269 copy_le16(trailer
.entries_on_this_disk
, zip_dir_entries
);
270 copy_le16(trailer
.entries
, zip_dir_entries
);
271 copy_le32(trailer
.size
, zip_dir_offset
);
272 copy_le32(trailer
.offset
, zip_offset
);
273 copy_le16(trailer
.comment_length
, sha1
? 40 : 0);
275 write_or_die(1, zip_dir
, zip_dir_offset
);
276 write_or_die(1, &trailer
, ZIP_DIR_TRAILER_SIZE
);
278 write_or_die(1, sha1_to_hex(sha1
), 40);
281 static void dos_time(time_t *time
, int *dos_date
, int *dos_time
)
283 struct tm
*t
= localtime(time
);
285 *dos_date
= t
->tm_mday
+ (t
->tm_mon
+ 1) * 32 +
286 (t
->tm_year
+ 1900 - 1980) * 512;
287 *dos_time
= t
->tm_sec
/ 2 + t
->tm_min
* 32 + t
->tm_hour
* 2048;
290 static int write_zip_archive(const struct archiver
*ar
,
291 struct archiver_args
*args
)
295 dos_time(&args
->time
, &zip_date
, &zip_time
);
297 zip_dir
= xmalloc(ZIP_DIRECTORY_MIN_SIZE
);
298 zip_dir_size
= ZIP_DIRECTORY_MIN_SIZE
;
300 err
= write_archive_entries(args
, write_zip_entry
);
302 write_zip_trailer(args
->commit_sha1
);
309 static struct archiver zip_archiver
= {
312 ARCHIVER_WANT_COMPRESSION_LEVELS
|ARCHIVER_REMOTE
315 void init_zip_archiver(void)
317 register_archiver(&zip_archiver
);