xref: /PHP-7.2/ext/fileinfo/libmagic/cdf.c (revision 46982004)
1 /*-
2  * Copyright (c) 2008 Christos Zoulas
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 /*
27  * Parse Composite Document Files, the format used in Microsoft Office
28  * document files before they switched to zipped XML.
29  * Info from: http://sc.openoffice.org/compdocfileformat.pdf
30  *
31  * N.B. This is the "Composite Document File" format, and not the
32  * "Compound Document Format", nor the "Channel Definition Format".
33  */
34 
35 #include "file.h"
36 
37 #ifndef lint
38 FILE_RCSID("@(#)$File: cdf.c,v 1.106 2017/04/30 17:05:02 christos Exp $")
39 #endif
40 
41 #include <assert.h>
42 #ifdef CDF_DEBUG
43 #include <err.h>
44 #endif
45 #include <stdlib.h>
46 
47 #ifdef PHP_WIN32
48 #include "win32/unistd.h"
49 #else
50 #include <unistd.h>
51 #endif
52 
53 #ifndef UINT32_MAX
54 # define UINT32_MAX (0xffffffff)
55 #endif
56 
57 #include <string.h>
58 #include <time.h>
59 #include <ctype.h>
60 #ifdef HAVE_LIMITS_H
61 #include <limits.h>
62 #endif
63 
64 #ifndef EFTYPE
65 #define EFTYPE EINVAL
66 #endif
67 
68 #include "cdf.h"
69 
70 #ifdef CDF_DEBUG
71 #define DPRINTF(a) printf a, fflush(stdout)
72 #else
73 #define DPRINTF(a)
74 #endif
75 
76 static union {
77 	char s[4];
78 	uint32_t u;
79 } cdf_bo;
80 
81 #define NEED_SWAP	(cdf_bo.u == (uint32_t)0x01020304)
82 
83 #define CDF_TOLE8(x)	((uint64_t)(NEED_SWAP ? _cdf_tole8(x) : (uint64_t)(x)))
84 #define CDF_TOLE4(x)	((uint32_t)(NEED_SWAP ? _cdf_tole4(x) : (uint32_t)(x)))
85 #define CDF_TOLE2(x)	((uint16_t)(NEED_SWAP ? _cdf_tole2(x) : (uint16_t)(x)))
86 #define CDF_TOLE(x)	(/*CONSTCOND*/sizeof(x) == 2 ? \
87 			    CDF_TOLE2(CAST(uint16_t, x)) : \
88 			(/*CONSTCOND*/sizeof(x) == 4 ? \
89 			    CDF_TOLE4(CAST(uint32_t, x)) : \
90 			    CDF_TOLE8(CAST(uint64_t, x))))
91 #define CDF_GETUINT32(x, y)	cdf_getuint32(x, y)
92 
93 #define CDF_MALLOC(n) cdf_malloc(__FILE__, __LINE__, (n))
94 #define CDF_REALLOC(p, n) cdf_realloc(__FILE__, __LINE__, (p), (n))
95 #define CDF_CALLOC(n, u) cdf_calloc(__FILE__, __LINE__, (n), (u))
96 
97 
98 static void *
cdf_malloc(const char * file,size_t line,size_t n)99 cdf_malloc(const char *file, size_t line, size_t n)
100 {
101 	DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
102 	return malloc(n);
103 }
104 
105 static void *
cdf_realloc(const char * file,size_t line,void * p,size_t n)106 cdf_realloc(const char *file, size_t line, void *p, size_t n)
107 {
108 	DPRINTF(("%s,%zu: %s %zu\n", file, line, __func__, n));
109 	return realloc(p, n);
110 }
111 
112 static void *
cdf_calloc(const char * file,size_t line,size_t n,size_t u)113 cdf_calloc(const char *file, size_t line, size_t n, size_t u)
114 {
115 	DPRINTF(("%s,%zu: %s %zu %zu\n", file, line, __func__, n, u));
116 	return calloc(n, u);
117 }
118 
119 /*
120  * swap a short
121  */
122 static uint16_t
_cdf_tole2(uint16_t sv)123 _cdf_tole2(uint16_t sv)
124 {
125 	uint16_t rv;
126 	uint8_t *s = (uint8_t *)(void *)&sv;
127 	uint8_t *d = (uint8_t *)(void *)&rv;
128 	d[0] = s[1];
129 	d[1] = s[0];
130 	return rv;
131 }
132 
133 /*
134  * swap an int
135  */
136 static uint32_t
_cdf_tole4(uint32_t sv)137 _cdf_tole4(uint32_t sv)
138 {
139 	uint32_t rv;
140 	uint8_t *s = (uint8_t *)(void *)&sv;
141 	uint8_t *d = (uint8_t *)(void *)&rv;
142 	d[0] = s[3];
143 	d[1] = s[2];
144 	d[2] = s[1];
145 	d[3] = s[0];
146 	return rv;
147 }
148 
149 /*
150  * swap a quad
151  */
152 static uint64_t
_cdf_tole8(uint64_t sv)153 _cdf_tole8(uint64_t sv)
154 {
155 	uint64_t rv;
156 	uint8_t *s = (uint8_t *)(void *)&sv;
157 	uint8_t *d = (uint8_t *)(void *)&rv;
158 	d[0] = s[7];
159 	d[1] = s[6];
160 	d[2] = s[5];
161 	d[3] = s[4];
162 	d[4] = s[3];
163 	d[5] = s[2];
164 	d[6] = s[1];
165 	d[7] = s[0];
166 	return rv;
167 }
168 
169 /*
170  * grab a uint32_t from a possibly unaligned address, and return it in
171  * the native host order.
172  */
173 static uint32_t
cdf_getuint32(const uint8_t * p,size_t offs)174 cdf_getuint32(const uint8_t *p, size_t offs)
175 {
176 	uint32_t rv;
177 	(void)memcpy(&rv, p + offs * sizeof(uint32_t), sizeof(rv));
178 	return CDF_TOLE4(rv);
179 }
180 
181 #define CDF_UNPACK(a)	\
182     (void)memcpy(&(a), &buf[len], sizeof(a)), len += sizeof(a)
183 #define CDF_UNPACKA(a)	\
184     (void)memcpy((a), &buf[len], sizeof(a)), len += sizeof(a)
185 
186 uint16_t
cdf_tole2(uint16_t sv)187 cdf_tole2(uint16_t sv)
188 {
189 	return CDF_TOLE2(sv);
190 }
191 
192 uint32_t
cdf_tole4(uint32_t sv)193 cdf_tole4(uint32_t sv)
194 {
195 	return CDF_TOLE4(sv);
196 }
197 
198 uint64_t
cdf_tole8(uint64_t sv)199 cdf_tole8(uint64_t sv)
200 {
201 	return CDF_TOLE8(sv);
202 }
203 
204 void
cdf_swap_header(cdf_header_t * h)205 cdf_swap_header(cdf_header_t *h)
206 {
207 	size_t i;
208 
209 	h->h_magic = CDF_TOLE8(h->h_magic);
210 	h->h_uuid[0] = CDF_TOLE8(h->h_uuid[0]);
211 	h->h_uuid[1] = CDF_TOLE8(h->h_uuid[1]);
212 	h->h_revision = CDF_TOLE2(h->h_revision);
213 	h->h_version = CDF_TOLE2(h->h_version);
214 	h->h_byte_order = CDF_TOLE2(h->h_byte_order);
215 	h->h_sec_size_p2 = CDF_TOLE2(h->h_sec_size_p2);
216 	h->h_short_sec_size_p2 = CDF_TOLE2(h->h_short_sec_size_p2);
217 	h->h_num_sectors_in_sat = CDF_TOLE4(h->h_num_sectors_in_sat);
218 	h->h_secid_first_directory = CDF_TOLE4(h->h_secid_first_directory);
219 	h->h_min_size_standard_stream =
220 	    CDF_TOLE4(h->h_min_size_standard_stream);
221 	h->h_secid_first_sector_in_short_sat =
222 	    CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_short_sat);
223 	h->h_num_sectors_in_short_sat =
224 	    CDF_TOLE4(h->h_num_sectors_in_short_sat);
225 	h->h_secid_first_sector_in_master_sat =
226 	    CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_master_sat);
227 	h->h_num_sectors_in_master_sat =
228 	    CDF_TOLE4(h->h_num_sectors_in_master_sat);
229 	for (i = 0; i < __arraycount(h->h_master_sat); i++)
230 		h->h_master_sat[i] = CDF_TOLE4((uint32_t)h->h_master_sat[i]);
231 }
232 
233 void
cdf_unpack_header(cdf_header_t * h,char * buf)234 cdf_unpack_header(cdf_header_t *h, char *buf)
235 {
236 	size_t i;
237 	size_t len = 0;
238 
239 	CDF_UNPACK(h->h_magic);
240 	CDF_UNPACKA(h->h_uuid);
241 	CDF_UNPACK(h->h_revision);
242 	CDF_UNPACK(h->h_version);
243 	CDF_UNPACK(h->h_byte_order);
244 	CDF_UNPACK(h->h_sec_size_p2);
245 	CDF_UNPACK(h->h_short_sec_size_p2);
246 	CDF_UNPACKA(h->h_unused0);
247 	CDF_UNPACK(h->h_num_sectors_in_sat);
248 	CDF_UNPACK(h->h_secid_first_directory);
249 	CDF_UNPACKA(h->h_unused1);
250 	CDF_UNPACK(h->h_min_size_standard_stream);
251 	CDF_UNPACK(h->h_secid_first_sector_in_short_sat);
252 	CDF_UNPACK(h->h_num_sectors_in_short_sat);
253 	CDF_UNPACK(h->h_secid_first_sector_in_master_sat);
254 	CDF_UNPACK(h->h_num_sectors_in_master_sat);
255 	for (i = 0; i < __arraycount(h->h_master_sat); i++)
256 		CDF_UNPACK(h->h_master_sat[i]);
257 }
258 
259 void
cdf_swap_dir(cdf_directory_t * d)260 cdf_swap_dir(cdf_directory_t *d)
261 {
262 	d->d_namelen = CDF_TOLE2(d->d_namelen);
263 	d->d_left_child = CDF_TOLE4((uint32_t)d->d_left_child);
264 	d->d_right_child = CDF_TOLE4((uint32_t)d->d_right_child);
265 	d->d_storage = CDF_TOLE4((uint32_t)d->d_storage);
266 	d->d_storage_uuid[0] = CDF_TOLE8(d->d_storage_uuid[0]);
267 	d->d_storage_uuid[1] = CDF_TOLE8(d->d_storage_uuid[1]);
268 	d->d_flags = CDF_TOLE4(d->d_flags);
269 	d->d_created = CDF_TOLE8((uint64_t)d->d_created);
270 	d->d_modified = CDF_TOLE8((uint64_t)d->d_modified);
271 	d->d_stream_first_sector = CDF_TOLE4((uint32_t)d->d_stream_first_sector);
272 	d->d_size = CDF_TOLE4(d->d_size);
273 }
274 
275 void
cdf_swap_class(cdf_classid_t * d)276 cdf_swap_class(cdf_classid_t *d)
277 {
278 	d->cl_dword = CDF_TOLE4(d->cl_dword);
279 	d->cl_word[0] = CDF_TOLE2(d->cl_word[0]);
280 	d->cl_word[1] = CDF_TOLE2(d->cl_word[1]);
281 }
282 
283 void
cdf_unpack_dir(cdf_directory_t * d,char * buf)284 cdf_unpack_dir(cdf_directory_t *d, char *buf)
285 {
286 	size_t len = 0;
287 
288 	CDF_UNPACKA(d->d_name);
289 	CDF_UNPACK(d->d_namelen);
290 	CDF_UNPACK(d->d_type);
291 	CDF_UNPACK(d->d_color);
292 	CDF_UNPACK(d->d_left_child);
293 	CDF_UNPACK(d->d_right_child);
294 	CDF_UNPACK(d->d_storage);
295 	CDF_UNPACKA(d->d_storage_uuid);
296 	CDF_UNPACK(d->d_flags);
297 	CDF_UNPACK(d->d_created);
298 	CDF_UNPACK(d->d_modified);
299 	CDF_UNPACK(d->d_stream_first_sector);
300 	CDF_UNPACK(d->d_size);
301 	CDF_UNPACK(d->d_unused0);
302 }
303 
304 int
cdf_zero_stream(cdf_stream_t * scn)305 cdf_zero_stream(cdf_stream_t *scn)
306 {
307 	scn->sst_len = 0;
308 	scn->sst_dirlen = 0;
309 	scn->sst_ss = 0;
310 	free(scn->sst_tab);
311 	scn->sst_tab = NULL;
312 	return -1;
313 }
314 
315 static size_t
cdf_check_stream(const cdf_stream_t * sst,const cdf_header_t * h)316 cdf_check_stream(const cdf_stream_t *sst, const cdf_header_t *h)
317 {
318 	size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
319 	    CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
320 	assert(ss == sst->sst_ss);
321 	return sst->sst_ss;
322 }
323 
324 static int
cdf_check_stream_offset(const cdf_stream_t * sst,const cdf_header_t * h,const void * p,size_t tail,int line)325 cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h,
326     const void *p, size_t tail, int line)
327 {
328 	const char *b = (const char *)sst->sst_tab;
329 	const char *e = ((const char *)p) + tail;
330 	size_t ss = cdf_check_stream(sst, h);
331 	/*LINTED*/(void)&line;
332 	if (e >= b && (size_t)(e - b) <= ss * sst->sst_len)
333 		return 0;
334 	DPRINTF(("%d: offset begin %p < end %p || %" SIZE_T_FORMAT "u"
335 	    " > %" SIZE_T_FORMAT "u [%" SIZE_T_FORMAT "u %"
336 	    SIZE_T_FORMAT "u]\n", line, b, e, (size_t)(e - b),
337 	    ss * sst->sst_len, ss, sst->sst_len));
338 	errno = EFTYPE;
339 	return -1;
340 }
341 
342 static ssize_t
cdf_read(const cdf_info_t * info,zend_off_t off,void * buf,size_t len)343 cdf_read(const cdf_info_t *info, zend_off_t off, void *buf, size_t len)
344 {
345 	size_t siz = (size_t)off + len;
346 
347 	if ((zend_off_t)(off + len) != (zend_off_t)siz) {
348 		goto out;
349 	}
350 
351 	if (info->i_buf != NULL && info->i_len >= siz) {
352 		(void)memcpy(buf, &info->i_buf[off], len);
353 		return (ssize_t)len;
354 	}
355 
356 	if (info->i_fd == -1)
357 		goto out;
358 
359 	if (FINFO_LSEEK_FUNC(info->i_fd, off, SEEK_SET) == (zend_off_t)-1)
360 		return -1;
361 
362 	if (FINFO_READ_FUNC(info->i_fd, buf, len) != (ssize_t)len)
363 		return -1;
364 
365 	return (ssize_t)len;
366 out:
367 	errno = EINVAL;
368 	return -1;
369 }
370 
371 int
cdf_read_header(const cdf_info_t * info,cdf_header_t * h)372 cdf_read_header(const cdf_info_t *info, cdf_header_t *h)
373 {
374 	char buf[512];
375 
376 	(void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
377 	if (cdf_read(info, (zend_off_t)0, buf, sizeof(buf)) == -1)
378 		return -1;
379 	cdf_unpack_header(h, buf);
380 	cdf_swap_header(h);
381 	if (h->h_magic != CDF_MAGIC) {
382 		DPRINTF(("Bad magic %#" INT64_T_FORMAT "x != %#"
383 		    INT64_T_FORMAT "x\n",
384 		    (unsigned long long)h->h_magic,
385 		    (unsigned long long)CDF_MAGIC));
386 		goto out;
387 	}
388 	if (h->h_sec_size_p2 > 20) {
389 		DPRINTF(("Bad sector size %hu\n", h->h_sec_size_p2));
390 		goto out;
391 	}
392 	if (h->h_short_sec_size_p2 > 20) {
393 		DPRINTF(("Bad short sector size %hu\n",
394 		    h->h_short_sec_size_p2));
395 		goto out;
396 	}
397 	return 0;
398 out:
399 	errno = EFTYPE;
400 	return -1;
401 }
402 
403 
404 ssize_t
cdf_read_sector(const cdf_info_t * info,void * buf,size_t offs,size_t len,const cdf_header_t * h,cdf_secid_t id)405 cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
406     const cdf_header_t *h, cdf_secid_t id)
407 {
408 	size_t ss = CDF_SEC_SIZE(h);
409 	size_t pos = CDF_SEC_POS(h, id);
410 	assert(ss == len);
411 	return cdf_read(info, (zend_off_t)pos, ((char *)buf) + offs, len);
412 }
413 
414 ssize_t
cdf_read_short_sector(const cdf_stream_t * sst,void * buf,size_t offs,size_t len,const cdf_header_t * h,cdf_secid_t id)415 cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
416     size_t len, const cdf_header_t *h, cdf_secid_t id)
417 {
418 	size_t ss = CDF_SHORT_SEC_SIZE(h);
419 	size_t pos = CDF_SHORT_SEC_POS(h, id);
420 	assert(ss == len);
421 	if (pos + len > CDF_SEC_SIZE(h) * sst->sst_len) {
422 		DPRINTF(("Out of bounds read %" SIZE_T_FORMAT "u > %"
423 		    SIZE_T_FORMAT "u\n",
424 		    pos + len, CDF_SEC_SIZE(h) * sst->sst_len));
425 		goto out;
426 	}
427 	(void)memcpy(((char *)buf) + offs,
428 	    ((const char *)sst->sst_tab) + pos, len);
429 	return len;
430 out:
431 	errno = EFTYPE;
432 	return -1;
433 }
434 
435 /*
436  * Read the sector allocation table.
437  */
438 int
cdf_read_sat(const cdf_info_t * info,cdf_header_t * h,cdf_sat_t * sat)439 cdf_read_sat(const cdf_info_t *info, cdf_header_t *h, cdf_sat_t *sat)
440 {
441 	size_t i, j, k;
442 	size_t ss = CDF_SEC_SIZE(h);
443 	cdf_secid_t *msa, mid, sec;
444 	size_t nsatpersec = (ss / sizeof(mid)) - 1;
445 
446 	for (i = 0; i < __arraycount(h->h_master_sat); i++)
447 		if (h->h_master_sat[i] == CDF_SECID_FREE)
448 			break;
449 
450 #define CDF_SEC_LIMIT (UINT32_MAX / (8 * ss))
451 	if ((nsatpersec > 0 &&
452 	    h->h_num_sectors_in_master_sat > CDF_SEC_LIMIT / nsatpersec) ||
453 	    i > CDF_SEC_LIMIT) {
454 		DPRINTF(("Number of sectors in master SAT too big %u %"
455 		    SIZE_T_FORMAT "u\n", h->h_num_sectors_in_master_sat, i));
456 		errno = EFTYPE;
457 		return -1;
458 	}
459 
460 	sat->sat_len = h->h_num_sectors_in_master_sat * nsatpersec + i;
461 	DPRINTF(("sat_len = %" SIZE_T_FORMAT "u ss = %" SIZE_T_FORMAT "u\n",
462 	    sat->sat_len, ss));
463 	if ((sat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(sat->sat_len, ss)))
464 	    == NULL)
465 		return -1;
466 
467 	for (i = 0; i < __arraycount(h->h_master_sat); i++) {
468 		if (h->h_master_sat[i] < 0)
469 			break;
470 		if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
471 		    h->h_master_sat[i]) != (ssize_t)ss) {
472 			DPRINTF(("Reading sector %d", h->h_master_sat[i]));
473 			goto out1;
474 		}
475 	}
476 
477 	if ((msa = CAST(cdf_secid_t *, CDF_CALLOC(1, ss))) == NULL)
478 		goto out1;
479 
480 	mid = h->h_secid_first_sector_in_master_sat;
481 	for (j = 0; j < h->h_num_sectors_in_master_sat; j++) {
482 		if (mid < 0)
483 			goto out;
484 		if (j >= CDF_LOOP_LIMIT) {
485 			DPRINTF(("Reading master sector loop limit"));
486 			goto out3;
487 		}
488 		if (cdf_read_sector(info, msa, 0, ss, h, mid) != (ssize_t)ss) {
489 			DPRINTF(("Reading master sector %d", mid));
490 			goto out2;
491 		}
492 		for (k = 0; k < nsatpersec; k++, i++) {
493 			sec = CDF_TOLE4((uint32_t)msa[k]);
494 			if (sec < 0)
495 				goto out;
496 			if (i >= sat->sat_len) {
497 			    DPRINTF(("Out of bounds reading MSA %" SIZE_T_FORMAT
498 				"u >= %" SIZE_T_FORMAT "u", i, sat->sat_len));
499 			    goto out3;
500 			}
501 			if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
502 			    sec) != (ssize_t)ss) {
503 				DPRINTF(("Reading sector %d",
504 				    CDF_TOLE4(msa[k])));
505 				goto out2;
506 			}
507 		}
508 		mid = CDF_TOLE4((uint32_t)msa[nsatpersec]);
509 	}
510 out:
511 	sat->sat_len = i;
512 	free(msa);
513 	return 0;
514 out3:
515 	errno = EFTYPE;
516 out2:
517 	free(msa);
518 out1:
519 	free(sat->sat_tab);
520 	return -1;
521 }
522 
523 size_t
cdf_count_chain(const cdf_sat_t * sat,cdf_secid_t sid,size_t size)524 cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
525 {
526 	size_t i, j;
527 	cdf_secid_t maxsector = (cdf_secid_t)((sat->sat_len * size)
528 	    / sizeof(maxsector));
529 
530 	DPRINTF(("Chain:"));
531 	if (sid == CDF_SECID_END_OF_CHAIN) {
532 		/* 0-length chain. */
533 		DPRINTF((" empty\n"));
534 		return 0;
535 	}
536 
537 	for (j = i = 0; sid >= 0; i++, j++) {
538 		DPRINTF((" %d", sid));
539 		if (j >= CDF_LOOP_LIMIT) {
540 			DPRINTF(("Counting chain loop limit"));
541 			goto out;
542 		}
543 		if (sid >= maxsector) {
544 			DPRINTF(("Sector %d >= %d\n", sid, maxsector));
545 			goto out;
546 		}
547 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
548 	}
549 	if (i == 0) {
550 		DPRINTF((" none, sid: %d\n", sid));
551 		goto out;
552 
553 	}
554 	DPRINTF(("\n"));
555 	return i;
556 out:
557 	errno = EFTYPE;
558 	return (size_t)-1;
559 }
560 
561 int
cdf_read_long_sector_chain(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,cdf_secid_t sid,size_t len,cdf_stream_t * scn)562 cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
563     const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
564 {
565 	size_t ss = CDF_SEC_SIZE(h), i, j;
566 	ssize_t nr;
567 	scn->sst_tab = NULL;
568 	scn->sst_len = cdf_count_chain(sat, sid, ss);
569 	scn->sst_dirlen = MAX(h->h_min_size_standard_stream, len);
570 	scn->sst_ss = ss;
571 
572 	if (sid == CDF_SECID_END_OF_CHAIN || len == 0)
573 		return cdf_zero_stream(scn);
574 
575 	if (scn->sst_len == (size_t)-1)
576 		goto out;
577 
578 	scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
579 	if (scn->sst_tab == NULL)
580 		return cdf_zero_stream(scn);
581 
582 	for (j = i = 0; sid >= 0; i++, j++) {
583 		if (j >= CDF_LOOP_LIMIT) {
584 			DPRINTF(("Read long sector chain loop limit"));
585 			goto out;
586 		}
587 		if (i >= scn->sst_len) {
588 			DPRINTF(("Out of bounds reading long sector chain "
589 			    "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
590 			    scn->sst_len));
591 			goto out;
592 		}
593 		if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
594 		    sid)) != (ssize_t)ss) {
595 			if (i == scn->sst_len - 1 && nr > 0) {
596 				/* Last sector might be truncated */
597 				return 0;
598 			}
599 			DPRINTF(("Reading long sector chain %d", sid));
600 			goto out;
601 		}
602 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
603 	}
604 	return 0;
605 out:
606 	errno = EFTYPE;
607 	return cdf_zero_stream(scn);
608 }
609 
610 int
cdf_read_short_sector_chain(const cdf_header_t * h,const cdf_sat_t * ssat,const cdf_stream_t * sst,cdf_secid_t sid,size_t len,cdf_stream_t * scn)611 cdf_read_short_sector_chain(const cdf_header_t *h,
612     const cdf_sat_t *ssat, const cdf_stream_t *sst,
613     cdf_secid_t sid, size_t len, cdf_stream_t *scn)
614 {
615 	size_t ss = CDF_SHORT_SEC_SIZE(h), i, j;
616 	scn->sst_tab = NULL;
617 	scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h));
618 	scn->sst_dirlen = len;
619 	scn->sst_ss = ss;
620 
621 	if (scn->sst_len == (size_t)-1)
622 		goto out;
623 
624 	scn->sst_tab = CDF_CALLOC(scn->sst_len, ss);
625 	if (scn->sst_tab == NULL)
626 		return cdf_zero_stream(scn);
627 
628 	for (j = i = 0; sid >= 0; i++, j++) {
629 		if (j >= CDF_LOOP_LIMIT) {
630 			DPRINTF(("Read short sector chain loop limit"));
631 			goto out;
632 		}
633 		if (i >= scn->sst_len) {
634 			DPRINTF(("Out of bounds reading short sector chain "
635 			    "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n",
636 			    i, scn->sst_len));
637 			goto out;
638 		}
639 		if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
640 		    sid) != (ssize_t)ss) {
641 			DPRINTF(("Reading short sector chain %d", sid));
642 			goto out;
643 		}
644 		sid = CDF_TOLE4((uint32_t)ssat->sat_tab[sid]);
645 	}
646 	return 0;
647 out:
648 	errno = EFTYPE;
649 	return cdf_zero_stream(scn);
650 }
651 
652 int
cdf_read_sector_chain(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,const cdf_sat_t * ssat,const cdf_stream_t * sst,cdf_secid_t sid,size_t len,cdf_stream_t * scn)653 cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
654     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
655     cdf_secid_t sid, size_t len, cdf_stream_t *scn)
656 {
657 
658 	if (len < h->h_min_size_standard_stream && sst->sst_tab != NULL)
659 		return cdf_read_short_sector_chain(h, ssat, sst, sid, len,
660 		    scn);
661 	else
662 		return cdf_read_long_sector_chain(info, h, sat, sid, len, scn);
663 }
664 
665 int
cdf_read_dir(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,cdf_dir_t * dir)666 cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h,
667     const cdf_sat_t *sat, cdf_dir_t *dir)
668 {
669 	size_t i, j;
670 	size_t ss = CDF_SEC_SIZE(h), ns, nd;
671 	char *buf;
672 	cdf_secid_t sid = h->h_secid_first_directory;
673 
674 	ns = cdf_count_chain(sat, sid, ss);
675 	if (ns == (size_t)-1)
676 		return -1;
677 
678 	nd = ss / CDF_DIRECTORY_SIZE;
679 
680 	dir->dir_len = ns * nd;
681 	dir->dir_tab = CAST(cdf_directory_t *,
682 	    CDF_CALLOC(dir->dir_len, sizeof(dir->dir_tab[0])));
683 	if (dir->dir_tab == NULL)
684 		return -1;
685 
686 	if ((buf = CAST(char *, CDF_MALLOC(ss))) == NULL) {
687 		free(dir->dir_tab);
688 		return -1;
689 	}
690 
691 	for (j = i = 0; i < ns; i++, j++) {
692 		if (j >= CDF_LOOP_LIMIT) {
693 			DPRINTF(("Read dir loop limit"));
694 			goto out;
695 		}
696 		if (cdf_read_sector(info, buf, 0, ss, h, sid) != (ssize_t)ss) {
697 			DPRINTF(("Reading directory sector %d", sid));
698 			goto out;
699 		}
700 		for (j = 0; j < nd; j++) {
701 			cdf_unpack_dir(&dir->dir_tab[i * nd + j],
702 			    &buf[j * CDF_DIRECTORY_SIZE]);
703 		}
704 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
705 	}
706 	if (NEED_SWAP)
707 		for (i = 0; i < dir->dir_len; i++)
708 			cdf_swap_dir(&dir->dir_tab[i]);
709 	free(buf);
710 	return 0;
711 out:
712 	free(dir->dir_tab);
713 	free(buf);
714 	errno = EFTYPE;
715 	return -1;
716 }
717 
718 
719 int
cdf_read_ssat(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,cdf_sat_t * ssat)720 cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h,
721     const cdf_sat_t *sat, cdf_sat_t *ssat)
722 {
723 	size_t i, j;
724 	size_t ss = CDF_SEC_SIZE(h);
725 	cdf_secid_t sid = h->h_secid_first_sector_in_short_sat;
726 
727 	ssat->sat_tab = NULL;
728 	ssat->sat_len = cdf_count_chain(sat, sid, ss);
729 	if (ssat->sat_len == (size_t)-1)
730 		goto out;
731 
732 	ssat->sat_tab = CAST(cdf_secid_t *, CDF_CALLOC(ssat->sat_len, ss));
733 	if (ssat->sat_tab == NULL)
734 		goto out1;
735 
736 	for (j = i = 0; sid >= 0; i++, j++) {
737 		if (j >= CDF_LOOP_LIMIT) {
738 			DPRINTF(("Read short sat sector loop limit"));
739 			goto out;
740 		}
741 		if (i >= ssat->sat_len) {
742 			DPRINTF(("Out of bounds reading short sector chain "
743 			    "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
744 			    ssat->sat_len));
745 			goto out;
746 		}
747 		if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
748 		    (ssize_t)ss) {
749 			DPRINTF(("Reading short sat sector %d", sid));
750 			goto out1;
751 		}
752 		sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
753 	}
754 	return 0;
755 out:
756 	errno = EFTYPE;
757 out1:
758 	free(ssat->sat_tab);
759 	return -1;
760 }
761 
762 int
cdf_read_short_stream(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,const cdf_dir_t * dir,cdf_stream_t * scn,const cdf_directory_t ** root)763 cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
764     const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn,
765     const cdf_directory_t **root)
766 {
767 	size_t i;
768 	const cdf_directory_t *d;
769 
770 	*root = NULL;
771 	for (i = 0; i < dir->dir_len; i++)
772 		if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
773 			break;
774 
775 	/* If the it is not there, just fake it; some docs don't have it */
776 	if (i == dir->dir_len) {
777 		DPRINTF(("Cannot find root storage dir\n"));
778 		goto out;
779 	}
780 	d = &dir->dir_tab[i];
781 	*root = d;
782 
783 	/* If the it is not there, just fake it; some docs don't have it */
784 	if (d->d_stream_first_sector < 0) {
785 		DPRINTF(("No first secror in dir\n"));
786 		goto out;
787 	}
788 
789 	return cdf_read_long_sector_chain(info, h, sat,
790 	    d->d_stream_first_sector, d->d_size, scn);
791 out:
792 	scn->sst_tab = NULL;
793 	(void)cdf_zero_stream(scn);
794 	return 0;
795 }
796 
797 static int
cdf_namecmp(const char * d,const uint16_t * s,size_t l)798 cdf_namecmp(const char *d, const uint16_t *s, size_t l)
799 {
800 	for (; l--; d++, s++)
801 		if (*d != CDF_TOLE2(*s))
802 			return (unsigned char)*d - CDF_TOLE2(*s);
803 	return 0;
804 }
805 
806 int
cdf_read_doc_summary_info(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,const cdf_sat_t * ssat,const cdf_stream_t * sst,const cdf_dir_t * dir,cdf_stream_t * scn)807 cdf_read_doc_summary_info(const cdf_info_t *info, const cdf_header_t *h,
808     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
809     const cdf_dir_t *dir, cdf_stream_t *scn)
810 {
811 	return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
812 	    "\05DocumentSummaryInformation", scn);
813 }
814 
815 int
cdf_read_summary_info(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,const cdf_sat_t * ssat,const cdf_stream_t * sst,const cdf_dir_t * dir,cdf_stream_t * scn)816 cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
817     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
818     const cdf_dir_t *dir, cdf_stream_t *scn)
819 {
820 	return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
821 	    "\05SummaryInformation", scn);
822 }
823 
824 int
cdf_read_user_stream(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,const cdf_sat_t * ssat,const cdf_stream_t * sst,const cdf_dir_t * dir,const char * name,cdf_stream_t * scn)825 cdf_read_user_stream(const cdf_info_t *info, const cdf_header_t *h,
826     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
827     const cdf_dir_t *dir, const char *name, cdf_stream_t *scn)
828 {
829 	const cdf_directory_t *d;
830 	int i = cdf_find_stream(dir, name, CDF_DIR_TYPE_USER_STREAM);
831 
832 	if (i <= 0) {
833 		memset(scn, 0, sizeof(*scn));
834 		return -1;
835 	}
836 
837 	d = &dir->dir_tab[i - 1];
838 	return cdf_read_sector_chain(info, h, sat, ssat, sst,
839 	    d->d_stream_first_sector, d->d_size, scn);
840 }
841 
842 int
cdf_find_stream(const cdf_dir_t * dir,const char * name,int type)843 cdf_find_stream(const cdf_dir_t *dir, const char *name, int type)
844 {
845 	size_t i, name_len = strlen(name) + 1;
846 
847 	for (i = dir->dir_len; i > 0; i--)
848 		if (dir->dir_tab[i - 1].d_type == type &&
849 		    cdf_namecmp(name, dir->dir_tab[i - 1].d_name, name_len)
850 		    == 0)
851 			break;
852 	if (i > 0)
853 		return CAST(int, i);
854 
855 	DPRINTF(("Cannot find type %d `%s'\n", type, name));
856 	errno = ESRCH;
857 	return 0;
858 }
859 
860 #define CDF_SHLEN_LIMIT (UINT32_MAX / 8)
861 #define CDF_PROP_LIMIT (UINT32_MAX / (8 * sizeof(cdf_property_info_t)))
862 
863 static const void *
cdf_offset(const void * p,size_t l)864 cdf_offset(const void *p, size_t l)
865 {
866 	return CAST(const void *, CAST(const uint8_t *, p) + l);
867 }
868 
869 static const uint8_t *
cdf_get_property_info_pos(const cdf_stream_t * sst,const cdf_header_t * h,const uint8_t * p,const uint8_t * e,size_t i)870 cdf_get_property_info_pos(const cdf_stream_t *sst, const cdf_header_t *h,
871     const uint8_t *p, const uint8_t *e, size_t i)
872 {
873 	size_t tail = (i << 1) + 1;
874 	size_t ofs;
875 	const uint8_t *q;
876 
877 	if (p >= e) {
878 		DPRINTF(("Past end %p < %p\n", e, p));
879 		return NULL;
880 	}
881 	if (cdf_check_stream_offset(sst, h, p, (tail + 1) * sizeof(uint32_t),
882 	    __LINE__) == -1)
883 		return NULL;
884 	ofs = CDF_GETUINT32(p, tail);
885 	q = CAST(const uint8_t *, cdf_offset(CAST(const void *, p),
886 	    ofs - 2 * sizeof(uint32_t)));
887 
888 	if (q < p) {
889 		DPRINTF(("Wrapped around %p < %p\n", q, p));
890 		return NULL;
891 	}
892 
893 	if (q >= e) {
894 		DPRINTF(("Ran off the end %p >= %p\n", q, e));
895 		return NULL;
896 	}
897 	return q;
898 }
899 
900 static cdf_property_info_t *
cdf_grow_info(cdf_property_info_t ** info,size_t * maxcount,size_t incr)901 cdf_grow_info(cdf_property_info_t **info, size_t *maxcount, size_t incr)
902 {
903 	cdf_property_info_t *inp;
904 	size_t newcount = *maxcount + incr;
905 
906 	if (newcount > CDF_PROP_LIMIT) {
907 		DPRINTF(("exceeded property limit %zu > %zu\n",
908 		    newcount, CDF_PROP_LIMIT));
909 		goto out;
910 	}
911 	inp = CAST(cdf_property_info_t *,
912 	    CDF_REALLOC(*info, newcount * sizeof(*inp)));
913 	if (inp == NULL)
914 		goto out;
915 
916 	*info = inp;
917 	*maxcount = newcount;
918 	return inp;
919 out:
920 	free(*info);
921 	*maxcount = 0;
922 	*info = NULL;
923 	return NULL;
924 }
925 
926 static int
cdf_copy_info(cdf_property_info_t * inp,const void * p,const void * e,size_t len)927 cdf_copy_info(cdf_property_info_t *inp, const void *p, const void *e,
928     size_t len)
929 {
930 	if (inp->pi_type & CDF_VECTOR)
931 		return 0;
932 
933 	if ((size_t)(CAST(const char *, e) - CAST(const char *, p)) < len)
934 		return 0;
935 
936 	(void)memcpy(&inp->pi_val, p, len);
937 
938 	switch (len) {
939 	case 2:
940 		inp->pi_u16 = CDF_TOLE2(inp->pi_u16);
941 		break;
942 	case 4:
943 		inp->pi_u32 = CDF_TOLE4(inp->pi_u32);
944 		break;
945 	case 8:
946 		inp->pi_u64 = CDF_TOLE8(inp->pi_u64);
947 		break;
948 	default:
949 		abort();
950 	}
951 	return 1;
952 }
953 
954 int
cdf_read_property_info(const cdf_stream_t * sst,const cdf_header_t * h,uint32_t offs,cdf_property_info_t ** info,size_t * count,size_t * maxcount)955 cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
956     uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
957 {
958 	const cdf_section_header_t *shp;
959 	cdf_section_header_t sh;
960 	const uint8_t *p, *q, *e;
961 	size_t i, o4, nelements, j, slen, left;
962 	cdf_property_info_t *inp;
963 
964 	if (offs > UINT32_MAX / 4) {
965 		errno = EFTYPE;
966 		goto out;
967 	}
968 	shp = CAST(const cdf_section_header_t *,
969 	    cdf_offset(sst->sst_tab, offs));
970 	if (cdf_check_stream_offset(sst, h, shp, sizeof(*shp), __LINE__) == -1)
971 		goto out;
972 	sh.sh_len = CDF_TOLE4(shp->sh_len);
973 	if (sh.sh_len > CDF_SHLEN_LIMIT) {
974 		errno = EFTYPE;
975 		goto out;
976 	}
977 
978 	if (cdf_check_stream_offset(sst, h, shp, sh.sh_len, __LINE__) == -1)
979 		goto out;
980 
981 	sh.sh_properties = CDF_TOLE4(shp->sh_properties);
982 	DPRINTF(("section len: %u properties %u\n", sh.sh_len,
983 	    sh.sh_properties));
984 	if (sh.sh_properties > CDF_PROP_LIMIT)
985 		goto out;
986 	inp = cdf_grow_info(info, maxcount, sh.sh_properties);
987 	if (inp == NULL)
988 		goto out;
989 	inp += *count;
990 	*count += sh.sh_properties;
991 	p = CAST(const uint8_t *, cdf_offset(sst->sst_tab, offs + sizeof(sh)));
992 	e = CAST(const uint8_t *, cdf_offset(shp, sh.sh_len));
993 	if (p >= e || cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1)
994 		goto out;
995 
996 	for (i = 0; i < sh.sh_properties; i++) {
997 		if ((q = cdf_get_property_info_pos(sst, h, p, e, i)) == NULL)
998 			goto out;
999 		inp[i].pi_id = CDF_GETUINT32(p, i << 1);
1000 		left = CAST(size_t, e - q);
1001 		if (left < sizeof(uint32_t)) {
1002 			DPRINTF(("short info (no type)_\n"));
1003 			goto out;
1004 		}
1005 		inp[i].pi_type = CDF_GETUINT32(q, 0);
1006 		DPRINTF(("%" SIZE_T_FORMAT "u) id=%#x type=%#x offs=%#tx,%#x\n",
1007 		    i, inp[i].pi_id, inp[i].pi_type, q - p, offs));
1008 		if (inp[i].pi_type & CDF_VECTOR) {
1009 			if (left < sizeof(uint32_t) * 2) {
1010 				DPRINTF(("missing CDF_VECTOR length\n"));
1011 				goto out;
1012 			}
1013 			nelements = CDF_GETUINT32(q, 1);
1014 			if (nelements > CDF_ELEMENT_LIMIT || nelements == 0) {
1015 				DPRINTF(("CDF_VECTOR with nelements == %"
1016 				    SIZE_T_FORMAT "u\n", nelements));
1017 				goto out;
1018 			}
1019 			slen = 2;
1020 		} else {
1021 			nelements = 1;
1022 			slen = 1;
1023 		}
1024 		o4 = slen * sizeof(uint32_t);
1025 		if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
1026 			goto unknown;
1027 		switch (inp[i].pi_type & CDF_TYPEMASK) {
1028 		case CDF_NULL:
1029 		case CDF_EMPTY:
1030 			break;
1031 		case CDF_SIGNED16:
1032 			if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int16_t)))
1033 				goto unknown;
1034 			break;
1035 		case CDF_SIGNED32:
1036 		case CDF_BOOL:
1037 		case CDF_UNSIGNED32:
1038 		case CDF_FLOAT:
1039 			if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int32_t)))
1040 				goto unknown;
1041 			break;
1042 		case CDF_SIGNED64:
1043 		case CDF_UNSIGNED64:
1044 		case CDF_DOUBLE:
1045 		case CDF_FILETIME:
1046 			if (!cdf_copy_info(&inp[i], &q[o4], e, sizeof(int64_t)))
1047 				goto unknown;
1048 			break;
1049 		case CDF_LENGTH32_STRING:
1050 		case CDF_LENGTH32_WSTRING:
1051 			if (nelements > 1) {
1052 				size_t nelem = inp - *info;
1053 				inp = cdf_grow_info(info, maxcount, nelements);
1054 				if (inp == NULL)
1055 					goto out;
1056 				inp += nelem;
1057 			}
1058 			for (j = 0; j < nelements && i < sh.sh_properties;
1059 			    j++, i++)
1060 			{
1061 				uint32_t l;
1062 
1063 				if (o4 + sizeof(uint32_t) > left)
1064 					goto out;
1065 
1066 				l = CDF_GETUINT32(q, slen);
1067 				o4 += sizeof(uint32_t);
1068 				if (o4 + l > left)
1069 					goto out;
1070 
1071 				inp[i].pi_str.s_len = l;
1072 				inp[i].pi_str.s_buf = CAST(const char *,
1073 				    CAST(const void *, &q[o4]));
1074 
1075 				DPRINTF(("o=%zu l=%d(%" SIZE_T_FORMAT
1076 				    "u), t=%zu s=%s\n", o4, l,
1077 				    CDF_ROUND(l, sizeof(l)), left,
1078 				    inp[i].pi_str.s_buf));
1079 
1080 				if (l & 1)
1081 					l++;
1082 
1083 				slen += l >> 1;
1084 				o4 = slen * sizeof(uint32_t);
1085 			}
1086 			i--;
1087 			break;
1088 		case CDF_CLIPBOARD:
1089 			if (inp[i].pi_type & CDF_VECTOR)
1090 				goto unknown;
1091 			break;
1092 		default:
1093 		unknown:
1094 			memset(&inp[i].pi_val, 0, sizeof(inp[i].pi_val));
1095 			DPRINTF(("Don't know how to deal with %#x\n",
1096 			    inp[i].pi_type));
1097 			break;
1098 		}
1099 	}
1100 	return 0;
1101 out:
1102 	free(*info);
1103 	*info = NULL;
1104 	*count = 0;
1105 	*maxcount = 0;
1106 	errno = EFTYPE;
1107 	return -1;
1108 }
1109 
1110 int
cdf_unpack_summary_info(const cdf_stream_t * sst,const cdf_header_t * h,cdf_summary_info_header_t * ssi,cdf_property_info_t ** info,size_t * count)1111 cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h,
1112     cdf_summary_info_header_t *ssi, cdf_property_info_t **info, size_t *count)
1113 {
1114 	size_t maxcount;
1115 	const cdf_summary_info_header_t *si =
1116 	    CAST(const cdf_summary_info_header_t *, sst->sst_tab);
1117 	const cdf_section_declaration_t *sd =
1118 	    CAST(const cdf_section_declaration_t *, (const void *)
1119 	    ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET));
1120 
1121 	if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 ||
1122 	    cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1)
1123 		return -1;
1124 	ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
1125 	ssi->si_os_version = CDF_TOLE2(si->si_os_version);
1126 	ssi->si_os = CDF_TOLE2(si->si_os);
1127 	ssi->si_class = si->si_class;
1128 	cdf_swap_class(&ssi->si_class);
1129 	ssi->si_count = CDF_TOLE4(si->si_count);
1130 	*count = 0;
1131 	maxcount = 0;
1132 	*info = NULL;
1133 	if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset), info,
1134 	    count, &maxcount) == -1)
1135 		return -1;
1136 	return 0;
1137 }
1138 
1139 
1140 #define extract_catalog_field(t, f, l) \
1141     if (b + l + sizeof(cep->f) > eb) { \
1142 	    cep->ce_namlen = 0; \
1143 	    break; \
1144     } \
1145     memcpy(&cep->f, b + (l), sizeof(cep->f)); \
1146     ce[i].f = CAST(t, CDF_TOLE(cep->f))
1147 
1148 int
cdf_unpack_catalog(const cdf_header_t * h,const cdf_stream_t * sst,cdf_catalog_t ** cat)1149 cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst,
1150     cdf_catalog_t **cat)
1151 {
1152 	size_t ss = cdf_check_stream(sst, h);
1153 	const char *b = CAST(const char *, sst->sst_tab);
1154 	const char *nb, *eb = b + ss * sst->sst_len;
1155 	size_t nr, i, j, k;
1156 	cdf_catalog_entry_t *ce;
1157 	uint16_t reclen;
1158 	const uint16_t *np;
1159 
1160 	for (nr = 0;; nr++) {
1161 		memcpy(&reclen, b, sizeof(reclen));
1162 		reclen = CDF_TOLE2(reclen);
1163 		if (reclen == 0)
1164 			break;
1165 		b += reclen;
1166 		if (b > eb)
1167 		    break;
1168 	}
1169 	if (nr == 0)
1170 		return -1;
1171 	nr--;
1172 	*cat = CAST(cdf_catalog_t *,
1173 	    CDF_MALLOC(sizeof(cdf_catalog_t) + nr * sizeof(*ce)));
1174 	if (*cat == NULL)
1175 		return -1;
1176 	ce = (*cat)->cat_e;
1177 	memset(ce, 0, nr * sizeof(*ce));
1178 	b = CAST(const char *, sst->sst_tab);
1179 	for (j = i = 0; i < nr; b += reclen) {
1180 		cdf_catalog_entry_t *cep = &ce[j];
1181 		uint16_t rlen;
1182 
1183 		extract_catalog_field(uint16_t, ce_namlen, 0);
1184 		extract_catalog_field(uint16_t, ce_num, 4);
1185 		extract_catalog_field(uint64_t, ce_timestamp, 8);
1186 		reclen = cep->ce_namlen;
1187 
1188 		if (reclen < 14) {
1189 			cep->ce_namlen = 0;
1190 			continue;
1191 		}
1192 
1193 		cep->ce_namlen = __arraycount(cep->ce_name) - 1;
1194 		rlen = reclen - 14;
1195 		if (cep->ce_namlen > rlen)
1196 			cep->ce_namlen = rlen;
1197 
1198 		np = CAST(const uint16_t *, CAST(const void *, (b + 16)));
1199 		nb = CAST(const char *, CAST(const void *,
1200 		    (np + cep->ce_namlen)));
1201 		if (nb > eb) {
1202 			cep->ce_namlen = 0;
1203 			break;
1204 		}
1205 
1206 		for (k = 0; k < cep->ce_namlen; k++)
1207 			cep->ce_name[k] = np[k]; /* XXX: CDF_TOLE2? */
1208 		cep->ce_name[cep->ce_namlen] = 0;
1209 		j = i;
1210 		i++;
1211 	}
1212 	(*cat)->cat_num = j;
1213 	return 0;
1214 }
1215 
1216 int
cdf_print_classid(char * buf,size_t buflen,const cdf_classid_t * id)1217 cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
1218 {
1219 	return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
1220 	    "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
1221 	    id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
1222 	    id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
1223 	    id->cl_six[5]);
1224 }
1225 
1226 static const struct {
1227 	uint32_t v;
1228 	const char *n;
1229 } vn[] = {
1230 	{ CDF_PROPERTY_CODE_PAGE, "Code page" },
1231 	{ CDF_PROPERTY_TITLE, "Title" },
1232 	{ CDF_PROPERTY_SUBJECT, "Subject" },
1233 	{ CDF_PROPERTY_AUTHOR, "Author" },
1234 	{ CDF_PROPERTY_KEYWORDS, "Keywords" },
1235 	{ CDF_PROPERTY_COMMENTS, "Comments" },
1236 	{ CDF_PROPERTY_TEMPLATE, "Template" },
1237 	{ CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
1238 	{ CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
1239 	{ CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
1240 	{ CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
1241 	{ CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
1242 	{ CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
1243 	{ CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
1244 	{ CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
1245 	{ CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
1246 	{ CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
1247 	{ CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
1248 	{ CDF_PROPERTY_SECURITY, "Security" },
1249 	{ CDF_PROPERTY_LOCALE_ID, "Locale ID" },
1250 };
1251 
1252 int
cdf_print_property_name(char * buf,size_t bufsiz,uint32_t p)1253 cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
1254 {
1255 	size_t i;
1256 
1257 	for (i = 0; i < __arraycount(vn); i++)
1258 		if (vn[i].v == p)
1259 			return snprintf(buf, bufsiz, "%s", vn[i].n);
1260 	return snprintf(buf, bufsiz, "%#x", p);
1261 }
1262 
1263 int
cdf_print_elapsed_time(char * buf,size_t bufsiz,cdf_timestamp_t ts)1264 cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
1265 {
1266 	int len = 0;
1267 	int days, hours, mins, secs;
1268 
1269 	ts /= CDF_TIME_PREC;
1270 	secs = (int)(ts % 60);
1271 	ts /= 60;
1272 	mins = (int)(ts % 60);
1273 	ts /= 60;
1274 	hours = (int)(ts % 24);
1275 	ts /= 24;
1276 	days = (int)ts;
1277 
1278 	if (days) {
1279 		len += snprintf(buf + len, bufsiz - len, "%dd+", days);
1280 		if ((size_t)len >= bufsiz)
1281 			return len;
1282 	}
1283 
1284 	if (days || hours) {
1285 		len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
1286 		if ((size_t)len >= bufsiz)
1287 			return len;
1288 	}
1289 
1290 	len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
1291 	if ((size_t)len >= bufsiz)
1292 		return len;
1293 
1294 	len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
1295 	return len;
1296 }
1297 
1298 char *
cdf_u16tos8(char * buf,size_t len,const uint16_t * p)1299 cdf_u16tos8(char *buf, size_t len, const uint16_t *p)
1300 {
1301 	size_t i;
1302 	for (i = 0; i < len && p[i]; i++)
1303 		buf[i] = (char)p[i];
1304 	buf[i] = '\0';
1305 	return buf;
1306 }
1307 
1308 #ifdef CDF_DEBUG
1309 void
cdf_dump_header(const cdf_header_t * h)1310 cdf_dump_header(const cdf_header_t *h)
1311 {
1312 	size_t i;
1313 
1314 #define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
1315 #define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \
1316     h->h_ ## b, 1 << h->h_ ## b)
1317 	DUMP("%d", revision);
1318 	DUMP("%d", version);
1319 	DUMP("%#x", byte_order);
1320 	DUMP2("%d", sec_size_p2);
1321 	DUMP2("%d", short_sec_size_p2);
1322 	DUMP("%d", num_sectors_in_sat);
1323 	DUMP("%d", secid_first_directory);
1324 	DUMP("%d", min_size_standard_stream);
1325 	DUMP("%d", secid_first_sector_in_short_sat);
1326 	DUMP("%d", num_sectors_in_short_sat);
1327 	DUMP("%d", secid_first_sector_in_master_sat);
1328 	DUMP("%d", num_sectors_in_master_sat);
1329 	for (i = 0; i < __arraycount(h->h_master_sat); i++) {
1330 		if (h->h_master_sat[i] == CDF_SECID_FREE)
1331 			break;
1332 		(void)fprintf(stderr, "%35.35s[%.3" SIZE_T_FORMAT "u] = %d\n",
1333 		    "master_sat", i, h->h_master_sat[i]);
1334 	}
1335 }
1336 
1337 void
cdf_dump_sat(const char * prefix,const cdf_sat_t * sat,size_t size)1338 cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
1339 {
1340 	size_t i, j, s = size / sizeof(cdf_secid_t);
1341 
1342 	for (i = 0; i < sat->sat_len; i++) {
1343 		(void)fprintf(stderr, "%s[%" SIZE_T_FORMAT "u]:\n%.6"
1344 		    SIZE_T_FORMAT "u: ", prefix, i, i * s);
1345 		for (j = 0; j < s; j++) {
1346 			(void)fprintf(stderr, "%5d, ",
1347 			    CDF_TOLE4(sat->sat_tab[s * i + j]));
1348 			if ((j + 1) % 10 == 0)
1349 				(void)fprintf(stderr, "\n%.6" SIZE_T_FORMAT
1350 				    "u: ", i * s + j + 1);
1351 		}
1352 		(void)fprintf(stderr, "\n");
1353 	}
1354 }
1355 
1356 void
cdf_dump(const void * v,size_t len)1357 cdf_dump(const void *v, size_t len)
1358 {
1359 	size_t i, j;
1360 	const unsigned char *p = v;
1361 	char abuf[16];
1362 
1363 	(void)fprintf(stderr, "%.4x: ", 0);
1364 	for (i = 0, j = 0; i < len; i++, p++) {
1365 		(void)fprintf(stderr, "%.2x ", *p);
1366 		abuf[j++] = isprint(*p) ? *p : '.';
1367 		if (j == 16) {
1368 			j = 0;
1369 			abuf[15] = '\0';
1370 			(void)fprintf(stderr, "%s\n%.4" SIZE_T_FORMAT "x: ",
1371 			    abuf, i + 1);
1372 		}
1373 	}
1374 	(void)fprintf(stderr, "\n");
1375 }
1376 
1377 void
cdf_dump_stream(const cdf_stream_t * sst)1378 cdf_dump_stream(const cdf_stream_t *sst)
1379 {
1380 	size_t ss = sst->sst_ss;
1381 	cdf_dump(sst->sst_tab, ss * sst->sst_len);
1382 }
1383 
1384 void
cdf_dump_dir(const cdf_info_t * info,const cdf_header_t * h,const cdf_sat_t * sat,const cdf_sat_t * ssat,const cdf_stream_t * sst,const cdf_dir_t * dir)1385 cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
1386     const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
1387     const cdf_dir_t *dir)
1388 {
1389 	size_t i, j;
1390 	cdf_directory_t *d;
1391 	char name[__arraycount(d->d_name)];
1392 	cdf_stream_t scn;
1393 	struct timeval ts;
1394 
1395 	static const char *types[] = { "empty", "user storage",
1396 	    "user stream", "lockbytes", "property", "root storage" };
1397 
1398 	for (i = 0; i < dir->dir_len; i++) {
1399 		char buf[26];
1400 		d = &dir->dir_tab[i];
1401 		for (j = 0; j < sizeof(name); j++)
1402 			name[j] = (char)CDF_TOLE2(d->d_name[j]);
1403 		(void)fprintf(stderr, "Directory %" SIZE_T_FORMAT "u: %s\n",
1404 		    i, name);
1405 		if (d->d_type < __arraycount(types))
1406 			(void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
1407 		else
1408 			(void)fprintf(stderr, "Type: %d\n", d->d_type);
1409 		(void)fprintf(stderr, "Color: %s\n",
1410 		    d->d_color ? "black" : "red");
1411 		(void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
1412 		(void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
1413 		(void)fprintf(stderr, "Flags: %#x\n", d->d_flags);
1414 		cdf_timestamp_to_timespec(&ts, d->d_created);
1415 		(void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec, buf));
1416 		cdf_timestamp_to_timespec(&ts, d->d_modified);
1417 		(void)fprintf(stderr, "Modified %s",
1418 		    cdf_ctime(&ts.tv_sec, buf));
1419 		(void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
1420 		(void)fprintf(stderr, "Size %d\n", d->d_size);
1421 		switch (d->d_type) {
1422 		case CDF_DIR_TYPE_USER_STORAGE:
1423 			(void)fprintf(stderr, "Storage: %d\n", d->d_storage);
1424 			break;
1425 		case CDF_DIR_TYPE_USER_STREAM:
1426 			if (sst == NULL)
1427 				break;
1428 			if (cdf_read_sector_chain(info, h, sat, ssat, sst,
1429 			    d->d_stream_first_sector, d->d_size, &scn) == -1) {
1430 				warn("Can't read stream for %s at %d len %d",
1431 				    name, d->d_stream_first_sector, d->d_size);
1432 				break;
1433 			}
1434 			cdf_dump_stream(&scn);
1435 			free(scn.sst_tab);
1436 			break;
1437 		default:
1438 			break;
1439 		}
1440 
1441 	}
1442 }
1443 
1444 void
cdf_dump_property_info(const cdf_property_info_t * info,size_t count)1445 cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
1446 {
1447 	cdf_timestamp_t tp;
1448 	struct timeval ts;
1449 	char buf[64];
1450 	size_t i, j;
1451 
1452 	for (i = 0; i < count; i++) {
1453 		cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
1454 		(void)fprintf(stderr, "%" SIZE_T_FORMAT "u) %s: ", i, buf);
1455 		switch (info[i].pi_type) {
1456 		case CDF_NULL:
1457 			break;
1458 		case CDF_SIGNED16:
1459 			(void)fprintf(stderr, "signed 16 [%hd]\n",
1460 			    info[i].pi_s16);
1461 			break;
1462 		case CDF_SIGNED32:
1463 			(void)fprintf(stderr, "signed 32 [%d]\n",
1464 			    info[i].pi_s32);
1465 			break;
1466 		case CDF_UNSIGNED32:
1467 			(void)fprintf(stderr, "unsigned 32 [%u]\n",
1468 			    info[i].pi_u32);
1469 			break;
1470 		case CDF_FLOAT:
1471 			(void)fprintf(stderr, "float [%g]\n",
1472 			    info[i].pi_f);
1473 			break;
1474 		case CDF_DOUBLE:
1475 			(void)fprintf(stderr, "double [%g]\n",
1476 			    info[i].pi_d);
1477 			break;
1478 		case CDF_LENGTH32_STRING:
1479 			(void)fprintf(stderr, "string %u [%.*s]\n",
1480 			    info[i].pi_str.s_len,
1481 			    info[i].pi_str.s_len, info[i].pi_str.s_buf);
1482 			break;
1483 		case CDF_LENGTH32_WSTRING:
1484 			(void)fprintf(stderr, "string %u [",
1485 			    info[i].pi_str.s_len);
1486 			for (j = 0; j < info[i].pi_str.s_len - 1; j++)
1487 			    (void)fputc(info[i].pi_str.s_buf[j << 1], stderr);
1488 			(void)fprintf(stderr, "]\n");
1489 			break;
1490 		case CDF_FILETIME:
1491 			tp = info[i].pi_tp;
1492 			if (tp < 1000000000000000LL) {
1493 				cdf_print_elapsed_time(buf, sizeof(buf), tp);
1494 				(void)fprintf(stderr, "timestamp %s\n", buf);
1495 			} else {
1496 				char tbuf[26];
1497 				cdf_timestamp_to_timespec(&ts, tp);
1498 				(void)fprintf(stderr, "timestamp %s",
1499 				    cdf_ctime(&ts.tv_sec, tbuf));
1500 			}
1501 			break;
1502 		case CDF_CLIPBOARD:
1503 			(void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
1504 			break;
1505 		default:
1506 			DPRINTF(("Don't know how to deal with %#x\n",
1507 			    info[i].pi_type));
1508 			break;
1509 		}
1510 	}
1511 }
1512 
1513 
1514 void
cdf_dump_summary_info(const cdf_header_t * h,const cdf_stream_t * sst)1515 cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
1516 {
1517 	char buf[128];
1518 	cdf_summary_info_header_t ssi;
1519 	cdf_property_info_t *info;
1520 	size_t count;
1521 
1522 	(void)&h;
1523 	if (cdf_unpack_summary_info(sst, h, &ssi, &info, &count) == -1)
1524 		return;
1525 	(void)fprintf(stderr, "Endian: %#x\n", ssi.si_byte_order);
1526 	(void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
1527 	    ssi.si_os_version >> 8);
1528 	(void)fprintf(stderr, "Os %d\n", ssi.si_os);
1529 	cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
1530 	(void)fprintf(stderr, "Class %s\n", buf);
1531 	(void)fprintf(stderr, "Count %d\n", ssi.si_count);
1532 	cdf_dump_property_info(info, count);
1533 	free(info);
1534 }
1535 
1536 
1537 void
cdf_dump_catalog(const cdf_header_t * h,const cdf_stream_t * sst)1538 cdf_dump_catalog(const cdf_header_t *h, const cdf_stream_t *sst)
1539 {
1540 	cdf_catalog_t *cat;
1541 	cdf_unpack_catalog(h, sst, &cat);
1542 	const cdf_catalog_entry_t *ce = cat->cat_e;
1543 	struct timespec ts;
1544 	char tbuf[64], sbuf[256];
1545 	size_t i;
1546 
1547 	printf("Catalog:\n");
1548 	for (i = 0; i < cat->cat_num; i++) {
1549 		cdf_timestamp_to_timespec(&ts, ce[i].ce_timestamp);
1550 		printf("\t%d %s %s", ce[i].ce_num,
1551 		    cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
1552 		    cdf_ctime(&ts.tv_sec, tbuf));
1553 	}
1554 	free(cat);
1555 }
1556 
1557 #endif
1558 
1559 #ifdef TEST
1560 int
main(int argc,char * argv[])1561 main(int argc, char *argv[])
1562 {
1563 	int i;
1564 	cdf_header_t h;
1565 	cdf_sat_t sat, ssat;
1566 	cdf_stream_t sst, scn;
1567 	cdf_dir_t dir;
1568 	cdf_info_t info;
1569 	const cdf_directory_t *root;
1570 
1571 	if (argc < 2) {
1572 		(void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
1573 		return -1;
1574 	}
1575 
1576 	info.i_buf = NULL;
1577 	info.i_len = 0;
1578 	for (i = 1; i < argc; i++) {
1579 		if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
1580 			err(1, "Cannot open `%s'", argv[1]);
1581 
1582 		if (cdf_read_header(&info, &h) == -1)
1583 			err(1, "Cannot read header");
1584 #ifdef CDF_DEBUG
1585 		cdf_dump_header(&h);
1586 #endif
1587 
1588 		if (cdf_read_sat(&info, &h, &sat) == -1)
1589 			err(1, "Cannot read sat");
1590 #ifdef CDF_DEBUG
1591 		cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
1592 #endif
1593 
1594 		if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
1595 			err(1, "Cannot read ssat");
1596 #ifdef CDF_DEBUG
1597 		cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
1598 #endif
1599 
1600 		if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
1601 			err(1, "Cannot read dir");
1602 
1603 		if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst, &root)
1604 		    == -1)
1605 			err(1, "Cannot read short stream");
1606 #ifdef CDF_DEBUG
1607 		cdf_dump_stream(&sst);
1608 #endif
1609 
1610 #ifdef CDF_DEBUG
1611 		cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
1612 #endif
1613 
1614 
1615 		if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
1616 		    &scn) == -1)
1617 			warn("Cannot read summary info");
1618 #ifdef CDF_DEBUG
1619 		else
1620 			cdf_dump_summary_info(&h, &scn);
1621 #endif
1622 		if (cdf_read_user_stream(&info, &h, &sat, &ssat, &sst,
1623 		    &dir, "Catalog", &scn) == -1)
1624 			warn("Cannot read catalog");
1625 #ifdef CDF_DEBUG
1626 		else
1627 			cdf_dump_catalog(&h, &scn);
1628 #endif
1629 
1630 		(void)close(info.i_fd);
1631 	}
1632 
1633 	return 0;
1634 }
1635 #endif
1636