xref: /dragonfly/contrib/file/src/cdf.c (revision 38c2ea22)
1 /*-
2  * Copyright (c) 2008 Christos Zoulas
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 /*
27  * Parse Composite Document Files, the format used in Microsoft Office
28  * document files before they switched to zipped XML.
29  * Info from: http://sc.openoffice.org/compdocfileformat.pdf
30  *
31  * N.B. This is the "Composite Document File" format, and not the
32  * "Compound Document Format", nor the "Channel Definition Format".
33  */
34 
35 #include "file.h"
36 
37 #ifndef lint
38 FILE_RCSID("@(#)$File: cdf.c,v 1.46 2011/09/16 21:23:59 christos Exp $")
39 #endif
40 
41 #include <assert.h>
42 #ifdef CDF_DEBUG
43 #include <err.h>
44 #endif
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 #include <time.h>
49 #include <ctype.h>
50 #ifdef HAVE_LIMITS_H
51 #include <limits.h>
52 #endif
53 
54 #ifndef EFTYPE
55 #define EFTYPE EINVAL
56 #endif
57 
58 #include "cdf.h"
59 
60 #ifdef CDF_DEBUG
61 #define DPRINTF(a) printf a, fflush(stdout)
62 #else
63 #define DPRINTF(a)
64 #endif
65 
66 static union {
67 	char s[4];
68 	uint32_t u;
69 } cdf_bo;
70 
71 #define NEED_SWAP	(cdf_bo.u == (uint32_t)0x01020304)
72 
73 #define CDF_TOLE8(x)	((uint64_t)(NEED_SWAP ? _cdf_tole8(x) : (uint64_t)(x)))
74 #define CDF_TOLE4(x)	((uint32_t)(NEED_SWAP ? _cdf_tole4(x) : (uint32_t)(x)))
75 #define CDF_TOLE2(x)	((uint16_t)(NEED_SWAP ? _cdf_tole2(x) : (uint16_t)(x)))
76 #define CDF_GETUINT32(x, y)	cdf_getuint32(x, y)
77 
78 /*
79  * swap a short
80  */
81 static uint16_t
82 _cdf_tole2(uint16_t sv)
83 {
84 	uint16_t rv;
85 	uint8_t *s = (uint8_t *)(void *)&sv;
86 	uint8_t *d = (uint8_t *)(void *)&rv;
87 	d[0] = s[1];
88 	d[1] = s[0];
89 	return rv;
90 }
91 
92 /*
93  * swap an int
94  */
95 static uint32_t
96 _cdf_tole4(uint32_t sv)
97 {
98 	uint32_t rv;
99 	uint8_t *s = (uint8_t *)(void *)&sv;
100 	uint8_t *d = (uint8_t *)(void *)&rv;
101 	d[0] = s[3];
102 	d[1] = s[2];
103 	d[2] = s[1];
104 	d[3] = s[0];
105 	return rv;
106 }
107 
108 /*
109  * swap a quad
110  */
111 static uint64_t
112 _cdf_tole8(uint64_t sv)
113 {
114 	uint64_t rv;
115 	uint8_t *s = (uint8_t *)(void *)&sv;
116 	uint8_t *d = (uint8_t *)(void *)&rv;
117 	d[0] = s[7];
118 	d[1] = s[6];
119 	d[2] = s[5];
120 	d[3] = s[4];
121 	d[4] = s[3];
122 	d[5] = s[2];
123 	d[6] = s[1];
124 	d[7] = s[0];
125 	return rv;
126 }
127 
128 /*
129  * grab a uint32_t from a possibly unaligned address, and return it in
130  * the native host order.
131  */
132 static uint32_t
133 cdf_getuint32(const uint8_t *p, size_t offs)
134 {
135 	uint32_t rv;
136 	(void)memcpy(&rv, p + offs * sizeof(uint32_t), sizeof(rv));
137 	return CDF_TOLE4(rv);
138 }
139 
140 #define CDF_UNPACK(a)	\
141     (void)memcpy(&(a), &buf[len], sizeof(a)), len += sizeof(a)
142 #define CDF_UNPACKA(a)	\
143     (void)memcpy((a), &buf[len], sizeof(a)), len += sizeof(a)
144 
145 uint16_t
146 cdf_tole2(uint16_t sv)
147 {
148 	return CDF_TOLE2(sv);
149 }
150 
151 uint32_t
152 cdf_tole4(uint32_t sv)
153 {
154 	return CDF_TOLE4(sv);
155 }
156 
157 uint64_t
158 cdf_tole8(uint64_t sv)
159 {
160 	return CDF_TOLE8(sv);
161 }
162 
163 void
164 cdf_swap_header(cdf_header_t *h)
165 {
166 	size_t i;
167 
168 	h->h_magic = CDF_TOLE8(h->h_magic);
169 	h->h_uuid[0] = CDF_TOLE8(h->h_uuid[0]);
170 	h->h_uuid[1] = CDF_TOLE8(h->h_uuid[1]);
171 	h->h_revision = CDF_TOLE2(h->h_revision);
172 	h->h_version = CDF_TOLE2(h->h_version);
173 	h->h_byte_order = CDF_TOLE2(h->h_byte_order);
174 	h->h_sec_size_p2 = CDF_TOLE2(h->h_sec_size_p2);
175 	h->h_short_sec_size_p2 = CDF_TOLE2(h->h_short_sec_size_p2);
176 	h->h_num_sectors_in_sat = CDF_TOLE4(h->h_num_sectors_in_sat);
177 	h->h_secid_first_directory = CDF_TOLE4(h->h_secid_first_directory);
178 	h->h_min_size_standard_stream =
179 	    CDF_TOLE4(h->h_min_size_standard_stream);
180 	h->h_secid_first_sector_in_short_sat =
181 	    CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_short_sat);
182 	h->h_num_sectors_in_short_sat =
183 	    CDF_TOLE4(h->h_num_sectors_in_short_sat);
184 	h->h_secid_first_sector_in_master_sat =
185 	    CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_master_sat);
186 	h->h_num_sectors_in_master_sat =
187 	    CDF_TOLE4(h->h_num_sectors_in_master_sat);
188 	for (i = 0; i < __arraycount(h->h_master_sat); i++)
189 		h->h_master_sat[i] = CDF_TOLE4((uint32_t)h->h_master_sat[i]);
190 }
191 
192 void
193 cdf_unpack_header(cdf_header_t *h, char *buf)
194 {
195 	size_t i;
196 	size_t len = 0;
197 
198 	CDF_UNPACK(h->h_magic);
199 	CDF_UNPACKA(h->h_uuid);
200 	CDF_UNPACK(h->h_revision);
201 	CDF_UNPACK(h->h_version);
202 	CDF_UNPACK(h->h_byte_order);
203 	CDF_UNPACK(h->h_sec_size_p2);
204 	CDF_UNPACK(h->h_short_sec_size_p2);
205 	CDF_UNPACKA(h->h_unused0);
206 	CDF_UNPACK(h->h_num_sectors_in_sat);
207 	CDF_UNPACK(h->h_secid_first_directory);
208 	CDF_UNPACKA(h->h_unused1);
209 	CDF_UNPACK(h->h_min_size_standard_stream);
210 	CDF_UNPACK(h->h_secid_first_sector_in_short_sat);
211 	CDF_UNPACK(h->h_num_sectors_in_short_sat);
212 	CDF_UNPACK(h->h_secid_first_sector_in_master_sat);
213 	CDF_UNPACK(h->h_num_sectors_in_master_sat);
214 	for (i = 0; i < __arraycount(h->h_master_sat); i++)
215 		CDF_UNPACK(h->h_master_sat[i]);
216 }
217 
218 void
219 cdf_swap_dir(cdf_directory_t *d)
220 {
221 	d->d_namelen = CDF_TOLE2(d->d_namelen);
222 	d->d_left_child = CDF_TOLE4((uint32_t)d->d_left_child);
223 	d->d_right_child = CDF_TOLE4((uint32_t)d->d_right_child);
224 	d->d_storage = CDF_TOLE4((uint32_t)d->d_storage);
225 	d->d_storage_uuid[0] = CDF_TOLE8(d->d_storage_uuid[0]);
226 	d->d_storage_uuid[1] = CDF_TOLE8(d->d_storage_uuid[1]);
227 	d->d_flags = CDF_TOLE4(d->d_flags);
228 	d->d_created = CDF_TOLE8((uint64_t)d->d_created);
229 	d->d_modified = CDF_TOLE8((uint64_t)d->d_modified);
230 	d->d_stream_first_sector = CDF_TOLE4((uint32_t)d->d_stream_first_sector);
231 	d->d_size = CDF_TOLE4(d->d_size);
232 }
233 
234 void
235 cdf_swap_class(cdf_classid_t *d)
236 {
237 	d->cl_dword = CDF_TOLE4(d->cl_dword);
238 	d->cl_word[0] = CDF_TOLE2(d->cl_word[0]);
239 	d->cl_word[1] = CDF_TOLE2(d->cl_word[1]);
240 }
241 
242 void
243 cdf_unpack_dir(cdf_directory_t *d, char *buf)
244 {
245 	size_t len = 0;
246 
247 	CDF_UNPACKA(d->d_name);
248 	CDF_UNPACK(d->d_namelen);
249 	CDF_UNPACK(d->d_type);
250 	CDF_UNPACK(d->d_color);
251 	CDF_UNPACK(d->d_left_child);
252 	CDF_UNPACK(d->d_right_child);
253 	CDF_UNPACK(d->d_storage);
254 	CDF_UNPACKA(d->d_storage_uuid);
255 	CDF_UNPACK(d->d_flags);
256 	CDF_UNPACK(d->d_created);
257 	CDF_UNPACK(d->d_modified);
258 	CDF_UNPACK(d->d_stream_first_sector);
259 	CDF_UNPACK(d->d_size);
260 	CDF_UNPACK(d->d_unused0);
261 }
262 
263 static int
264 cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h,
265     const void *p, size_t tail, int line)
266 {
267 	const char *b = (const char *)sst->sst_tab;
268 	const char *e = ((const char *)p) + tail;
269 	(void)&line;
270 	if (e >= b && (size_t)(e - b) < CDF_SEC_SIZE(h) * sst->sst_len)
271 		return 0;
272 	DPRINTF(("%d: offset begin %p end %p %" SIZE_T_FORMAT "u"
273 	    " >= %" SIZE_T_FORMAT "u [%" SIZE_T_FORMAT "u %"
274 	    SIZE_T_FORMAT "u]\n", line, b, e, (size_t)(e - b),
275 	    CDF_SEC_SIZE(h) * sst->sst_len, CDF_SEC_SIZE(h), sst->sst_len));
276 	errno = EFTYPE;
277 	return -1;
278 }
279 
280 static ssize_t
281 cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
282 {
283 	size_t siz = (size_t)off + len;
284 
285 	if ((off_t)(off + len) != (off_t)siz) {
286 		errno = EINVAL;
287 		return -1;
288 	}
289 
290 	if (info->i_buf != NULL && info->i_len >= siz) {
291 		(void)memcpy(buf, &info->i_buf[off], len);
292 		return (ssize_t)len;
293 	}
294 
295 	if (info->i_fd == -1)
296 		return -1;
297 
298 	if (lseek(info->i_fd, off, SEEK_SET) == (off_t)-1)
299 		return -1;
300 
301 	if (read(info->i_fd, buf, len) != (ssize_t)len)
302 		return -1;
303 
304 	return (ssize_t)len;
305 }
306 
307 int
308 cdf_read_header(const cdf_info_t *info, cdf_header_t *h)
309 {
310 	char buf[512];
311 
312 	(void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
313 	if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1)
314 		return -1;
315 	cdf_unpack_header(h, buf);
316 	cdf_swap_header(h);
317 	if (h->h_magic != CDF_MAGIC) {
318 		DPRINTF(("Bad magic 0x%" INT64_T_FORMAT "x != 0x%"
319 		    INT64_T_FORMAT "x\n",
320 		    (unsigned long long)h->h_magic,
321 		    (unsigned long long)CDF_MAGIC));
322 		goto out;
323 	}
324 	if (h->h_sec_size_p2 > 20) {
325 		DPRINTF(("Bad sector size 0x%u\n", h->h_sec_size_p2));
326 		goto out;
327 	}
328 	if (h->h_short_sec_size_p2 > 20) {
329 		DPRINTF(("Bad short sector size 0x%u\n",
330 		    h->h_short_sec_size_p2));
331 		goto out;
332 	}
333 	return 0;
334 out:
335 	errno = EFTYPE;
336 	return -1;
337 }
338 
339 
340 ssize_t
341 cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
342     const cdf_header_t *h, cdf_secid_t id)
343 {
344 	assert((size_t)CDF_SEC_SIZE(h) == len);
345 	return cdf_read(info, (off_t)CDF_SEC_POS(h, id),
346 	    ((char *)buf) + offs, len);
347 }
348 
349 ssize_t
350 cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
351     size_t len, const cdf_header_t *h, cdf_secid_t id)
352 {
353 	assert((size_t)CDF_SHORT_SEC_SIZE(h) == len);
354 	(void)memcpy(((char *)buf) + offs,
355 	    ((const char *)sst->sst_tab) + CDF_SHORT_SEC_POS(h, id), len);
356 	return len;
357 }
358 
359 /*
360  * Read the sector allocation table.
361  */
362 int
363 cdf_read_sat(const cdf_info_t *info, cdf_header_t *h, cdf_sat_t *sat)
364 {
365 	size_t i, j, k;
366 	size_t ss = CDF_SEC_SIZE(h);
367 	cdf_secid_t *msa, mid, sec;
368 	size_t nsatpersec = (ss / sizeof(mid)) - 1;
369 
370 	for (i = 0; i < __arraycount(h->h_master_sat); i++)
371 		if (h->h_master_sat[i] == CDF_SECID_FREE)
372 			break;
373 
374 #define CDF_SEC_LIMIT (UINT32_MAX / (4 * ss))
375 	if ((nsatpersec > 0 &&
376 	    h->h_num_sectors_in_master_sat > CDF_SEC_LIMIT / nsatpersec) ||
377 	    i > CDF_SEC_LIMIT) {
378 		DPRINTF(("Number of sectors in master SAT too big %u %"
379 		    SIZE_T_FORMAT "u\n", h->h_num_sectors_in_master_sat, i));
380 		errno = EFTYPE;
381 		return -1;
382 	}
383 
384 	sat->sat_len = h->h_num_sectors_in_master_sat * nsatpersec + i;
385 	DPRINTF(("sat_len = %" SIZE_T_FORMAT "u ss = %" SIZE_T_FORMAT "u\n",
386 	    sat->sat_len, ss));
387 	if ((sat->sat_tab = CAST(cdf_secid_t *, calloc(sat->sat_len, ss)))
388 	    == NULL)
389 		return -1;
390 
391 	for (i = 0; i < __arraycount(h->h_master_sat); i++) {
392 		if (h->h_master_sat[i] < 0)
393 			break;
394 		if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
395 		    h->h_master_sat[i]) != (ssize_t)ss) {
396 			DPRINTF(("Reading sector %d", h->h_master_sat[i]));
397 			goto out1;
398 		}
399 	}
400 
401 	if ((msa = CAST(cdf_secid_t *, calloc(1, ss))) == NULL)
402 		goto out1;
403 
404 	mid = h->h_secid_first_sector_in_master_sat;
405 	for (j = 0; j < h->h_num_sectors_in_master_sat; j++) {
406 		if (mid < 0)
407 			goto out;
408 		if (j >= CDF_LOOP_LIMIT) {
409 			DPRINTF(("Reading master sector loop limit"));
410 			errno = EFTYPE;
411 			goto out2;
412 		}
413 		if (cdf_read_sector(info, msa, 0, ss, h, mid) != (ssize_t)ss) {
414 			DPRINTF(("Reading master sector %d", mid));
415 			goto out2;
416 		}
417 		for (k = 0; k < nsatpersec; k++, i++) {
418 			sec = CDF_TOLE4((uint32_t)msa[k]);
419 			if (sec < 0)
420 				goto out;
421 			if (i >= sat->sat_len) {
422 			    DPRINTF(("Out of bounds reading MSA %u >= %u",
423 				i, sat->sat_len));
424 			    errno = EFTYPE;
425 			    goto out2;
426 			}
427 			if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
428 			    sec) != (ssize_t)ss) {
429 				DPRINTF(("Reading sector %d",
430 				    CDF_TOLE4(msa[k])));
431 				goto out2;
432 			}
433 		}
434 		mid = CDF_TOLE4((uint32_t)msa[nsatpersec]);
435 	}
436 out:
437 	sat->sat_len = i;
438 	free(msa);
439 	return 0;
440 out2:
441 	free(msa);
442 out1:
443 	free(sat->sat_tab);
444 	return -1;
445 }
446 
447 size_t
448 cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
449 {
450 	size_t i, j;
451 	cdf_secid_t maxsector = (cdf_secid_t)(sat->sat_len * size);
452 
453 	DPRINTF(("Chain:"));
454 	for (j = i = 0; sid >= 0; i++, j++) {
455 		DPRINTF((" %d", sid));
456 		if (j >= CDF_LOOP_LIMIT) {
457 			DPRINTF(("Counting chain loop limit"));
458 			errno = EFTYPE;
459 			return (size_t)-1;
460 		}
461 		if (sid > maxsector) {
462 			DPRINTF(("Sector %d > %d\n", sid, maxsector));
463 			errno = EFTYPE;
464 			return (size_t)-1;
465 		}
466 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
467 	}
468 	DPRINTF(("\n"));
469 	return i;
470 }
471 
472 int
473 cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
474     const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
475 {
476 	size_t ss = CDF_SEC_SIZE(h), i, j;
477 	ssize_t nr;
478 	scn->sst_len = cdf_count_chain(sat, sid, ss);
479 	scn->sst_dirlen = len;
480 
481 	if (scn->sst_len == (size_t)-1)
482 		return -1;
483 
484 	scn->sst_tab = calloc(scn->sst_len, ss);
485 	if (scn->sst_tab == NULL)
486 		return -1;
487 
488 	for (j = i = 0; sid >= 0; i++, j++) {
489 		if (j >= CDF_LOOP_LIMIT) {
490 			DPRINTF(("Read long sector chain loop limit"));
491 			errno = EFTYPE;
492 			goto out;
493 		}
494 		if (i >= scn->sst_len) {
495 			DPRINTF(("Out of bounds reading long sector chain "
496 			    "%u > %u\n", i, scn->sst_len));
497 			errno = EFTYPE;
498 			goto out;
499 		}
500 		if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
501 		    sid)) != (ssize_t)ss) {
502 			if (i == scn->sst_len - 1 && nr > 0) {
503 				/* Last sector might be truncated */
504 				return 0;
505 			}
506 			DPRINTF(("Reading long sector chain %d", sid));
507 			goto out;
508 		}
509 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
510 	}
511 	return 0;
512 out:
513 	free(scn->sst_tab);
514 	return -1;
515 }
516 
517 int
518 cdf_read_short_sector_chain(const cdf_header_t *h,
519     const cdf_sat_t *ssat, const cdf_stream_t *sst,
520     cdf_secid_t sid, size_t len, cdf_stream_t *scn)
521 {
522 	size_t ss = CDF_SHORT_SEC_SIZE(h), i, j;
523 	scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h));
524 	scn->sst_dirlen = len;
525 
526 	if (sst->sst_tab == NULL || scn->sst_len == (size_t)-1)
527 		return -1;
528 
529 	scn->sst_tab = calloc(scn->sst_len, ss);
530 	if (scn->sst_tab == NULL)
531 		return -1;
532 
533 	for (j = i = 0; sid >= 0; i++, j++) {
534 		if (j >= CDF_LOOP_LIMIT) {
535 			DPRINTF(("Read short sector chain loop limit"));
536 			errno = EFTYPE;
537 			goto out;
538 		}
539 		if (i >= scn->sst_len) {
540 			DPRINTF(("Out of bounds reading short sector chain "
541 			    "%u > %u\n", i, scn->sst_len));
542 			errno = EFTYPE;
543 			goto out;
544 		}
545 		if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
546 		    sid) != (ssize_t)ss) {
547 			DPRINTF(("Reading short sector chain %d", sid));
548 			goto out;
549 		}
550 		sid = CDF_TOLE4((uint32_t)ssat->sat_tab[sid]);
551 	}
552 	return 0;
553 out:
554 	free(scn->sst_tab);
555 	return -1;
556 }
557 
558 int
559 cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
560     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
561     cdf_secid_t sid, size_t len, cdf_stream_t *scn)
562 {
563 
564 	if (len < h->h_min_size_standard_stream && sst->sst_tab != NULL)
565 		return cdf_read_short_sector_chain(h, ssat, sst, sid, len,
566 		    scn);
567 	else
568 		return cdf_read_long_sector_chain(info, h, sat, sid, len, scn);
569 }
570 
571 int
572 cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h,
573     const cdf_sat_t *sat, cdf_dir_t *dir)
574 {
575 	size_t i, j;
576 	size_t ss = CDF_SEC_SIZE(h), ns, nd;
577 	char *buf;
578 	cdf_secid_t sid = h->h_secid_first_directory;
579 
580 	ns = cdf_count_chain(sat, sid, ss);
581 	if (ns == (size_t)-1)
582 		return -1;
583 
584 	nd = ss / CDF_DIRECTORY_SIZE;
585 
586 	dir->dir_len = ns * nd;
587 	dir->dir_tab = CAST(cdf_directory_t *,
588 	    calloc(dir->dir_len, sizeof(dir->dir_tab[0])));
589 	if (dir->dir_tab == NULL)
590 		return -1;
591 
592 	if ((buf = CAST(char *, malloc(ss))) == NULL) {
593 		free(dir->dir_tab);
594 		return -1;
595 	}
596 
597 	for (j = i = 0; i < ns; i++, j++) {
598 		if (j >= CDF_LOOP_LIMIT) {
599 			DPRINTF(("Read dir loop limit"));
600 			errno = EFTYPE;
601 			goto out;
602 		}
603 		if (cdf_read_sector(info, buf, 0, ss, h, sid) != (ssize_t)ss) {
604 			DPRINTF(("Reading directory sector %d", sid));
605 			goto out;
606 		}
607 		for (j = 0; j < nd; j++) {
608 			cdf_unpack_dir(&dir->dir_tab[i * nd + j],
609 			    &buf[j * CDF_DIRECTORY_SIZE]);
610 		}
611 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
612 	}
613 	if (NEED_SWAP)
614 		for (i = 0; i < dir->dir_len; i++)
615 			cdf_swap_dir(&dir->dir_tab[i]);
616 	free(buf);
617 	return 0;
618 out:
619 	free(dir->dir_tab);
620 	free(buf);
621 	return -1;
622 }
623 
624 
625 int
626 cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h,
627     const cdf_sat_t *sat, cdf_sat_t *ssat)
628 {
629 	size_t i, j;
630 	size_t ss = CDF_SEC_SIZE(h);
631 	cdf_secid_t sid = h->h_secid_first_sector_in_short_sat;
632 
633 	ssat->sat_len = cdf_count_chain(sat, sid, CDF_SEC_SIZE(h));
634 	if (ssat->sat_len == (size_t)-1)
635 		return -1;
636 
637 	ssat->sat_tab = CAST(cdf_secid_t *, calloc(ssat->sat_len, ss));
638 	if (ssat->sat_tab == NULL)
639 		return -1;
640 
641 	for (j = i = 0; sid >= 0; i++, j++) {
642 		if (j >= CDF_LOOP_LIMIT) {
643 			DPRINTF(("Read short sat sector loop limit"));
644 			errno = EFTYPE;
645 			goto out;
646 		}
647 		if (i >= ssat->sat_len) {
648 			DPRINTF(("Out of bounds reading short sector chain "
649 			    "%u > %u\n", i, ssat->sat_len));
650 			errno = EFTYPE;
651 			goto out;
652 		}
653 		if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
654 		    (ssize_t)ss) {
655 			DPRINTF(("Reading short sat sector %d", sid));
656 			goto out;
657 		}
658 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
659 	}
660 	return 0;
661 out:
662 	free(ssat->sat_tab);
663 	return -1;
664 }
665 
666 int
667 cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
668     const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn)
669 {
670 	size_t i;
671 	const cdf_directory_t *d;
672 
673 	for (i = 0; i < dir->dir_len; i++)
674 		if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
675 			break;
676 
677 	/* If the it is not there, just fake it; some docs don't have it */
678 	if (i == dir->dir_len)
679 		goto out;
680 	d = &dir->dir_tab[i];
681 
682 	/* If the it is not there, just fake it; some docs don't have it */
683 	if (d->d_stream_first_sector < 0)
684 		goto out;
685 
686 	return	cdf_read_long_sector_chain(info, h, sat,
687 	    d->d_stream_first_sector, d->d_size, scn);
688 out:
689 	scn->sst_tab = NULL;
690 	scn->sst_len = 0;
691 	scn->sst_dirlen = 0;
692 	return 0;
693 }
694 
695 static int
696 cdf_namecmp(const char *d, const uint16_t *s, size_t l)
697 {
698 	for (; l--; d++, s++)
699 		if (*d != CDF_TOLE2(*s))
700 			return (unsigned char)*d - CDF_TOLE2(*s);
701 	return 0;
702 }
703 
704 int
705 cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
706     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
707     const cdf_dir_t *dir, cdf_stream_t *scn)
708 {
709 	size_t i;
710 	const cdf_directory_t *d;
711 	static const char name[] = "\05SummaryInformation";
712 
713 	for (i = dir->dir_len; i > 0; i--)
714 		if (dir->dir_tab[i - 1].d_type == CDF_DIR_TYPE_USER_STREAM &&
715 		    cdf_namecmp(name, dir->dir_tab[i - 1].d_name, sizeof(name))
716 		    == 0)
717 			break;
718 
719 	if (i == 0) {
720 		DPRINTF(("Cannot find summary information section\n"));
721 		errno = ESRCH;
722 		return -1;
723 	}
724 	d = &dir->dir_tab[i - 1];
725 	return cdf_read_sector_chain(info, h, sat, ssat, sst,
726 	    d->d_stream_first_sector, d->d_size, scn);
727 }
728 
729 int
730 cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
731     uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
732 {
733 	const cdf_section_header_t *shp;
734 	cdf_section_header_t sh;
735 	const uint8_t *p, *q, *e;
736 	int16_t s16;
737 	int32_t s32;
738 	uint32_t u32;
739 	int64_t s64;
740 	uint64_t u64;
741 	cdf_timestamp_t tp;
742 	size_t i, o, o4, nelements, j;
743 	cdf_property_info_t *inp;
744 
745 	if (offs > UINT32_MAX / 4) {
746 		errno = EFTYPE;
747 		goto out;
748 	}
749 	shp = CAST(const cdf_section_header_t *, (const void *)
750 	    ((const char *)sst->sst_tab + offs));
751 	if (cdf_check_stream_offset(sst, h, shp, sizeof(*shp), __LINE__) == -1)
752 		goto out;
753 	sh.sh_len = CDF_TOLE4(shp->sh_len);
754 #define CDF_SHLEN_LIMIT (UINT32_MAX / 8)
755 	if (sh.sh_len > CDF_SHLEN_LIMIT) {
756 		errno = EFTYPE;
757 		goto out;
758 	}
759 	sh.sh_properties = CDF_TOLE4(shp->sh_properties);
760 #define CDF_PROP_LIMIT (UINT32_MAX / (4 * sizeof(*inp)))
761 	if (sh.sh_properties > CDF_PROP_LIMIT)
762 		goto out;
763 	DPRINTF(("section len: %u properties %u\n", sh.sh_len,
764 	    sh.sh_properties));
765 	if (*maxcount) {
766 		if (*maxcount > CDF_PROP_LIMIT)
767 			goto out;
768 		*maxcount += sh.sh_properties;
769 		inp = CAST(cdf_property_info_t *,
770 		    realloc(*info, *maxcount * sizeof(*inp)));
771 	} else {
772 		*maxcount = sh.sh_properties;
773 		inp = CAST(cdf_property_info_t *,
774 		    malloc(*maxcount * sizeof(*inp)));
775 	}
776 	if (inp == NULL)
777 		goto out;
778 	*info = inp;
779 	inp += *count;
780 	*count += sh.sh_properties;
781 	p = CAST(const uint8_t *, (const void *)
782 	    ((const char *)(const void *)sst->sst_tab +
783 	    offs + sizeof(sh)));
784 	e = CAST(const uint8_t *, (const void *)
785 	    (((const char *)(const void *)shp) + sh.sh_len));
786 	if (cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1)
787 		goto out;
788 	for (i = 0; i < sh.sh_properties; i++) {
789 		q = (const uint8_t *)(const void *)
790 		    ((const char *)(const void *)p +
791 		    CDF_GETUINT32(p, (i << 1) + 1)) - 2 * sizeof(uint32_t);
792 		if (q > e) {
793 			DPRINTF(("Ran of the end %p > %p\n", q, e));
794 			goto out;
795 		}
796 		inp[i].pi_id = CDF_GETUINT32(p, i << 1);
797 		inp[i].pi_type = CDF_GETUINT32(q, 0);
798 		DPRINTF(("%d) id=%x type=%x offs=%x,%d\n", i, inp[i].pi_id,
799 		    inp[i].pi_type, q - p, CDF_GETUINT32(p, (i << 1) + 1)));
800 		if (inp[i].pi_type & CDF_VECTOR) {
801 			nelements = CDF_GETUINT32(q, 1);
802 			o = 2;
803 		} else {
804 			nelements = 1;
805 			o = 1;
806 		}
807 		o4 = o * sizeof(uint32_t);
808 		if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
809 			goto unknown;
810 		switch (inp[i].pi_type & CDF_TYPEMASK) {
811 		case CDF_NULL:
812 		case CDF_EMPTY:
813 			break;
814 		case CDF_SIGNED16:
815 			if (inp[i].pi_type & CDF_VECTOR)
816 				goto unknown;
817 			(void)memcpy(&s16, &q[o4], sizeof(s16));
818 			inp[i].pi_s16 = CDF_TOLE2(s16);
819 			break;
820 		case CDF_SIGNED32:
821 			if (inp[i].pi_type & CDF_VECTOR)
822 				goto unknown;
823 			(void)memcpy(&s32, &q[o4], sizeof(s32));
824 			inp[i].pi_s32 = CDF_TOLE4((uint32_t)s32);
825 			break;
826 		case CDF_BOOL:
827 		case CDF_UNSIGNED32:
828 			if (inp[i].pi_type & CDF_VECTOR)
829 				goto unknown;
830 			(void)memcpy(&u32, &q[o4], sizeof(u32));
831 			inp[i].pi_u32 = CDF_TOLE4(u32);
832 			break;
833 		case CDF_SIGNED64:
834 			if (inp[i].pi_type & CDF_VECTOR)
835 				goto unknown;
836 			(void)memcpy(&s64, &q[o4], sizeof(s64));
837 			inp[i].pi_s64 = CDF_TOLE8((uint64_t)s64);
838 			break;
839 		case CDF_UNSIGNED64:
840 			if (inp[i].pi_type & CDF_VECTOR)
841 				goto unknown;
842 			(void)memcpy(&u64, &q[o4], sizeof(u64));
843 			inp[i].pi_u64 = CDF_TOLE8((uint64_t)u64);
844 			break;
845 		case CDF_LENGTH32_STRING:
846 		case CDF_LENGTH32_WSTRING:
847 			if (nelements > 1) {
848 				size_t nelem = inp - *info;
849 				if (*maxcount > CDF_PROP_LIMIT
850 				    || nelements > CDF_PROP_LIMIT)
851 					goto out;
852 				*maxcount += nelements;
853 				inp = CAST(cdf_property_info_t *,
854 				    realloc(*info, *maxcount * sizeof(*inp)));
855 				if (inp == NULL)
856 					goto out;
857 				*info = inp;
858 				inp = *info + nelem;
859 			}
860 			DPRINTF(("nelements = %d\n", nelements));
861 			for (j = 0; j < nelements; j++, i++) {
862 				uint32_t l = CDF_GETUINT32(q, o);
863 				inp[i].pi_str.s_len = l;
864 				inp[i].pi_str.s_buf = (const char *)
865 				    (const void *)(&q[o4 + sizeof(l)]);
866 				DPRINTF(("l = %d, r = %d, s = %s\n", l,
867 				    CDF_ROUND(l, sizeof(l)),
868 				    inp[i].pi_str.s_buf));
869 				l = 4 + (uint32_t)CDF_ROUND(l, sizeof(l));
870 				o += l >> 2;
871 				o4 = o * sizeof(uint32_t);
872 			}
873 			i--;
874 			break;
875 		case CDF_FILETIME:
876 			if (inp[i].pi_type & CDF_VECTOR)
877 				goto unknown;
878 			(void)memcpy(&tp, &q[o4], sizeof(tp));
879 			inp[i].pi_tp = CDF_TOLE8((uint64_t)tp);
880 			break;
881 		case CDF_CLIPBOARD:
882 			if (inp[i].pi_type & CDF_VECTOR)
883 				goto unknown;
884 			break;
885 		default:
886 		unknown:
887 			DPRINTF(("Don't know how to deal with %x\n",
888 			    inp[i].pi_type));
889 			goto out;
890 		}
891 	}
892 	return 0;
893 out:
894 	free(*info);
895 	return -1;
896 }
897 
898 int
899 cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h,
900     cdf_summary_info_header_t *ssi, cdf_property_info_t **info, size_t *count)
901 {
902 	size_t i, maxcount;
903 	const cdf_summary_info_header_t *si =
904 	    CAST(const cdf_summary_info_header_t *, sst->sst_tab);
905 	const cdf_section_declaration_t *sd =
906 	    CAST(const cdf_section_declaration_t *, (const void *)
907 	    ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET));
908 
909 	if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 ||
910 	    cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1)
911 		return -1;
912 	ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
913 	ssi->si_os_version = CDF_TOLE2(si->si_os_version);
914 	ssi->si_os = CDF_TOLE2(si->si_os);
915 	ssi->si_class = si->si_class;
916 	cdf_swap_class(&ssi->si_class);
917 	ssi->si_count = CDF_TOLE2(si->si_count);
918 	*count = 0;
919 	maxcount = 0;
920 	*info = NULL;
921 	for (i = 0; i < CDF_TOLE4(si->si_count); i++) {
922 		if (i >= CDF_LOOP_LIMIT) {
923 			DPRINTF(("Unpack summary info loop limit"));
924 			errno = EFTYPE;
925 			return -1;
926 		}
927 		if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset),
928 		    info, count, &maxcount) == -1)
929 			return -1;
930 	}
931 	return 0;
932 }
933 
934 
935 
936 int
937 cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
938 {
939 	return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
940 	    "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
941 	    id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
942 	    id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
943 	    id->cl_six[5]);
944 }
945 
946 static const struct {
947 	uint32_t v;
948 	const char *n;
949 } vn[] = {
950 	{ CDF_PROPERTY_CODE_PAGE, "Code page" },
951 	{ CDF_PROPERTY_TITLE, "Title" },
952 	{ CDF_PROPERTY_SUBJECT, "Subject" },
953 	{ CDF_PROPERTY_AUTHOR, "Author" },
954 	{ CDF_PROPERTY_KEYWORDS, "Keywords" },
955 	{ CDF_PROPERTY_COMMENTS, "Comments" },
956 	{ CDF_PROPERTY_TEMPLATE, "Template" },
957 	{ CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
958 	{ CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
959 	{ CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
960 	{ CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
961 	{ CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
962 	{ CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
963 	{ CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
964 	{ CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
965 	{ CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
966 	{ CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
967 	{ CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
968 	{ CDF_PROPERTY_SECURITY, "Security" },
969 	{ CDF_PROPERTY_LOCALE_ID, "Locale ID" },
970 };
971 
972 int
973 cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
974 {
975 	size_t i;
976 
977 	for (i = 0; i < __arraycount(vn); i++)
978 		if (vn[i].v == p)
979 			return snprintf(buf, bufsiz, "%s", vn[i].n);
980 	return snprintf(buf, bufsiz, "0x%x", p);
981 }
982 
983 int
984 cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
985 {
986 	int len = 0;
987 	int days, hours, mins, secs;
988 
989 	ts /= CDF_TIME_PREC;
990 	secs = (int)(ts % 60);
991 	ts /= 60;
992 	mins = (int)(ts % 60);
993 	ts /= 60;
994 	hours = (int)(ts % 24);
995 	ts /= 24;
996 	days = (int)ts;
997 
998 	if (days) {
999 		len += snprintf(buf + len, bufsiz - len, "%dd+", days);
1000 		if ((size_t)len >= bufsiz)
1001 			return len;
1002 	}
1003 
1004 	if (days || hours) {
1005 		len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
1006 		if ((size_t)len >= bufsiz)
1007 			return len;
1008 	}
1009 
1010 	len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
1011 	if ((size_t)len >= bufsiz)
1012 		return len;
1013 
1014 	len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
1015 	return len;
1016 }
1017 
1018 
1019 #ifdef CDF_DEBUG
1020 void
1021 cdf_dump_header(const cdf_header_t *h)
1022 {
1023 	size_t i;
1024 
1025 #define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
1026 #define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \
1027     h->h_ ## b, 1 << h->h_ ## b)
1028 	DUMP("%d", revision);
1029 	DUMP("%d", version);
1030 	DUMP("0x%x", byte_order);
1031 	DUMP2("%d", sec_size_p2);
1032 	DUMP2("%d", short_sec_size_p2);
1033 	DUMP("%d", num_sectors_in_sat);
1034 	DUMP("%d", secid_first_directory);
1035 	DUMP("%d", min_size_standard_stream);
1036 	DUMP("%d", secid_first_sector_in_short_sat);
1037 	DUMP("%d", num_sectors_in_short_sat);
1038 	DUMP("%d", secid_first_sector_in_master_sat);
1039 	DUMP("%d", num_sectors_in_master_sat);
1040 	for (i = 0; i < __arraycount(h->h_master_sat); i++) {
1041 		if (h->h_master_sat[i] == CDF_SECID_FREE)
1042 			break;
1043 		(void)fprintf(stderr, "%35.35s[%.3zu] = %d\n",
1044 		    "master_sat", i, h->h_master_sat[i]);
1045 	}
1046 }
1047 
1048 void
1049 cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
1050 {
1051 	size_t i, j, s = size / sizeof(cdf_secid_t);
1052 
1053 	for (i = 0; i < sat->sat_len; i++) {
1054 		(void)fprintf(stderr, "%s[%" SIZE_T_FORMAT "u]:\n%.6d: ",
1055 		    prefix, i, i * s);
1056 		for (j = 0; j < s; j++) {
1057 			(void)fprintf(stderr, "%5d, ",
1058 			    CDF_TOLE4(sat->sat_tab[s * i + j]));
1059 			if ((j + 1) % 10 == 0)
1060 				(void)fprintf(stderr, "\n%.6d: ",
1061 				    i * s + j + 1);
1062 		}
1063 		(void)fprintf(stderr, "\n");
1064 	}
1065 }
1066 
1067 void
1068 cdf_dump(void *v, size_t len)
1069 {
1070 	size_t i, j;
1071 	unsigned char *p = v;
1072 	char abuf[16];
1073 	(void)fprintf(stderr, "%.4x: ", 0);
1074 	for (i = 0, j = 0; i < len; i++, p++) {
1075 		(void)fprintf(stderr, "%.2x ", *p);
1076 		abuf[j++] = isprint(*p) ? *p : '.';
1077 		if (j == 16) {
1078 			j = 0;
1079 			abuf[15] = '\0';
1080 			(void)fprintf(stderr, "%s\n%.4x: ", abuf, i + 1);
1081 		}
1082 	}
1083 	(void)fprintf(stderr, "\n");
1084 }
1085 
1086 void
1087 cdf_dump_stream(const cdf_header_t *h, const cdf_stream_t *sst)
1088 {
1089 	size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
1090 	    CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
1091 	cdf_dump(sst->sst_tab, ss * sst->sst_len);
1092 }
1093 
1094 void
1095 cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
1096     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
1097     const cdf_dir_t *dir)
1098 {
1099 	size_t i, j;
1100 	cdf_directory_t *d;
1101 	char name[__arraycount(d->d_name)];
1102 	cdf_stream_t scn;
1103 	struct timespec ts;
1104 
1105 	static const char *types[] = { "empty", "user storage",
1106 	    "user stream", "lockbytes", "property", "root storage" };
1107 
1108 	for (i = 0; i < dir->dir_len; i++) {
1109 		d = &dir->dir_tab[i];
1110 		for (j = 0; j < sizeof(name); j++)
1111 			name[j] = (char)CDF_TOLE2(d->d_name[j]);
1112 		(void)fprintf(stderr, "Directory %" SIZE_T_FORMAT "u: %s\n",
1113 		    i, name);
1114 		if (d->d_type < __arraycount(types))
1115 			(void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
1116 		else
1117 			(void)fprintf(stderr, "Type: %d\n", d->d_type);
1118 		(void)fprintf(stderr, "Color: %s\n",
1119 		    d->d_color ? "black" : "red");
1120 		(void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
1121 		(void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
1122 		(void)fprintf(stderr, "Flags: 0x%x\n", d->d_flags);
1123 		cdf_timestamp_to_timespec(&ts, d->d_created);
1124 		(void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec));
1125 		cdf_timestamp_to_timespec(&ts, d->d_modified);
1126 		(void)fprintf(stderr, "Modified %s", cdf_ctime(&ts.tv_sec));
1127 		(void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
1128 		(void)fprintf(stderr, "Size %d\n", d->d_size);
1129 		switch (d->d_type) {
1130 		case CDF_DIR_TYPE_USER_STORAGE:
1131 			(void)fprintf(stderr, "Storage: %d\n", d->d_storage);
1132 			break;
1133 		case CDF_DIR_TYPE_USER_STREAM:
1134 			if (sst == NULL)
1135 				break;
1136 			if (cdf_read_sector_chain(info, h, sat, ssat, sst,
1137 			    d->d_stream_first_sector, d->d_size, &scn) == -1) {
1138 				warn("Can't read stream for %s at %d len %d",
1139 				    name, d->d_stream_first_sector, d->d_size);
1140 				break;
1141 			}
1142 			cdf_dump_stream(h, &scn);
1143 			free(scn.sst_tab);
1144 			break;
1145 		default:
1146 			break;
1147 		}
1148 
1149 	}
1150 }
1151 
1152 void
1153 cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
1154 {
1155 	cdf_timestamp_t tp;
1156 	struct timespec ts;
1157 	char buf[64];
1158 	size_t i, j;
1159 
1160 	for (i = 0; i < count; i++) {
1161 		cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
1162 		(void)fprintf(stderr, "%" SIZE_T_FORMAT "u) %s: ", i, buf);
1163 		switch (info[i].pi_type) {
1164 		case CDF_NULL:
1165 			break;
1166 		case CDF_SIGNED16:
1167 			(void)fprintf(stderr, "signed 16 [%hd]\n",
1168 			    info[i].pi_s16);
1169 			break;
1170 		case CDF_SIGNED32:
1171 			(void)fprintf(stderr, "signed 32 [%d]\n",
1172 			    info[i].pi_s32);
1173 			break;
1174 		case CDF_UNSIGNED32:
1175 			(void)fprintf(stderr, "unsigned 32 [%u]\n",
1176 			    info[i].pi_u32);
1177 			break;
1178 		case CDF_LENGTH32_STRING:
1179 			(void)fprintf(stderr, "string %u [%.*s]\n",
1180 			    info[i].pi_str.s_len,
1181 			    info[i].pi_str.s_len, info[i].pi_str.s_buf);
1182 			break;
1183 		case CDF_LENGTH32_WSTRING:
1184 			(void)fprintf(stderr, "string %u [",
1185 			    info[i].pi_str.s_len);
1186 			for (j = 0; j < info[i].pi_str.s_len - 1; j++)
1187 			    (void)fputc(info[i].pi_str.s_buf[j << 1], stderr);
1188 			(void)fprintf(stderr, "]\n");
1189 			break;
1190 		case CDF_FILETIME:
1191 			tp = info[i].pi_tp;
1192 			if (tp < 1000000000000000LL) {
1193 				cdf_print_elapsed_time(buf, sizeof(buf), tp);
1194 				(void)fprintf(stderr, "timestamp %s\n", buf);
1195 			} else {
1196 				cdf_timestamp_to_timespec(&ts, tp);
1197 				(void)fprintf(stderr, "timestamp %s",
1198 				    cdf_ctime(&ts.tv_sec));
1199 			}
1200 			break;
1201 		case CDF_CLIPBOARD:
1202 			(void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
1203 			break;
1204 		default:
1205 			DPRINTF(("Don't know how to deal with %x\n",
1206 			    info[i].pi_type));
1207 			break;
1208 		}
1209 	}
1210 }
1211 
1212 
1213 void
1214 cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
1215 {
1216 	char buf[128];
1217 	cdf_summary_info_header_t ssi;
1218 	cdf_property_info_t *info;
1219 	size_t count;
1220 
1221 	(void)&h;
1222 	if (cdf_unpack_summary_info(sst, h, &ssi, &info, &count) == -1)
1223 		return;
1224 	(void)fprintf(stderr, "Endian: %x\n", ssi.si_byte_order);
1225 	(void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
1226 		ssi.si_os_version >> 8);
1227 	(void)fprintf(stderr, "Os %d\n", ssi.si_os);
1228 	cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
1229 	(void)fprintf(stderr, "Class %s\n", buf);
1230 	(void)fprintf(stderr, "Count %d\n", ssi.si_count);
1231 	cdf_dump_property_info(info, count);
1232 	free(info);
1233 }
1234 
1235 #endif
1236 
1237 #ifdef TEST
1238 int
1239 main(int argc, char *argv[])
1240 {
1241 	int i;
1242 	cdf_header_t h;
1243 	cdf_sat_t sat, ssat;
1244 	cdf_stream_t sst, scn;
1245 	cdf_dir_t dir;
1246 	cdf_info_t info;
1247 
1248 	if (argc < 2) {
1249 		(void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
1250 		return -1;
1251 	}
1252 
1253 	info.i_buf = NULL;
1254 	info.i_len = 0;
1255 	for (i = 1; i < argc; i++) {
1256 		if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
1257 			err(1, "Cannot open `%s'", argv[1]);
1258 
1259 		if (cdf_read_header(&info, &h) == -1)
1260 			err(1, "Cannot read header");
1261 #ifdef CDF_DEBUG
1262 		cdf_dump_header(&h);
1263 #endif
1264 
1265 		if (cdf_read_sat(&info, &h, &sat) == -1)
1266 			err(1, "Cannot read sat");
1267 #ifdef CDF_DEBUG
1268 		cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
1269 #endif
1270 
1271 		if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
1272 			err(1, "Cannot read ssat");
1273 #ifdef CDF_DEBUG
1274 		cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
1275 #endif
1276 
1277 		if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
1278 			err(1, "Cannot read dir");
1279 
1280 		if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst) == -1)
1281 			err(1, "Cannot read short stream");
1282 #ifdef CDF_DEBUG
1283 		cdf_dump_stream(&h, &sst);
1284 #endif
1285 
1286 #ifdef CDF_DEBUG
1287 		cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
1288 #endif
1289 
1290 
1291 		if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
1292 		    &scn) == -1)
1293 			err(1, "Cannot read summary info");
1294 #ifdef CDF_DEBUG
1295 		cdf_dump_summary_info(&h, &scn);
1296 #endif
1297 
1298 		(void)close(info.i_fd);
1299 	}
1300 
1301 	return 0;
1302 }
1303 #endif
1304