archive: make zip compression level independent from core git
[git/git.git] / archive-zip.c
CommitLineData
e4fbbfe9
RS
1/*
2 * Copyright (c) 2006 Rene Scharfe
3 */
e4fbbfe9
RS
4#include "cache.h"
5#include "commit.h"
6#include "blob.h"
7#include "tree.h"
8#include "quote.h"
9#include "builtin.h"
ec06bff5 10#include "archive.h"
e4fbbfe9 11
e4fbbfe9
RS
12static int zip_date;
13static int zip_time;
14
15static unsigned char *zip_dir;
16static unsigned int zip_dir_size;
17
18static unsigned int zip_offset;
19static unsigned int zip_dir_offset;
20static unsigned int zip_dir_entries;
21
22#define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
23
24struct 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];
0ea865ce 36 unsigned char _end[1];
e4fbbfe9
RS
37};
38
39struct 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];
0ea865ce 57 unsigned char _end[1];
e4fbbfe9
RS
58};
59
60struct 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];
0ea865ce 69 unsigned char _end[1];
e4fbbfe9
RS
70};
71
0ea865ce
RS
72/*
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.
76 */
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)
80
e4fbbfe9
RS
81static void copy_le16(unsigned char *dest, unsigned int n)
82{
83 dest[0] = 0xff & n;
84 dest[1] = 0xff & (n >> 010);
85}
86
87static void copy_le32(unsigned char *dest, unsigned int n)
88{
89 dest[0] = 0xff & n;
90 dest[1] = 0xff & (n >> 010);
91 dest[2] = 0xff & (n >> 020);
92 dest[3] = 0xff & (n >> 030);
93}
94
95static void *zlib_deflate(void *data, unsigned long size,
3a176c6c 96 int compression_level, unsigned long *compressed_size)
e4fbbfe9
RS
97{
98 z_stream stream;
99 unsigned long maxsize;
100 void *buffer;
101 int result;
102
103 memset(&stream, 0, sizeof(stream));
3a176c6c 104 deflateInit(&stream, compression_level);
e4fbbfe9
RS
105 maxsize = deflateBound(&stream, size);
106 buffer = xmalloc(maxsize);
107
108 stream.next_in = data;
109 stream.avail_in = size;
110 stream.next_out = buffer;
111 stream.avail_out = maxsize;
112
113 do {
114 result = deflate(&stream, Z_FINISH);
115 } while (result == Z_OK);
116
117 if (result != Z_STREAM_END) {
118 free(buffer);
119 return NULL;
120 }
121
122 deflateEnd(&stream);
123 *compressed_size = stream.total_out;
124
125 return buffer;
126}
127
562e25ab
RS
128static 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)
e4fbbfe9
RS
131{
132 struct zip_local_header header;
133 struct zip_dir_header dirent;
62cdce17 134 unsigned long attr2;
e4fbbfe9
RS
135 unsigned long compressed_size;
136 unsigned long uncompressed_size;
137 unsigned long crc;
138 unsigned long direntsize;
e4fbbfe9 139 int method;
e4fbbfe9 140 unsigned char *out;
e4fbbfe9
RS
141 void *deflated = NULL;
142
38f4d138 143 crc = crc32(0, NULL, 0);
e4fbbfe9 144
e4fbbfe9 145 if (pathlen > 0xffff) {
562e25ab
RS
146 return error("path too long (%d chars, SHA1: %s): %s",
147 (int)pathlen, sha1_to_hex(sha1), path);
e4fbbfe9
RS
148 }
149
302b9282 150 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
e4fbbfe9 151 method = 0;
62cdce17 152 attr2 = 16;
e4fbbfe9
RS
153 out = NULL;
154 uncompressed_size = 0;
155 compressed_size = 0;
62cdce17
RS
156 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
157 method = 0;
76bf8d0e
DP
158 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
159 (mode & 0111) ? ((mode) << 16) : 0;
3a176c6c 160 if (S_ISREG(mode) && args->compression_level != 0)
62cdce17 161 method = 8;
e4fbbfe9
RS
162 crc = crc32(crc, buffer, size);
163 out = buffer;
164 uncompressed_size = size;
165 compressed_size = size;
166 } else {
562e25ab
RS
167 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
168 sha1_to_hex(sha1));
e4fbbfe9
RS
169 }
170
171 if (method == 8) {
3a176c6c
RS
172 deflated = zlib_deflate(buffer, size, args->compression_level,
173 &compressed_size);
e4fbbfe9
RS
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 */
179 } else {
180 method = 0;
181 compressed_size = size;
182 }
183 }
184
185 /* make sure we have enough free space in the dictionary */
0ea865ce 186 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
e4fbbfe9
RS
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);
190 }
191
192 copy_le32(dirent.magic, 0x02014b50);
76bf8d0e
DP
193 copy_le16(dirent.creator_version,
194 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
cf72fb07 195 copy_le16(dirent.version, 10);
e4fbbfe9
RS
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);
62cdce17 208 copy_le32(dirent.attr2, attr2);
e4fbbfe9 209 copy_le32(dirent.offset, zip_offset);
0ea865ce
RS
210 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
211 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
e4fbbfe9
RS
212 memcpy(zip_dir + zip_dir_offset, path, pathlen);
213 zip_dir_offset += pathlen;
214 zip_dir_entries++;
215
216 copy_le32(header.magic, 0x04034b50);
cf72fb07 217 copy_le16(header.version, 10);
e4fbbfe9
RS
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);
0ea865ce
RS
227 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
228 zip_offset += ZIP_LOCAL_HEADER_SIZE;
e4fbbfe9
RS
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;
234 }
235
e4fbbfe9 236 free(deflated);
e4fbbfe9 237
562e25ab 238 return 0;
e4fbbfe9
RS
239}
240
241static void write_zip_trailer(const unsigned char *sha1)
242{
243 struct zip_dir_trailer trailer;
244
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);
253
254 write_or_die(1, zip_dir, zip_dir_offset);
0ea865ce 255 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
e4fbbfe9
RS
256 if (sha1)
257 write_or_die(1, sha1_to_hex(sha1), 40);
258}
259
260static void dos_time(time_t *time, int *dos_date, int *dos_time)
261{
262 struct tm *t = localtime(time);
263
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;
267}
268
ec06bff5
FBH
269int write_zip_archive(struct archiver_args *args)
270{
562e25ab
RS
271 int err;
272
ec06bff5
FBH
273 dos_time(&args->time, &zip_date, &zip_time);
274
275 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
276 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
562e25ab
RS
277
278 err = write_archive_entries(args, write_zip_entry);
279 if (!err)
280 write_zip_trailer(args->commit_sha1);
ec06bff5
FBH
281
282 free(zip_dir);
283
562e25ab 284 return err;
ec06bff5 285}