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
51354939Sdelphij#define CDF_ELEMENT_LIMIT				100000
52191739Sobrien
53191739Sobrien#define CDF_SECID_NULL					0
54191739Sobrien#define CDF_SECID_FREE					-1
55234250Sobrien#define CDF_SECID_END_OF_CHAIN				-2
56234250Sobrien#define CDF_SECID_SECTOR_ALLOCATION_TABLE		-3
57191739Sobrien#define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE	-4
58191739Sobrien
59191739Sobrientypedef struct {
60234250Sobrien	uint64_t	h_magic;
61191739Sobrien#define CDF_MAGIC	0xE11AB1A1E011CFD0LL
62234250Sobrien	uint64_t	h_uuid[2];
63234250Sobrien	uint16_t	h_revision;
64234250Sobrien	uint16_t	h_version;
65234250Sobrien	uint16_t	h_byte_order;
66234250Sobrien	uint16_t	h_sec_size_p2;
67234250Sobrien	uint16_t	h_short_sec_size_p2;
68234250Sobrien	uint8_t		h_unused0[10];
69234250Sobrien	uint32_t	h_num_sectors_in_sat;
70234250Sobrien	uint32_t	h_secid_first_directory;
71234250Sobrien	uint8_t		h_unused1[4];
72234250Sobrien	uint32_t	h_min_size_standard_stream;
73234250Sobrien	cdf_secid_t	h_secid_first_sector_in_short_sat;
74234250Sobrien	uint32_t	h_num_sectors_in_short_sat;
75234250Sobrien	cdf_secid_t	h_secid_first_sector_in_master_sat;
76234250Sobrien	uint32_t	h_num_sectors_in_master_sat;
77234250Sobrien	cdf_secid_t	h_master_sat[436/4];
78191739Sobrien} cdf_header_t;
79191739Sobrien
80354939Sdelphij#define CDF_SEC_SIZE(h) CAST(size_t, 1 << (h)->h_sec_size_p2)
81191739Sobrien#define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h))
82354939Sdelphij#define CDF_SHORT_SEC_SIZE(h)	CAST(size_t, 1 << (h)->h_short_sec_size_p2)
83191739Sobrien#define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h))
84191739Sobrien
85234250Sobrientypedef int32_t cdf_dirid_t;
86191739Sobrien#define CDF_DIRID_NULL	-1
87191739Sobrien
88234250Sobrientypedef int64_t cdf_timestamp_t;
89191739Sobrien#define CDF_BASE_YEAR	1601
90191739Sobrien#define CDF_TIME_PREC	10000000
91191739Sobrien
92191739Sobrientypedef struct {
93234250Sobrien	uint16_t	d_name[32];
94234250Sobrien	uint16_t	d_namelen;
95234250Sobrien	uint8_t		d_type;
96191739Sobrien#define CDF_DIR_TYPE_EMPTY		0
97234250Sobrien#define CDF_DIR_TYPE_USER_STORAGE	1
98234250Sobrien#define CDF_DIR_TYPE_USER_STREAM	2
99234250Sobrien#define CDF_DIR_TYPE_LOCKBYTES		3
100234250Sobrien#define CDF_DIR_TYPE_PROPERTY		4
101234250Sobrien#define CDF_DIR_TYPE_ROOT_STORAGE	5
102234250Sobrien	uint8_t		d_color;
103191739Sobrien#define CDF_DIR_COLOR_READ	0
104191739Sobrien#define CDF_DIR_COLOR_BLACK	1
105234250Sobrien	cdf_dirid_t	d_left_child;
106234250Sobrien	cdf_dirid_t	d_right_child;
107234250Sobrien	cdf_dirid_t	d_storage;
108234250Sobrien	uint64_t	d_storage_uuid[2];
109234250Sobrien	uint32_t	d_flags;
110234250Sobrien	cdf_timestamp_t d_created;
111234250Sobrien	cdf_timestamp_t d_modified;
112234250Sobrien	cdf_secid_t	d_stream_first_sector;
113234250Sobrien	uint32_t	d_size;
114234250Sobrien	uint32_t	d_unused0;
115191739Sobrien} cdf_directory_t;
116191739Sobrien
117191739Sobrien#define CDF_DIRECTORY_SIZE	128
118191739Sobrien
119191739Sobrientypedef struct {
120234250Sobrien	cdf_secid_t *sat_tab;
121234250Sobrien	size_t sat_len;
122191739Sobrien} cdf_sat_t;
123191739Sobrien
124191739Sobrientypedef struct {
125234250Sobrien	cdf_directory_t *dir_tab;
126234250Sobrien	size_t dir_len;
127191739Sobrien} cdf_dir_t;
128191739Sobrien
129191739Sobrientypedef struct {
130234250Sobrien	void *sst_tab;
131328874Seadler	size_t sst_len;		/* Number of sectors */
132328874Seadler	size_t sst_dirlen;	/* Directory sector size */
133328874Seadler	size_t sst_ss;		/* Sector size */
134191739Sobrien} cdf_stream_t;
135191739Sobrien
136191739Sobrientypedef struct {
137234250Sobrien	uint32_t	cl_dword;
138234250Sobrien	uint16_t	cl_word[2];
139234250Sobrien	uint8_t		cl_two[2];
140234250Sobrien	uint8_t		cl_six[6];
141191739Sobrien} cdf_classid_t;
142191739Sobrien
143191739Sobrientypedef struct {
144234250Sobrien	uint16_t	si_byte_order;
145234250Sobrien	uint16_t	si_zero;
146234250Sobrien	uint16_t	si_os_version;
147234250Sobrien	uint16_t	si_os;
148234250Sobrien	cdf_classid_t	si_class;
149234250Sobrien	uint32_t	si_count;
150191739Sobrien} cdf_summary_info_header_t;
151191739Sobrien
152191739Sobrien#define CDF_SECTION_DECLARATION_OFFSET 0x1c
153191739Sobrien
154191739Sobrientypedef struct {
155234250Sobrien	cdf_classid_t	sd_class;
156234250Sobrien	uint32_t	sd_offset;
157191739Sobrien} cdf_section_declaration_t;
158191739Sobrien
159191739Sobrientypedef struct {
160234250Sobrien	uint32_t	sh_len;
161234250Sobrien	uint32_t	sh_properties;
162191739Sobrien} cdf_section_header_t;
163191739Sobrien
164191739Sobrientypedef struct {
165234250Sobrien	uint32_t	pi_id;
166234250Sobrien	uint32_t	pi_type;
167234250Sobrien	union {
168234250Sobrien		uint16_t	_pi_u16;
169234250Sobrien		int16_t		_pi_s16;
170234250Sobrien		uint32_t	_pi_u32;
171234250Sobrien		int32_t		_pi_s32;
172234250Sobrien		uint64_t	_pi_u64;
173234250Sobrien		int64_t		_pi_s64;
174234250Sobrien		cdf_timestamp_t _pi_tp;
175234250Sobrien		float		_pi_f;
176234250Sobrien		double		_pi_d;
177234250Sobrien		struct {
178234250Sobrien			uint32_t s_len;
179234250Sobrien			const char *s_buf;
180234250Sobrien		} _pi_str;
181234250Sobrien	} pi_val;
182191739Sobrien#define pi_u64	pi_val._pi_u64
183191739Sobrien#define pi_s64	pi_val._pi_s64
184191739Sobrien#define pi_u32	pi_val._pi_u32
185191739Sobrien#define pi_s32	pi_val._pi_s32
186191739Sobrien#define pi_u16	pi_val._pi_u16
187191739Sobrien#define pi_s16	pi_val._pi_s16
188234250Sobrien#define pi_f	pi_val._pi_f
189234250Sobrien#define pi_d	pi_val._pi_d
190191739Sobrien#define pi_tp	pi_val._pi_tp
191191739Sobrien#define pi_str	pi_val._pi_str
192191739Sobrien} cdf_property_info_t;
193191739Sobrien
194191739Sobrien#define CDF_ROUND(val, by)     (((val) + (by) - 1) & ~((by) - 1))
195191739Sobrien
196191739Sobrien/* Variant type definitions */
197191739Sobrien#define CDF_EMPTY		0x00000000
198234250Sobrien#define CDF_NULL		0x00000001
199191739Sobrien#define CDF_SIGNED16		0x00000002
200191739Sobrien#define CDF_SIGNED32		0x00000003
201191739Sobrien#define CDF_FLOAT		0x00000004
202191739Sobrien#define CDF_DOUBLE		0x00000005
203191739Sobrien#define CDF_CY			0x00000006
204234250Sobrien#define CDF_DATE		0x00000007
205191739Sobrien#define CDF_BSTR		0x00000008
206191739Sobrien#define CDF_DISPATCH		0x00000009
207191739Sobrien#define CDF_ERROR		0x0000000a
208191739Sobrien#define CDF_BOOL		0x0000000b
209191739Sobrien#define CDF_VARIANT		0x0000000c
210191739Sobrien#define CDF_UNKNOWN		0x0000000d
211191739Sobrien#define CDF_DECIMAL		0x0000000e
212191739Sobrien#define CDF_SIGNED8		0x00000010
213191739Sobrien#define CDF_UNSIGNED8		0x00000011
214191739Sobrien#define CDF_UNSIGNED16		0x00000012
215234250Sobrien#define CDF_UNSIGNED32		0x00000013
216191739Sobrien#define CDF_SIGNED64		0x00000014
217191739Sobrien#define CDF_UNSIGNED64		0x00000015
218191739Sobrien#define CDF_INT			0x00000016
219191739Sobrien#define CDF_UINT		0x00000017
220191739Sobrien#define CDF_VOID		0x00000018
221191739Sobrien#define CDF_HRESULT		0x00000019
222191739Sobrien#define CDF_PTR			0x0000001a
223191739Sobrien#define CDF_SAFEARRAY		0x0000001b
224191739Sobrien#define CDF_CARRAY		0x0000001c
225191739Sobrien#define CDF_USERDEFINED		0x0000001d
226191739Sobrien#define CDF_LENGTH32_STRING	0x0000001e
227191739Sobrien#define CDF_LENGTH32_WSTRING	0x0000001f
228191739Sobrien#define CDF_FILETIME		0x00000040
229191739Sobrien#define CDF_BLOB		0x00000041
230191739Sobrien#define CDF_STREAM		0x00000042
231191739Sobrien#define CDF_STORAGE		0x00000043
232191739Sobrien#define CDF_STREAMED_OBJECT	0x00000044
233191739Sobrien#define CDF_STORED_OBJECT	0x00000045
234191739Sobrien#define CDF_BLOB_OBJECT		0x00000046
235191739Sobrien#define CDF_CLIPBOARD		0x00000047
236191739Sobrien#define CDF_CLSID		0x00000048
237191739Sobrien#define CDF_VECTOR		0x00001000
238191739Sobrien#define CDF_ARRAY		0x00002000
239191739Sobrien#define CDF_BYREF		0x00004000
240191739Sobrien#define CDF_RESERVED		0x00008000
241191739Sobrien#define CDF_ILLEGAL		0x0000ffff
242191739Sobrien#define CDF_ILLEGALMASKED	0x00000fff
243191739Sobrien#define CDF_TYPEMASK		0x00000fff
244191739Sobrien
245191739Sobrien#define CDF_PROPERTY_CODE_PAGE			0x00000001
246191739Sobrien#define CDF_PROPERTY_TITLE			0x00000002
247191739Sobrien#define CDF_PROPERTY_SUBJECT			0x00000003
248191739Sobrien#define CDF_PROPERTY_AUTHOR			0x00000004
249191739Sobrien#define CDF_PROPERTY_KEYWORDS			0x00000005
250234250Sobrien#define CDF_PROPERTY_COMMENTS			0x00000006
251191739Sobrien#define CDF_PROPERTY_TEMPLATE			0x00000007
252191739Sobrien#define CDF_PROPERTY_LAST_SAVED_BY		0x00000008
253191739Sobrien#define CDF_PROPERTY_REVISION_NUMBER		0x00000009
254191739Sobrien#define CDF_PROPERTY_TOTAL_EDITING_TIME		0x0000000a
255191739Sobrien#define CDF_PROPERTY_LAST_PRINTED		0X0000000b
256191739Sobrien#define CDF_PROPERTY_CREATE_TIME		0x0000000c
257191739Sobrien#define CDF_PROPERTY_LAST_SAVED_TIME		0x0000000d
258191739Sobrien#define CDF_PROPERTY_NUMBER_OF_PAGES		0x0000000e
259191739Sobrien#define CDF_PROPERTY_NUMBER_OF_WORDS		0x0000000f
260191739Sobrien#define CDF_PROPERTY_NUMBER_OF_CHARACTERS	0x00000010
261191739Sobrien#define CDF_PROPERTY_THUMBNAIL			0x00000011
262191739Sobrien#define CDF_PROPERTY_NAME_OF_APPLICATION	0x00000012
263191739Sobrien#define CDF_PROPERTY_SECURITY			0x00000013
264191739Sobrien#define CDF_PROPERTY_LOCALE_ID			0x80000000
265191739Sobrien
266192348Sdelphijtypedef struct {
267234250Sobrien	int i_fd;
268234250Sobrien	const unsigned char *i_buf;
269234250Sobrien	size_t i_len;
270192348Sdelphij} cdf_info_t;
271192348Sdelphij
272275698Sdelphij
273275698Sdelphijtypedef struct {
274275698Sdelphij	uint16_t ce_namlen;
275275698Sdelphij	uint32_t ce_num;
276354939Sdelphij	uint64_t ce_timestamp;
277275698Sdelphij	uint16_t ce_name[256];
278275698Sdelphij} cdf_catalog_entry_t;
279275698Sdelphij
280275698Sdelphijtypedef struct {
281275698Sdelphij	size_t cat_num;
282299736Sdelphij	cdf_catalog_entry_t cat_e[1];
283275698Sdelphij} cdf_catalog_t;
284275698Sdelphij
285191739Sobrienstruct timespec;
286191739Sobrienint cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
287191739Sobrienint cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
288192348Sdelphijint cdf_read_header(const cdf_info_t *, cdf_header_t *);
289191739Sobrienvoid cdf_swap_header(cdf_header_t *);
290191739Sobrienvoid cdf_unpack_header(cdf_header_t *, char *);
291191739Sobrienvoid cdf_swap_dir(cdf_directory_t *);
292191739Sobrienvoid cdf_unpack_dir(cdf_directory_t *, char *);
293191739Sobrienvoid cdf_swap_class(cdf_classid_t *);
294192348Sdelphijssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t,
295192348Sdelphij    const cdf_header_t *, cdf_secid_t);
296191739Sobrienssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t,
297191739Sobrien    const cdf_header_t *, cdf_secid_t);
298192348Sdelphijint cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *);
299192348Sdelphijsize_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t);
300192348Sdelphijint cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *,
301191739Sobrien    const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *);
302191739Sobrienint cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *,
303191739Sobrien    const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *);
304192348Sdelphijint cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *,
305191739Sobrien    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t,
306191739Sobrien    size_t, cdf_stream_t *);
307192348Sdelphijint cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
308192348Sdelphij    cdf_dir_t *);
309192348Sdelphijint cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
310192348Sdelphij    cdf_sat_t *);
311192348Sdelphijint cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *,
312267843Sdelphij    const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *,
313267843Sdelphij    const cdf_directory_t **);
314226048Sobrienint cdf_read_property_info(const cdf_stream_t *, const cdf_header_t *, uint32_t,
315191739Sobrien    cdf_property_info_t **, size_t *, size_t *);
316267843Sdelphijint cdf_read_user_stream(const cdf_info_t *, const cdf_header_t *,
317267843Sdelphij    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
318267843Sdelphij    const cdf_dir_t *, const char *, cdf_stream_t *);
319284237Sdelphijint cdf_find_stream(const cdf_dir_t *, const char *, int);
320309847Sdelphijint cdf_zero_stream(cdf_stream_t *);
321309847Sdelphijint cdf_read_doc_summary_info(const cdf_info_t *, const cdf_header_t *,
322309847Sdelphij    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
323309847Sdelphij    const cdf_dir_t *, cdf_stream_t *);
324192348Sdelphijint cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
325192348Sdelphij    const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
326192348Sdelphij    const cdf_dir_t *, cdf_stream_t *);
327226048Sobrienint cdf_unpack_summary_info(const cdf_stream_t *, const cdf_header_t *,
328226048Sobrien    cdf_summary_info_header_t *, cdf_property_info_t **, size_t *);
329275698Sdelphijint cdf_unpack_catalog(const cdf_header_t *, const cdf_stream_t *,
330275698Sdelphij    cdf_catalog_t **);
331191739Sobrienint cdf_print_classid(char *, size_t, const cdf_classid_t *);
332191739Sobrienint cdf_print_property_name(char *, size_t, uint32_t);
333191739Sobrienint cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t);
334191739Sobrienuint16_t cdf_tole2(uint16_t);
335191739Sobrienuint32_t cdf_tole4(uint32_t);
336191739Sobrienuint64_t cdf_tole8(uint64_t);
337267843Sdelphijchar *cdf_ctime(const time_t *, char *);
338275698Sdelphijchar *cdf_u16tos8(char *, size_t, const uint16_t *);
339191739Sobrien
340191739Sobrien#ifdef CDF_DEBUG
341191739Sobrienvoid cdf_dump_header(const cdf_header_t *);
342192348Sdelphijvoid cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
343284237Sdelphijvoid cdf_dump(const void *, size_t);
344309847Sdelphijvoid cdf_dump_stream(const cdf_stream_t *);
345192348Sdelphijvoid cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
346191739Sobrien    const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
347191739Sobrienvoid cdf_dump_property_info(const cdf_property_info_t *, size_t);
348191739Sobrienvoid cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *);
349275698Sdelphijvoid cdf_dump_catalog(const cdf_header_t *, const cdf_stream_t *);
350191739Sobrien#endif
351191739Sobrien
352191739Sobrien
353191739Sobrien#endif /* _H_CDF_ */
354