readcdf.c 16 KB

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