cvsserver: Add cvs co -c support
[git/dscho.git] / archive-zip.c
blobd56e5cfc1e4047a2e86a2aeacaafc8fb2931179e
1 /*
2 * Copyright (c) 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "commit.h"
6 #include "blob.h"
7 #include "tree.h"
8 #include "quote.h"
9 #include "builtin.h"
10 #include "archive.h"
12 static int zip_date;
13 static int zip_time;
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)
83 dest[0] = 0xff & n;
84 dest[1] = 0xff & (n >> 010);
87 static void copy_le32(unsigned char *dest, unsigned int n)
89 dest[0] = 0xff & 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 unsigned long *compressed_size)
98 z_stream stream;
99 unsigned long maxsize;
100 void *buffer;
101 int result;
103 memset(&stream, 0, sizeof(stream));
104 deflateInit(&stream, zlib_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;
113 do {
114 result = deflate(&stream, Z_FINISH);
115 } while (result == Z_OK);
117 if (result != Z_STREAM_END) {
118 free(buffer);
119 return NULL;
122 deflateEnd(&stream);
123 *compressed_size = stream.total_out;
125 return buffer;
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;
134 unsigned long attr2;
135 unsigned long compressed_size;
136 unsigned long uncompressed_size;
137 unsigned long crc;
138 unsigned long direntsize;
139 int method;
140 unsigned char *out;
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)) {
151 method = 0;
152 attr2 = 16;
153 out = NULL;
154 uncompressed_size = 0;
155 compressed_size = 0;
156 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
157 method = 0;
158 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
159 (mode & 0111) ? ((mode) << 16) : 0;
160 if (S_ISREG(mode) && zlib_compression_level != 0)
161 method = 8;
162 crc = crc32(crc, buffer, size);
163 out = buffer;
164 uncompressed_size = size;
165 compressed_size = size;
166 } else {
167 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
168 sha1_to_hex(sha1));
171 if (method == 8) {
172 deflated = zlib_deflate(buffer, size, &compressed_size);
173 if (deflated && compressed_size - 6 < size) {
174 /* ZLIB --> raw compressed data (see RFC 1950) */
175 /* CMF and FLG ... */
176 out = (unsigned char *)deflated + 2;
177 compressed_size -= 6; /* ... and ADLER32 */
178 } else {
179 method = 0;
180 compressed_size = size;
184 /* make sure we have enough free space in the dictionary */
185 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
186 while (zip_dir_size < zip_dir_offset + direntsize) {
187 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
188 zip_dir = xrealloc(zip_dir, zip_dir_size);
191 copy_le32(dirent.magic, 0x02014b50);
192 copy_le16(dirent.creator_version,
193 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
194 copy_le16(dirent.version, 10);
195 copy_le16(dirent.flags, 0);
196 copy_le16(dirent.compression_method, method);
197 copy_le16(dirent.mtime, zip_time);
198 copy_le16(dirent.mdate, zip_date);
199 copy_le32(dirent.crc32, crc);
200 copy_le32(dirent.compressed_size, compressed_size);
201 copy_le32(dirent.size, uncompressed_size);
202 copy_le16(dirent.filename_length, pathlen);
203 copy_le16(dirent.extra_length, 0);
204 copy_le16(dirent.comment_length, 0);
205 copy_le16(dirent.disk, 0);
206 copy_le16(dirent.attr1, 0);
207 copy_le32(dirent.attr2, attr2);
208 copy_le32(dirent.offset, zip_offset);
209 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
210 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
211 memcpy(zip_dir + zip_dir_offset, path, pathlen);
212 zip_dir_offset += pathlen;
213 zip_dir_entries++;
215 copy_le32(header.magic, 0x04034b50);
216 copy_le16(header.version, 10);
217 copy_le16(header.flags, 0);
218 copy_le16(header.compression_method, method);
219 copy_le16(header.mtime, zip_time);
220 copy_le16(header.mdate, zip_date);
221 copy_le32(header.crc32, crc);
222 copy_le32(header.compressed_size, compressed_size);
223 copy_le32(header.size, uncompressed_size);
224 copy_le16(header.filename_length, pathlen);
225 copy_le16(header.extra_length, 0);
226 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
227 zip_offset += ZIP_LOCAL_HEADER_SIZE;
228 write_or_die(1, path, pathlen);
229 zip_offset += pathlen;
230 if (compressed_size > 0) {
231 write_or_die(1, out, compressed_size);
232 zip_offset += compressed_size;
235 free(deflated);
237 return 0;
240 static void write_zip_trailer(const unsigned char *sha1)
242 struct zip_dir_trailer trailer;
244 copy_le32(trailer.magic, 0x06054b50);
245 copy_le16(trailer.disk, 0);
246 copy_le16(trailer.directory_start_disk, 0);
247 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
248 copy_le16(trailer.entries, zip_dir_entries);
249 copy_le32(trailer.size, zip_dir_offset);
250 copy_le32(trailer.offset, zip_offset);
251 copy_le16(trailer.comment_length, sha1 ? 40 : 0);
253 write_or_die(1, zip_dir, zip_dir_offset);
254 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
255 if (sha1)
256 write_or_die(1, sha1_to_hex(sha1), 40);
259 static void dos_time(time_t *time, int *dos_date, int *dos_time)
261 struct tm *t = localtime(time);
263 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
264 (t->tm_year + 1900 - 1980) * 512;
265 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
268 int write_zip_archive(struct archiver_args *args)
270 int err;
272 dos_time(&args->time, &zip_date, &zip_time);
274 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
275 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
277 err = write_archive_entries(args, write_zip_entry);
278 if (!err)
279 write_zip_trailer(args->commit_sha1);
281 free(zip_dir);
283 return err;