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.53 2015/04/09 20:01:41 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 { "WordDocument", "msword", },
64 { "PowerPoint", "vnd.ms-powerpoint", },
65 { "DigitalSignature", "vnd.ms-msi", },
68 { "WordDocument", "Microsoft Office Word",},
69 { "PowerPoint", "Microsoft PowerPoint", },
70 { "DigitalSignature", "Microsoft Installer", },
74 static const struct cv {
79 { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
87 { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
96 cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
99 for (i = 0; cv[i].mime != NULL; i++) {
100 if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
104 fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0],
111 cdf_app_to_mime(const char *vbuf, const struct nv *nv)
114 const char *rv = NULL;
116 locale_t old_lc_ctype, c_lc_ctype;
118 c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
119 assert(c_lc_ctype != NULL);
120 old_lc_ctype = uselocale(c_lc_ctype);
121 assert(old_lc_ctype != NULL);
123 for (i = 0; nv[i].pattern != NULL; i++)
124 if (strcasestr(vbuf, nv[i].pattern) != NULL) {
129 fprintf(stderr, "unknown app %s\n", vbuf);
132 (void)uselocale(old_lc_ctype);
133 freelocale(c_lc_ctype);
139 cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
140 size_t count, const cdf_directory_t *root_storage)
146 const char *str = NULL;
150 if (!NOTMIME(ms) && root_storage)
151 str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
154 for (i = 0; i < count; i++) {
155 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
156 switch (info[i].pi_type) {
160 if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
161 info[i].pi_s16) == -1)
165 if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
166 info[i].pi_s32) == -1)
170 if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
171 info[i].pi_u32) == -1)
175 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
180 if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
184 case CDF_LENGTH32_STRING:
185 case CDF_LENGTH32_WSTRING:
186 len = info[i].pi_str.s_len;
191 if (info[i].pi_type == CDF_LENGTH32_WSTRING)
193 s = info[i].pi_str.s_buf;
194 for (j = 0; j < sizeof(vbuf) && len--; s += k) {
197 if (isprint((unsigned char)*s))
200 if (j == sizeof(vbuf))
205 if (file_printf(ms, ", %s: %s",
209 } else if (str == NULL && info[i].pi_id ==
210 CDF_PROPERTY_NAME_OF_APPLICATION) {
211 str = cdf_app_to_mime(vbuf, app2mime);
219 if (tp < 1000000000000000LL) {
220 cdf_print_elapsed_time(tbuf,
222 if (NOTMIME(ms) && file_printf(ms,
223 ", %s: %s", buf, tbuf) == -1)
227 cdf_timestamp_to_timespec(&ts, tp);
228 c = cdf_ctime(&ts.tv_sec, tbuf);
230 (ec = strchr(c, '\n')) != NULL)
233 if (NOTMIME(ms) && file_printf(ms,
234 ", %s: %s", buf, c) == -1)
248 if (file_printf(ms, "application/%s", str) == -1)
255 cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
256 const cdf_stream_t *sst)
261 cdf_catalog_entry_t *ce;
264 if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
266 if (cdf_unpack_catalog(h, sst, &cat) == -1)
269 /* skip first entry since it has a , or paren */
270 for (i = 1; i < cat->cat_num; i++)
271 if (file_printf(ms, "%s%s",
272 cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
273 i == cat->cat_num - 1 ? "]" : ", ") == -1) {
279 if (file_printf(ms, "application/CDFV2") == -1)
286 cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
287 const cdf_stream_t *sst, const cdf_directory_t *root_storage)
289 cdf_summary_info_header_t si;
290 cdf_property_info_t *info;
294 if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
300 if (file_printf(ms, "Composite Document File V2 Document")
304 if (file_printf(ms, ", %s Endian",
305 si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
309 if (file_printf(ms, ", Os: Windows, Version %d.%d",
310 si.si_os_version & 0xff,
311 (uint32_t)si.si_os_version >> 8) == -1)
315 if (file_printf(ms, ", Os: MacOS, Version %d.%d",
316 (uint32_t)si.si_os_version >> 8,
317 si.si_os_version & 0xff) == -1)
321 if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
322 si.si_os_version & 0xff,
323 (uint32_t)si.si_os_version >> 8) == -1)
328 str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
331 if (file_printf(ms, ", %s", str) == -1)
337 m = cdf_file_property_info(ms, info, count, root_storage);
340 return m == -1 ? -2 : m;
345 format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
346 snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
347 PRIx64 "-%.12" PRIx64,
348 (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
349 (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
350 (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
351 (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
352 (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL);
358 cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info,
359 const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
360 const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn)
364 if ((i = cdf_read_user_stream(info, h, sat, ssat, sst,
365 dir, "Catalog", scn)) == -1)
368 cdf_dump_catalog(&h, &scn);
370 if ((i = cdf_file_catalog(ms, h, scn)) == -1)
375 private struct sinfo {
378 const char *sections[5];
381 { "Encrypted", "encrypted",
383 "EncryptedPackage", NULL, NULL, NULL, NULL,
386 CDF_DIR_TYPE_USER_STREAM, 0, 0, 0, 0,
390 { "QuickBooks", "quickbooks",
393 "TaxForms", "PDFTaxForms", "modulesInBackup",
395 "mfbu_header", NULL, NULL, NULL, NULL,
399 CDF_DIR_TYPE_USER_STORAGE,
400 CDF_DIR_TYPE_USER_STORAGE,
401 CDF_DIR_TYPE_USER_STREAM,
403 CDF_DIR_TYPE_USER_STREAM,
410 cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir)
414 for (sd = 0; sd < __arraycount(sectioninfo); sd++) {
415 const struct sinfo *si = §ioninfo[sd];
416 for (j = 0; si->sections[j]; j++) {
417 if (cdf_find_stream(dir, si->sections[j], si->types[j])
420 fprintf(stderr, "Can't read %s\n",
426 if (si->sections[j] != NULL)
429 if (file_printf(ms, "CDFV2 %s", si->name) == -1)
432 if (file_printf(ms, "application/CDFV2-%s",
442 file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
448 cdf_stream_t sst, scn;
451 const char *expn = "";
452 const cdf_directory_t *root_storage;
457 if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
459 if (cdf_read_header(&info, &h) == -1)
465 if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
466 expn = "Can't read SAT";
470 cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
473 if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
474 expn = "Can't read SSAT";
478 cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
481 if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
482 expn = "Can't read directory";
486 if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
487 &root_storage)) == -1) {
488 expn = "Cannot read short stream";
492 cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
498 if (file_printf(ms, "CLSID %s, ",
499 format_clsid(clsbuf, sizeof(clsbuf),
500 root_storage->d_storage_uuid)) == -1)
506 if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
507 "FileHeader", &scn)) != -1) {
508 #define HWP5_SIGNATURE "HWP Document File"
509 if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1
510 && memcmp(scn.sst_tab, HWP5_SIGNATURE,
511 sizeof(HWP5_SIGNATURE) - 1) == 0) {
514 "Hangul (Korean) Word Processor File 5.x") == -1)
517 if (file_printf(ms, "application/x-hwp") == -1)
530 if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
532 if (errno != ESRCH) {
533 expn = "Cannot read summary info";
536 i = cdf_file_catalog_info(ms, &info, &h, &sat, &ssat, &sst,
540 i = cdf_file_dir_info(ms, &dir);
542 expn = "Cannot read section info";
548 cdf_dump_summary_info(&h, &scn);
550 if ((i = cdf_file_summary_info(ms, &h, &scn, root_storage)) < 0)
551 expn = "Can't expand summary_info";
554 const char *str = NULL;
556 char name[__arraycount(d->d_name)];
559 for (j = 0; str == NULL && j < dir.dir_len; j++) {
561 for (k = 0; k < sizeof(name); k++)
562 name[k] = (char)cdf_tole2(d->d_name[k]);
563 str = cdf_app_to_mime(name,
564 NOTMIME(ms) ? name2desc : name2mime);
568 if (file_printf(ms, "%s", str) == -1)
574 str = "vnd.ms-office";
575 if (file_printf(ms, "application/%s", str) == -1)
594 "Composite Document File V2 Document") == -1)
597 if (file_printf(ms, ", %s", expn) == -1)
600 if (file_printf(ms, "application/CDFV2-unknown") == -1)