readcdf.c 12 KB

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