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