Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Rene Scharfe |
| 3 | */ |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 4 | #include "cache.h" |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 5 | #include "archive.h" |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 6 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 7 | static int zip_date; |
| 8 | static int zip_time; |
| 9 | |
| 10 | static unsigned char *zip_dir; |
| 11 | static unsigned int zip_dir_size; |
| 12 | |
| 13 | static unsigned int zip_offset; |
| 14 | static unsigned int zip_dir_offset; |
| 15 | static unsigned int zip_dir_entries; |
| 16 | |
| 17 | #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024) |
| 18 | |
| 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]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 31 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
| 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]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 52 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 53 | }; |
| 54 | |
| 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]; |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 64 | unsigned char _end[1]; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 65 | }; |
| 66 | |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 67 | /* |
| 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. |
| 71 | */ |
| 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) |
| 75 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 76 | static void copy_le16(unsigned char *dest, unsigned int n) |
| 77 | { |
| 78 | dest[0] = 0xff & n; |
| 79 | dest[1] = 0xff & (n >> 010); |
| 80 | } |
| 81 | |
| 82 | static void copy_le32(unsigned char *dest, unsigned int n) |
| 83 | { |
| 84 | dest[0] = 0xff & n; |
| 85 | dest[1] = 0xff & (n >> 010); |
| 86 | dest[2] = 0xff & (n >> 020); |
| 87 | dest[3] = 0xff & (n >> 030); |
| 88 | } |
| 89 | |
| 90 | static void *zlib_deflate(void *data, unsigned long size, |
René Scharfe | 3a176c6 | 2008-07-18 16:30:32 +0200 | [diff] [blame] | 91 | int compression_level, unsigned long *compressed_size) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 92 | { |
| 93 | z_stream stream; |
| 94 | unsigned long maxsize; |
| 95 | void *buffer; |
| 96 | int result; |
| 97 | |
| 98 | memset(&stream, 0, sizeof(stream)); |
René Scharfe | 3a176c6 | 2008-07-18 16:30:32 +0200 | [diff] [blame] | 99 | deflateInit(&stream, compression_level); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 100 | maxsize = deflateBound(&stream, size); |
| 101 | buffer = xmalloc(maxsize); |
| 102 | |
| 103 | stream.next_in = data; |
| 104 | stream.avail_in = size; |
| 105 | stream.next_out = buffer; |
| 106 | stream.avail_out = maxsize; |
| 107 | |
| 108 | do { |
| 109 | result = deflate(&stream, Z_FINISH); |
| 110 | } while (result == Z_OK); |
| 111 | |
| 112 | if (result != Z_STREAM_END) { |
| 113 | free(buffer); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | deflateEnd(&stream); |
| 118 | *compressed_size = stream.total_out; |
| 119 | |
| 120 | return buffer; |
| 121 | } |
| 122 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 123 | static int write_zip_entry(struct archiver_args *args, |
| 124 | const unsigned char *sha1, const char *path, size_t pathlen, |
| 125 | unsigned int mode, void *buffer, unsigned long size) |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 126 | { |
| 127 | struct zip_local_header header; |
| 128 | struct zip_dir_header dirent; |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 129 | unsigned long attr2; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 130 | unsigned long compressed_size; |
| 131 | unsigned long uncompressed_size; |
| 132 | unsigned long crc; |
| 133 | unsigned long direntsize; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 134 | int method; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 135 | unsigned char *out; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 136 | void *deflated = NULL; |
| 137 | |
Rene Scharfe | 38f4d13 | 2006-11-18 13:06:56 +0100 | [diff] [blame] | 138 | crc = crc32(0, NULL, 0); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 139 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 140 | if (pathlen > 0xffff) { |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 141 | return error("path too long (%d chars, SHA1: %s): %s", |
| 142 | (int)pathlen, sha1_to_hex(sha1), path); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 145 | if (S_ISDIR(mode) || S_ISGITLINK(mode)) { |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 146 | method = 0; |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 147 | attr2 = 16; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 148 | out = NULL; |
| 149 | uncompressed_size = 0; |
| 150 | compressed_size = 0; |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 151 | } else if (S_ISREG(mode) || S_ISLNK(mode)) { |
| 152 | method = 0; |
Dmitry Potapov | 76bf8d0 | 2007-09-16 21:07:38 +0400 | [diff] [blame] | 153 | attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : |
| 154 | (mode & 0111) ? ((mode) << 16) : 0; |
René Scharfe | 3a176c6 | 2008-07-18 16:30:32 +0200 | [diff] [blame] | 155 | if (S_ISREG(mode) && args->compression_level != 0) |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 156 | method = 8; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 157 | crc = crc32(crc, buffer, size); |
| 158 | out = buffer; |
| 159 | uncompressed_size = size; |
| 160 | compressed_size = size; |
| 161 | } else { |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 162 | return error("unsupported file mode: 0%o (SHA1: %s)", mode, |
| 163 | sha1_to_hex(sha1)); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (method == 8) { |
René Scharfe | 3a176c6 | 2008-07-18 16:30:32 +0200 | [diff] [blame] | 167 | deflated = zlib_deflate(buffer, size, args->compression_level, |
| 168 | &compressed_size); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 169 | if (deflated && compressed_size - 6 < size) { |
| 170 | /* ZLIB --> raw compressed data (see RFC 1950) */ |
| 171 | /* CMF and FLG ... */ |
| 172 | out = (unsigned char *)deflated + 2; |
| 173 | compressed_size -= 6; /* ... and ADLER32 */ |
| 174 | } else { |
| 175 | method = 0; |
| 176 | compressed_size = size; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /* make sure we have enough free space in the dictionary */ |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 181 | direntsize = ZIP_DIR_HEADER_SIZE + pathlen; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 182 | while (zip_dir_size < zip_dir_offset + direntsize) { |
| 183 | zip_dir_size += ZIP_DIRECTORY_MIN_SIZE; |
| 184 | zip_dir = xrealloc(zip_dir, zip_dir_size); |
| 185 | } |
| 186 | |
| 187 | copy_le32(dirent.magic, 0x02014b50); |
Dmitry Potapov | 76bf8d0 | 2007-09-16 21:07:38 +0400 | [diff] [blame] | 188 | copy_le16(dirent.creator_version, |
| 189 | S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0); |
Rene Scharfe | cf72fb0 | 2006-10-07 01:47:24 +0200 | [diff] [blame] | 190 | copy_le16(dirent.version, 10); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 191 | copy_le16(dirent.flags, 0); |
| 192 | copy_le16(dirent.compression_method, method); |
| 193 | copy_le16(dirent.mtime, zip_time); |
| 194 | copy_le16(dirent.mdate, zip_date); |
| 195 | copy_le32(dirent.crc32, crc); |
| 196 | copy_le32(dirent.compressed_size, compressed_size); |
| 197 | copy_le32(dirent.size, uncompressed_size); |
| 198 | copy_le16(dirent.filename_length, pathlen); |
| 199 | copy_le16(dirent.extra_length, 0); |
| 200 | copy_le16(dirent.comment_length, 0); |
| 201 | copy_le16(dirent.disk, 0); |
| 202 | copy_le16(dirent.attr1, 0); |
Rene Scharfe | 62cdce1 | 2006-10-07 01:47:35 +0200 | [diff] [blame] | 203 | copy_le32(dirent.attr2, attr2); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 204 | copy_le32(dirent.offset, zip_offset); |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 205 | memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE); |
| 206 | zip_dir_offset += ZIP_DIR_HEADER_SIZE; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 207 | memcpy(zip_dir + zip_dir_offset, path, pathlen); |
| 208 | zip_dir_offset += pathlen; |
| 209 | zip_dir_entries++; |
| 210 | |
| 211 | copy_le32(header.magic, 0x04034b50); |
Rene Scharfe | cf72fb0 | 2006-10-07 01:47:24 +0200 | [diff] [blame] | 212 | copy_le16(header.version, 10); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 213 | copy_le16(header.flags, 0); |
| 214 | copy_le16(header.compression_method, method); |
| 215 | copy_le16(header.mtime, zip_time); |
| 216 | copy_le16(header.mdate, zip_date); |
| 217 | copy_le32(header.crc32, crc); |
| 218 | copy_le32(header.compressed_size, compressed_size); |
| 219 | copy_le32(header.size, uncompressed_size); |
| 220 | copy_le16(header.filename_length, pathlen); |
| 221 | copy_le16(header.extra_length, 0); |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 222 | write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE); |
| 223 | zip_offset += ZIP_LOCAL_HEADER_SIZE; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 224 | write_or_die(1, path, pathlen); |
| 225 | zip_offset += pathlen; |
| 226 | if (compressed_size > 0) { |
| 227 | write_or_die(1, out, compressed_size); |
| 228 | zip_offset += compressed_size; |
| 229 | } |
| 230 | |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 231 | free(deflated); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 232 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 233 | return 0; |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static void write_zip_trailer(const unsigned char *sha1) |
| 237 | { |
| 238 | struct zip_dir_trailer trailer; |
| 239 | |
| 240 | copy_le32(trailer.magic, 0x06054b50); |
| 241 | copy_le16(trailer.disk, 0); |
| 242 | copy_le16(trailer.directory_start_disk, 0); |
| 243 | copy_le16(trailer.entries_on_this_disk, zip_dir_entries); |
| 244 | copy_le16(trailer.entries, zip_dir_entries); |
| 245 | copy_le32(trailer.size, zip_dir_offset); |
| 246 | copy_le32(trailer.offset, zip_offset); |
| 247 | copy_le16(trailer.comment_length, sha1 ? 40 : 0); |
| 248 | |
| 249 | write_or_die(1, zip_dir, zip_dir_offset); |
René Scharfe | 0ea865c | 2006-11-23 23:02:37 +0100 | [diff] [blame] | 250 | write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE); |
Rene Scharfe | e4fbbfe | 2006-08-26 23:19:21 +0200 | [diff] [blame] | 251 | if (sha1) |
| 252 | write_or_die(1, sha1_to_hex(sha1), 40); |
| 253 | } |
| 254 | |
| 255 | static void dos_time(time_t *time, int *dos_date, int *dos_time) |
| 256 | { |
| 257 | struct tm *t = localtime(time); |
| 258 | |
| 259 | *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 + |
| 260 | (t->tm_year + 1900 - 1980) * 512; |
| 261 | *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048; |
| 262 | } |
| 263 | |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 264 | int write_zip_archive(struct archiver_args *args) |
| 265 | { |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 266 | int err; |
| 267 | |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 268 | dos_time(&args->time, &zip_date, &zip_time); |
| 269 | |
| 270 | zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE); |
| 271 | zip_dir_size = ZIP_DIRECTORY_MIN_SIZE; |
| 272 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 273 | err = write_archive_entries(args, write_zip_entry); |
| 274 | if (!err) |
| 275 | write_zip_trailer(args->commit_sha1); |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 276 | |
| 277 | free(zip_dir); |
| 278 | |
René Scharfe | 562e25a | 2008-07-14 21:22:24 +0200 | [diff] [blame] | 279 | return err; |
Franck Bui-Huu | ec06bff | 2006-09-07 15:12:04 +0200 | [diff] [blame] | 280 | } |