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