readcdf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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.37 2014/01/06 13:41:18 rrt Exp $")
  29. #endif
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #if defined(HAVE_LOCALE_H)
  36. #include <locale.h>
  37. #endif
  38. #include "cdf.h"
  39. #include "magic.h"
  40. #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0)
  41. static const struct nv {
  42. const char *pattern;
  43. const char *mime;
  44. } app2mime[] = {
  45. { "Word", "msword", },
  46. { "Excel", "vnd.ms-excel", },
  47. { "Powerpoint", "vnd.ms-powerpoint", },
  48. { "Crystal Reports", "x-rpt", },
  49. { "Advanced Installer", "vnd.ms-msi", },
  50. { "InstallShield", "vnd.ms-msi", },
  51. { "Microsoft Patch Compiler", "vnd.ms-msi", },
  52. { "NAnt", "vnd.ms-msi", },
  53. { "Windows Installer", "vnd.ms-msi", },
  54. { NULL, NULL, },
  55. }, name2mime[] = {
  56. { "WordDocument", "msword", },
  57. { "PowerPoint", "vnd.ms-powerpoint", },
  58. { "DigitalSignature", "vnd.ms-msi", },
  59. { NULL, NULL, },
  60. }, name2desc[] = {
  61. { "WordDocument", "Microsoft Office Word",},
  62. { "PowerPoint", "Microsoft PowerPoint", },
  63. { "DigitalSignature", "Microsoft Installer", },
  64. { NULL, NULL, },
  65. };
  66. private const char *
  67. cdf_app_to_mime(const char *vbuf, const struct nv *nv)
  68. {
  69. size_t i;
  70. const char *rv = NULL;
  71. (void)setlocale(LC_CTYPE, "C");
  72. for (i = 0; nv[i].pattern != NULL; i++)
  73. if (strcasestr(vbuf, nv[i].pattern) != NULL) {
  74. rv = nv[i].mime;
  75. break;
  76. }
  77. (void)setlocale(LC_CTYPE, "");
  78. return rv;
  79. }
  80. private int
  81. cdf_file_property_info(struct magic_set *ms, const cdf_property_info_t *info,
  82. size_t count)
  83. {
  84. size_t i;
  85. cdf_timestamp_t tp;
  86. struct timespec ts;
  87. char buf[64];
  88. const char *str = NULL;
  89. const char *s;
  90. int len;
  91. for (i = 0; i < count; i++) {
  92. cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
  93. switch (info[i].pi_type) {
  94. case CDF_NULL:
  95. break;
  96. case CDF_SIGNED16:
  97. if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
  98. info[i].pi_s16) == -1)
  99. return -1;
  100. break;
  101. case CDF_SIGNED32:
  102. if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
  103. info[i].pi_s32) == -1)
  104. return -1;
  105. break;
  106. case CDF_UNSIGNED32:
  107. if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
  108. info[i].pi_u32) == -1)
  109. return -1;
  110. break;
  111. case CDF_FLOAT:
  112. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  113. info[i].pi_f) == -1)
  114. return -1;
  115. break;
  116. case CDF_DOUBLE:
  117. if (NOTMIME(ms) && file_printf(ms, ", %s: %g", buf,
  118. info[i].pi_d) == -1)
  119. return -1;
  120. break;
  121. case CDF_LENGTH32_STRING:
  122. case CDF_LENGTH32_WSTRING:
  123. len = info[i].pi_str.s_len;
  124. if (len > 1) {
  125. char vbuf[1024];
  126. size_t j, k = 1;
  127. if (info[i].pi_type == CDF_LENGTH32_WSTRING)
  128. k++;
  129. s = info[i].pi_str.s_buf;
  130. for (j = 0; j < sizeof(vbuf) && len--;
  131. j++, s += k) {
  132. if (*s == '\0')
  133. break;
  134. if (isprint((unsigned char)*s))
  135. vbuf[j] = *s;
  136. }
  137. if (j == sizeof(vbuf))
  138. --j;
  139. vbuf[j] = '\0';
  140. if (NOTMIME(ms)) {
  141. if (vbuf[0]) {
  142. if (file_printf(ms, ", %s: %s",
  143. buf, vbuf) == -1)
  144. return -1;
  145. }
  146. } else if (info[i].pi_id ==
  147. CDF_PROPERTY_NAME_OF_APPLICATION) {
  148. str = cdf_app_to_mime(vbuf, app2mime);
  149. }
  150. }
  151. break;
  152. case CDF_FILETIME:
  153. tp = info[i].pi_tp;
  154. if (tp != 0) {
  155. char tbuf[64];
  156. if (tp < 1000000000000000LL) {
  157. cdf_print_elapsed_time(tbuf,
  158. sizeof(tbuf), tp);
  159. if (NOTMIME(ms) && file_printf(ms,
  160. ", %s: %s", buf, tbuf) == -1)
  161. return -1;
  162. } else {
  163. char *c, *ec;
  164. cdf_timestamp_to_timespec(&ts, tp);
  165. c = cdf_ctime(&ts.tv_sec, tbuf);
  166. if (c != NULL &&
  167. (ec = strchr(c, '\n')) != NULL)
  168. *ec = '\0';
  169. if (NOTMIME(ms) && file_printf(ms,
  170. ", %s: %s", buf, c) == -1)
  171. return -1;
  172. }
  173. }
  174. break;
  175. case CDF_CLIPBOARD:
  176. break;
  177. default:
  178. return -1;
  179. }
  180. }
  181. if (!NOTMIME(ms)) {
  182. if (str == NULL)
  183. return 0;
  184. if (file_printf(ms, "application/%s", str) == -1)
  185. return -1;
  186. }
  187. return 1;
  188. }
  189. private int
  190. cdf_file_summary_info(struct magic_set *ms, const cdf_header_t *h,
  191. const cdf_stream_t *sst)
  192. {
  193. cdf_summary_info_header_t si;
  194. cdf_property_info_t *info;
  195. size_t count;
  196. int m;
  197. if (cdf_unpack_summary_info(sst, h, &si, &info, &count) == -1)
  198. return -1;
  199. if (NOTMIME(ms)) {
  200. if (file_printf(ms, "Composite Document File V2 Document")
  201. == -1)
  202. return -1;
  203. if (file_printf(ms, ", %s Endian",
  204. si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
  205. return -2;
  206. switch (si.si_os) {
  207. case 2:
  208. if (file_printf(ms, ", Os: Windows, Version %d.%d",
  209. si.si_os_version & 0xff,
  210. (uint32_t)si.si_os_version >> 8) == -1)
  211. return -2;
  212. break;
  213. case 1:
  214. if (file_printf(ms, ", Os: MacOS, Version %d.%d",
  215. (uint32_t)si.si_os_version >> 8,
  216. si.si_os_version & 0xff) == -1)
  217. return -2;
  218. break;
  219. default:
  220. if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
  221. si.si_os_version & 0xff,
  222. (uint32_t)si.si_os_version >> 8) == -1)
  223. return -2;
  224. break;
  225. }
  226. }
  227. m = cdf_file_property_info(ms, info, count);
  228. free(info);
  229. return m == -1 ? -2 : m;
  230. }
  231. protected int
  232. file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
  233. size_t nbytes)
  234. {
  235. cdf_info_t info;
  236. cdf_header_t h;
  237. cdf_sat_t sat, ssat;
  238. cdf_stream_t sst, scn;
  239. cdf_dir_t dir;
  240. int i;
  241. const char *expn = "";
  242. const char *corrupt = "corrupt: ";
  243. info.i_fd = fd;
  244. info.i_buf = buf;
  245. info.i_len = nbytes;
  246. if (ms->flags & MAGIC_APPLE)
  247. return 0;
  248. if (cdf_read_header(&info, &h) == -1)
  249. return 0;
  250. #ifdef CDF_DEBUG
  251. cdf_dump_header(&h);
  252. #endif
  253. if ((i = cdf_read_sat(&info, &h, &sat)) == -1) {
  254. expn = "Can't read SAT";
  255. goto out0;
  256. }
  257. #ifdef CDF_DEBUG
  258. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  259. #endif
  260. if ((i = cdf_read_ssat(&info, &h, &sat, &ssat)) == -1) {
  261. expn = "Can't read SSAT";
  262. goto out1;
  263. }
  264. #ifdef CDF_DEBUG
  265. cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
  266. #endif
  267. if ((i = cdf_read_dir(&info, &h, &sat, &dir)) == -1) {
  268. expn = "Can't read directory";
  269. goto out2;
  270. }
  271. if ((i = cdf_read_short_stream(&info, &h, &sat, &dir, &sst)) == -1) {
  272. expn = "Cannot read short stream";
  273. goto out3;
  274. }
  275. #ifdef CDF_DEBUG
  276. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  277. #endif
  278. if ((i = cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  279. &scn)) == -1) {
  280. if (errno == ESRCH) {
  281. corrupt = expn;
  282. expn = "No summary info";
  283. } else {
  284. expn = "Cannot read summary info";
  285. }
  286. goto out4;
  287. }
  288. #ifdef CDF_DEBUG
  289. cdf_dump_summary_info(&h, &scn);
  290. #endif
  291. if ((i = cdf_file_summary_info(ms, &h, &scn)) < 0)
  292. expn = "Can't expand summary_info";
  293. if (i == 0) {
  294. const char *str = NULL;
  295. cdf_directory_t *d;
  296. char name[__arraycount(d->d_name)];
  297. size_t j, k;
  298. for (j = 0; j < dir.dir_len; j++) {
  299. d = &dir.dir_tab[j];
  300. for (k = 0; k < sizeof(name); k++)
  301. name[k] = (char)cdf_tole2(d->d_name[k]);
  302. if (NOTMIME(ms))
  303. str = cdf_app_to_mime(name, name2desc);
  304. else
  305. str = cdf_app_to_mime(name, name2mime);
  306. if (str != NULL)
  307. break;
  308. }
  309. if (NOTMIME(ms)) {
  310. if (str != NULL) {
  311. if (file_printf(ms, "%s", str) == -1)
  312. return -1;
  313. i = 1;
  314. }
  315. } else {
  316. if (str == NULL)
  317. str = "vnd.ms-office";
  318. if (file_printf(ms, "application/%s", str) == -1)
  319. return -1;
  320. i = 1;
  321. }
  322. }
  323. free(scn.sst_tab);
  324. out4:
  325. free(sst.sst_tab);
  326. out3:
  327. free(dir.dir_tab);
  328. out2:
  329. free(ssat.sat_tab);
  330. out1:
  331. free(sat.sat_tab);
  332. out0:
  333. if (i == -1) {
  334. if (NOTMIME(ms)) {
  335. if (file_printf(ms,
  336. "Composite Document File V2 Document") == -1)
  337. return -1;
  338. if (*expn)
  339. if (file_printf(ms, ", %s%s", corrupt, expn) == -1)
  340. return -1;
  341. } else {
  342. if (file_printf(ms, "application/CDFV2-corrupt") == -1)
  343. return -1;
  344. }
  345. i = 1;
  346. }
  347. return i;
  348. }