1b6cee71dSXin LI /*- 2b6cee71dSXin LI * Copyright (c) 2008 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*3e41d09dSXin LI FILE_RCSID("@(#)$File: readcdf.c,v 1.56 2016/03/03 22:20: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[] = { 63*3e41d09dSXin LI { "Book", "vnd.ms-excel", }, 64*3e41d09dSXin 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[] = { 70*3e41d09dSXin LI { "Book", "Microsoft Excel", }, 71*3e41d09dSXin LI { "Workbook", "Microsoft Excel", }, 72*3e41d09dSXin 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); 126*3e41d09dSXin LI #else 127*3e41d09dSXin 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); 140*3e41d09dSXin LI #else 141*3e41d09dSXin 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; 155b6cee71dSXin LI const char *s; 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; 202b6cee71dSXin LI for (j = 0; j < sizeof(vbuf) && len--; s += k) { 203b6cee71dSXin LI if (*s == '\0') 204b6cee71dSXin LI break; 205b6cee71dSXin LI if (isprint((unsigned char)*s)) 206b6cee71dSXin LI vbuf[j++] = *s; 207b6cee71dSXin LI } 208b6cee71dSXin LI if (j == sizeof(vbuf)) 209b6cee71dSXin LI --j; 210b6cee71dSXin LI vbuf[j] = '\0'; 211b6cee71dSXin LI if (NOTMIME(ms)) { 212b6cee71dSXin LI if (vbuf[0]) { 213b6cee71dSXin LI if (file_printf(ms, ", %s: %s", 214b6cee71dSXin LI buf, vbuf) == -1) 215b6cee71dSXin LI return -1; 216b6cee71dSXin LI } 217b6cee71dSXin LI } else if (str == NULL && info[i].pi_id == 218b6cee71dSXin LI CDF_PROPERTY_NAME_OF_APPLICATION) { 219b6cee71dSXin LI str = cdf_app_to_mime(vbuf, app2mime); 220b6cee71dSXin LI } 221b6cee71dSXin LI } 222b6cee71dSXin LI break; 223b6cee71dSXin LI case CDF_FILETIME: 224b6cee71dSXin LI tp = info[i].pi_tp; 225b6cee71dSXin LI if (tp != 0) { 226b6cee71dSXin LI char tbuf[64]; 227b6cee71dSXin LI if (tp < 1000000000000000LL) { 228b6cee71dSXin LI cdf_print_elapsed_time(tbuf, 229b6cee71dSXin LI sizeof(tbuf), tp); 230b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, 231b6cee71dSXin LI ", %s: %s", buf, tbuf) == -1) 232b6cee71dSXin LI return -1; 233b6cee71dSXin LI } else { 234b6cee71dSXin LI char *c, *ec; 235b6cee71dSXin LI cdf_timestamp_to_timespec(&ts, tp); 236b6cee71dSXin LI c = cdf_ctime(&ts.tv_sec, tbuf); 237b6cee71dSXin LI if (c != NULL && 238b6cee71dSXin LI (ec = strchr(c, '\n')) != NULL) 239b6cee71dSXin LI *ec = '\0'; 240b6cee71dSXin LI 241b6cee71dSXin LI if (NOTMIME(ms) && file_printf(ms, 242b6cee71dSXin LI ", %s: %s", buf, c) == -1) 243b6cee71dSXin LI return -1; 244b6cee71dSXin LI } 245b6cee71dSXin LI } 246b6cee71dSXin LI break; 247b6cee71dSXin LI case CDF_CLIPBOARD: 248b6cee71dSXin LI break; 249b6cee71dSXin LI default: 250b6cee71dSXin LI return -1; 251b6cee71dSXin LI } 252b6cee71dSXin LI } 253b6cee71dSXin LI if (!NOTMIME(ms)) { 254b6cee71dSXin LI if (str == NULL) 255b6cee71dSXin LI return 0; 256b6cee71dSXin LI if (file_printf(ms, "application/%s", str) == -1) 257b6cee71dSXin LI return -1; 258b6cee71dSXin LI } 259b6cee71dSXin LI return 1; 260b6cee71dSXin LI } 261b6cee71dSXin LI 262b6cee71dSXin LI private int 263c2931133SXin LI cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h, 264c2931133SXin LI const cdf_stream_t *sst) 265c2931133SXin LI { 266c2931133SXin LI cdf_catalog_t *cat; 267c2931133SXin LI size_t i; 268c2931133SXin LI char buf[256]; 269c2931133SXin LI cdf_catalog_entry_t *ce; 270c2931133SXin LI 271c2931133SXin LI if (NOTMIME(ms)) { 272c2931133SXin LI if (file_printf(ms, "Microsoft Thumbs.db [") == -1) 273c2931133SXin LI return -1; 274c2931133SXin LI if (cdf_unpack_catalog(h, sst, &cat) == -1) 275c2931133SXin LI return -1; 276c2931133SXin LI ce = cat->cat_e; 277c2931133SXin LI /* skip first entry since it has a , or paren */ 278c2931133SXin LI for (i = 1; i < cat->cat_num; i++) 279c2931133SXin LI if (file_printf(ms, "%s%s", 280c2931133SXin LI cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name), 281c2931133SXin LI i == cat->cat_num - 1 ? "]" : ", ") == -1) { 282c2931133SXin LI free(cat); 283c2931133SXin LI return -1; 284c2931133SXin LI } 285c2931133SXin LI free(cat); 286c2931133SXin LI } else { 287c2931133SXin LI if (file_printf(ms, "application/CDFV2") == -1) 288c2931133SXin LI return -1; 289c2931133SXin LI } 290c2931133SXin LI return 1; 291c2931133SXin LI } 292c2931133SXin LI 293c2931133SXin LI private int 294b6cee71dSXin LI cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h, 295b6cee71dSXin LI const cdf_stream_t *sst, const cdf_directory_t *root_storage) 296b6cee71dSXin LI { 297b6cee71dSXin LI cdf_summary_info_header_t si; 298b6cee71dSXin LI cdf_property_info_t *info; 299b6cee71dSXin LI size_t count; 300b6cee71dSXin LI int m; 301b6cee71dSXin LI 302b6cee71dSXin LI if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1) 303b6cee71dSXin LI return -1; 304b6cee71dSXin LI 305b6cee71dSXin LI if (NOTMIME(ms)) { 306b6cee71dSXin LI const char *str; 307b6cee71dSXin LI 308b6cee71dSXin LI if (file_printf(ms, "Composite Document File V2 Document") 309b6cee71dSXin LI == -1) 310b6cee71dSXin LI return -1; 311b6cee71dSXin LI 312b6cee71dSXin LI if (file_printf(ms, ", %s Endian", 313b6cee71dSXin LI si.si_byte_order == 0xfffe ? "Little" : "Big") == -1) 314b6cee71dSXin LI return -2; 315b6cee71dSXin LI switch (si.si_os) { 316b6cee71dSXin LI case 2: 317b6cee71dSXin LI if (file_printf(ms, ", Os: Windows, Version %d.%d", 318b6cee71dSXin LI si.si_os_version & 0xff, 319b6cee71dSXin LI (uint32_t)si.si_os_version >> 8) == -1) 320b6cee71dSXin LI return -2; 321b6cee71dSXin LI break; 322b6cee71dSXin LI case 1: 323b6cee71dSXin LI if (file_printf(ms, ", Os: MacOS, Version %d.%d", 324b6cee71dSXin LI (uint32_t)si.si_os_version >> 8, 325b6cee71dSXin LI si.si_os_version & 0xff) == -1) 326b6cee71dSXin LI return -2; 327b6cee71dSXin LI break; 328b6cee71dSXin LI default: 329b6cee71dSXin LI if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os, 330b6cee71dSXin LI si.si_os_version & 0xff, 331b6cee71dSXin LI (uint32_t)si.si_os_version >> 8) == -1) 332b6cee71dSXin LI return -2; 333b6cee71dSXin LI break; 334b6cee71dSXin LI } 335b6cee71dSXin LI if (root_storage) { 336b6cee71dSXin LI str = cdf_clsid_to_mime(root_storage->d_storage_uuid, 337b6cee71dSXin LI clsid2desc); 338c2931133SXin LI if (str) { 339b6cee71dSXin LI if (file_printf(ms, ", %s", str) == -1) 340b6cee71dSXin LI return -2; 341b6cee71dSXin LI } 342b6cee71dSXin LI } 343c2931133SXin LI } 344b6cee71dSXin LI 345b6cee71dSXin LI m = cdf_file_property_info(ms, info, count, root_storage); 346b6cee71dSXin LI free(info); 347b6cee71dSXin LI 348b6cee71dSXin LI return m == -1 ? -2 : m; 349b6cee71dSXin LI } 350b6cee71dSXin LI 351b6cee71dSXin LI #ifdef notdef 352b6cee71dSXin LI private char * 353b6cee71dSXin LI format_clsid(char *buf, size_t len, const uint64_t uuid[2]) { 354b6cee71dSXin LI snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" 355b6cee71dSXin LI PRIx64 "-%.12" PRIx64, 356c2931133SXin LI (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL, 357c2931133SXin LI (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL, 358c2931133SXin LI (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL, 359c2931133SXin LI (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL, 360c2931133SXin LI (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL); 361b6cee71dSXin LI return buf; 362b6cee71dSXin LI } 363b6cee71dSXin LI #endif 364b6cee71dSXin LI 3655f0216bdSXin LI private int 3665f0216bdSXin LI cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info, 3675f0216bdSXin LI const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat, 3685f0216bdSXin LI const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn) 3695f0216bdSXin LI { 3705f0216bdSXin LI int i; 3715f0216bdSXin LI 3725f0216bdSXin LI if ((i = cdf_read_user_stream(info, h, sat, ssat, sst, 3735f0216bdSXin LI dir, "Catalog", scn)) == -1) 3745f0216bdSXin LI return i; 3755f0216bdSXin LI #ifdef CDF_DEBUG 3765f0216bdSXin LI cdf_dump_catalog(&h, &scn); 3775f0216bdSXin LI #endif 3785f0216bdSXin LI if ((i = cdf_file_catalog(ms, h, scn)) == -1) 3795f0216bdSXin LI return -1; 3805f0216bdSXin LI return i; 3815f0216bdSXin LI } 3825f0216bdSXin LI 3835f0216bdSXin LI private struct sinfo { 3845f0216bdSXin LI const char *name; 3855f0216bdSXin LI const char *mime; 3865f0216bdSXin LI const char *sections[5]; 3875f0216bdSXin LI const int types[5]; 3885f0216bdSXin LI } sectioninfo[] = { 3895f0216bdSXin LI { "Encrypted", "encrypted", 3905f0216bdSXin LI { 3915f0216bdSXin LI "EncryptedPackage", NULL, NULL, NULL, NULL, 3925f0216bdSXin LI }, 3935f0216bdSXin LI { 3945f0216bdSXin LI CDF_DIR_TYPE_USER_STREAM, 0, 0, 0, 0, 3955f0216bdSXin LI 3965f0216bdSXin LI }, 3975f0216bdSXin LI }, 3985f0216bdSXin LI { "QuickBooks", "quickbooks", 3995f0216bdSXin LI { 4005f0216bdSXin LI #if 0 4015f0216bdSXin LI "TaxForms", "PDFTaxForms", "modulesInBackup", 4025f0216bdSXin LI #endif 4035f0216bdSXin LI "mfbu_header", NULL, NULL, NULL, NULL, 4045f0216bdSXin LI }, 4055f0216bdSXin LI { 4065f0216bdSXin LI #if 0 4075f0216bdSXin LI CDF_DIR_TYPE_USER_STORAGE, 4085f0216bdSXin LI CDF_DIR_TYPE_USER_STORAGE, 4095f0216bdSXin LI CDF_DIR_TYPE_USER_STREAM, 4105f0216bdSXin LI #endif 4115f0216bdSXin LI CDF_DIR_TYPE_USER_STREAM, 4125f0216bdSXin LI 0, 0, 0, 0 4135f0216bdSXin LI }, 4145f0216bdSXin LI }, 4155f0216bdSXin LI }; 4165f0216bdSXin LI 4175f0216bdSXin LI private int 4185f0216bdSXin LI cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir) 4195f0216bdSXin LI { 4205f0216bdSXin LI size_t sd, j; 4215f0216bdSXin LI 4225f0216bdSXin LI for (sd = 0; sd < __arraycount(sectioninfo); sd++) { 4235f0216bdSXin LI const struct sinfo *si = §ioninfo[sd]; 4245f0216bdSXin LI for (j = 0; si->sections[j]; j++) { 4255f0216bdSXin LI if (cdf_find_stream(dir, si->sections[j], si->types[j]) 4265f0216bdSXin LI <= 0) { 4275f0216bdSXin LI #ifdef CDF_DEBUG 4285f0216bdSXin LI fprintf(stderr, "Can't read %s\n", 4295f0216bdSXin LI si->sections[j]); 4305f0216bdSXin LI #endif 4315f0216bdSXin LI break; 4325f0216bdSXin LI } 4335f0216bdSXin LI } 4345f0216bdSXin LI if (si->sections[j] != NULL) 4355f0216bdSXin LI continue; 4365f0216bdSXin LI if (NOTMIME(ms)) { 4375f0216bdSXin LI if (file_printf(ms, "CDFV2 %s", si->name) == -1) 4385f0216bdSXin LI return -1; 4395f0216bdSXin LI } else { 4405f0216bdSXin LI if (file_printf(ms, "application/CDFV2-%s", 4415f0216bdSXin LI si->mime) == -1) 4425f0216bdSXin LI return -1; 4435f0216bdSXin LI } 4445f0216bdSXin LI return 1; 4455f0216bdSXin LI } 4465f0216bdSXin LI return -1; 4475f0216bdSXin LI } 4485f0216bdSXin LI 449b6cee71dSXin LI protected int 450b6cee71dSXin LI file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf, 451b6cee71dSXin LI size_t nbytes) 452b6cee71dSXin LI { 453b6cee71dSXin LI cdf_info_t info; 454b6cee71dSXin LI cdf_header_t h; 455b6cee71dSXin LI cdf_sat_t sat, ssat; 456b6cee71dSXin LI cdf_stream_t sst, scn; 457b6cee71dSXin LI cdf_dir_t dir; 458b6cee71dSXin LI int i; 459b6cee71dSXin LI const char *expn = ""; 460c2931133SXin LI const cdf_directory_t *root_storage; 461b6cee71dSXin LI 462b6cee71dSXin LI info.i_fd = fd; 463b6cee71dSXin LI info.i_buf = buf; 464b6cee71dSXin LI info.i_len = nbytes; 4655f0216bdSXin LI if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION)) 466b6cee71dSXin LI return 0; 467b6cee71dSXin LI if (cdf_read_header(&info, &h) == -1) 468b6cee71dSXin LI return 0; 469b6cee71dSXin LI #ifdef CDF_DEBUG 470b6cee71dSXin LI cdf_dump_header(&h); 471b6cee71dSXin LI #endif 472b6cee71dSXin LI 473b6cee71dSXin LI if ((i = cdf_read_sat(&info, &h, &sat)) == -1) { 474b6cee71dSXin LI expn = "Can't read SAT"; 475b6cee71dSXin LI goto out0; 476b6cee71dSXin LI } 477b6cee71dSXin LI #ifdef CDF_DEBUG 478b6cee71dSXin LI cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h)); 479b6cee71dSXin LI #endif 480b6cee71dSXin LI 481b6cee71dSXin LI if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) { 482b6cee71dSXin LI expn = "Can't read SSAT"; 483b6cee71dSXin LI goto out1; 484b6cee71dSXin LI } 485b6cee71dSXin LI #ifdef CDF_DEBUG 486b6cee71dSXin LI cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h)); 487b6cee71dSXin LI #endif 488b6cee71dSXin LI 489b6cee71dSXin LI if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) { 490b6cee71dSXin LI expn = "Can't read directory"; 491b6cee71dSXin LI goto out2; 492b6cee71dSXin LI } 493b6cee71dSXin LI 494b6cee71dSXin LI if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst, 495b6cee71dSXin LI &root_storage)) == -1) { 496b6cee71dSXin LI expn = "Cannot read short stream"; 497b6cee71dSXin LI goto out3; 498b6cee71dSXin LI } 499b6cee71dSXin LI #ifdef CDF_DEBUG 500b6cee71dSXin LI cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir); 501b6cee71dSXin LI #endif 502b6cee71dSXin LI #ifdef notdef 503b6cee71dSXin LI if (root_storage) { 504b6cee71dSXin LI if (NOTMIME(ms)) { 505b6cee71dSXin LI char clsbuf[128]; 506b6cee71dSXin LI if (file_printf(ms, "CLSID %s, ", 507b6cee71dSXin LI format_clsid(clsbuf, sizeof(clsbuf), 508b6cee71dSXin LI root_storage->d_storage_uuid)) == -1) 509b6cee71dSXin LI return -1; 510b6cee71dSXin LI } 511b6cee71dSXin LI } 512b6cee71dSXin LI #endif 513b6cee71dSXin LI 514b6cee71dSXin LI if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir, 515b6cee71dSXin LI "FileHeader", &scn)) != -1) { 516b6cee71dSXin LI #define HWP5_SIGNATURE "HWP Document File" 517b6cee71dSXin LI if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1 518b6cee71dSXin LI && memcmp(scn.sst_tab, HWP5_SIGNATURE, 519b6cee71dSXin LI sizeof(HWP5_SIGNATURE) - 1) == 0) { 520b6cee71dSXin LI if (NOTMIME(ms)) { 521b6cee71dSXin LI if (file_printf(ms, 522b6cee71dSXin LI "Hangul (Korean) Word Processor File 5.x") == -1) 523b6cee71dSXin LI return -1; 524b6cee71dSXin LI } else { 525b6cee71dSXin LI if (file_printf(ms, "application/x-hwp") == -1) 526b6cee71dSXin LI return -1; 527b6cee71dSXin LI } 528b6cee71dSXin LI i = 1; 529b6cee71dSXin LI goto out5; 530b6cee71dSXin LI } else { 531b6cee71dSXin LI free(scn.sst_tab); 532b6cee71dSXin LI scn.sst_tab = NULL; 533b6cee71dSXin LI scn.sst_len = 0; 534b6cee71dSXin LI scn.sst_dirlen = 0; 535b6cee71dSXin LI } 536b6cee71dSXin LI } 537b6cee71dSXin LI 538b6cee71dSXin LI if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir, 539b6cee71dSXin LI &scn)) == -1) { 5405f0216bdSXin LI if (errno != ESRCH) { 541b6cee71dSXin LI expn = "Cannot read summary info"; 542b6cee71dSXin LI goto out4; 543b6cee71dSXin LI } 5445f0216bdSXin LI i = cdf_file_catalog_info(ms, &info, &h, &sat, &ssat, &sst, 5455f0216bdSXin LI &dir, &scn); 5465f0216bdSXin LI if (i > 0) 5475f0216bdSXin LI goto out4; 5485f0216bdSXin LI i = cdf_file_dir_info(ms, &dir); 5495f0216bdSXin LI if (i < 0) 5505f0216bdSXin LI expn = "Cannot read section info"; 5515f0216bdSXin LI goto out4; 5525f0216bdSXin LI } 5535f0216bdSXin LI 5545f0216bdSXin LI 555b6cee71dSXin LI #ifdef CDF_DEBUG 556b6cee71dSXin LI cdf_dump_summary_info(&h, &scn); 557b6cee71dSXin LI #endif 558b6cee71dSXin LI if ((i = cdf_file_summary_info(ms, &h, &scn, root_storage)) < 0) 559b6cee71dSXin LI expn = "Can't expand summary_info"; 560b6cee71dSXin LI 561b6cee71dSXin LI if (i == 0) { 562b6cee71dSXin LI const char *str = NULL; 563b6cee71dSXin LI cdf_directory_t *d; 564b6cee71dSXin LI char name[__arraycount(d->d_name)]; 565b6cee71dSXin LI size_t j, k; 566b6cee71dSXin LI 567b6cee71dSXin LI for (j = 0; str == NULL && j < dir.dir_len; j++) { 568b6cee71dSXin LI d = &dir.dir_tab[j]; 569b6cee71dSXin LI for (k = 0; k < sizeof(name); k++) 570b6cee71dSXin LI name[k] = (char)cdf_tole2(d->d_name[k]); 571b6cee71dSXin LI str = cdf_app_to_mime(name, 572b6cee71dSXin LI NOTMIME(ms) ? name2desc : name2mime); 573b6cee71dSXin LI } 574b6cee71dSXin LI if (NOTMIME(ms)) { 575b6cee71dSXin LI if (str != NULL) { 576b6cee71dSXin LI if (file_printf(ms, "%s", str) == -1) 577b6cee71dSXin LI return -1; 578b6cee71dSXin LI i = 1; 579b6cee71dSXin LI } 580b6cee71dSXin LI } else { 581b6cee71dSXin LI if (str == NULL) 582b6cee71dSXin LI str = "vnd.ms-office"; 583b6cee71dSXin LI if (file_printf(ms, "application/%s", str) == -1) 584b6cee71dSXin LI return -1; 585b6cee71dSXin LI i = 1; 586b6cee71dSXin LI } 587b6cee71dSXin LI } 588b6cee71dSXin LI out5: 589b6cee71dSXin LI free(scn.sst_tab); 590b6cee71dSXin LI out4: 591b6cee71dSXin LI free(sst.sst_tab); 592b6cee71dSXin LI out3: 593b6cee71dSXin LI free(dir.dir_tab); 594b6cee71dSXin LI out2: 595b6cee71dSXin LI free(ssat.sat_tab); 596b6cee71dSXin LI out1: 597b6cee71dSXin LI free(sat.sat_tab); 598b6cee71dSXin LI out0: 599b6cee71dSXin LI if (i == -1) { 600b6cee71dSXin LI if (NOTMIME(ms)) { 601b6cee71dSXin LI if (file_printf(ms, 602b6cee71dSXin LI "Composite Document File V2 Document") == -1) 603b6cee71dSXin LI return -1; 604b6cee71dSXin LI if (*expn) 6055f0216bdSXin LI if (file_printf(ms, ", %s", expn) == -1) 606b6cee71dSXin LI return -1; 607b6cee71dSXin LI } else { 6085f0216bdSXin LI if (file_printf(ms, "application/CDFV2-unknown") == -1) 609b6cee71dSXin LI return -1; 610b6cee71dSXin LI } 611b6cee71dSXin LI i = 1; 612b6cee71dSXin LI } 613b6cee71dSXin LI return i; 614b6cee71dSXin LI } 615