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