xref: /PHP-5.4/ext/fileinfo/libmagic/readcdf.c (revision 44be7b7f)
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.33 2012/06/20 21:52:36 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 
42 #include "cdf.h"
43 #include "magic.h"
44 
45 #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
46 
47 private int
cdf_file_property_info(struct magic_set * ms,const cdf_property_info_t * info,size_t count)48 cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
49     size_t count)
50 {
51         size_t i;
52         cdf_timestamp_t tp;
53         struct timeval ts;
54         char buf[64];
55         const char *str = NULL;
56         const char *s;
57         int len;
58 
59 	memset(&ts, 0, sizeof(ts));
60 
61         for (i = 0; i < count; i++) {
62                 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
63                 switch (info[i].pi_type) {
64                 case CDF_NULL:
65                         break;
66                 case CDF_SIGNED16:
67                         if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
68                             info[i].pi_s16) == -1)
69                                 return -1;
70                         break;
71                 case CDF_SIGNED32:
72                         if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
73                             info[i].pi_s32) == -1)
74                                 return -1;
75                         break;
76                 case CDF_UNSIGNED32:
77                         if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
78                             info[i].pi_u32) == -1)
79                                 return -1;
80                         break;
81                 case CDF_FLOAT:
82                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
83                             info[i].pi_f) == -1)
84                                 return -1;
85                         break;
86                 case CDF_DOUBLE:
87                         if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
88                             info[i].pi_d) == -1)
89                                 return -1;
90                         break;
91                 case CDF_LENGTH32_STRING:
92                 case CDF_LENGTH32_WSTRING:
93                         len = info[i].pi_str.s_len;
94                         if (len > 1) {
95                                 char vbuf[1024];
96                                 size_t j, k = 1;
97 
98                                 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
99                                     k++;
100                                 s = info[i].pi_str.s_buf;
101                                 for (j = 0; j < sizeof(vbuf) && len--;
102                                     j++, s += k) {
103                                         if (*s == '\0')
104                                                 break;
105                                         if (isprint((unsigned char)*s))
106                                                 vbuf[j] = *s;
107                                 }
108                                 if (j == sizeof(vbuf))
109                                         --j;
110                                 vbuf[j] = '\0';
111                                 if (NOTMIME(ms)) {
112                                         if (vbuf[0]) {
113                                                 if (file_printf(ms, ", %s: %s",
114                                                     buf, vbuf) == -1)
115                                                         return -1;
116                                         }
117                                 } else if (info[i].pi_id ==
118                                         CDF_PROPERTY_NAME_OF_APPLICATION) {
119                                         if (strstr(vbuf, "Word"))
120                                                 str = "msword";
121                                         else if (strstr(vbuf, "Excel"))
122                                                 str = "vnd.ms-excel";
123                                         else if (strstr(vbuf, "Powerpoint"))
124                                                 str = "vnd.ms-powerpoint";
125                                         else if (strstr(vbuf,
126                                             "Crystal Reports"))
127                                                 str = "x-rpt";
128                                 }
129                         }
130                         break;
131                 case CDF_FILETIME:
132                         tp = info[i].pi_tp;
133                         if (tp != 0) {
134 							char tbuf[64];
135 #if defined(PHP_WIN32) && _MSC_VER <= 1500
136 							if (tp < 1000000000000000i64) {
137 #else
138 							if (tp < 1000000000000000LL) {
139 #endif
140                                         cdf_print_elapsed_time(tbuf,
141                                             sizeof(tbuf), tp);
142                                         if (NOTMIME(ms) && file_printf(ms,
143                                             ", %s: %s", buf, tbuf) == -1)
144                                                 return -1;
145                                 } else {
146                                         char *c, *ec;
147 					const time_t sec = ts.tv_sec;
148                                         if (cdf_timestamp_to_timespec(&ts, tp) == -1) {
149 											return -1;
150 										}
151                                         c = cdf_ctime(&sec, tbuf);
152                                         if (c != NULL &&
153 					    (ec = strchr(c, '\n')) != NULL)
154                                                 *ec = '\0';
155 
156                                         if (NOTMIME(ms) && file_printf(ms,
157                                             ", %s: %s", buf, c) == -1)
158                                                 return -1;
159                                 }
160                         }
161                         break;
162                 case CDF_CLIPBOARD:
163                         break;
164                 default:
165                         return -1;
166                 }
167         }
168         if (!NOTMIME(ms)) {
169 		if (str == NULL)
170 			return 0;
171                 if (file_printf(ms, "application/%s", str) == -1)
172                         return -1;
173         }
174         return 1;
175 }
176 
177 private int
178 cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
179     const cdf_stream_t *sst)
180 {
181         cdf_summary_info_header_t si;
182         cdf_property_info_t *info;
183         size_t count;
184         int m;
185 
186         if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
187                 return -1;
188 
189         if (NOTMIME(ms)) {
190                 if (file_printf(ms, "Composite Document File V2 Document")
191 		    == -1)
192                         return -1;
193 
194                 if (file_printf(ms, ", %s Endian",
195                     si.si_byte_order == 0xfffe ?  "Little" : "Big") == -1)
196                         return -2;
197                 switch (si.si_os) {
198                 case 2:
199                         if (file_printf(ms, ", Os: Windows, Version %d.%d",
200                             si.si_os_version & 0xff,
201                             (uint32_t)si.si_os_version >> 8) == -1)
202                                 return -2;
203                         break;
204                 case 1:
205                         if (file_printf(ms, ", Os: MacOS, Version %d.%d",
206                             (uint32_t)si.si_os_version >> 8,
207                             si.si_os_version & 0xff) == -1)
208                                 return -2;
209                         break;
210                 default:
211                         if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
212                             si.si_os_version & 0xff,
213                             (uint32_t)si.si_os_version >> 8) == -1)
214                                 return -2;
215                         break;
216                 }
217         }
218 
219         m = cdf_file_property_info(ms, info, count);
220         free(info);
221 
222         return m == -1 ? -2 : m;
223 }
224 
225 protected int
226 file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
227     size_t nbytes)
228 {
229         cdf_info_t info;
230         cdf_header_t h;
231         cdf_sat_t sat, ssat;
232         cdf_stream_t sst, scn;
233         cdf_dir_t dir;
234         int i;
235         const char *expn = "";
236         const char *corrupt = "corrupt: ";
237 
238         info.i_fd = fd;
239         info.i_buf = buf;
240         info.i_len = nbytes;
241         if (ms->flags & MAGIC_APPLE)
242                 return 0;
243         if (cdf_read_header(&info, &h) == -1)
244                 return 0;
245 #ifdef CDF_DEBUG
246         cdf_dump_header(&h);
247 #endif
248 
249         if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
250                 expn = "Can't read SAT";
251                 goto out0;
252         }
253 #ifdef CDF_DEBUG
254         cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
255 #endif
256 
257         if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
258                 expn = "Can't read SSAT";
259                 goto out1;
260         }
261 #ifdef CDF_DEBUG
262         cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
263 #endif
264 
265         if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
266                 expn = "Can't read directory";
267                 goto out2;
268         }
269 
270         if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst)) == -1) {
271                 expn = "Cannot read short stream";
272                 goto out3;
273         }
274 #ifdef CDF_DEBUG
275         cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
276 #endif
277 
278         if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
279             &scn)) == -1) {
280                 if (errno == ESRCH) {
281                         corrupt = expn;
282                         expn = "No summary info";
283                 } else {
284                         expn = "Cannot read summary info";
285                 }
286                 goto out4;
287         }
288 #ifdef CDF_DEBUG
289         cdf_dump_summary_info(&h, &scn);
290 #endif
291         if ((i = cdf_file_summary_info(ms, &h, &scn)) < 0)
292                 expn = "Can't expand summary_info";
293 	if (i == 0) {
294 		const char *str = "vnd.ms-office";
295 		cdf_directory_t *d;
296 		char name[__arraycount(d->d_name)];
297 		size_t j, k;
298 		for (j = 0; j < dir.dir_len; j++) {
299 		    d = &dir.dir_tab[j];
300 		    for (k = 0; k < sizeof(name); k++)
301 			name[k] = (char)cdf_tole2(d->d_name[k]);
302 		    if (strstr(name, "WordDocument") != 0) {
303 			str = "msword";
304 			break;
305 		    }
306 		    if (strstr(name, "PowerPoint") != 0) {
307 			str = "vnd.ms-powerpoint";
308 			break;
309 		    }
310 		}
311                 if (file_printf(ms, "application/%s", str) == -1)
312                         return -1;
313 		i = 1;
314 	}
315         free(scn.sst_tab);
316 out4:
317         free(sst.sst_tab);
318 out3:
319         free(dir.dir_tab);
320 out2:
321         free(ssat.sat_tab);
322 out1:
323         free(sat.sat_tab);
324 out0:
325         if (i != 1) {
326 		if (i == -1) {
327 		    if (NOTMIME(ms)) {
328 			if (file_printf(ms,
329 			    "Composite Document File V2 Document") == -1)
330 			    return -1;
331                 if (*expn)
332                         if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
333                                 return -1;
334 		    } else {
335 			if (file_printf(ms, "application/CDFV2-corrupt") == -1)
336 			    return -1;
337 		    }
338 		}
339                 i = 1;
340         }
341         return i;
342 }
343