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