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