readcdf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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.44 2014/05/14 23:22:48 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. #if defined(HAVE_LOCALE_H)
  37. #include <locale.h>
  38. #endif
  39. #include "cdf.h"
  40. #include "magic.h"
  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. { 0x00000000000c1084LLU, 0x46000000000000c0LLU },
  73. "x-msi",
  74. },
  75. { { 0, 0 },
  76. NULL,
  77. },
  78. }, clsid2desc[] = {
  79. {
  80. { 0x00000000000c1084LLU, 0x46000000000000c0LLU },
  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. char *old_lc_ctype;
  103. old_lc_ctype = setlocale(LC_CTYPE, NULL);
  104. assert(old_lc_ctype != NULL);
  105. old_lc_ctype = strdup(old_lc_ctype);
  106. assert(old_lc_ctype != NULL);
  107. (void)setlocale(LC_CTYPE, "C");
  108. for (i = 0; nv[i].pattern != NULL; i++)
  109. if (strcasestr(vbuf, nv[i].pattern) != NULL) {
  110. rv = nv[i].mime;
  111. break;
  112. }
  113. (void)setlocale(LC_CTYPE, old_lc_ctype);
  114. free(old_lc_ctype);
  115. return rv;
  116. }
  117. private int
  118. cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
  119. size_t count, const cdf_directory_t *root_storage)
  120. {
  121. size_t i;
  122. cdf_timestamp_t tp;
  123. struct timespec ts;
  124. char buf[64];
  125. const char *str = NULL;
  126. const char *s;
  127. int len;
  128. if (!NOTMIME(ms) && root_storage)
  129. str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
  130. clsid2mime);
  131. for (i = 0; i < count; i++) {
  132. cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
  133. switch (info[i].pi_type) {
  134. case CDF_NULL:
  135. break;
  136. case CDF_SIGNED16:
  137. if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
  138. info[i].pi_s16) == -1)
  139. return -1;
  140. break;
  141. case CDF_SIGNED32:
  142. if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
  143. info[i].pi_s32) == -1)
  144. return -1;
  145. break;
  146. case CDF_UNSIGNED32:
  147. if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
  148. info[i].pi_u32) == -1)
  149. return -1;
  150. break;
  151. case CDF_FLOAT:
  152. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  153. info[i].pi_f) == -1)
  154. return -1;
  155. break;
  156. case CDF_DOUBLE:
  157. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  158. info[i].pi_d) == -1)
  159. return -1;
  160. break;
  161. case CDF_LENGTH32_STRING:
  162. case CDF_LENGTH32_WSTRING:
  163. len = info[i].pi_str.s_len;
  164. if (len > 1) {
  165. char vbuf[1024];
  166. size_t j, k = 1;
  167. if (info[i].pi_type == CDF_LENGTH32_WSTRING)
  168. k++;
  169. s = info[i].pi_str.s_buf;
  170. for (j = 0; j < sizeof(vbuf) && len--; s += k) {
  171. if (*s == '\0')
  172. break;
  173. if (isprint((unsigned char)*s))
  174. vbuf[j++] = *s;
  175. }
  176. if (j == sizeof(vbuf))
  177. --j;
  178. vbuf[j] = '\0';
  179. if (NOTMIME(ms)) {
  180. if (vbuf[0]) {
  181. if (file_printf(ms, ", %s: %s",
  182. buf, vbuf) == -1)
  183. return -1;
  184. }
  185. } else if (str == NULL && info[i].pi_id ==
  186. CDF_PROPERTY_NAME_OF_APPLICATION) {
  187. str = cdf_app_to_mime(vbuf, app2mime);
  188. }
  189. }
  190. break;
  191. case CDF_FILETIME:
  192. tp = info[i].pi_tp;
  193. if (tp != 0) {
  194. char tbuf[64];
  195. if (tp < 1000000000000000LL) {
  196. cdf_print_elapsed_time(tbuf,
  197. sizeof(tbuf), tp);
  198. if (NOTMIME(ms) && file_printf(ms,
  199. ", %s: %s", buf, tbuf) == -1)
  200. return -1;
  201. } else {
  202. char *c, *ec;
  203. cdf_timestamp_to_timespec(&ts, tp);
  204. c = cdf_ctime(&ts.tv_sec, tbuf);
  205. if (c != NULL &&
  206. (ec = strchr(c, '\n')) != NULL)
  207. *ec = '\0';
  208. if (NOTMIME(ms) && file_printf(ms,
  209. ", %s: %s", buf, c) == -1)
  210. return -1;
  211. }
  212. }
  213. break;
  214. case CDF_CLIPBOARD:
  215. break;
  216. default:
  217. return -1;
  218. }
  219. }
  220. if (!NOTMIME(ms)) {
  221. if (str == NULL)
  222. return 0;
  223. if (file_printf(ms, "application/%s", str) == -1)
  224. return -1;
  225. }
  226. return 1;
  227. }
  228. private int
  229. cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
  230. const cdf_stream_t *sst, const cdf_directory_t *root_storage)
  231. {
  232. cdf_summary_info_header_t si;
  233. cdf_property_info_t *info;
  234. size_t count;
  235. int m;
  236. if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
  237. return -1;
  238. if (NOTMIME(ms)) {
  239. const char *str;
  240. if (file_printf(ms, "Composite Document File V2 Document")
  241. == -1)
  242. return -1;
  243. if (file_printf(ms, ", %s Endian",
  244. si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
  245. return -2;
  246. switch (si.si_os) {
  247. case 2:
  248. if (file_printf(ms, ", Os: Windows, Version %d.%d",
  249. si.si_os_version & 0xff,
  250. (uint32_t)si.si_os_version >> 8) == -1)
  251. return -2;
  252. break;
  253. case 1:
  254. if (file_printf(ms, ", Os: MacOS, Version %d.%d",
  255. (uint32_t)si.si_os_version >> 8,
  256. si.si_os_version & 0xff) == -1)
  257. return -2;
  258. break;
  259. default:
  260. if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
  261. si.si_os_version & 0xff,
  262. (uint32_t)si.si_os_version >> 8) == -1)
  263. return -2;
  264. break;
  265. }
  266. if (root_storage) {
  267. str = cdf_clsid_to_mime(root_storage->d_storage_uuid,
  268. clsid2desc);
  269. if (str)
  270. if (file_printf(ms, ", %s", str) == -1)
  271. return -2;
  272. }
  273. }
  274. m = cdf_file_property_info(ms, info, count, root_storage);
  275. free(info);
  276. return m == -1 ? -2 : m;
  277. }
  278. #ifdef notdef
  279. private char *
  280. format_clsid(char *buf, size_t len, const uint64_t uuid[2]) {
  281. snprintf(buf, len, "%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4"
  282. PRIx64 "-%.12" PRIx64,
  283. (uuid[0] >> 32) & (uint64_t)0x000000000ffffffffLLU,
  284. (uuid[0] >> 16) & (uint64_t)0x0000000000000ffffLLU,
  285. (uuid[0] >> 0) & (uint64_t)0x0000000000000ffffLLU,
  286. (uuid[1] >> 48) & (uint64_t)0x0000000000000ffffLLU,
  287. (uuid[1] >> 0) & (uint64_t)0x0000fffffffffffffLLU);
  288. return buf;
  289. }
  290. #endif
  291. protected int
  292. file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
  293. size_t nbytes)
  294. {
  295. cdf_info_t info;
  296. cdf_header_t h;
  297. cdf_sat_t sat, ssat;
  298. cdf_stream_t sst, scn;
  299. cdf_dir_t dir;
  300. int i;
  301. const char *expn = "";
  302. const char *corrupt = "corrupt: ";
  303. info.i_fd = fd;
  304. info.i_buf = buf;
  305. info.i_len = nbytes;
  306. if (ms->flags & MAGIC_APPLE)
  307. return 0;
  308. if (cdf_read_header(&info, &h) == -1)
  309. return 0;
  310. #ifdef CDF_DEBUG
  311. cdf_dump_header(&h);
  312. #endif
  313. if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
  314. expn = "Can't read SAT";
  315. goto out0;
  316. }
  317. #ifdef CDF_DEBUG
  318. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  319. #endif
  320. if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
  321. expn = "Can't read SSAT";
  322. goto out1;
  323. }
  324. #ifdef CDF_DEBUG
  325. cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
  326. #endif
  327. if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
  328. expn = "Can't read directory";
  329. goto out2;
  330. }
  331. const cdf_directory_t *root_storage;
  332. if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst,
  333. &root_storage)) == -1) {
  334. expn = "Cannot read short stream";
  335. goto out3;
  336. }
  337. #ifdef CDF_DEBUG
  338. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  339. #endif
  340. #ifdef notdef
  341. if (root_storage) {
  342. if (NOTMIME(ms)) {
  343. char clsbuf[128];
  344. if (file_printf(ms, "CLSID %s, ",
  345. format_clsid(clsbuf, sizeof(clsbuf),
  346. root_storage->d_storage_uuid)) == -1)
  347. return -1;
  348. }
  349. }
  350. #endif
  351. if ((i = cdf_read_user_stream(&info, &h, &sat, &ssat, &sst, &dir,
  352. "FileHeader", &scn)) != -1) {
  353. #define HWP5_SIGNATURE "HWP Document File"
  354. if (scn.sst_dirlen >= sizeof(HWP5_SIGNATURE) - 1
  355. && memcmp(scn.sst_tab, HWP5_SIGNATURE,
  356. sizeof(HWP5_SIGNATURE) - 1) == 0) {
  357. if (NOTMIME(ms)) {
  358. if (file_printf(ms,
  359. "Hangul (Korean) Word Processor File 5.x") == -1)
  360. return -1;
  361. } else {
  362. if (file_printf(ms, "application/x-hwp") == -1)
  363. return -1;
  364. }
  365. i = 1;
  366. goto out5;
  367. } else {
  368. free(scn.sst_tab);
  369. scn.sst_tab = NULL;
  370. scn.sst_len = 0;
  371. scn.sst_dirlen = 0;
  372. }
  373. }
  374. if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  375. &scn)) == -1) {
  376. if (errno == ESRCH) {
  377. corrupt = expn;
  378. expn = "No summary info";
  379. } else {
  380. expn = "Cannot read summary info";
  381. }
  382. goto out4;
  383. }
  384. #ifdef CDF_DEBUG
  385. cdf_dump_summary_info(&h, &scn);
  386. #endif
  387. if ((i = cdf_file_summary_info(ms, &h, &scn, root_storage)) < 0)
  388. expn = "Can't expand summary_info";
  389. if (i == 0) {
  390. const char *str = NULL;
  391. cdf_directory_t *d;
  392. char name[__arraycount(d->d_name)];
  393. size_t j, k;
  394. for (j = 0; str == NULL && j < dir.dir_len; j++) {
  395. d = &dir.dir_tab[j];
  396. for (k = 0; k < sizeof(name); k++)
  397. name[k] = (char)cdf_tole2(d->d_name[k]);
  398. str = cdf_app_to_mime(name,
  399. NOTMIME(ms) ? name2desc : name2mime);
  400. }
  401. if (NOTMIME(ms)) {
  402. if (str != NULL) {
  403. if (file_printf(ms, "%s", str) == -1)
  404. return -1;
  405. i = 1;
  406. }
  407. } else {
  408. if (str == NULL)
  409. str = "vnd.ms-office";
  410. if (file_printf(ms, "application/%s", str) == -1)
  411. return -1;
  412. i = 1;
  413. }
  414. }
  415. out5:
  416. free(scn.sst_tab);
  417. out4:
  418. free(sst.sst_tab);
  419. out3:
  420. free(dir.dir_tab);
  421. out2:
  422. free(ssat.sat_tab);
  423. out1:
  424. free(sat.sat_tab);
  425. out0:
  426. if (i == -1) {
  427. if (NOTMIME(ms)) {
  428. if (file_printf(ms,
  429. "Composite Document File V2 Document") == -1)
  430. return -1;
  431. if (*expn)
  432. if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
  433. return -1;
  434. } else {
  435. if (file_printf(ms, "application/CDFV2-corrupt") == -1)
  436. return -1;
  437. }
  438. i = 1;
  439. }
  440. return i;
  441. }