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