cdf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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. #include "file.h"
  32. #ifndef lint
  33. FILE_RCSID("@(#)$File: cdf.c,v 1.22 2009/04/30 21:03:26 christos Exp $")
  34. #endif
  35. #include <assert.h>
  36. #ifdef CDF_DEBUG
  37. #include <err.h>
  38. #endif
  39. #include <stdlib.h>
  40. #include <unistd.h>
  41. #include <string.h>
  42. #include <time.h>
  43. #include <ctype.h>
  44. #ifndef EFTYPE
  45. #define EFTYPE EINVAL
  46. #endif
  47. #include "cdf.h"
  48. #ifndef __arraycount
  49. #define __arraycount(a) (sizeof(a) / sizeof(a[0]))
  50. #endif
  51. #ifdef CDF_DEBUG
  52. #define DPRINTF(a) printf a, fflush(stdout)
  53. #else
  54. #define DPRINTF(a)
  55. #endif
  56. static union {
  57. char s[4];
  58. uint32_t u;
  59. } cdf_bo;
  60. #define NEED_SWAP (cdf_bo.u == (uint32_t)0x01020304)
  61. #define CDF_TOLE8(x) (NEED_SWAP ? cdf_tole8(x) : (uint64_t)(x))
  62. #define CDF_TOLE4(x) (NEED_SWAP ? cdf_tole4(x) : (uint32_t)(x))
  63. #define CDF_TOLE2(x) (NEED_SWAP ? cdf_tole2(x) : (uint16_t)(x))
  64. /*
  65. * swap a short
  66. */
  67. uint16_t
  68. cdf_tole2(uint16_t sv)
  69. {
  70. uint16_t rv;
  71. uint8_t *s = (uint8_t *)(void *)&sv;
  72. uint8_t *d = (uint8_t *)(void *)&rv;
  73. d[0] = s[1];
  74. d[1] = s[0];
  75. return rv;
  76. }
  77. /*
  78. * swap an int
  79. */
  80. uint32_t
  81. cdf_tole4(uint32_t sv)
  82. {
  83. uint32_t rv;
  84. uint8_t *s = (uint8_t *)(void *)&sv;
  85. uint8_t *d = (uint8_t *)(void *)&rv;
  86. d[0] = s[3];
  87. d[1] = s[2];
  88. d[2] = s[1];
  89. d[3] = s[0];
  90. return rv;
  91. }
  92. /*
  93. * swap a quad
  94. */
  95. uint64_t
  96. cdf_tole8(uint64_t sv)
  97. {
  98. uint64_t rv;
  99. uint8_t *s = (uint8_t *)(void *)&sv;
  100. uint8_t *d = (uint8_t *)(void *)&rv;
  101. d[0] = s[7];
  102. d[1] = s[6];
  103. d[2] = s[5];
  104. d[3] = s[4];
  105. d[4] = s[3];
  106. d[5] = s[2];
  107. d[6] = s[1];
  108. d[7] = s[0];
  109. return rv;
  110. }
  111. #define CDF_UNPACK(a) \
  112. (void)memcpy(&(a), &buf[len], sizeof(a)), len += sizeof(a)
  113. #define CDF_UNPACKA(a) \
  114. (void)memcpy((a), &buf[len], sizeof(a)), len += sizeof(a)
  115. void
  116. cdf_swap_header(cdf_header_t *h)
  117. {
  118. size_t i;
  119. h->h_magic = CDF_TOLE8(h->h_magic);
  120. h->h_uuid[0] = CDF_TOLE8(h->h_uuid[0]);
  121. h->h_uuid[1] = CDF_TOLE8(h->h_uuid[1]);
  122. h->h_revision = CDF_TOLE2(h->h_revision);
  123. h->h_version = CDF_TOLE2(h->h_version);
  124. h->h_byte_order = CDF_TOLE2(h->h_byte_order);
  125. h->h_sec_size_p2 = CDF_TOLE2(h->h_sec_size_p2);
  126. h->h_short_sec_size_p2 = CDF_TOLE2(h->h_short_sec_size_p2);
  127. h->h_num_sectors_in_sat = CDF_TOLE4(h->h_num_sectors_in_sat);
  128. h->h_secid_first_directory = CDF_TOLE4(h->h_secid_first_directory);
  129. h->h_min_size_standard_stream =
  130. CDF_TOLE4(h->h_min_size_standard_stream);
  131. h->h_secid_first_sector_in_short_sat =
  132. CDF_TOLE4(h->h_secid_first_sector_in_short_sat);
  133. h->h_num_sectors_in_short_sat =
  134. CDF_TOLE4(h->h_num_sectors_in_short_sat);
  135. h->h_secid_first_sector_in_master_sat =
  136. CDF_TOLE4(h->h_secid_first_sector_in_master_sat);
  137. h->h_num_sectors_in_master_sat =
  138. CDF_TOLE4(h->h_num_sectors_in_master_sat);
  139. for (i = 0; i < __arraycount(h->h_master_sat); i++)
  140. h->h_master_sat[i] = CDF_TOLE4(h->h_master_sat[i]);
  141. }
  142. void
  143. cdf_unpack_header(cdf_header_t *h, char *buf)
  144. {
  145. size_t i;
  146. size_t len = 0;
  147. CDF_UNPACK(h->h_magic);
  148. CDF_UNPACKA(h->h_uuid);
  149. CDF_UNPACK(h->h_revision);
  150. CDF_UNPACK(h->h_version);
  151. CDF_UNPACK(h->h_byte_order);
  152. CDF_UNPACK(h->h_sec_size_p2);
  153. CDF_UNPACK(h->h_short_sec_size_p2);
  154. CDF_UNPACKA(h->h_unused0);
  155. CDF_UNPACK(h->h_num_sectors_in_sat);
  156. CDF_UNPACK(h->h_secid_first_directory);
  157. CDF_UNPACKA(h->h_unused1);
  158. CDF_UNPACK(h->h_min_size_standard_stream);
  159. CDF_UNPACK(h->h_secid_first_sector_in_short_sat);
  160. CDF_UNPACK(h->h_num_sectors_in_short_sat);
  161. CDF_UNPACK(h->h_secid_first_sector_in_master_sat);
  162. CDF_UNPACK(h->h_num_sectors_in_master_sat);
  163. for (i = 0; i < __arraycount(h->h_master_sat); i++)
  164. CDF_UNPACK(h->h_master_sat[i]);
  165. }
  166. void
  167. cdf_swap_dir(cdf_directory_t *d)
  168. {
  169. d->d_namelen = CDF_TOLE2(d->d_namelen);
  170. d->d_left_child = CDF_TOLE4(d->d_left_child);
  171. d->d_right_child = CDF_TOLE4(d->d_right_child);
  172. d->d_storage = CDF_TOLE4(d->d_storage);
  173. d->d_storage_uuid[0] = CDF_TOLE8(d->d_storage_uuid[0]);
  174. d->d_storage_uuid[1] = CDF_TOLE8(d->d_storage_uuid[1]);
  175. d->d_flags = CDF_TOLE4(d->d_flags);
  176. d->d_created = CDF_TOLE8(d->d_created);
  177. d->d_modified = CDF_TOLE8(d->d_modified);
  178. d->d_stream_first_sector = CDF_TOLE4(d->d_stream_first_sector);
  179. d->d_size = CDF_TOLE4(d->d_size);
  180. }
  181. void
  182. cdf_swap_class(cdf_classid_t *d)
  183. {
  184. d->cl_dword = CDF_TOLE4(d->cl_dword);
  185. d->cl_word[0] = CDF_TOLE2(d->cl_word[0]);
  186. d->cl_word[1] = CDF_TOLE2(d->cl_word[1]);
  187. }
  188. void
  189. cdf_unpack_dir(cdf_directory_t *d, char *buf)
  190. {
  191. size_t len = 0;
  192. CDF_UNPACKA(d->d_name);
  193. CDF_UNPACK(d->d_namelen);
  194. CDF_UNPACK(d->d_type);
  195. CDF_UNPACK(d->d_color);
  196. CDF_UNPACK(d->d_left_child);
  197. CDF_UNPACK(d->d_right_child);
  198. CDF_UNPACK(d->d_storage);
  199. CDF_UNPACKA(d->d_storage_uuid);
  200. CDF_UNPACK(d->d_flags);
  201. CDF_UNPACK(d->d_created);
  202. CDF_UNPACK(d->d_modified);
  203. CDF_UNPACK(d->d_stream_first_sector);
  204. CDF_UNPACK(d->d_size);
  205. CDF_UNPACK(d->d_unused0);
  206. }
  207. static ssize_t
  208. cdf_read(const cdf_info_t *info, off_t off, void *buf, size_t len)
  209. {
  210. size_t siz = (size_t)off + len;
  211. if ((off_t)(off + len) != (off_t)siz) {
  212. errno = EINVAL;
  213. return -1;
  214. }
  215. if (info->i_buf != NULL && info->i_len >= siz) {
  216. (void)memcpy(buf, &info->i_buf[off], len);
  217. return (ssize_t)len;
  218. }
  219. if (info->i_fd == -1)
  220. return -1;
  221. if (lseek(info->i_fd, off, SEEK_SET) == (off_t)-1)
  222. return -1;
  223. if (read(info->i_fd, buf, len) != (ssize_t)len)
  224. return -1;
  225. return (ssize_t)len;
  226. }
  227. int
  228. cdf_read_header(const cdf_info_t *info, cdf_header_t *h)
  229. {
  230. char buf[512];
  231. (void)memcpy(cdf_bo.s, "\01\02\03\04", 4);
  232. if (cdf_read(info, (off_t)0, buf, sizeof(buf)) == -1)
  233. return -1;
  234. cdf_unpack_header(h, buf);
  235. cdf_swap_header(h);
  236. if (h->h_magic != CDF_MAGIC) {
  237. DPRINTF(("Bad magic 0x%llx != 0x%llx\n",
  238. (unsigned long long)h->h_magic,
  239. (unsigned long long)CDF_MAGIC));
  240. errno = EFTYPE;
  241. return -1;
  242. }
  243. return 0;
  244. }
  245. ssize_t
  246. cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
  247. const cdf_header_t *h, cdf_secid_t id)
  248. {
  249. assert((size_t)CDF_SEC_SIZE(h) == len);
  250. return cdf_read(info, (off_t)CDF_SEC_POS(h, id),
  251. ((char *)buf) + offs, len);
  252. }
  253. ssize_t
  254. cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
  255. size_t len, const cdf_header_t *h, cdf_secid_t id)
  256. {
  257. assert((size_t)CDF_SHORT_SEC_SIZE(h) == len);
  258. (void)memcpy(((char *)buf) + offs,
  259. ((const char *)sst->sst_tab) + CDF_SHORT_SEC_POS(h, id), len);
  260. return len;
  261. }
  262. /*
  263. * Read the sector allocation table.
  264. */
  265. int
  266. cdf_read_sat(const cdf_info_t *info, cdf_header_t *h, cdf_sat_t *sat)
  267. {
  268. size_t i, j, k;
  269. size_t ss = CDF_SEC_SIZE(h);
  270. cdf_secid_t *msa, mid;
  271. size_t nsatpersec = (ss / sizeof(mid)) - 1;
  272. for (i = 0; i < __arraycount(h->h_master_sat); i++)
  273. if (h->h_master_sat[i] == CDF_SECID_FREE)
  274. break;
  275. sat->sat_len = h->h_num_sectors_in_master_sat + i;
  276. DPRINTF(("sat_len = %zu ss = %zu\n", sat->sat_len, ss));
  277. if ((sat->sat_tab = calloc(sat->sat_len, ss)) == NULL)
  278. return -1;
  279. for (i = 0; i < __arraycount(h->h_master_sat); i++) {
  280. if (h->h_master_sat[i] < 0)
  281. break;
  282. if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
  283. h->h_master_sat[i]) != (ssize_t)ss) {
  284. DPRINTF(("Reading sector %d", h->h_master_sat[i]));
  285. goto out1;
  286. }
  287. }
  288. if ((msa = calloc(1, ss)) == NULL)
  289. goto out1;
  290. mid = h->h_secid_first_sector_in_master_sat;
  291. for (j = 0; j < h->h_num_sectors_in_master_sat; j++) {
  292. if (j >= CDF_LOOP_LIMIT) {
  293. DPRINTF(("Reading master sector loop limit"));
  294. errno = EFTYPE;
  295. goto out2;
  296. }
  297. if (cdf_read_sector(info, msa, 0, ss, h, mid) != (ssize_t)ss) {
  298. DPRINTF(("Reading master sector %d", mid));
  299. goto out2;
  300. }
  301. for (k = 0; k < nsatpersec; k++, i++) {
  302. if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
  303. CDF_TOLE4(msa[k])) != (ssize_t)ss) {
  304. DPRINTF(("Reading sector %d",
  305. CDF_TOLE4(msa[k])));
  306. goto out2;
  307. }
  308. }
  309. mid = CDF_TOLE4(msa[nsatpersec]);
  310. }
  311. free(msa);
  312. return 0;
  313. out2:
  314. free(msa);
  315. out1:
  316. free(sat->sat_tab);
  317. return -1;
  318. }
  319. size_t
  320. cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
  321. {
  322. size_t i, j;
  323. cdf_secid_t maxsector = (cdf_secid_t)(sat->sat_len * size);
  324. DPRINTF(("Chain:"));
  325. for (j = i = 0; sid >= 0; i++, j++) {
  326. DPRINTF((" %d", sid));
  327. if (j >= CDF_LOOP_LIMIT) {
  328. DPRINTF(("Counting chain loop limit"));
  329. errno = EFTYPE;
  330. return (size_t)-1;
  331. }
  332. if (sid > maxsector) {
  333. DPRINTF(("Sector %d > %d\n", sid, maxsector));
  334. errno = EFTYPE;
  335. return (size_t)-1;
  336. }
  337. sid = CDF_TOLE4(sat->sat_tab[sid]);
  338. }
  339. DPRINTF(("\n"));
  340. return i;
  341. }
  342. int
  343. cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
  344. const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
  345. {
  346. size_t ss = CDF_SEC_SIZE(h), i, j;
  347. ssize_t nr;
  348. scn->sst_len = cdf_count_chain(sat, sid, ss);
  349. scn->sst_dirlen = len;
  350. if (scn->sst_len == (size_t)-1)
  351. return -1;
  352. scn->sst_tab = calloc(scn->sst_len, ss);
  353. if (scn->sst_tab == NULL)
  354. return -1;
  355. for (j = i = 0; sid >= 0; i++, j++) {
  356. if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
  357. sid)) != (ssize_t)ss) {
  358. if (i == scn->sst_len - 1 && nr > 0) {
  359. /* Last sector might be truncated */
  360. return 0;
  361. }
  362. DPRINTF(("Reading long sector chain %d", sid));
  363. goto out;
  364. }
  365. sid = CDF_TOLE4(sat->sat_tab[sid]);
  366. if (j >= CDF_LOOP_LIMIT) {
  367. DPRINTF(("Read long sector chain loop limit"));
  368. errno = EFTYPE;
  369. goto out;
  370. }
  371. }
  372. return 0;
  373. out:
  374. free(scn->sst_tab);
  375. return -1;
  376. }
  377. int
  378. cdf_read_short_sector_chain(const cdf_header_t *h,
  379. const cdf_sat_t *ssat, const cdf_stream_t *sst,
  380. cdf_secid_t sid, size_t len, cdf_stream_t *scn)
  381. {
  382. size_t ss = CDF_SHORT_SEC_SIZE(h), i, j;
  383. scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h));
  384. scn->sst_dirlen = len;
  385. if (scn->sst_len == (size_t)-1)
  386. return -1;
  387. scn->sst_tab = calloc(scn->sst_len, ss);
  388. if (scn->sst_tab == NULL)
  389. return -1;
  390. for (j = i = 0; sid >= 0; i++, j++) {
  391. if (j >= CDF_LOOP_LIMIT) {
  392. DPRINTF(("Read short sector chain loop limit"));
  393. errno = EFTYPE;
  394. goto out;
  395. }
  396. if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
  397. sid) != (ssize_t)ss) {
  398. DPRINTF(("Reading short sector chain %d", sid));
  399. goto out;
  400. }
  401. sid = CDF_TOLE4(ssat->sat_tab[sid]);
  402. }
  403. return 0;
  404. out:
  405. free(scn->sst_tab);
  406. return -1;
  407. }
  408. int
  409. cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
  410. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  411. cdf_secid_t sid, size_t len, cdf_stream_t *scn)
  412. {
  413. if (len < h->h_min_size_standard_stream)
  414. return cdf_read_short_sector_chain(h, ssat, sst, sid, len,
  415. scn);
  416. else
  417. return cdf_read_long_sector_chain(info, h, sat, sid, len, scn);
  418. }
  419. int
  420. cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h,
  421. const cdf_sat_t *sat, cdf_dir_t *dir)
  422. {
  423. size_t i, j;
  424. size_t ss = CDF_SEC_SIZE(h), ns, nd;
  425. char *buf;
  426. cdf_secid_t sid = h->h_secid_first_directory;
  427. ns = cdf_count_chain(sat, sid, ss);
  428. if (ns == (size_t)-1)
  429. return -1;
  430. nd = ss / CDF_DIRECTORY_SIZE;
  431. dir->dir_len = ns * nd;
  432. dir->dir_tab = calloc(dir->dir_len, sizeof(dir->dir_tab[0]));
  433. if (dir->dir_tab == NULL)
  434. return -1;
  435. if ((buf = malloc(ss)) == NULL) {
  436. free(dir->dir_tab);
  437. return -1;
  438. }
  439. for (j = i = 0; i < ns; i++, j++) {
  440. if (j >= CDF_LOOP_LIMIT) {
  441. DPRINTF(("Read dir loop limit"));
  442. errno = EFTYPE;
  443. goto out;
  444. }
  445. if (cdf_read_sector(info, buf, 0, ss, h, sid) != (ssize_t)ss) {
  446. DPRINTF(("Reading directory sector %d", sid));
  447. goto out;
  448. }
  449. for (j = 0; j < nd; j++) {
  450. cdf_unpack_dir(&dir->dir_tab[i * nd + j],
  451. &buf[j * CDF_DIRECTORY_SIZE]);
  452. }
  453. sid = CDF_TOLE4(sat->sat_tab[sid]);
  454. }
  455. if (NEED_SWAP)
  456. for (i = 0; i < dir->dir_len; i++)
  457. cdf_swap_dir(&dir->dir_tab[i]);
  458. free(buf);
  459. return 0;
  460. out:
  461. free(dir->dir_tab);
  462. free(buf);
  463. return -1;
  464. }
  465. int
  466. cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h,
  467. const cdf_sat_t *sat, cdf_sat_t *ssat)
  468. {
  469. size_t i, j;
  470. size_t ss = CDF_SEC_SIZE(h);
  471. cdf_secid_t sid = h->h_secid_first_sector_in_short_sat;
  472. ssat->sat_len = cdf_count_chain(sat, sid, CDF_SEC_SIZE(h));
  473. if (ssat->sat_len == (size_t)-1)
  474. return -1;
  475. ssat->sat_tab = calloc(ssat->sat_len, ss);
  476. if (ssat->sat_tab == NULL)
  477. return -1;
  478. for (j = i = 0; sid >= 0; i++, j++) {
  479. if (j >= CDF_LOOP_LIMIT) {
  480. DPRINTF(("Read short sat sector loop limit"));
  481. errno = EFTYPE;
  482. goto out;
  483. }
  484. if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
  485. (ssize_t)ss) {
  486. DPRINTF(("Reading short sat sector %d", sid));
  487. goto out;
  488. }
  489. sid = CDF_TOLE4(sat->sat_tab[sid]);
  490. }
  491. return 0;
  492. out:
  493. free(ssat->sat_tab);
  494. return -1;
  495. }
  496. int
  497. cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
  498. const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn)
  499. {
  500. size_t i;
  501. const cdf_directory_t *d;
  502. for (i = 0; i < dir->dir_len; i++)
  503. if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
  504. break;
  505. /* If the it is not there, just fake it; some docs don't have it */
  506. if (i == dir->dir_len) {
  507. scn->sst_tab = NULL;
  508. scn->sst_len = 0;
  509. return 0;
  510. }
  511. d = &dir->dir_tab[i];
  512. /* If the it is not there, just fake it; some docs don't have it */
  513. if (d->d_stream_first_sector < 0) {
  514. scn->sst_tab = NULL;
  515. scn->sst_len = 0;
  516. return 0;
  517. }
  518. return cdf_read_long_sector_chain(info, h, sat,
  519. d->d_stream_first_sector, d->d_size, scn);
  520. }
  521. static int
  522. cdf_namecmp(const char *d, const uint16_t *s, size_t l)
  523. {
  524. for (; l--; d++, s++)
  525. if (*d != CDF_TOLE2(*s))
  526. return (unsigned char)*d - CDF_TOLE2(*s);
  527. return 0;
  528. }
  529. int
  530. cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
  531. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  532. const cdf_dir_t *dir, cdf_stream_t *scn)
  533. {
  534. size_t i;
  535. const cdf_directory_t *d;
  536. static const char name[] = "\05SummaryInformation";
  537. for (i = 0; i < dir->dir_len; i++)
  538. if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_USER_STREAM &&
  539. cdf_namecmp(name, dir->dir_tab[i].d_name, sizeof(name))
  540. == 0)
  541. break;
  542. if (i == dir->dir_len) {
  543. DPRINTF(("Cannot find summary information section\n"));
  544. errno = EFTYPE;
  545. return -1;
  546. }
  547. d = &dir->dir_tab[i];
  548. return cdf_read_sector_chain(info, h, sat, ssat, sst,
  549. d->d_stream_first_sector, d->d_size, scn);
  550. }
  551. int
  552. cdf_read_property_info(const cdf_stream_t *sst, uint32_t offs,
  553. cdf_property_info_t **info, size_t *count, size_t *maxcount)
  554. {
  555. const cdf_section_header_t *shp;
  556. cdf_section_header_t sh;
  557. const uint32_t *p, *q, *e;
  558. int16_t s16;
  559. int32_t s32;
  560. uint32_t u32;
  561. int64_t s64;
  562. uint64_t u64;
  563. cdf_timestamp_t tp;
  564. size_t i, o, nelements, j;
  565. cdf_property_info_t *inp;
  566. shp = (const void *)((const char *)sst->sst_tab + offs);
  567. sh.sh_len = CDF_TOLE4(shp->sh_len);
  568. sh.sh_properties = CDF_TOLE4(shp->sh_properties);
  569. DPRINTF(("section len: %d properties %d\n", sh.sh_len,
  570. sh.sh_properties));
  571. if (*maxcount) {
  572. *maxcount += sh.sh_properties;
  573. inp = realloc(*info, *maxcount * sizeof(*inp));
  574. } else {
  575. *maxcount = sh.sh_properties;
  576. inp = malloc(*maxcount * sizeof(*inp));
  577. }
  578. if (inp == NULL)
  579. goto out;
  580. *info = inp;
  581. inp += *count;
  582. *count += sh.sh_properties;
  583. p = (const void *)((const char *)sst->sst_tab + offs + sizeof(sh));
  584. e = (const void *)(((const char *)shp) + sh.sh_len);
  585. for (i = 0; i < sh.sh_properties; i++) {
  586. q = (const uint32_t *)((const char *)p +
  587. CDF_TOLE4(p[(i << 1) + 1])) - 2;
  588. if (q > e) {
  589. DPRINTF(("Ran of the end %p > %p\n", q, e));
  590. goto out;
  591. }
  592. inp[i].pi_id = CDF_TOLE4(p[i << 1]);
  593. inp[i].pi_type = CDF_TOLE4(q[0]);
  594. DPRINTF(("%d) id=%x type=%x offs=%x\n", i, inp[i].pi_id,
  595. inp[i].pi_type, (const char *)q - (const char *)p));
  596. if (inp[i].pi_type & CDF_VECTOR) {
  597. nelements = CDF_TOLE4(q[1]);
  598. o = 2;
  599. } else {
  600. nelements = 1;
  601. o = 1;
  602. }
  603. if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
  604. goto unknown;
  605. switch (inp[i].pi_type & CDF_TYPEMASK) {
  606. case CDF_EMPTY:
  607. break;
  608. case CDF_SIGNED16:
  609. if (inp[i].pi_type & CDF_VECTOR)
  610. goto unknown;
  611. (void)memcpy(&s16, &q[o], sizeof(s16));
  612. inp[i].pi_s16 = CDF_TOLE2(s16);
  613. break;
  614. case CDF_SIGNED32:
  615. if (inp[i].pi_type & CDF_VECTOR)
  616. goto unknown;
  617. (void)memcpy(&s32, &q[o], sizeof(s32));
  618. inp[i].pi_s32 = CDF_TOLE4(s32);
  619. break;
  620. case CDF_BOOL:
  621. case CDF_UNSIGNED32:
  622. if (inp[i].pi_type & CDF_VECTOR)
  623. goto unknown;
  624. (void)memcpy(&u32, &q[o], sizeof(u32));
  625. inp[i].pi_u32 = CDF_TOLE4(u32);
  626. break;
  627. case CDF_SIGNED64:
  628. if (inp[i].pi_type & CDF_VECTOR)
  629. goto unknown;
  630. (void)memcpy(&s64, &q[o], sizeof(s64));
  631. inp[i].pi_s64 = CDF_TOLE4(s64);
  632. break;
  633. case CDF_UNSIGNED64:
  634. if (inp[i].pi_type & CDF_VECTOR)
  635. goto unknown;
  636. (void)memcpy(&u64, &q[o], sizeof(u64));
  637. inp[i].pi_u64 = CDF_TOLE4(u64);
  638. break;
  639. case CDF_LENGTH32_STRING:
  640. if (nelements > 1) {
  641. size_t nelem = inp - *info;
  642. *maxcount += nelements;
  643. inp = realloc(*info, *maxcount * sizeof(*inp));
  644. if (inp == NULL)
  645. goto out;
  646. *info = inp;
  647. inp = *info + nelem;
  648. }
  649. DPRINTF(("nelements = %d\n", nelements));
  650. for (j = 0; j < nelements; j++, i++) {
  651. uint32_t l = CDF_TOLE4(q[o]);
  652. inp[i].pi_str.s_len = l;
  653. inp[i].pi_str.s_buf = (const char *)(&q[o+1]);
  654. DPRINTF(("l = %d, r = %d, s = %s\n", l,
  655. CDF_ROUND(l, sizeof(l)),
  656. inp[i].pi_str.s_buf));
  657. l = 4 + CDF_ROUND(l, sizeof(l));
  658. o += l >> 2;
  659. }
  660. i--;
  661. break;
  662. case CDF_FILETIME:
  663. if (inp[i].pi_type & CDF_VECTOR)
  664. goto unknown;
  665. (void)memcpy(&tp, &q[o], sizeof(tp));
  666. inp[i].pi_tp = CDF_TOLE8(tp);
  667. break;
  668. case CDF_CLIPBOARD:
  669. if (inp[i].pi_type & CDF_VECTOR)
  670. goto unknown;
  671. break;
  672. default:
  673. unknown:
  674. DPRINTF(("Don't know how to deal with %x\n",
  675. inp[i].pi_type));
  676. goto out;
  677. }
  678. }
  679. return 0;
  680. out:
  681. free(*info);
  682. return -1;
  683. }
  684. int
  685. cdf_unpack_summary_info(const cdf_stream_t *sst, cdf_summary_info_header_t *ssi,
  686. cdf_property_info_t **info, size_t *count)
  687. {
  688. size_t i, maxcount;
  689. const cdf_summary_info_header_t *si = sst->sst_tab;
  690. const cdf_section_declaration_t *sd = (const void *)
  691. ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET);
  692. ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
  693. ssi->si_os_version = CDF_TOLE2(si->si_os_version);
  694. ssi->si_os = CDF_TOLE2(si->si_os);
  695. ssi->si_class = si->si_class;
  696. cdf_swap_class(&ssi->si_class);
  697. ssi->si_count = CDF_TOLE2(si->si_count);
  698. *count = 0;
  699. maxcount = 0;
  700. *info = NULL;
  701. for (i = 0; i < CDF_TOLE4(si->si_count); i++) {
  702. if (i >= CDF_LOOP_LIMIT) {
  703. DPRINTF(("Unpack summary info loop limit"));
  704. errno = EFTYPE;
  705. return -1;
  706. }
  707. if (cdf_read_property_info(sst, CDF_TOLE4(sd->sd_offset),
  708. info, count, &maxcount) == -1)
  709. return -1;
  710. }
  711. return 0;
  712. }
  713. int
  714. cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
  715. {
  716. return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
  717. "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
  718. id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
  719. id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
  720. id->cl_six[5]);
  721. }
  722. static const struct {
  723. uint32_t v;
  724. const char *n;
  725. } vn[] = {
  726. { CDF_PROPERTY_CODE_PAGE, "Code page" },
  727. { CDF_PROPERTY_TITLE, "Title" },
  728. { CDF_PROPERTY_SUBJECT, "Subject" },
  729. { CDF_PROPERTY_AUTHOR, "Author" },
  730. { CDF_PROPERTY_KEYWORDS, "Keywords" },
  731. { CDF_PROPERTY_COMMENTS, "Comments" },
  732. { CDF_PROPERTY_TEMPLATE, "Template" },
  733. { CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
  734. { CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
  735. { CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
  736. { CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
  737. { CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
  738. { CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
  739. { CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
  740. { CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
  741. { CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
  742. { CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
  743. { CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
  744. { CDF_PROPERTY_SECURITY, "Security" },
  745. { CDF_PROPERTY_LOCALE_ID, "Locale ID" },
  746. };
  747. int
  748. cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
  749. {
  750. size_t i;
  751. for (i = 0; i < __arraycount(vn); i++)
  752. if (vn[i].v == p)
  753. return snprintf(buf, bufsiz, "%s", vn[i].n);
  754. return snprintf(buf, bufsiz, "0x%x", p);
  755. }
  756. int
  757. cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
  758. {
  759. size_t len = 0;
  760. int days, hours, mins, secs;
  761. ts /= CDF_TIME_PREC;
  762. secs = ts % 60;
  763. ts /= 60;
  764. mins = ts % 60;
  765. ts /= 60;
  766. hours = ts % 24;
  767. ts /= 24;
  768. days = ts;
  769. if (days) {
  770. len += snprintf(buf + len, bufsiz - len, "%dd+", days);
  771. if (len >= bufsiz)
  772. return len;
  773. }
  774. if (days || hours) {
  775. len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
  776. if (len >= bufsiz)
  777. return len;
  778. }
  779. len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
  780. if (len >= bufsiz)
  781. return len;
  782. len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
  783. return len;
  784. }
  785. #ifdef CDF_DEBUG
  786. void
  787. cdf_dump_header(const cdf_header_t *h)
  788. {
  789. size_t i;
  790. #define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
  791. DUMP("%d", revision);
  792. DUMP("%d", version);
  793. DUMP("0x%x", byte_order);
  794. DUMP("%d", sec_size_p2);
  795. DUMP("%d", short_sec_size_p2);
  796. DUMP("%d", num_sectors_in_sat);
  797. DUMP("%d", secid_first_directory);
  798. DUMP("%d", min_size_standard_stream);
  799. DUMP("%d", secid_first_sector_in_short_sat);
  800. DUMP("%d", num_sectors_in_short_sat);
  801. DUMP("%d", secid_first_sector_in_master_sat);
  802. DUMP("%d", num_sectors_in_master_sat);
  803. for (i = 0; i < __arraycount(h->h_master_sat); i++) {
  804. if (h->h_master_sat[i] == CDF_SECID_FREE)
  805. break;
  806. (void)fprintf(stderr, "%35.35s[%.3zu] = %d\n",
  807. "master_sat", i, h->h_master_sat[i]);
  808. }
  809. }
  810. void
  811. cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
  812. {
  813. size_t i, j, s = size / sizeof(cdf_secid_t);
  814. for (i = 0; i < sat->sat_len; i++) {
  815. (void)fprintf(stderr, "%s[%zu]:\n%.6d: ", prefix, i, i * s);
  816. for (j = 0; j < s; j++) {
  817. (void)fprintf(stderr, "%5d, ",
  818. CDF_TOLE4(sat->sat_tab[s * i + j]));
  819. if ((j + 1) % 10 == 0)
  820. (void)fprintf(stderr, "\n%.6d: ",
  821. i * s + j + 1);
  822. }
  823. (void)fprintf(stderr, "\n");
  824. }
  825. }
  826. void
  827. cdf_dump(void *v, size_t len)
  828. {
  829. size_t i, j;
  830. unsigned char *p = v;
  831. char abuf[16];
  832. (void)fprintf(stderr, "%.4x: ", 0);
  833. for (i = 0, j = 0; i < len; i++, p++) {
  834. (void)fprintf(stderr, "%.2x ", *p);
  835. abuf[j++] = isprint(*p) ? *p : '.';
  836. if (j == 16) {
  837. j = 0;
  838. abuf[15] = '\0';
  839. (void)fprintf(stderr, "%s\n%.4x: ", abuf, i + 1);
  840. }
  841. }
  842. (void)fprintf(stderr, "\n");
  843. }
  844. void
  845. cdf_dump_stream(const cdf_header_t *h, const cdf_stream_t *sst)
  846. {
  847. size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
  848. CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
  849. cdf_dump(sst->sst_tab, ss * sst->sst_len);
  850. }
  851. void
  852. cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
  853. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  854. const cdf_dir_t *dir)
  855. {
  856. size_t i, j;
  857. cdf_directory_t *d;
  858. char name[__arraycount(d->d_name)];
  859. cdf_stream_t scn;
  860. struct timespec ts;
  861. static const char *types[] = { "empty", "user storage",
  862. "user stream", "lockbytes", "property", "root storage" };
  863. for (i = 0; i < dir->dir_len; i++) {
  864. d = &dir->dir_tab[i];
  865. for (j = 0; j < sizeof(name); j++)
  866. name[j] = (char)CDF_TOLE2(d->d_name[j]);
  867. (void)fprintf(stderr, "Directory %zu: %s\n", i, name);
  868. if (d->d_type < __arraycount(types))
  869. (void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
  870. else
  871. (void)fprintf(stderr, "Type: %d\n", d->d_type);
  872. (void)fprintf(stderr, "Color: %s\n",
  873. d->d_color ? "black" : "red");
  874. (void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
  875. (void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
  876. (void)fprintf(stderr, "Flags: 0x%x\n", d->d_flags);
  877. cdf_timestamp_to_timespec(&ts, d->d_created);
  878. (void)fprintf(stderr, "Created %s", ctime(&ts.tv_sec));
  879. cdf_timestamp_to_timespec(&ts, d->d_modified);
  880. (void)fprintf(stderr, "Modified %s", ctime(&ts.tv_sec));
  881. (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
  882. (void)fprintf(stderr, "Size %d\n", d->d_size);
  883. switch (d->d_type) {
  884. case CDF_DIR_TYPE_USER_STORAGE:
  885. (void)fprintf(stderr, "Storage: %d\n", d->d_storage);
  886. break;
  887. case CDF_DIR_TYPE_USER_STREAM:
  888. if (sst == NULL)
  889. break;
  890. if (cdf_read_sector_chain(info, h, sat, ssat, sst,
  891. d->d_stream_first_sector, d->d_size, &scn) == -1) {
  892. warn("Can't read stream for %s at %d len %d",
  893. name, d->d_stream_first_sector, d->d_size);
  894. break;
  895. }
  896. cdf_dump_stream(h, &scn);
  897. free(scn.sst_tab);
  898. break;
  899. default:
  900. break;
  901. }
  902. }
  903. }
  904. void
  905. cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
  906. {
  907. cdf_timestamp_t tp;
  908. struct timespec ts;
  909. char buf[64];
  910. size_t i;
  911. for (i = 0; i < count; i++) {
  912. cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
  913. (void)fprintf(stderr, "%zu) %s: ", i, buf);
  914. switch (info[i].pi_type) {
  915. case CDF_SIGNED16:
  916. (void)fprintf(stderr, "signed 16 [%hd]\n",
  917. info[i].pi_s16);
  918. break;
  919. case CDF_SIGNED32:
  920. (void)fprintf(stderr, "signed 32 [%d]\n",
  921. info[i].pi_s32);
  922. break;
  923. case CDF_UNSIGNED32:
  924. (void)fprintf(stderr, "unsigned 32 [%u]\n",
  925. info[i].pi_u32);
  926. break;
  927. case CDF_LENGTH32_STRING:
  928. (void)fprintf(stderr, "string %u [%.*s]\n",
  929. info[i].pi_str.s_len,
  930. info[i].pi_str.s_len, info[i].pi_str.s_buf);
  931. break;
  932. case CDF_FILETIME:
  933. tp = info[i].pi_tp;
  934. if (tp < 1000000000000000LL) {
  935. cdf_print_elapsed_time(buf, sizeof(buf), tp);
  936. (void)fprintf(stderr, "timestamp %s\n", buf);
  937. } else {
  938. cdf_timestamp_to_timespec(&ts, tp);
  939. (void)fprintf(stderr, "timestamp %s",
  940. ctime(&ts.tv_sec));
  941. }
  942. break;
  943. case CDF_CLIPBOARD:
  944. (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
  945. break;
  946. default:
  947. DPRINTF(("Don't know how to deal with %x\n",
  948. info[i].pi_type));
  949. break;
  950. }
  951. }
  952. }
  953. void
  954. cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
  955. {
  956. char buf[128];
  957. cdf_summary_info_header_t ssi;
  958. cdf_property_info_t *info;
  959. size_t count;
  960. (void)&h;
  961. if (cdf_unpack_summary_info(sst, &ssi, &info, &count) == -1)
  962. return;
  963. (void)fprintf(stderr, "Endian: %x\n", ssi.si_byte_order);
  964. (void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
  965. ssi.si_os_version >> 8);
  966. (void)fprintf(stderr, "Os %d\n", ssi.si_os);
  967. cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
  968. (void)fprintf(stderr, "Class %s\n", buf);
  969. (void)fprintf(stderr, "Count %d\n", ssi.si_count);
  970. cdf_dump_property_info(info, count);
  971. free(info);
  972. }
  973. #endif
  974. #ifdef TEST
  975. int
  976. main(int argc, char *argv[])
  977. {
  978. int i;
  979. cdf_header_t h;
  980. cdf_sat_t sat, ssat;
  981. cdf_stream_t sst, scn;
  982. cdf_dir_t dir;
  983. cdf_info_t info;
  984. if (argc < 2) {
  985. (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
  986. return -1;
  987. }
  988. info.i_buf = NULL;
  989. info.i_len = 0;
  990. for (i = 1; i < argc; i++) {
  991. if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
  992. err(1, "Cannot open `%s'", argv[1]);
  993. if (cdf_read_header(&info, &h) == -1)
  994. err(1, "Cannot read header");
  995. #ifdef CDF_DEBUG
  996. cdf_dump_header(&h);
  997. #endif
  998. if (cdf_read_sat(&info, &h, &sat) == -1)
  999. err(1, "Cannot read sat");
  1000. #ifdef CDF_DEBUG
  1001. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  1002. #endif
  1003. if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
  1004. err(1, "Cannot read ssat");
  1005. #ifdef CDF_DEBUG
  1006. cdf_dump_sat("SSAT", &h, &ssat, CDF_SHORT_SEC_SIZE(&h));
  1007. #endif
  1008. if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
  1009. err(1, "Cannot read dir");
  1010. if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst) == -1)
  1011. err(1, "Cannot read short stream");
  1012. #ifdef CDF_DEBUG
  1013. cdf_dump_stream(&h, &sst);
  1014. #endif
  1015. #ifdef CDF_DEBUG
  1016. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  1017. #endif
  1018. if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  1019. &scn) == -1)
  1020. err(1, "Cannot read summary info");
  1021. #ifdef CDF_DEBUG
  1022. cdf_dump_summary_info(&h, &scn);
  1023. #endif
  1024. (void)close(info.i_fd);
  1025. }
  1026. return 0;
  1027. }
  1028. #endif