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