Back to index

php5  5.3.10
cdf.h
Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 2008 Christos Zoulas
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
00015  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00016  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
00018  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00019  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00020  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00021  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00022  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00023  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00024  * POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 /*
00027  * Info from: http://sc.openoffice.org/compdocfileformat.pdf 
00028  */
00029 
00030 #ifndef _H_CDF_
00031 #define _H_CDF_
00032 
00033 typedef int32_t cdf_secid_t;
00034 
00035 #define CDF_LOOP_LIMIT                                  10000
00036 
00037 #define CDF_SECID_NULL                                  0
00038 #define CDF_SECID_FREE                                  -1
00039 #define       CDF_SECID_END_OF_CHAIN                           -2
00040 #define       CDF_SECID_SECTOR_ALLOCATION_TABLE         -3
00041 #define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE -4
00042 
00043 typedef struct {
00044        uint64_t      h_magic;
00045 #if defined(PHP_WIN32) && _MSC_VER <= 1500
00046 # define CDF_MAGIC   0xE11AB1A1E011CFD0i64
00047 #else
00048 # define CDF_MAGIC   0xE11AB1A1E011CFD0LL
00049 #endif
00050        uint64_t      h_uuid[2];
00051        uint16_t      h_revision;
00052        uint16_t      h_version;
00053        uint16_t      h_byte_order;
00054        uint16_t      h_sec_size_p2;
00055        uint16_t      h_short_sec_size_p2;
00056        uint8_t              h_unused0[10];
00057        uint32_t      h_num_sectors_in_sat;
00058        uint32_t      h_secid_first_directory;
00059        uint8_t              h_unused1[4];
00060        uint32_t      h_min_size_standard_stream;
00061        cdf_secid_t   h_secid_first_sector_in_short_sat;
00062        uint32_t      h_num_sectors_in_short_sat;
00063        cdf_secid_t   h_secid_first_sector_in_master_sat;
00064        uint32_t      h_num_sectors_in_master_sat;
00065        cdf_secid_t   h_master_sat[436/4];
00066 } cdf_header_t;
00067 
00068 #define CDF_SEC_SIZE(h)     (1 << (h)->h_sec_size_p2)
00069 #define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h))
00070 #define CDF_SHORT_SEC_SIZE(h)      (1 << (h)->h_short_sec_size_p2)
00071 #define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h))
00072 
00073 typedef int32_t      cdf_dirid_t;
00074 #define CDF_DIRID_NULL      -1
00075 
00076 typedef int64_t      cdf_timestamp_t;
00077 #define CDF_BASE_YEAR       1601
00078 #define CDF_TIME_PREC       10000000
00079 
00080 typedef struct {
00081        uint16_t      d_name[32];
00082        uint16_t      d_namelen;
00083        uint8_t              d_type;
00084 #define CDF_DIR_TYPE_EMPTY         0
00085 #define       CDF_DIR_TYPE_USER_STORAGE   1
00086 #define       CDF_DIR_TYPE_USER_STREAM    2
00087 #define       CDF_DIR_TYPE_LOCKBYTES             3
00088 #define       CDF_DIR_TYPE_PROPERTY              4
00089 #define       CDF_DIR_TYPE_ROOT_STORAGE   5
00090        uint8_t              d_color;
00091 #define CDF_DIR_COLOR_READ  0
00092 #define CDF_DIR_COLOR_BLACK 1
00093        cdf_dirid_t   d_left_child;
00094        cdf_dirid_t   d_right_child;
00095        cdf_dirid_t   d_storage;
00096        uint64_t      d_storage_uuid[2];
00097        uint32_t      d_flags;
00098        cdf_timestamp_t      d_created;
00099        cdf_timestamp_t      d_modified;
00100        cdf_secid_t   d_stream_first_sector;
00101        uint32_t      d_size;
00102        uint32_t      d_unused0;
00103 } cdf_directory_t;
00104 
00105 #define CDF_DIRECTORY_SIZE  128
00106 
00107 typedef struct {
00108        cdf_secid_t *sat_tab;
00109        size_t sat_len;
00110 } cdf_sat_t;
00111 
00112 typedef struct {
00113        cdf_directory_t *dir_tab;
00114        size_t dir_len;
00115 } cdf_dir_t;
00116 
00117 typedef struct {
00118        void *sst_tab;
00119        size_t sst_len;
00120        size_t sst_dirlen;
00121 } cdf_stream_t;
00122 
00123 typedef struct {
00124        uint32_t      cl_dword;
00125        uint16_t      cl_word[2];
00126        uint8_t              cl_two[2];
00127        uint8_t              cl_six[6];
00128 } cdf_classid_t;
00129 
00130 typedef struct {
00131        uint16_t      si_byte_order;
00132        uint16_t      si_zero;
00133        uint16_t      si_os_version;
00134        uint16_t      si_os;
00135        cdf_classid_t si_class;
00136        uint32_t      si_count;
00137 } cdf_summary_info_header_t;
00138 
00139 #define CDF_SECTION_DECLARATION_OFFSET 0x1c
00140 
00141 typedef struct {
00142        cdf_classid_t sd_class;
00143        uint32_t      sd_offset;
00144 } cdf_section_declaration_t;
00145 
00146 typedef struct {
00147        uint32_t      sh_len;
00148        uint32_t      sh_properties;
00149 } cdf_section_header_t;
00150 
00151 typedef struct {
00152        uint32_t      pi_id;
00153        uint32_t      pi_type;
00154        union {
00155               uint16_t      _pi_u16;
00156               int16_t              _pi_s16;
00157               uint32_t      _pi_u32;
00158               int32_t              _pi_s32;
00159               uint64_t      _pi_u64;
00160               int64_t              _pi_s64;
00161               cdf_timestamp_t      _pi_tp;
00162               struct {
00163                      uint32_t s_len;
00164                      const char *s_buf;
00165               } _pi_str;
00166        } pi_val;
00167 #define pi_u64       pi_val._pi_u64
00168 #define pi_s64       pi_val._pi_s64
00169 #define pi_u32       pi_val._pi_u32
00170 #define pi_s32       pi_val._pi_s32
00171 #define pi_u16       pi_val._pi_u16
00172 #define pi_s16       pi_val._pi_s16
00173 #define pi_tp pi_val._pi_tp
00174 #define pi_str       pi_val._pi_str
00175 } cdf_property_info_t;
00176 
00177 #define CDF_ROUND(val, by)     (((val) + (by) - 1) & ~((by) - 1))
00178 
00179 /* Variant type definitions */
00180 #define CDF_EMPTY           0x00000000
00181 #define       CDF_NULL             0x00000001
00182 #define CDF_SIGNED16        0x00000002
00183 #define CDF_SIGNED32        0x00000003
00184 #define CDF_FLOAT           0x00000004
00185 #define CDF_DOUBLE          0x00000005
00186 #define CDF_CY                     0x00000006
00187 #define       CDF_DATE             0x00000007
00188 #define CDF_BSTR            0x00000008
00189 #define CDF_DISPATCH        0x00000009
00190 #define CDF_ERROR           0x0000000a
00191 #define CDF_BOOL            0x0000000b
00192 #define CDF_VARIANT         0x0000000c
00193 #define CDF_UNKNOWN         0x0000000d
00194 #define CDF_DECIMAL         0x0000000e
00195 #define CDF_SIGNED8         0x00000010
00196 #define CDF_UNSIGNED8              0x00000011
00197 #define CDF_UNSIGNED16             0x00000012
00198 #define       CDF_UNSIGNED32              0x00000013
00199 #define CDF_SIGNED64        0x00000014
00200 #define CDF_UNSIGNED64             0x00000015
00201 #define CDF_INT                    0x00000016
00202 #define CDF_UINT            0x00000017
00203 #define CDF_VOID            0x00000018
00204 #define CDF_HRESULT         0x00000019
00205 #define CDF_PTR                    0x0000001a
00206 #define CDF_SAFEARRAY              0x0000001b
00207 #define CDF_CARRAY          0x0000001c
00208 #define CDF_USERDEFINED            0x0000001d
00209 #define CDF_LENGTH32_STRING 0x0000001e
00210 #define CDF_LENGTH32_WSTRING       0x0000001f
00211 #define CDF_FILETIME        0x00000040
00212 #define CDF_BLOB            0x00000041
00213 #define CDF_STREAM          0x00000042
00214 #define CDF_STORAGE         0x00000043
00215 #define CDF_STREAMED_OBJECT 0x00000044
00216 #define CDF_STORED_OBJECT   0x00000045
00217 #define CDF_BLOB_OBJECT            0x00000046
00218 #define CDF_CLIPBOARD              0x00000047
00219 #define CDF_CLSID           0x00000048
00220 #define CDF_VECTOR          0x00001000
00221 #define CDF_ARRAY           0x00002000
00222 #define CDF_BYREF           0x00004000
00223 #define CDF_RESERVED        0x00008000
00224 #define CDF_ILLEGAL         0x0000ffff
00225 #define CDF_ILLEGALMASKED   0x00000fff
00226 #define CDF_TYPEMASK        0x00000fff
00227 
00228 #define CDF_PROPERTY_CODE_PAGE                   0x00000001
00229 #define CDF_PROPERTY_TITLE                0x00000002
00230 #define CDF_PROPERTY_SUBJECT                     0x00000003
00231 #define CDF_PROPERTY_AUTHOR               0x00000004
00232 #define CDF_PROPERTY_KEYWORDS                    0x00000005
00233 #define CDF_PROPERTY_COMMENTS                    0x00000006
00234 #define CDF_PROPERTY_TEMPLATE                    0x00000007
00235 #define CDF_PROPERTY_LAST_SAVED_BY        0x00000008
00236 #define CDF_PROPERTY_REVISION_NUMBER             0x00000009
00237 #define CDF_PROPERTY_TOTAL_EDITING_TIME          0x0000000a
00238 #define CDF_PROPERTY_LAST_PRINTED         0X0000000b
00239 #define CDF_PROPERTY_CREATE_TIME          0x0000000c
00240 #define CDF_PROPERTY_LAST_SAVED_TIME             0x0000000d
00241 #define CDF_PROPERTY_NUMBER_OF_PAGES             0x0000000e
00242 #define CDF_PROPERTY_NUMBER_OF_WORDS             0x0000000f
00243 #define CDF_PROPERTY_NUMBER_OF_CHARACTERS 0x00000010
00244 #define CDF_PROPERTY_THUMBNAIL                   0x00000011
00245 #define CDF_PROPERTY_NAME_OF_APPLICATION  0x00000012
00246 #define CDF_PROPERTY_SECURITY                    0x00000013
00247 #define CDF_PROPERTY_LOCALE_ID                   0x80000000
00248 
00249 typedef struct {
00250        int i_fd;
00251        const unsigned char *i_buf;
00252        size_t i_len;
00253 } cdf_info_t;
00254 
00255 struct timeval;
00256 int cdf_timestamp_to_timespec(struct timeval *, cdf_timestamp_t);
00257 int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timeval *);
00258 int cdf_read_header(const cdf_info_t *, cdf_header_t *);
00259 void cdf_swap_header(cdf_header_t *);
00260 void cdf_unpack_header(cdf_header_t *, char *);
00261 void cdf_swap_dir(cdf_directory_t *);
00262 void cdf_unpack_dir(cdf_directory_t *, char *);
00263 void cdf_swap_class(cdf_classid_t *);
00264 ssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t,
00265     const cdf_header_t *, cdf_secid_t);
00266 ssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t,
00267     const cdf_header_t *, cdf_secid_t);
00268 int cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *);
00269 size_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t);
00270 int cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *,
00271     const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *);
00272 int cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *,
00273     const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *);
00274 int cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *,
00275     const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t,
00276     size_t, cdf_stream_t *);
00277 int cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
00278     cdf_dir_t *);
00279 int cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
00280     cdf_sat_t *);
00281 int cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *,
00282     const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *);
00283 int cdf_read_property_info(const cdf_stream_t *, uint32_t,
00284     cdf_property_info_t **, size_t *, size_t *);
00285 int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
00286     const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
00287     const cdf_dir_t *, cdf_stream_t *);
00288 int cdf_unpack_summary_info(const cdf_stream_t *, cdf_summary_info_header_t *,
00289     cdf_property_info_t **, size_t *);
00290 int cdf_print_classid(char *, size_t, const cdf_classid_t *);
00291 int cdf_print_property_name(char *, size_t, uint32_t);
00292 int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t);
00293 uint16_t cdf_tole2(uint16_t);
00294 uint32_t cdf_tole4(uint32_t);
00295 uint64_t cdf_tole8(uint64_t);
00296 
00297 #ifdef CDF_DEBUG
00298 void cdf_dump_header(const cdf_header_t *);
00299 void cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
00300 void cdf_dump(void *, size_t);
00301 void cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *);
00302 void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
00303     const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
00304 void cdf_dump_property_info(const cdf_property_info_t *, size_t);
00305 void cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *);
00306 #endif
00307 
00308 
00309 #endif /* _H_CDF_ */