xref: /PHP-7.2/ext/fileinfo/libmagic/readcdf.c (revision 08d8623d)
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 #include "file.h"
27 
28 #ifndef lint
29 FILE_RCSID("@(#)$File: readcdf.c,v 1.65 2017/04/08 20:58:03 christos Exp $")
30 #endif
31 
32 #include <assert.h>
33 #include <stdlib.h>
34 #ifdef PHP_WIN32
35 #include "win32/unistd.h"
36 #else
37 #include <unistd.h>
38 #endif
39 #include <string.h>
40 #include <time.h>
41 #include <ctype.h>
42 
43 #include "cdf.h"
44 #include "magic.h"
45 
46 #ifndef __arraycount
47 #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
48 #endif
49 
50 #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
51 
52 static const struct nv {
53 	const char *pattern;
54 	const char *mime;
55 } app2mime[] =  {
56 	{ "Word",			"msword",		},
57 	{ "Excel",			"vnd.ms-excel",		},
58 	{ "Powerpoint",			"vnd.ms-powerpoint",	},
59 	{ "Crystal Reports",		"x-rpt",		},
60 	{ "Advanced Installer",		"vnd.ms-msi",		},
61 	{ "InstallShield",		"vnd.ms-msi",		},
62 	{ "Microsoft Patch Compiler",	"vnd.ms-msi",		},
63 	{ "NAnt",			"vnd.ms-msi",		},
64 	{ "Windows Installer",		"vnd.ms-msi",		},
65 	{ NULL,				NULL,			},
66 }, name2mime[] = {
67 	{ "Book",			"vnd.ms-excel",		},
68 	{ "Workbook",			"vnd.ms-excel",		},
69 	{ "WordDocument",		"msword",		},
70 	{ "PowerPoint",			"vnd.ms-powerpoint",	},
71 	{ "DigitalSignature",		"vnd.ms-msi",		},
72 	{ NULL,				NULL,			},
73 }, name2desc[] = {
74 	{ "Book",			"Microsoft Excel",	},
75 	{ "Workbook",			"Microsoft Excel",	},
76 	{ "WordDocument",		"Microsoft Word",	},
77 	{ "PowerPoint",			"Microsoft PowerPoint",	},
78 	{ "DigitalSignature",		"Microsoft Installer",	},
79 	{ NULL,				NULL,			},
80 };
81 
82 #ifdef PHP_WIN32
83 # define strcasestr strstr
84 #endif
85 
86 static const struct cv {
87 	uint64_t clsid[2];
88 	const char *mime;
89 } clsid2mime[] = {
90 	{
91 		{ 0x00000000000c1084ULL, 0x46000000000000c0ULL  },
92 		"x-msi",
93 	},
94 	{	{ 0,			 0			},
95 		NULL,
96 	},
97 }, clsid2desc[] = {
98 	{
99 		{ 0x00000000000c1084ULL, 0x46000000000000c0ULL  },
100 		"MSI Installer",
101 	},
102 	{	{ 0,			 0			},
103 		NULL,
104 	},
105 };
106 
107 private const char *
cdf_clsid_to_mime(const uint64_t clsid[2],const struct cv * cv)108 cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
109 {
110 	size_t i;
111 	for (i = 0; cv[i].mime != NULL; i++) {
112 		if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
113 			return cv[i].mime;
114 	}
115 	return NULL;
116 }
117 
118 private const char *
cdf_app_to_mime(const char * vbuf,const struct nv * nv)119 cdf_app_to_mime(const char *vbuf, const struct nv *nv)
120 {
121 	size_t i;
122 	const char *rv = NULL;
123 
124 	(void)setlocale(LC_CTYPE, "C");
125 	for (i = 0; nv[i].pattern != NULL; i++)
126 		if (strcasestr(vbuf, nv[i].pattern) != NULL) {
127 			rv = nv[i].mime;
128 			break;
129 		}
130 	(void)setlocale(LC_CTYPE, "");
131 	return rv;
132 }
133 
134 private int
cdf_file_property_info(struct magic_set * ms,const cdf_property_info_t * info,size_t count,const cdf_directory_t * root_storage)135 cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
136     size_t count, const cdf_directory_t *root_storage)
137 {
138         size_t i;
139         cdf_timestamp_t tp;
140         struct timeval ts;
141         char buf[64];
142         const char *str = NULL;
143         const char *s, *e;
144         int len;
145 
146 	memset(&ts, 0, sizeof(ts));
147 
148         if (!NOTMIME(ms) && root_storage)
149 		str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
150 		    clsid2mime);
151 
152         for (i = 0; i < count; i++) {
153                 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
154                 switch (info[i].pi_type) {
155                 case CDF_NULL:
156                         break;
157                 case CDF_SIGNED16:
158                         if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
159                             info[i].pi_s16) == -1)
160                                 return -1;
161                         break;
162                 case CDF_SIGNED32:
163                         if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
164                             info[i].pi_s32) == -1)
165                                 return -1;
166                         break;
167                 case CDF_UNSIGNED32:
168                         if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
169                             info[i].pi_u32) == -1)
170                                 return -1;
171                         break;
172                 case CDF_FLOAT:
173                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
174                             info[i].pi_f) == -1)
175                                 return -1;
176                         break;
177                 case CDF_DOUBLE:
178                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
179                             info[i].pi_d) == -1)
180                                 return -1;
181                         break;
182                 case CDF_LENGTH32_STRING:
183                 case CDF_LENGTH32_WSTRING:
184                         len = info[i].pi_str.s_len;
185                         if (len > 1) {
186                                 char vbuf[1024];
187                                 size_t j, k = 1;
188 
189                                 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
190                                     k++;
191                                 s = info[i].pi_str.s_buf;
192 				e = info[i].pi_str.s_buf + len;
193                                 for (j = 0; s < e && j < sizeof(vbuf)
194 				    && len--; s += k) {
195                                         if (*s == '\0')
196                                                 break;
197                                         if (isprint((unsigned char)*s))
198                                                 vbuf[j++] = *s;
199                                 }
200                                 if (j == sizeof(vbuf))
201                                         --j;
202                                 vbuf[j] = '\0';
203                                 if (NOTMIME(ms)) {
204                                         if (vbuf[0]) {
205                                                 if (file_printf(ms, ", %s: %s",
206                                                     buf, vbuf) == -1)
207                                                         return -1;
208                                         }
209                                 } else if (str == NULL && info[i].pi_id ==
210 				    CDF_PROPERTY_NAME_OF_APPLICATION) {
211 					str = cdf_app_to_mime(vbuf, app2mime);
212 				}
213 			}
214                         break;
215                 case CDF_FILETIME:
216                         tp = info[i].pi_tp;
217                         if (tp != 0) {
218 				char tbuf[64];
219                                 if (tp < 1000000000000000LL) {
220                                         cdf_print_elapsed_time(tbuf,
221                                             sizeof(tbuf), tp);
222                                         if (NOTMIME(ms) && file_printf(ms,
223                                             ", %s: %s", buf, tbuf) == -1)
224                                                 return -1;
225                                 } else {
226                                         char *c, *ec;
227 					const time_t sec = ts.tv_sec;
228                                         if (cdf_timestamp_to_timespec(&ts, tp) == -1) {
229 											return -1;
230 										}
231                                         c = cdf_ctime(&sec, tbuf);
232                                         if (c != NULL &&
233 					    (ec = strchr(c, '\n')) != NULL)
234 						*ec = '\0';
235 
236                                         if (NOTMIME(ms) && file_printf(ms,
237                                             ", %s: %s", buf, c) == -1)
238                                                 return -1;
239                                 }
240                         }
241                         break;
242                 case CDF_CLIPBOARD:
243                         break;
244                 default:
245                         return -1;
246                 }
247         }
248         if (!NOTMIME(ms)) {
249 		if (str == NULL)
250 			return 0;
251                 if (file_printf(ms, "application/%s", str) == -1)
252                         return -1;
253         }
254         return 1;
255 }
256 
257 private int
cdf_file_catalog(struct magic_set * ms,const cdf_header_t * h,const cdf_stream_t * sst)258 cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
259     const cdf_stream_t *sst)
260 {
261 	cdf_catalog_t *cat;
262 	size_t i;
263 	char buf[256];
264 	cdf_catalog_entry_t *ce;
265 
266         if (NOTMIME(ms)) {
267 		if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
268 			return -1;
269 		if (cdf_unpack_catalog(h, sst, &cat) == -1)
270 			return -1;
271 		ce = cat->cat_e;
272 		/* skip first entry since it has a , or paren */
273 		for (i = 1; i < cat->cat_num; i++)
274 			if (file_printf(ms, "%s%s",
275 			    cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
276 			    i == cat->cat_num - 1 ? "]" : ", ") == -1) {
277 				free(cat);
278 				return -1;
279 			}
280 		free(cat);
281 	} else {
282 		if (file_printf(ms, "application/CDFV2") == -1)
283 			return -1;
284 	}
285 	return 1;
286 }
287 
288 private int
cdf_file_summary_info(struct magic_set * ms,const cdf_header_t * h,const cdf_stream_t * sst,const cdf_directory_t * root_storage)289 cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
290     const cdf_stream_t *sst, const cdf_directory_t *root_storage)
291 {
292         cdf_summary_info_header_t si;
293         cdf_property_info_t *info;
294         size_t count;
295         int m;
296 
297         if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
298                 return -1;
299 
300         if (NOTMIME(ms)) {
301 		const char *str;
302 
303                 if (file_printf(ms, "Composite Document File V2 Document")
304 		    == -1)
305                         return -1;
306 
307                 if (file_printf(ms, ", %s Endian",
308                     si.si_byte_order == 0xfffe ?  "Little" : "Big") == -1)
309                         return -2;
310                 switch (si.si_os) {
311                 case 2:
312                         if (file_printf(ms, ", Os: Windows, Version %d.%d",
313                             si.si_os_version & 0xff,
314                             (uint32_t)si.si_os_version >> 8) == -1)
315                                 return -2;
316                         break;
317                 case 1:
318                         if (file_printf(ms, ", Os: MacOS, Version %d.%d",
319                             (uint32_t)si.si_os_version >> 8,
320                             si.si_os_version & 0xff) == -1)
321                                 return -2;
322                         break;
323                 default:
324                         if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
325                             si.si_os_version & 0xff,
326                             (uint32_t)si.si_os_version >> 8) == -1)
327                                 return -2;
328                         break;
329                 }
330 		if (root_storage) {
331 			str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
332 			    clsid2desc);
333 			if (str) {
334 				if (file_printf(ms, ", %s", str) == -1)
335 					return -2;
336 			}
337 		}
338 	}
339 
340         m = cdf_file_property_info(ms, info, count, root_storage);
341         free(info);
342 
343         return m == -1 ? -2 : m;
344 }
345 
346 #ifdef notdef
347 private char *
format_clsid(char * buf,size_t len,const uint64_t uuid[2])348 format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
349 	snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
350 	    PRIx64 "-%.12" PRIx64,
351 	    (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
352 	    (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
353 	    (uuid[0] >>  0) & (uint64_t)0x0000000000000ffffULL,
354 	    (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
355 	    (uuid[1] >>  0) & (uint64_t)0x0000fffffffffffffULL);
356 	return buf;
357 }
358 #endif
359 
360 private int
cdf_file_catalog_info(struct magic_set * ms,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)361 cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info,
362     const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
363     const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn)
364 {
365 	int i;
366 
367 	if ((i = cdf_read_user_stream(info, h, sat, ssat, sst,
368 	    dir, "Catalog", scn)) == -1)
369 		return i;
370 #ifdef CDF_DEBUG
371 	cdf_dump_catalog(h, scn);
372 #endif
373 	if ((i = cdf_file_catalog(ms, h, scn)) == -1)
374 		return -1;
375 	return i;
376 }
377 
378 private int
cdf_check_summary_info(struct magic_set * ms,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,const cdf_directory_t * root_storage,const char ** expn)379 cdf_check_summary_info(struct magic_set *ms, const cdf_info_t *info,
380     const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
381     const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn,
382     const cdf_directory_t *root_storage, const char **expn)
383 {
384 	int i;
385 	const char *str = NULL;
386 	cdf_directory_t *d;
387 	char name[__arraycount(d->d_name)];
388 	size_t j, k;
389 
390 #ifdef CDF_DEBUG
391         cdf_dump_summary_info(h, scn);
392 #endif
393         if ((i = cdf_file_summary_info(ms, h, scn, root_storage)) < 0) {
394             *expn = "Can't expand summary_info";
395 	    return i;
396 	}
397 	if (i == 1)
398 		return i;
399 	for (j = 0; str == NULL && j < dir->dir_len; j++) {
400 		d = &dir->dir_tab[j];
401 		for (k = 0; k < sizeof(name); k++)
402 			name[k] = (char)cdf_tole2(d->d_name[k]);
403 		str = cdf_app_to_mime(name,
404 				      NOTMIME(ms) ? name2desc : name2mime);
405 	}
406 	if (NOTMIME(ms)) {
407 		if (str != NULL) {
408 			if (file_printf(ms, "%s", str) == -1)
409 				return -1;
410 			i = 1;
411 		}
412 	} else {
413 		if (str == NULL)
414 			str = "vnd.ms-office";
415 		if (file_printf(ms, "application/%s", str) == -1)
416 			return -1;
417 		i = 1;
418 	}
419 	if (i <= 0) {
420 		i = cdf_file_catalog_info(ms, info, h, sat, ssat, sst,
421 					  dir, scn);
422 	}
423 	return i;
424 }
425 
426 private struct sinfo {
427 	const char *name;
428 	const char *mime;
429 	const char *sections[5];
430 	const int  types[5];
431 } sectioninfo[] = {
432 	{ "Encrypted", "encrypted",
433 		{
434 			"EncryptedPackage", "EncryptedSummary",
435 			NULL, NULL, NULL,
436 		},
437 		{
438 			CDF_DIR_TYPE_USER_STREAM,
439 			CDF_DIR_TYPE_USER_STREAM,
440 			0, 0, 0,
441 
442 		},
443 	},
444 	{ "QuickBooks", "quickbooks",
445 		{
446 #if 0
447 			"TaxForms", "PDFTaxForms", "modulesInBackup",
448 #endif
449 			"mfbu_header", NULL, NULL, NULL, NULL,
450 		},
451 		{
452 #if 0
453 			CDF_DIR_TYPE_USER_STORAGE,
454 			CDF_DIR_TYPE_USER_STORAGE,
455 			CDF_DIR_TYPE_USER_STREAM,
456 #endif
457 			CDF_DIR_TYPE_USER_STREAM,
458 			0, 0, 0, 0
459 		},
460 	},
461 	{ "Microsoft Excel", "vnd.ms-excel",
462 		{
463 			"Book", "Workbook", NULL, NULL, NULL,
464 		},
465 		{
466 			CDF_DIR_TYPE_USER_STREAM,
467 			CDF_DIR_TYPE_USER_STREAM,
468 			0, 0, 0,
469 		},
470 	},
471 	{ "Microsoft Word", "msword",
472 		{
473 			"WordDocument", NULL, NULL, NULL, NULL,
474 		},
475 		{
476 			CDF_DIR_TYPE_USER_STREAM,
477 			0, 0, 0, 0,
478 		},
479 	},
480 	{ "Microsoft PowerPoint", "vnd.ms-powerpoint",
481 		{
482 			"PowerPoint", NULL, NULL, NULL, NULL,
483 		},
484 		{
485 			CDF_DIR_TYPE_USER_STREAM,
486 			0, 0, 0, 0,
487 		},
488 	},
489 	{ "Microsoft Outlook Message", "vnd.ms-outlook",
490 		{
491 			"__properties_version1.0",
492 			"__recip_version1.0_#00000000",
493 			NULL, NULL, NULL,
494 		},
495 		{
496 			CDF_DIR_TYPE_USER_STREAM,
497 			CDF_DIR_TYPE_USER_STORAGE,
498 			0, 0, 0,
499 		},
500 	},
501 };
502 
503 private int
cdf_file_dir_info(struct magic_set * ms,const cdf_dir_t * dir)504 cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir)
505 {
506 	size_t sd, j;
507 
508 	for (sd = 0; sd < __arraycount(sectioninfo); sd++) {
509 		const struct sinfo *si = &sectioninfo[sd];
510 		for (j = 0; si->sections[j]; j++) {
511 			if (cdf_find_stream(dir, si->sections[j], si->types[j])
512 			    > 0)
513 				break;
514 #ifdef CDF_DEBUG
515 			fprintf(stderr, "Can't read %s\n", si->sections[j]);
516 #endif
517 		}
518 		if (si->sections[j] == NULL)
519 			continue;
520 		if (NOTMIME(ms)) {
521 			if (file_printf(ms, "CDFV2 %s", si->name) == -1)
522 				return -1;
523 		} else {
524 			if (file_printf(ms, "application/%s", si->mime) == -1)
525 				return -1;
526 		}
527 		return 1;
528 	}
529 	return -1;
530 }
531 
532 protected int
file_trycdf(struct magic_set * ms,int fd,const unsigned char * buf,size_t nbytes)533 file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
534     size_t nbytes)
535 {
536         cdf_info_t info;
537         cdf_header_t h;
538         cdf_sat_t sat, ssat;
539         cdf_stream_t sst, scn;
540         cdf_dir_t dir;
541         int i;
542         const char *expn = "";
543         const cdf_directory_t *root_storage;
544 
545         scn.sst_tab = NULL;
546         info.i_fd = fd;
547         info.i_buf = buf;
548         info.i_len = nbytes;
549         if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
550                 return 0;
551         if (cdf_read_header(&info, &h) == -1)
552                 return 0;
553 #ifdef CDF_DEBUG
554         cdf_dump_header(&h);
555 #endif
556 
557         if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
558                 expn = "Can't read SAT";
559                 goto out0;
560         }
561 #ifdef CDF_DEBUG
562         cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
563 #endif
564 
565         if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
566                 expn = "Can't read SSAT";
567                 goto out1;
568         }
569 #ifdef CDF_DEBUG
570         cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
571 #endif
572 
573         if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
574                 expn = "Can't read directory";
575                 goto out2;
576         }
577 
578         if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
579 	    &root_storage)) == -1) {
580                 expn = "Cannot read short stream";
581                 goto out3;
582         }
583 #ifdef CDF_DEBUG
584         cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
585 #endif
586 #ifdef notdef
587 	if (root_storage) {
588 		if (NOTMIME(ms)) {
589 			char clsbuf[128];
590 			if (file_printf(ms, "CLSID %s, ",
591 			    format_clsid(clsbuf, sizeof(clsbuf),
592 			    root_storage->d_storage_uuid)) == -1)
593 				return -1;
594 		}
595 	}
596 #endif
597 
598 	if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
599 	    "FileHeader", &scn)) != -1) {
600 #define HWP5_SIGNATURE "HWP Document File"
601 		if (scn.sst_len * scn.sst_ss >= sizeof(HWP5_SIGNATURE) - 1
602 		    && memcmp(scn.sst_tab, HWP5_SIGNATURE,
603 		    sizeof(HWP5_SIGNATURE) - 1) == 0) {
604 		    if (NOTMIME(ms)) {
605 			if (file_printf(ms,
606 			    "Hangul (Korean) Word Processor File 5.x") == -1)
607 			    return -1;
608 		    } else {
609 			if (file_printf(ms, "application/x-hwp") == -1)
610 			    return -1;
611 		    }
612 		    i = 1;
613 		    goto out5;
614 		} else {
615 		    cdf_zero_stream(&scn);
616 		}
617 	}
618 
619         if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
620             &scn)) == -1) {
621                 if (errno != ESRCH) {
622                         expn = "Cannot read summary info";
623 		}
624 	} else {
625 		i = cdf_check_summary_info(ms, &info, &h,
626 		    &sat, &ssat, &sst, &dir, &scn, root_storage, &expn);
627 		cdf_zero_stream(&scn);
628 	}
629 	if (i <= 0) {
630 		if ((i = cdf_read_doc_summary_info(&info, &h, &sat, &ssat,
631 		    &sst, &dir, &scn)) == -1) {
632 			if (errno != ESRCH) {
633 				expn = "Cannot read summary info";
634 			}
635 		} else {
636 			i = cdf_check_summary_info(ms, &info, &h, &sat, &ssat,
637 			    &sst, &dir, &scn, root_storage, &expn);
638 		}
639 	}
640 	if (i <= 0) {
641 		i = cdf_file_dir_info(ms, &dir);
642 		if (i < 0)
643 			expn = "Cannot read section info";
644 	}
645 out5:
646 	cdf_zero_stream(&scn);
647 	cdf_zero_stream(&sst);
648 out3:
649         free(dir.dir_tab);
650 out2:
651         free(ssat.sat_tab);
652 out1:
653         free(sat.sat_tab);
654 out0:
655 	if (i == -1) {
656 	    if (NOTMIME(ms)) {
657 		if (file_printf(ms,
658 		    "Composite Document File V2 Document") == -1)
659 		    return -1;
660 		if (*expn)
661 		    if (file_printf(ms, ", %s", expn) == -1)
662 			return -1;
663 	    } else {
664 		if (file_printf(ms, "application/CDFV2") == -1)
665 		    return -1;
666 	    }
667 	    i = 1;
668 	}
669         return i;
670 }
671