2 * Copyright (c) 2008 Christos Zoulas
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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
31 * N.B. This is the "Composite Document File" format, and not the
32 * "Compound Document Format", nor the "Channel Definition Format".
38 FILE_RCSID("@(#)$File: cdf.c,v 1.50 2012/02/20 22:35:29 christos Exp $")
61 #define DPRINTF(a) printf a, fflush(stdout)
71 #define NEED_SWAP (cdf_bo.u == (uint32_t)0x01020304)
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_GETUINT32(x, y) cdf_getuint32(x, y)
83 _cdf_tole2(uint16_t sv)
86 uint8_t *s = (uint8_t *)(void *)&sv;
87 uint8_t *d = (uint8_t *)(void *)&rv;
97 _cdf_tole4(uint32_t sv)
100 uint8_t *s = (uint8_t *)(void *)&sv;
101 uint8_t *d = (uint8_t *)(void *)&rv;
113 _cdf_tole8(uint64_t sv)
116 uint8_t *s = (uint8_t *)(void *)&sv;
117 uint8_t *d = (uint8_t *)(void *)&rv;
130 * grab a uint32_t from a possibly unaligned address, and return it in
131 * the native host order.
134 cdf_getuint32(const uint8_t *p, size_t offs)
137 (void)memcpy(&rv, p + offs * sizeof(uint32_t), sizeof(rv));
138 return CDF_TOLE4(rv);
141 #define CDF_UNPACK(a) \
142 (void)memcpy(&(a), &buf[len], sizeof(a)), len += sizeof(a)
143 #define CDF_UNPACKA(a) \
144 (void)memcpy((a), &buf[len], sizeof(a)), len += sizeof(a)
147 cdf_tole2(uint16_t sv)
149 return CDF_TOLE2(sv);
153 cdf_tole4(uint32_t sv)
155 return CDF_TOLE4(sv);
159 cdf_tole8(uint64_t sv)
161 return CDF_TOLE8(sv);
165 cdf_swap_header(cdf_header_t *h)
169 h->h_magic = CDF_TOLE8(h->h_magic);
170 h->h_uuid[0] = CDF_TOLE8(h->h_uuid[0]);
171 h->h_uuid[1] = CDF_TOLE8(h->h_uuid[1]);
172 h->h_revision = CDF_TOLE2(h->h_revision);
173 h->h_version = CDF_TOLE2(h->h_version);
174 h->h_byte_order = CDF_TOLE2(h->h_byte_order);
175 h->h_sec_size_p2 = CDF_TOLE2(h->h_sec_size_p2);
176 h->h_short_sec_size_p2 = CDF_TOLE2(h->h_short_sec_size_p2);
177 h->h_num_sectors_in_sat = CDF_TOLE4(h->h_num_sectors_in_sat);
178 h->h_secid_first_directory = CDF_TOLE4(h->h_secid_first_directory);
179 h->h_min_size_standard_stream =
180 CDF_TOLE4(h->h_min_size_standard_stream);
181 h->h_secid_first_sector_in_short_sat =
182 CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_short_sat);
183 h->h_num_sectors_in_short_sat =
184 CDF_TOLE4(h->h_num_sectors_in_short_sat);
185 h->h_secid_first_sector_in_master_sat =
186 CDF_TOLE4((uint32_t)h->h_secid_first_sector_in_master_sat);
187 h->h_num_sectors_in_master_sat =
188 CDF_TOLE4(h->h_num_sectors_in_master_sat);
189 for (i = 0; i < __arraycount(h->h_master_sat); i++)
190 h->h_master_sat[i] = CDF_TOLE4((uint32_t)h->h_master_sat[i]);
194 cdf_unpack_header(cdf_header_t *h, char *buf)
199 CDF_UNPACK(h->h_magic);
200 CDF_UNPACKA(h->h_uuid);
201 CDF_UNPACK(h->h_revision);
202 CDF_UNPACK(h->h_version);
203 CDF_UNPACK(h->h_byte_order);
204 CDF_UNPACK(h->h_sec_size_p2);
205 CDF_UNPACK(h->h_short_sec_size_p2);
206 CDF_UNPACKA(h->h_unused0);
207 CDF_UNPACK(h->h_num_sectors_in_sat);
208 CDF_UNPACK(h->h_secid_first_directory);
209 CDF_UNPACKA(h->h_unused1);
210 CDF_UNPACK(h->h_min_size_standard_stream);
211 CDF_UNPACK(h->h_secid_first_sector_in_short_sat);
212 CDF_UNPACK(h->h_num_sectors_in_short_sat);
213 CDF_UNPACK(h->h_secid_first_sector_in_master_sat);
214 CDF_UNPACK(h->h_num_sectors_in_master_sat);
215 for (i = 0; i < __arraycount(h->h_master_sat); i++)
216 CDF_UNPACK(h->h_master_sat[i]);
220 cdf_swap_dir(cdf_directory_t *d)
222 d->d_namelen = CDF_TOLE2(d->d_namelen);
223 d->d_left_child = CDF_TOLE4((uint32_t)d->d_left_child);
224 d->d_right_child = CDF_TOLE4((uint32_t)d->d_right_child);
225 d->d_storage = CDF_TOLE4((uint32_t)d->d_storage);
226 d->d_storage_uuid[0] = CDF_TOLE8(d->d_storage_uuid[0]);
227 d->d_storage_uuid[1] = CDF_TOLE8(d->d_storage_uuid[1]);
228 d->d_flags = CDF_TOLE4(d->d_flags);
229 d->d_created = CDF_TOLE8((uint64_t)d->d_created);
230 d->d_modified = CDF_TOLE8((uint64_t)d->d_modified);
231 d->d_stream_first_sector = CDF_TOLE4((uint32_t)d->d_stream_first_sector);
232 d->d_size = CDF_TOLE4(d->d_size);
236 cdf_swap_class(cdf_classid_t *d)
238 d->cl_dword = CDF_TOLE4(d->cl_dword);
239 d->cl_word[0] = CDF_TOLE2(d->cl_word[0]);
240 d->cl_word[1] = CDF_TOLE2(d->cl_word[1]);
244 cdf_unpack_dir(cdf_directory_t *d, char *buf)
248 CDF_UNPACKA(d->d_name);
249 CDF_UNPACK(d->d_namelen);
250 CDF_UNPACK(d->d_type);
251 CDF_UNPACK(d->d_color);
252 CDF_UNPACK(d->d_left_child);
253 CDF_UNPACK(d->d_right_child);
254 CDF_UNPACK(d->d_storage);
255 CDF_UNPACKA(d->d_storage_uuid);
256 CDF_UNPACK(d->d_flags);
257 CDF_UNPACK(d->d_created);
258 CDF_UNPACK(d->d_modified);
259 CDF_UNPACK(d->d_stream_first_sector);
260 CDF_UNPACK(d->d_size);
261 CDF_UNPACK(d->d_unused0);
265 cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h,
266 const void *p, size_t tail, int line)
268 const char *b = (const char *)sst->sst_tab;
269 const char *e = ((const char *)p) + tail;
271 if (e >= b && (size_t)(e - b) < CDF_SEC_SIZE(h) * sst->sst_len)
273 DPRINTF(("%d: offset begin %p end %p %" SIZE_T_FORMAT "u"
274 " >= %" SIZE_T_FORMAT "u [%" SIZE_T_FORMAT "u %"
275 SIZE_T_FORMAT "u]\n", line, b, e, (size_t)(e - b),
276 CDF_SEC_SIZE(h) * sst->sst_len, CDF_SEC_SIZE(h), sst->sst_len));
282 cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
284 size_t siz = (size_t)off + len;
286 if ((off_t)(off + len) != (off_t)siz) {
291 if (info->i_buf != NULL && info->i_len >= siz) {
292 (void)memcpy(buf, &info->i_buf[off], len);
296 if (info->i_fd == -1)
299 if (lseek(info->i_fd, off, SEEK_SET) == (off_t)-1)
302 if (read(info->i_fd, buf, len) != (ssize_t)len)
309 cdf_read_header(const cdf_info_t *info, cdf_header_t *h)
313 (void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
314 if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1)
316 cdf_unpack_header(h, buf);
318 if (h->h_magic != CDF_MAGIC) {
319 DPRINTF(("Bad magic 0x%" INT64_T_FORMAT "x != 0x%"
320 INT64_T_FORMAT "x\n",
321 (unsigned long long)h->h_magic,
322 (unsigned long long)CDF_MAGIC));
325 if (h->h_sec_size_p2 > 20) {
326 DPRINTF(("Bad sector size 0x%u\n", h->h_sec_size_p2));
329 if (h->h_short_sec_size_p2 > 20) {
330 DPRINTF(("Bad short sector size 0x%u\n",
331 h->h_short_sec_size_p2));
342 cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
343 const cdf_header_t *h, cdf_secid_t id)
345 size_t ss = CDF_SEC_SIZE(h);
346 size_t pos = CDF_SEC_POS(h, id);
348 return cdf_read(info, (off_t)pos, ((char *)buf) + offs, len);
352 cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
353 size_t len, const cdf_header_t *h, cdf_secid_t id)
355 size_t ss = CDF_SHORT_SEC_SIZE(h);
356 size_t pos = CDF_SHORT_SEC_POS(h, id);
358 if (pos > CDF_SEC_SIZE(h) * sst->sst_len) {
359 DPRINTF(("Out of bounds read %" SIZE_T_FORMAT "u > %"
361 pos, CDF_SEC_SIZE(h) * sst->sst_len));
364 (void)memcpy(((char *)buf) + offs,
365 ((const char *)sst->sst_tab) + pos, len);
370 * Read the sector allocation table.
373 cdf_read_sat(const cdf_info_t *info, cdf_header_t *h, cdf_sat_t *sat)
376 size_t ss = CDF_SEC_SIZE(h);
377 cdf_secid_t *msa, mid, sec;
378 size_t nsatpersec = (ss / sizeof(mid)) - 1;
380 for (i = 0; i < __arraycount(h->h_master_sat); i++)
381 if (h->h_master_sat[i] == CDF_SECID_FREE)
384 #define CDF_SEC_LIMIT (UINT32_MAX / (4 * ss))
385 if ((nsatpersec > 0 &&
386 h->h_num_sectors_in_master_sat > CDF_SEC_LIMIT / nsatpersec) ||
388 DPRINTF(("Number of sectors in master SAT too big %u %"
389 SIZE_T_FORMAT "u\n", h->h_num_sectors_in_master_sat, i));
394 sat->sat_len = h->h_num_sectors_in_master_sat * nsatpersec + i;
395 DPRINTF(("sat_len = %" SIZE_T_FORMAT "u ss = %" SIZE_T_FORMAT "u\n",
397 if ((sat->sat_tab = CAST(cdf_secid_t *, calloc(sat->sat_len, ss)))
401 for (i = 0; i < __arraycount(h->h_master_sat); i++) {
402 if (h->h_master_sat[i] < 0)
404 if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
405 h->h_master_sat[i]) != (ssize_t)ss) {
406 DPRINTF(("Reading sector %d", h->h_master_sat[i]));
411 if ((msa = CAST(cdf_secid_t *, calloc(1, ss))) == NULL)
414 mid = h->h_secid_first_sector_in_master_sat;
415 for (j = 0; j < h->h_num_sectors_in_master_sat; j++) {
418 if (j >= CDF_LOOP_LIMIT) {
419 DPRINTF(("Reading master sector loop limit"));
423 if (cdf_read_sector(info, msa, 0, ss, h, mid) != (ssize_t)ss) {
424 DPRINTF(("Reading master sector %d", mid));
427 for (k = 0; k < nsatpersec; k++, i++) {
428 sec = CDF_TOLE4((uint32_t)msa[k]);
431 if (i >= sat->sat_len) {
432 DPRINTF(("Out of bounds reading MSA %" SIZE_T_FORMAT
433 "u >= %" SIZE_T_FORMAT "u", i, sat->sat_len));
437 if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
438 sec) != (ssize_t)ss) {
439 DPRINTF(("Reading sector %d",
444 mid = CDF_TOLE4((uint32_t)msa[nsatpersec]);
458 cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
461 cdf_secid_t maxsector = (cdf_secid_t)(sat->sat_len * size);
464 for (j = i = 0; sid >= 0; i++, j++) {
465 DPRINTF((" %d", sid));
466 if (j >= CDF_LOOP_LIMIT) {
467 DPRINTF(("Counting chain loop limit"));
471 if (sid > maxsector) {
472 DPRINTF(("Sector %d > %d\n", sid, maxsector));
476 sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
483 cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
484 const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
486 size_t ss = CDF_SEC_SIZE(h), i, j;
488 scn->sst_len = cdf_count_chain(sat, sid, ss);
489 scn->sst_dirlen = len;
491 if (scn->sst_len == (size_t)-1)
494 scn->sst_tab = calloc(scn->sst_len, ss);
495 if (scn->sst_tab == NULL)
498 for (j = i = 0; sid >= 0; i++, j++) {
499 if (j >= CDF_LOOP_LIMIT) {
500 DPRINTF(("Read long sector chain loop limit"));
504 if (i >= scn->sst_len) {
505 DPRINTF(("Out of bounds reading long sector chain "
506 "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
511 if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
512 sid)) != (ssize_t)ss) {
513 if (i == scn->sst_len - 1 && nr > 0) {
514 /* Last sector might be truncated */
517 DPRINTF(("Reading long sector chain %d", sid));
520 sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
529 cdf_read_short_sector_chain(const cdf_header_t *h,
530 const cdf_sat_t *ssat, const cdf_stream_t *sst,
531 cdf_secid_t sid, size_t len, cdf_stream_t *scn)
533 size_t ss = CDF_SHORT_SEC_SIZE(h), i, j;
534 scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h));
535 scn->sst_dirlen = len;
537 if (sst->sst_tab == NULL || scn->sst_len == (size_t)-1)
540 scn->sst_tab = calloc(scn->sst_len, ss);
541 if (scn->sst_tab == NULL)
544 for (j = i = 0; sid >= 0; i++, j++) {
545 if (j >= CDF_LOOP_LIMIT) {
546 DPRINTF(("Read short sector chain loop limit"));
550 if (i >= scn->sst_len) {
551 DPRINTF(("Out of bounds reading short sector chain "
552 "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n",
557 if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
558 sid) != (ssize_t)ss) {
559 DPRINTF(("Reading short sector chain %d", sid));
562 sid = CDF_TOLE4((uint32_t)ssat->sat_tab[sid]);
571 cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
572 const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
573 cdf_secid_t sid, size_t len, cdf_stream_t *scn)
576 if (len < h->h_min_size_standard_stream && sst->sst_tab != NULL)
577 return cdf_read_short_sector_chain(h, ssat, sst, sid, len,
580 return cdf_read_long_sector_chain(info, h, sat, sid, len, scn);
584 cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h,
585 const cdf_sat_t *sat, cdf_dir_t *dir)
588 size_t ss = CDF_SEC_SIZE(h), ns, nd;
590 cdf_secid_t sid = h->h_secid_first_directory;
592 ns = cdf_count_chain(sat, sid, ss);
593 if (ns == (size_t)-1)
596 nd = ss / CDF_DIRECTORY_SIZE;
598 dir->dir_len = ns * nd;
599 dir->dir_tab = CAST(cdf_directory_t *,
600 calloc(dir->dir_len, sizeof(dir->dir_tab[0])));
601 if (dir->dir_tab == NULL)
604 if ((buf = CAST(char *, malloc(ss))) == NULL) {
609 for (j = i = 0; i < ns; i++, j++) {
610 if (j >= CDF_LOOP_LIMIT) {
611 DPRINTF(("Read dir loop limit"));
615 if (cdf_read_sector(info, buf, 0, ss, h, sid) != (ssize_t)ss) {
616 DPRINTF(("Reading directory sector %d", sid));
619 for (j = 0; j < nd; j++) {
620 cdf_unpack_dir(&dir->dir_tab[i * nd + j],
621 &buf[j * CDF_DIRECTORY_SIZE]);
623 sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
626 for (i = 0; i < dir->dir_len; i++)
627 cdf_swap_dir(&dir->dir_tab[i]);
638 cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h,
639 const cdf_sat_t *sat, cdf_sat_t *ssat)
642 size_t ss = CDF_SEC_SIZE(h);
643 cdf_secid_t sid = h->h_secid_first_sector_in_short_sat;
645 ssat->sat_len = cdf_count_chain(sat, sid, CDF_SEC_SIZE(h));
646 if (ssat->sat_len == (size_t)-1)
649 ssat->sat_tab = CAST(cdf_secid_t *, calloc(ssat->sat_len, ss));
650 if (ssat->sat_tab == NULL)
653 for (j = i = 0; sid >= 0; i++, j++) {
654 if (j >= CDF_LOOP_LIMIT) {
655 DPRINTF(("Read short sat sector loop limit"));
659 if (i >= ssat->sat_len) {
660 DPRINTF(("Out of bounds reading short sector chain "
661 "%" SIZE_T_FORMAT "u > %" SIZE_T_FORMAT "u\n", i,
666 if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
668 DPRINTF(("Reading short sat sector %d", sid));
671 sid = CDF_TOLE4((uint32_t)sat->sat_tab[sid]);
680 cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
681 const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn)
684 const cdf_directory_t *d;
686 for (i = 0; i < dir->dir_len; i++)
687 if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
690 /* If the it is not there, just fake it; some docs don't have it */
691 if (i == dir->dir_len)
693 d = &dir->dir_tab[i];
695 /* If the it is not there, just fake it; some docs don't have it */
696 if (d->d_stream_first_sector < 0)
699 return cdf_read_long_sector_chain(info, h, sat,
700 d->d_stream_first_sector, d->d_size, scn);
709 cdf_namecmp(const char *d, const uint16_t *s, size_t l)
711 for (; l--; d++, s++)
712 if (*d != CDF_TOLE2(*s))
713 return (unsigned char)*d - CDF_TOLE2(*s);
718 cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
719 const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
720 const cdf_dir_t *dir, cdf_stream_t *scn)
723 const cdf_directory_t *d;
724 static const char name[] = "\05SummaryInformation";
726 for (i = dir->dir_len; i > 0; i--)
727 if (dir->dir_tab[i - 1].d_type == CDF_DIR_TYPE_USER_STREAM &&
728 cdf_namecmp(name, dir->dir_tab[i - 1].d_name, sizeof(name))
733 DPRINTF(("Cannot find summary information section\n"));
737 d = &dir->dir_tab[i - 1];
738 return cdf_read_sector_chain(info, h, sat, ssat, sst,
739 d->d_stream_first_sector, d->d_size, scn);
743 cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
744 uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
746 const cdf_section_header_t *shp;
747 cdf_section_header_t sh;
748 const uint8_t *p, *q, *e;
755 size_t i, o, o4, nelements, j;
756 cdf_property_info_t *inp;
758 if (offs > UINT32_MAX / 4) {
762 shp = CAST(const cdf_section_header_t *, (const void *)
763 ((const char *)sst->sst_tab + offs));
764 if (cdf_check_stream_offset(sst, h, shp, sizeof(*shp), __LINE__) == -1)
766 sh.sh_len = CDF_TOLE4(shp->sh_len);
767 #define CDF_SHLEN_LIMIT (UINT32_MAX / 8)
768 if (sh.sh_len > CDF_SHLEN_LIMIT) {
772 sh.sh_properties = CDF_TOLE4(shp->sh_properties);
773 #define CDF_PROP_LIMIT (UINT32_MAX / (4 * sizeof(*inp)))
774 if (sh.sh_properties > CDF_PROP_LIMIT)
776 DPRINTF(("section len: %u properties %u\n", sh.sh_len,
779 if (*maxcount > CDF_PROP_LIMIT)
781 *maxcount += sh.sh_properties;
782 inp = CAST(cdf_property_info_t *,
783 realloc(*info, *maxcount * sizeof(*inp)));
785 *maxcount = sh.sh_properties;
786 inp = CAST(cdf_property_info_t *,
787 malloc(*maxcount * sizeof(*inp)));
793 *count += sh.sh_properties;
794 p = CAST(const uint8_t *, (const void *)
795 ((const char *)(const void *)sst->sst_tab +
797 e = CAST(const uint8_t *, (const void *)
798 (((const char *)(const void *)shp) + sh.sh_len));
799 if (cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1)
801 for (i = 0; i < sh.sh_properties; i++) {
802 size_t ofs = CDF_GETUINT32(p, (i << 1) + 1);
803 q = (const uint8_t *)(const void *)
804 ((const char *)(const void *)p + ofs
805 - 2 * sizeof(uint32_t));
807 DPRINTF(("Ran of the end %p > %p\n", q, e));
810 inp[i].pi_id = CDF_GETUINT32(p, i << 1);
811 inp[i].pi_type = CDF_GETUINT32(q, 0);
812 DPRINTF(("%" SIZE_T_FORMAT "u) id=%x type=%x offs=0x%tx,0x%x\n",
813 i, inp[i].pi_id, inp[i].pi_type, q - p, offs));
814 if (inp[i].pi_type & CDF_VECTOR) {
815 nelements = CDF_GETUINT32(q, 1);
821 o4 = o * sizeof(uint32_t);
822 if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
824 switch (inp[i].pi_type & CDF_TYPEMASK) {
829 if (inp[i].pi_type & CDF_VECTOR)
831 (void)memcpy(&s16, &q[o4], sizeof(s16));
832 inp[i].pi_s16 = CDF_TOLE2(s16);
835 if (inp[i].pi_type & CDF_VECTOR)
837 (void)memcpy(&s32, &q[o4], sizeof(s32));
838 inp[i].pi_s32 = CDF_TOLE4((uint32_t)s32);
842 if (inp[i].pi_type & CDF_VECTOR)
844 (void)memcpy(&u32, &q[o4], sizeof(u32));
845 inp[i].pi_u32 = CDF_TOLE4(u32);
848 if (inp[i].pi_type & CDF_VECTOR)
850 (void)memcpy(&s64, &q[o4], sizeof(s64));
851 inp[i].pi_s64 = CDF_TOLE8((uint64_t)s64);
854 if (inp[i].pi_type & CDF_VECTOR)
856 (void)memcpy(&u64, &q[o4], sizeof(u64));
857 inp[i].pi_u64 = CDF_TOLE8((uint64_t)u64);
860 if (inp[i].pi_type & CDF_VECTOR)
862 (void)memcpy(&u32, &q[o4], sizeof(u32));
863 u32 = CDF_TOLE4(u32);
864 memcpy(&inp[i].pi_f, &u32, sizeof(inp[i].pi_f));
867 if (inp[i].pi_type & CDF_VECTOR)
869 (void)memcpy(&u64, &q[o4], sizeof(u64));
870 u64 = CDF_TOLE8((uint64_t)u64);
871 memcpy(&inp[i].pi_d, &u64, sizeof(inp[i].pi_d));
873 case CDF_LENGTH32_STRING:
874 case CDF_LENGTH32_WSTRING:
876 size_t nelem = inp - *info;
877 if (*maxcount > CDF_PROP_LIMIT
878 || nelements > CDF_PROP_LIMIT)
880 *maxcount += nelements;
881 inp = CAST(cdf_property_info_t *,
882 realloc(*info, *maxcount * sizeof(*inp)));
888 DPRINTF(("nelements = %" SIZE_T_FORMAT "u\n",
890 for (j = 0; j < nelements; j++, i++) {
891 uint32_t l = CDF_GETUINT32(q, o);
892 inp[i].pi_str.s_len = l;
893 inp[i].pi_str.s_buf = (const char *)
894 (const void *)(&q[o4 + sizeof(l)]);
895 DPRINTF(("l = %d, r = %" SIZE_T_FORMAT
897 CDF_ROUND(l, sizeof(l)),
898 inp[i].pi_str.s_buf));
904 o4 = o * sizeof(uint32_t);
909 if (inp[i].pi_type & CDF_VECTOR)
911 (void)memcpy(&tp, &q[o4], sizeof(tp));
912 inp[i].pi_tp = CDF_TOLE8((uint64_t)tp);
915 if (inp[i].pi_type & CDF_VECTOR)
920 DPRINTF(("Don't know how to deal with %x\n",
932 cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h,
933 cdf_summary_info_header_t *ssi, cdf_property_info_t **info, size_t *count)
936 const cdf_summary_info_header_t *si =
937 CAST(const cdf_summary_info_header_t *, sst->sst_tab);
938 const cdf_section_declaration_t *sd =
939 CAST(const cdf_section_declaration_t *, (const void *)
940 ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET));
942 if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 ||
943 cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1)
945 ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
946 ssi->si_os_version = CDF_TOLE2(si->si_os_version);
947 ssi->si_os = CDF_TOLE2(si->si_os);
948 ssi->si_class = si->si_class;
949 cdf_swap_class(&ssi->si_class);
950 ssi->si_count = CDF_TOLE2(si->si_count);
954 for (i = 0; i < CDF_TOLE4(si->si_count); i++) {
955 if (i >= CDF_LOOP_LIMIT) {
956 DPRINTF(("Unpack summary info loop limit"));
960 if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset),
961 info, count, &maxcount) == -1) {
971 cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
973 return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
974 "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
975 id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
976 id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
980 static const struct {
984 { CDF_PROPERTY_CODE_PAGE, "Code page" },
985 { CDF_PROPERTY_TITLE, "Title" },
986 { CDF_PROPERTY_SUBJECT, "Subject" },
987 { CDF_PROPERTY_AUTHOR, "Author" },
988 { CDF_PROPERTY_KEYWORDS, "Keywords" },
989 { CDF_PROPERTY_COMMENTS, "Comments" },
990 { CDF_PROPERTY_TEMPLATE, "Template" },
991 { CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
992 { CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
993 { CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
994 { CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
995 { CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
996 { CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
997 { CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
998 { CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
999 { CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
1000 { CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
1001 { CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
1002 { CDF_PROPERTY_SECURITY, "Security" },
1003 { CDF_PROPERTY_LOCALE_ID, "Locale ID" },
1007 cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
1011 for (i = 0; i < __arraycount(vn); i++)
1013 return snprintf(buf, bufsiz, "%s", vn[i].n);
1014 return snprintf(buf, bufsiz, "0x%x", p);
1018 cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
1021 int days, hours, mins, secs;
1023 ts /= CDF_TIME_PREC;
1024 secs = (int)(ts % 60);
1026 mins = (int)(ts % 60);
1028 hours = (int)(ts % 24);
1033 len += snprintf(buf + len, bufsiz - len, "%dd+", days);
1034 if ((size_t)len >= bufsiz)
1038 if (days || hours) {
1039 len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
1040 if ((size_t)len >= bufsiz)
1044 len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
1045 if ((size_t)len >= bufsiz)
1048 len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
1055 cdf_dump_header(const cdf_header_t *h)
1059 #define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
1060 #define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \
1061 h->h_ ## b, 1 << h->h_ ## b)
1062 DUMP("%d", revision);
1063 DUMP("%d", version);
1064 DUMP("0x%x", byte_order);
1065 DUMP2("%d", sec_size_p2);
1066 DUMP2("%d", short_sec_size_p2);
1067 DUMP("%d", num_sectors_in_sat);
1068 DUMP("%d", secid_first_directory);
1069 DUMP("%d", min_size_standard_stream);
1070 DUMP("%d", secid_first_sector_in_short_sat);
1071 DUMP("%d", num_sectors_in_short_sat);
1072 DUMP("%d", secid_first_sector_in_master_sat);
1073 DUMP("%d", num_sectors_in_master_sat);
1074 for (i = 0; i < __arraycount(h->h_master_sat); i++) {
1075 if (h->h_master_sat[i] == CDF_SECID_FREE)
1077 (void)fprintf(stderr, "%35.35s[%.3zu] = %d\n",
1078 "master_sat", i, h->h_master_sat[i]);
1083 cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
1085 size_t i, j, s = size / sizeof(cdf_secid_t);
1087 for (i = 0; i < sat->sat_len; i++) {
1088 (void)fprintf(stderr, "%s[%" SIZE_T_FORMAT "u]:\n%.6"
1089 SIZE_T_FORMAT "u: ", prefix, i, i * s);
1090 for (j = 0; j < s; j++) {
1091 (void)fprintf(stderr, "%5d, ",
1092 CDF_TOLE4(sat->sat_tab[s * i + j]));
1093 if ((j + 1) % 10 == 0)
1094 (void)fprintf(stderr, "\n%.6" SIZE_T_FORMAT
1095 "u: ", i * s + j + 1);
1097 (void)fprintf(stderr, "\n");
1102 cdf_dump(void *v, size_t len)
1105 unsigned char *p = v;
1107 (void)fprintf(stderr, "%.4x: ", 0);
1108 for (i = 0, j = 0; i < len; i++, p++) {
1109 (void)fprintf(stderr, "%.2x ", *p);
1110 abuf[j++] = isprint(*p) ? *p : '.';
1114 (void)fprintf(stderr, "%s\n%.4" SIZE_T_FORMAT "x: ",
1118 (void)fprintf(stderr, "\n");
1122 cdf_dump_stream(const cdf_header_t *h, const cdf_stream_t *sst)
1124 size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
1125 CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
1126 cdf_dump(sst->sst_tab, ss * sst->sst_len);
1130 cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
1131 const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
1132 const cdf_dir_t *dir)
1136 char name[__arraycount(d->d_name)];
1140 static const char *types[] = { "empty", "user storage",
1141 "user stream", "lockbytes", "property", "root storage" };
1143 for (i = 0; i < dir->dir_len; i++) {
1144 d = &dir->dir_tab[i];
1145 for (j = 0; j < sizeof(name); j++)
1146 name[j] = (char)CDF_TOLE2(d->d_name[j]);
1147 (void)fprintf(stderr, "Directory %" SIZE_T_FORMAT "u: %s\n",
1149 if (d->d_type < __arraycount(types))
1150 (void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
1152 (void)fprintf(stderr, "Type: %d\n", d->d_type);
1153 (void)fprintf(stderr, "Color: %s\n",
1154 d->d_color ? "black" : "red");
1155 (void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
1156 (void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
1157 (void)fprintf(stderr, "Flags: 0x%x\n", d->d_flags);
1158 cdf_timestamp_to_timespec(&ts, d->d_created);
1159 (void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec));
1160 cdf_timestamp_to_timespec(&ts, d->d_modified);
1161 (void)fprintf(stderr, "Modified %s", cdf_ctime(&ts.tv_sec));
1162 (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
1163 (void)fprintf(stderr, "Size %d\n", d->d_size);
1164 switch (d->d_type) {
1165 case CDF_DIR_TYPE_USER_STORAGE:
1166 (void)fprintf(stderr, "Storage: %d\n", d->d_storage);
1168 case CDF_DIR_TYPE_USER_STREAM:
1171 if (cdf_read_sector_chain(info, h, sat, ssat, sst,
1172 d->d_stream_first_sector, d->d_size, &scn) == -1) {
1173 warn("Can't read stream for %s at %d len %d",
1174 name, d->d_stream_first_sector, d->d_size);
1177 cdf_dump_stream(h, &scn);
1188 cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
1195 for (i = 0; i < count; i++) {
1196 cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
1197 (void)fprintf(stderr, "%" SIZE_T_FORMAT "u) %s: ", i, buf);
1198 switch (info[i].pi_type) {
1202 (void)fprintf(stderr, "signed 16 [%hd]\n",
1206 (void)fprintf(stderr, "signed 32 [%d]\n",
1209 case CDF_UNSIGNED32:
1210 (void)fprintf(stderr, "unsigned 32 [%u]\n",
1214 (void)fprintf(stderr, "float [%g]\n",
1218 (void)fprintf(stderr, "double [%g]\n",
1221 case CDF_LENGTH32_STRING:
1222 (void)fprintf(stderr, "string %u [%.*s]\n",
1223 info[i].pi_str.s_len,
1224 info[i].pi_str.s_len, info[i].pi_str.s_buf);
1226 case CDF_LENGTH32_WSTRING:
1227 (void)fprintf(stderr, "string %u [",
1228 info[i].pi_str.s_len);
1229 for (j = 0; j < info[i].pi_str.s_len - 1; j++)
1230 (void)fputc(info[i].pi_str.s_buf[j << 1], stderr);
1231 (void)fprintf(stderr, "]\n");
1235 if (tp < 1000000000000000LL) {
1236 cdf_print_elapsed_time(buf, sizeof(buf), tp);
1237 (void)fprintf(stderr, "timestamp %s\n", buf);
1239 cdf_timestamp_to_timespec(&ts, tp);
1240 (void)fprintf(stderr, "timestamp %s",
1241 cdf_ctime(&ts.tv_sec));
1245 (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
1248 DPRINTF(("Don't know how to deal with %x\n",
1257 cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
1260 cdf_summary_info_header_t ssi;
1261 cdf_property_info_t *info;
1265 if (cdf_unpack_summary_info(sst, h, &ssi, &info, &count) == -1)
1267 (void)fprintf(stderr, "Endian: %x\n", ssi.si_byte_order);
1268 (void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
1269 ssi.si_os_version >> 8);
1270 (void)fprintf(stderr, "Os %d\n", ssi.si_os);
1271 cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
1272 (void)fprintf(stderr, "Class %s\n", buf);
1273 (void)fprintf(stderr, "Count %d\n", ssi.si_count);
1274 cdf_dump_property_info(info, count);
1282 main(int argc, char *argv[])
1286 cdf_sat_t sat, ssat;
1287 cdf_stream_t sst, scn;
1292 (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
1298 for (i = 1; i < argc; i++) {
1299 if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
1300 err(1, "Cannot open `%s'", argv[1]);
1302 if (cdf_read_header(&info, &h) == -1)
1303 err(1, "Cannot read header");
1305 cdf_dump_header(&h);
1308 if (cdf_read_sat(&info, &h, &sat) == -1)
1309 err(1, "Cannot read sat");
1311 cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
1314 if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
1315 err(1, "Cannot read ssat");
1317 cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
1320 if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
1321 err(1, "Cannot read dir");
1323 if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst) == -1)
1324 err(1, "Cannot read short stream");
1326 cdf_dump_stream(&h, &sst);
1330 cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
1334 if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
1336 err(1, "Cannot read summary info");
1338 cdf_dump_summary_info(&h, &scn);
1341 (void)close(info.i_fd);