2 * Copyright (c) 2008, 2016 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.63 2016/10/18 22:25:42 christos Exp $")
43 #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
46 #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
48 static const struct nv {
52 { "Word", "msword", },
53 { "Excel", "vnd.ms-excel", },
54 { "Powerpoint", "vnd.ms-powerpoint", },
55 { "Crystal Reports", "x-rpt", },
56 { "Advanced Installer", "vnd.ms-msi", },
57 { "InstallShield", "vnd.ms-msi", },
58 { "Microsoft Patch Compiler", "vnd.ms-msi", },
59 { "NAnt", "vnd.ms-msi", },
60 { "Windows Installer", "vnd.ms-msi", },
63 { "Book", "vnd.ms-excel", },
64 { "Workbook", "vnd.ms-excel", },
65 { "WordDocument", "msword", },
66 { "PowerPoint", "vnd.ms-powerpoint", },
67 { "DigitalSignature", "vnd.ms-msi", },
70 { "Book", "Microsoft Excel", },
71 { "Workbook", "Microsoft Excel", },
72 { "WordDocument", "Microsoft Word", },
73 { "PowerPoint", "Microsoft PowerPoint", },
74 { "DigitalSignature", "Microsoft Installer", },
78 static const struct cv {
83 { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
91 { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
100 cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
103 for (i = 0; cv[i].mime != NULL; i++) {
104 if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
108 fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0],
115 cdf_app_to_mime(const char *vbuf, const struct nv *nv)
118 const char *rv = NULL;
120 locale_t old_lc_ctype, c_lc_ctype;
122 c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
123 assert(c_lc_ctype != NULL);
124 old_lc_ctype = uselocale(c_lc_ctype);
125 assert(old_lc_ctype != NULL);
127 char *old_lc_ctype = setlocale(LC_CTYPE, "C");
129 for (i = 0; nv[i].pattern != NULL; i++)
130 if (strcasestr(vbuf, nv[i].pattern) != NULL) {
135 fprintf(stderr, "unknown app %s\n", vbuf);
138 (void)uselocale(old_lc_ctype);
139 freelocale(c_lc_ctype);
141 setlocale(LC_CTYPE, old_lc_ctype);
147 cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
148 size_t count, const cdf_directory_t *root_storage)
154 const char *str = NULL;
158 if (!NOTMIME(ms) && root_storage)
159 str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
162 for (i = 0; i < count; i++) {
163 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
164 switch (info[i].pi_type) {
168 if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
169 info[i].pi_s16) == -1)
173 if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
174 info[i].pi_s32) == -1)
178 if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
179 info[i].pi_u32) == -1)
183 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
188 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
192 case CDF_LENGTH32_STRING:
193 case CDF_LENGTH32_WSTRING:
194 len = info[i].pi_str.s_len;
199 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
201 s = info[i].pi_str.s_buf;
202 for (j = 0; j < sizeof(vbuf) && len--; s += k) {
205 if (isprint((unsigned char)*s))
208 if (j == sizeof(vbuf))
213 if (file_printf(ms, ", %s: %s",
217 } else if (str == NULL && info[i].pi_id ==
218 CDF_PROPERTY_NAME_OF_APPLICATION) {
219 str = cdf_app_to_mime(vbuf, app2mime);
227 if (tp < 1000000000000000LL) {
228 cdf_print_elapsed_time(tbuf,
230 if (NOTMIME(ms) && file_printf(ms,
231 ", %s: %s", buf, tbuf) == -1)
235 cdf_timestamp_to_timespec(&ts, tp);
236 c = cdf_ctime(&ts.tv_sec, tbuf);
238 (ec = strchr(c, '\n')) != NULL)
241 if (NOTMIME(ms) && file_printf(ms,
242 ", %s: %s", buf, c) == -1)
256 if (file_printf(ms, "application/%s", str) == -1)
263 cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
264 const cdf_stream_t *sst)
269 cdf_catalog_entry_t *ce;
272 if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
274 if (cdf_unpack_catalog(h, sst, &cat) == -1)
277 /* skip first entry since it has a , or paren */
278 for (i = 1; i < cat->cat_num; i++)
279 if (file_printf(ms, "%s%s",
280 cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
281 i == cat->cat_num - 1 ? "]" : ", ") == -1) {
287 if (file_printf(ms, "application/CDFV2") == -1)
294 cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
295 const cdf_stream_t *sst, const cdf_directory_t *root_storage)
297 cdf_summary_info_header_t si;
298 cdf_property_info_t *info;
302 if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
308 if (file_printf(ms, "Composite Document File V2 Document")
312 if (file_printf(ms, ", %s Endian",
313 si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
317 if (file_printf(ms, ", Os: Windows, Version %d.%d",
318 si.si_os_version & 0xff,
319 (uint32_t)si.si_os_version >> 8) == -1)
323 if (file_printf(ms, ", Os: MacOS, Version %d.%d",
324 (uint32_t)si.si_os_version >> 8,
325 si.si_os_version & 0xff) == -1)
329 if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
330 si.si_os_version & 0xff,
331 (uint32_t)si.si_os_version >> 8) == -1)
336 str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
339 if (file_printf(ms, ", %s", str) == -1)
345 m = cdf_file_property_info(ms, info, count, root_storage);
348 return m == -1 ? -2 : m;
353 format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
354 snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
355 PRIx64 "-%.12" PRIx64,
356 (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
357 (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
358 (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
359 (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
360 (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL);
366 cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info,
367 const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
368 const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn)
372 if ((i = cdf_read_user_stream(info, h, sat, ssat, sst,
373 dir, "Catalog", scn)) == -1)
376 cdf_dump_catalog(h, scn);
378 if ((i = cdf_file_catalog(ms, h, scn)) == -1)
384 cdf_check_summary_info(struct magic_set *ms, const cdf_info_t *info,
385 const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
386 const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn,
387 const cdf_directory_t *root_storage, const char **expn)
390 const char *str = NULL;
392 char name[__arraycount(d->d_name)];
396 cdf_dump_summary_info(h, scn);
398 if ((i = cdf_file_summary_info(ms, h, scn, root_storage)) < 0) {
399 *expn = "Can't expand summary_info";
404 for (j = 0; str == NULL && j < dir->dir_len; j++) {
405 d = &dir->dir_tab[j];
406 for (k = 0; k < sizeof(name); k++)
407 name[k] = (char)cdf_tole2(d->d_name[k]);
408 str = cdf_app_to_mime(name,
409 NOTMIME(ms) ? name2desc : name2mime);
413 if (file_printf(ms, "%s", str) == -1)
419 str = "vnd.ms-office";
420 if (file_printf(ms, "application/%s", str) == -1)
425 i = cdf_file_catalog_info(ms, info, h, sat, ssat, sst,
431 private struct sinfo {
434 const char *sections[5];
437 { "Encrypted", "encrypted",
439 "EncryptedPackage", "EncryptedSummary",
443 CDF_DIR_TYPE_USER_STREAM,
444 CDF_DIR_TYPE_USER_STREAM,
449 { "QuickBooks", "quickbooks",
452 "TaxForms", "PDFTaxForms", "modulesInBackup",
454 "mfbu_header", NULL, NULL, NULL, NULL,
458 CDF_DIR_TYPE_USER_STORAGE,
459 CDF_DIR_TYPE_USER_STORAGE,
460 CDF_DIR_TYPE_USER_STREAM,
462 CDF_DIR_TYPE_USER_STREAM,
466 { "Microsoft Excel", "vnd.ms-excel",
468 "Book", "Workbook", NULL, NULL, NULL,
471 CDF_DIR_TYPE_USER_STREAM,
472 CDF_DIR_TYPE_USER_STREAM,
476 { "Microsoft Word", "msword",
478 "WordDocument", NULL, NULL, NULL, NULL,
481 CDF_DIR_TYPE_USER_STREAM,
485 { "Microsoft PowerPoint", "vnd.ms-powerpoint",
487 "PowerPoint", NULL, NULL, NULL, NULL,
490 CDF_DIR_TYPE_USER_STREAM,
494 { "Microsoft Outlook Message", "vnd.ms-outlook",
496 "__properties_version1.0",
497 "__recip_version1.0_#00000000",
501 CDF_DIR_TYPE_USER_STREAM,
502 CDF_DIR_TYPE_USER_STORAGE,
509 cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir)
513 for (sd = 0; sd < __arraycount(sectioninfo); sd++) {
514 const struct sinfo *si = §ioninfo[sd];
515 for (j = 0; si->sections[j]; j++) {
516 if (cdf_find_stream(dir, si->sections[j], si->types[j])
520 fprintf(stderr, "Can't read %s\n", si->sections[j]);
523 if (si->sections[j] == NULL)
526 if (file_printf(ms, "CDFV2 %s", si->name) == -1)
529 if (file_printf(ms, "application/%s", si->mime) == -1)
538 file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
544 cdf_stream_t sst, scn;
547 const char *expn = "";
548 const cdf_directory_t *root_storage;
554 if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
556 if (cdf_read_header(&info, &h) == -1)
562 if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
563 expn = "Can't read SAT";
567 cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
570 if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
571 expn = "Can't read SSAT";
575 cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
578 if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
579 expn = "Can't read directory";
583 if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
584 &root_storage)) == -1) {
585 expn = "Cannot read short stream";
589 cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
595 if (file_printf(ms, "CLSID %s, ",
596 format_clsid(clsbuf, sizeof(clsbuf),
597 root_storage->d_storage_uuid)) == -1)
603 if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
604 "FileHeader", &scn)) != -1) {
605 #define HWP5_SIGNATURE "HWP Document File"
606 if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1
607 && memcmp(scn.sst_tab, HWP5_SIGNATURE,
608 sizeof(HWP5_SIGNATURE) - 1) == 0) {
611 "Hangul (Korean) Word Processor File 5.x") == -1)
614 if (file_printf(ms, "application/x-hwp") == -1)
620 cdf_zero_stream(&scn);
624 if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
626 if (errno != ESRCH) {
627 expn = "Cannot read summary info";
630 i = cdf_check_summary_info(ms, &info, &h,
631 &sat, &ssat, &sst, &dir, &scn, root_storage, &expn);
632 cdf_zero_stream(&scn);
635 if ((i = cdf_read_doc_summary_info(&info, &h, &sat, &ssat,
636 &sst, &dir, &scn)) == -1) {
637 if (errno != ESRCH) {
638 expn = "Cannot read summary info";
641 i = cdf_check_summary_info(ms, &info, &h, &sat, &ssat,
642 &sst, &dir, &scn, root_storage, &expn);
646 i = cdf_file_dir_info(ms, &dir);
648 expn = "Cannot read section info";
651 cdf_zero_stream(&scn);
652 cdf_zero_stream(&sst);
663 "Composite Document File V2 Document") == -1)
666 if (file_printf(ms, ", %s", expn) == -1)
669 if (file_printf(ms, "application/CDFV2") == -1)