1 /* $NetBSD: cdf.h,v 1.1.1.5 2014/06/13 01:48:23 christos Exp $ */ 2 /*- 3 * Copyright (c) 2008 Christos Zoulas 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 /* 28 * Parse Composite Document Files, the format used in Microsoft Office 29 * document files before they switched to zipped XML. 30 * Info from: http://sc.openoffice.org/compdocfileformat.pdf 31 * 32 * N.B. This is the "Composite Document File" format, and not the 33 * "Compound Document Format", nor the "Channel Definition Format". 34 */ 35 36 #ifndef _H_CDF_ 37 #define _H_CDF_ 38 39 #ifdef WIN32 40 #include <winsock2.h> 41 #define timespec timeval 42 #define tv_nsec tv_usec 43 #endif 44 #ifdef __DJGPP__ 45 #define timespec timeval 46 #define tv_nsec tv_usec 47 #endif 48 49 typedef int32_t cdf_secid_t; 50 51 #define CDF_LOOP_LIMIT 10000 52 53 #define CDF_SECID_NULL 0 54 #define CDF_SECID_FREE -1 55 #define CDF_SECID_END_OF_CHAIN -2 56 #define CDF_SECID_SECTOR_ALLOCATION_TABLE -3 57 #define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE -4 58 59 typedef struct { 60 uint64_t h_magic; 61 #define CDF_MAGIC 0xE11AB1A1E011CFD0LL 62 uint64_t h_uuid[2]; 63 uint16_t h_revision; 64 uint16_t h_version; 65 uint16_t h_byte_order; 66 uint16_t h_sec_size_p2; 67 uint16_t h_short_sec_size_p2; 68 uint8_t h_unused0[10]; 69 uint32_t h_num_sectors_in_sat; 70 uint32_t h_secid_first_directory; 71 uint8_t h_unused1[4]; 72 uint32_t h_min_size_standard_stream; 73 cdf_secid_t h_secid_first_sector_in_short_sat; 74 uint32_t h_num_sectors_in_short_sat; 75 cdf_secid_t h_secid_first_sector_in_master_sat; 76 uint32_t h_num_sectors_in_master_sat; 77 cdf_secid_t h_master_sat[436/4]; 78 } cdf_header_t; 79 80 #define CDF_SEC_SIZE(h) ((size_t)(1 << (h)->h_sec_size_p2)) 81 #define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h)) 82 #define CDF_SHORT_SEC_SIZE(h) ((size_t)(1 << (h)->h_short_sec_size_p2)) 83 #define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h)) 84 85 typedef int32_t cdf_dirid_t; 86 #define CDF_DIRID_NULL -1 87 88 typedef int64_t cdf_timestamp_t; 89 #define CDF_BASE_YEAR 1601 90 #define CDF_TIME_PREC 10000000 91 92 typedef struct { 93 uint16_t d_name[32]; 94 uint16_t d_namelen; 95 uint8_t d_type; 96 #define CDF_DIR_TYPE_EMPTY 0 97 #define CDF_DIR_TYPE_USER_STORAGE 1 98 #define CDF_DIR_TYPE_USER_STREAM 2 99 #define CDF_DIR_TYPE_LOCKBYTES 3 100 #define CDF_DIR_TYPE_PROPERTY 4 101 #define CDF_DIR_TYPE_ROOT_STORAGE 5 102 uint8_t d_color; 103 #define CDF_DIR_COLOR_READ 0 104 #define CDF_DIR_COLOR_BLACK 1 105 cdf_dirid_t d_left_child; 106 cdf_dirid_t d_right_child; 107 cdf_dirid_t d_storage; 108 uint64_t d_storage_uuid[2]; 109 uint32_t d_flags; 110 cdf_timestamp_t d_created; 111 cdf_timestamp_t d_modified; 112 cdf_secid_t d_stream_first_sector; 113 uint32_t d_size; 114 uint32_t d_unused0; 115 } cdf_directory_t; 116 117 #define CDF_DIRECTORY_SIZE 128 118 119 typedef struct { 120 cdf_secid_t *sat_tab; 121 size_t sat_len; 122 } cdf_sat_t; 123 124 typedef struct { 125 cdf_directory_t *dir_tab; 126 size_t dir_len; 127 } cdf_dir_t; 128 129 typedef struct { 130 void *sst_tab; 131 size_t sst_len; 132 size_t sst_dirlen; 133 } cdf_stream_t; 134 135 typedef struct { 136 uint32_t cl_dword; 137 uint16_t cl_word[2]; 138 uint8_t cl_two[2]; 139 uint8_t cl_six[6]; 140 } cdf_classid_t; 141 142 typedef struct { 143 uint16_t si_byte_order; 144 uint16_t si_zero; 145 uint16_t si_os_version; 146 uint16_t si_os; 147 cdf_classid_t si_class; 148 uint32_t si_count; 149 } cdf_summary_info_header_t; 150 151 #define CDF_SECTION_DECLARATION_OFFSET 0x1c 152 153 typedef struct { 154 cdf_classid_t sd_class; 155 uint32_t sd_offset; 156 } cdf_section_declaration_t; 157 158 typedef struct { 159 uint32_t sh_len; 160 uint32_t sh_properties; 161 } cdf_section_header_t; 162 163 typedef struct { 164 uint32_t pi_id; 165 uint32_t pi_type; 166 union { 167 uint16_t _pi_u16; 168 int16_t _pi_s16; 169 uint32_t _pi_u32; 170 int32_t _pi_s32; 171 uint64_t _pi_u64; 172 int64_t _pi_s64; 173 cdf_timestamp_t _pi_tp; 174 float _pi_f; 175 double _pi_d; 176 struct { 177 uint32_t s_len; 178 const char *s_buf; 179 } _pi_str; 180 } pi_val; 181 #define pi_u64 pi_val._pi_u64 182 #define pi_s64 pi_val._pi_s64 183 #define pi_u32 pi_val._pi_u32 184 #define pi_s32 pi_val._pi_s32 185 #define pi_u16 pi_val._pi_u16 186 #define pi_s16 pi_val._pi_s16 187 #define pi_f pi_val._pi_f 188 #define pi_d pi_val._pi_d 189 #define pi_tp pi_val._pi_tp 190 #define pi_str pi_val._pi_str 191 } cdf_property_info_t; 192 193 #define CDF_ROUND(val, by) (((val) + (by) - 1) & ~((by) - 1)) 194 195 /* Variant type definitions */ 196 #define CDF_EMPTY 0x00000000 197 #define CDF_NULL 0x00000001 198 #define CDF_SIGNED16 0x00000002 199 #define CDF_SIGNED32 0x00000003 200 #define CDF_FLOAT 0x00000004 201 #define CDF_DOUBLE 0x00000005 202 #define CDF_CY 0x00000006 203 #define CDF_DATE 0x00000007 204 #define CDF_BSTR 0x00000008 205 #define CDF_DISPATCH 0x00000009 206 #define CDF_ERROR 0x0000000a 207 #define CDF_BOOL 0x0000000b 208 #define CDF_VARIANT 0x0000000c 209 #define CDF_UNKNOWN 0x0000000d 210 #define CDF_DECIMAL 0x0000000e 211 #define CDF_SIGNED8 0x00000010 212 #define CDF_UNSIGNED8 0x00000011 213 #define CDF_UNSIGNED16 0x00000012 214 #define CDF_UNSIGNED32 0x00000013 215 #define CDF_SIGNED64 0x00000014 216 #define CDF_UNSIGNED64 0x00000015 217 #define CDF_INT 0x00000016 218 #define CDF_UINT 0x00000017 219 #define CDF_VOID 0x00000018 220 #define CDF_HRESULT 0x00000019 221 #define CDF_PTR 0x0000001a 222 #define CDF_SAFEARRAY 0x0000001b 223 #define CDF_CARRAY 0x0000001c 224 #define CDF_USERDEFINED 0x0000001d 225 #define CDF_LENGTH32_STRING 0x0000001e 226 #define CDF_LENGTH32_WSTRING 0x0000001f 227 #define CDF_FILETIME 0x00000040 228 #define CDF_BLOB 0x00000041 229 #define CDF_STREAM 0x00000042 230 #define CDF_STORAGE 0x00000043 231 #define CDF_STREAMED_OBJECT 0x00000044 232 #define CDF_STORED_OBJECT 0x00000045 233 #define CDF_BLOB_OBJECT 0x00000046 234 #define CDF_CLIPBOARD 0x00000047 235 #define CDF_CLSID 0x00000048 236 #define CDF_VECTOR 0x00001000 237 #define CDF_ARRAY 0x00002000 238 #define CDF_BYREF 0x00004000 239 #define CDF_RESERVED 0x00008000 240 #define CDF_ILLEGAL 0x0000ffff 241 #define CDF_ILLEGALMASKED 0x00000fff 242 #define CDF_TYPEMASK 0x00000fff 243 244 #define CDF_PROPERTY_CODE_PAGE 0x00000001 245 #define CDF_PROPERTY_TITLE 0x00000002 246 #define CDF_PROPERTY_SUBJECT 0x00000003 247 #define CDF_PROPERTY_AUTHOR 0x00000004 248 #define CDF_PROPERTY_KEYWORDS 0x00000005 249 #define CDF_PROPERTY_COMMENTS 0x00000006 250 #define CDF_PROPERTY_TEMPLATE 0x00000007 251 #define CDF_PROPERTY_LAST_SAVED_BY 0x00000008 252 #define CDF_PROPERTY_REVISION_NUMBER 0x00000009 253 #define CDF_PROPERTY_TOTAL_EDITING_TIME 0x0000000a 254 #define CDF_PROPERTY_LAST_PRINTED 0X0000000b 255 #define CDF_PROPERTY_CREATE_TIME 0x0000000c 256 #define CDF_PROPERTY_LAST_SAVED_TIME 0x0000000d 257 #define CDF_PROPERTY_NUMBER_OF_PAGES 0x0000000e 258 #define CDF_PROPERTY_NUMBER_OF_WORDS 0x0000000f 259 #define CDF_PROPERTY_NUMBER_OF_CHARACTERS 0x00000010 260 #define CDF_PROPERTY_THUMBNAIL 0x00000011 261 #define CDF_PROPERTY_NAME_OF_APPLICATION 0x00000012 262 #define CDF_PROPERTY_SECURITY 0x00000013 263 #define CDF_PROPERTY_LOCALE_ID 0x80000000 264 265 typedef struct { 266 int i_fd; 267 const unsigned char *i_buf; 268 size_t i_len; 269 } cdf_info_t; 270 271 struct timespec; 272 int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t); 273 int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *); 274 int cdf_read_header(const cdf_info_t *, cdf_header_t *); 275 void cdf_swap_header(cdf_header_t *); 276 void cdf_unpack_header(cdf_header_t *, char *); 277 void cdf_swap_dir(cdf_directory_t *); 278 void cdf_unpack_dir(cdf_directory_t *, char *); 279 void cdf_swap_class(cdf_classid_t *); 280 ssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t, 281 const cdf_header_t *, cdf_secid_t); 282 ssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t, 283 const cdf_header_t *, cdf_secid_t); 284 int cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *); 285 size_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t); 286 int cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *, 287 const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *); 288 int cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *, 289 const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *); 290 int cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *, 291 const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t, 292 size_t, cdf_stream_t *); 293 int cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, 294 cdf_dir_t *); 295 int cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, 296 cdf_sat_t *); 297 int cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *, 298 const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *, 299 const cdf_directory_t **); 300 int cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t, 301 cdf_property_info_t **, size_t *, size_t *); 302 int cdf_read_user_stream(const cdf_info_t *, const cdf_header_t *, 303 const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, 304 const cdf_dir_t *, const char *, cdf_stream_t *); 305 int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *, 306 const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, 307 const cdf_dir_t *, cdf_stream_t *); 308 int cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *, 309 cdf_summary_info_header_t *, cdf_property_info_t **, size_t *); 310 int cdf_print_classid(char *, size_t, const cdf_classid_t *); 311 int cdf_print_property_name(char *, size_t, uint32_t); 312 int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t); 313 uint16_t cdf_tole2(uint16_t); 314 uint32_t cdf_tole4(uint32_t); 315 uint64_t cdf_tole8(uint64_t); 316 char *cdf_ctime(const time_t *, char *); 317 318 #ifdef CDF_DEBUG 319 void cdf_dump_header(const cdf_header_t *); 320 void cdf_dump_sat(const char *, const cdf_sat_t *, size_t); 321 void cdf_dump(void *, size_t); 322 void cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *); 323 void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, 324 const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *); 325 void cdf_dump_property_info(const cdf_property_info_t *, size_t); 326 void cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *); 327 #endif 328 329 330 #endif /* _H_CDF_ */ 331