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