2 * Copyright (c) 2008 Christos Zoulas
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 FILE_RCSID("@(#)$File: readcdf.c,v 1.29 2012/02/20 20:04:58 christos Exp $")
41 #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
44 cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
51 const char *str = NULL;
55 for (i = 0; i < count; i++) {
56 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
57 switch (info[i].pi_type) {
61 if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
62 info[i].pi_s16) == -1)
66 if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
67 info[i].pi_s32) == -1)
71 if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
72 info[i].pi_u32) == -1)
76 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
81 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
85 case CDF_LENGTH32_STRING:
86 case CDF_LENGTH32_WSTRING:
87 len = info[i].pi_str.s_len;
92 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
94 s = info[i].pi_str.s_buf;
95 for (j = 0; j < sizeof(vbuf) && len--;
99 if (isprint((unsigned char)*s))
102 if (j == sizeof(vbuf))
107 if (file_printf(ms, ", %s: %s",
111 } else if (info[i].pi_id ==
112 CDF_PROPERTY_NAME_OF_APPLICATION) {
113 if (strstr(vbuf, "Word"))
115 else if (strstr(vbuf, "Excel"))
116 str = "vnd.ms-excel";
117 else if (strstr(vbuf, "Powerpoint"))
118 str = "vnd.ms-powerpoint";
119 else if (strstr(vbuf,
128 if (tp < 1000000000000000LL) {
130 cdf_print_elapsed_time(tbuf,
132 if (NOTMIME(ms) && file_printf(ms,
133 ", %s: %s", buf, tbuf) == -1)
137 cdf_timestamp_to_timespec(&ts, tp);
138 c = cdf_ctime(&ts.tv_sec);
139 if ((ec = strchr(c, '\n')) != NULL)
142 if (NOTMIME(ms) && file_printf(ms,
143 ", %s: %s", buf, c) == -1)
157 if (file_printf(ms, "application/%s", str) == -1)
164 cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
165 const cdf_stream_t *sst)
167 cdf_summary_info_header_t si;
168 cdf_property_info_t *info;
172 if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
176 if (file_printf(ms, "Composite Document File V2 Document")
180 if (file_printf(ms, ", %s Endian",
181 si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
185 if (file_printf(ms, ", Os: Windows, Version %d.%d",
186 si.si_os_version & 0xff,
187 (uint32_t)si.si_os_version >> 8) == -1)
191 if (file_printf(ms, ", Os: MacOS, Version %d.%d",
192 (uint32_t)si.si_os_version >> 8,
193 si.si_os_version & 0xff) == -1)
197 if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
198 si.si_os_version & 0xff,
199 (uint32_t)si.si_os_version >> 8) == -1)
205 m = cdf_file_property_info(ms, info, count);
208 return m == -1 ? -2 : m;
212 file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
218 cdf_stream_t sst, scn;
221 const char *expn = "";
222 const char *corrupt = "corrupt: ";
227 if (ms->flags & MAGIC_APPLE)
229 if (cdf_read_header(&info, &h) == -1)
235 if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
236 expn = "Can't read SAT";
240 cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
243 if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
244 expn = "Can't read SSAT";
248 cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
251 if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
252 expn = "Can't read directory";
256 if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst)) == -1) {
257 expn = "Cannot read short stream";
261 cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
264 if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
266 if (errno == ESRCH) {
268 expn = "No summary info";
270 expn = "Cannot read summary info";
275 cdf_dump_summary_info(&h, &scn);
277 if ((i = cdf_file_summary_info(ms, &h, &scn)) < 0)
278 expn = "Can't expand summary_info";
280 const char *str = "vnd.ms-office";
282 char name[__arraycount(d->d_name)];
284 for (j = 0; j < dir.dir_len; j++) {
286 for (k = 0; k < sizeof(name); k++)
287 name[k] = (char)cdf_tole2(d->d_name[k]);
288 if (strstr(name, "WordDocument") == 0) {
293 if (file_printf(ms, "application/%s", str) == -1)
309 if (file_printf(ms, "Composite Document File V2 Document")
313 if (file_printf(ms, ", %s%s", corrupt, expn) == -1)