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.106 2017/04/30 17:05:02 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_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)
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))
89 cdf_malloc(const char *file
__attribute__((__unused__
)),
90 size_t line
__attribute__((__unused__
)), size_t n
)
92 DPRINTF(("%s,%zu: %s %zu\n", file
, line
, __func__
, n
));
97 cdf_realloc(const char *file
__attribute__((__unused__
)),
98 size_t line
__attribute__((__unused__
)), void *p
, size_t n
)
100 DPRINTF(("%s,%zu: %s %zu\n", file
, line
, __func__
, n
));
101 return realloc(p
, n
);
105 cdf_calloc(const char *file
__attribute__((__unused__
)),
106 size_t line
__attribute__((__unused__
)), size_t n
, size_t u
)
108 DPRINTF(("%s,%zu: %s %zu %zu\n", file
, line
, __func__
, n
, u
));
116 _cdf_tole2(uint16_t sv
)
119 uint8_t *s
= (uint8_t *)(void *)&sv
;
120 uint8_t *d
= (uint8_t *)(void *)&rv
;
130 _cdf_tole4(uint32_t sv
)
133 uint8_t *s
= (uint8_t *)(void *)&sv
;
134 uint8_t *d
= (uint8_t *)(void *)&rv
;
146 _cdf_tole8(uint64_t sv
)
149 uint8_t *s
= (uint8_t *)(void *)&sv
;
150 uint8_t *d
= (uint8_t *)(void *)&rv
;
163 * grab a uint32_t from a possibly unaligned address, and return it in
164 * the native host order.
167 cdf_getuint32(const uint8_t *p
, size_t offs
)
170 (void)memcpy(&rv
, p
+ offs
* sizeof(uint32_t), sizeof(rv
));
171 return CDF_TOLE4(rv
);
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)
180 cdf_tole2(uint16_t sv
)
182 return CDF_TOLE2(sv
);
186 cdf_tole4(uint32_t sv
)
188 return CDF_TOLE4(sv
);
192 cdf_tole8(uint64_t sv
)
194 return CDF_TOLE8(sv
);
198 cdf_swap_header(cdf_header_t
*h
)
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
]);
227 cdf_unpack_header(cdf_header_t
*h
, char *buf
)
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
]);
253 cdf_swap_dir(cdf_directory_t
*d
)
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
);
269 cdf_swap_class(cdf_classid_t
*d
)
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]);
277 cdf_unpack_dir(cdf_directory_t
*d
, char *buf
)
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
);
298 cdf_zero_stream(cdf_stream_t
*scn
)
309 cdf_check_stream(const cdf_stream_t
*sst
, const cdf_header_t
*h
)
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
);
318 cdf_check_stream_offset(const cdf_stream_t
*sst
, const cdf_header_t
*h
,
319 const void *p
, size_t tail
, int line
)
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
)
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
));
336 cdf_read(const cdf_info_t
*info
, off_t off
, void *buf
, size_t len
)
338 size_t siz
= (size_t)off
+ len
;
340 if ((off_t
)(off
+ len
) != (off_t
)siz
)
343 if (info
->i_buf
!= NULL
&& info
->i_len
>= siz
) {
344 (void)memcpy(buf
, &info
->i_buf
[off
], len
);
348 if (info
->i_fd
== -1)
351 if (pread(info
->i_fd
, buf
, len
, off
) != (ssize_t
)len
)
361 cdf_read_header(const cdf_info_t
*info
, cdf_header_t
*h
)
365 (void)memcpy(cdf_bo
.s
, "\01\02\03\04", 4);
366 if (cdf_read(info
, (off_t
)0, buf
, sizeof(buf
)) == -1)
368 cdf_unpack_header(h
, buf
);
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
));
377 if (h
->h_sec_size_p2
> 20) {
378 DPRINTF(("Bad sector size %hu\n", h
->h_sec_size_p2
));
381 if (h
->h_short_sec_size_p2
> 20) {
382 DPRINTF(("Bad short sector size %hu\n",
383 h
->h_short_sec_size_p2
));
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
)
397 size_t ss
= CDF_SEC_SIZE(h
);
398 size_t pos
= CDF_SEC_POS(h
, id
);
400 return cdf_read(info
, (off_t
)pos
, ((char *)buf
) + offs
, len
);
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
)
407 size_t ss
= CDF_SHORT_SEC_SIZE(h
);
408 size_t pos
= CDF_SHORT_SEC_POS(h
, id
);
410 if (pos
+ len
> CDF_SEC_SIZE(h
) * sst
->sst_len
) {
411 DPRINTF(("Out of bounds read %" SIZE_T_FORMAT
"u > %"
413 pos
+ len
, CDF_SEC_SIZE(h
) * sst
->sst_len
));
416 (void)memcpy(((char *)buf
) + offs
,
417 ((const char *)sst
->sst_tab
) + pos
, len
);
425 * Read the sector allocation table.
428 cdf_read_sat(const cdf_info_t
*info
, cdf_header_t
*h
, cdf_sat_t
*sat
)
431 size_t ss
= CDF_SEC_SIZE(h
);
432 cdf_secid_t
*msa
, mid
, sec
;
433 size_t nsatpersec
= (ss
/ sizeof(mid
)) - 1;
435 for (i
= 0; i
< __arraycount(h
->h_master_sat
); i
++)
436 if (h
->h_master_sat
[i
] == CDF_SECID_FREE
)
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
) ||
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
));
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",
452 if ((sat
->sat_tab
= CAST(cdf_secid_t
*, CDF_CALLOC(sat
->sat_len
, ss
)))
456 for (i
= 0; i
< __arraycount(h
->h_master_sat
); i
++) {
457 if (h
->h_master_sat
[i
] < 0)
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
]));
466 if ((msa
= CAST(cdf_secid_t
*, CDF_CALLOC(1, ss
))) == NULL
)
469 mid
= h
->h_secid_first_sector_in_master_sat
;
470 for (j
= 0; j
< h
->h_num_sectors_in_master_sat
; j
++) {
473 if (j
>= CDF_LOOP_LIMIT
) {
474 DPRINTF(("Reading master sector loop limit"));
477 if (cdf_read_sector(info
, msa
, 0, ss
, h
, mid
) != (ssize_t
)ss
) {
478 DPRINTF(("Reading master sector %d", mid
));
481 for (k
= 0; k
< nsatpersec
; k
++, i
++) {
482 sec
= CDF_TOLE4((uint32_t)msa
[k
]);
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
));
490 if (cdf_read_sector(info
, sat
->sat_tab
, ss
* i
, ss
, h
,
491 sec
) != (ssize_t
)ss
) {
492 DPRINTF(("Reading sector %d",
497 mid
= CDF_TOLE4((uint32_t)msa
[nsatpersec
]);
513 cdf_count_chain(const cdf_sat_t
*sat
, cdf_secid_t sid
, size_t size
)
516 cdf_secid_t maxsector
= (cdf_secid_t
)((sat
->sat_len
* size
)
517 / sizeof(maxsector
));
520 if (sid
== CDF_SECID_END_OF_CHAIN
) {
521 /* 0-length chain. */
522 DPRINTF((" empty\n"));
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"));
532 if (sid
>= maxsector
) {
533 DPRINTF(("Sector %d >= %d\n", sid
, maxsector
));
536 sid
= CDF_TOLE4((uint32_t)sat
->sat_tab
[sid
]);
539 DPRINTF((" none, sid: %d\n", sid
));
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
)
554 size_t ss
= CDF_SEC_SIZE(h
), i
, j
;
557 scn
->sst_len
= cdf_count_chain(sat
, sid
, ss
);
558 scn
->sst_dirlen
= MAX(h
->h_min_size_standard_stream
, len
);
561 if (sid
== CDF_SECID_END_OF_CHAIN
|| len
== 0)
562 return cdf_zero_stream(scn
);
564 if (scn
->sst_len
== (size_t)-1)
567 scn
->sst_tab
= CDF_CALLOC(scn
->sst_len
, ss
);
568 if (scn
->sst_tab
== NULL
)
569 return cdf_zero_stream(scn
);
571 for (j
= i
= 0; sid
>= 0; i
++, j
++) {
572 if (j
>= CDF_LOOP_LIMIT
) {
573 DPRINTF(("Read long sector chain loop limit"));
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
,
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 */
588 DPRINTF(("Reading long sector chain %d", sid
));
591 sid
= CDF_TOLE4((uint32_t)sat
->sat_tab
[sid
]);
596 return cdf_zero_stream(scn
);
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
)
604 size_t ss
= CDF_SHORT_SEC_SIZE(h
), i
, j
;
606 scn
->sst_len
= cdf_count_chain(ssat
, sid
, CDF_SEC_SIZE(h
));
607 scn
->sst_dirlen
= len
;
610 if (scn
->sst_len
== (size_t)-1)
613 scn
->sst_tab
= CDF_CALLOC(scn
->sst_len
, ss
);
614 if (scn
->sst_tab
== NULL
)
615 return cdf_zero_stream(scn
);
617 for (j
= i
= 0; sid
>= 0; i
++, j
++) {
618 if (j
>= CDF_LOOP_LIMIT
) {
619 DPRINTF(("Read short sector chain loop limit"));
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",
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
));
633 sid
= CDF_TOLE4((uint32_t)ssat
->sat_tab
[sid
]);
638 return cdf_zero_stream(scn
);
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
)
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
,
651 return cdf_read_long_sector_chain(info
, h
, sat
, sid
, len
, scn
);
655 cdf_read_dir(const cdf_info_t
*info
, const cdf_header_t
*h
,
656 const cdf_sat_t
*sat
, cdf_dir_t
*dir
)
659 size_t ss
= CDF_SEC_SIZE(h
), ns
, nd
;
661 cdf_secid_t sid
= h
->h_secid_first_directory
;
663 ns
= cdf_count_chain(sat
, sid
, ss
);
664 if (ns
== (size_t)-1)
667 nd
= ss
/ CDF_DIRECTORY_SIZE
;
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
)
675 if ((buf
= CAST(char *, CDF_MALLOC(ss
))) == NULL
) {
680 for (j
= i
= 0; i
< ns
; i
++, j
++) {
681 if (j
>= CDF_LOOP_LIMIT
) {
682 DPRINTF(("Read dir loop limit"));
685 if (cdf_read_sector(info
, buf
, 0, ss
, h
, sid
) != (ssize_t
)ss
) {
686 DPRINTF(("Reading directory sector %d", sid
));
689 for (j
= 0; j
< nd
; j
++) {
690 cdf_unpack_dir(&dir
->dir_tab
[i
* nd
+ j
],
691 &buf
[j
* CDF_DIRECTORY_SIZE
]);
693 sid
= CDF_TOLE4((uint32_t)sat
->sat_tab
[sid
]);
696 for (i
= 0; i
< dir
->dir_len
; i
++)
697 cdf_swap_dir(&dir
->dir_tab
[i
]);
709 cdf_read_ssat(const cdf_info_t
*info
, const cdf_header_t
*h
,
710 const cdf_sat_t
*sat
, cdf_sat_t
*ssat
)
713 size_t ss
= CDF_SEC_SIZE(h
);
714 cdf_secid_t sid
= h
->h_secid_first_sector_in_short_sat
;
716 ssat
->sat_tab
= NULL
;
717 ssat
->sat_len
= cdf_count_chain(sat
, sid
, ss
);
718 if (ssat
->sat_len
== (size_t)-1)
721 ssat
->sat_tab
= CAST(cdf_secid_t
*, CDF_CALLOC(ssat
->sat_len
, ss
));
722 if (ssat
->sat_tab
== NULL
)
725 for (j
= i
= 0; sid
>= 0; i
++, j
++) {
726 if (j
>= CDF_LOOP_LIMIT
) {
727 DPRINTF(("Read short sat sector loop limit"));
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
,
736 if (cdf_read_sector(info
, ssat
->sat_tab
, i
* ss
, ss
, h
, sid
) !=
738 DPRINTF(("Reading short sat sector %d", sid
));
741 sid
= CDF_TOLE4((uint32_t)sat
->sat_tab
[sid
]);
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
)
757 const cdf_directory_t
*d
;
760 for (i
= 0; i
< dir
->dir_len
; i
++)
761 if (dir
->dir_tab
[i
].d_type
== CDF_DIR_TYPE_ROOT_STORAGE
)
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"));
769 d
= &dir
->dir_tab
[i
];
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"));
778 return cdf_read_long_sector_chain(info
, h
, sat
,
779 d
->d_stream_first_sector
, d
->d_size
, scn
);
782 (void)cdf_zero_stream(scn
);
787 cdf_namecmp(const char *d
, const uint16_t *s
, size_t l
)
789 for (; l
--; d
++, s
++)
790 if (*d
!= CDF_TOLE2(*s
))
791 return (unsigned char)*d
- CDF_TOLE2(*s
);
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
)
800 return cdf_read_user_stream(info
, h
, sat
, ssat
, sst
, dir
,
801 "\05DocumentSummaryInformation", scn
);
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
)
809 return cdf_read_user_stream(info
, h
, sat
, ssat
, sst
, dir
,
810 "\05SummaryInformation", scn
);
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
)
818 const cdf_directory_t
*d
;
819 int i
= cdf_find_stream(dir
, name
, CDF_DIR_TYPE_USER_STREAM
);
822 memset(scn
, 0, sizeof(*scn
));
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
);
832 cdf_find_stream(const cdf_dir_t
*dir
, const char *name
, int type
)
834 size_t i
, name_len
= strlen(name
) + 1;
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
)
844 DPRINTF(("Cannot find type %d `%s'\n", type
, name
));
849 #define CDF_SHLEN_LIMIT (UINT32_MAX / 8)
850 #define CDF_PROP_LIMIT (UINT32_MAX / (8 * sizeof(cdf_property_info_t)))
853 cdf_offset(const void *p
, size_t l
)
855 return CAST(const void *, CAST(const uint8_t *, p
) + l
);
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
)
862 size_t tail
= (i
<< 1) + 1;
867 DPRINTF(("Past end %p < %p\n", e
, p
));
870 if (cdf_check_stream_offset(sst
, h
, p
, (tail
+ 1) * sizeof(uint32_t),
873 ofs
= CDF_GETUINT32(p
, tail
);
874 q
= CAST(const uint8_t *, cdf_offset(CAST(const void *, p
),
875 ofs
- 2 * sizeof(uint32_t)));
878 DPRINTF(("Wrapped around %p < %p\n", q
, p
));
883 DPRINTF(("Ran off the end %p >= %p\n", q
, e
));
889 static cdf_property_info_t
*
890 cdf_grow_info(cdf_property_info_t
**info
, size_t *maxcount
, size_t incr
)
892 cdf_property_info_t
*inp
;
893 size_t newcount
= *maxcount
+ incr
;
895 if (newcount
> CDF_PROP_LIMIT
) {
896 DPRINTF(("exceeded property limit %zu > %zu\n",
897 newcount
, CDF_PROP_LIMIT
));
900 inp
= CAST(cdf_property_info_t
*,
901 CDF_REALLOC(*info
, newcount
* sizeof(*inp
)));
906 *maxcount
= newcount
;
916 cdf_copy_info(cdf_property_info_t
*inp
, const void *p
, const void *e
,
919 if (inp
->pi_type
& CDF_VECTOR
)
922 if ((size_t)(CAST(const char *, e
) - CAST(const char *, p
)) < len
)
925 (void)memcpy(&inp
->pi_val
, p
, len
);
929 inp
->pi_u16
= CDF_TOLE2(inp
->pi_u16
);
932 inp
->pi_u32
= CDF_TOLE4(inp
->pi_u32
);
935 inp
->pi_u64
= CDF_TOLE8(inp
->pi_u64
);
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
)
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
;
953 if (offs
> UINT32_MAX
/ 4) {
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)
961 sh
.sh_len
= CDF_TOLE4(shp
->sh_len
);
962 if (sh
.sh_len
> CDF_SHLEN_LIMIT
) {
967 if (cdf_check_stream_offset(sst
, h
, shp
, sh
.sh_len
, __LINE__
) == -1)
970 sh
.sh_properties
= CDF_TOLE4(shp
->sh_properties
);
971 DPRINTF(("section len: %u properties %u\n", sh
.sh_len
,
973 if (sh
.sh_properties
> CDF_PROP_LIMIT
)
975 inp
= cdf_grow_info(info
, maxcount
, sh
.sh_properties
);
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)
985 for (i
= 0; i
< sh
.sh_properties
; i
++) {
986 if ((q
= cdf_get_property_info_pos(sst
, h
, p
, e
, i
)) == NULL
)
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"));
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"));
1002 nelements
= CDF_GETUINT32(q
, 1);
1003 if (nelements
== 0) {
1004 DPRINTF(("CDF_VECTOR with nelements == 0\n"));
1012 o4
= slen
* sizeof(uint32_t);
1013 if (inp
[i
].pi_type
& (CDF_ARRAY
|CDF_BYREF
|CDF_RESERVED
))
1015 switch (inp
[i
].pi_type
& CDF_TYPEMASK
) {
1020 if (!cdf_copy_info(&inp
[i
], &q
[o4
], e
, sizeof(int16_t)))
1025 case CDF_UNSIGNED32
:
1027 if (!cdf_copy_info(&inp
[i
], &q
[o4
], e
, sizeof(int32_t)))
1031 case CDF_UNSIGNED64
:
1034 if (!cdf_copy_info(&inp
[i
], &q
[o4
], e
, sizeof(int64_t)))
1037 case CDF_LENGTH32_STRING
:
1038 case CDF_LENGTH32_WSTRING
:
1039 if (nelements
> 1) {
1040 size_t nelem
= inp
- *info
;
1041 inp
= cdf_grow_info(info
, maxcount
, nelements
);
1046 DPRINTF(("nelements = %" SIZE_T_FORMAT
"u\n",
1048 for (j
= 0; j
< nelements
&& i
< sh
.sh_properties
;
1053 if (o4
+ sizeof(uint32_t) > left
)
1056 l
= CDF_GETUINT32(q
, slen
);
1057 o4
+= sizeof(uint32_t);
1061 inp
[i
].pi_str
.s_len
= l
;
1062 inp
[i
].pi_str
.s_buf
= CAST(const char *,
1063 CAST(const void *, &q
[o4
]));
1065 DPRINTF(("o=%zu l=%d(%" SIZE_T_FORMAT
1066 "u), t=%zu s=%s\n", o4
, l
,
1067 CDF_ROUND(l
, sizeof(l
)), left
,
1068 inp
[i
].pi_str
.s_buf
));
1074 o4
= slen
* sizeof(uint32_t);
1079 if (inp
[i
].pi_type
& CDF_VECTOR
)
1084 memset(&inp
[i
].pi_val
, 0, sizeof(inp
[i
].pi_val
));
1085 DPRINTF(("Don't know how to deal with %#x\n",
1101 cdf_unpack_summary_info(const cdf_stream_t
*sst
, const cdf_header_t
*h
,
1102 cdf_summary_info_header_t
*ssi
, cdf_property_info_t
**info
, size_t *count
)
1105 const cdf_summary_info_header_t
*si
=
1106 CAST(const cdf_summary_info_header_t
*, sst
->sst_tab
);
1107 const cdf_section_declaration_t
*sd
=
1108 CAST(const cdf_section_declaration_t
*, (const void *)
1109 ((const char *)sst
->sst_tab
+ CDF_SECTION_DECLARATION_OFFSET
));
1111 if (cdf_check_stream_offset(sst
, h
, si
, sizeof(*si
), __LINE__
) == -1 ||
1112 cdf_check_stream_offset(sst
, h
, sd
, sizeof(*sd
), __LINE__
) == -1)
1114 ssi
->si_byte_order
= CDF_TOLE2(si
->si_byte_order
);
1115 ssi
->si_os_version
= CDF_TOLE2(si
->si_os_version
);
1116 ssi
->si_os
= CDF_TOLE2(si
->si_os
);
1117 ssi
->si_class
= si
->si_class
;
1118 cdf_swap_class(&ssi
->si_class
);
1119 ssi
->si_count
= CDF_TOLE4(si
->si_count
);
1123 if (cdf_read_property_info(sst
, h
, CDF_TOLE4(sd
->sd_offset
), info
,
1124 count
, &maxcount
) == -1)
1130 #define extract_catalog_field(t, f, l) \
1131 if (b + l + sizeof(cep->f) > eb) { \
1132 cep->ce_namlen = 0; \
1135 memcpy(&cep->f, b + (l), sizeof(cep->f)); \
1136 ce[i].f = CAST(t, CDF_TOLE(cep->f))
1139 cdf_unpack_catalog(const cdf_header_t
*h
, const cdf_stream_t
*sst
,
1140 cdf_catalog_t
**cat
)
1142 size_t ss
= cdf_check_stream(sst
, h
);
1143 const char *b
= CAST(const char *, sst
->sst_tab
);
1144 const char *nb
, *eb
= b
+ ss
* sst
->sst_len
;
1146 cdf_catalog_entry_t
*ce
;
1150 for (nr
= 0;; nr
++) {
1151 memcpy(&reclen
, b
, sizeof(reclen
));
1152 reclen
= CDF_TOLE2(reclen
);
1162 *cat
= CAST(cdf_catalog_t
*,
1163 CDF_MALLOC(sizeof(cdf_catalog_t
) + nr
* sizeof(*ce
)));
1167 memset(ce
, 0, nr
* sizeof(*ce
));
1168 b
= CAST(const char *, sst
->sst_tab
);
1169 for (j
= i
= 0; i
< nr
; b
+= reclen
) {
1170 cdf_catalog_entry_t
*cep
= &ce
[j
];
1173 extract_catalog_field(uint16_t, ce_namlen
, 0);
1174 extract_catalog_field(uint16_t, ce_num
, 4);
1175 extract_catalog_field(uint64_t, ce_timestamp
, 8);
1176 reclen
= cep
->ce_namlen
;
1183 cep
->ce_namlen
= __arraycount(cep
->ce_name
) - 1;
1185 if (cep
->ce_namlen
> rlen
)
1186 cep
->ce_namlen
= rlen
;
1188 np
= CAST(const uint16_t *, CAST(const void *, (b
+ 16)));
1189 nb
= CAST(const char *, CAST(const void *,
1190 (np
+ cep
->ce_namlen
)));
1196 for (k
= 0; k
< cep
->ce_namlen
; k
++)
1197 cep
->ce_name
[k
] = np
[k
]; /* XXX: CDF_TOLE2? */
1198 cep
->ce_name
[cep
->ce_namlen
] = 0;
1202 (*cat
)->cat_num
= j
;
1207 cdf_print_classid(char *buf
, size_t buflen
, const cdf_classid_t
*id
)
1209 return snprintf(buf
, buflen
, "%.8x-%.4x-%.4x-%.2x%.2x-"
1210 "%.2x%.2x%.2x%.2x%.2x%.2x", id
->cl_dword
, id
->cl_word
[0],
1211 id
->cl_word
[1], id
->cl_two
[0], id
->cl_two
[1], id
->cl_six
[0],
1212 id
->cl_six
[1], id
->cl_six
[2], id
->cl_six
[3], id
->cl_six
[4],
1216 static const struct {
1220 { CDF_PROPERTY_CODE_PAGE
, "Code page" },
1221 { CDF_PROPERTY_TITLE
, "Title" },
1222 { CDF_PROPERTY_SUBJECT
, "Subject" },
1223 { CDF_PROPERTY_AUTHOR
, "Author" },
1224 { CDF_PROPERTY_KEYWORDS
, "Keywords" },
1225 { CDF_PROPERTY_COMMENTS
, "Comments" },
1226 { CDF_PROPERTY_TEMPLATE
, "Template" },
1227 { CDF_PROPERTY_LAST_SAVED_BY
, "Last Saved By" },
1228 { CDF_PROPERTY_REVISION_NUMBER
, "Revision Number" },
1229 { CDF_PROPERTY_TOTAL_EDITING_TIME
, "Total Editing Time" },
1230 { CDF_PROPERTY_LAST_PRINTED
, "Last Printed" },
1231 { CDF_PROPERTY_CREATE_TIME
, "Create Time/Date" },
1232 { CDF_PROPERTY_LAST_SAVED_TIME
, "Last Saved Time/Date" },
1233 { CDF_PROPERTY_NUMBER_OF_PAGES
, "Number of Pages" },
1234 { CDF_PROPERTY_NUMBER_OF_WORDS
, "Number of Words" },
1235 { CDF_PROPERTY_NUMBER_OF_CHARACTERS
, "Number of Characters" },
1236 { CDF_PROPERTY_THUMBNAIL
, "Thumbnail" },
1237 { CDF_PROPERTY_NAME_OF_APPLICATION
, "Name of Creating Application" },
1238 { CDF_PROPERTY_SECURITY
, "Security" },
1239 { CDF_PROPERTY_LOCALE_ID
, "Locale ID" },
1243 cdf_print_property_name(char *buf
, size_t bufsiz
, uint32_t p
)
1247 for (i
= 0; i
< __arraycount(vn
); i
++)
1249 return snprintf(buf
, bufsiz
, "%s", vn
[i
].n
);
1250 return snprintf(buf
, bufsiz
, "%#x", p
);
1254 cdf_print_elapsed_time(char *buf
, size_t bufsiz
, cdf_timestamp_t ts
)
1257 int days
, hours
, mins
, secs
;
1259 ts
/= CDF_TIME_PREC
;
1260 secs
= (int)(ts
% 60);
1262 mins
= (int)(ts
% 60);
1264 hours
= (int)(ts
% 24);
1269 len
+= snprintf(buf
+ len
, bufsiz
- len
, "%dd+", days
);
1270 if ((size_t)len
>= bufsiz
)
1274 if (days
|| hours
) {
1275 len
+= snprintf(buf
+ len
, bufsiz
- len
, "%.2d:", hours
);
1276 if ((size_t)len
>= bufsiz
)
1280 len
+= snprintf(buf
+ len
, bufsiz
- len
, "%.2d:", mins
);
1281 if ((size_t)len
>= bufsiz
)
1284 len
+= snprintf(buf
+ len
, bufsiz
- len
, "%.2d", secs
);
1289 cdf_u16tos8(char *buf
, size_t len
, const uint16_t *p
)
1292 for (i
= 0; i
< len
&& p
[i
]; i
++)
1293 buf
[i
] = (char)p
[i
];
1300 cdf_dump_header(const cdf_header_t
*h
)
1304 #define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
1305 #define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \
1306 h->h_ ## b, 1 << h->h_ ## b)
1307 DUMP("%d", revision
);
1308 DUMP("%d", version
);
1309 DUMP("%#x", byte_order
);
1310 DUMP2("%d", sec_size_p2
);
1311 DUMP2("%d", short_sec_size_p2
);
1312 DUMP("%d", num_sectors_in_sat
);
1313 DUMP("%d", secid_first_directory
);
1314 DUMP("%d", min_size_standard_stream
);
1315 DUMP("%d", secid_first_sector_in_short_sat
);
1316 DUMP("%d", num_sectors_in_short_sat
);
1317 DUMP("%d", secid_first_sector_in_master_sat
);
1318 DUMP("%d", num_sectors_in_master_sat
);
1319 for (i
= 0; i
< __arraycount(h
->h_master_sat
); i
++) {
1320 if (h
->h_master_sat
[i
] == CDF_SECID_FREE
)
1322 (void)fprintf(stderr
, "%35.35s[%.3" SIZE_T_FORMAT
"u] = %d\n",
1323 "master_sat", i
, h
->h_master_sat
[i
]);
1328 cdf_dump_sat(const char *prefix
, const cdf_sat_t
*sat
, size_t size
)
1330 size_t i
, j
, s
= size
/ sizeof(cdf_secid_t
);
1332 for (i
= 0; i
< sat
->sat_len
; i
++) {
1333 (void)fprintf(stderr
, "%s[%" SIZE_T_FORMAT
"u]:\n%.6"
1334 SIZE_T_FORMAT
"u: ", prefix
, i
, i
* s
);
1335 for (j
= 0; j
< s
; j
++) {
1336 (void)fprintf(stderr
, "%5d, ",
1337 CDF_TOLE4(sat
->sat_tab
[s
* i
+ j
]));
1338 if ((j
+ 1) % 10 == 0)
1339 (void)fprintf(stderr
, "\n%.6" SIZE_T_FORMAT
1340 "u: ", i
* s
+ j
+ 1);
1342 (void)fprintf(stderr
, "\n");
1347 cdf_dump(const void *v
, size_t len
)
1350 const unsigned char *p
= v
;
1353 (void)fprintf(stderr
, "%.4x: ", 0);
1354 for (i
= 0, j
= 0; i
< len
; i
++, p
++) {
1355 (void)fprintf(stderr
, "%.2x ", *p
);
1356 abuf
[j
++] = isprint(*p
) ? *p
: '.';
1360 (void)fprintf(stderr
, "%s\n%.4" SIZE_T_FORMAT
"x: ",
1364 (void)fprintf(stderr
, "\n");
1368 cdf_dump_stream(const cdf_stream_t
*sst
)
1370 size_t ss
= sst
->sst_ss
;
1371 cdf_dump(sst
->sst_tab
, ss
* sst
->sst_len
);
1375 cdf_dump_dir(const cdf_info_t
*info
, const cdf_header_t
*h
,
1376 const cdf_sat_t
*sat
, const cdf_sat_t
*ssat
, const cdf_stream_t
*sst
,
1377 const cdf_dir_t
*dir
)
1381 char name
[__arraycount(d
->d_name
)];
1385 static const char *types
[] = { "empty", "user storage",
1386 "user stream", "lockbytes", "property", "root storage" };
1388 for (i
= 0; i
< dir
->dir_len
; i
++) {
1390 d
= &dir
->dir_tab
[i
];
1391 for (j
= 0; j
< sizeof(name
); j
++)
1392 name
[j
] = (char)CDF_TOLE2(d
->d_name
[j
]);
1393 (void)fprintf(stderr
, "Directory %" SIZE_T_FORMAT
"u: %s\n",
1395 if (d
->d_type
< __arraycount(types
))
1396 (void)fprintf(stderr
, "Type: %s\n", types
[d
->d_type
]);
1398 (void)fprintf(stderr
, "Type: %d\n", d
->d_type
);
1399 (void)fprintf(stderr
, "Color: %s\n",
1400 d
->d_color
? "black" : "red");
1401 (void)fprintf(stderr
, "Left child: %d\n", d
->d_left_child
);
1402 (void)fprintf(stderr
, "Right child: %d\n", d
->d_right_child
);
1403 (void)fprintf(stderr
, "Flags: %#x\n", d
->d_flags
);
1404 cdf_timestamp_to_timespec(&ts
, d
->d_created
);
1405 (void)fprintf(stderr
, "Created %s", cdf_ctime(&ts
.tv_sec
, buf
));
1406 cdf_timestamp_to_timespec(&ts
, d
->d_modified
);
1407 (void)fprintf(stderr
, "Modified %s",
1408 cdf_ctime(&ts
.tv_sec
, buf
));
1409 (void)fprintf(stderr
, "Stream %d\n", d
->d_stream_first_sector
);
1410 (void)fprintf(stderr
, "Size %d\n", d
->d_size
);
1411 switch (d
->d_type
) {
1412 case CDF_DIR_TYPE_USER_STORAGE
:
1413 (void)fprintf(stderr
, "Storage: %d\n", d
->d_storage
);
1415 case CDF_DIR_TYPE_USER_STREAM
:
1418 if (cdf_read_sector_chain(info
, h
, sat
, ssat
, sst
,
1419 d
->d_stream_first_sector
, d
->d_size
, &scn
) == -1) {
1420 warn("Can't read stream for %s at %d len %d",
1421 name
, d
->d_stream_first_sector
, d
->d_size
);
1424 cdf_dump_stream(&scn
);
1435 cdf_dump_property_info(const cdf_property_info_t
*info
, size_t count
)
1442 for (i
= 0; i
< count
; i
++) {
1443 cdf_print_property_name(buf
, sizeof(buf
), info
[i
].pi_id
);
1444 (void)fprintf(stderr
, "%" SIZE_T_FORMAT
"u) %s: ", i
, buf
);
1445 switch (info
[i
].pi_type
) {
1449 (void)fprintf(stderr
, "signed 16 [%hd]\n",
1453 (void)fprintf(stderr
, "signed 32 [%d]\n",
1456 case CDF_UNSIGNED32
:
1457 (void)fprintf(stderr
, "unsigned 32 [%u]\n",
1461 (void)fprintf(stderr
, "float [%g]\n",
1465 (void)fprintf(stderr
, "double [%g]\n",
1468 case CDF_LENGTH32_STRING
:
1469 (void)fprintf(stderr
, "string %u [%.*s]\n",
1470 info
[i
].pi_str
.s_len
,
1471 info
[i
].pi_str
.s_len
, info
[i
].pi_str
.s_buf
);
1473 case CDF_LENGTH32_WSTRING
:
1474 (void)fprintf(stderr
, "string %u [",
1475 info
[i
].pi_str
.s_len
);
1476 for (j
= 0; j
< info
[i
].pi_str
.s_len
- 1; j
++)
1477 (void)fputc(info
[i
].pi_str
.s_buf
[j
<< 1], stderr
);
1478 (void)fprintf(stderr
, "]\n");
1482 if (tp
< 1000000000000000LL) {
1483 cdf_print_elapsed_time(buf
, sizeof(buf
), tp
);
1484 (void)fprintf(stderr
, "timestamp %s\n", buf
);
1487 cdf_timestamp_to_timespec(&ts
, tp
);
1488 (void)fprintf(stderr
, "timestamp %s",
1489 cdf_ctime(&ts
.tv_sec
, tbuf
));
1493 (void)fprintf(stderr
, "CLIPBOARD %u\n", info
[i
].pi_u32
);
1496 DPRINTF(("Don't know how to deal with %#x\n",
1505 cdf_dump_summary_info(const cdf_header_t
*h
, const cdf_stream_t
*sst
)
1508 cdf_summary_info_header_t ssi
;
1509 cdf_property_info_t
*info
;
1513 if (cdf_unpack_summary_info(sst
, h
, &ssi
, &info
, &count
) == -1)
1515 (void)fprintf(stderr
, "Endian: %#x\n", ssi
.si_byte_order
);
1516 (void)fprintf(stderr
, "Os Version %d.%d\n", ssi
.si_os_version
& 0xff,
1517 ssi
.si_os_version
>> 8);
1518 (void)fprintf(stderr
, "Os %d\n", ssi
.si_os
);
1519 cdf_print_classid(buf
, sizeof(buf
), &ssi
.si_class
);
1520 (void)fprintf(stderr
, "Class %s\n", buf
);
1521 (void)fprintf(stderr
, "Count %d\n", ssi
.si_count
);
1522 cdf_dump_property_info(info
, count
);
1528 cdf_dump_catalog(const cdf_header_t
*h
, const cdf_stream_t
*sst
)
1531 cdf_unpack_catalog(h
, sst
, &cat
);
1532 const cdf_catalog_entry_t
*ce
= cat
->cat_e
;
1534 char tbuf
[64], sbuf
[256];
1537 printf("Catalog:\n");
1538 for (i
= 0; i
< cat
->cat_num
; i
++) {
1539 cdf_timestamp_to_timespec(&ts
, ce
[i
].ce_timestamp
);
1540 printf("\t%d %s %s", ce
[i
].ce_num
,
1541 cdf_u16tos8(sbuf
, ce
[i
].ce_namlen
, ce
[i
].ce_name
),
1542 cdf_ctime(&ts
.tv_sec
, tbuf
));
1551 main(int argc
, char *argv
[])
1555 cdf_sat_t sat
, ssat
;
1556 cdf_stream_t sst
, scn
;
1559 const cdf_directory_t
*root
;
1561 #define getprogname() __progname
1562 extern char *__progname
;
1565 (void)fprintf(stderr
, "Usage: %s <filename>\n", getprogname());
1571 for (i
= 1; i
< argc
; i
++) {
1572 if ((info
.i_fd
= open(argv
[1], O_RDONLY
)) == -1)
1573 err(1, "Cannot open `%s'", argv
[1]);
1575 if (cdf_read_header(&info
, &h
) == -1)
1576 err(1, "Cannot read header");
1578 cdf_dump_header(&h
);
1581 if (cdf_read_sat(&info
, &h
, &sat
) == -1)
1582 err(1, "Cannot read sat");
1584 cdf_dump_sat("SAT", &sat
, CDF_SEC_SIZE(&h
));
1587 if (cdf_read_ssat(&info
, &h
, &sat
, &ssat
) == -1)
1588 err(1, "Cannot read ssat");
1590 cdf_dump_sat("SSAT", &ssat
, CDF_SHORT_SEC_SIZE(&h
));
1593 if (cdf_read_dir(&info
, &h
, &sat
, &dir
) == -1)
1594 err(1, "Cannot read dir");
1596 if (cdf_read_short_stream(&info
, &h
, &sat
, &dir
, &sst
, &root
)
1598 err(1, "Cannot read short stream");
1600 cdf_dump_stream(&sst
);
1604 cdf_dump_dir(&info
, &h
, &sat
, &ssat
, &sst
, &dir
);
1608 if (cdf_read_summary_info(&info
, &h
, &sat
, &ssat
, &sst
, &dir
,
1610 warn("Cannot read summary info");
1613 cdf_dump_summary_info(&h
, &scn
);
1615 if (cdf_read_user_stream(&info
, &h
, &sat
, &ssat
, &sst
,
1616 &dir
, "Catalog", &scn
) == -1)
1617 warn("Cannot read catalog");
1620 cdf_dump_catalog(&h
, &scn
);
1623 (void)close(info
.i_fd
);