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/*
27234449Sobrien * Parse Composite Document Files, the format used in Microsoft Office
28234449Sobrien * document files before they switched to zipped XML.
29234449Sobrien * Info from: http://sc.openoffice.org/compdocfileformat.pdf
30234449Sobrien *
31234449Sobrien * N.B. This is the "Composite Document File" format, and not the
32234449Sobrien * "Compound Document Format", nor the "Channel Definition Format".
33191739Sobrien */
34191739Sobrien
35191739Sobrien#ifndef _H_CDF_
36191739Sobrien#define _H_CDF_
37191739Sobrien
38234449Sobrien#ifdef WIN32
39234449Sobrien#include <winsock2.h>
40234449Sobrien#define timespec timeval
41234449Sobrien#define tv_nsec tv_usec
42234449Sobrien#endif
43234449Sobrien#ifdef __DJGPP__
44234449Sobrien#define timespec timeval
45234449Sobrien#define tv_nsec tv_usec
46234449Sobrien#endif
47234449Sobrien
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
54234449Sobrien#define CDF_SECID_END_OF_CHAIN				-2
55234449Sobrien#define CDF_SECID_SECTOR_ALLOCATION_TABLE		-3
56191739Sobrien#define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE	-4
57191739Sobrien
58191739Sobrientypedef struct {
59191739Sobrien	uint64_t	h_magic;
60191739Sobrien#define CDF_MAGIC	0xE11AB1A1E011CFD0LL
61191739Sobrien	uint64_t	h_uuid[2];
62191739Sobrien	uint16_t	h_revision;
63191739Sobrien	uint16_t	h_version;
64191739Sobrien	uint16_t	h_byte_order;
65191739Sobrien	uint16_t	h_sec_size_p2;
66191739Sobrien	uint16_t	h_short_sec_size_p2;
67191739Sobrien	uint8_t		h_unused0[10];
68191739Sobrien	uint32_t	h_num_sectors_in_sat;
69191739Sobrien	uint32_t	h_secid_first_directory;
70191739Sobrien	uint8_t		h_unused1[4];
71191739Sobrien	uint32_t	h_min_size_standard_stream;
72191739Sobrien	cdf_secid_t	h_secid_first_sector_in_short_sat;
73191739Sobrien	uint32_t	h_num_sectors_in_short_sat;
74191739Sobrien	cdf_secid_t	h_secid_first_sector_in_master_sat;
75191739Sobrien	uint32_t	h_num_sectors_in_master_sat;
76191739Sobrien	cdf_secid_t	h_master_sat[436/4];
77191739Sobrien} cdf_header_t;
78191739Sobrien
79234449Sobrien#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))
81234449Sobrien#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
84234449Sobrientypedef int32_t cdf_dirid_t;
85191739Sobrien#define CDF_DIRID_NULL	-1
86191739Sobrien
87234449Sobrientypedef int64_t cdf_timestamp_t;
88191739Sobrien#define CDF_BASE_YEAR	1601
89191739Sobrien#define CDF_TIME_PREC	10000000
90191739Sobrien
91191739Sobrientypedef struct {
92191739Sobrien	uint16_t	d_name[32];
93191739Sobrien	uint16_t	d_namelen;
94191739Sobrien	uint8_t		d_type;
95191739Sobrien#define CDF_DIR_TYPE_EMPTY		0
96234449Sobrien#define CDF_DIR_TYPE_USER_STORAGE	1
97234449Sobrien#define CDF_DIR_TYPE_USER_STREAM	2
98234449Sobrien#define CDF_DIR_TYPE_LOCKBYTES		3
99234449Sobrien#define CDF_DIR_TYPE_PROPERTY		4
100234449Sobrien#define CDF_DIR_TYPE_ROOT_STORAGE	5
101191739Sobrien	uint8_t		d_color;
102191739Sobrien#define CDF_DIR_COLOR_READ	0
103191739Sobrien#define CDF_DIR_COLOR_BLACK	1
104191739Sobrien	cdf_dirid_t	d_left_child;
105191739Sobrien	cdf_dirid_t	d_right_child;
106191739Sobrien	cdf_dirid_t	d_storage;
107191739Sobrien	uint64_t	d_storage_uuid[2];
108191739Sobrien	uint32_t	d_flags;
109234449Sobrien	cdf_timestamp_t d_created;
110234449Sobrien	cdf_timestamp_t d_modified;
111191739Sobrien	cdf_secid_t	d_stream_first_sector;
112191739Sobrien	uint32_t	d_size;
113191739Sobrien	uint32_t	d_unused0;
114191739Sobrien} cdf_directory_t;
115191739Sobrien
116191739Sobrien#define CDF_DIRECTORY_SIZE	128
117191739Sobrien
118191739Sobrientypedef struct {
119191739Sobrien	cdf_secid_t *sat_tab;
120191739Sobrien	size_t sat_len;
121191739Sobrien} cdf_sat_t;
122191739Sobrien
123191739Sobrientypedef struct {
124191739Sobrien	cdf_directory_t *dir_tab;
125191739Sobrien	size_t dir_len;
126191739Sobrien} cdf_dir_t;
127191739Sobrien
128191739Sobrientypedef struct {
129191739Sobrien	void *sst_tab;
130191739Sobrien	size_t sst_len;
131191739Sobrien	size_t sst_dirlen;
132191739Sobrien} cdf_stream_t;
133191739Sobrien
134191739Sobrientypedef struct {
135191739Sobrien	uint32_t	cl_dword;
136191739Sobrien	uint16_t	cl_word[2];
137191739Sobrien	uint8_t		cl_two[2];
138191739Sobrien	uint8_t		cl_six[6];
139191739Sobrien} cdf_classid_t;
140191739Sobrien
141191739Sobrientypedef struct {
142191739Sobrien	uint16_t	si_byte_order;
143191739Sobrien	uint16_t	si_zero;
144191739Sobrien	uint16_t	si_os_version;
145191739Sobrien	uint16_t	si_os;
146191739Sobrien	cdf_classid_t	si_class;
147191739Sobrien	uint32_t	si_count;
148191739Sobrien} cdf_summary_info_header_t;
149191739Sobrien
150191739Sobrien#define CDF_SECTION_DECLARATION_OFFSET 0x1c
151191739Sobrien
152191739Sobrientypedef struct {
153191739Sobrien	cdf_classid_t	sd_class;
154191739Sobrien	uint32_t	sd_offset;
155191739Sobrien} cdf_section_declaration_t;
156191739Sobrien
157191739Sobrientypedef struct {
158191739Sobrien	uint32_t	sh_len;
159191739Sobrien	uint32_t	sh_properties;
160191739Sobrien} cdf_section_header_t;
161191739Sobrien
162191739Sobrientypedef struct {
163191739Sobrien	uint32_t	pi_id;
164191739Sobrien	uint32_t	pi_type;
165191739Sobrien	union {
166191739Sobrien		uint16_t	_pi_u16;
167191739Sobrien		int16_t		_pi_s16;
168191739Sobrien		uint32_t	_pi_u32;
169191739Sobrien		int32_t		_pi_s32;
170191739Sobrien		uint64_t	_pi_u64;
171191739Sobrien		int64_t		_pi_s64;
172234449Sobrien		cdf_timestamp_t _pi_tp;
173234449Sobrien		float		_pi_f;
174234449Sobrien		double		_pi_d;
175191739Sobrien		struct {
176191739Sobrien			uint32_t s_len;
177191739Sobrien			const char *s_buf;
178191739Sobrien		} _pi_str;
179191739Sobrien	} pi_val;
180191739Sobrien#define pi_u64	pi_val._pi_u64
181191739Sobrien#define pi_s64	pi_val._pi_s64
182191739Sobrien#define pi_u32	pi_val._pi_u32
183191739Sobrien#define pi_s32	pi_val._pi_s32
184191739Sobrien#define pi_u16	pi_val._pi_u16
185191739Sobrien#define pi_s16	pi_val._pi_s16
186234449Sobrien#define pi_f	pi_val._pi_f
187234449Sobrien#define pi_d	pi_val._pi_d
188191739Sobrien#define pi_tp	pi_val._pi_tp
189191739Sobrien#define pi_str	pi_val._pi_str
190191739Sobrien} cdf_property_info_t;
191191739Sobrien
192191739Sobrien#define CDF_ROUND(val, by)     (((val) + (by) - 1) & ~((by) - 1))
193191739Sobrien
194191739Sobrien/* Variant type definitions */
195191739Sobrien#define CDF_EMPTY		0x00000000
196234449Sobrien#define CDF_NULL		0x00000001
197191739Sobrien#define CDF_SIGNED16		0x00000002
198191739Sobrien#define CDF_SIGNED32		0x00000003
199191739Sobrien#define CDF_FLOAT		0x00000004
200191739Sobrien#define CDF_DOUBLE		0x00000005
201191739Sobrien#define CDF_CY			0x00000006
202234449Sobrien#define CDF_DATE		0x00000007
203191739Sobrien#define CDF_BSTR		0x00000008
204191739Sobrien#define CDF_DISPATCH		0x00000009
205191739Sobrien#define CDF_ERROR		0x0000000a
206191739Sobrien#define CDF_BOOL		0x0000000b
207191739Sobrien#define CDF_VARIANT		0x0000000c
208191739Sobrien#define CDF_UNKNOWN		0x0000000d
209191739Sobrien#define CDF_DECIMAL		0x0000000e
210191739Sobrien#define CDF_SIGNED8		0x00000010
211191739Sobrien#define CDF_UNSIGNED8		0x00000011
212191739Sobrien#define CDF_UNSIGNED16		0x00000012
213234449Sobrien#define CDF_UNSIGNED32		0x00000013
214191739Sobrien#define CDF_SIGNED64		0x00000014
215191739Sobrien#define CDF_UNSIGNED64		0x00000015
216191739Sobrien#define CDF_INT			0x00000016
217191739Sobrien#define CDF_UINT		0x00000017
218191739Sobrien#define CDF_VOID		0x00000018
219191739Sobrien#define CDF_HRESULT		0x00000019
220191739Sobrien#define CDF_PTR			0x0000001a
221191739Sobrien#define CDF_SAFEARRAY		0x0000001b
222191739Sobrien#define CDF_CARRAY		0x0000001c
223191739Sobrien#define CDF_USERDEFINED		0x0000001d
224191739Sobrien#define CDF_LENGTH32_STRING	0x0000001e
225191739Sobrien#define CDF_LENGTH32_WSTRING	0x0000001f
226191739Sobrien#define CDF_FILETIME		0x00000040
227191739Sobrien#define CDF_BLOB		0x00000041
228191739Sobrien#define CDF_STREAM		0x00000042
229191739Sobrien#define CDF_STORAGE		0x00000043
230191739Sobrien#define CDF_STREAMED_OBJECT	0x00000044
231191739Sobrien#define CDF_STORED_OBJECT	0x00000045
232191739Sobrien#define CDF_BLOB_OBJECT		0x00000046
233191739Sobrien#define CDF_CLIPBOARD		0x00000047
234191739Sobrien#define CDF_CLSID		0x00000048
235191739Sobrien#define CDF_VECTOR		0x00001000
236191739Sobrien#define CDF_ARRAY		0x00002000
237191739Sobrien#define CDF_BYREF		0x00004000
238191739Sobrien#define CDF_RESERVED		0x00008000
239191739Sobrien#define CDF_ILLEGAL		0x0000ffff
240191739Sobrien#define CDF_ILLEGALMASKED	0x00000fff
241191739Sobrien#define CDF_TYPEMASK		0x00000fff
242191739Sobrien
243191739Sobrien#define CDF_PROPERTY_CODE_PAGE			0x00000001
244191739Sobrien#define CDF_PROPERTY_TITLE			0x00000002
245191739Sobrien#define CDF_PROPERTY_SUBJECT			0x00000003
246191739Sobrien#define CDF_PROPERTY_AUTHOR			0x00000004
247191739Sobrien#define CDF_PROPERTY_KEYWORDS			0x00000005
248234449Sobrien#define CDF_PROPERTY_COMMENTS			0x00000006
249191739Sobrien#define CDF_PROPERTY_TEMPLATE			0x00000007
250191739Sobrien#define CDF_PROPERTY_LAST_SAVED_BY		0x00000008
251191739Sobrien#define CDF_PROPERTY_REVISION_NUMBER		0x00000009
252191739Sobrien#define CDF_PROPERTY_TOTAL_EDITING_TIME		0x0000000a
253191739Sobrien#define CDF_PROPERTY_LAST_PRINTED		0X0000000b
254191739Sobrien#define CDF_PROPERTY_CREATE_TIME		0x0000000c
255191739Sobrien#define CDF_PROPERTY_LAST_SAVED_TIME		0x0000000d
256191739Sobrien#define CDF_PROPERTY_NUMBER_OF_PAGES		0x0000000e
257191739Sobrien#define CDF_PROPERTY_NUMBER_OF_WORDS		0x0000000f
258191739Sobrien#define CDF_PROPERTY_NUMBER_OF_CHARACTERS	0x00000010
259191739Sobrien#define CDF_PROPERTY_THUMBNAIL			0x00000011
260191739Sobrien#define CDF_PROPERTY_NAME_OF_APPLICATION	0x00000012
261191739Sobrien#define CDF_PROPERTY_SECURITY			0x00000013
262191739Sobrien#define CDF_PROPERTY_LOCALE_ID			0x80000000
263191739Sobrien
264192350Sdelphijtypedef struct {
265192350Sdelphij	int i_fd;
266192350Sdelphij	const unsigned char *i_buf;
267192350Sdelphij	size_t i_len;
268192350Sdelphij} cdf_info_t;
269192350Sdelphij
270191739Sobrienstruct timespec;
271191739Sobrienint cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
272191739Sobrienint cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
273192350Sdelphijint cdf_read_header(const cdf_info_t *, cdf_header_t *);
274191739Sobrienvoid cdf_swap_header(cdf_header_t *);
275191739Sobrienvoid cdf_unpack_header(cdf_header_t *, char *);
276191739Sobrienvoid cdf_swap_dir(cdf_directory_t *);
277191739Sobrienvoid cdf_unpack_dir(cdf_directory_t *, char *);
278191739Sobrienvoid cdf_swap_class(cdf_classid_t *);
279192350Sdelphijssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t,
280192350Sdelphij    const cdf_header_t *, cdf_secid_t);
281191739Sobrienssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t,
282191739Sobrien    const cdf_header_t *, cdf_secid_t);
283192350Sdelphijint cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *);
284192350Sdelphijsize_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t);
285192350Sdelphijint cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *,
286191739Sobrien    const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *);
287191739Sobrienint cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *,
288191739Sobrien    const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *);
289192350Sdelphijint cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *,
290191739Sobrien    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t,
291191739Sobrien    size_t, cdf_stream_t *);
292192350Sdelphijint cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
293192350Sdelphij    cdf_dir_t *);
294192350Sdelphijint cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
295192350Sdelphij    cdf_sat_t *);
296192350Sdelphijint cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *,
297192350Sdelphij    const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *);
298234449Sobrienint cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t,
299191739Sobrien    cdf_property_info_t **, size_t *, size_t *);
300192350Sdelphijint cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
301192350Sdelphij    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
302192350Sdelphij    const cdf_dir_t *, cdf_stream_t *);
303234449Sobrienint cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *,
304234449Sobrien    cdf_summary_info_header_t *, cdf_property_info_t **, size_t *);
305191739Sobrienint cdf_print_classid(char *, size_t, const cdf_classid_t *);
306191739Sobrienint cdf_print_property_name(char *, size_t, uint32_t);
307191739Sobrienint cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t);
308191739Sobrienuint16_t cdf_tole2(uint16_t);
309191739Sobrienuint32_t cdf_tole4(uint32_t);
310191739Sobrienuint64_t cdf_tole8(uint64_t);
311234449Sobrienchar *cdf_ctime(const time_t *);
312191739Sobrien
313191739Sobrien#ifdef CDF_DEBUG
314191739Sobrienvoid cdf_dump_header(const cdf_header_t *);
315192350Sdelphijvoid cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
316191739Sobrienvoid cdf_dump(void *, size_t);
317191739Sobrienvoid cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *);
318192350Sdelphijvoid cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
319191739Sobrien    const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
320191739Sobrienvoid cdf_dump_property_info(const cdf_property_info_t *, size_t);
321191739Sobrienvoid cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *);
322191739Sobrien#endif
323191739Sobrien
324191739Sobrien
325191739Sobrien#endif /* _H_CDF_ */
326