xref: /PHP-5.6/ext/fileinfo/libmagic/readcdf.c (revision ef2a2fa3)
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.40 2014/03/06 15:23:33 christos Exp $")
30 #endif
31 
32 #include <stdlib.h>
33 #ifdef PHP_WIN32
34 #include "win32/unistd.h"
35 #else
36 #include <unistd.h>
37 #endif
38 #include <string.h>
39 #include <time.h>
40 #include <ctype.h>
41 #if defined(HAVE_LOCALE_H)
42 #include <locale.h>
43 #endif
44 
45 #include "cdf.h"
46 #include "magic.h"
47 
48 #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
49 
50 static const struct nv {
51 	const char *pattern;
52 	const char *mime;
53 } app2mime[] =  {
54 	{ "Word",			"msword",		},
55 	{ "Excel",			"vnd.ms-excel",		},
56 	{ "Powerpoint",			"vnd.ms-powerpoint",	},
57 	{ "Crystal Reports",		"x-rpt",		},
58 	{ "Advanced Installer",		"vnd.ms-msi",		},
59 	{ "InstallShield",		"vnd.ms-msi",		},
60 	{ "Microsoft Patch Compiler",	"vnd.ms-msi",		},
61 	{ "NAnt",			"vnd.ms-msi",		},
62 	{ "Windows Installer",		"vnd.ms-msi",		},
63 	{ NULL,				NULL,			},
64 }, name2mime[] = {
65 	{ "WordDocument",		"msword",		},
66 	{ "PowerPoint",			"vnd.ms-powerpoint",	},
67 	{ "DigitalSignature",		"vnd.ms-msi",		},
68 	{ NULL,				NULL,			},
69 }, name2desc[] = {
70 	{ "WordDocument",		"Microsoft Office Word",},
71 	{ "PowerPoint",			"Microsoft PowerPoint",	},
72 	{ "DigitalSignature",		"Microsoft Installer",	},
73 	{ NULL,				NULL,			},
74 };
75 
76 #ifdef PHP_WIN32
77 # define strcasestr strstr
78 #endif
79 
80 static const struct cv {
81 	uint64_t clsid[2];
82 	const char *mime;
83 } clsid2mime[] = {
84 	{
85 #ifdef PHP_WIN32
86 		{ 0x00000000000c1084ui64, 0x46000000000000c0ui64 },
87 #else
88 		{ 0x00000000000c1084LLU, 0x46000000000000c0LLU },
89 #endif
90 		"x-msi",
91 	},
92 	{	{ 0,			 0			},
93 		NULL,
94 	}
95 }, clsid2desc[] = {
96 	{
97 #ifdef PHP_WIN32
98 		{ 0x00000000000c1084ui64, 0x46000000000000c0ui64 },
99 #else
100 		{ 0x00000000000c1084LLU, 0x46000000000000c0LLU },
101 #endif
102 		"MSI Installer",
103 	},
104 	{	{ 0,			 0			},
105 		NULL,
106 	}
107 };
108 
109 private const char *
cdf_clsid_to_mime(const uint64_t clsid[2],const struct cv * cv)110 cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
111 {
112 	size_t i;
113 	for (i = 0; cv[i].mime != NULL; i++) {
114 		if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
115 			return cv[i].mime;
116 	}
117 	return NULL;
118 }
119 
120 private const char *
cdf_app_to_mime(const char * vbuf,const struct nv * nv)121 cdf_app_to_mime(const char *vbuf, const struct nv *nv)
122 {
123 	size_t i;
124 	const char *rv = NULL;
125 
126 	(void)setlocale(LC_CTYPE, "C");
127 	for (i = 0; nv[i].pattern != NULL; i++)
128 		if (strcasestr(vbuf, nv[i].pattern) != NULL) {
129 			rv = nv[i].mime;
130 			break;
131 		}
132 	(void)setlocale(LC_CTYPE, "");
133 	return rv;
134 }
135 
136 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)137 cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
138     size_t count, const cdf_directory_t *root_storage)
139 {
140         size_t i;
141         cdf_timestamp_t tp;
142         struct timeval ts;
143         char buf[64];
144         const char *str = NULL;
145         const char *s;
146         int len;
147 
148 	memset(&ts, 0, sizeof(ts));
149 
150         if (!NOTMIME(ms) && root_storage)
151 		str = cdf_clsid_to_mime(root_storage->d_storage_uuid, clsid2mime);
152 
153         for (i = 0; i < count; i++) {
154                 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
155                 switch (info[i].pi_type) {
156                 case CDF_NULL:
157                         break;
158                 case CDF_SIGNED16:
159                         if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
160                             info[i].pi_s16) == -1)
161                                 return -1;
162                         break;
163                 case CDF_SIGNED32:
164                         if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
165                             info[i].pi_s32) == -1)
166                                 return -1;
167                         break;
168                 case CDF_UNSIGNED32:
169                         if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
170                             info[i].pi_u32) == -1)
171                                 return -1;
172                         break;
173                 case CDF_FLOAT:
174                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
175                             info[i].pi_f) == -1)
176                                 return -1;
177                         break;
178                 case CDF_DOUBLE:
179                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
180                             info[i].pi_d) == -1)
181                                 return -1;
182                         break;
183                 case CDF_LENGTH32_STRING:
184                 case CDF_LENGTH32_WSTRING:
185                         len = info[i].pi_str.s_len;
186                         if (len > 1) {
187                                 char vbuf[1024];
188                                 size_t j, k = 1;
189 
190                                 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
191                                     k++;
192                                 s = info[i].pi_str.s_buf;
193                                 for (j = 0; j < sizeof(vbuf) && len--;
194                                     j++, 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 defined(PHP_WIN32) && _MSC_VER <= 1500
220 							if (tp < 1000000000000000i64) {
221 #else
222 							if (tp < 1000000000000000LL) {
223 #endif
224                                         cdf_print_elapsed_time(tbuf,
225                                             sizeof(tbuf), tp);
226                                         if (NOTMIME(ms) && file_printf(ms,
227                                             ", %s: %s", buf, tbuf) == -1)
228                                                 return -1;
229                                 } else {
230                                         char *c, *ec;
231 					const time_t sec = ts.tv_sec;
232                                         if (cdf_timestamp_to_timespec(&ts, tp) == -1) {
233 											return -1;
234 										}
235                                         c = cdf_ctime(&sec, tbuf);
236                                         if (c != NULL &&
237 					    (ec = strchr(c, '\n')) != NULL)
238 						*ec = '\0';
239 
240                                         if (NOTMIME(ms) && file_printf(ms,
241                                             ", %s: %s", buf, c) == -1)
242                                                 return -1;
243                                 }
244                         }
245                         break;
246                 case CDF_CLIPBOARD:
247                         break;
248                 default:
249                         return -1;
250                 }
251         }
252         if (!NOTMIME(ms)) {
253 		if (str == NULL)
254 			return 0;
255                 if (file_printf(ms, "application/%s", str) == -1)
256                         return -1;
257         }
258         return 1;
259 }
260 
261 private int
262 cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
263     const cdf_stream_t *sst, const cdf_directory_t *root_storage)
264 {
265         cdf_summary_info_header_t si;
266         cdf_property_info_t *info;
267         size_t count;
268         int m;
269 
270         if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
271                 return -1;
272 
273         if (NOTMIME(ms)) {
274 		const char *str;
275 
276                 if (file_printf(ms, "Composite Document File V2 Document")
277 		    == -1)
278                         return -1;
279 
280                 if (file_printf(ms, ", %s Endian",
281                     si.si_byte_order == 0xfffe ?  "Little" : "Big") == -1)
282                         return -2;
283                 switch (si.si_os) {
284                 case 2:
285                         if (file_printf(ms, ", Os: Windows, Version %d.%d",
286                             si.si_os_version & 0xff,
287                             (uint32_t)si.si_os_version >> 8) == -1)
288                                 return -2;
289                         break;
290                 case 1:
291                         if (file_printf(ms, ", Os: MacOS, Version %d.%d",
292                             (uint32_t)si.si_os_version >> 8,
293                             si.si_os_version & 0xff) == -1)
294                                 return -2;
295                         break;
296                 default:
297                         if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
298                             si.si_os_version & 0xff,
299                             (uint32_t)si.si_os_version >> 8) == -1)
300                                 return -2;
301                         break;
302                 }
303 		if (root_storage) {
304 			str = cdf_clsid_to_mime(root_storage->d_storage_uuid, clsid2desc);
305 			if (str)
306 				if (file_printf(ms, ", %s", str) == -1)
307 					return -2;
308 			}
309 		}
310 
311         m = cdf_file_property_info(ms, info, count, root_storage);
312         free(info);
313 
314         return m == -1 ? -2 : m;
315 }
316 
317 protected int
318 file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
319     size_t nbytes)
320 {
321         cdf_info_t info;
322         cdf_header_t h;
323         cdf_sat_t sat, ssat;
324         cdf_stream_t sst, scn;
325         cdf_dir_t dir;
326         int i;
327         const char *expn = "";
328         const char *corrupt = "corrupt: ";
329         const cdf_directory_t *root_storage;
330 
331         info.i_fd = fd;
332         info.i_buf = buf;
333         info.i_len = nbytes;
334         if (ms->flags & MAGIC_APPLE)
335                 return 0;
336         if (cdf_read_header(&info, &h) == -1)
337                 return 0;
338 #ifdef CDF_DEBUG
339         cdf_dump_header(&h);
340 #endif
341 
342         if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
343                 expn = "Can't read SAT";
344                 goto out0;
345         }
346 #ifdef CDF_DEBUG
347         cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
348 #endif
349 
350         if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
351                 expn = "Can't read SSAT";
352                 goto out1;
353         }
354 #ifdef CDF_DEBUG
355         cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
356 #endif
357 
358         if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
359                 expn = "Can't read directory";
360                 goto out2;
361         }
362 
363         if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
364 	    &root_storage)) == -1) {
365                 expn = "Cannot read short stream";
366                 goto out3;
367         }
368 #ifdef CDF_DEBUG
369         cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
370 #endif
371 
372         if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
373             &scn)) == -1) {
374                 if (errno == ESRCH) {
375                         corrupt = expn;
376                         expn = "No summary info";
377                 } else {
378                         expn = "Cannot read summary info";
379                 }
380                 goto out4;
381         }
382 #ifdef CDF_DEBUG
383         cdf_dump_summary_info(&h, &scn);
384 #endif
385         if ((i = cdf_file_summary_info(ms, &h, &scn, root_storage)) < 0)
386             expn = "Can't expand summary_info";
387 
388 	if (i == 0) {
389 		const char *str = NULL;
390 		cdf_directory_t *d;
391 		char name[__arraycount(d->d_name)];
392 		size_t j, k;
393 
394 		for (j = 0; str == NULL && j < dir.dir_len; j++) {
395 			d = &dir.dir_tab[j];
396 			for (k = 0; k < sizeof(name); k++)
397 				name[k] = (char)cdf_tole2(d->d_name[k]);
398 			str = cdf_app_to_mime(name,
399 			    NOTMIME(ms) ? name2desc : name2mime);
400 		}
401 		if (NOTMIME(ms)) {
402 			if (str != NULL) {
403 				if (file_printf(ms, "%s", str) == -1)
404 					return -1;
405 				i = 1;
406 			}
407 		} else {
408 			if (str == NULL)
409 				str = "vnd.ms-office";
410 			if (file_printf(ms, "application/%s", str) == -1)
411 				return -1;
412 			i = 1;
413 		}
414 	}
415         free(scn.sst_tab);
416 out4:
417         free(sst.sst_tab);
418 out3:
419         free(dir.dir_tab);
420 out2:
421         free(ssat.sat_tab);
422 out1:
423         free(sat.sat_tab);
424 out0:
425 	if (i == -1) {
426 	    if (NOTMIME(ms)) {
427 		if (file_printf(ms,
428 		    "Composite Document File V2 Document") == -1)
429 		    return -1;
430 		if (*expn)
431 		    if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
432 			return -1;
433 	    } else {
434 		if (file_printf(ms, "application/CDFV2-corrupt") == -1)
435 		    return -1;
436 	    }
437 	    i = 1;
438 	}
439         return i;
440 }
441