readcdf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*-
  2. * Copyright (c) 2008, 2016 Christos Zoulas
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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.
  13. *
  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.
  25. */
  26. #include "file.h"
  27. #ifndef lint
  28. FILE_RCSID("@(#)$File: readcdf.c,v 1.74 2019/09/11 15:46:30 christos Exp $")
  29. #endif
  30. #include <assert.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <ctype.h>
  36. #include "cdf.h"
  37. #include "magic.h"
  38. #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
  39. static const struct nv {
  40. const char *pattern;
  41. const char *mime;
  42. } app2mime[] = {
  43. { "Word", "msword", },
  44. { "Excel", "vnd.ms-excel", },
  45. { "Powerpoint", "vnd.ms-powerpoint", },
  46. { "Crystal Reports", "x-rpt", },
  47. { "Advanced Installer", "vnd.ms-msi", },
  48. { "InstallShield", "vnd.ms-msi", },
  49. { "Microsoft Patch Compiler", "vnd.ms-msi", },
  50. { "NAnt", "vnd.ms-msi", },
  51. { "Windows Installer", "vnd.ms-msi", },
  52. { NULL, NULL, },
  53. }, name2mime[] = {
  54. { "Book", "vnd.ms-excel", },
  55. { "Workbook", "vnd.ms-excel", },
  56. { "WordDocument", "msword", },
  57. { "PowerPoint", "vnd.ms-powerpoint", },
  58. { "DigitalSignature", "vnd.ms-msi", },
  59. { NULL, NULL, },
  60. }, name2desc[] = {
  61. { "Book", "Microsoft Excel", },
  62. { "Workbook", "Microsoft Excel", },
  63. { "WordDocument", "Microsoft Word", },
  64. { "PowerPoint", "Microsoft PowerPoint", },
  65. { "DigitalSignature", "Microsoft Installer", },
  66. { NULL, NULL, },
  67. };
  68. static const struct cv {
  69. uint64_t clsid[2];
  70. const char *mime;
  71. } clsid2mime[] = {
  72. {
  73. { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
  74. "x-msi",
  75. },
  76. { { 0, 0 },
  77. NULL,
  78. },
  79. }, clsid2desc[] = {
  80. {
  81. { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
  82. "MSI Installer",
  83. },
  84. { { 0, 0 },
  85. NULL,
  86. },
  87. };
  88. private const char *
  89. cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
  90. {
  91. size_t i;
  92. for (i = 0; cv[i].mime != NULL; i++) {
  93. if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
  94. return cv[i].mime;
  95. }
  96. #ifdef CDF_DEBUG
  97. fprintf(stderr, "unknown mime %" PRIx64 ", %" PRIx64 "\n", clsid[0],
  98. clsid[1]);
  99. #endif
  100. return NULL;
  101. }
  102. private const char *
  103. cdf_app_to_mime(const char *vbuf, const struct nv *nv)
  104. {
  105. size_t i;
  106. const char *rv = NULL;
  107. #ifdef USE_C_LOCALE
  108. locale_t old_lc_ctype, c_lc_ctype;
  109. c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
  110. assert(c_lc_ctype != NULL);
  111. old_lc_ctype = uselocale(c_lc_ctype);
  112. assert(old_lc_ctype != NULL);
  113. #else
  114. char *old_lc_ctype = setlocale(LC_CTYPE, NULL);
  115. assert(old_lc_ctype != NULL);
  116. old_lc_ctype = strdup(old_lc_ctype);
  117. assert(old_lc_ctype != NULL);
  118. (void)setlocale(LC_CTYPE, "C");
  119. #endif
  120. for (i = 0; nv[i].pattern != NULL; i++)
  121. if (strcasestr(vbuf, nv[i].pattern) != NULL) {
  122. rv = nv[i].mime;
  123. break;
  124. }
  125. #ifdef CDF_DEBUG
  126. fprintf(stderr, "unknown app %s\n", vbuf);
  127. #endif
  128. #ifdef USE_C_LOCALE
  129. (void)uselocale(old_lc_ctype);
  130. freelocale(c_lc_ctype);
  131. #else
  132. (void)setlocale(LC_CTYPE, old_lc_ctype);
  133. free(old_lc_ctype);
  134. #endif
  135. return rv;
  136. }
  137. private int
  138. cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
  139. size_t count, const cdf_directory_t *root_storage)
  140. {
  141. size_t i;
  142. cdf_timestamp_t tp;
  143. struct timespec ts;
  144. char buf[64];
  145. const char *str = NULL;
  146. const char *s, *e;
  147. int len;
  148. if (!NOTMIME(ms) && root_storage)
  149. str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
  150. clsid2mime);
  151. for (i = 0; i < count; i++) {
  152. cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
  153. switch (info[i].pi_type) {
  154. case CDF_NULL:
  155. break;
  156. case CDF_SIGNED16:
  157. if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
  158. info[i].pi_s16) == -1)
  159. return -1;
  160. break;
  161. case CDF_SIGNED32:
  162. if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
  163. info[i].pi_s32) == -1)
  164. return -1;
  165. break;
  166. case CDF_UNSIGNED32:
  167. if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
  168. info[i].pi_u32) == -1)
  169. return -1;
  170. break;
  171. case CDF_FLOAT:
  172. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  173. info[i].pi_f) == -1)
  174. return -1;
  175. break;
  176. case CDF_DOUBLE:
  177. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  178. info[i].pi_d) == -1)
  179. return -1;
  180. break;
  181. case CDF_LENGTH32_STRING:
  182. case CDF_LENGTH32_WSTRING:
  183. len = info[i].pi_str.s_len;
  184. if (len > 1) {
  185. char vbuf[1024];
  186. size_t j, k = 1;
  187. if (info[i].pi_type == CDF_LENGTH32_WSTRING)
  188. k++;
  189. s = info[i].pi_str.s_buf;
  190. e = info[i].pi_str.s_buf + len;
  191. for (j = 0; s < e && j < sizeof(vbuf)
  192. && len--; s += k) {
  193. if (*s == '\0')
  194. break;
  195. if (isprint(CAST(unsigned char, *s)))
  196. vbuf[j++] = *s;
  197. }
  198. if (j == sizeof(vbuf))
  199. --j;
  200. vbuf[j] = '\0';
  201. if (NOTMIME(ms)) {
  202. if (vbuf[0]) {
  203. if (file_printf(ms, ", %s: %s",
  204. buf, vbuf) == -1)
  205. return -1;
  206. }
  207. } else if (str == NULL && info[i].pi_id ==
  208. CDF_PROPERTY_NAME_OF_APPLICATION) {
  209. str = cdf_app_to_mime(vbuf, app2mime);
  210. }
  211. }
  212. break;
  213. case CDF_FILETIME:
  214. tp = info[i].pi_tp;
  215. if (tp != 0) {
  216. char tbuf[64];
  217. if (tp < 1000000000000000LL) {
  218. cdf_print_elapsed_time(tbuf,
  219. sizeof(tbuf), tp);
  220. if (NOTMIME(ms) && file_printf(ms,
  221. ", %s: %s", buf, tbuf) == -1)
  222. return -1;
  223. } else {
  224. char *c, *ec;
  225. cdf_timestamp_to_timespec(&ts, tp);
  226. c = cdf_ctime(&ts.tv_sec, tbuf);
  227. if (c != NULL &&
  228. (ec = strchr(c, '\n')) != NULL)
  229. *ec = '\0';
  230. if (NOTMIME(ms) && file_printf(ms,
  231. ", %s: %s", buf, c) == -1)
  232. return -1;
  233. }
  234. }
  235. break;
  236. case CDF_CLIPBOARD:
  237. break;
  238. default:
  239. return -1;
  240. }
  241. }
  242. if (ms->flags & MAGIC_MIME_TYPE) {
  243. if (str == NULL)
  244. return 0;
  245. if (file_printf(ms, "application/%s", str) == -1)
  246. return -1;
  247. }
  248. return 1;
  249. }
  250. private int
  251. cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
  252. const cdf_stream_t *sst)
  253. {
  254. cdf_catalog_t *cat;
  255. size_t i;
  256. char buf[256];
  257. cdf_catalog_entry_t *ce;
  258. if (NOTMIME(ms)) {
  259. if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
  260. return -1;
  261. if (cdf_unpack_catalog(h, sst, &cat) == -1)
  262. return -1;
  263. ce = cat->cat_e;
  264. /* skip first entry since it has a , or paren */
  265. for (i = 1; i < cat->cat_num; i++)
  266. if (file_printf(ms, "%s%s",
  267. cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
  268. i == cat->cat_num - 1 ? "]" : ", ") == -1) {
  269. free(cat);
  270. return -1;
  271. }
  272. free(cat);
  273. } else if (ms->flags & MAGIC_MIME_TYPE) {
  274. if (file_printf(ms, "application/CDFV2") == -1)
  275. return -1;
  276. }
  277. return 1;
  278. }
  279. private int
  280. cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
  281. const cdf_stream_t *sst, const cdf_directory_t *root_storage)
  282. {
  283. cdf_summary_info_header_t si;
  284. cdf_property_info_t *info;
  285. size_t count;
  286. int m;
  287. if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
  288. return -1;
  289. if (NOTMIME(ms)) {
  290. const char *str;
  291. if (file_printf(ms, "Composite Document File V2 Document")
  292. == -1)
  293. return -1;
  294. if (file_printf(ms, ", %s Endian",
  295. si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
  296. return -2;
  297. switch (si.si_os) {
  298. case 2:
  299. if (file_printf(ms, ", Os: Windows, Version %d.%d",
  300. si.si_os_version & 0xff,
  301. CAST(uint32_t, si.si_os_version) >> 8) == -1)
  302. return -2;
  303. break;
  304. case 1:
  305. if (file_printf(ms, ", Os: MacOS, Version %d.%d",
  306. CAST(uint32_t, si.si_os_version) >> 8,
  307. si.si_os_version & 0xff) == -1)
  308. return -2;
  309. break;
  310. default:
  311. if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
  312. si.si_os_version & 0xff,
  313. CAST(uint32_t, si.si_os_version) >> 8) == -1)
  314. return -2;
  315. break;
  316. }
  317. if (root_storage) {
  318. str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
  319. clsid2desc);
  320. if (str) {
  321. if (file_printf(ms, ", %s", str) == -1)
  322. return -2;
  323. }
  324. }
  325. }
  326. m = cdf_file_property_info(ms, info, count, root_storage);
  327. free(info);
  328. return m == -1 ? -2 : m;
  329. }
  330. #ifdef notdef
  331. private char *
  332. format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
  333. snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
  334. PRIx64 "-%.12" PRIx64,
  335. (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
  336. (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
  337. (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
  338. (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
  339. (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL);
  340. return buf;
  341. }
  342. #endif
  343. private int
  344. cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info,
  345. const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
  346. const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn)
  347. {
  348. int i;
  349. if ((i = cdf_read_user_stream(info, h, sat, ssat, sst,
  350. dir, "Catalog", scn)) == -1)
  351. return i;
  352. #ifdef CDF_DEBUG
  353. cdf_dump_catalog(h, scn);
  354. #endif
  355. if ((i = cdf_file_catalog(ms, h, scn)) == -1)
  356. return -1;
  357. return i;
  358. }
  359. private int
  360. cdf_check_summary_info(struct magic_set *ms, const cdf_info_t *info,
  361. const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
  362. const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn,
  363. const cdf_directory_t *root_storage, const char **expn)
  364. {
  365. int i;
  366. const char *str = NULL;
  367. cdf_directory_t *d;
  368. char name[__arraycount(d->d_name)];
  369. size_t j, k;
  370. #ifdef CDF_DEBUG
  371. cdf_dump_summary_info(h, scn);
  372. #endif
  373. if ((i = cdf_file_summary_info(ms, h, scn, root_storage)) < 0) {
  374. *expn = "Can't expand summary_info";
  375. return i;
  376. }
  377. if (i == 1)
  378. return i;
  379. for (j = 0; str == NULL && j < dir->dir_len; j++) {
  380. d = &dir->dir_tab[j];
  381. for (k = 0; k < sizeof(name); k++)
  382. name[k] = CAST(char, cdf_tole2(d->d_name[k]));
  383. str = cdf_app_to_mime(name,
  384. NOTMIME(ms) ? name2desc : name2mime);
  385. }
  386. if (NOTMIME(ms)) {
  387. if (str != NULL) {
  388. if (file_printf(ms, "%s", str) == -1)
  389. return -1;
  390. i = 1;
  391. }
  392. } else if (ms->flags & MAGIC_MIME_TYPE) {
  393. if (str == NULL)
  394. str = "vnd.ms-office";
  395. if (file_printf(ms, "application/%s", str) == -1)
  396. return -1;
  397. i = 1;
  398. }
  399. if (i <= 0) {
  400. i = cdf_file_catalog_info(ms, info, h, sat, ssat, sst,
  401. dir, scn);
  402. }
  403. return i;
  404. }
  405. private struct sinfo {
  406. const char *name;
  407. const char *mime;
  408. const char *sections[5];
  409. const int types[5];
  410. } sectioninfo[] = {
  411. { "Encrypted", "encrypted",
  412. {
  413. "EncryptedPackage", "EncryptedSummary",
  414. NULL, NULL, NULL,
  415. },
  416. {
  417. CDF_DIR_TYPE_USER_STREAM,
  418. CDF_DIR_TYPE_USER_STREAM,
  419. 0, 0, 0,
  420. },
  421. },
  422. { "QuickBooks", "quickbooks",
  423. {
  424. #if 0
  425. "TaxForms", "PDFTaxForms", "modulesInBackup",
  426. #endif
  427. "mfbu_header", NULL, NULL, NULL, NULL,
  428. },
  429. {
  430. #if 0
  431. CDF_DIR_TYPE_USER_STORAGE,
  432. CDF_DIR_TYPE_USER_STORAGE,
  433. CDF_DIR_TYPE_USER_STREAM,
  434. #endif
  435. CDF_DIR_TYPE_USER_STREAM,
  436. 0, 0, 0, 0
  437. },
  438. },
  439. { "Microsoft Excel", "vnd.ms-excel",
  440. {
  441. "Book", "Workbook", NULL, NULL, NULL,
  442. },
  443. {
  444. CDF_DIR_TYPE_USER_STREAM,
  445. CDF_DIR_TYPE_USER_STREAM,
  446. 0, 0, 0,
  447. },
  448. },
  449. { "Microsoft Word", "msword",
  450. {
  451. "WordDocument", NULL, NULL, NULL, NULL,
  452. },
  453. {
  454. CDF_DIR_TYPE_USER_STREAM,
  455. 0, 0, 0, 0,
  456. },
  457. },
  458. { "Microsoft PowerPoint", "vnd.ms-powerpoint",
  459. {
  460. "PowerPoint", NULL, NULL, NULL, NULL,
  461. },
  462. {
  463. CDF_DIR_TYPE_USER_STREAM,
  464. 0, 0, 0, 0,
  465. },
  466. },
  467. { "Microsoft Outlook Message", "vnd.ms-outlook",
  468. {
  469. "__properties_version1.0",
  470. "__recip_version1.0_#00000000",
  471. NULL, NULL, NULL,
  472. },
  473. {
  474. CDF_DIR_TYPE_USER_STREAM,
  475. CDF_DIR_TYPE_USER_STORAGE,
  476. 0, 0, 0,
  477. },
  478. },
  479. };
  480. private int
  481. cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir)
  482. {
  483. size_t sd, j;
  484. for (sd = 0; sd < __arraycount(sectioninfo); sd++) {
  485. const struct sinfo *si = &sectioninfo[sd];
  486. for (j = 0; si->sections[j]; j++) {
  487. if (cdf_find_stream(dir, si->sections[j], si->types[j])
  488. > 0)
  489. break;
  490. #ifdef CDF_DEBUG
  491. fprintf(stderr, "Can't read %s\n", si->sections[j]);
  492. #endif
  493. }
  494. if (si->sections[j] == NULL)
  495. continue;
  496. if (NOTMIME(ms)) {
  497. if (file_printf(ms, "CDFV2 %s", si->name) == -1)
  498. return -1;
  499. } else if (ms->flags & MAGIC_MIME_TYPE) {
  500. if (file_printf(ms, "application/%s", si->mime) == -1)
  501. return -1;
  502. }
  503. return 1;
  504. }
  505. return -1;
  506. }
  507. protected int
  508. file_trycdf(struct magic_set *ms, const struct buffer *b)
  509. {
  510. int fd = b->fd;
  511. const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
  512. size_t nbytes = b->flen;
  513. cdf_info_t info;
  514. cdf_header_t h;
  515. cdf_sat_t sat, ssat;
  516. cdf_stream_t sst, scn;
  517. cdf_dir_t dir;
  518. int i;
  519. const char *expn = "";
  520. const cdf_directory_t *root_storage;
  521. scn.sst_tab = NULL;
  522. info.i_fd = fd;
  523. info.i_buf = buf;
  524. info.i_len = nbytes;
  525. if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
  526. return 0;
  527. if (cdf_read_header(&info, &h) == -1)
  528. return 0;
  529. #ifdef CDF_DEBUG
  530. cdf_dump_header(&h);
  531. #endif
  532. if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
  533. expn = "Can't read SAT";
  534. goto out0;
  535. }
  536. #ifdef CDF_DEBUG
  537. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  538. #endif
  539. if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
  540. expn = "Can't read SSAT";
  541. goto out1;
  542. }
  543. #ifdef CDF_DEBUG
  544. cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
  545. #endif
  546. if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
  547. expn = "Can't read directory";
  548. goto out2;
  549. }
  550. if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
  551. &root_storage)) == -1) {
  552. expn = "Cannot read short stream";
  553. goto out3;
  554. }
  555. #ifdef CDF_DEBUG
  556. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  557. #endif
  558. #ifdef notdef
  559. if (root_storage) {
  560. if (NOTMIME(ms)) {
  561. char clsbuf[128];
  562. if (file_printf(ms, "CLSID %s, ",
  563. format_clsid(clsbuf, sizeof(clsbuf),
  564. root_storage->d_storage_uuid)) == -1)
  565. return -1;
  566. }
  567. }
  568. #endif
  569. if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
  570. "FileHeader", &scn)) != -1) {
  571. #define HWP5_SIGNATURE "HWP Document File"
  572. if (scn.sst_len * scn.sst_ss >= sizeof(HWP5_SIGNATURE) - 1
  573. && memcmp(scn.sst_tab, HWP5_SIGNATURE,
  574. sizeof(HWP5_SIGNATURE) - 1) == 0) {
  575. if (NOTMIME(ms)) {
  576. if (file_printf(ms,
  577. "Hangul (Korean) Word Processor File 5.x") == -1)
  578. return -1;
  579. } else if (ms->flags & MAGIC_MIME_TYPE) {
  580. if (file_printf(ms, "application/x-hwp") == -1)
  581. return -1;
  582. }
  583. i = 1;
  584. goto out5;
  585. } else {
  586. cdf_zero_stream(&scn);
  587. }
  588. }
  589. if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  590. &scn)) == -1) {
  591. if (errno != ESRCH) {
  592. expn = "Cannot read summary info";
  593. }
  594. } else {
  595. i = cdf_check_summary_info(ms, &info, &h,
  596. &sat, &ssat, &sst, &dir, &scn, root_storage, &expn);
  597. cdf_zero_stream(&scn);
  598. }
  599. if (i <= 0) {
  600. if ((i = cdf_read_doc_summary_info(&info, &h, &sat, &ssat,
  601. &sst, &dir, &scn)) == -1) {
  602. if (errno != ESRCH) {
  603. expn = "Cannot read summary info";
  604. }
  605. } else {
  606. i = cdf_check_summary_info(ms, &info, &h, &sat, &ssat,
  607. &sst, &dir, &scn, root_storage, &expn);
  608. }
  609. }
  610. if (i <= 0) {
  611. i = cdf_file_dir_info(ms, &dir);
  612. if (i < 0)
  613. expn = "Cannot read section info";
  614. }
  615. out5:
  616. cdf_zero_stream(&scn);
  617. cdf_zero_stream(&sst);
  618. out3:
  619. free(dir.dir_tab);
  620. out2:
  621. free(ssat.sat_tab);
  622. out1:
  623. free(sat.sat_tab);
  624. out0:
  625. /* If we handled it already, return */
  626. if (i != -1)
  627. return i;
  628. /* Provide a default handler */
  629. if (NOTMIME(ms)) {
  630. if (file_printf(ms,
  631. "Composite Document File V2 Document") == -1)
  632. return -1;
  633. if (*expn)
  634. if (file_printf(ms, ", %s", expn) == -1)
  635. return -1;
  636. } else if (ms->flags & MAGIC_MIME_TYPE) {
  637. if (file_printf(ms, "application/CDFV2") == -1)
  638. return -1;
  639. }
  640. return 1;
  641. }