xref: /netbsd/external/bsd/file/dist/src/cdf.h (revision 6550d01e)
1 /*	$NetBSD: cdf.h,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Christos Zoulas
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 /*
29  * Info from: http://sc.openoffice.org/compdocfileformat.pdf
30  */
31 
32 #ifndef _H_CDF_
33 #define _H_CDF_
34 
35 typedef int32_t cdf_secid_t;
36 
37 #define CDF_LOOP_LIMIT					10000
38 
39 #define CDF_SECID_NULL					0
40 #define CDF_SECID_FREE					-1
41 #define	CDF_SECID_END_OF_CHAIN				-2
42 #define	CDF_SECID_SECTOR_ALLOCATION_TABLE		-3
43 #define CDF_SECID_MASTER_SECTOR_ALLOCATION_TABLE	-4
44 
45 typedef struct {
46 	uint64_t	h_magic;
47 #define CDF_MAGIC	0xE11AB1A1E011CFD0LL
48 	uint64_t	h_uuid[2];
49 	uint16_t	h_revision;
50 	uint16_t	h_version;
51 	uint16_t	h_byte_order;
52 	uint16_t	h_sec_size_p2;
53 	uint16_t	h_short_sec_size_p2;
54 	uint8_t		h_unused0[10];
55 	uint32_t	h_num_sectors_in_sat;
56 	uint32_t	h_secid_first_directory;
57 	uint8_t		h_unused1[4];
58 	uint32_t	h_min_size_standard_stream;
59 	cdf_secid_t	h_secid_first_sector_in_short_sat;
60 	uint32_t	h_num_sectors_in_short_sat;
61 	cdf_secid_t	h_secid_first_sector_in_master_sat;
62 	uint32_t	h_num_sectors_in_master_sat;
63 	cdf_secid_t	h_master_sat[436/4];
64 } cdf_header_t;
65 
66 #define CDF_SEC_SIZE(h)	(1 << (h)->h_sec_size_p2)
67 #define CDF_SEC_POS(h, secid) (CDF_SEC_SIZE(h) + (secid) * CDF_SEC_SIZE(h))
68 #define CDF_SHORT_SEC_SIZE(h)	(1 << (h)->h_short_sec_size_p2)
69 #define CDF_SHORT_SEC_POS(h, secid) ((secid) * CDF_SHORT_SEC_SIZE(h))
70 
71 typedef int32_t	cdf_dirid_t;
72 #define CDF_DIRID_NULL	-1
73 
74 typedef int64_t	cdf_timestamp_t;
75 #define CDF_BASE_YEAR	1601
76 #define CDF_TIME_PREC	10000000
77 
78 typedef struct {
79 	uint16_t	d_name[32];
80 	uint16_t	d_namelen;
81 	uint8_t		d_type;
82 #define CDF_DIR_TYPE_EMPTY		0
83 #define	CDF_DIR_TYPE_USER_STORAGE	1
84 #define	CDF_DIR_TYPE_USER_STREAM	2
85 #define	CDF_DIR_TYPE_LOCKBYTES		3
86 #define	CDF_DIR_TYPE_PROPERTY		4
87 #define	CDF_DIR_TYPE_ROOT_STORAGE	5
88 	uint8_t		d_color;
89 #define CDF_DIR_COLOR_READ	0
90 #define CDF_DIR_COLOR_BLACK	1
91 	cdf_dirid_t	d_left_child;
92 	cdf_dirid_t	d_right_child;
93 	cdf_dirid_t	d_storage;
94 	uint64_t	d_storage_uuid[2];
95 	uint32_t	d_flags;
96 	cdf_timestamp_t	d_created;
97 	cdf_timestamp_t	d_modified;
98 	cdf_secid_t	d_stream_first_sector;
99 	uint32_t	d_size;
100 	uint32_t	d_unused0;
101 } cdf_directory_t;
102 
103 #define CDF_DIRECTORY_SIZE	128
104 
105 typedef struct {
106 	cdf_secid_t *sat_tab;
107 	size_t sat_len;
108 } cdf_sat_t;
109 
110 typedef struct {
111 	cdf_directory_t *dir_tab;
112 	size_t dir_len;
113 } cdf_dir_t;
114 
115 typedef struct {
116 	void *sst_tab;
117 	size_t sst_len;
118 	size_t sst_dirlen;
119 } cdf_stream_t;
120 
121 typedef struct {
122 	uint32_t	cl_dword;
123 	uint16_t	cl_word[2];
124 	uint8_t		cl_two[2];
125 	uint8_t		cl_six[6];
126 } cdf_classid_t;
127 
128 typedef struct {
129 	uint16_t	si_byte_order;
130 	uint16_t	si_zero;
131 	uint16_t	si_os_version;
132 	uint16_t	si_os;
133 	cdf_classid_t	si_class;
134 	uint32_t	si_count;
135 } cdf_summary_info_header_t;
136 
137 #define CDF_SECTION_DECLARATION_OFFSET 0x1c
138 
139 typedef struct {
140 	cdf_classid_t	sd_class;
141 	uint32_t	sd_offset;
142 } cdf_section_declaration_t;
143 
144 typedef struct {
145 	uint32_t	sh_len;
146 	uint32_t	sh_properties;
147 } cdf_section_header_t;
148 
149 typedef struct {
150 	uint32_t	pi_id;
151 	uint32_t	pi_type;
152 	union {
153 		uint16_t	_pi_u16;
154 		int16_t		_pi_s16;
155 		uint32_t	_pi_u32;
156 		int32_t		_pi_s32;
157 		uint64_t	_pi_u64;
158 		int64_t		_pi_s64;
159 		cdf_timestamp_t	_pi_tp;
160 		struct {
161 			uint32_t s_len;
162 			const char *s_buf;
163 		} _pi_str;
164 	} pi_val;
165 #define pi_u64	pi_val._pi_u64
166 #define pi_s64	pi_val._pi_s64
167 #define pi_u32	pi_val._pi_u32
168 #define pi_s32	pi_val._pi_s32
169 #define pi_u16	pi_val._pi_u16
170 #define pi_s16	pi_val._pi_s16
171 #define pi_tp	pi_val._pi_tp
172 #define pi_str	pi_val._pi_str
173 } cdf_property_info_t;
174 
175 #define CDF_ROUND(val, by)     (((val) + (by) - 1) & ~((by) - 1))
176 
177 /* Variant type definitions */
178 #define CDF_EMPTY		0x00000000
179 #define	CDF_NULL		0x00000001
180 #define CDF_SIGNED16		0x00000002
181 #define CDF_SIGNED32		0x00000003
182 #define CDF_FLOAT		0x00000004
183 #define CDF_DOUBLE		0x00000005
184 #define CDF_CY			0x00000006
185 #define	CDF_DATE		0x00000007
186 #define CDF_BSTR		0x00000008
187 #define CDF_DISPATCH		0x00000009
188 #define CDF_ERROR		0x0000000a
189 #define CDF_BOOL		0x0000000b
190 #define CDF_VARIANT		0x0000000c
191 #define CDF_UNKNOWN		0x0000000d
192 #define CDF_DECIMAL		0x0000000e
193 #define CDF_SIGNED8		0x00000010
194 #define CDF_UNSIGNED8		0x00000011
195 #define CDF_UNSIGNED16		0x00000012
196 #define	CDF_UNSIGNED32		0x00000013
197 #define CDF_SIGNED64		0x00000014
198 #define CDF_UNSIGNED64		0x00000015
199 #define CDF_INT			0x00000016
200 #define CDF_UINT		0x00000017
201 #define CDF_VOID		0x00000018
202 #define CDF_HRESULT		0x00000019
203 #define CDF_PTR			0x0000001a
204 #define CDF_SAFEARRAY		0x0000001b
205 #define CDF_CARRAY		0x0000001c
206 #define CDF_USERDEFINED		0x0000001d
207 #define CDF_LENGTH32_STRING	0x0000001e
208 #define CDF_LENGTH32_WSTRING	0x0000001f
209 #define CDF_FILETIME		0x00000040
210 #define CDF_BLOB		0x00000041
211 #define CDF_STREAM		0x00000042
212 #define CDF_STORAGE		0x00000043
213 #define CDF_STREAMED_OBJECT	0x00000044
214 #define CDF_STORED_OBJECT	0x00000045
215 #define CDF_BLOB_OBJECT		0x00000046
216 #define CDF_CLIPBOARD		0x00000047
217 #define CDF_CLSID		0x00000048
218 #define CDF_VECTOR		0x00001000
219 #define CDF_ARRAY		0x00002000
220 #define CDF_BYREF		0x00004000
221 #define CDF_RESERVED		0x00008000
222 #define CDF_ILLEGAL		0x0000ffff
223 #define CDF_ILLEGALMASKED	0x00000fff
224 #define CDF_TYPEMASK		0x00000fff
225 
226 #define CDF_PROPERTY_CODE_PAGE			0x00000001
227 #define CDF_PROPERTY_TITLE			0x00000002
228 #define CDF_PROPERTY_SUBJECT			0x00000003
229 #define CDF_PROPERTY_AUTHOR			0x00000004
230 #define CDF_PROPERTY_KEYWORDS			0x00000005
231 #define CDF_PROPERTY_COMMENTS 			0x00000006
232 #define CDF_PROPERTY_TEMPLATE			0x00000007
233 #define CDF_PROPERTY_LAST_SAVED_BY		0x00000008
234 #define CDF_PROPERTY_REVISION_NUMBER		0x00000009
235 #define CDF_PROPERTY_TOTAL_EDITING_TIME		0x0000000a
236 #define CDF_PROPERTY_LAST_PRINTED		0X0000000b
237 #define CDF_PROPERTY_CREATE_TIME		0x0000000c
238 #define CDF_PROPERTY_LAST_SAVED_TIME		0x0000000d
239 #define CDF_PROPERTY_NUMBER_OF_PAGES		0x0000000e
240 #define CDF_PROPERTY_NUMBER_OF_WORDS		0x0000000f
241 #define CDF_PROPERTY_NUMBER_OF_CHARACTERS	0x00000010
242 #define CDF_PROPERTY_THUMBNAIL			0x00000011
243 #define CDF_PROPERTY_NAME_OF_APPLICATION	0x00000012
244 #define CDF_PROPERTY_SECURITY			0x00000013
245 #define CDF_PROPERTY_LOCALE_ID			0x80000000
246 
247 typedef struct {
248 	int i_fd;
249 	const unsigned char *i_buf;
250 	size_t i_len;
251 } cdf_info_t;
252 
253 struct timespec;
254 int cdf_timestamp_to_timespec(struct timespec *, cdf_timestamp_t);
255 int cdf_timespec_to_timestamp(cdf_timestamp_t *, const struct timespec *);
256 int cdf_read_header(const cdf_info_t *, cdf_header_t *);
257 void cdf_swap_header(cdf_header_t *);
258 void cdf_unpack_header(cdf_header_t *, char *);
259 void cdf_swap_dir(cdf_directory_t *);
260 void cdf_unpack_dir(cdf_directory_t *, char *);
261 void cdf_swap_class(cdf_classid_t *);
262 ssize_t cdf_read_sector(const cdf_info_t *, void *, size_t, size_t,
263     const cdf_header_t *, cdf_secid_t);
264 ssize_t cdf_read_short_sector(const cdf_stream_t *, void *, size_t, size_t,
265     const cdf_header_t *, cdf_secid_t);
266 int cdf_read_sat(const cdf_info_t *, cdf_header_t *, cdf_sat_t *);
267 size_t cdf_count_chain(const cdf_sat_t *, cdf_secid_t, size_t);
268 int cdf_read_long_sector_chain(const cdf_info_t *, const cdf_header_t *,
269     const cdf_sat_t *, cdf_secid_t, size_t, cdf_stream_t *);
270 int cdf_read_short_sector_chain(const cdf_header_t *, const cdf_sat_t *,
271     const cdf_stream_t *, cdf_secid_t, size_t, cdf_stream_t *);
272 int cdf_read_sector_chain(const cdf_info_t *, const cdf_header_t *,
273     const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *, cdf_secid_t,
274     size_t, cdf_stream_t *);
275 int cdf_read_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
276     cdf_dir_t *);
277 int cdf_read_ssat(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
278     cdf_sat_t *);
279 int cdf_read_short_stream(const cdf_info_t *, const cdf_header_t *,
280     const cdf_sat_t *, const cdf_dir_t *, cdf_stream_t *);
281 int cdf_read_property_info(const cdf_stream_t *, uint32_t,
282     cdf_property_info_t **, size_t *, size_t *);
283 int cdf_read_summary_info(const cdf_info_t *, const cdf_header_t *,
284     const cdf_sat_t *, const cdf_sat_t *, const cdf_stream_t *,
285     const cdf_dir_t *, cdf_stream_t *);
286 int cdf_unpack_summary_info(const cdf_stream_t *, cdf_summary_info_header_t *,
287     cdf_property_info_t **, size_t *);
288 int cdf_print_classid(char *, size_t, const cdf_classid_t *);
289 int cdf_print_property_name(char *, size_t, uint32_t);
290 int cdf_print_elapsed_time(char *, size_t, cdf_timestamp_t);
291 uint16_t cdf_tole2(uint16_t);
292 uint32_t cdf_tole4(uint32_t);
293 uint64_t cdf_tole8(uint64_t);
294 
295 #ifdef CDF_DEBUG
296 void cdf_dump_header(const cdf_header_t *);
297 void cdf_dump_sat(const char *, const cdf_sat_t *, size_t);
298 void cdf_dump(void *, size_t);
299 void cdf_dump_stream(const cdf_header_t *, const cdf_stream_t *);
300 void cdf_dump_dir(const cdf_info_t *, const cdf_header_t *, const cdf_sat_t *,
301     const cdf_sat_t *, const cdf_stream_t *, const cdf_dir_t *);
302 void cdf_dump_property_info(const cdf_property_info_t *, size_t);
303 void cdf_dump_summary_info(const cdf_header_t *, const cdf_stream_t *);
304 #endif
305 
306 
307 #endif /* _H_CDF_ */
308