readcdf.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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.63 2016/10/18 22:25:42 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;
  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. for (j = 0; j < sizeof(vbuf) && len--; s += k) {
  189. if (*s == '\0')
  190. break;
  191. if (isprint((unsigned char)*s))
  192. vbuf[j++] = *s;
  193. }
  194. if (j == sizeof(vbuf))
  195. --j;
  196. vbuf[j] = '\0';
  197. if (NOTMIME(ms)) {
  198. if (vbuf[0]) {
  199. if (file_printf(ms, ", %s: %s",
  200. buf, vbuf) == -1)
  201. return -1;
  202. }
  203. } else if (str == NULL && info[i].pi_id ==
  204. CDF_PROPERTY_NAME_OF_APPLICATION) {
  205. str = cdf_app_to_mime(vbuf, app2mime);
  206. }
  207. }
  208. break;
  209. case CDF_FILETIME:
  210. tp = info[i].pi_tp;
  211. if (tp != 0) {
  212. char tbuf[64];
  213. if (tp < 1000000000000000LL) {
  214. cdf_print_elapsed_time(tbuf,
  215. sizeof(tbuf), tp);
  216. if (NOTMIME(ms) && file_printf(ms,
  217. ", %s: %s", buf, tbuf) == -1)
  218. return -1;
  219. } else {
  220. char *c, *ec;
  221. cdf_timestamp_to_timespec(&ts, tp);
  222. c = cdf_ctime(&ts.tv_sec, tbuf);
  223. if (c != NULL &&
  224. (ec = strchr(c, '\n')) != NULL)
  225. *ec = '\0';
  226. if (NOTMIME(ms) && file_printf(ms,
  227. ", %s: %s", buf, c) == -1)
  228. return -1;
  229. }
  230. }
  231. break;
  232. case CDF_CLIPBOARD:
  233. break;
  234. default:
  235. return -1;
  236. }
  237. }
  238. if (!NOTMIME(ms)) {
  239. if (str == NULL)
  240. return 0;
  241. if (file_printf(ms, "application/%s", str) == -1)
  242. return -1;
  243. }
  244. return 1;
  245. }
  246. private int
  247. cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
  248. const cdf_stream_t *sst)
  249. {
  250. cdf_catalog_t *cat;
  251. size_t i;
  252. char buf[256];
  253. cdf_catalog_entry_t *ce;
  254. if (NOTMIME(ms)) {
  255. if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
  256. return -1;
  257. if (cdf_unpack_catalog(h, sst, &cat) == -1)
  258. return -1;
  259. ce = cat->cat_e;
  260. /* skip first entry since it has a , or paren */
  261. for (i = 1; i < cat->cat_num; i++)
  262. if (file_printf(ms, "%s%s",
  263. cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
  264. i == cat->cat_num - 1 ? "]" : ", ") == -1) {
  265. free(cat);
  266. return -1;
  267. }
  268. free(cat);
  269. } else {
  270. if (file_printf(ms, "application/CDFV2") == -1)
  271. return -1;
  272. }
  273. return 1;
  274. }
  275. private int
  276. cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
  277. const cdf_stream_t *sst, const cdf_directory_t *root_storage)
  278. {
  279. cdf_summary_info_header_t si;
  280. cdf_property_info_t *info;
  281. size_t count;
  282. int m;
  283. if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
  284. return -1;
  285. if (NOTMIME(ms)) {
  286. const char *str;
  287. if (file_printf(ms, "Composite Document File V2 Document")
  288. == -1)
  289. return -1;
  290. if (file_printf(ms, ", %s Endian",
  291. si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
  292. return -2;
  293. switch (si.si_os) {
  294. case 2:
  295. if (file_printf(ms, ", Os: Windows, Version %d.%d",
  296. si.si_os_version & 0xff,
  297. (uint32_t)si.si_os_version >> 8) == -1)
  298. return -2;
  299. break;
  300. case 1:
  301. if (file_printf(ms, ", Os: MacOS, Version %d.%d",
  302. (uint32_t)si.si_os_version >> 8,
  303. si.si_os_version & 0xff) == -1)
  304. return -2;
  305. break;
  306. default:
  307. if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
  308. si.si_os_version & 0xff,
  309. (uint32_t)si.si_os_version >> 8) == -1)
  310. return -2;
  311. break;
  312. }
  313. if (root_storage) {
  314. str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
  315. clsid2desc);
  316. if (str) {
  317. if (file_printf(ms, ", %s", str) == -1)
  318. return -2;
  319. }
  320. }
  321. }
  322. m = cdf_file_property_info(ms, info, count, root_storage);
  323. free(info);
  324. return m == -1 ? -2 : m;
  325. }
  326. #ifdef notdef
  327. private char *
  328. format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
  329. snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
  330. PRIx64 "-%.12" PRIx64,
  331. (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
  332. (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
  333. (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
  334. (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
  335. (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL);
  336. return buf;
  337. }
  338. #endif
  339. private int
  340. cdf_file_catalog_info(struct magic_set *ms, const cdf_info_t *info,
  341. const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
  342. const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn)
  343. {
  344. int i;
  345. if ((i = cdf_read_user_stream(info, h, sat, ssat, sst,
  346. dir, "Catalog", scn)) == -1)
  347. return i;
  348. #ifdef CDF_DEBUG
  349. cdf_dump_catalog(h, scn);
  350. #endif
  351. if ((i = cdf_file_catalog(ms, h, scn)) == -1)
  352. return -1;
  353. return i;
  354. }
  355. private int
  356. cdf_check_summary_info(struct magic_set *ms, const cdf_info_t *info,
  357. const cdf_header_t *h, const cdf_sat_t *sat, const cdf_sat_t *ssat,
  358. const cdf_stream_t *sst, const cdf_dir_t *dir, cdf_stream_t *scn,
  359. const cdf_directory_t *root_storage, const char **expn)
  360. {
  361. int i;
  362. const char *str = NULL;
  363. cdf_directory_t *d;
  364. char name[__arraycount(d->d_name)];
  365. size_t j, k;
  366. #ifdef CDF_DEBUG
  367. cdf_dump_summary_info(h, scn);
  368. #endif
  369. if ((i = cdf_file_summary_info(ms, h, scn, root_storage)) < 0) {
  370. *expn = "Can't expand summary_info";
  371. return i;
  372. }
  373. if (i == 1)
  374. return i;
  375. for (j = 0; str == NULL && j < dir->dir_len; j++) {
  376. d = &dir->dir_tab[j];
  377. for (k = 0; k < sizeof(name); k++)
  378. name[k] = (char)cdf_tole2(d->d_name[k]);
  379. str = cdf_app_to_mime(name,
  380. NOTMIME(ms) ? name2desc : name2mime);
  381. }
  382. if (NOTMIME(ms)) {
  383. if (str != NULL) {
  384. if (file_printf(ms, "%s", str) == -1)
  385. return -1;
  386. i = 1;
  387. }
  388. } else {
  389. if (str == NULL)
  390. str = "vnd.ms-office";
  391. if (file_printf(ms, "application/%s", str) == -1)
  392. return -1;
  393. i = 1;
  394. }
  395. if (i <= 0) {
  396. i = cdf_file_catalog_info(ms, info, h, sat, ssat, sst,
  397. dir, scn);
  398. }
  399. return i;
  400. }
  401. private struct sinfo {
  402. const char *name;
  403. const char *mime;
  404. const char *sections[5];
  405. const int types[5];
  406. } sectioninfo[] = {
  407. { "Encrypted", "encrypted",
  408. {
  409. "EncryptedPackage", "EncryptedSummary",
  410. NULL, NULL, NULL,
  411. },
  412. {
  413. CDF_DIR_TYPE_USER_STREAM,
  414. CDF_DIR_TYPE_USER_STREAM,
  415. 0, 0, 0,
  416. },
  417. },
  418. { "QuickBooks", "quickbooks",
  419. {
  420. #if 0
  421. "TaxForms", "PDFTaxForms", "modulesInBackup",
  422. #endif
  423. "mfbu_header", NULL, NULL, NULL, NULL,
  424. },
  425. {
  426. #if 0
  427. CDF_DIR_TYPE_USER_STORAGE,
  428. CDF_DIR_TYPE_USER_STORAGE,
  429. CDF_DIR_TYPE_USER_STREAM,
  430. #endif
  431. CDF_DIR_TYPE_USER_STREAM,
  432. 0, 0, 0, 0
  433. },
  434. },
  435. { "Microsoft Excel", "vnd.ms-excel",
  436. {
  437. "Book", "Workbook", NULL, NULL, NULL,
  438. },
  439. {
  440. CDF_DIR_TYPE_USER_STREAM,
  441. CDF_DIR_TYPE_USER_STREAM,
  442. 0, 0, 0,
  443. },
  444. },
  445. { "Microsoft Word", "msword",
  446. {
  447. "WordDocument", NULL, NULL, NULL, NULL,
  448. },
  449. {
  450. CDF_DIR_TYPE_USER_STREAM,
  451. 0, 0, 0, 0,
  452. },
  453. },
  454. { "Microsoft PowerPoint", "vnd.ms-powerpoint",
  455. {
  456. "PowerPoint", NULL, NULL, NULL, NULL,
  457. },
  458. {
  459. CDF_DIR_TYPE_USER_STREAM,
  460. 0, 0, 0, 0,
  461. },
  462. },
  463. { "Microsoft Outlook Message", "vnd.ms-outlook",
  464. {
  465. "__properties_version1.0",
  466. "__recip_version1.0_#00000000",
  467. NULL, NULL, NULL,
  468. },
  469. {
  470. CDF_DIR_TYPE_USER_STREAM,
  471. CDF_DIR_TYPE_USER_STORAGE,
  472. 0, 0, 0,
  473. },
  474. },
  475. };
  476. private int
  477. cdf_file_dir_info(struct magic_set *ms, const cdf_dir_t *dir)
  478. {
  479. size_t sd, j;
  480. for (sd = 0; sd < __arraycount(sectioninfo); sd++) {
  481. const struct sinfo *si = &sectioninfo[sd];
  482. for (j = 0; si->sections[j]; j++) {
  483. if (cdf_find_stream(dir, si->sections[j], si->types[j])
  484. > 0)
  485. break;
  486. #ifdef CDF_DEBUG
  487. fprintf(stderr, "Can't read %s\n", si->sections[j]);
  488. #endif
  489. }
  490. if (si->sections[j] == NULL)
  491. continue;
  492. if (NOTMIME(ms)) {
  493. if (file_printf(ms, "CDFV2 %s", si->name) == -1)
  494. return -1;
  495. } else {
  496. if (file_printf(ms, "application/%s", si->mime) == -1)
  497. return -1;
  498. }
  499. return 1;
  500. }
  501. return -1;
  502. }
  503. protected int
  504. file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
  505. size_t nbytes)
  506. {
  507. cdf_info_t info;
  508. cdf_header_t h;
  509. cdf_sat_t sat, ssat;
  510. cdf_stream_t sst, scn;
  511. cdf_dir_t dir;
  512. int i;
  513. const char *expn = "";
  514. const cdf_directory_t *root_storage;
  515. scn.sst_tab = NULL;
  516. info.i_fd = fd;
  517. info.i_buf = buf;
  518. info.i_len = nbytes;
  519. if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
  520. return 0;
  521. if (cdf_read_header(&info, &h) == -1)
  522. return 0;
  523. #ifdef CDF_DEBUG
  524. cdf_dump_header(&h);
  525. #endif
  526. if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
  527. expn = "Can't read SAT";
  528. goto out0;
  529. }
  530. #ifdef CDF_DEBUG
  531. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  532. #endif
  533. if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
  534. expn = "Can't read SSAT";
  535. goto out1;
  536. }
  537. #ifdef CDF_DEBUG
  538. cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
  539. #endif
  540. if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
  541. expn = "Can't read directory";
  542. goto out2;
  543. }
  544. if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
  545. &root_storage)) == -1) {
  546. expn = "Cannot read short stream";
  547. goto out3;
  548. }
  549. #ifdef CDF_DEBUG
  550. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  551. #endif
  552. #ifdef notdef
  553. if (root_storage) {
  554. if (NOTMIME(ms)) {
  555. char clsbuf[128];
  556. if (file_printf(ms, "CLSID %s, ",
  557. format_clsid(clsbuf, sizeof(clsbuf),
  558. root_storage->d_storage_uuid)) == -1)
  559. return -1;
  560. }
  561. }
  562. #endif
  563. if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
  564. "FileHeader", &scn)) != -1) {
  565. #define HWP5_SIGNATURE "HWP Document File"
  566. if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1
  567. && memcmp(scn.sst_tab, HWP5_SIGNATURE,
  568. sizeof(HWP5_SIGNATURE) - 1) == 0) {
  569. if (NOTMIME(ms)) {
  570. if (file_printf(ms,
  571. "Hangul (Korean) Word Processor File 5.x") == -1)
  572. return -1;
  573. } else {
  574. if (file_printf(ms, "application/x-hwp") == -1)
  575. return -1;
  576. }
  577. i = 1;
  578. goto out5;
  579. } else {
  580. cdf_zero_stream(&scn);
  581. }
  582. }
  583. if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  584. &scn)) == -1) {
  585. if (errno != ESRCH) {
  586. expn = "Cannot read summary info";
  587. }
  588. } else {
  589. i = cdf_check_summary_info(ms, &info, &h,
  590. &sat, &ssat, &sst, &dir, &scn, root_storage, &expn);
  591. cdf_zero_stream(&scn);
  592. }
  593. if (i <= 0) {
  594. if ((i = cdf_read_doc_summary_info(&info, &h, &sat, &ssat,
  595. &sst, &dir, &scn)) == -1) {
  596. if (errno != ESRCH) {
  597. expn = "Cannot read summary info";
  598. }
  599. } else {
  600. i = cdf_check_summary_info(ms, &info, &h, &sat, &ssat,
  601. &sst, &dir, &scn, root_storage, &expn);
  602. }
  603. }
  604. if (i <= 0) {
  605. i = cdf_file_dir_info(ms, &dir);
  606. if (i < 0)
  607. expn = "Cannot read section info";
  608. }
  609. out5:
  610. cdf_zero_stream(&scn);
  611. cdf_zero_stream(&sst);
  612. out3:
  613. free(dir.dir_tab);
  614. out2:
  615. free(ssat.sat_tab);
  616. out1:
  617. free(sat.sat_tab);
  618. out0:
  619. if (i == -1) {
  620. if (NOTMIME(ms)) {
  621. if (file_printf(ms,
  622. "Composite Document File V2 Document") == -1)
  623. return -1;
  624. if (*expn)
  625. if (file_printf(ms, ", %s", expn) == -1)
  626. return -1;
  627. } else {
  628. if (file_printf(ms, "application/CDFV2") == -1)
  629. return -1;
  630. }
  631. i = 1;
  632. }
  633. return i;
  634. }