2 * Copyright (c) 2006 Rene Scharfe
15 static unsigned char *zip_dir;
16 static unsigned int zip_dir_size;
18 static unsigned int zip_offset;
19 static unsigned int zip_dir_offset;
20 static unsigned int zip_dir_entries;
22 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
24 struct zip_local_header {
25 unsigned char magic[4];
26 unsigned char version[2];
27 unsigned char flags[2];
28 unsigned char compression_method[2];
29 unsigned char mtime[2];
30 unsigned char mdate[2];
31 unsigned char crc32[4];
32 unsigned char compressed_size[4];
33 unsigned char size[4];
34 unsigned char filename_length[2];
35 unsigned char extra_length[2];
36 unsigned char _end[1];
39 struct zip_dir_header {
40 unsigned char magic[4];
41 unsigned char creator_version[2];
42 unsigned char version[2];
43 unsigned char flags[2];
44 unsigned char compression_method[2];
45 unsigned char mtime[2];
46 unsigned char mdate[2];
47 unsigned char crc32[4];
48 unsigned char compressed_size[4];
49 unsigned char size[4];
50 unsigned char filename_length[2];
51 unsigned char extra_length[2];
52 unsigned char comment_length[2];
53 unsigned char disk[2];
54 unsigned char attr1[2];
55 unsigned char attr2[4];
56 unsigned char offset[4];
57 unsigned char _end[1];
60 struct zip_dir_trailer {
61 unsigned char magic[4];
62 unsigned char disk[2];
63 unsigned char directory_start_disk[2];
64 unsigned char entries_on_this_disk[2];
65 unsigned char entries[2];
66 unsigned char size[4];
67 unsigned char offset[4];
68 unsigned char comment_length[2];
69 unsigned char _end[1];
73 * On ARM, padding is added at the end of the struct, so a simple
74 * sizeof(struct ...) reports two bytes more than the payload size
75 * we're interested in.
77 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
78 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
79 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
81 static void copy_le16(unsigned char *dest, unsigned int n)
84 dest[1] = 0xff & (n >> 010);
87 static void copy_le32(unsigned char *dest, unsigned int n)
90 dest[1] = 0xff & (n >> 010);
91 dest[2] = 0xff & (n >> 020);
92 dest[3] = 0xff & (n >> 030);
95 static void *zlib_deflate(void *data, unsigned long size,
96 int compression_level, unsigned long *compressed_size)
99 unsigned long maxsize;
103 memset(&stream, 0, sizeof(stream));
104 deflateInit(&stream, compression_level);
105 maxsize = deflateBound(&stream, size);
106 buffer = xmalloc(maxsize);
108 stream.next_in = data;
109 stream.avail_in = size;
110 stream.next_out = buffer;
111 stream.avail_out = maxsize;
114 result = deflate(&stream, Z_FINISH);
115 } while (result == Z_OK);
117 if (result != Z_STREAM_END) {
123 *compressed_size = stream.total_out;
128 static int write_zip_entry(struct archiver_args *args,
129 const unsigned char *sha1, const char *path, size_t pathlen,
130 unsigned int mode, void *buffer, unsigned long size)
132 struct zip_local_header header;
133 struct zip_dir_header dirent;
135 unsigned long compressed_size;
136 unsigned long uncompressed_size;
138 unsigned long direntsize;
141 void *deflated = NULL;
143 crc = crc32(0, NULL, 0);
145 if (pathlen > 0xffff) {
146 return error("path too long (%d chars, SHA1: %s): %s",
147 (int)pathlen, sha1_to_hex(sha1), path);
150 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
154 uncompressed_size = 0;
156 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
158 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
159 (mode & 0111) ? ((mode) << 16) : 0;
160 if (S_ISREG(mode) && args->compression_level != 0)
162 crc = crc32(crc, buffer, size);
164 uncompressed_size = size;
165 compressed_size = size;
167 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
172 deflated = zlib_deflate(buffer, size, args->compression_level,
174 if (deflated && compressed_size - 6 < size) {
175 /* ZLIB --> raw compressed data (see RFC 1950) */
176 /* CMF and FLG ... */
177 out = (unsigned char *)deflated + 2;
178 compressed_size -= 6; /* ... and ADLER32 */
181 compressed_size = size;
185 /* make sure we have enough free space in the dictionary */
186 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
187 while (zip_dir_size < zip_dir_offset + direntsize) {
188 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
189 zip_dir = xrealloc(zip_dir, zip_dir_size);
192 copy_le32(dirent.magic, 0x02014b50);
193 copy_le16(dirent.creator_version,
194 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
195 copy_le16(dirent.version, 10);
196 copy_le16(dirent.flags, 0);
197 copy_le16(dirent.compression_method, method);
198 copy_le16(dirent.mtime, zip_time);
199 copy_le16(dirent.mdate, zip_date);
200 copy_le32(dirent.crc32, crc);
201 copy_le32(dirent.compressed_size, compressed_size);
202 copy_le32(dirent.size, uncompressed_size);
203 copy_le16(dirent.filename_length, pathlen);
204 copy_le16(dirent.extra_length, 0);
205 copy_le16(dirent.comment_length, 0);
206 copy_le16(dirent.disk, 0);
207 copy_le16(dirent.attr1, 0);
208 copy_le32(dirent.attr2, attr2);
209 copy_le32(dirent.offset, zip_offset);
210 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
211 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
212 memcpy(zip_dir + zip_dir_offset, path, pathlen);
213 zip_dir_offset += pathlen;
216 copy_le32(header.magic, 0x04034b50);
217 copy_le16(header.version, 10);
218 copy_le16(header.flags, 0);
219 copy_le16(header.compression_method, method);
220 copy_le16(header.mtime, zip_time);
221 copy_le16(header.mdate, zip_date);
222 copy_le32(header.crc32, crc);
223 copy_le32(header.compressed_size, compressed_size);
224 copy_le32(header.size, uncompressed_size);
225 copy_le16(header.filename_length, pathlen);
226 copy_le16(header.extra_length, 0);
227 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
228 zip_offset += ZIP_LOCAL_HEADER_SIZE;
229 write_or_die(1, path, pathlen);
230 zip_offset += pathlen;
231 if (compressed_size > 0) {
232 write_or_die(1, out, compressed_size);
233 zip_offset += compressed_size;
241 static void write_zip_trailer(const unsigned char *sha1)
243 struct zip_dir_trailer trailer;
245 copy_le32(trailer.magic, 0x06054b50);
246 copy_le16(trailer.disk, 0);
247 copy_le16(trailer.directory_start_disk, 0);
248 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
249 copy_le16(trailer.entries, zip_dir_entries);
250 copy_le32(trailer.size, zip_dir_offset);
251 copy_le32(trailer.offset, zip_offset);
252 copy_le16(trailer.comment_length, sha1 ? 40 : 0);
254 write_or_die(1, zip_dir, zip_dir_offset);
255 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
257 write_or_die(1, sha1_to_hex(sha1), 40);
260 static void dos_time(time_t *time, int *dos_date, int *dos_time)
262 struct tm *t = localtime(time);
264 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
265 (t->tm_year + 1900 - 1980) * 512;
266 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
269 int write_zip_archive(struct archiver_args *args)
273 dos_time(&args->time, &zip_date, &zip_time);
275 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
276 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
278 err = write_archive_entries(args, write_zip_entry);
280 write_zip_trailer(args->commit_sha1);