2 * Copyright (c) 2003-2008 Tim Kientzle
3 * Copyright (c) 2008 Anselm Strauss
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Development supported by Google Summer of Code 2008.
32 __FBSDID("$FreeBSD: head/lib/libarchive/test/test_write_format_zip.c 201247 2009-12-30 05:59:21Z kientzle $");
35 * Detailed byte-for-byte verification of the format of a zip archive
36 * with a single file written to it that uses Zip64 extensions.
40 bitcrc32(unsigned long c
, void *_p
, size_t s
)
42 /* This is a drop-in replacement for crc32() from zlib.
43 * Libarchive should be able to correctly generate
44 * uncompressed zip archives (including correct CRCs) even
45 * when zlib is unavailable, and this function helps us verify
46 * that. Yes, this is very, very slow and unsuitable for
47 * production use, but it's correct, compact, and works well
48 * enough for this particular usage. Libarchive internally
49 * uses a much more efficient implementation. */
50 const unsigned char *p
= _p
;
58 for (bitctr
= 8; bitctr
> 0; --bitctr
) {
59 if (c
& 1) c
= (c
>> 1);
60 else c
= (c
>> 1) ^ 0xedb88320;
67 /* Quick and dirty: Read 2-byte and 4-byte integers from Zip file. */
68 static unsigned i2(const unsigned char *p
) { return ((p
[0] & 0xff) | ((p
[1] & 0xff) << 8)); }
69 static unsigned i4(const unsigned char *p
) { return (i2(p
) | (i2(p
+ 2) << 16)); }
70 /* We're only working with small values here; ignore the 4 high bytes. */
71 static unsigned i8(const unsigned char *p
) { return (i4(p
)); }
73 DEFINE_TEST(test_write_format_zip_file_zip64
)
76 struct archive_entry
*ae
;
77 time_t t
= 1234567890;
79 #if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S)
82 size_t used
, buffsize
= 1000000;
84 int file_perm
= 00644;
86 int zip_compression
= 8;
87 short file_uid
= 10, file_gid
= 20;
88 unsigned char *buff
, *buffend
, *p
;
89 unsigned char *central_header
, *local_header
, *eocd
, *eocd_record
;
90 unsigned char *extension_start
, *extension_end
;
91 char file_data
[] = {'1', '2', '3', '4', '5', '6', '7', '8'};
92 const char *file_name
= "file";
98 #if defined(HAVE__LOCALTIME64_S)
99 tm
= _localtime64_s(&tmbuf
, &t
) ? NULL
: &tmbuf
;
100 #elif defined(HAVE_LOCALTIME_R)
101 tm
= localtime_r(&t
, &tmbuf
);
105 buff
= malloc(buffsize
);
107 /* Create a new archive in memory. */
108 assert((a
= archive_write_new()) != NULL
);
109 assertEqualIntA(a
, ARCHIVE_OK
, archive_write_set_format_zip(a
));
110 assertEqualIntA(a
, ARCHIVE_OK
,
111 archive_write_set_options(a
, "zip:zip64"));
112 assertEqualIntA(a
, ARCHIVE_OK
,
113 archive_write_set_options(a
, "zip:experimental"));
114 assertEqualIntA(a
, ARCHIVE_OK
,
115 archive_write_open_memory(a
, buff
, buffsize
, &used
));
117 assert((ae
= archive_entry_new()) != NULL
);
118 archive_entry_copy_pathname(ae
, file_name
);
119 archive_entry_set_mode(ae
, AE_IFREG
| file_perm
);
120 archive_entry_set_size(ae
, sizeof(file_data
));
121 archive_entry_set_uid(ae
, file_uid
);
122 archive_entry_set_gid(ae
, file_gid
);
123 archive_entry_set_mtime(ae
, t
, 0);
124 assertEqualInt(0, archive_write_header(a
, ae
));
125 archive_entry_free(ae
);
126 assertEqualInt(8, archive_write_data(a
, file_data
, sizeof(file_data
)));
127 assertEqualIntA(a
, ARCHIVE_OK
, archive_write_close(a
));
128 assertEqualInt(ARCHIVE_OK
, archive_write_free(a
));
129 buffend
= buff
+ used
;
130 dumpfile("constructed.zip", buff
, used
);
132 /* Verify "End of Central Directory" record. */
133 /* Get address of end-of-central-directory record. */
134 eocd_record
= p
= buffend
- 22; /* Assumes there is no zip comment field. */
135 failure("End-of-central-directory begins with PK\\005\\006 signature");
136 assertEqualMem(p
, "PK\005\006", 4);
137 failure("This must be disk 0");
138 assertEqualInt(i2(p
+ 4), 0);
139 failure("Central dir must start on disk 0");
140 assertEqualInt(i2(p
+ 6), 0);
141 failure("All central dir entries are on this disk");
142 assertEqualInt(i2(p
+ 8), i2(p
+ 10));
143 eocd
= buff
+ i4(p
+ 12) + i4(p
+ 16);
144 failure("no zip comment");
145 assertEqualInt(i2(p
+ 20), 0);
147 /* Get address of first entry in central directory. */
148 central_header
= p
= buff
+ i4(buffend
- 6);
149 failure("Central file record at offset %d should begin with"
150 " PK\\001\\002 signature",
153 /* Verify file entry in central directory. */
154 assertEqualMem(p
, "PK\001\002", 4); /* Signature */
155 assertEqualInt(i2(p
+ 4), 3 * 256 + zip_version
); /* Version made by */
156 assertEqualInt(i2(p
+ 6), zip_version
); /* Version needed to extract */
157 assertEqualInt(i2(p
+ 8), 8); /* Flags */
158 assertEqualInt(i2(p
+ 10), zip_compression
); /* Compression method */
159 assertEqualInt(i2(p
+ 12), (tm
->tm_hour
* 2048) + (tm
->tm_min
* 32) + (tm
->tm_sec
/ 2)); /* File time */
160 assertEqualInt(i2(p
+ 14), ((tm
->tm_year
- 80) * 512) + ((tm
->tm_mon
+ 1) * 32) + tm
->tm_mday
); /* File date */
161 crc
= bitcrc32(0, file_data
, sizeof(file_data
));
162 assertEqualInt(i4(p
+ 16), crc
); /* CRC-32 */
163 /* assertEqualInt(i4(p + 20), sizeof(file_data)); */ /* Compressed size */
164 assertEqualInt(i4(p
+ 24), sizeof(file_data
)); /* Uncompressed size */
165 assertEqualInt(i2(p
+ 28), strlen(file_name
)); /* Pathname length */
166 /* assertEqualInt(i2(p + 30), 28); */ /* Extra field length: See below */
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
= extension_start
= central_header
+ 46 + strlen(file_name
);
174 extension_end
= extension_start
+ i2(central_header
+ 30);
176 assertEqualInt(i2(p
), 0x5455); /* 'UT' extension header */
177 assertEqualInt(i2(p
+ 2), 5); /* 'UT' size */
178 assertEqualInt(p
[4], 1); /* 'UT' flags */
179 assertEqualInt(i4(p
+ 5), t
); /* 'UT' mtime */
182 assertEqualInt(i2(p
), 0x7875); /* 'ux' extension header */
183 assertEqualInt(i2(p
+ 2), 11); /* 'ux' size */
184 /* TODO: verify 'ux' contents */
187 /* Note: We don't expect to see zip64 extension in the central
188 * directory, since the writer knows the actual full size by
189 * the time it is ready to write the central directory and has
190 * no reason to insert it then. Info-Zip seems to do the same
193 /* Just in case: Report any extra extensions. */
194 while (p
< extension_end
) {
195 failure("Unexpected extension 0x%04X", i2(p
));
200 /* Should have run exactly to end of extra data. */
201 assert(p
== extension_end
);
205 /* After Central dir, we find Zip64 eocd and Zip64 eocd locator. */
206 assertEqualMem(p
, "PK\006\006", 4); /* Zip64 eocd */
207 assertEqualInt(i8(p
+ 4), 44); /* We're using v1 Zip64 eocd */
208 assertEqualInt(i2(p
+ 12), 45); /* Written by Version 4.5 */
209 assertEqualInt(i2(p
+ 14), 45); /* Needs version 4.5 to extract */
210 assertEqualInt(i4(p
+ 16), 0); /* This is disk #0 */
211 assertEqualInt(i4(p
+ 20), 0); /* Dir starts on disk #0 */
212 assertEqualInt(i8(p
+ 24), 1); /* 1 entry on this disk */
213 assertEqualInt(i8(p
+ 32), 1); /* 1 entry total */
214 assertEqualInt(i8(p
+ 40), eocd
- central_header
); /* size of cd */
215 assertEqualInt(i8(p
+ 48), central_header
- buff
); /* start of cd */
218 assertEqualMem(p
, "PK\006\007", 4); /* Zip64 eocd locator */
219 assertEqualInt(i4(p
+ 4), 0); /* Zip64 eocd is on disk #0 */
220 assertEqualInt(i8(p
+ 8), eocd
- buff
); /* Offset of Zip64 eocd */
221 assertEqualInt(i4(p
+ 16), 1); /* 1 disk */
224 /* Regular EOCD immediately follows Zip64 records. */
225 assert(p
== eocd_record
);
227 /* Verify local header of file entry. */
228 p
= local_header
= buff
;
229 assertEqualMem(p
, "PK\003\004", 4); /* Signature */
230 assertEqualInt(i2(p
+ 4), zip_version
); /* Version needed to extract */
231 assertEqualInt(i2(p
+ 6), 8); /* Flags */
232 assertEqualInt(i2(p
+ 8), zip_compression
); /* Compression method */
233 assertEqualInt(i2(p
+ 10), (tm
->tm_hour
* 2048) + (tm
->tm_min
* 32) + (tm
->tm_sec
/ 2)); /* File time */
234 assertEqualInt(i2(p
+ 12), ((tm
->tm_year
- 80) * 512) + ((tm
->tm_mon
+ 1) * 32) + tm
->tm_mday
); /* File date */
235 assertEqualInt(i4(p
+ 14), 0); /* CRC-32 */
236 /* assertEqualInt(i4(p + 18), sizeof(file_data)); */ /* Compressed size */
237 /* assertEqualInt(i4(p + 22), sizeof(file_data)); */ /* Uncompressed size not stored because we're using length-at-end. */
238 assertEqualInt(i2(p
+ 26), strlen(file_name
)); /* Pathname length */
239 assertEqualInt(i2(p
+ 28), 57); /* Extra field length */
240 assertEqualMem(p
+ 30, file_name
, strlen(file_name
)); /* Pathname */
241 p
= extension_start
= local_header
+ 30 + strlen(file_name
);
242 extension_end
= extension_start
+ i2(local_header
+ 28);
244 assertEqualInt(i2(p
), 0x5455); /* 'UT' extension header */
245 assertEqualInt(i2(p
+ 2), 5); /* 'UT' size */
246 assertEqualInt(p
[4], 1); /* 'UT' flags */
247 assertEqualInt(i4(p
+ 5), t
); /* 'UT' mtime */
250 assertEqualInt(i2(p
), 0x7875); /* 'ux' extension header */
251 assertEqualInt(i2(p
+ 2), 11); /* 'ux' size */
252 assertEqualInt(p
[4], 1); /* 'ux' version */
253 assertEqualInt(p
[5], 4); /* 'ux' uid size */
254 assertEqualInt(i4(p
+ 6), file_uid
); /* 'Ux' UID */
255 assertEqualInt(p
[10], 4); /* 'ux' gid size */
256 assertEqualInt(i4(p
+ 11), file_gid
); /* 'Ux' GID */
259 assertEqualInt(i2(p
), 0x0001); /* Zip64 extension header */
260 assertEqualInt(i2(p
+ 2), 16); /* size */
261 assertEqualInt(i8(p
+ 4), 8); /* uncompressed file size */
262 /* compressed file size we can't verify here */
265 assertEqualInt(i2(p
), 0x6c78); /* 'xl' experimental extension header */
266 assertEqualInt(i2(p
+ 2), 9); /* size */
267 assertEqualInt(p
[4], 7); /* bitmap of included fields */
268 assertEqualInt(i2(p
+ 5) >> 8, 3); /* system & version made by */
269 assertEqualInt(i2(p
+ 7), 0); /* internal file attributes */
270 assertEqualInt(i4(p
+ 9) >> 16 & 01777, file_perm
); /* external file attributes */
273 /* Just in case: Report any extra extensions. */
274 while (p
< extension_end
) {
275 failure("Unexpected extension 0x%04X", i2(p
));
280 /* Should have run exactly to end of extra data. */
281 assert(p
== extension_end
);
283 /* Data descriptor should follow compressed data. */
284 while (p
< central_header
&& memcmp(p
, "PK\007\010", 4) != 0)
286 assertEqualMem(p
, "PK\007\010", 4);
287 assertEqualInt(i4(p
+ 4), crc
); /* CRC-32 */
288 /* assertEqualInt(i8(p + 8), ???); */ /* compressed size */
289 assertEqualInt(i8(p
+ 16), sizeof(file_data
)); /* uncompressed size */
291 /* Central directory should immediately follow the only entry. */
292 assert(p
+ 24 == central_header
);