cdf.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /*
  27. * Info from: http://sc.openoffice.org/compdocfileformat.pdf
  28. */
  29. #ifndef _H_CDF_
  30. #define _H_CDF_
  31. typedef int32_t cdf_secid_t;
  32. #define CDF_LOOP_LIMIT 10000
  33. #define CDF_SECID_NULL 0
  34. #define CDF_SECID_FREE -1
  35. #define CDF_SECID_END_OF_CHAIN -2
  36. #define CDF_SECID_SECTOR_ALLOCATION_TABLE -3
  37. #define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE -4
  38. typedef struct {
  39. uint64_t h_magic;
  40. #define CDF_MAGIC 0xE11AB1A1E011CFD0LL
  41. uint64_t h_uuid[2];
  42. uint16_t h_revision;
  43. uint16_t h_version;
  44. uint16_t h_byte_order;
  45. uint16_t h_sec_size_p2;
  46. uint16_t h_short_sec_size_p2;
  47. uint8_t h_unused0[10];
  48. uint32_t h_num_sectors_in_sat;
  49. uint32_t h_secid_first_directory;
  50. uint8_t h_unused1[4];
  51. uint32_t h_min_size_standard_stream;
  52. cdf_secid_t h_secid_first_sector_in_short_sat;
  53. uint32_t h_num_sectors_in_short_sat;
  54. cdf_secid_t h_secid_first_sector_in_master_sat;
  55. uint32_t h_num_sectors_in_master_sat;
  56. cdf_secid_t h_master_sat[436/4];
  57. } cdf_header_t;
  58. #define CDF_SEC_SIZE(h) (1 << (h)->h_sec_size_p2)
  59. #define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h))
  60. #define CDF_SHORT_SEC_SIZE(h) (1 << (h)->h_short_sec_size_p2)
  61. #define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h))
  62. typedef int32_t cdf_dirid_t;
  63. #define CDF_DIRID_NULL -1
  64. typedef int64_t cdf_timestamp_t;
  65. #define CDF_BASE_YEAR 1601
  66. #define CDF_TIME_PREC 10000000
  67. typedef struct {
  68. uint16_t d_name[32];
  69. uint16_t d_namelen;
  70. uint8_t d_type;
  71. #define CDF_DIR_TYPE_EMPTY 0
  72. #define CDF_DIR_TYPE_USER_STORAGE 1
  73. #define CDF_DIR_TYPE_USER_STREAM 2
  74. #define CDF_DIR_TYPE_LOCKBYTES 3
  75. #define CDF_DIR_TYPE_PROPERTY 4
  76. #define CDF_DIR_TYPE_ROOT_STORAGE 5
  77. uint8_t d_color;
  78. #define CDF_DIR_COLOR_READ 0
  79. #define CDF_DIR_COLOR_BLACK 1
  80. cdf_dirid_t d_left_child;
  81. cdf_dirid_t d_right_child;
  82. cdf_dirid_t d_storage;
  83. uint64_t d_storage_uuid[2];
  84. uint32_t d_flags;
  85. cdf_timestamp_t d_created;
  86. cdf_timestamp_t d_modified;
  87. cdf_secid_t d_stream_first_sector;
  88. uint32_t d_size;
  89. uint32_t d_unused0;
  90. } cdf_directory_t;
  91. #define CDF_DIRECTORY_SIZE 128
  92. typedef struct {
  93. cdf_secid_t *sat_tab;
  94. size_t sat_len;
  95. } cdf_sat_t;
  96. typedef struct {
  97. cdf_directory_t *dir_tab;
  98. size_t dir_len;
  99. } cdf_dir_t;
  100. typedef struct {
  101. void *sst_tab;
  102. size_t sst_len;
  103. size_t sst_dirlen;
  104. } cdf_stream_t;
  105. typedef struct {
  106. uint32_t cl_dword;
  107. uint16_t cl_word[2];
  108. uint8_t cl_two[2];
  109. uint8_t cl_six[6];
  110. } cdf_classid_t;
  111. typedef struct {
  112. uint16_t si_byte_order;
  113. uint16_t si_zero;
  114. uint16_t si_os_version;
  115. uint16_t si_os;
  116. cdf_classid_t si_class;
  117. uint32_t si_count;
  118. } cdf_summary_info_header_t;
  119. #define CDF_SECTION_DECLARATION_OFFSET 0x1c
  120. typedef struct {
  121. cdf_classid_t sd_class;
  122. uint32_t sd_offset;
  123. } cdf_section_declaration_t;
  124. typedef struct {
  125. uint32_t sh_len;
  126. uint32_t sh_properties;
  127. } cdf_section_header_t;
  128. typedef struct {
  129. uint32_t pi_id;
  130. uint32_t pi_type;
  131. union {
  132. uint16_t _pi_u16;
  133. int16_t _pi_s16;
  134. uint32_t _pi_u32;
  135. int32_t _pi_s32;
  136. uint64_t _pi_u64;
  137. int64_t _pi_s64;
  138. cdf_timestamp_t _pi_tp;
  139. struct {
  140. uint32_t s_len;
  141. const char *s_buf;
  142. } _pi_str;
  143. } pi_val;
  144. #define pi_u64 pi_val._pi_u64
  145. #define pi_s64 pi_val._pi_s64
  146. #define pi_u32 pi_val._pi_u32
  147. #define pi_s32 pi_val._pi_s32
  148. #define pi_u16 pi_val._pi_u16
  149. #define pi_s16 pi_val._pi_s16
  150. #define pi_tp pi_val._pi_tp
  151. #define pi_str pi_val._pi_str
  152. } cdf_property_info_t;
  153. #define CDF_ROUND(val, by) (((val) + (by) - 1) & ~((by) - 1))
  154. /* Variant type definitions */
  155. #define CDF_EMPTY 0x00000000
  156. #define CDF_NULL 0x00000001
  157. #define CDF_SIGNED16 0x00000002
  158. #define CDF_SIGNED32 0x00000003
  159. #define CDF_FLOAT 0x00000004
  160. #define CDF_DOUBLE 0x00000005
  161. #define CDF_CY 0x00000006
  162. #define CDF_DATE 0x00000007
  163. #define CDF_BSTR 0x00000008
  164. #define CDF_DISPATCH 0x00000009
  165. #define CDF_ERROR 0x0000000a
  166. #define CDF_BOOL 0x0000000b
  167. #define CDF_VARIANT 0x0000000c
  168. #define CDF_UNKNOWN 0x0000000d
  169. #define CDF_DECIMAL 0x0000000e
  170. #define CDF_SIGNED8 0x00000010
  171. #define CDF_UNSIGNED8 0x00000011
  172. #define CDF_UNSIGNED16 0x00000012
  173. #define CDF_UNSIGNED32 0x00000013
  174. #define CDF_SIGNED64 0x00000014
  175. #define CDF_UNSIGNED64 0x00000015
  176. #define CDF_INT 0x00000016
  177. #define CDF_UINT 0x00000017
  178. #define CDF_VOID 0x00000018
  179. #define CDF_HRESULT 0x00000019
  180. #define CDF_PTR 0x0000001a
  181. #define CDF_SAFEARRAY 0x0000001b
  182. #define CDF_CARRAY 0x0000001c
  183. #define CDF_USERDEFINED 0x0000001d
  184. #define CDF_LENGTH32_STRING 0x0000001e
  185. #define CDF_LENGTH32_WSTRING 0x0000001f
  186. #define CDF_FILETIME 0x00000040
  187. #define CDF_BLOB 0x00000041
  188. #define CDF_STREAM 0x00000042
  189. #define CDF_STORAGE 0x00000043
  190. #define CDF_STREAMED_OBJECT 0x00000044
  191. #define CDF_STORED_OBJECT 0x00000045
  192. #define CDF_BLOB_OBJECT 0x00000046
  193. #define CDF_CLIPBOARD 0x00000047
  194. #define CDF_CLSID 0x00000048
  195. #define CDF_VECTOR 0x00001000
  196. #define CDF_ARRAY 0x00002000
  197. #define CDF_BYREF 0x00004000
  198. #define CDF_RESERVED 0x00008000
  199. #define CDF_ILLEGAL 0x0000ffff
  200. #define CDF_ILLEGALMASKED 0x00000fff
  201. #define CDF_TYPEMASK 0x00000fff
  202. #define CDF_PROPERTY_CODE_PAGE 0x00000001
  203. #define CDF_PROPERTY_TITLE 0x00000002
  204. #define CDF_PROPERTY_SUBJECT 0x00000003
  205. #define CDF_PROPERTY_AUTHOR 0x00000004
  206. #define CDF_PROPERTY_KEYWORDS 0x00000005
  207. #define CDF_PROPERTY_COMMENTS 0x00000006
  208. #define CDF_PROPERTY_TEMPLATE 0x00000007
  209. #define CDF_PROPERTY_LAST_SAVED_BY 0x00000008
  210. #define CDF_PROPERTY_REVISION_NUMBER 0x00000009
  211. #define CDF_PROPERTY_TOTAL_EDITING_TIME 0x0000000a
  212. #define CDF_PROPERTY_LAST_PRINTED 0X0000000b
  213. #define CDF_PROPERTY_CREATE_TIME 0x0000000c
  214. #define CDF_PROPERTY_LAST_SAVED_TIME 0x0000000d
  215. #define CDF_PROPERTY_NUMBER_OF_PAGES 0x0000000e
  216. #define CDF_PROPERTY_NUMBER_OF_WORDS 0x0000000f
  217. #define CDF_PROPERTY_NUMBER_OF_CHARACTERS 0x00000010
  218. #define CDF_PROPERTY_THUMBNAIL 0x00000011
  219. #define CDF_PROPERTY_NAME_OF_APPLICATION 0x00000012
  220. #define CDF_PROPERTY_SECURITY 0x00000013
  221. #define CDF_PROPERTY_LOCALE_ID 0x80000000
  222. typedef struct {
  223. int i_fd;
  224. const unsigned char *i_buf;
  225. size_t i_len;
  226. } cdf_info_t;
  227. struct timespec;
  228. int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
  229. int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
  230. int cdf_read_header(const cdf_info_t *, cdf_header_t *);
  231. void cdf_swap_header(cdf_header_t *);
  232. void cdf_unpack_header(cdf_header_t *, char *);
  233. void cdf_swap_dir(cdf_directory_t *);
  234. void cdf_unpack_dir(cdf_directory_t *, char *);
  235. void cdf_swap_class(cdf_classid_t *);
  236. ssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t,
  237. const cdf_header_t *, cdf_secid_t);
  238. ssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t,
  239. const cdf_header_t *, cdf_secid_t);
  240. int cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *);
  241. size_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t);
  242. int cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *,
  243. const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *);
  244. int cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *,
  245. const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *);
  246. int cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *,
  247. const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t,
  248. size_t, cdf_stream_t *);
  249. int cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
  250. cdf_dir_t *);
  251. int cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
  252. cdf_sat_t *);
  253. int cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *,
  254. const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *);
  255. int cdf_read_property_info(const cdf_stream_t *, uint32_t,
  256. cdf_property_info_t **, size_t *, size_t *);
  257. int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
  258. const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
  259. const cdf_dir_t *, cdf_stream_t *);
  260. int cdf_unpack_summary_info(const cdf_stream_t *, cdf_summary_info_header_t *,
  261. cdf_property_info_t **, size_t *);
  262. int cdf_print_classid(char *, size_t, const cdf_classid_t *);
  263. int cdf_print_property_name(char *, size_t, uint32_t);
  264. int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t);
  265. uint16_t cdf_tole2(uint16_t);
  266. uint32_t cdf_tole4(uint32_t);
  267. uint64_t cdf_tole8(uint64_t);
  268. #ifdef CDF_DEBUG
  269. void cdf_dump_header(const cdf_header_t *);
  270. void cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
  271. void cdf_dump(void *, size_t);
  272. void cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *);
  273. void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
  274. const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
  275. void cdf_dump_property_info(const cdf_property_info_t *, size_t);
  276. void cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *);
  277. #endif
  278. #endif /* _H_CDF_ */