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