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.40 2014/03/06 15:23:33 christos Exp $")
38 #if defined(HAVE_LOCALE_H)
45 #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
47 static const struct nv {
51 { "Word", "msword", },
52 { "Excel", "vnd.ms-excel", },
53 { "Powerpoint", "vnd.ms-powerpoint", },
54 { "Crystal Reports", "x-rpt", },
55 { "Advanced Installer", "vnd.ms-msi", },
56 { "InstallShield", "vnd.ms-msi", },
57 { "Microsoft Patch Compiler", "vnd.ms-msi", },
58 { "NAnt", "vnd.ms-msi", },
59 { "Windows Installer", "vnd.ms-msi", },
62 { "WordDocument", "msword", },
63 { "PowerPoint", "vnd.ms-powerpoint", },
64 { "DigitalSignature", "vnd.ms-msi", },
67 { "WordDocument", "Microsoft Office Word",},
68 { "PowerPoint", "Microsoft PowerPoint", },
69 { "DigitalSignature", "Microsoft Installer", },
73 static const struct cv {
78 { 0x00000000000c1084LLU, 0x46000000000000c0LLU },
83 { 0x00000000000c1084LLU, 0x46000000000000c0LLU },
89 cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
92 for (i = 0; cv[i].mime != NULL; i++) {
93 if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
100 cdf_app_to_mime(const char *vbuf, const struct nv *nv)
103 const char *rv = NULL;
106 old_lc_ctype = setlocale(LC_CTYPE, NULL);
107 assert(old_lc_ctype != NULL);
108 old_lc_ctype = strdup(old_lc_ctype);
109 assert(old_lc_ctype != NULL);
110 (void)setlocale(LC_CTYPE, "C");
111 for (i = 0; nv[i].pattern != NULL; i++)
112 if (strcasestr(vbuf, nv[i].pattern) != NULL) {
116 (void)setlocale(LC_CTYPE, old_lc_ctype);
122 cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
123 size_t count, const uint64_t clsid[2])
129 const char *str = NULL;
134 str = cdf_clsid_to_mime(clsid, clsid2mime);
136 for (i = 0; i < count; i++) {
137 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
138 switch (info[i].pi_type) {
142 if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
143 info[i].pi_s16) == -1)
147 if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
148 info[i].pi_s32) == -1)
152 if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
153 info[i].pi_u32) == -1)
157 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
162 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
166 case CDF_LENGTH32_STRING:
167 case CDF_LENGTH32_WSTRING:
168 len = info[i].pi_str.s_len;
173 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
175 s = info[i].pi_str.s_buf;
176 for (j = 0; j < sizeof(vbuf) && len--;
180 if (isprint((unsigned char)*s))
183 if (j == sizeof(vbuf))
188 if (file_printf(ms, ", %s: %s",
192 } else if (str == NULL && info[i].pi_id ==
193 CDF_PROPERTY_NAME_OF_APPLICATION) {
194 str = cdf_app_to_mime(vbuf, app2mime);
202 if (tp < 1000000000000000LL) {
203 cdf_print_elapsed_time(tbuf,
205 if (NOTMIME(ms) && file_printf(ms,
206 ", %s: %s", buf, tbuf) == -1)
210 cdf_timestamp_to_timespec(&ts, tp);
211 c = cdf_ctime(&ts.tv_sec, tbuf);
213 (ec = strchr(c, '\n')) != NULL)
216 if (NOTMIME(ms) && file_printf(ms,
217 ", %s: %s", buf, c) == -1)
231 if (file_printf(ms, "application/%s", str) == -1)
238 cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
239 const cdf_stream_t *sst, const uint64_t clsid[2])
241 cdf_summary_info_header_t si;
242 cdf_property_info_t *info;
246 if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
252 if (file_printf(ms, "Composite Document File V2 Document")
256 if (file_printf(ms, ", %s Endian",
257 si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
261 if (file_printf(ms, ", Os: Windows, Version %d.%d",
262 si.si_os_version & 0xff,
263 (uint32_t)si.si_os_version >> 8) == -1)
267 if (file_printf(ms, ", Os: MacOS, Version %d.%d",
268 (uint32_t)si.si_os_version >> 8,
269 si.si_os_version & 0xff) == -1)
273 if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
274 si.si_os_version & 0xff,
275 (uint32_t)si.si_os_version >> 8) == -1)
279 str = cdf_clsid_to_mime(clsid, clsid2desc);
281 if (file_printf(ms, ", %s", str) == -1)
285 m = cdf_file_property_info(ms, info, count, clsid);
288 return m == -1 ? -2 : m;
293 format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
294 snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
295 PRIx64 "-%.12" PRIx64,
296 (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffLLU,
297 (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffLLU,
298 (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffLLU,
299 (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffLLU,
300 (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffLLU);
306 file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
312 cdf_stream_t sst, scn;
315 const char *expn = "";
316 const char *corrupt = "corrupt: ";
321 if (ms->flags & MAGIC_APPLE)
323 if (cdf_read_header(&info, &h) == -1)
329 if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
330 expn = "Can't read SAT";
334 cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
337 if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
338 expn = "Can't read SSAT";
342 cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
345 if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
346 expn = "Can't read directory";
350 const cdf_directory_t *root_storage;
351 if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
352 &root_storage)) == -1) {
353 expn = "Cannot read short stream";
357 cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
363 if (file_printf(ms, "CLSID %s, ",
364 format_clsid(clsbuf, sizeof(clsbuf),
365 root_storage->d_storage_uuid)) == -1)
371 if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
373 if (errno == ESRCH) {
375 expn = "No summary info";
377 expn = "Cannot read summary info";
382 cdf_dump_summary_info(&h, &scn);
384 if ((i = cdf_file_summary_info(ms, &h, &scn,
385 root_storage->d_storage_uuid)) < 0)
386 expn = "Can't expand summary_info";
389 const char *str = NULL;
391 char name[__arraycount(d->d_name)];
394 for (j = 0; str == NULL && j < dir.dir_len; 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);
403 if (file_printf(ms, "%s", str) == -1)
409 str = "vnd.ms-office";
410 if (file_printf(ms, "application/%s", str) == -1)
428 "Composite Document File V2 Document") == -1)
431 if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
434 if (file_printf(ms, "application/CDFV2-corrupt") == -1)