readcdf.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.11 2009/02/03 20:27:51 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 = "vnd.ms-office";
  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_SIGNED16:
  53. if (NOTMIME(ms) && file_printf(ms, ", %s: %hd", buf,
  54. info[i].pi_s16) == -1)
  55. return -1;
  56. break;
  57. case CDF_SIGNED32:
  58. if (NOTMIME(ms) && file_printf(ms, ", %s: %d", buf,
  59. info[i].pi_s32) == -1)
  60. return -1;
  61. break;
  62. case CDF_UNSIGNED32:
  63. if (NOTMIME(ms) && file_printf(ms, ", %s: %u", buf,
  64. info[i].pi_u32) == -1)
  65. return -1;
  66. break;
  67. case CDF_LENGTH32_STRING:
  68. len = info[i].pi_str.s_len;
  69. if (len > 1) {
  70. s = info[i].pi_str.s_buf;
  71. if (NOTMIME(ms)) {
  72. if (file_printf(ms, ", %s: %.*s", buf,
  73. len, s) == -1)
  74. return -1;
  75. } else if (info[i].pi_id ==
  76. CDF_PROPERTY_NAME_OF_APPLICATION) {
  77. if (strstr(s, "Word"))
  78. str = "msword";
  79. else if (strstr(s, "Excel"))
  80. str = "vnd.ms-excel";
  81. else if (strstr(s, "Powerpoint"))
  82. str = "vnd.ms-powerpoint";
  83. }
  84. }
  85. break;
  86. case CDF_FILETIME:
  87. tp = info[i].pi_tp;
  88. if (tp != 0) {
  89. if (tp < 1000000000000000LL) {
  90. char tbuf[64];
  91. cdf_print_elapsed_time(tbuf,
  92. sizeof(tbuf), tp);
  93. if (NOTMIME(ms) && file_printf(ms,
  94. ", %s: %s", buf, tbuf) == -1)
  95. return -1;
  96. } else {
  97. char *c, *ec;
  98. cdf_timestamp_to_timespec(&ts, tp);
  99. c = ctime(&ts.tv_sec);
  100. if ((ec = strchr(c, '\n')) != NULL)
  101. *ec = '\0';
  102. if (NOTMIME(ms) && file_printf(ms,
  103. ", %s: %s", buf, c) == -1)
  104. return -1;
  105. }
  106. }
  107. break;
  108. case CDF_CLIPBOARD:
  109. break;
  110. default:
  111. file_error(ms, 0, "Internal parsing error");
  112. return -1;
  113. }
  114. }
  115. if (!NOTMIME(ms)) {
  116. if (file_printf(ms, "application/%s", str) == -1)
  117. return -1;
  118. }
  119. return 1;
  120. }
  121. private int
  122. cdf_file_summary_info(struct magic_set *ms, const cdf_stream_t *sst)
  123. {
  124. cdf_summary_info_header_t si;
  125. cdf_property_info_t *info;
  126. size_t count;
  127. int m;
  128. if (cdf_unpack_summary_info(sst, &si, &info, &count) == -1) {
  129. if (si.si_byte_order != 0xfffe)
  130. return 0;
  131. else
  132. return -1;
  133. }
  134. if (si.si_byte_order != 0xfffe)
  135. return 0;
  136. if (NOTMIME(ms)) {
  137. if (file_printf(ms, "CDF V2 Document") == -1)
  138. return -1;
  139. if (file_printf(ms, ", %s Endian",
  140. si.si_byte_order == 0xfffe ? "Little" : "Big") == -1)
  141. return -1;
  142. switch (si.si_os) {
  143. case 2:
  144. if (file_printf(ms, ", Os: Windows, Version %d.%d",
  145. si.si_os_version & 0xff, si.si_os_version >> 8)
  146. == -1)
  147. return -1;
  148. break;
  149. case 1:
  150. if (file_printf(ms, ", Os: MacOS, Version %d.%d",
  151. si.si_os_version >> 8, si.si_os_version & 0xff)
  152. == -1)
  153. return -1;
  154. break;
  155. default:
  156. if (file_printf(ms, ", Os %d, Version: %d.%d", si.si_os,
  157. si.si_os_version & 0xff, si.si_os_version >> 8)
  158. == -1)
  159. return -1;
  160. break;
  161. }
  162. }
  163. m = cdf_file_property_info(ms, info, count);
  164. free(info);
  165. return m;
  166. }
  167. protected int
  168. file_trycdf(struct magic_set *ms, int fd, const unsigned char *buf,
  169. size_t nbytes)
  170. {
  171. cdf_header_t h;
  172. cdf_sat_t sat, ssat;
  173. cdf_stream_t sst, scn;
  174. cdf_dir_t dir;
  175. int i;
  176. (void)&nbytes;
  177. (void)&buf;
  178. if (ms->flags & MAGIC_APPLE)
  179. return 0;
  180. if (cdf_read_header(fd, &h) == -1)
  181. return 0;
  182. #ifdef CDF_DEBUG
  183. cdf_dump_header(&h);
  184. #endif
  185. if (cdf_read_sat(fd, &h, &sat) == -1) {
  186. file_error(ms, errno, "Can't read SAT");
  187. return -1;
  188. }
  189. #ifdef CDF_DEBUG
  190. cdf_dump_sat("SAT", &h, &sat);
  191. #endif
  192. if ((i = cdf_read_ssat(fd, &h, &sat, &ssat)) == -1) {
  193. file_error(ms, errno, "Can't read SAT");
  194. goto out1;
  195. }
  196. #ifdef CDF_DEBUG
  197. cdf_dump_sat("SSAT", &h, &ssat);
  198. #endif
  199. if ((i = cdf_read_dir(fd, &h, &sat, &dir)) == -1) {
  200. file_error(ms, errno, "Can't read directory");
  201. goto out2;
  202. }
  203. if ((i = cdf_read_short_stream(fd, &h, &sat, &dir, &sst)) == -1) {
  204. file_error(ms, errno, "Cannot read short stream");
  205. goto out3;
  206. }
  207. #ifdef CDF_DEBUG
  208. cdf_dump_dir(fd, &h, &sat, &ssat, &sst, &dir);
  209. #endif
  210. if ((i = cdf_read_summary_info(fd, &h, &sat, &ssat, &sst, &dir, &scn))
  211. == -1) {
  212. /* Some files don't have summary info! */
  213. #ifdef notyet
  214. file_error(ms, errno, "Can't read summary_info");
  215. #else
  216. i = 0;
  217. #endif
  218. goto out4;
  219. }
  220. #ifdef CDF_DEBUG
  221. cdf_dump_summary_info(&h, &scn);
  222. #endif
  223. if ((i = cdf_file_summary_info(ms, &scn)) == -1)
  224. file_error(ms, errno, "Can't expand summary_info");
  225. free(scn.sst_tab);
  226. out4:
  227. free(sst.sst_tab);
  228. out3:
  229. free(dir.dir_tab);
  230. out2:
  231. free(ssat.sat_tab);
  232. out1:
  233. free(sat.sat_tab);
  234. return i;
  235. }