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