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