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