1191739Sobrien/*-
2191739Sobrien * Copyright (c) 2008 Christos Zoulas
3191739Sobrien * All rights reserved.
4191739Sobrien *
5191739Sobrien * Redistribution and use in source and binary forms, with or without
6191739Sobrien * modification, are permitted provided that the following conditions
7191739Sobrien * are met:
8191739Sobrien * 1. Redistributions of source code must retain the above copyright
9191739Sobrien *    notice, this list of conditions and the following disclaimer.
10191739Sobrien * 2. Redistributions in binary form must reproduce the above copyright
11191739Sobrien *    notice, this list of conditions and the following disclaimer in the
12191739Sobrien *    documentation and/or other materials provided with the distribution.
13191739Sobrien *
14191739Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15191739Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16191739Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17191739Sobrien * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18191739Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19191739Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20191739Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21191739Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22191739Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23191739Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24191739Sobrien * POSSIBILITY OF SUCH DAMAGE.
25191739Sobrien */
26191739Sobrien/*
27226048Sobrien * Parse Composite Document Files, the format used in Microsoft Office
28226048Sobrien * document files before they switched to zipped XML.
29226048Sobrien * Info from: http://sc.openoffice.org/compdocfileformat.pdf
30226048Sobrien *
31226048Sobrien * N.B. This is the "Composite Document File" format, and not the
32226048Sobrien * "Compound Document Format", nor the "Channel Definition Format".
33191739Sobrien */
34191739Sobrien
35191739Sobrien#ifndef _H_CDF_
36191739Sobrien#define _H_CDF_
37191739Sobrien
38226048Sobrien#ifdef WIN32
39226048Sobrien#include <winsock2.h>
40226048Sobrien#define timespec timeval
41226048Sobrien#define tv_nsec tv_usec
42226048Sobrien#endif
43226048Sobrien#ifdef __DJGPP__
44226048Sobrien#define timespec timeval
45226048Sobrien#define tv_nsec tv_usec
46226048Sobrien#endif
47226048Sobrien
48191739Sobrientypedef int32_t cdf_secid_t;
49191739Sobrien
50191739Sobrien#define CDF_LOOP_LIMIT					10000
51191739Sobrien
52191739Sobrien#define CDF_SECID_NULL					0
53191739Sobrien#define CDF_SECID_FREE					-1
54234250Sobrien#define CDF_SECID_END_OF_CHAIN				-2
55234250Sobrien#define CDF_SECID_SECTOR_ALLOCATION_TABLE		-3
56191739Sobrien#define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE	-4
57191739Sobrien
58191739Sobrientypedef struct {
59234250Sobrien	uint64_t	h_magic;
60191739Sobrien#define CDF_MAGIC	0xE11AB1A1E011CFD0LL
61234250Sobrien	uint64_t	h_uuid[2];
62234250Sobrien	uint16_t	h_revision;
63234250Sobrien	uint16_t	h_version;
64234250Sobrien	uint16_t	h_byte_order;
65234250Sobrien	uint16_t	h_sec_size_p2;
66234250Sobrien	uint16_t	h_short_sec_size_p2;
67234250Sobrien	uint8_t		h_unused0[10];
68234250Sobrien	uint32_t	h_num_sectors_in_sat;
69234250Sobrien	uint32_t	h_secid_first_directory;
70234250Sobrien	uint8_t		h_unused1[4];
71234250Sobrien	uint32_t	h_min_size_standard_stream;
72234250Sobrien	cdf_secid_t	h_secid_first_sector_in_short_sat;
73234250Sobrien	uint32_t	h_num_sectors_in_short_sat;
74234250Sobrien	cdf_secid_t	h_secid_first_sector_in_master_sat;
75234250Sobrien	uint32_t	h_num_sectors_in_master_sat;
76234250Sobrien	cdf_secid_t	h_master_sat[436/4];
77191739Sobrien} cdf_header_t;
78191739Sobrien
79234250Sobrien#define CDF_SEC_SIZE(h) ((size_t)(1 << (h)->h_sec_size_p2))
80191739Sobrien#define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h))
81234250Sobrien#define CDF_SHORT_SEC_SIZE(h)	((size_t)(1 << (h)->h_short_sec_size_p2))
82191739Sobrien#define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h))
83191739Sobrien
84234250Sobrientypedef int32_t cdf_dirid_t;
85191739Sobrien#define CDF_DIRID_NULL	-1
86191739Sobrien
87234250Sobrientypedef int64_t cdf_timestamp_t;
88191739Sobrien#define CDF_BASE_YEAR	1601
89191739Sobrien#define CDF_TIME_PREC	10000000
90191739Sobrien
91191739Sobrientypedef struct {
92234250Sobrien	uint16_t	d_name[32];
93234250Sobrien	uint16_t	d_namelen;
94234250Sobrien	uint8_t		d_type;
95191739Sobrien#define CDF_DIR_TYPE_EMPTY		0
96234250Sobrien#define CDF_DIR_TYPE_USER_STORAGE	1
97234250Sobrien#define CDF_DIR_TYPE_USER_STREAM	2
98234250Sobrien#define CDF_DIR_TYPE_LOCKBYTES		3
99234250Sobrien#define CDF_DIR_TYPE_PROPERTY		4
100234250Sobrien#define CDF_DIR_TYPE_ROOT_STORAGE	5
101234250Sobrien	uint8_t		d_color;
102191739Sobrien#define CDF_DIR_COLOR_READ	0
103191739Sobrien#define CDF_DIR_COLOR_BLACK	1
104234250Sobrien	cdf_dirid_t	d_left_child;
105234250Sobrien	cdf_dirid_t	d_right_child;
106234250Sobrien	cdf_dirid_t	d_storage;
107234250Sobrien	uint64_t	d_storage_uuid[2];
108234250Sobrien	uint32_t	d_flags;
109234250Sobrien	cdf_timestamp_t d_created;
110234250Sobrien	cdf_timestamp_t d_modified;
111234250Sobrien	cdf_secid_t	d_stream_first_sector;
112234250Sobrien	uint32_t	d_size;
113234250Sobrien	uint32_t	d_unused0;
114191739Sobrien} cdf_directory_t;
115191739Sobrien
116191739Sobrien#define CDF_DIRECTORY_SIZE	128
117191739Sobrien
118191739Sobrientypedef struct {
119234250Sobrien	cdf_secid_t *sat_tab;
120234250Sobrien	size_t sat_len;
121191739Sobrien} cdf_sat_t;
122191739Sobrien
123191739Sobrientypedef struct {
124234250Sobrien	cdf_directory_t *dir_tab;
125234250Sobrien	size_t dir_len;
126191739Sobrien} cdf_dir_t;
127191739Sobrien
128191739Sobrientypedef struct {
129234250Sobrien	void *sst_tab;
130328875Seadler	size_t sst_len;		/* Number of sectors */
131328875Seadler	size_t sst_dirlen;	/* Directory sector size */
132328875Seadler	size_t sst_ss;		/* Sector size */
133191739Sobrien} cdf_stream_t;
134191739Sobrien
135191739Sobrientypedef struct {
136234250Sobrien	uint32_t	cl_dword;
137234250Sobrien	uint16_t	cl_word[2];
138234250Sobrien	uint8_t		cl_two[2];
139234250Sobrien	uint8_t		cl_six[6];
140191739Sobrien} cdf_classid_t;
141191739Sobrien
142191739Sobrientypedef struct {
143234250Sobrien	uint16_t	si_byte_order;
144234250Sobrien	uint16_t	si_zero;
145234250Sobrien	uint16_t	si_os_version;
146234250Sobrien	uint16_t	si_os;
147234250Sobrien	cdf_classid_t	si_class;
148234250Sobrien	uint32_t	si_count;
149191739Sobrien} cdf_summary_info_header_t;
150191739Sobrien
151191739Sobrien#define CDF_SECTION_DECLARATION_OFFSET 0x1c
152191739Sobrien
153191739Sobrientypedef struct {
154234250Sobrien	cdf_classid_t	sd_class;
155234250Sobrien	uint32_t	sd_offset;
156191739Sobrien} cdf_section_declaration_t;
157191739Sobrien
158191739Sobrientypedef struct {
159234250Sobrien	uint32_t	sh_len;
160234250Sobrien	uint32_t	sh_properties;
161191739Sobrien} cdf_section_header_t;
162191739Sobrien
163191739Sobrientypedef struct {
164234250Sobrien	uint32_t	pi_id;
165234250Sobrien	uint32_t	pi_type;
166234250Sobrien	union {
167234250Sobrien		uint16_t	_pi_u16;
168234250Sobrien		int16_t		_pi_s16;
169234250Sobrien		uint32_t	_pi_u32;
170234250Sobrien		int32_t		_pi_s32;
171234250Sobrien		uint64_t	_pi_u64;
172234250Sobrien		int64_t		_pi_s64;
173234250Sobrien		cdf_timestamp_t _pi_tp;
174234250Sobrien		float		_pi_f;
175234250Sobrien		double		_pi_d;
176234250Sobrien		struct {
177234250Sobrien			uint32_t s_len;
178234250Sobrien			const char *s_buf;
179234250Sobrien		} _pi_str;
180234250Sobrien	} pi_val;
181191739Sobrien#define pi_u64	pi_val._pi_u64
182191739Sobrien#define pi_s64	pi_val._pi_s64
183191739Sobrien#define pi_u32	pi_val._pi_u32
184191739Sobrien#define pi_s32	pi_val._pi_s32
185191739Sobrien#define pi_u16	pi_val._pi_u16
186191739Sobrien#define pi_s16	pi_val._pi_s16
187234250Sobrien#define pi_f	pi_val._pi_f
188234250Sobrien#define pi_d	pi_val._pi_d
189191739Sobrien#define pi_tp	pi_val._pi_tp
190191739Sobrien#define pi_str	pi_val._pi_str
191191739Sobrien} cdf_property_info_t;
192191739Sobrien
193191739Sobrien#define CDF_ROUND(val, by)     (((val) + (by) - 1) & ~((by) - 1))
194191739Sobrien
195191739Sobrien/* Variant type definitions */
196191739Sobrien#define CDF_EMPTY		0x00000000
197234250Sobrien#define CDF_NULL		0x00000001
198191739Sobrien#define CDF_SIGNED16		0x00000002
199191739Sobrien#define CDF_SIGNED32		0x00000003
200191739Sobrien#define CDF_FLOAT		0x00000004
201191739Sobrien#define CDF_DOUBLE		0x00000005
202191739Sobrien#define CDF_CY			0x00000006
203234250Sobrien#define CDF_DATE		0x00000007
204191739Sobrien#define CDF_BSTR		0x00000008
205191739Sobrien#define CDF_DISPATCH		0x00000009
206191739Sobrien#define CDF_ERROR		0x0000000a
207191739Sobrien#define CDF_BOOL		0x0000000b
208191739Sobrien#define CDF_VARIANT		0x0000000c
209191739Sobrien#define CDF_UNKNOWN		0x0000000d
210191739Sobrien#define CDF_DECIMAL		0x0000000e
211191739Sobrien#define CDF_SIGNED8		0x00000010
212191739Sobrien#define CDF_UNSIGNED8		0x00000011
213191739Sobrien#define CDF_UNSIGNED16		0x00000012
214234250Sobrien#define CDF_UNSIGNED32		0x00000013
215191739Sobrien#define CDF_SIGNED64		0x00000014
216191739Sobrien#define CDF_UNSIGNED64		0x00000015
217191739Sobrien#define CDF_INT			0x00000016
218191739Sobrien#define CDF_UINT		0x00000017
219191739Sobrien#define CDF_VOID		0x00000018
220191739Sobrien#define CDF_HRESULT		0x00000019
221191739Sobrien#define CDF_PTR			0x0000001a
222191739Sobrien#define CDF_SAFEARRAY		0x0000001b
223191739Sobrien#define CDF_CARRAY		0x0000001c
224191739Sobrien#define CDF_USERDEFINED		0x0000001d
225191739Sobrien#define CDF_LENGTH32_STRING	0x0000001e
226191739Sobrien#define CDF_LENGTH32_WSTRING	0x0000001f
227191739Sobrien#define CDF_FILETIME		0x00000040
228191739Sobrien#define CDF_BLOB		0x00000041
229191739Sobrien#define CDF_STREAM		0x00000042
230191739Sobrien#define CDF_STORAGE		0x00000043
231191739Sobrien#define CDF_STREAMED_OBJECT	0x00000044
232191739Sobrien#define CDF_STORED_OBJECT	0x00000045
233191739Sobrien#define CDF_BLOB_OBJECT		0x00000046
234191739Sobrien#define CDF_CLIPBOARD		0x00000047
235191739Sobrien#define CDF_CLSID		0x00000048
236191739Sobrien#define CDF_VECTOR		0x00001000
237191739Sobrien#define CDF_ARRAY		0x00002000
238191739Sobrien#define CDF_BYREF		0x00004000
239191739Sobrien#define CDF_RESERVED		0x00008000
240191739Sobrien#define CDF_ILLEGAL		0x0000ffff
241191739Sobrien#define CDF_ILLEGALMASKED	0x00000fff
242191739Sobrien#define CDF_TYPEMASK		0x00000fff
243191739Sobrien
244191739Sobrien#define CDF_PROPERTY_CODE_PAGE			0x00000001
245191739Sobrien#define CDF_PROPERTY_TITLE			0x00000002
246191739Sobrien#define CDF_PROPERTY_SUBJECT			0x00000003
247191739Sobrien#define CDF_PROPERTY_AUTHOR			0x00000004
248191739Sobrien#define CDF_PROPERTY_KEYWORDS			0x00000005
249234250Sobrien#define CDF_PROPERTY_COMMENTS			0x00000006
250191739Sobrien#define CDF_PROPERTY_TEMPLATE			0x00000007
251191739Sobrien#define CDF_PROPERTY_LAST_SAVED_BY		0x00000008
252191739Sobrien#define CDF_PROPERTY_REVISION_NUMBER		0x00000009
253191739Sobrien#define CDF_PROPERTY_TOTAL_EDITING_TIME		0x0000000a
254191739Sobrien#define CDF_PROPERTY_LAST_PRINTED		0X0000000b
255191739Sobrien#define CDF_PROPERTY_CREATE_TIME		0x0000000c
256191739Sobrien#define CDF_PROPERTY_LAST_SAVED_TIME		0x0000000d
257191739Sobrien#define CDF_PROPERTY_NUMBER_OF_PAGES		0x0000000e
258191739Sobrien#define CDF_PROPERTY_NUMBER_OF_WORDS		0x0000000f
259191739Sobrien#define CDF_PROPERTY_NUMBER_OF_CHARACTERS	0x00000010
260191739Sobrien#define CDF_PROPERTY_THUMBNAIL			0x00000011
261191739Sobrien#define CDF_PROPERTY_NAME_OF_APPLICATION	0x00000012
262191739Sobrien#define CDF_PROPERTY_SECURITY			0x00000013
263191739Sobrien#define CDF_PROPERTY_LOCALE_ID			0x80000000
264191739Sobrien
265192348Sdelphijtypedef struct {
266234250Sobrien	int i_fd;
267234250Sobrien	const unsigned char *i_buf;
268234250Sobrien	size_t i_len;
269192348Sdelphij} cdf_info_t;
270192348Sdelphij
271276415Sdelphij
272276415Sdelphijtypedef struct {
273276415Sdelphij	uint16_t ce_namlen;
274276415Sdelphij	uint32_t ce_num;
275276415Sdelphij	uint64_t ce_timestamp;
276276415Sdelphij	uint16_t ce_name[256];
277276415Sdelphij} cdf_catalog_entry_t;
278276415Sdelphij
279276415Sdelphijtypedef struct {
280276415Sdelphij	size_t cat_num;
281300899Sdelphij	cdf_catalog_entry_t cat_e[1];
282276415Sdelphij} cdf_catalog_t;
283276415Sdelphij
284191739Sobrienstruct timespec;
285191739Sobrienint cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
286191739Sobrienint cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
287192348Sdelphijint cdf_read_header(const cdf_info_t *, cdf_header_t *);
288191739Sobrienvoid cdf_swap_header(cdf_header_t *);
289191739Sobrienvoid cdf_unpack_header(cdf_header_t *, char *);
290191739Sobrienvoid cdf_swap_dir(cdf_directory_t *);
291191739Sobrienvoid cdf_unpack_dir(cdf_directory_t *, char *);
292191739Sobrienvoid cdf_swap_class(cdf_classid_t *);
293192348Sdelphijssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t,
294192348Sdelphij    const cdf_header_t *, cdf_secid_t);
295191739Sobrienssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t,
296191739Sobrien    const cdf_header_t *, cdf_secid_t);
297192348Sdelphijint cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *);
298192348Sdelphijsize_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t);
299192348Sdelphijint cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *,
300191739Sobrien    const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *);
301191739Sobrienint cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *,
302191739Sobrien    const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *);
303192348Sdelphijint cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *,
304191739Sobrien    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t,
305191739Sobrien    size_t, cdf_stream_t *);
306192348Sdelphijint cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
307192348Sdelphij    cdf_dir_t *);
308192348Sdelphijint cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
309192348Sdelphij    cdf_sat_t *);
310192348Sdelphijint cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *,
311267843Sdelphij    const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *,
312267843Sdelphij    const cdf_directory_t **);
313226048Sobrienint cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t,
314191739Sobrien    cdf_property_info_t **, size_t *, size_t *);
315267843Sdelphijint cdf_read_user_stream(const cdf_info_t *, const cdf_header_t *,
316267843Sdelphij    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
317267843Sdelphij    const cdf_dir_t *, const char *, cdf_stream_t *);
318284778Sdelphijint cdf_find_stream(const cdf_dir_t *, const char *, int);
319309848Sdelphijint cdf_zero_stream(cdf_stream_t *);
320309848Sdelphijint cdf_read_doc_summary_info(const cdf_info_t *, const cdf_header_t *,
321309848Sdelphij    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
322309848Sdelphij    const cdf_dir_t *, cdf_stream_t *);
323192348Sdelphijint cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
324192348Sdelphij    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
325192348Sdelphij    const cdf_dir_t *, cdf_stream_t *);
326226048Sobrienint cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *,
327226048Sobrien    cdf_summary_info_header_t *, cdf_property_info_t **, size_t *);
328276415Sdelphijint cdf_unpack_catalog(const cdf_header_t *, const cdf_stream_t *,
329276415Sdelphij    cdf_catalog_t **);
330191739Sobrienint cdf_print_classid(char *, size_t, const cdf_classid_t *);
331191739Sobrienint cdf_print_property_name(char *, size_t, uint32_t);
332191739Sobrienint cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t);
333191739Sobrienuint16_t cdf_tole2(uint16_t);
334191739Sobrienuint32_t cdf_tole4(uint32_t);
335191739Sobrienuint64_t cdf_tole8(uint64_t);
336267843Sdelphijchar *cdf_ctime(const time_t *, char *);
337276415Sdelphijchar *cdf_u16tos8(char *, size_t, const uint16_t *);
338191739Sobrien
339191739Sobrien#ifdef CDF_DEBUG
340191739Sobrienvoid cdf_dump_header(const cdf_header_t *);
341192348Sdelphijvoid cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
342284778Sdelphijvoid cdf_dump(const void *, size_t);
343309848Sdelphijvoid cdf_dump_stream(const cdf_stream_t *);
344192348Sdelphijvoid cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
345191739Sobrien    const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
346191739Sobrienvoid cdf_dump_property_info(const cdf_property_info_t *, size_t);
347191739Sobrienvoid cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *);
348276415Sdelphijvoid cdf_dump_catalog(const cdf_header_t *, const cdf_stream_t *);
349191739Sobrien#endif
350191739Sobrien
351191739Sobrien
352191739Sobrien#endif /* _H_CDF_ */
353