readcdf.c 14 KB

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