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