readcdf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*-
  2. * Copyright (c) 2008 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.49 2014/12/04 15:56:46 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. { "WordDocument", "msword", },
  58. { "PowerPoint", "vnd.ms-powerpoint", },
  59. { "DigitalSignature", "vnd.ms-msi", },
  60. { NULL, NULL, },
  61. }, name2desc[] = {
  62. { "WordDocument", "Microsoft Office Word",},
  63. { "PowerPoint", "Microsoft PowerPoint", },
  64. { "DigitalSignature", "Microsoft Installer", },
  65. { NULL, NULL, },
  66. };
  67. static const struct cv {
  68. uint64_t clsid[2];
  69. const char *mime;
  70. } clsid2mime[] = {
  71. {
  72. { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
  73. "x-msi",
  74. },
  75. { { 0, 0 },
  76. NULL,
  77. },
  78. }, clsid2desc[] = {
  79. {
  80. { 0x00000000000c1084ULL, 0x46000000000000c0ULL },
  81. "MSI Installer",
  82. },
  83. { { 0, 0 },
  84. NULL,
  85. },
  86. };
  87. private const char *
  88. cdf_clsid_to_mime(const uint64_t clsid[2], const struct cv *cv)
  89. {
  90. size_t i;
  91. for (i = 0; cv[i].mime != NULL; i++) {
  92. if (clsid[0] == cv[i].clsid[0] && clsid[1] == cv[i].clsid[1])
  93. return cv[i].mime;
  94. }
  95. return NULL;
  96. }
  97. private const char *
  98. cdf_app_to_mime(const char *vbuf, const struct nv *nv)
  99. {
  100. size_t i;
  101. const char *rv = NULL;
  102. #ifdef USE_C_LOCALE
  103. locale_t old_lc_ctype, c_lc_ctype;
  104. c_lc_ctype = newlocale(LC_CTYPE_MASK, "C", 0);
  105. assert(c_lc_ctype != NULL);
  106. old_lc_ctype = uselocale(c_lc_ctype);
  107. assert(old_lc_ctype != NULL);
  108. #endif
  109. for (i = 0; nv[i].pattern != NULL; i++)
  110. if (strcasestr(vbuf, nv[i].pattern) != NULL) {
  111. rv = nv[i].mime;
  112. break;
  113. }
  114. #ifdef USE_C_LOCALE
  115. (void)uselocale(old_lc_ctype);
  116. freelocale(c_lc_ctype);
  117. #endif
  118. return rv;
  119. }
  120. private int
  121. cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
  122. size_t count, const cdf_directory_t *root_storage)
  123. {
  124. size_t i;
  125. cdf_timestamp_t tp;
  126. struct timespec ts;
  127. char buf[64];
  128. const char *str = NULL;
  129. const char *s;
  130. int len;
  131. if (!NOTMIME(ms) && root_storage)
  132. str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
  133. clsid2mime);
  134. for (i = 0; i < count; i++) {
  135. cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
  136. switch (info[i].pi_type) {
  137. case CDF_NULL:
  138. break;
  139. case CDF_SIGNED16:
  140. if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
  141. info[i].pi_s16) == -1)
  142. return -1;
  143. break;
  144. case CDF_SIGNED32:
  145. if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
  146. info[i].pi_s32) == -1)
  147. return -1;
  148. break;
  149. case CDF_UNSIGNED32:
  150. if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
  151. info[i].pi_u32) == -1)
  152. return -1;
  153. break;
  154. case CDF_FLOAT:
  155. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  156. info[i].pi_f) == -1)
  157. return -1;
  158. break;
  159. case CDF_DOUBLE:
  160. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  161. info[i].pi_d) == -1)
  162. return -1;
  163. break;
  164. case CDF_LENGTH32_STRING:
  165. case CDF_LENGTH32_WSTRING:
  166. len = info[i].pi_str.s_len;
  167. if (len > 1) {
  168. char vbuf[1024];
  169. size_t j, k = 1;
  170. if (info[i].pi_type == CDF_LENGTH32_WSTRING)
  171. k++;
  172. s = info[i].pi_str.s_buf;
  173. for (j = 0; j < sizeof(vbuf) && len--; s += k) {
  174. if (*s == '\0')
  175. break;
  176. if (isprint((unsigned char)*s))
  177. vbuf[j++] = *s;
  178. }
  179. if (j == sizeof(vbuf))
  180. --j;
  181. vbuf[j] = '\0';
  182. if (NOTMIME(ms)) {
  183. if (vbuf[0]) {
  184. if (file_printf(ms, ", %s: %s",
  185. buf, vbuf) == -1)
  186. return -1;
  187. }
  188. } else if (str == NULL && info[i].pi_id ==
  189. CDF_PROPERTY_NAME_OF_APPLICATION) {
  190. str = cdf_app_to_mime(vbuf, app2mime);
  191. }
  192. }
  193. break;
  194. case CDF_FILETIME:
  195. tp = info[i].pi_tp;
  196. if (tp != 0) {
  197. char tbuf[64];
  198. if (tp < 1000000000000000LL) {
  199. cdf_print_elapsed_time(tbuf,
  200. sizeof(tbuf), tp);
  201. if (NOTMIME(ms) && file_printf(ms,
  202. ", %s: %s", buf, tbuf) == -1)
  203. return -1;
  204. } else {
  205. char *c, *ec;
  206. cdf_timestamp_to_timespec(&ts, tp);
  207. c = cdf_ctime(&ts.tv_sec, tbuf);
  208. if (c != NULL &&
  209. (ec = strchr(c, '\n')) != NULL)
  210. *ec = '\0';
  211. if (NOTMIME(ms) && file_printf(ms,
  212. ", %s: %s", buf, c) == -1)
  213. return -1;
  214. }
  215. }
  216. break;
  217. case CDF_CLIPBOARD:
  218. break;
  219. default:
  220. return -1;
  221. }
  222. }
  223. if (!NOTMIME(ms)) {
  224. if (str == NULL)
  225. return 0;
  226. if (file_printf(ms, "application/%s", str) == -1)
  227. return -1;
  228. }
  229. return 1;
  230. }
  231. private int
  232. cdf_file_catalog(struct magic_set *ms, const cdf_header_t *h,
  233. const cdf_stream_t *sst)
  234. {
  235. cdf_catalog_t *cat;
  236. size_t i;
  237. char buf[256];
  238. cdf_catalog_entry_t *ce;
  239. if (NOTMIME(ms)) {
  240. if (file_printf(ms, "Microsoft Thumbs.db [") == -1)
  241. return -1;
  242. if (cdf_unpack_catalog(h, sst, &cat) == -1)
  243. return -1;
  244. ce = cat->cat_e;
  245. /* skip first entry since it has a , or paren */
  246. for (i = 1; i < cat->cat_num; i++)
  247. if (file_printf(ms, "%s%s",
  248. cdf_u16tos8(buf, ce[i].ce_namlen, ce[i].ce_name),
  249. i == cat->cat_num - 1 ? "]" : ", ") == -1) {
  250. free(cat);
  251. return -1;
  252. }
  253. free(cat);
  254. } else {
  255. if (file_printf(ms, "application/CDFV2") == -1)
  256. return -1;
  257. }
  258. return 1;
  259. }
  260. private int
  261. cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
  262. const cdf_stream_t *sst, const cdf_directory_t *root_storage)
  263. {
  264. cdf_summary_info_header_t si;
  265. cdf_property_info_t *info;
  266. size_t count;
  267. int m;
  268. if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
  269. return -1;
  270. if (NOTMIME(ms)) {
  271. const char *str;
  272. if (file_printf(ms, "Composite Document File V2 Document")
  273. == -1)
  274. return -1;
  275. if (file_printf(ms, ", %s Endian",
  276. si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
  277. return -2;
  278. switch (si.si_os) {
  279. case 2:
  280. if (file_printf(ms, ", Os: Windows, Version %d.%d",
  281. si.si_os_version & 0xff,
  282. (uint32_t)si.si_os_version >> 8) == -1)
  283. return -2;
  284. break;
  285. case 1:
  286. if (file_printf(ms, ", Os: MacOS, Version %d.%d",
  287. (uint32_t)si.si_os_version >> 8,
  288. si.si_os_version & 0xff) == -1)
  289. return -2;
  290. break;
  291. default:
  292. if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
  293. si.si_os_version & 0xff,
  294. (uint32_t)si.si_os_version >> 8) == -1)
  295. return -2;
  296. break;
  297. }
  298. if (root_storage) {
  299. str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
  300. clsid2desc);
  301. if (str) {
  302. if (file_printf(ms, ", %s", str) == -1)
  303. return -2;
  304. }
  305. }
  306. }
  307. m = cdf_file_property_info(ms, info, count, root_storage);
  308. free(info);
  309. return m == -1 ? -2 : m;
  310. }
  311. #ifdef notdef
  312. private char *
  313. format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
  314. snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
  315. PRIx64 "-%.12" PRIx64,
  316. (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffULL,
  317. (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffULL,
  318. (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffULL,
  319. (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffULL,
  320. (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffULL);
  321. return buf;
  322. }
  323. #endif
  324. protected int
  325. file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
  326. size_t nbytes)
  327. {
  328. cdf_info_t info;
  329. cdf_header_t h;
  330. cdf_sat_t sat, ssat;
  331. cdf_stream_t sst, scn;
  332. cdf_dir_t dir;
  333. int i;
  334. const char *expn = "";
  335. const char *corrupt = "corrupt: ";
  336. const cdf_directory_t *root_storage;
  337. info.i_fd = fd;
  338. info.i_buf = buf;
  339. info.i_len = nbytes;
  340. if (ms->flags & MAGIC_APPLE)
  341. return 0;
  342. if (cdf_read_header(&info, &h) == -1)
  343. return 0;
  344. #ifdef CDF_DEBUG
  345. cdf_dump_header(&h);
  346. #endif
  347. if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
  348. expn = "Can't read SAT";
  349. goto out0;
  350. }
  351. #ifdef CDF_DEBUG
  352. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  353. #endif
  354. if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
  355. expn = "Can't read SSAT";
  356. goto out1;
  357. }
  358. #ifdef CDF_DEBUG
  359. cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
  360. #endif
  361. if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
  362. expn = "Can't read directory";
  363. goto out2;
  364. }
  365. if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
  366. &root_storage)) == -1) {
  367. expn = "Cannot read short stream";
  368. goto out3;
  369. }
  370. #ifdef CDF_DEBUG
  371. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  372. #endif
  373. #ifdef notdef
  374. if (root_storage) {
  375. if (NOTMIME(ms)) {
  376. char clsbuf[128];
  377. if (file_printf(ms, "CLSID %s, ",
  378. format_clsid(clsbuf, sizeof(clsbuf),
  379. root_storage->d_storage_uuid)) == -1)
  380. return -1;
  381. }
  382. }
  383. #endif
  384. if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
  385. "FileHeader", &scn)) != -1) {
  386. #define HWP5_SIGNATURE "HWP Document File"
  387. if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1
  388. && memcmp(scn.sst_tab, HWP5_SIGNATURE,
  389. sizeof(HWP5_SIGNATURE) - 1) == 0) {
  390. if (NOTMIME(ms)) {
  391. if (file_printf(ms,
  392. "Hangul (Korean) Word Processor File 5.x") == -1)
  393. return -1;
  394. } else {
  395. if (file_printf(ms, "application/x-hwp") == -1)
  396. return -1;
  397. }
  398. i = 1;
  399. goto out5;
  400. } else {
  401. free(scn.sst_tab);
  402. scn.sst_tab = NULL;
  403. scn.sst_len = 0;
  404. scn.sst_dirlen = 0;
  405. }
  406. }
  407. if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  408. &scn)) == -1) {
  409. if (errno == ESRCH) {
  410. if ((i = cdf_read_catalog(&info, &h, &sat, &ssat, &sst,
  411. &dir, &scn)) == -1) {
  412. corrupt = expn;
  413. if ((i = cdf_read_encrypted_package(&info, &h,
  414. &sat, &ssat, &sst, &dir, &scn)) == -1)
  415. expn = "No summary info";
  416. else {
  417. expn = "Encrypted";
  418. i = -1;
  419. }
  420. goto out4;
  421. }
  422. #ifdef CDF_DEBUG
  423. cdf_dump_catalog(&h, &scn);
  424. #endif
  425. if ((i = cdf_file_catalog(ms, &h, &scn))
  426. < 0)
  427. expn = "Can't expand catalog";
  428. } else {
  429. expn = "Cannot read summary info";
  430. }
  431. goto out4;
  432. }
  433. #ifdef CDF_DEBUG
  434. cdf_dump_summary_info(&h, &scn);
  435. #endif
  436. if ((i = cdf_file_summary_info(ms, &h, &scn, root_storage)) < 0)
  437. expn = "Can't expand summary_info";
  438. if (i == 0) {
  439. const char *str = NULL;
  440. cdf_directory_t *d;
  441. char name[__arraycount(d->d_name)];
  442. size_t j, k;
  443. for (j = 0; str == NULL && j < dir.dir_len; j++) {
  444. d = &dir.dir_tab[j];
  445. for (k = 0; k < sizeof(name); k++)
  446. name[k] = (char)cdf_tole2(d->d_name[k]);
  447. str = cdf_app_to_mime(name,
  448. NOTMIME(ms) ? name2desc : name2mime);
  449. }
  450. if (NOTMIME(ms)) {
  451. if (str != NULL) {
  452. if (file_printf(ms, "%s", str) == -1)
  453. return -1;
  454. i = 1;
  455. }
  456. } else {
  457. if (str == NULL)
  458. str = "vnd.ms-office";
  459. if (file_printf(ms, "application/%s", str) == -1)
  460. return -1;
  461. i = 1;
  462. }
  463. }
  464. out5:
  465. free(scn.sst_tab);
  466. out4:
  467. free(sst.sst_tab);
  468. out3:
  469. free(dir.dir_tab);
  470. out2:
  471. free(ssat.sat_tab);
  472. out1:
  473. free(sat.sat_tab);
  474. out0:
  475. if (i == -1) {
  476. if (NOTMIME(ms)) {
  477. if (file_printf(ms,
  478. "Composite Document File V2 Document") == -1)
  479. return -1;
  480. if (*expn)
  481. if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
  482. return -1;
  483. } else {
  484. if (file_printf(ms, "application/CDFV2-%s",
  485. *corrupt ? "corrupt" : "encrypted") == -1)
  486. return -1;
  487. }
  488. i = 1;
  489. }
  490. return i;
  491. }