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