1284194Sdelphij/*- 2284194Sdelphij * Copyright (c) 2008 Christos Zoulas 3284194Sdelphij * All rights reserved. 4284194Sdelphij * 5284194Sdelphij * Redistribution and use in source and binary forms, with or without 6284194Sdelphij * modification, are permitted provided that the following conditions 7284194Sdelphij * are met: 8284194Sdelphij * 1. Redistributions of source code must retain the above copyright 9284194Sdelphij * notice, this list of conditions and the following disclaimer. 10284194Sdelphij * 2. Redistributions in binary form must reproduce the above copyright 11284194Sdelphij * notice, this list of conditions and the following disclaimer in the 12284194Sdelphij * documentation and/or other materials provided with the distribution. 13284194Sdelphij * 14284194Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 15284194Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16284194Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17284194Sdelphij * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 18284194Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19284194Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20284194Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21284194Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22284194Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23284194Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24284194Sdelphij * POSSIBILITY OF SUCH DAMAGE. 25284194Sdelphij */ 26284194Sdelphij/* 27284194Sdelphij * Parse Composite Document Files, the format used in Microsoft Office 28284194Sdelphij * document files before they switched to zipped XML. 29284194Sdelphij * Info from: http://sc.openoffice.org/compdocfileformat.pdf 30284194Sdelphij * 31284194Sdelphij * N.B. This is the "Composite Document File" format, and not the 32284194Sdelphij * "Compound Document Format", nor the "Channel Definition Format". 33284194Sdelphij */ 34284194Sdelphij 35284194Sdelphij#ifndef _H_CDF_ 36284194Sdelphij#define _H_CDF_ 37284194Sdelphij 38284194Sdelphij#ifdef WIN32 39284194Sdelphij#include <winsock2.h> 40284194Sdelphij#define timespec timeval 41284194Sdelphij#define tv_nsec tv_usec 42284194Sdelphij#endif 43284194Sdelphij#ifdef __DJGPP__ 44284194Sdelphij#define timespec timeval 45284194Sdelphij#define tv_nsec tv_usec 46284194Sdelphij#endif 47284194Sdelphij 48284194Sdelphijtypedef int32_t cdf_secid_t; 49284194Sdelphij 50284194Sdelphij#define CDF_LOOP_LIMIT 10000 51284194Sdelphij 52284194Sdelphij#define CDF_SECID_NULL 0 53284194Sdelphij#define CDF_SECID_FREE -1 54284194Sdelphij#define CDF_SECID_END_OF_CHAIN -2 55284194Sdelphij#define CDF_SECID_SECTOR_ALLOCATION_TABLE -3 56284194Sdelphij#define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE -4 57284194Sdelphij 58284194Sdelphijtypedef struct { 59284194Sdelphij uint64_t h_magic; 60284194Sdelphij#define CDF_MAGIC 0xE11AB1A1E011CFD0LL 61284194Sdelphij uint64_t h_uuid[2]; 62284194Sdelphij uint16_t h_revision; 63284194Sdelphij uint16_t h_version; 64284194Sdelphij uint16_t h_byte_order; 65284194Sdelphij uint16_t h_sec_size_p2; 66284194Sdelphij uint16_t h_short_sec_size_p2; 67284194Sdelphij uint8_t h_unused0[10]; 68284194Sdelphij uint32_t h_num_sectors_in_sat; 69284194Sdelphij uint32_t h_secid_first_directory; 70284194Sdelphij uint8_t h_unused1[4]; 71284194Sdelphij uint32_t h_min_size_standard_stream; 72284194Sdelphij cdf_secid_t h_secid_first_sector_in_short_sat; 73284194Sdelphij uint32_t h_num_sectors_in_short_sat; 74284194Sdelphij cdf_secid_t h_secid_first_sector_in_master_sat; 75284194Sdelphij uint32_t h_num_sectors_in_master_sat; 76284194Sdelphij cdf_secid_t h_master_sat[436/4]; 77284194Sdelphij} cdf_header_t; 78284194Sdelphij 79284194Sdelphij#define CDF_SEC_SIZE(h) ((size_t)(1 << (h)->h_sec_size_p2)) 80284194Sdelphij#define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h)) 81284194Sdelphij#define CDF_SHORT_SEC_SIZE(h) ((size_t)(1 << (h)->h_short_sec_size_p2)) 82284194Sdelphij#define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h)) 83284194Sdelphij 84284194Sdelphijtypedef int32_t cdf_dirid_t; 85284194Sdelphij#define CDF_DIRID_NULL -1 86284194Sdelphij 87284194Sdelphijtypedef int64_t cdf_timestamp_t; 88284194Sdelphij#define CDF_BASE_YEAR 1601 89284194Sdelphij#define CDF_TIME_PREC 10000000 90284194Sdelphij 91284194Sdelphijtypedef struct { 92284194Sdelphij uint16_t d_name[32]; 93284194Sdelphij uint16_t d_namelen; 94284194Sdelphij uint8_t d_type; 95284194Sdelphij#define CDF_DIR_TYPE_EMPTY 0 96284194Sdelphij#define CDF_DIR_TYPE_USER_STORAGE 1 97284194Sdelphij#define CDF_DIR_TYPE_USER_STREAM 2 98284194Sdelphij#define CDF_DIR_TYPE_LOCKBYTES 3 99284194Sdelphij#define CDF_DIR_TYPE_PROPERTY 4 100284194Sdelphij#define CDF_DIR_TYPE_ROOT_STORAGE 5 101284194Sdelphij uint8_t d_color; 102284194Sdelphij#define CDF_DIR_COLOR_READ 0 103284194Sdelphij#define CDF_DIR_COLOR_BLACK 1 104284194Sdelphij cdf_dirid_t d_left_child; 105284194Sdelphij cdf_dirid_t d_right_child; 106284194Sdelphij cdf_dirid_t d_storage; 107284194Sdelphij uint64_t d_storage_uuid[2]; 108284194Sdelphij uint32_t d_flags; 109284194Sdelphij cdf_timestamp_t d_created; 110284194Sdelphij cdf_timestamp_t d_modified; 111284194Sdelphij cdf_secid_t d_stream_first_sector; 112284194Sdelphij uint32_t d_size; 113284194Sdelphij uint32_t d_unused0; 114284194Sdelphij} cdf_directory_t; 115284194Sdelphij 116284194Sdelphij#define CDF_DIRECTORY_SIZE 128 117284194Sdelphij 118284194Sdelphijtypedef struct { 119284194Sdelphij cdf_secid_t *sat_tab; 120284194Sdelphij size_t sat_len; 121284194Sdelphij} cdf_sat_t; 122284194Sdelphij 123284194Sdelphijtypedef struct { 124284194Sdelphij cdf_directory_t *dir_tab; 125284194Sdelphij size_t dir_len; 126284194Sdelphij} cdf_dir_t; 127284194Sdelphij 128284194Sdelphijtypedef struct { 129284194Sdelphij void *sst_tab; 130284194Sdelphij size_t sst_len; 131284194Sdelphij size_t sst_dirlen; 132284194Sdelphij} cdf_stream_t; 133284194Sdelphij 134284194Sdelphijtypedef struct { 135284194Sdelphij uint32_t cl_dword; 136284194Sdelphij uint16_t cl_word[2]; 137284194Sdelphij uint8_t cl_two[2]; 138284194Sdelphij uint8_t cl_six[6]; 139284194Sdelphij} cdf_classid_t; 140284194Sdelphij 141284194Sdelphijtypedef struct { 142284194Sdelphij uint16_t si_byte_order; 143284194Sdelphij uint16_t si_zero; 144284194Sdelphij uint16_t si_os_version; 145284194Sdelphij uint16_t si_os; 146284194Sdelphij cdf_classid_t si_class; 147284194Sdelphij uint32_t si_count; 148284194Sdelphij} cdf_summary_info_header_t; 149284194Sdelphij 150284194Sdelphij#define CDF_SECTION_DECLARATION_OFFSET 0x1c 151284194Sdelphij 152284194Sdelphijtypedef struct { 153284194Sdelphij cdf_classid_t sd_class; 154284194Sdelphij uint32_t sd_offset; 155284194Sdelphij} cdf_section_declaration_t; 156284194Sdelphij 157284194Sdelphijtypedef struct { 158284194Sdelphij uint32_t sh_len; 159284194Sdelphij uint32_t sh_properties; 160284194Sdelphij} cdf_section_header_t; 161284194Sdelphij 162284194Sdelphijtypedef struct { 163284194Sdelphij uint32_t pi_id; 164284194Sdelphij uint32_t pi_type; 165284194Sdelphij union { 166284194Sdelphij uint16_t _pi_u16; 167284194Sdelphij int16_t _pi_s16; 168284194Sdelphij uint32_t _pi_u32; 169284194Sdelphij int32_t _pi_s32; 170284194Sdelphij uint64_t _pi_u64; 171284194Sdelphij int64_t _pi_s64; 172284194Sdelphij cdf_timestamp_t _pi_tp; 173284194Sdelphij float _pi_f; 174284194Sdelphij double _pi_d; 175284194Sdelphij struct { 176284194Sdelphij uint32_t s_len; 177284194Sdelphij const char *s_buf; 178284194Sdelphij } _pi_str; 179284194Sdelphij } pi_val; 180284194Sdelphij#define pi_u64 pi_val._pi_u64 181284194Sdelphij#define pi_s64 pi_val._pi_s64 182284194Sdelphij#define pi_u32 pi_val._pi_u32 183284194Sdelphij#define pi_s32 pi_val._pi_s32 184284194Sdelphij#define pi_u16 pi_val._pi_u16 185284194Sdelphij#define pi_s16 pi_val._pi_s16 186284194Sdelphij#define pi_f pi_val._pi_f 187284194Sdelphij#define pi_d pi_val._pi_d 188284194Sdelphij#define pi_tp pi_val._pi_tp 189284194Sdelphij#define pi_str pi_val._pi_str 190284194Sdelphij} cdf_property_info_t; 191284194Sdelphij 192284194Sdelphij#define CDF_ROUND(val, by) (((val) + (by) - 1) & ~((by) - 1)) 193284194Sdelphij 194284194Sdelphij/* Variant type definitions */ 195284194Sdelphij#define CDF_EMPTY 0x00000000 196284194Sdelphij#define CDF_NULL 0x00000001 197284194Sdelphij#define CDF_SIGNED16 0x00000002 198284194Sdelphij#define CDF_SIGNED32 0x00000003 199284194Sdelphij#define CDF_FLOAT 0x00000004 200284194Sdelphij#define CDF_DOUBLE 0x00000005 201284194Sdelphij#define CDF_CY 0x00000006 202284194Sdelphij#define CDF_DATE 0x00000007 203284194Sdelphij#define CDF_BSTR 0x00000008 204284194Sdelphij#define CDF_DISPATCH 0x00000009 205284194Sdelphij#define CDF_ERROR 0x0000000a 206284194Sdelphij#define CDF_BOOL 0x0000000b 207284194Sdelphij#define CDF_VARIANT 0x0000000c 208284194Sdelphij#define CDF_UNKNOWN 0x0000000d 209284194Sdelphij#define CDF_DECIMAL 0x0000000e 210284194Sdelphij#define CDF_SIGNED8 0x00000010 211284194Sdelphij#define CDF_UNSIGNED8 0x00000011 212284194Sdelphij#define CDF_UNSIGNED16 0x00000012 213284194Sdelphij#define CDF_UNSIGNED32 0x00000013 214284194Sdelphij#define CDF_SIGNED64 0x00000014 215284194Sdelphij#define CDF_UNSIGNED64 0x00000015 216284194Sdelphij#define CDF_INT 0x00000016 217284194Sdelphij#define CDF_UINT 0x00000017 218284194Sdelphij#define CDF_VOID 0x00000018 219284194Sdelphij#define CDF_HRESULT 0x00000019 220284194Sdelphij#define CDF_PTR 0x0000001a 221284194Sdelphij#define CDF_SAFEARRAY 0x0000001b 222284194Sdelphij#define CDF_CARRAY 0x0000001c 223284194Sdelphij#define CDF_USERDEFINED 0x0000001d 224284194Sdelphij#define CDF_LENGTH32_STRING 0x0000001e 225284194Sdelphij#define CDF_LENGTH32_WSTRING 0x0000001f 226284194Sdelphij#define CDF_FILETIME 0x00000040 227284194Sdelphij#define CDF_BLOB 0x00000041 228284194Sdelphij#define CDF_STREAM 0x00000042 229284194Sdelphij#define CDF_STORAGE 0x00000043 230284194Sdelphij#define CDF_STREAMED_OBJECT 0x00000044 231284194Sdelphij#define CDF_STORED_OBJECT 0x00000045 232284194Sdelphij#define CDF_BLOB_OBJECT 0x00000046 233284194Sdelphij#define CDF_CLIPBOARD 0x00000047 234284194Sdelphij#define CDF_CLSID 0x00000048 235284194Sdelphij#define CDF_VECTOR 0x00001000 236284194Sdelphij#define CDF_ARRAY 0x00002000 237284194Sdelphij#define CDF_BYREF 0x00004000 238284194Sdelphij#define CDF_RESERVED 0x00008000 239284194Sdelphij#define CDF_ILLEGAL 0x0000ffff 240284194Sdelphij#define CDF_ILLEGALMASKED 0x00000fff 241284194Sdelphij#define CDF_TYPEMASK 0x00000fff 242284194Sdelphij 243284194Sdelphij#define CDF_PROPERTY_CODE_PAGE 0x00000001 244284194Sdelphij#define CDF_PROPERTY_TITLE 0x00000002 245284194Sdelphij#define CDF_PROPERTY_SUBJECT 0x00000003 246284194Sdelphij#define CDF_PROPERTY_AUTHOR 0x00000004 247284194Sdelphij#define CDF_PROPERTY_KEYWORDS 0x00000005 248284194Sdelphij#define CDF_PROPERTY_COMMENTS 0x00000006 249284194Sdelphij#define CDF_PROPERTY_TEMPLATE 0x00000007 250284194Sdelphij#define CDF_PROPERTY_LAST_SAVED_BY 0x00000008 251284194Sdelphij#define CDF_PROPERTY_REVISION_NUMBER 0x00000009 252284194Sdelphij#define CDF_PROPERTY_TOTAL_EDITING_TIME 0x0000000a 253284194Sdelphij#define CDF_PROPERTY_LAST_PRINTED 0X0000000b 254284194Sdelphij#define CDF_PROPERTY_CREATE_TIME 0x0000000c 255284194Sdelphij#define CDF_PROPERTY_LAST_SAVED_TIME 0x0000000d 256284194Sdelphij#define CDF_PROPERTY_NUMBER_OF_PAGES 0x0000000e 257284194Sdelphij#define CDF_PROPERTY_NUMBER_OF_WORDS 0x0000000f 258284194Sdelphij#define CDF_PROPERTY_NUMBER_OF_CHARACTERS 0x00000010 259284194Sdelphij#define CDF_PROPERTY_THUMBNAIL 0x00000011 260284194Sdelphij#define CDF_PROPERTY_NAME_OF_APPLICATION 0x00000012 261284194Sdelphij#define CDF_PROPERTY_SECURITY 0x00000013 262284194Sdelphij#define CDF_PROPERTY_LOCALE_ID 0x80000000 263284194Sdelphij 264284194Sdelphijtypedef struct { 265284194Sdelphij int i_fd; 266284194Sdelphij const unsigned char *i_buf; 267284194Sdelphij size_t i_len; 268284194Sdelphij} cdf_info_t; 269284194Sdelphij 270284194Sdelphij 271284194Sdelphijtypedef struct { 272284194Sdelphij uint16_t ce_namlen; 273284194Sdelphij uint32_t ce_num; 274284194Sdelphij uint64_t ce_timestamp; 275284194Sdelphij uint16_t ce_name[256]; 276284194Sdelphij} cdf_catalog_entry_t; 277284194Sdelphij 278284194Sdelphijtypedef struct { 279284194Sdelphij size_t cat_num; 280284194Sdelphij cdf_catalog_entry_t cat_e[0]; 281284194Sdelphij} cdf_catalog_t; 282284194Sdelphij 283284194Sdelphijstruct timespec; 284284194Sdelphijint cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t); 285284194Sdelphijint cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *); 286284194Sdelphijint cdf_read_header(const cdf_info_t *, cdf_header_t *); 287284194Sdelphijvoid cdf_swap_header(cdf_header_t *); 288284194Sdelphijvoid cdf_unpack_header(cdf_header_t *, char *); 289284194Sdelphijvoid cdf_swap_dir(cdf_directory_t *); 290284194Sdelphijvoid cdf_unpack_dir(cdf_directory_t *, char *); 291284194Sdelphijvoid cdf_swap_class(cdf_classid_t *); 292284194Sdelphijssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t, 293284194Sdelphij const cdf_header_t *, cdf_secid_t); 294284194Sdelphijssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t, 295284194Sdelphij const cdf_header_t *, cdf_secid_t); 296284194Sdelphijint cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *); 297284194Sdelphijsize_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t); 298284194Sdelphijint cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *, 299284194Sdelphij const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *); 300284194Sdelphijint cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *, 301284194Sdelphij const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *); 302284194Sdelphijint cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *, 303284194Sdelphij const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t, 304284194Sdelphij size_t, cdf_stream_t *); 305284194Sdelphijint cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, 306284194Sdelphij cdf_dir_t *); 307284194Sdelphijint cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, 308284194Sdelphij cdf_sat_t *); 309284194Sdelphijint cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *, 310284194Sdelphij const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *, 311284194Sdelphij const cdf_directory_t **); 312284194Sdelphijint cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t, 313284194Sdelphij cdf_property_info_t **, size_t *, size_t *); 314284194Sdelphijint cdf_read_user_stream(const cdf_info_t *, const cdf_header_t *, 315284194Sdelphij const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, 316284194Sdelphij const cdf_dir_t *, const char *, cdf_stream_t *); 317284194Sdelphij#define cdf_read_catalog(info, header, sat, ssat, stream, dir, scn) \ 318284194Sdelphij cdf_read_user_stream(info, header, sat, ssat, stream, dir, "Catalog", \ 319284194Sdelphij scn) 320284194Sdelphij#define cdf_read_encrypted_package(info, header, sat, ssat, stream, dir, scn) \ 321284194Sdelphij cdf_read_user_stream(info, header, sat, ssat, stream, dir, \ 322284194Sdelphij "EncryptedPackage", scn) 323284194Sdelphijint cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *, 324284194Sdelphij const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, 325284194Sdelphij const cdf_dir_t *, cdf_stream_t *); 326284194Sdelphijint cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *, 327284194Sdelphij cdf_summary_info_header_t *, cdf_property_info_t **, size_t *); 328284194Sdelphijint cdf_unpack_catalog(const cdf_header_t *, const cdf_stream_t *, 329284194Sdelphij cdf_catalog_t **); 330284194Sdelphijint cdf_print_classid(char *, size_t, const cdf_classid_t *); 331284194Sdelphijint cdf_print_property_name(char *, size_t, uint32_t); 332284194Sdelphijint cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t); 333284194Sdelphijuint16_t cdf_tole2(uint16_t); 334284194Sdelphijuint32_t cdf_tole4(uint32_t); 335284194Sdelphijuint64_t cdf_tole8(uint64_t); 336284194Sdelphijchar *cdf_ctime(const time_t *, char *); 337284194Sdelphijchar *cdf_u16tos8(char *, size_t, const uint16_t *); 338284194Sdelphij 339284194Sdelphij#ifdef CDF_DEBUG 340284194Sdelphijvoid cdf_dump_header(const cdf_header_t *); 341284194Sdelphijvoid cdf_dump_sat(const char *, const cdf_sat_t *, size_t); 342284194Sdelphijvoid cdf_dump(void *, size_t); 343284194Sdelphijvoid cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *); 344284194Sdelphijvoid cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *, 345284194Sdelphij const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *); 346284194Sdelphijvoid cdf_dump_property_info(const cdf_property_info_t *, size_t); 347284194Sdelphijvoid cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *); 348284194Sdelphijvoid cdf_dump_catalog(const cdf_header_t *, const cdf_stream_t *); 349284194Sdelphij#endif 350284194Sdelphij 351284194Sdelphij 352284194Sdelphij#endif /* _H_CDF_ */ 353