xref: /freebsd/contrib/file/src/readcdf.c (revision 5f0216bd883edee71bf81051e3c20505e4820903)
1b6cee71dSXin LI /*-
2b6cee71dSXin LI  * Copyright (c) 2008 Christos Zoulas
3b6cee71dSXin LI  * All rights reserved.
4b6cee71dSXin LI  *
5b6cee71dSXin LI  * Redistribution and use in source and binary forms, with or without
6b6cee71dSXin LI  * modification, are permitted provided that the following conditions
7b6cee71dSXin LI  * are met:
8b6cee71dSXin LI  * 1. Redistributions of source code must retain the above copyright
9b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer.
10b6cee71dSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
11b6cee71dSXin LI  *    notice, this list of conditions and the following disclaimer in the
12b6cee71dSXin LI  *    documentation and/or other materials provided with the distribution.
13b6cee71dSXin LI  *
14b6cee71dSXin LI  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15b6cee71dSXin LI  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16b6cee71dSXin LI  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17b6cee71dSXin LI  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18b6cee71dSXin LI  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19b6cee71dSXin LI  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20b6cee71dSXin LI  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21b6cee71dSXin LI  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22b6cee71dSXin LI  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23b6cee71dSXin LI  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24b6cee71dSXin LI  * POSSIBILITY OF SUCH DAMAGE.
25b6cee71dSXin LI  */
26b6cee71dSXin LI #include "file.h"
27b6cee71dSXin LI 
28b6cee71dSXin LI #ifndef lint
29*5f0216bdSXin LI FILE_RCSID("@(#)$File: readcdf.c,v 1.53 2015/04/09 20:01:41 christos Exp $")
30b6cee71dSXin LI #endif
31b6cee71dSXin LI 
32b6cee71dSXin LI #include <assert.h>
33b6cee71dSXin LI #include <stdlib.h>
34b6cee71dSXin LI #include <unistd.h>
35b6cee71dSXin LI #include <string.h>
36b6cee71dSXin LI #include <time.h>
37b6cee71dSXin LI #include <ctype.h>
38b6cee71dSXin LI 
39b6cee71dSXin LI #include "cdf.h"
40b6cee71dSXin LI #include "magic.h"
41b6cee71dSXin LI 
42*5f0216bdSXin LI #ifndef __arraycount
43*5f0216bdSXin LI #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
44*5f0216bdSXin LI #endif
45*5f0216bdSXin LI 
46b6cee71dSXin LI #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
47b6cee71dSXin LI 
48b6cee71dSXin LI static const struct nv {
49b6cee71dSXin LI 	const char *pattern;
50b6cee71dSXin LI 	const char *mime;
51b6cee71dSXin LI } app2mime[] =  {
52b6cee71dSXin LI 	{ "Word",			"msword",		},
53b6cee71dSXin LI 	{ "Excel",			"vnd.ms-excel",		},
54b6cee71dSXin LI 	{ "Powerpoint",			"vnd.ms-powerpoint",	},
55b6cee71dSXin LI 	{ "Crystal Reports",		"x-rpt",		},
56b6cee71dSXin LI 	{ "Advanced Installer",		"vnd.ms-msi",		},
57b6cee71dSXin LI 	{ "InstallShield",		"vnd.ms-msi",		},
58b6cee71dSXin LI 	{ "Microsoft Patch Compiler",	"vnd.ms-msi",		},
59b6cee71dSXin LI 	{ "NAnt",			"vnd.ms-msi",		},
60b6cee71dSXin LI 	{ "Windows Installer",		"vnd.ms-msi",		},
61b6cee71dSXin LI 	{ NULL,				NULL,			},
62b6cee71dSXin LI }, name2mime[] = {
63b6cee71dSXin LI 	{ "WordDocument",		"msword",		},
64b6cee71dSXin LI 	{ "PowerPoint",			"vnd.ms-powerpoint",	},
65b6cee71dSXin LI 	{ "DigitalSignature",		"vnd.ms-msi",		},
66b6cee71dSXin LI 	{ NULL,				NULL,			},
67b6cee71dSXin LI }, name2desc[] = {
68b6cee71dSXin LI 	{ "WordDocument",		"Microsoft Office Word",},
69b6cee71dSXin LI 	{ "PowerPoint",			"Microsoft PowerPoint",	},
70b6cee71dSXin LI 	{ "DigitalSignature",		"Microsoft Installer",	},
71b6cee71dSXin LI 	{ NULL,				NULL,			},
72b6cee71dSXin LI };
73b6cee71dSXin LI 
74b6cee71dSXin LI static const struct cv {
75b6cee71dSXin LI 	uint64_t clsid[2];
76b6cee71dSXin LI 	const char *mime;
77b6cee71dSXin LI } clsid2mime[] = {
78b6cee71dSXin LI 	{
79c2931133SXin LI 		{ 0x00000000000c1084ULL, 0x46000000000000c0ULL  },
80b6cee71dSXin LI 		"x-msi",
81b6cee71dSXin LI 	},
82b6cee71dSXin LI 	{	{ 0,			 0			},
83b6cee71dSXin LI 		NULL,
84b6cee71dSXin LI 	},
85b6cee71dSXin LI }, clsid2desc[] = {
86b6cee71dSXin LI 	{
87c2931133SXin LI 		{ 0x00000000000c1084ULL, 0x46000000000000c0ULL  },
88b6cee71dSXin LI 		"MSI Installer",
89b6cee71dSXin LI 	},
90b6cee71dSXin LI 	{	{ 0,			 0			},
91b6cee71dSXin LI 		NULL,
92b6cee71dSXin LI 	},
93b6cee71dSXin LI };
94b6cee71dSXin LI 
95b6cee71dSXin LI private const char *
96b6cee71dSXin LI cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
97b6cee71dSXin LI {
98b6cee71dSXin LI 	size_t i;
99b6cee71dSXin LI 	for (i = 0; cv[i].mime != NULL; i++) {
100b6cee71dSXin LI 		if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
101b6cee71dSXin LI 			return cv[i].mime;
102b6cee71dSXin LI 	}
103*5f0216bdSXin LI #ifdef CDF_DEBUG
104*5f0216bdSXin LI 	fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0],
105*5f0216bdSXin LI 	    clsid[1]);
106*5f0216bdSXin LI #endif
107b6cee71dSXin LI 	return NULL;
108b6cee71dSXin LI }
109b6cee71dSXin LI 
110b6cee71dSXin LI private const char *
111b6cee71dSXin LI cdf_app_to_mime(const char *vbuf, const struct nv *nv)
112b6cee71dSXin LI {
113b6cee71dSXin LI 	size_t i;
114b6cee71dSXin LI 	const char *rv = NULL;
115c2931133SXin LI #ifdef USE_C_LOCALE
116c2931133SXin LI 	locale_t old_lc_ctype, c_lc_ctype;
117b6cee71dSXin LI 
118c2931133SXin LI 	c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
119c2931133SXin LI 	assert(c_lc_ctype != NULL);
120c2931133SXin LI 	old_lc_ctype = uselocale(c_lc_ctype);
121b6cee71dSXin LI 	assert(old_lc_ctype != NULL);
122c2931133SXin LI #endif
123b6cee71dSXin LI 	for (i = 0; nv[i].pattern != NULL; i++)
124b6cee71dSXin LI 		if (strcasestr(vbuf, nv[i].pattern) != NULL) {
125b6cee71dSXin LI 			rv = nv[i].mime;
126b6cee71dSXin LI 			break;
127b6cee71dSXin LI 		}
128*5f0216bdSXin LI #ifdef CDF_DEBUG
129*5f0216bdSXin LI 	fprintf(stderr, "unknown app %s\n", vbuf);
130*5f0216bdSXin LI #endif
131c2931133SXin LI #ifdef USE_C_LOCALE
132c2931133SXin LI 	(void)uselocale(old_lc_ctype);
133c2931133SXin LI 	freelocale(c_lc_ctype);
134c2931133SXin LI #endif
135b6cee71dSXin LI 	return rv;
136b6cee71dSXin LI }
137b6cee71dSXin LI 
138b6cee71dSXin LI private int
139b6cee71dSXin LI cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
140b6cee71dSXin LI     size_t count, const cdf_directory_t *root_storage)
141b6cee71dSXin LI {
142b6cee71dSXin LI         size_t i;
143b6cee71dSXin LI         cdf_timestamp_t tp;
144b6cee71dSXin LI         struct timespec ts;
145b6cee71dSXin LI         char buf[64];
146b6cee71dSXin LI         const char *str = NULL;
147b6cee71dSXin LI         const char *s;
148b6cee71dSXin LI         int len;
149b6cee71dSXin LI 
150b6cee71dSXin LI         if (!NOTMIME(ms) && root_storage)
151b6cee71dSXin LI 		str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
152b6cee71dSXin LI 		    clsid2mime);
153b6cee71dSXin LI 
154b6cee71dSXin LI         for (i = 0; i < count; i++) {
155b6cee71dSXin LI                 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
156b6cee71dSXin LI                 switch (info[i].pi_type) {
157b6cee71dSXin LI                 case CDF_NULL:
158b6cee71dSXin LI                         break;
159b6cee71dSXin LI                 case CDF_SIGNED16:
160b6cee71dSXin LI                         if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
161b6cee71dSXin LI                             info[i].pi_s16) == -1)
162b6cee71dSXin LI                                 return -1;
163b6cee71dSXin LI                         break;
164b6cee71dSXin LI                 case CDF_SIGNED32:
165b6cee71dSXin LI                         if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
166b6cee71dSXin LI                             info[i].pi_s32) == -1)
167b6cee71dSXin LI                                 return -1;
168b6cee71dSXin LI                         break;
169b6cee71dSXin LI                 case CDF_UNSIGNED32:
170b6cee71dSXin LI                         if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
171b6cee71dSXin LI                             info[i].pi_u32) == -1)
172b6cee71dSXin LI                                 return -1;
173b6cee71dSXin LI                         break;
174b6cee71dSXin LI                 case CDF_FLOAT:
175b6cee71dSXin LI                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
176b6cee71dSXin LI                             info[i].pi_f) == -1)
177b6cee71dSXin LI                                 return -1;
178b6cee71dSXin LI                         break;
179b6cee71dSXin LI                 case CDF_DOUBLE:
180b6cee71dSXin LI                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
181b6cee71dSXin LI                             info[i].pi_d) == -1)
182b6cee71dSXin LI                                 return -1;
183b6cee71dSXin LI                         break;
184b6cee71dSXin LI                 case CDF_LENGTH32_STRING:
185b6cee71dSXin LI                 case CDF_LENGTH32_WSTRING:
186b6cee71dSXin LI                         len = info[i].pi_str.s_len;
187b6cee71dSXin LI                         if (len > 1) {
188b6cee71dSXin LI                                 char vbuf[1024];
189b6cee71dSXin LI                                 size_t j, k = 1;
190b6cee71dSXin LI 
191b6cee71dSXin LI                                 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
192b6cee71dSXin LI                                     k++;
193b6cee71dSXin LI                                 s = info[i].pi_str.s_buf;
194b6cee71dSXin LI                                 for (j = 0; j < sizeof(vbuf) && len--; s += k) {
195b6cee71dSXin LI                                         if (*s == '\0')
196b6cee71dSXin LI                                                 break;
197b6cee71dSXin LI                                         if (isprint((unsigned char)*s))
198b6cee71dSXin LI                                                 vbuf[j++] = *s;
199b6cee71dSXin LI                                 }
200b6cee71dSXin LI                                 if (j == sizeof(vbuf))
201b6cee71dSXin LI                                         --j;
202b6cee71dSXin LI                                 vbuf[j] = '\0';
203b6cee71dSXin LI                                 if (NOTMIME(ms)) {
204b6cee71dSXin LI                                         if (vbuf[0]) {
205b6cee71dSXin LI                                                 if (file_printf(ms, ", %s: %s",
206b6cee71dSXin LI                                                     buf, vbuf) == -1)
207b6cee71dSXin LI                                                         return -1;
208b6cee71dSXin LI                                         }
209b6cee71dSXin LI                                 } else if (str == NULL && info[i].pi_id ==
210b6cee71dSXin LI 				    CDF_PROPERTY_NAME_OF_APPLICATION) {
211b6cee71dSXin LI 					str = cdf_app_to_mime(vbuf, app2mime);
212b6cee71dSXin LI 				}
213b6cee71dSXin LI 			}
214b6cee71dSXin LI                         break;
215b6cee71dSXin LI                 case CDF_FILETIME:
216b6cee71dSXin LI                         tp = info[i].pi_tp;
217b6cee71dSXin LI                         if (tp != 0) {
218b6cee71dSXin LI 				char tbuf[64];
219b6cee71dSXin LI                                 if (tp < 1000000000000000LL) {
220b6cee71dSXin LI                                         cdf_print_elapsed_time(tbuf,
221b6cee71dSXin LI                                             sizeof(tbuf), tp);
222b6cee71dSXin LI                                         if (NOTMIME(ms) && file_printf(ms,
223b6cee71dSXin LI                                             ", %s: %s", buf, tbuf) == -1)
224b6cee71dSXin LI                                                 return -1;
225b6cee71dSXin LI                                 } else {
226b6cee71dSXin LI                                         char *c, *ec;
227b6cee71dSXin LI                                         cdf_timestamp_to_timespec(&ts, tp);
228b6cee71dSXin LI                                         c = cdf_ctime(&ts.tv_sec, tbuf);
229b6cee71dSXin LI                                         if (c != NULL &&
230b6cee71dSXin LI 					    (ec = strchr(c, '\n')) != NULL)
231b6cee71dSXin LI 						*ec = '\0';
232b6cee71dSXin LI 
233b6cee71dSXin LI                                         if (NOTMIME(ms) && file_printf(ms,
234b6cee71dSXin LI                                             ", %s: %s", buf, c) == -1)
235b6cee71dSXin LI                                                 return -1;
236b6cee71dSXin LI                                 }
237b6cee71dSXin LI                         }
238b6cee71dSXin LI                         break;
239b6cee71dSXin LI                 case CDF_CLIPBOARD:
240b6cee71dSXin LI                         break;
241b6cee71dSXin LI                 default:
242b6cee71dSXin LI                         return -1;
243b6cee71dSXin LI                 }
244b6cee71dSXin LI         }
245b6cee71dSXin LI         if (!NOTMIME(ms)) {
246b6cee71dSXin LI 		if (str == NULL)
247b6cee71dSXin LI 			return 0;
248b6cee71dSXin LI                 if (file_printf(ms, "application/%s", str) == -1)
249b6cee71dSXin LI                         return -1;
250b6cee71dSXin LI         }
251b6cee71dSXin LI         return 1;
252b6cee71dSXin LI }
253b6cee71dSXin LI 
254b6cee71dSXin LI private int
255c2931133SXin LI cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
256c2931133SXin LI     const cdf_stream_t *sst)
257c2931133SXin LI {
258c2931133SXin LI 	cdf_catalog_t *cat;
259c2931133SXin LI 	size_t i;
260c2931133SXin LI 	char buf[256];
261c2931133SXin LI 	cdf_catalog_entry_t *ce;
262c2931133SXin LI 
263c2931133SXin LI         if (NOTMIME(ms)) {
264c2931133SXin LI 		if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
265c2931133SXin LI 			return -1;
266c2931133SXin LI 		if (cdf_unpack_catalog(h, sst, &cat) == -1)
267c2931133SXin LI 			return -1;
268c2931133SXin LI 		ce = cat->cat_e;
269c2931133SXin LI 		/* skip first entry since it has a , or paren */
270c2931133SXin LI 		for (i = 1; i < cat->cat_num; i++)
271c2931133SXin LI 			if (file_printf(ms, "%s%s",
272c2931133SXin LI 			    cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
273c2931133SXin LI 			    i == cat->cat_num - 1 ? "]" : ", ") == -1) {
274c2931133SXin LI 				free(cat);
275c2931133SXin LI 				return -1;
276c2931133SXin LI 			}
277c2931133SXin LI 		free(cat);
278c2931133SXin LI 	} else {
279c2931133SXin LI 		if (file_printf(ms, "application/CDFV2") == -1)
280c2931133SXin LI 			return -1;
281c2931133SXin LI 	}
282c2931133SXin LI 	return 1;
283c2931133SXin LI }
284c2931133SXin LI 
285c2931133SXin LI private int
286b6cee71dSXin LI cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
287b6cee71dSXin LI     const cdf_stream_t *sst, const cdf_directory_t *root_storage)
288b6cee71dSXin LI {
289b6cee71dSXin LI         cdf_summary_info_header_t si;
290b6cee71dSXin LI         cdf_property_info_t *info;
291b6cee71dSXin LI         size_t count;
292b6cee71dSXin LI         int m;
293b6cee71dSXin LI 
294b6cee71dSXin LI         if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
295b6cee71dSXin LI                 return -1;
296b6cee71dSXin LI 
297b6cee71dSXin LI         if (NOTMIME(ms)) {
298b6cee71dSXin LI 		const char *str;
299b6cee71dSXin LI 
300b6cee71dSXin LI                 if (file_printf(ms, "Composite Document File V2 Document")
301b6cee71dSXin LI 		    == -1)
302b6cee71dSXin LI                         return -1;
303b6cee71dSXin LI 
304b6cee71dSXin LI                 if (file_printf(ms, ", %s Endian",
305b6cee71dSXin LI                     si.si_byte_order == 0xfffe ?  "Little" : "Big") == -1)
306b6cee71dSXin LI                         return -2;
307b6cee71dSXin LI                 switch (si.si_os) {
308b6cee71dSXin LI                 case 2:
309b6cee71dSXin LI                         if (file_printf(ms, ", Os: Windows, Version %d.%d",
310b6cee71dSXin LI                             si.si_os_version & 0xff,
311b6cee71dSXin LI                             (uint32_t)si.si_os_version >> 8) == -1)
312b6cee71dSXin LI                                 return -2;
313b6cee71dSXin LI                         break;
314b6cee71dSXin LI                 case 1:
315b6cee71dSXin LI                         if (file_printf(ms, ", Os: MacOS, Version %d.%d",
316b6cee71dSXin LI                             (uint32_t)si.si_os_version >> 8,
317b6cee71dSXin LI                             si.si_os_version & 0xff) == -1)
318b6cee71dSXin LI                                 return -2;
319b6cee71dSXin LI                         break;
320b6cee71dSXin LI                 default:
321b6cee71dSXin LI                         if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
322b6cee71dSXin LI                             si.si_os_version & 0xff,
323b6cee71dSXin LI                             (uint32_t)si.si_os_version >> 8) == -1)
324b6cee71dSXin LI                                 return -2;
325b6cee71dSXin LI                         break;
326b6cee71dSXin LI                 }
327b6cee71dSXin LI 		if (root_storage) {
328b6cee71dSXin LI 			str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
329b6cee71dSXin LI 			    clsid2desc);
330c2931133SXin LI 			if (str) {
331b6cee71dSXin LI 				if (file_printf(ms, ", %s", str) == -1)
332b6cee71dSXin LI 					return -2;
333b6cee71dSXin LI 			}
334b6cee71dSXin LI 		}
335c2931133SXin LI 	}
336b6cee71dSXin LI 
337b6cee71dSXin LI         m = cdf_file_property_info(ms, info, count, root_storage);
338b6cee71dSXin LI         free(info);
339b6cee71dSXin LI 
340b6cee71dSXin LI         return m == -1 ? -2 : m;
341b6cee71dSXin LI }
342b6cee71dSXin LI 
343b6cee71dSXin LI #ifdef notdef
344b6cee71dSXin LI private char *
345b6cee71dSXin LI format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
346b6cee71dSXin LI 	snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
347b6cee71dSXin LI 	    PRIx64 "-%.12" PRIx64,
348c2931133SXin LI 	    (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
349c2931133SXin LI 	    (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
350c2931133SXin LI 	    (uuid[0] >>  0) & (uint64_t)0x0000000000000ffffULL,
351c2931133SXin LI 	    (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
352c2931133SXin LI 	    (uuid[1] >>  0) & (uint64_t)0x0000fffffffffffffULL);
353b6cee71dSXin LI 	return buf;
354b6cee71dSXin LI }
355b6cee71dSXin LI #endif
356b6cee71dSXin LI 
357*5f0216bdSXin LI private int
358*5f0216bdSXin LI cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info,
359*5f0216bdSXin LI     const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
360*5f0216bdSXin LI     const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn)
361*5f0216bdSXin LI {
362*5f0216bdSXin LI 	int i;
363*5f0216bdSXin LI 
364*5f0216bdSXin LI 	if ((i = cdf_read_user_stream(info, h, sat, ssat, sst,
365*5f0216bdSXin LI 	    dir, "Catalog", scn)) == -1)
366*5f0216bdSXin LI 		return i;
367*5f0216bdSXin LI #ifdef CDF_DEBUG
368*5f0216bdSXin LI 	cdf_dump_catalog(&h, &scn);
369*5f0216bdSXin LI #endif
370*5f0216bdSXin LI 	if ((i = cdf_file_catalog(ms, h, scn)) == -1)
371*5f0216bdSXin LI 		return -1;
372*5f0216bdSXin LI 	return i;
373*5f0216bdSXin LI }
374*5f0216bdSXin LI 
375*5f0216bdSXin LI private struct sinfo {
376*5f0216bdSXin LI 	const char *name;
377*5f0216bdSXin LI 	const char *mime;
378*5f0216bdSXin LI 	const char *sections[5];
379*5f0216bdSXin LI 	const int  types[5];
380*5f0216bdSXin LI } sectioninfo[] = {
381*5f0216bdSXin LI 	{ "Encrypted", "encrypted",
382*5f0216bdSXin LI 		{
383*5f0216bdSXin LI 			"EncryptedPackage", NULL, NULL, NULL, NULL,
384*5f0216bdSXin LI 		},
385*5f0216bdSXin LI 		{
386*5f0216bdSXin LI 			CDF_DIR_TYPE_USER_STREAM, 0, 0, 0, 0,
387*5f0216bdSXin LI 
388*5f0216bdSXin LI 		},
389*5f0216bdSXin LI 	},
390*5f0216bdSXin LI 	{ "QuickBooks", "quickbooks",
391*5f0216bdSXin LI 		{
392*5f0216bdSXin LI #if 0
393*5f0216bdSXin LI 			"TaxForms", "PDFTaxForms", "modulesInBackup",
394*5f0216bdSXin LI #endif
395*5f0216bdSXin LI 			"mfbu_header", NULL, NULL, NULL, NULL,
396*5f0216bdSXin LI 		},
397*5f0216bdSXin LI 		{
398*5f0216bdSXin LI #if 0
399*5f0216bdSXin LI 			CDF_DIR_TYPE_USER_STORAGE,
400*5f0216bdSXin LI 			CDF_DIR_TYPE_USER_STORAGE,
401*5f0216bdSXin LI 			CDF_DIR_TYPE_USER_STREAM,
402*5f0216bdSXin LI #endif
403*5f0216bdSXin LI 			CDF_DIR_TYPE_USER_STREAM,
404*5f0216bdSXin LI 			0, 0, 0, 0
405*5f0216bdSXin LI 		},
406*5f0216bdSXin LI 	},
407*5f0216bdSXin LI };
408*5f0216bdSXin LI 
409*5f0216bdSXin LI private int
410*5f0216bdSXin LI cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir)
411*5f0216bdSXin LI {
412*5f0216bdSXin LI 	size_t sd, j;
413*5f0216bdSXin LI 
414*5f0216bdSXin LI 	for (sd = 0; sd < __arraycount(sectioninfo); sd++) {
415*5f0216bdSXin LI 		const struct sinfo *si = &sectioninfo[sd];
416*5f0216bdSXin LI 		for (j = 0; si->sections[j]; j++) {
417*5f0216bdSXin LI 			if (cdf_find_stream(dir, si->sections[j], si->types[j])
418*5f0216bdSXin LI 			    <= 0) {
419*5f0216bdSXin LI #ifdef CDF_DEBUG
420*5f0216bdSXin LI 				fprintf(stderr, "Can't read %s\n",
421*5f0216bdSXin LI 				    si->sections[j]);
422*5f0216bdSXin LI #endif
423*5f0216bdSXin LI 				break;
424*5f0216bdSXin LI 			}
425*5f0216bdSXin LI 		}
426*5f0216bdSXin LI 		if (si->sections[j] != NULL)
427*5f0216bdSXin LI 			continue;
428*5f0216bdSXin LI 		if (NOTMIME(ms)) {
429*5f0216bdSXin LI 			if (file_printf(ms, "CDFV2 %s", si->name) == -1)
430*5f0216bdSXin LI 				return -1;
431*5f0216bdSXin LI 		} else {
432*5f0216bdSXin LI 			if (file_printf(ms, "application/CDFV2-%s",
433*5f0216bdSXin LI 			    si->mime) == -1)
434*5f0216bdSXin LI 				return -1;
435*5f0216bdSXin LI 		}
436*5f0216bdSXin LI 		return 1;
437*5f0216bdSXin LI 	}
438*5f0216bdSXin LI 	return -1;
439*5f0216bdSXin LI }
440*5f0216bdSXin LI 
441b6cee71dSXin LI protected int
442b6cee71dSXin LI file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
443b6cee71dSXin LI     size_t nbytes)
444b6cee71dSXin LI {
445b6cee71dSXin LI         cdf_info_t info;
446b6cee71dSXin LI         cdf_header_t h;
447b6cee71dSXin LI         cdf_sat_t sat, ssat;
448b6cee71dSXin LI         cdf_stream_t sst, scn;
449b6cee71dSXin LI         cdf_dir_t dir;
450b6cee71dSXin LI         int i;
451b6cee71dSXin LI         const char *expn = "";
452c2931133SXin LI         const cdf_directory_t *root_storage;
453b6cee71dSXin LI 
454b6cee71dSXin LI         info.i_fd = fd;
455b6cee71dSXin LI         info.i_buf = buf;
456b6cee71dSXin LI         info.i_len = nbytes;
457*5f0216bdSXin LI         if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
458b6cee71dSXin LI                 return 0;
459b6cee71dSXin LI         if (cdf_read_header(&info, &h) == -1)
460b6cee71dSXin LI                 return 0;
461b6cee71dSXin LI #ifdef CDF_DEBUG
462b6cee71dSXin LI         cdf_dump_header(&h);
463b6cee71dSXin LI #endif
464b6cee71dSXin LI 
465b6cee71dSXin LI         if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
466b6cee71dSXin LI                 expn = "Can't read SAT";
467b6cee71dSXin LI                 goto out0;
468b6cee71dSXin LI         }
469b6cee71dSXin LI #ifdef CDF_DEBUG
470b6cee71dSXin LI         cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
471b6cee71dSXin LI #endif
472b6cee71dSXin LI 
473b6cee71dSXin LI         if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
474b6cee71dSXin LI                 expn = "Can't read SSAT";
475b6cee71dSXin LI                 goto out1;
476b6cee71dSXin LI         }
477b6cee71dSXin LI #ifdef CDF_DEBUG
478b6cee71dSXin LI         cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
479b6cee71dSXin LI #endif
480b6cee71dSXin LI 
481b6cee71dSXin LI         if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
482b6cee71dSXin LI                 expn = "Can't read directory";
483b6cee71dSXin LI                 goto out2;
484b6cee71dSXin LI         }
485b6cee71dSXin LI 
486b6cee71dSXin LI         if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
487b6cee71dSXin LI 	    &root_storage)) == -1) {
488b6cee71dSXin LI                 expn = "Cannot read short stream";
489b6cee71dSXin LI                 goto out3;
490b6cee71dSXin LI         }
491b6cee71dSXin LI #ifdef CDF_DEBUG
492b6cee71dSXin LI         cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
493b6cee71dSXin LI #endif
494b6cee71dSXin LI #ifdef notdef
495b6cee71dSXin LI 	if (root_storage) {
496b6cee71dSXin LI 		if (NOTMIME(ms)) {
497b6cee71dSXin LI 			char clsbuf[128];
498b6cee71dSXin LI 			if (file_printf(ms, "CLSID %s, ",
499b6cee71dSXin LI 			    format_clsid(clsbuf, sizeof(clsbuf),
500b6cee71dSXin LI 			    root_storage->d_storage_uuid)) == -1)
501b6cee71dSXin LI 				return -1;
502b6cee71dSXin LI 		}
503b6cee71dSXin LI 	}
504b6cee71dSXin LI #endif
505b6cee71dSXin LI 
506b6cee71dSXin LI 	if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
507b6cee71dSXin LI 	    "FileHeader", &scn)) != -1) {
508b6cee71dSXin LI #define HWP5_SIGNATURE "HWP Document File"
509b6cee71dSXin LI 		if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1
510b6cee71dSXin LI 		    && memcmp(scn.sst_tab, HWP5_SIGNATURE,
511b6cee71dSXin LI 		    sizeof(HWP5_SIGNATURE) - 1) == 0) {
512b6cee71dSXin LI 		    if (NOTMIME(ms)) {
513b6cee71dSXin LI 			if (file_printf(ms,
514b6cee71dSXin LI 			    "Hangul (Korean) Word Processor File 5.x") == -1)
515b6cee71dSXin LI 			    return -1;
516b6cee71dSXin LI 		    } else {
517b6cee71dSXin LI 			if (file_printf(ms, "application/x-hwp") == -1)
518b6cee71dSXin LI 			    return -1;
519b6cee71dSXin LI 		    }
520b6cee71dSXin LI 		    i = 1;
521b6cee71dSXin LI 		    goto out5;
522b6cee71dSXin LI 		} else {
523b6cee71dSXin LI 		    free(scn.sst_tab);
524b6cee71dSXin LI 		    scn.sst_tab = NULL;
525b6cee71dSXin LI 		    scn.sst_len = 0;
526b6cee71dSXin LI 		    scn.sst_dirlen = 0;
527b6cee71dSXin LI 		}
528b6cee71dSXin LI 	}
529b6cee71dSXin LI 
530b6cee71dSXin LI         if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
531b6cee71dSXin LI             &scn)) == -1) {
532*5f0216bdSXin LI                 if (errno != ESRCH) {
533b6cee71dSXin LI                         expn = "Cannot read summary info";
534b6cee71dSXin LI 			goto out4;
535b6cee71dSXin LI 		}
536*5f0216bdSXin LI 		i = cdf_file_catalog_info(ms, &info, &h, &sat, &ssat, &sst,
537*5f0216bdSXin LI 		    &dir, &scn);
538*5f0216bdSXin LI 		if (i > 0)
539*5f0216bdSXin LI 			goto out4;
540*5f0216bdSXin LI 		i = cdf_file_dir_info(ms, &dir);
541*5f0216bdSXin LI 		if (i < 0)
542*5f0216bdSXin LI                         expn = "Cannot read section info";
543*5f0216bdSXin LI 		goto out4;
544*5f0216bdSXin LI 	}
545*5f0216bdSXin LI 
546*5f0216bdSXin LI 
547b6cee71dSXin LI #ifdef CDF_DEBUG
548b6cee71dSXin LI         cdf_dump_summary_info(&h, &scn);
549b6cee71dSXin LI #endif
550b6cee71dSXin LI         if ((i = cdf_file_summary_info(ms, &h, &scn, root_storage)) < 0)
551b6cee71dSXin LI             expn = "Can't expand summary_info";
552b6cee71dSXin LI 
553b6cee71dSXin LI 	if (i == 0) {
554b6cee71dSXin LI 		const char *str = NULL;
555b6cee71dSXin LI 		cdf_directory_t *d;
556b6cee71dSXin LI 		char name[__arraycount(d->d_name)];
557b6cee71dSXin LI 		size_t j, k;
558b6cee71dSXin LI 
559b6cee71dSXin LI 		for (j = 0; str == NULL && j < dir.dir_len; j++) {
560b6cee71dSXin LI 			d = &dir.dir_tab[j];
561b6cee71dSXin LI 			for (k = 0; k < sizeof(name); k++)
562b6cee71dSXin LI 				name[k] = (char)cdf_tole2(d->d_name[k]);
563b6cee71dSXin LI 			str = cdf_app_to_mime(name,
564b6cee71dSXin LI 			    NOTMIME(ms) ? name2desc : name2mime);
565b6cee71dSXin LI 		}
566b6cee71dSXin LI 		if (NOTMIME(ms)) {
567b6cee71dSXin LI 			if (str != NULL) {
568b6cee71dSXin LI 				if (file_printf(ms, "%s", str) == -1)
569b6cee71dSXin LI 					return -1;
570b6cee71dSXin LI 				i = 1;
571b6cee71dSXin LI 			}
572b6cee71dSXin LI 		} else {
573b6cee71dSXin LI 			if (str == NULL)
574b6cee71dSXin LI 				str = "vnd.ms-office";
575b6cee71dSXin LI 			if (file_printf(ms, "application/%s", str) == -1)
576b6cee71dSXin LI 				return -1;
577b6cee71dSXin LI 			i = 1;
578b6cee71dSXin LI 		}
579b6cee71dSXin LI 	}
580b6cee71dSXin LI out5:
581b6cee71dSXin LI         free(scn.sst_tab);
582b6cee71dSXin LI out4:
583b6cee71dSXin LI         free(sst.sst_tab);
584b6cee71dSXin LI out3:
585b6cee71dSXin LI         free(dir.dir_tab);
586b6cee71dSXin LI out2:
587b6cee71dSXin LI         free(ssat.sat_tab);
588b6cee71dSXin LI out1:
589b6cee71dSXin LI         free(sat.sat_tab);
590b6cee71dSXin LI out0:
591b6cee71dSXin LI 	if (i == -1) {
592b6cee71dSXin LI 	    if (NOTMIME(ms)) {
593b6cee71dSXin LI 		if (file_printf(ms,
594b6cee71dSXin LI 		    "Composite Document File V2 Document") == -1)
595b6cee71dSXin LI 		    return -1;
596b6cee71dSXin LI 		if (*expn)
597*5f0216bdSXin LI 		    if (file_printf(ms, ", %s", expn) == -1)
598b6cee71dSXin LI 			return -1;
599b6cee71dSXin LI 	    } else {
600*5f0216bdSXin LI 		if (file_printf(ms, "application/CDFV2-unknown") == -1)
601b6cee71dSXin LI 		    return -1;
602b6cee71dSXin LI 	    }
603b6cee71dSXin LI 	    i = 1;
604b6cee71dSXin LI 	}
605b6cee71dSXin LI         return i;
606b6cee71dSXin LI }
607