readcdf.c 16 KB

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