archive-zip: remove uncompressed_size
[git/git.git] / archive-zip.c
CommitLineData
e4fbbfe9
RS
1/*
2 * Copyright (c) 2006 Rene Scharfe
3 */
e4fbbfe9 4#include "cache.h"
ec06bff5 5#include "archive.h"
e4fbbfe9 6
e4fbbfe9
RS
7static int zip_date;
8static int zip_time;
9
10static unsigned char *zip_dir;
11static unsigned int zip_dir_size;
12
13static unsigned int zip_offset;
14static unsigned int zip_dir_offset;
15static unsigned int zip_dir_entries;
16
17#define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
18
19struct 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];
0ea865ce 31 unsigned char _end[1];
e4fbbfe9
RS
32};
33
34struct 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];
0ea865ce 52 unsigned char _end[1];
e4fbbfe9
RS
53};
54
55struct 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];
0ea865ce 64 unsigned char _end[1];
e4fbbfe9
RS
65};
66
0ea865ce
RS
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
e4fbbfe9
RS
76static void copy_le16(unsigned char *dest, unsigned int n)
77{
78 dest[0] = 0xff & n;
79 dest[1] = 0xff & (n >> 010);
80}
81
82static 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
90static void *zlib_deflate(void *data, unsigned long size,
3a176c6c 91 int compression_level, unsigned long *compressed_size)
e4fbbfe9 92{
ef49a7a0 93 git_zstream stream;
e4fbbfe9
RS
94 unsigned long maxsize;
95 void *buffer;
96 int result;
97
98 memset(&stream, 0, sizeof(stream));
55bb5c91 99 git_deflate_init(&stream, compression_level);
225a6f10 100 maxsize = git_deflate_bound(&stream, size);
e4fbbfe9
RS
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 {
55bb5c91 109 result = git_deflate(&stream, Z_FINISH);
e4fbbfe9
RS
110 } while (result == Z_OK);
111
112 if (result != Z_STREAM_END) {
113 free(buffer);
114 return NULL;
115 }
116
55bb5c91 117 git_deflate_end(&stream);
e4fbbfe9
RS
118 *compressed_size = stream.total_out;
119
120 return buffer;
121}
122
562e25ab 123static int write_zip_entry(struct archiver_args *args,
9cb513b7
NTND
124 const unsigned char *sha1,
125 const char *path, size_t pathlen,
126 unsigned int mode)
e4fbbfe9
RS
127{
128 struct zip_local_header header;
129 struct zip_dir_header dirent;
62cdce17 130 unsigned long attr2;
e4fbbfe9 131 unsigned long compressed_size;
e4fbbfe9
RS
132 unsigned long crc;
133 unsigned long direntsize;
e4fbbfe9 134 int method;
e4fbbfe9 135 unsigned char *out;
e4fbbfe9 136 void *deflated = NULL;
9cb513b7
NTND
137 void *buffer;
138 unsigned long size;
e4fbbfe9 139
38f4d138 140 crc = crc32(0, NULL, 0);
e4fbbfe9 141
e4fbbfe9 142 if (pathlen > 0xffff) {
562e25ab
RS
143 return error("path too long (%d chars, SHA1: %s): %s",
144 (int)pathlen, sha1_to_hex(sha1), path);
e4fbbfe9
RS
145 }
146
302b9282 147 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
e4fbbfe9 148 method = 0;
62cdce17 149 attr2 = 16;
e4fbbfe9 150 out = NULL;
60df6bd1 151 size = 0;
e4fbbfe9 152 compressed_size = 0;
9cb513b7
NTND
153 buffer = NULL;
154 size = 0;
62cdce17 155 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
9cb513b7
NTND
156 enum object_type type;
157 buffer = sha1_file_to_archive(args, path, sha1, mode, &type, &size);
158 if (!buffer)
159 return error("cannot read %s", sha1_to_hex(sha1));
160
62cdce17 161 method = 0;
76bf8d0e
DP
162 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
163 (mode & 0111) ? ((mode) << 16) : 0;
3a176c6c 164 if (S_ISREG(mode) && args->compression_level != 0)
62cdce17 165 method = 8;
e4fbbfe9
RS
166 crc = crc32(crc, buffer, size);
167 out = buffer;
e4fbbfe9
RS
168 compressed_size = size;
169 } else {
562e25ab
RS
170 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
171 sha1_to_hex(sha1));
e4fbbfe9
RS
172 }
173
174 if (method == 8) {
3a176c6c
RS
175 deflated = zlib_deflate(buffer, size, args->compression_level,
176 &compressed_size);
e4fbbfe9
RS
177 if (deflated && compressed_size - 6 < size) {
178 /* ZLIB --> raw compressed data (see RFC 1950) */
179 /* CMF and FLG ... */
180 out = (unsigned char *)deflated + 2;
181 compressed_size -= 6; /* ... and ADLER32 */
182 } else {
183 method = 0;
184 compressed_size = size;
185 }
186 }
187
188 /* make sure we have enough free space in the dictionary */
0ea865ce 189 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
e4fbbfe9
RS
190 while (zip_dir_size < zip_dir_offset + direntsize) {
191 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
192 zip_dir = xrealloc(zip_dir, zip_dir_size);
193 }
194
195 copy_le32(dirent.magic, 0x02014b50);
76bf8d0e
DP
196 copy_le16(dirent.creator_version,
197 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
cf72fb07 198 copy_le16(dirent.version, 10);
e4fbbfe9
RS
199 copy_le16(dirent.flags, 0);
200 copy_le16(dirent.compression_method, method);
201 copy_le16(dirent.mtime, zip_time);
202 copy_le16(dirent.mdate, zip_date);
203 copy_le32(dirent.crc32, crc);
204 copy_le32(dirent.compressed_size, compressed_size);
60df6bd1 205 copy_le32(dirent.size, size);
e4fbbfe9
RS
206 copy_le16(dirent.filename_length, pathlen);
207 copy_le16(dirent.extra_length, 0);
208 copy_le16(dirent.comment_length, 0);
209 copy_le16(dirent.disk, 0);
210 copy_le16(dirent.attr1, 0);
62cdce17 211 copy_le32(dirent.attr2, attr2);
e4fbbfe9 212 copy_le32(dirent.offset, zip_offset);
0ea865ce
RS
213 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
214 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
e4fbbfe9
RS
215 memcpy(zip_dir + zip_dir_offset, path, pathlen);
216 zip_dir_offset += pathlen;
217 zip_dir_entries++;
218
219 copy_le32(header.magic, 0x04034b50);
cf72fb07 220 copy_le16(header.version, 10);
e4fbbfe9
RS
221 copy_le16(header.flags, 0);
222 copy_le16(header.compression_method, method);
223 copy_le16(header.mtime, zip_time);
224 copy_le16(header.mdate, zip_date);
225 copy_le32(header.crc32, crc);
226 copy_le32(header.compressed_size, compressed_size);
60df6bd1 227 copy_le32(header.size, size);
e4fbbfe9
RS
228 copy_le16(header.filename_length, pathlen);
229 copy_le16(header.extra_length, 0);
0ea865ce
RS
230 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
231 zip_offset += ZIP_LOCAL_HEADER_SIZE;
e4fbbfe9
RS
232 write_or_die(1, path, pathlen);
233 zip_offset += pathlen;
234 if (compressed_size > 0) {
235 write_or_die(1, out, compressed_size);
236 zip_offset += compressed_size;
237 }
238
e4fbbfe9 239 free(deflated);
9cb513b7 240 free(buffer);
e4fbbfe9 241
562e25ab 242 return 0;
e4fbbfe9
RS
243}
244
245static void write_zip_trailer(const unsigned char *sha1)
246{
247 struct zip_dir_trailer trailer;
248
249 copy_le32(trailer.magic, 0x06054b50);
250 copy_le16(trailer.disk, 0);
251 copy_le16(trailer.directory_start_disk, 0);
252 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
253 copy_le16(trailer.entries, zip_dir_entries);
254 copy_le32(trailer.size, zip_dir_offset);
255 copy_le32(trailer.offset, zip_offset);
256 copy_le16(trailer.comment_length, sha1 ? 40 : 0);
257
258 write_or_die(1, zip_dir, zip_dir_offset);
0ea865ce 259 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
e4fbbfe9
RS
260 if (sha1)
261 write_or_die(1, sha1_to_hex(sha1), 40);
262}
263
264static void dos_time(time_t *time, int *dos_date, int *dos_time)
265{
266 struct tm *t = localtime(time);
267
268 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
269 (t->tm_year + 1900 - 1980) * 512;
270 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
271}
272
4d7c9898
JK
273static int write_zip_archive(const struct archiver *ar,
274 struct archiver_args *args)
ec06bff5 275{
562e25ab
RS
276 int err;
277
ec06bff5
FBH
278 dos_time(&args->time, &zip_date, &zip_time);
279
280 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
281 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
562e25ab
RS
282
283 err = write_archive_entries(args, write_zip_entry);
284 if (!err)
285 write_zip_trailer(args->commit_sha1);
ec06bff5
FBH
286
287 free(zip_dir);
288
562e25ab 289 return err;
ec06bff5 290}
13e0f88d
JK
291
292static struct archiver zip_archiver = {
293 "zip",
294 write_zip_archive,
7b97730b 295 ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
13e0f88d
JK
296};
297
298void init_zip_archiver(void)
299{
300 register_archiver(&zip_archiver);
301}