1b6cee71dSXin LI /*- 2a5d223e6SXin LI * Copyright (c) 2008, 2016 Christos Zoulas 3b6cee71dSXin LI * All rights reserved. 4b6cee71dSXin LI * 5b6cee71dSXin LI * Redistribution and use in source and binary forms, with or without 6b6cee71dSXin LI * modification, are permitted provided that the following conditions 7b6cee71dSXin LI * are met: 8b6cee71dSXin LI * 1. Redistributions of source code must retain the above copyright 9b6cee71dSXin LI * notice, this list of conditions and the following disclaimer. 10b6cee71dSXin LI * 2. Redistributions in binary form must reproduce the above copyright 11b6cee71dSXin LI * notice, this list of conditions and the following disclaimer in the 12b6cee71dSXin LI * documentation and/or other materials provided with the distribution. 13b6cee71dSXin LI * 14b6cee71dSXin LI * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 15b6cee71dSXin LI * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16b6cee71dSXin LI * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17b6cee71dSXin LI * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 18b6cee71dSXin LI * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19b6cee71dSXin LI * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20b6cee71dSXin LI * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21b6cee71dSXin LI * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22b6cee71dSXin LI * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23b6cee71dSXin LI * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24b6cee71dSXin LI * POSSIBILITY OF SUCH DAMAGE. 25b6cee71dSXin LI */ 26b6cee71dSXin LI #include "file.h" 27b6cee71dSXin LI 28b6cee71dSXin LI #ifndef lint 29*40427ccaSGordon Tetlow FILE_RCSID("@(#)$File: readcdf.c,v 1.65 2017/04/08 20:58:03 christos Exp $") 30b6cee71dSXin LI #endif 31b6cee71dSXin LI 32b6cee71dSXin LI #include <assert.h> 33b6cee71dSXin LI #include <stdlib.h> 34b6cee71dSXin LI #include <unistd.h> 35b6cee71dSXin LI #include <string.h> 36b6cee71dSXin LI #include <time.h> 37b6cee71dSXin LI #include <ctype.h> 38b6cee71dSXin LI 39b6cee71dSXin LI #include "cdf.h" 40b6cee71dSXin LI #include "magic.h" 41b6cee71dSXin LI 425f0216bdSXin LI #ifndef __arraycount 435f0216bdSXin LI #define __arraycount(a) (sizeof(a) / sizeof(a[0])) 445f0216bdSXin LI #endif 455f0216bdSXin LI 46b6cee71dSXin LI #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0) 47b6cee71dSXin LI 48b6cee71dSXin LI static const struct nv { 49b6cee71dSXin LI const char *pattern; 50b6cee71dSXin LI const char *mime; 51b6cee71dSXin LI } app2mime[] = { 52b6cee71dSXin LI { "Word", "msword", }, 53b6cee71dSXin LI { "Excel", "vnd.ms-excel", }, 54b6cee71dSXin LI { "Powerpoint", "vnd.ms-powerpoint", }, 55b6cee71dSXin LI { "Crystal Reports", "x-rpt", }, 56b6cee71dSXin LI { "Advanced Installer", "vnd.ms-msi", }, 57b6cee71dSXin LI { "InstallShield", "vnd.ms-msi", }, 58b6cee71dSXin LI { "Microsoft Patch Compiler", "vnd.ms-msi", }, 59b6cee71dSXin LI { "NAnt", "vnd.ms-msi", }, 60b6cee71dSXin LI { "Windows Installer", "vnd.ms-msi", }, 61b6cee71dSXin LI { NULL, NULL, }, 62b6cee71dSXin LI }, name2mime[] = { 633e41d09dSXin LI { "Book", "vnd.ms-excel", }, 643e41d09dSXin LI { "Workbook", "vnd.ms-excel", }, 65b6cee71dSXin LI { "WordDocument", "msword", }, 66b6cee71dSXin LI { "PowerPoint", "vnd.ms-powerpoint", }, 67b6cee71dSXin LI { "DigitalSignature", "vnd.ms-msi", }, 68b6cee71dSXin LI { NULL, NULL, }, 69b6cee71dSXin LI }, name2desc[] = { 703e41d09dSXin LI { "Book", "Microsoft Excel", }, 713e41d09dSXin LI { "Workbook", "Microsoft Excel", }, 723e41d09dSXin LI { "WordDocument", "Microsoft Word", }, 73b6cee71dSXin LI { "PowerPoint", "Microsoft PowerPoint", }, 74b6cee71dSXin LI { "DigitalSignature", "Microsoft Installer", }, 75b6cee71dSXin LI { NULL, NULL, }, 76b6cee71dSXin LI }; 77b6cee71dSXin LI 78b6cee71dSXin LI static const struct cv { 79b6cee71dSXin LI uint64_t clsid[2]; 80b6cee71dSXin LI const char *mime; 81b6cee71dSXin LI } clsid2mime[] = { 82b6cee71dSXin LI { 83c2931133SXin LI { 0x00000000000c1084ULL, 0x46000000000000c0ULL }, 84b6cee71dSXin LI "x-msi", 85b6cee71dSXin LI }, 86b6cee71dSXin LI { { 0, 0 }, 87b6cee71dSXin LI NULL, 88b6cee71dSXin LI }, 89b6cee71dSXin LI }, clsid2desc[] = { 90b6cee71dSXin LI { 91c2931133SXin LI { 0x00000000000c1084ULL, 0x46000000000000c0ULL }, 92b6cee71dSXin LI "MSI Installer", 93b6cee71dSXin LI }, 94b6cee71dSXin LI { { 0, 0 }, 95b6cee71dSXin LI NULL, 96b6cee71dSXin LI }, 97b6cee71dSXin LI }; 98b6cee71dSXin LI 99b6cee71dSXin LI private const char * 100b6cee71dSXin LI cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv) 101b6cee71dSXin LI { 102b6cee71dSXin LI size_t i; 103b6cee71dSXin LI for (i = 0; cv[i].mime != NULL; i++) { 104b6cee71dSXin LI if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1]) 105b6cee71dSXin LI return cv[i].mime; 106b6cee71dSXin LI } 1075f0216bdSXin LI #ifdef CDF_DEBUG 1085f0216bdSXin LI fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0], 1095f0216bdSXin LI clsid[1]); 1105f0216bdSXin LI #endif 111b6cee71dSXin LI return NULL; 112b6cee71dSXin LI } 113b6cee71dSXin LI 114b6cee71dSXin LI private const char * 115b6cee71dSXin LI cdf_app_to_mime(const char *vbuf, const struct nv *nv) 116b6cee71dSXin LI { 117b6cee71dSXin LI size_t i; 118b6cee71dSXin LI const char *rv = NULL; 119c2931133SXin LI #ifdef USE_C_LOCALE 120c2931133SXin LI locale_t old_lc_ctype, c_lc_ctype; 121b6cee71dSXin LI 122c2931133SXin LI c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0); 123c2931133SXin LI assert(c_lc_ctype != NULL); 124c2931133SXin LI old_lc_ctype = uselocale(c_lc_ctype); 125b6cee71dSXin LI assert(old_lc_ctype != NULL); 1263e41d09dSXin LI #else 1273e41d09dSXin LI char *old_lc_ctype = setlocale(LC_CTYPE, "C"); 128c2931133SXin LI #endif 129b6cee71dSXin LI for (i = 0; nv[i].pattern != NULL; i++) 130b6cee71dSXin LI if (strcasestr(vbuf, nv[i].pattern) != NULL) { 131b6cee71dSXin LI rv = nv[i].mime; 132b6cee71dSXin LI break; 133b6cee71dSXin LI } 1345f0216bdSXin LI #ifdef CDF_DEBUG 1355f0216bdSXin LI fprintf(stderr, "unknown app %s\n", vbuf); 1365f0216bdSXin LI #endif 137c2931133SXin LI #ifdef USE_C_LOCALE 138c2931133SXin LI (void)uselocale(old_lc_ctype); 139c2931133SXin LI freelocale(c_lc_ctype); 1403e41d09dSXin LI #else 1413e41d09dSXin LI setlocale(LC_CTYPE, old_lc_ctype); 142c2931133SXin LI #endif 143b6cee71dSXin LI return rv; 144b6cee71dSXin LI } 145b6cee71dSXin LI 146b6cee71dSXin LI private int 147b6cee71dSXin LI cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info, 148b6cee71dSXin LI size_t count, const cdf_directory_t *root_storage) 149b6cee71dSXin LI { 150b6cee71dSXin LI size_t i; 151b6cee71dSXin LI cdf_timestamp_t tp; 152b6cee71dSXin LI struct timespec ts; 153b6cee71dSXin LI char buf[64]; 154b6cee71dSXin LI const char *str = NULL; 155*40427ccaSGordon Tetlow const char *s, *e; 156b6cee71dSXin LI int len; 157b6cee71dSXin LI 158b6cee71dSXin LI if (!NOTMIME(ms) && root_storage) 159b6cee71dSXin LI str = cdf_clsid_to_mime(root_storage->d_storage_uuid, 160b6cee71dSXin LI clsid2mime); 161b6cee71dSXin LI 162b6cee71dSXin LI for (i = 0; i < count; i++) { 163b6cee71dSXin LI cdf_print_property_name(buf, sizeof(buf), info[i].pi_id); 164b6cee71dSXin LI switch (info[i].pi_type) { 165b6cee71dSXin LI case CDF_NULL: 166b6cee71dSXin LI break; 167b6cee71dSXin LI case CDF_SIGNED16: 168b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf, 169b6cee71dSXin LI info[i].pi_s16) == -1) 170b6cee71dSXin LI return -1; 171b6cee71dSXin LI break; 172b6cee71dSXin LI case CDF_SIGNED32: 173b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf, 174b6cee71dSXin LI info[i].pi_s32) == -1) 175b6cee71dSXin LI return -1; 176b6cee71dSXin LI break; 177b6cee71dSXin LI case CDF_UNSIGNED32: 178b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf, 179b6cee71dSXin LI info[i].pi_u32) == -1) 180b6cee71dSXin LI return -1; 181b6cee71dSXin LI break; 182b6cee71dSXin LI case CDF_FLOAT: 183b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf, 184b6cee71dSXin LI info[i].pi_f) == -1) 185b6cee71dSXin LI return -1; 186b6cee71dSXin LI break; 187b6cee71dSXin LI case CDF_DOUBLE: 188b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf, 189b6cee71dSXin LI info[i].pi_d) == -1) 190b6cee71dSXin LI return -1; 191b6cee71dSXin LI break; 192b6cee71dSXin LI case CDF_LENGTH32_STRING: 193b6cee71dSXin LI case CDF_LENGTH32_WSTRING: 194b6cee71dSXin LI len = info[i].pi_str.s_len; 195b6cee71dSXin LI if (len > 1) { 196b6cee71dSXin LI char vbuf[1024]; 197b6cee71dSXin LI size_t j, k = 1; 198b6cee71dSXin LI 199b6cee71dSXin LI if (info[i].pi_type == CDF_LENGTH32_WSTRING) 200b6cee71dSXin LI k++; 201b6cee71dSXin LI s = info[i].pi_str.s_buf; 202*40427ccaSGordon Tetlow e = info[i].pi_str.s_buf + len; 203*40427ccaSGordon Tetlow for (j = 0; s < e && j < sizeof(vbuf) 204*40427ccaSGordon Tetlow && len--; s += k) { 205b6cee71dSXin LI if (*s == '\0') 206b6cee71dSXin LI break; 207b6cee71dSXin LI if (isprint((unsigned char)*s)) 208b6cee71dSXin LI vbuf[j++] = *s; 209b6cee71dSXin LI } 210b6cee71dSXin LI if (j == sizeof(vbuf)) 211b6cee71dSXin LI --j; 212b6cee71dSXin LI vbuf[j] = '\0'; 213b6cee71dSXin LI if (NOTMIME(ms)) { 214b6cee71dSXin LI if (vbuf[0]) { 215b6cee71dSXin LI if (file_printf(ms, ", %s: %s", 216b6cee71dSXin LI buf, vbuf) == -1) 217b6cee71dSXin LI return -1; 218b6cee71dSXin LI } 219b6cee71dSXin LI } else if (str == NULL && info[i].pi_id == 220b6cee71dSXin LI CDF_PROPERTY_NAME_OF_APPLICATION) { 221b6cee71dSXin LI str = cdf_app_to_mime(vbuf, app2mime); 222b6cee71dSXin LI } 223b6cee71dSXin LI } 224b6cee71dSXin LI break; 225b6cee71dSXin LI case CDF_FILETIME: 226b6cee71dSXin LI tp = info[i].pi_tp; 227b6cee71dSXin LI if (tp != 0) { 228b6cee71dSXin LI char tbuf[64]; 229b6cee71dSXin LI if (tp < 1000000000000000LL) { 230b6cee71dSXin LI cdf_print_elapsed_time(tbuf, 231b6cee71dSXin LI sizeof(tbuf), tp); 232b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, 233b6cee71dSXin LI ", %s: %s", buf, tbuf) == -1) 234b6cee71dSXin LI return -1; 235b6cee71dSXin LI } else { 236b6cee71dSXin LI char *c, *ec; 237b6cee71dSXin LI cdf_timestamp_to_timespec(&ts, tp); 238b6cee71dSXin LI c = cdf_ctime(&ts.tv_sec, tbuf); 239b6cee71dSXin LI if (c != NULL && 240b6cee71dSXin LI (ec = strchr(c, '\n')) != NULL) 241b6cee71dSXin LI *ec = '\0'; 242b6cee71dSXin LI 243b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, 244b6cee71dSXin LI ", %s: %s", buf, c) == -1) 245b6cee71dSXin LI return -1; 246b6cee71dSXin LI } 247b6cee71dSXin LI } 248b6cee71dSXin LI break; 249b6cee71dSXin LI case CDF_CLIPBOARD: 250b6cee71dSXin LI break; 251b6cee71dSXin LI default: 252b6cee71dSXin LI return -1; 253b6cee71dSXin LI } 254b6cee71dSXin LI } 255b6cee71dSXin LI if (!NOTMIME(ms)) { 256b6cee71dSXin LI if (str == NULL) 257b6cee71dSXin LI return 0; 258b6cee71dSXin LI if (file_printf(ms, "application/%s", str) == -1) 259b6cee71dSXin LI return -1; 260b6cee71dSXin LI } 261b6cee71dSXin LI return 1; 262b6cee71dSXin LI } 263b6cee71dSXin LI 264b6cee71dSXin LI private int 265c2931133SXin LI cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h, 266c2931133SXin LI const cdf_stream_t *sst) 267c2931133SXin LI { 268c2931133SXin LI cdf_catalog_t *cat; 269c2931133SXin LI size_t i; 270c2931133SXin LI char buf[256]; 271c2931133SXin LI cdf_catalog_entry_t *ce; 272c2931133SXin LI 273c2931133SXin LI if (NOTMIME(ms)) { 274c2931133SXin LI if (file_printf(ms, "Microsoft Thumbs.db [") == -1) 275c2931133SXin LI return -1; 276c2931133SXin LI if (cdf_unpack_catalog(h, sst, &cat) == -1) 277c2931133SXin LI return -1; 278c2931133SXin LI ce = cat->cat_e; 279c2931133SXin LI /* skip first entry since it has a , or paren */ 280c2931133SXin LI for (i = 1; i < cat->cat_num; i++) 281c2931133SXin LI if (file_printf(ms, "%s%s", 282c2931133SXin LI cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name), 283c2931133SXin LI i == cat->cat_num - 1 ? "]" : ", ") == -1) { 284c2931133SXin LI free(cat); 285c2931133SXin LI return -1; 286c2931133SXin LI } 287c2931133SXin LI free(cat); 288c2931133SXin LI } else { 289c2931133SXin LI if (file_printf(ms, "application/CDFV2") == -1) 290c2931133SXin LI return -1; 291c2931133SXin LI } 292c2931133SXin LI return 1; 293c2931133SXin LI } 294c2931133SXin LI 295c2931133SXin LI private int 296b6cee71dSXin LI cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h, 297b6cee71dSXin LI const cdf_stream_t *sst, const cdf_directory_t *root_storage) 298b6cee71dSXin LI { 299b6cee71dSXin LI cdf_summary_info_header_t si; 300b6cee71dSXin LI cdf_property_info_t *info; 301b6cee71dSXin LI size_t count; 302b6cee71dSXin LI int m; 303b6cee71dSXin LI 304b6cee71dSXin LI if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1) 305b6cee71dSXin LI return -1; 306b6cee71dSXin LI 307b6cee71dSXin LI if (NOTMIME(ms)) { 308b6cee71dSXin LI const char *str; 309b6cee71dSXin LI 310b6cee71dSXin LI if (file_printf(ms, "Composite Document File V2 Document") 311b6cee71dSXin LI == -1) 312b6cee71dSXin LI return -1; 313b6cee71dSXin LI 314b6cee71dSXin LI if (file_printf(ms, ", %s Endian", 315b6cee71dSXin LI si.si_byte_order == 0xfffe ? "Little" : "Big") == -1) 316b6cee71dSXin LI return -2; 317b6cee71dSXin LI switch (si.si_os) { 318b6cee71dSXin LI case 2: 319b6cee71dSXin LI if (file_printf(ms, ", Os: Windows, Version %d.%d", 320b6cee71dSXin LI si.si_os_version & 0xff, 321b6cee71dSXin LI (uint32_t)si.si_os_version >> 8) == -1) 322b6cee71dSXin LI return -2; 323b6cee71dSXin LI break; 324b6cee71dSXin LI case 1: 325b6cee71dSXin LI if (file_printf(ms, ", Os: MacOS, Version %d.%d", 326b6cee71dSXin LI (uint32_t)si.si_os_version >> 8, 327b6cee71dSXin LI si.si_os_version & 0xff) == -1) 328b6cee71dSXin LI return -2; 329b6cee71dSXin LI break; 330b6cee71dSXin LI default: 331b6cee71dSXin LI if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os, 332b6cee71dSXin LI si.si_os_version & 0xff, 333b6cee71dSXin LI (uint32_t)si.si_os_version >> 8) == -1) 334b6cee71dSXin LI return -2; 335b6cee71dSXin LI break; 336b6cee71dSXin LI } 337b6cee71dSXin LI if (root_storage) { 338b6cee71dSXin LI str = cdf_clsid_to_mime(root_storage->d_storage_uuid, 339b6cee71dSXin LI clsid2desc); 340c2931133SXin LI if (str) { 341b6cee71dSXin LI if (file_printf(ms, ", %s", str) == -1) 342b6cee71dSXin LI return -2; 343b6cee71dSXin LI } 344b6cee71dSXin LI } 345c2931133SXin LI } 346b6cee71dSXin LI 347b6cee71dSXin LI m = cdf_file_property_info(ms, info, count, root_storage); 348b6cee71dSXin LI free(info); 349b6cee71dSXin LI 350b6cee71dSXin LI return m == -1 ? -2 : m; 351b6cee71dSXin LI } 352b6cee71dSXin LI 353b6cee71dSXin LI #ifdef notdef 354b6cee71dSXin LI private char * 355b6cee71dSXin LI format_clsid(char *buf, size_t len, const uint64_t uuid[2]) { 356b6cee71dSXin LI snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" 357b6cee71dSXin LI PRIx64 "-%.12" PRIx64, 358c2931133SXin LI (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL, 359c2931133SXin LI (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL, 360c2931133SXin LI (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL, 361c2931133SXin LI (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL, 362c2931133SXin LI (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL); 363b6cee71dSXin LI return buf; 364b6cee71dSXin LI } 365b6cee71dSXin LI #endif 366b6cee71dSXin LI 3675f0216bdSXin LI private int 3685f0216bdSXin LI cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info, 3695f0216bdSXin LI const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat, 3705f0216bdSXin LI const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn) 3715f0216bdSXin LI { 3725f0216bdSXin LI int i; 3735f0216bdSXin LI 3745f0216bdSXin LI if ((i = cdf_read_user_stream(info, h, sat, ssat, sst, 3755f0216bdSXin LI dir, "Catalog", scn)) == -1) 3765f0216bdSXin LI return i; 3775f0216bdSXin LI #ifdef CDF_DEBUG 378a5d223e6SXin LI cdf_dump_catalog(h, scn); 3795f0216bdSXin LI #endif 3805f0216bdSXin LI if ((i = cdf_file_catalog(ms, h, scn)) == -1) 3815f0216bdSXin LI return -1; 3825f0216bdSXin LI return i; 3835f0216bdSXin LI } 3845f0216bdSXin LI 385a5d223e6SXin LI private int 386a5d223e6SXin LI cdf_check_summary_info(struct magic_set *ms, const cdf_info_t *info, 387a5d223e6SXin LI const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat, 388a5d223e6SXin LI const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn, 389a5d223e6SXin LI const cdf_directory_t *root_storage, const char **expn) 390a5d223e6SXin LI { 391a5d223e6SXin LI int i; 392a5d223e6SXin LI const char *str = NULL; 393a5d223e6SXin LI cdf_directory_t *d; 394a5d223e6SXin LI char name[__arraycount(d->d_name)]; 395a5d223e6SXin LI size_t j, k; 396a5d223e6SXin LI 397a5d223e6SXin LI #ifdef CDF_DEBUG 398a5d223e6SXin LI cdf_dump_summary_info(h, scn); 399a5d223e6SXin LI #endif 400a5d223e6SXin LI if ((i = cdf_file_summary_info(ms, h, scn, root_storage)) < 0) { 401a5d223e6SXin LI *expn = "Can't expand summary_info"; 402a5d223e6SXin LI return i; 403a5d223e6SXin LI } 404a5d223e6SXin LI if (i == 1) 405a5d223e6SXin LI return i; 406a5d223e6SXin LI for (j = 0; str == NULL && j < dir->dir_len; j++) { 407a5d223e6SXin LI d = &dir->dir_tab[j]; 408a5d223e6SXin LI for (k = 0; k < sizeof(name); k++) 409a5d223e6SXin LI name[k] = (char)cdf_tole2(d->d_name[k]); 410a5d223e6SXin LI str = cdf_app_to_mime(name, 411a5d223e6SXin LI NOTMIME(ms) ? name2desc : name2mime); 412a5d223e6SXin LI } 413a5d223e6SXin LI if (NOTMIME(ms)) { 414a5d223e6SXin LI if (str != NULL) { 415a5d223e6SXin LI if (file_printf(ms, "%s", str) == -1) 416a5d223e6SXin LI return -1; 417a5d223e6SXin LI i = 1; 418a5d223e6SXin LI } 419a5d223e6SXin LI } else { 420a5d223e6SXin LI if (str == NULL) 421a5d223e6SXin LI str = "vnd.ms-office"; 422a5d223e6SXin LI if (file_printf(ms, "application/%s", str) == -1) 423a5d223e6SXin LI return -1; 424a5d223e6SXin LI i = 1; 425a5d223e6SXin LI } 426a5d223e6SXin LI if (i <= 0) { 427a5d223e6SXin LI i = cdf_file_catalog_info(ms, info, h, sat, ssat, sst, 428a5d223e6SXin LI dir, scn); 429a5d223e6SXin LI } 430a5d223e6SXin LI return i; 431a5d223e6SXin LI } 432a5d223e6SXin LI 4335f0216bdSXin LI private struct sinfo { 4345f0216bdSXin LI const char *name; 4355f0216bdSXin LI const char *mime; 4365f0216bdSXin LI const char *sections[5]; 4375f0216bdSXin LI const int types[5]; 4385f0216bdSXin LI } sectioninfo[] = { 4395f0216bdSXin LI { "Encrypted", "encrypted", 4405f0216bdSXin LI { 441a5d223e6SXin LI "EncryptedPackage", "EncryptedSummary", 442a5d223e6SXin LI NULL, NULL, NULL, 4435f0216bdSXin LI }, 4445f0216bdSXin LI { 445a5d223e6SXin LI CDF_DIR_TYPE_USER_STREAM, 446a5d223e6SXin LI CDF_DIR_TYPE_USER_STREAM, 447a5d223e6SXin LI 0, 0, 0, 4485f0216bdSXin LI 4495f0216bdSXin LI }, 4505f0216bdSXin LI }, 4515f0216bdSXin LI { "QuickBooks", "quickbooks", 4525f0216bdSXin LI { 4535f0216bdSXin LI #if 0 4545f0216bdSXin LI "TaxForms", "PDFTaxForms", "modulesInBackup", 4555f0216bdSXin LI #endif 4565f0216bdSXin LI "mfbu_header", NULL, NULL, NULL, NULL, 4575f0216bdSXin LI }, 4585f0216bdSXin LI { 4595f0216bdSXin LI #if 0 4605f0216bdSXin LI CDF_DIR_TYPE_USER_STORAGE, 4615f0216bdSXin LI CDF_DIR_TYPE_USER_STORAGE, 4625f0216bdSXin LI CDF_DIR_TYPE_USER_STREAM, 4635f0216bdSXin LI #endif 4645f0216bdSXin LI CDF_DIR_TYPE_USER_STREAM, 4655f0216bdSXin LI 0, 0, 0, 0 4665f0216bdSXin LI }, 4675f0216bdSXin LI }, 468a5d223e6SXin LI { "Microsoft Excel", "vnd.ms-excel", 469a5d223e6SXin LI { 470a5d223e6SXin LI "Book", "Workbook", NULL, NULL, NULL, 471a5d223e6SXin LI }, 472a5d223e6SXin LI { 473a5d223e6SXin LI CDF_DIR_TYPE_USER_STREAM, 474a5d223e6SXin LI CDF_DIR_TYPE_USER_STREAM, 475a5d223e6SXin LI 0, 0, 0, 476a5d223e6SXin LI }, 477a5d223e6SXin LI }, 478a5d223e6SXin LI { "Microsoft Word", "msword", 479a5d223e6SXin LI { 480a5d223e6SXin LI "WordDocument", NULL, NULL, NULL, NULL, 481a5d223e6SXin LI }, 482a5d223e6SXin LI { 483a5d223e6SXin LI CDF_DIR_TYPE_USER_STREAM, 484a5d223e6SXin LI 0, 0, 0, 0, 485a5d223e6SXin LI }, 486a5d223e6SXin LI }, 487a5d223e6SXin LI { "Microsoft PowerPoint", "vnd.ms-powerpoint", 488a5d223e6SXin LI { 489a5d223e6SXin LI "PowerPoint", NULL, NULL, NULL, NULL, 490a5d223e6SXin LI }, 491a5d223e6SXin LI { 492a5d223e6SXin LI CDF_DIR_TYPE_USER_STREAM, 493a5d223e6SXin LI 0, 0, 0, 0, 494a5d223e6SXin LI }, 495a5d223e6SXin LI }, 496a5d223e6SXin LI { "Microsoft Outlook Message", "vnd.ms-outlook", 497a5d223e6SXin LI { 498a5d223e6SXin LI "__properties_version1.0", 499a5d223e6SXin LI "__recip_version1.0_#00000000", 500a5d223e6SXin LI NULL, NULL, NULL, 501a5d223e6SXin LI }, 502a5d223e6SXin LI { 503a5d223e6SXin LI CDF_DIR_TYPE_USER_STREAM, 504a5d223e6SXin LI CDF_DIR_TYPE_USER_STORAGE, 505a5d223e6SXin LI 0, 0, 0, 506a5d223e6SXin LI }, 507a5d223e6SXin LI }, 5085f0216bdSXin LI }; 5095f0216bdSXin LI 5105f0216bdSXin LI private int 5115f0216bdSXin LI cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir) 5125f0216bdSXin LI { 5135f0216bdSXin LI size_t sd, j; 5145f0216bdSXin LI 5155f0216bdSXin LI for (sd = 0; sd < __arraycount(sectioninfo); sd++) { 5165f0216bdSXin LI const struct sinfo *si = §ioninfo[sd]; 5175f0216bdSXin LI for (j = 0; si->sections[j]; j++) { 5185f0216bdSXin LI if (cdf_find_stream(dir, si->sections[j], si->types[j]) 519a5d223e6SXin LI > 0) 5205f0216bdSXin LI break; 521a5d223e6SXin LI #ifdef CDF_DEBUG 522a5d223e6SXin LI fprintf(stderr, "Can't read %s\n", si->sections[j]); 523a5d223e6SXin LI #endif 5245f0216bdSXin LI } 525a5d223e6SXin LI if (si->sections[j] == NULL) 5265f0216bdSXin LI continue; 5275f0216bdSXin LI if (NOTMIME(ms)) { 5285f0216bdSXin LI if (file_printf(ms, "CDFV2 %s", si->name) == -1) 5295f0216bdSXin LI return -1; 5305f0216bdSXin LI } else { 531a5d223e6SXin LI if (file_printf(ms, "application/%s", si->mime) == -1) 5325f0216bdSXin LI return -1; 5335f0216bdSXin LI } 5345f0216bdSXin LI return 1; 5355f0216bdSXin LI } 5365f0216bdSXin LI return -1; 5375f0216bdSXin LI } 5385f0216bdSXin LI 539b6cee71dSXin LI protected int 540b6cee71dSXin LI file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf, 541b6cee71dSXin LI size_t nbytes) 542b6cee71dSXin LI { 543b6cee71dSXin LI cdf_info_t info; 544b6cee71dSXin LI cdf_header_t h; 545b6cee71dSXin LI cdf_sat_t sat, ssat; 546b6cee71dSXin LI cdf_stream_t sst, scn; 547b6cee71dSXin LI cdf_dir_t dir; 548b6cee71dSXin LI int i; 549b6cee71dSXin LI const char *expn = ""; 550c2931133SXin LI const cdf_directory_t *root_storage; 551b6cee71dSXin LI 552a5d223e6SXin LI scn.sst_tab = NULL; 553b6cee71dSXin LI info.i_fd = fd; 554b6cee71dSXin LI info.i_buf = buf; 555b6cee71dSXin LI info.i_len = nbytes; 5565f0216bdSXin LI if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) 557b6cee71dSXin LI return 0; 558b6cee71dSXin LI if (cdf_read_header(&info, &h) == -1) 559b6cee71dSXin LI return 0; 560b6cee71dSXin LI #ifdef CDF_DEBUG 561b6cee71dSXin LI cdf_dump_header(&h); 562b6cee71dSXin LI #endif 563b6cee71dSXin LI 564b6cee71dSXin LI if ((i = cdf_read_sat(&info, &h, &sat)) == -1) { 565b6cee71dSXin LI expn = "Can't read SAT"; 566b6cee71dSXin LI goto out0; 567b6cee71dSXin LI } 568b6cee71dSXin LI #ifdef CDF_DEBUG 569b6cee71dSXin LI cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h)); 570b6cee71dSXin LI #endif 571b6cee71dSXin LI 572b6cee71dSXin LI if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) { 573b6cee71dSXin LI expn = "Can't read SSAT"; 574b6cee71dSXin LI goto out1; 575b6cee71dSXin LI } 576b6cee71dSXin LI #ifdef CDF_DEBUG 577b6cee71dSXin LI cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h)); 578b6cee71dSXin LI #endif 579b6cee71dSXin LI 580b6cee71dSXin LI if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) { 581b6cee71dSXin LI expn = "Can't read directory"; 582b6cee71dSXin LI goto out2; 583b6cee71dSXin LI } 584b6cee71dSXin LI 585b6cee71dSXin LI if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst, 586b6cee71dSXin LI &root_storage)) == -1) { 587b6cee71dSXin LI expn = "Cannot read short stream"; 588b6cee71dSXin LI goto out3; 589b6cee71dSXin LI } 590b6cee71dSXin LI #ifdef CDF_DEBUG 591b6cee71dSXin LI cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir); 592b6cee71dSXin LI #endif 593b6cee71dSXin LI #ifdef notdef 594b6cee71dSXin LI if (root_storage) { 595b6cee71dSXin LI if (NOTMIME(ms)) { 596b6cee71dSXin LI char clsbuf[128]; 597b6cee71dSXin LI if (file_printf(ms, "CLSID %s, ", 598b6cee71dSXin LI format_clsid(clsbuf, sizeof(clsbuf), 599b6cee71dSXin LI root_storage->d_storage_uuid)) == -1) 600b6cee71dSXin LI return -1; 601b6cee71dSXin LI } 602b6cee71dSXin LI } 603b6cee71dSXin LI #endif 604b6cee71dSXin LI 605b6cee71dSXin LI if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir, 606b6cee71dSXin LI "FileHeader", &scn)) != -1) { 607b6cee71dSXin LI #define HWP5_SIGNATURE "HWP Document File" 608*40427ccaSGordon Tetlow if (scn.sst_len * scn.sst_ss >= sizeof(HWP5_SIGNATURE) - 1 609b6cee71dSXin LI && memcmp(scn.sst_tab, HWP5_SIGNATURE, 610b6cee71dSXin LI sizeof(HWP5_SIGNATURE) - 1) == 0) { 611b6cee71dSXin LI if (NOTMIME(ms)) { 612b6cee71dSXin LI if (file_printf(ms, 613b6cee71dSXin LI "Hangul (Korean) Word Processor File 5.x") == -1) 614b6cee71dSXin LI return -1; 615b6cee71dSXin LI } else { 616b6cee71dSXin LI if (file_printf(ms, "application/x-hwp") == -1) 617b6cee71dSXin LI return -1; 618b6cee71dSXin LI } 619b6cee71dSXin LI i = 1; 620b6cee71dSXin LI goto out5; 621b6cee71dSXin LI } else { 622a5d223e6SXin LI cdf_zero_stream(&scn); 623b6cee71dSXin LI } 624b6cee71dSXin LI } 625b6cee71dSXin LI 626b6cee71dSXin LI if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir, 627b6cee71dSXin LI &scn)) == -1) { 6285f0216bdSXin LI if (errno != ESRCH) { 629b6cee71dSXin LI expn = "Cannot read summary info"; 630b6cee71dSXin LI } 631a5d223e6SXin LI } else { 632a5d223e6SXin LI i = cdf_check_summary_info(ms, &info, &h, 633a5d223e6SXin LI &sat, &ssat, &sst, &dir, &scn, root_storage, &expn); 634a5d223e6SXin LI cdf_zero_stream(&scn); 635a5d223e6SXin LI } 636a5d223e6SXin LI if (i <= 0) { 637a5d223e6SXin LI if ((i = cdf_read_doc_summary_info(&info, &h, &sat, &ssat, 638a5d223e6SXin LI &sst, &dir, &scn)) == -1) { 639a5d223e6SXin LI if (errno != ESRCH) { 640a5d223e6SXin LI expn = "Cannot read summary info"; 641a5d223e6SXin LI } 642a5d223e6SXin LI } else { 643a5d223e6SXin LI i = cdf_check_summary_info(ms, &info, &h, &sat, &ssat, 644a5d223e6SXin LI &sst, &dir, &scn, root_storage, &expn); 645a5d223e6SXin LI } 646a5d223e6SXin LI } 647a5d223e6SXin LI if (i <= 0) { 6485f0216bdSXin LI i = cdf_file_dir_info(ms, &dir); 6495f0216bdSXin LI if (i < 0) 6505f0216bdSXin LI expn = "Cannot read section info"; 651b6cee71dSXin LI } 652b6cee71dSXin LI out5: 653a5d223e6SXin LI cdf_zero_stream(&scn); 654a5d223e6SXin LI cdf_zero_stream(&sst); 655b6cee71dSXin LI out3: 656b6cee71dSXin LI free(dir.dir_tab); 657b6cee71dSXin LI out2: 658b6cee71dSXin LI free(ssat.sat_tab); 659b6cee71dSXin LI out1: 660b6cee71dSXin LI free(sat.sat_tab); 661b6cee71dSXin LI out0: 662b6cee71dSXin LI if (i == -1) { 663b6cee71dSXin LI if (NOTMIME(ms)) { 664b6cee71dSXin LI if (file_printf(ms, 665b6cee71dSXin LI "Composite Document File V2 Document") == -1) 666b6cee71dSXin LI return -1; 667b6cee71dSXin LI if (*expn) 6685f0216bdSXin LI if (file_printf(ms, ", %s", expn) == -1) 669b6cee71dSXin LI return -1; 670b6cee71dSXin LI } else { 671a5d223e6SXin LI if (file_printf(ms, "application/CDFV2") == -1) 672b6cee71dSXin LI return -1; 673b6cee71dSXin LI } 674b6cee71dSXin LI i = 1; 675b6cee71dSXin LI } 676b6cee71dSXin LI return i; 677b6cee71dSXin LI } 678