2 * Copyright (c) 2006 Rene Scharfe
16 static unsigned char *zip_dir
;
17 static unsigned int zip_dir_size
;
19 static unsigned int zip_offset
;
20 static unsigned int zip_dir_offset
;
21 static unsigned int zip_dir_entries
;
23 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
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_dir_header
{
41 unsigned char magic
[4];
42 unsigned char creator_version
[2];
43 unsigned char version
[2];
44 unsigned char flags
[2];
45 unsigned char compression_method
[2];
46 unsigned char mtime
[2];
47 unsigned char mdate
[2];
48 unsigned char crc32
[4];
49 unsigned char compressed_size
[4];
50 unsigned char size
[4];
51 unsigned char filename_length
[2];
52 unsigned char extra_length
[2];
53 unsigned char comment_length
[2];
54 unsigned char disk
[2];
55 unsigned char attr1
[2];
56 unsigned char attr2
[4];
57 unsigned char offset
[4];
58 unsigned char _end
[1];
61 struct zip_dir_trailer
{
62 unsigned char magic
[4];
63 unsigned char disk
[2];
64 unsigned char directory_start_disk
[2];
65 unsigned char entries_on_this_disk
[2];
66 unsigned char entries
[2];
67 unsigned char size
[4];
68 unsigned char offset
[4];
69 unsigned char comment_length
[2];
70 unsigned char _end
[1];
74 * On ARM, padding is added at the end of the struct, so a simple
75 * sizeof(struct ...) reports two bytes more than the payload size
76 * we're interested in.
78 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
79 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
80 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
82 static void copy_le16(unsigned char *dest
, unsigned int n
)
85 dest
[1] = 0xff & (n
>> 010);
88 static void copy_le32(unsigned char *dest
, unsigned int n
)
91 dest
[1] = 0xff & (n
>> 010);
92 dest
[2] = 0xff & (n
>> 020);
93 dest
[3] = 0xff & (n
>> 030);
96 static void *zlib_deflate(void *data
, unsigned long size
,
97 unsigned long *compressed_size
)
100 unsigned long maxsize
;
104 memset(&stream
, 0, sizeof(stream
));
105 deflateInit(&stream
, zlib_compression_level
);
106 maxsize
= deflateBound(&stream
, size
);
107 buffer
= xmalloc(maxsize
);
109 stream
.next_in
= data
;
110 stream
.avail_in
= size
;
111 stream
.next_out
= buffer
;
112 stream
.avail_out
= maxsize
;
115 result
= deflate(&stream
, Z_FINISH
);
116 } while (result
== Z_OK
);
118 if (result
!= Z_STREAM_END
) {
124 *compressed_size
= stream
.total_out
;
129 static char *construct_path(const char *base
, int baselen
,
130 const char *filename
, int isdir
, int *pathlen
)
132 int filenamelen
= strlen(filename
);
133 int len
= baselen
+ filenamelen
;
138 p
= path
= xmalloc(len
+ 1);
140 memcpy(p
, base
, baselen
);
142 memcpy(p
, filename
, filenamelen
);
153 static int write_zip_entry(const unsigned char *sha1
,
154 const char *base
, int baselen
,
155 const char *filename
, unsigned mode
, int stage
)
157 struct zip_local_header header
;
158 struct zip_dir_header dirent
;
160 unsigned long compressed_size
;
161 unsigned long uncompressed_size
;
163 unsigned long direntsize
;
170 enum object_type type
;
172 void *deflated
= NULL
;
174 crc
= crc32(0, NULL
, 0);
176 path
= construct_path(base
, baselen
, filename
, S_ISDIR(mode
), &pathlen
);
178 fprintf(stderr
, "%s\n", path
);
179 if (pathlen
> 0xffff) {
180 error("path too long (%d chars, SHA1: %s): %s", pathlen
,
181 sha1_to_hex(sha1
), path
);
185 if (S_ISDIR(mode
) || S_ISGITLINK(mode
)) {
188 result
= (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
190 uncompressed_size
= 0;
192 } else if (S_ISREG(mode
) || S_ISLNK(mode
)) {
194 attr2
= S_ISLNK(mode
) ? ((mode
| 0777) << 16) : 0;
195 if (S_ISREG(mode
) && zlib_compression_level
!= 0)
198 buffer
= convert_sha1_file(path
, sha1
, mode
, &type
, &size
);
200 die("cannot read %s", sha1_to_hex(sha1
));
201 crc
= crc32(crc
, buffer
, size
);
203 uncompressed_size
= size
;
204 compressed_size
= size
;
206 error("unsupported file mode: 0%o (SHA1: %s)", mode
,
212 deflated
= zlib_deflate(buffer
, size
, &compressed_size
);
213 if (deflated
&& compressed_size
- 6 < size
) {
214 /* ZLIB --> raw compressed data (see RFC 1950) */
215 /* CMF and FLG ... */
216 out
= (unsigned char *)deflated
+ 2;
217 compressed_size
-= 6; /* ... and ADLER32 */
220 compressed_size
= size
;
224 /* make sure we have enough free space in the dictionary */
225 direntsize
= ZIP_DIR_HEADER_SIZE
+ pathlen
;
226 while (zip_dir_size
< zip_dir_offset
+ direntsize
) {
227 zip_dir_size
+= ZIP_DIRECTORY_MIN_SIZE
;
228 zip_dir
= xrealloc(zip_dir
, zip_dir_size
);
231 copy_le32(dirent
.magic
, 0x02014b50);
232 copy_le16(dirent
.creator_version
, S_ISLNK(mode
) ? 0x0317 : 0);
233 copy_le16(dirent
.version
, 10);
234 copy_le16(dirent
.flags
, 0);
235 copy_le16(dirent
.compression_method
, method
);
236 copy_le16(dirent
.mtime
, zip_time
);
237 copy_le16(dirent
.mdate
, zip_date
);
238 copy_le32(dirent
.crc32
, crc
);
239 copy_le32(dirent
.compressed_size
, compressed_size
);
240 copy_le32(dirent
.size
, uncompressed_size
);
241 copy_le16(dirent
.filename_length
, pathlen
);
242 copy_le16(dirent
.extra_length
, 0);
243 copy_le16(dirent
.comment_length
, 0);
244 copy_le16(dirent
.disk
, 0);
245 copy_le16(dirent
.attr1
, 0);
246 copy_le32(dirent
.attr2
, attr2
);
247 copy_le32(dirent
.offset
, zip_offset
);
248 memcpy(zip_dir
+ zip_dir_offset
, &dirent
, ZIP_DIR_HEADER_SIZE
);
249 zip_dir_offset
+= ZIP_DIR_HEADER_SIZE
;
250 memcpy(zip_dir
+ zip_dir_offset
, path
, pathlen
);
251 zip_dir_offset
+= pathlen
;
254 copy_le32(header
.magic
, 0x04034b50);
255 copy_le16(header
.version
, 10);
256 copy_le16(header
.flags
, 0);
257 copy_le16(header
.compression_method
, method
);
258 copy_le16(header
.mtime
, zip_time
);
259 copy_le16(header
.mdate
, zip_date
);
260 copy_le32(header
.crc32
, crc
);
261 copy_le32(header
.compressed_size
, compressed_size
);
262 copy_le32(header
.size
, uncompressed_size
);
263 copy_le16(header
.filename_length
, pathlen
);
264 copy_le16(header
.extra_length
, 0);
265 write_or_die(1, &header
, ZIP_LOCAL_HEADER_SIZE
);
266 zip_offset
+= ZIP_LOCAL_HEADER_SIZE
;
267 write_or_die(1, path
, pathlen
);
268 zip_offset
+= pathlen
;
269 if (compressed_size
> 0) {
270 write_or_die(1, out
, compressed_size
);
271 zip_offset
+= compressed_size
;
282 static void write_zip_trailer(const unsigned char *sha1
)
284 struct zip_dir_trailer trailer
;
286 copy_le32(trailer
.magic
, 0x06054b50);
287 copy_le16(trailer
.disk
, 0);
288 copy_le16(trailer
.directory_start_disk
, 0);
289 copy_le16(trailer
.entries_on_this_disk
, zip_dir_entries
);
290 copy_le16(trailer
.entries
, zip_dir_entries
);
291 copy_le32(trailer
.size
, zip_dir_offset
);
292 copy_le32(trailer
.offset
, zip_offset
);
293 copy_le16(trailer
.comment_length
, sha1
? 40 : 0);
295 write_or_die(1, zip_dir
, zip_dir_offset
);
296 write_or_die(1, &trailer
, ZIP_DIR_TRAILER_SIZE
);
298 write_or_die(1, sha1_to_hex(sha1
), 40);
301 static void dos_time(time_t *time
, int *dos_date
, int *dos_time
)
303 struct tm
*t
= localtime(time
);
305 *dos_date
= t
->tm_mday
+ (t
->tm_mon
+ 1) * 32 +
306 (t
->tm_year
+ 1900 - 1980) * 512;
307 *dos_time
= t
->tm_sec
/ 2 + t
->tm_min
* 32 + t
->tm_hour
* 2048;
310 int write_zip_archive(struct archiver_args
*args
)
312 int plen
= strlen(args
->base
);
314 dos_time(&args
->time
, &zip_date
, &zip_time
);
316 zip_dir
= xmalloc(ZIP_DIRECTORY_MIN_SIZE
);
317 zip_dir_size
= ZIP_DIRECTORY_MIN_SIZE
;
318 verbose
= args
->verbose
;
320 if (args
->base
&& plen
> 0 && args
->base
[plen
- 1] == '/') {
321 char *base
= xstrdup(args
->base
);
322 int baselen
= strlen(base
);
324 while (baselen
> 0 && base
[baselen
- 1] == '/')
325 base
[--baselen
] = '\0';
326 write_zip_entry(args
->tree
->object
.sha1
, "", 0, base
, 040777, 0);
329 read_tree_recursive(args
->tree
, args
->base
, plen
, 0,
330 args
->pathspec
, write_zip_entry
);
331 write_zip_trailer(args
->commit_sha1
);
338 void *parse_extra_zip_args(int argc
, const char **argv
)
340 for (; argc
> 0; argc
--, argv
++) {
341 const char *arg
= argv
[0];
343 if (arg
[0] == '-' && isdigit(arg
[1]) && arg
[2] == '\0')
344 zlib_compression_level
= arg
[1] - '0';
346 die("Unknown argument for zip format: %s", arg
);