readcdf.c 16 KB

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