2 * Copyright (c) 2008 Anselm Strauss
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * Development supported by Google Summer of Code 2008.
31 __FBSDID("$FreeBSD$");
34 static const char file_name[] = "file";
35 static const char file_data1[] = {'1', '2', '3', '4', '5'};
36 static const char file_data2[] = {'6', '7', '8', '9', '0'};
37 static const int file_perm = 00644;
38 static const short file_uid = 10;
39 static const short file_gid = 20;
42 static const char folder_name[] = "folder/";
43 static const int folder_perm = 00755;
44 static const short folder_uid = 30;
45 static const short folder_gid = 40;
50 bitcrc32(unsigned long c, const void *_p, size_t s)
52 /* This is a drop-in replacement for crc32() from zlib.
53 * Libarchive should be able to correctly generate
54 * uncompressed zip archives (including correct CRCs) even
55 * when zlib is unavailable, and this function helps us verify
56 * that. Yes, this is very, very slow and unsuitable for
57 * production use, but it's correct, compact, and works well
58 * enough for this particular usage. Libarchive internally
59 * uses a much more efficient implementation. */
60 const unsigned char *p = _p;
68 for (bitctr = 8; bitctr > 0; --bitctr) {
69 if (c & 1) c = (c >> 1);
70 else c = (c >> 1) ^ 0xedb88320;
77 static void verify_write_uncompressed(struct archive *a)
79 struct archive_entry *entry;
84 assert((entry = archive_entry_new()) != NULL);
85 archive_entry_set_pathname(entry, file_name);
86 archive_entry_set_mode(entry, S_IFREG | 0644);
87 archive_entry_set_size(entry, sizeof(file_data1) + sizeof(file_data2));
88 archive_entry_set_uid(entry, file_uid);
89 archive_entry_set_gid(entry, file_gid);
90 archive_entry_set_mtime(entry, now, 0);
91 archive_entry_set_atime(entry, now + 3, 0);
92 assertEqualIntA(a, 0, archive_write_header(a, entry));
93 assertEqualIntA(a, sizeof(file_data1), archive_write_data(a, file_data1, sizeof(file_data1)));
94 assertEqualIntA(a, sizeof(file_data2), archive_write_data(a, file_data2, sizeof(file_data2)));
95 archive_entry_free(entry);
98 assert((entry = archive_entry_new()) != NULL);
99 archive_entry_set_pathname(entry, folder_name);
100 archive_entry_set_mode(entry, S_IFDIR | folder_perm);
101 archive_entry_set_size(entry, 0);
102 archive_entry_set_uid(entry, folder_uid);
103 archive_entry_set_gid(entry, folder_gid);
104 archive_entry_set_mtime(entry, now, 0);
105 archive_entry_set_ctime(entry, now + 5, 0);
106 assertEqualIntA(a, 0, archive_write_header(a, entry));
107 archive_entry_free(entry);
110 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
111 static int i2(const char *p) { return ((p[0] & 0xff) | ((p[1] & 0xff) << 8)); }
112 static int i4(const char *p) { return (i2(p) | (i2(p + 2) << 16)); }
114 static void verify_uncompressed_contents(const char *buff, size_t used)
120 struct tm *tm = localtime(&now);
122 /* p is the pointer to walk over the central directory,
123 * q walks over the local headers, the data and the data descriptors. */
124 const char *p, *q, *local_header, *extra_start;
126 /* Remember the end of the archive in memory. */
127 buffend = buff + used;
129 /* Verify "End of Central Directory" record. */
130 /* Get address of end-of-central-directory record. */
131 p = buffend - 22; /* Assumes there is no zip comment field. */
132 failure("End-of-central-directory begins with PK\\005\\006 signature");
133 assertEqualMem(p, "PK\005\006", 4);
134 failure("This must be disk 0");
135 assertEqualInt(i2(p + 4), 0);
136 failure("Central dir must start on disk 0");
137 assertEqualInt(i2(p + 6), 0);
138 failure("All central dir entries are on this disk");
139 assertEqualInt(i2(p + 8), i2(p + 10));
140 failure("CD start (%d) + CD length (%d) should == archive size - 22",
141 i4(p + 12), i4(p + 16));
142 assertEqualInt(i4(p + 12) + i4(p + 16), used - 22);
143 failure("no zip comment");
144 assertEqualInt(i2(p + 20), 0);
146 /* Get address of first entry in central directory. */
147 p = buff + i4(buffend - 6);
148 failure("Central file record at offset %d should begin with"
149 " PK\\001\\002 signature",
152 /* Verify file entry in central directory. */
153 assertEqualMem(p, "PK\001\002", 4); /* Signature */
154 assertEqualInt(i2(p + 4), 3 * 256 + 10); /* Version made by */
155 assertEqualInt(i2(p + 6), 10); /* Version needed to extract */
156 assertEqualInt(i2(p + 8), 8); /* Flags */
157 assertEqualInt(i2(p + 10), 0); /* Compression method */
158 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
159 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
160 crc = bitcrc32(0, file_data1, sizeof(file_data1));
161 crc = bitcrc32(crc, file_data2, sizeof(file_data2));
162 assertEqualInt(i4(p + 16), crc); /* CRC-32 */
163 assertEqualInt(i4(p + 20), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
164 assertEqualInt(i4(p + 24), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
165 assertEqualInt(i2(p + 28), strlen(file_name)); /* Pathname length */
166 assertEqualInt(i2(p + 30), 28); /* Extra field length */
167 assertEqualInt(i2(p + 32), 0); /* File comment length */
168 assertEqualInt(i2(p + 34), 0); /* Disk number start */
169 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
170 assertEqualInt(i4(p + 38) >> 16 & 01777, file_perm); /* External file attrs */
171 assertEqualInt(i4(p + 42), 0); /* Offset of local header */
172 assertEqualMem(p + 46, file_name, strlen(file_name)); /* Pathname */
173 p = p + 46 + strlen(file_name);
174 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
175 assertEqualInt(i2(p + 2), 9); /* 'UT' size */
176 assertEqualInt(p[4], 3); /* 'UT' flags */
177 assertEqualInt(i4(p + 5), now); /* 'UT' mtime */
178 assertEqualInt(i4(p + 9), now + 3); /* 'UT' atime */
179 p = p + 4 + i2(p + 2);
180 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
181 assertEqualInt(i2(p + 2), 11); /* 'ux' size */
183 p = p + 4 + i2(p + 2);
185 /* Verify local header of file entry. */
186 local_header = q = buff;
187 assertEqualMem(q, "PK\003\004", 4); /* Signature */
188 assertEqualInt(i2(q + 4), 10); /* Version needed to extract */
189 assertEqualInt(i2(q + 6), 8); /* Flags */
190 assertEqualInt(i2(q + 8), 0); /* Compression method */
191 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
192 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
193 assertEqualInt(i4(q + 14), 0); /* CRC-32 */
194 assertEqualInt(i4(q + 18), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
195 assertEqualInt(i4(q + 22), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
196 assertEqualInt(i2(q + 26), strlen(file_name)); /* Pathname length */
197 assertEqualInt(i2(q + 28), 41); /* Extra field length */
198 assertEqualMem(q + 30, file_name, strlen(file_name)); /* Pathname */
199 extra_start = q = q + 30 + strlen(file_name);
200 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
201 assertEqualInt(i2(q + 2), 9); /* 'UT' size */
202 assertEqualInt(q[4], 3); /* 'UT' flags */
203 assertEqualInt(i4(q + 5), now); /* 'UT' mtime */
204 assertEqualInt(i4(q + 9), now + 3); /* 'UT' atime */
205 q = q + 4 + i2(q + 2);
207 assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */
208 assertEqualInt(i2(q + 2), 11); /* 'ux' size */
209 assertEqualInt(q[4], 1); /* 'ux' version */
210 assertEqualInt(q[5], 4); /* 'ux' uid size */
211 assertEqualInt(i4(q + 6), file_uid); /* 'Ux' UID */
212 assertEqualInt(q[10], 4); /* 'ux' gid size */
213 assertEqualInt(i4(q + 11), file_gid); /* 'Ux' GID */
214 q = q + 4 + i2(q + 2);
216 assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */
217 assertEqualInt(i2(q + 2), 9); /* size */
218 assertEqualInt(q[4], 7); /* Bitmap of fields included. */
219 assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */
220 assertEqualInt(i2(q + 7), 0); /* internal file attributes */
221 assertEqualInt(i4(q + 9) >> 16 & 01777, file_perm); /* external file attributes */
222 q = q + 4 + i2(q + 2);
224 assert(q == extra_start + i2(local_header + 28));
225 q = extra_start + i2(local_header + 28);
227 /* Verify data of file entry. */
228 assertEqualMem(q, file_data1, sizeof(file_data1));
229 assertEqualMem(q + sizeof(file_data1), file_data2, sizeof(file_data2));
230 q = q + sizeof(file_data1) + sizeof(file_data2);
232 /* Verify data descriptor of file entry. */
233 assertEqualMem(q, "PK\007\010", 4); /* Signature */
234 assertEqualInt(i4(q + 4), crc); /* CRC-32 */
235 assertEqualInt(i4(q + 8), sizeof(file_data1) + sizeof(file_data2)); /* Compressed size */
236 assertEqualInt(i4(q + 12), sizeof(file_data1) + sizeof(file_data2)); /* Uncompressed size */
239 /* Verify folder entry in central directory. */
240 assertEqualMem(p, "PK\001\002", 4); /* Signature */
241 assertEqualInt(i2(p + 4), 3 * 256 + 20); /* Version made by */
242 assertEqualInt(i2(p + 6), 20); /* Version needed to extract */
243 assertEqualInt(i2(p + 8), 0); /* Flags */
244 assertEqualInt(i2(p + 10), 0); /* Compression method */
245 assertEqualInt(i2(p + 12), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
246 assertEqualInt(i2(p + 14), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
248 assertEqualInt(i4(p + 16), crc); /* CRC-32 */
249 assertEqualInt(i4(p + 20), 0); /* Compressed size */
250 assertEqualInt(i4(p + 24), 0); /* Uncompressed size */
251 assertEqualInt(i2(p + 28), strlen(folder_name)); /* Pathname length */
252 assertEqualInt(i2(p + 30), 28); /* Extra field length */
253 assertEqualInt(i2(p + 32), 0); /* File comment length */
254 assertEqualInt(i2(p + 34), 0); /* Disk number start */
255 assertEqualInt(i2(p + 36), 0); /* Internal file attrs */
256 assertEqualInt(i4(p + 38) >> 16 & 01777, folder_perm); /* External file attrs */
257 assertEqualInt(i4(p + 42), q - buff); /* Offset of local header */
258 assertEqualMem(p + 46, folder_name, strlen(folder_name)); /* Pathname */
259 p = p + 46 + strlen(folder_name);
260 assertEqualInt(i2(p), 0x5455); /* 'UT' extension header */
261 assertEqualInt(i2(p + 2), 9); /* 'UT' size */
262 assertEqualInt(p[4], 5); /* 'UT' flags */
263 assertEqualInt(i4(p + 5), now); /* 'UT' mtime */
264 assertEqualInt(i4(p + 9), now + 5); /* 'UT' atime */
265 p = p + 4 + i2(p + 2);
266 assertEqualInt(i2(p), 0x7875); /* 'ux' extension header */
267 assertEqualInt(i2(p + 2), 11); /* 'ux' size */
268 assertEqualInt(p[4], 1); /* 'ux' version */
269 assertEqualInt(p[5], 4); /* 'ux' uid size */
270 assertEqualInt(i4(p + 6), folder_uid); /* 'ux' UID */
271 assertEqualInt(p[10], 4); /* 'ux' gid size */
272 assertEqualInt(i4(p + 11), folder_gid); /* 'ux' GID */
273 /*p = p + 4 + i2(p + 2);*/
275 /* Verify local header of folder entry. */
277 assertEqualMem(q, "PK\003\004", 4); /* Signature */
278 assertEqualInt(i2(q + 4), 20); /* Version needed to extract */
279 assertEqualInt(i2(q + 6), 0); /* Flags */
280 assertEqualInt(i2(q + 8), 0); /* Compression method */
281 assertEqualInt(i2(q + 10), (tm->tm_hour * 2048) + (tm->tm_min * 32) + (tm->tm_sec / 2)); /* File time */
282 assertEqualInt(i2(q + 12), ((tm->tm_year - 80) * 512) + ((tm->tm_mon + 1) * 32) + tm->tm_mday); /* File date */
283 assertEqualInt(i4(q + 14), 0); /* CRC-32 */
284 assertEqualInt(i4(q + 18), 0); /* Compressed size */
285 assertEqualInt(i4(q + 22), 0); /* Uncompressed size */
286 assertEqualInt(i2(q + 26), strlen(folder_name)); /* Pathname length */
287 assertEqualInt(i2(q + 28), 41); /* Extra field length */
288 assertEqualMem(q + 30, folder_name, strlen(folder_name)); /* Pathname */
289 extra_start = q = q + 30 + strlen(folder_name);
290 assertEqualInt(i2(q), 0x5455); /* 'UT' extension header */
291 assertEqualInt(i2(q + 2), 9); /* 'UT' size */
292 assertEqualInt(q[4], 5); /* 'UT' flags */
293 assertEqualInt(i4(q + 5), now); /* 'UT' mtime */
294 assertEqualInt(i4(q + 9), now + 5); /* 'UT' atime */
295 q = q + 4 + i2(q + 2);
296 assertEqualInt(i2(q), 0x7875); /* 'ux' extension header */
297 assertEqualInt(i2(q + 2), 11); /* 'ux' size */
298 assertEqualInt(q[4], 1); /* 'ux' version */
299 assertEqualInt(q[5], 4); /* 'ux' uid size */
300 assertEqualInt(i4(q + 6), folder_uid); /* 'ux' UID */
301 assertEqualInt(q[10], 4); /* 'ux' gid size */
302 assertEqualInt(i4(q + 11), folder_gid); /* 'ux' GID */
303 q = q + 4 + i2(q + 2);
305 assertEqualInt(i2(q), 0x6c78); /* 'xl' experimental extension header */
306 assertEqualInt(i2(q + 2), 9); /* size */
307 assertEqualInt(q[4], 7); /* bitmap of fields */
308 assertEqualInt(i2(q + 5) >> 8, 3); /* system & version made by */
309 assertEqualInt(i2(q + 7), 0); /* internal file attributes */
310 assertEqualInt(i4(q + 9) >> 16 & 01777, folder_perm); /* external file attributes */
311 q = q + 4 + i2(q + 2);
313 assert(q == extra_start + i2(local_header + 28));
314 q = extra_start + i2(local_header + 28);
316 /* There should not be any data in the folder entry,
317 * so the first central directory entry should be next: */
318 assertEqualMem(q, "PK\001\002", 4); /* Signature */
321 DEFINE_TEST(test_write_format_zip_compression_store)
331 /* Create new ZIP archive in memory without padding. */
332 /* Use compression=store to disable compression. */
333 assert((a = archive_write_new()) != NULL);
334 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
335 assertEqualIntA(a, ARCHIVE_OK,
336 archive_write_set_options(a, "zip:compression=store"));
337 assertEqualIntA(a, ARCHIVE_OK,
338 archive_write_set_options(a, "zip:experimental"));
339 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
340 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
341 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
342 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
344 verify_write_uncompressed(a);
346 /* Close the archive . */
347 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
348 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
349 dumpfile("constructed.zip", buff, used);
351 verify_uncompressed_contents(buff, used);
353 /* Create new ZIP archive in memory without padding. */
354 /* Use compression-level=0 to disable compression. */
355 assert((a = archive_write_new()) != NULL);
356 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_format_zip(a));
357 assertEqualIntA(a, ARCHIVE_OK,
358 archive_write_set_options(a, "zip:compression-level=0"));
359 assertEqualIntA(a, ARCHIVE_OK,
360 archive_write_set_options(a, "zip:experimental"));
361 assertEqualIntA(a, ARCHIVE_OK, archive_write_add_filter_none(a));
362 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_per_block(a, 1));
363 assertEqualIntA(a, ARCHIVE_OK, archive_write_set_bytes_in_last_block(a, 1));
364 assertEqualIntA(a, ARCHIVE_OK, archive_write_open_memory(a, buff, sizeof(buff), &used));
366 verify_write_uncompressed(a);
368 /* Close the archive . */
369 assertEqualIntA(a, ARCHIVE_OK, archive_write_close(a));
370 assertEqualInt(ARCHIVE_OK, archive_write_free(a));
371 dumpfile("constructed.zip", buff, used);
373 verify_uncompressed_contents(buff, used);