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