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