cdf.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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.26 2009/05/02 20:06:55 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. goto out;
  241. }
  242. if (h->h_sec_size_p2 > 20) {
  243. DPRINTF(("Bad sector size 0x%u\n", h->h_sec_size_p2));
  244. goto out;
  245. }
  246. if (h->h_short_sec_size_p2 > 20) {
  247. DPRINTF(("Bad short sector size 0x%u\n",
  248. h->h_short_sec_size_p2));
  249. goto out;
  250. }
  251. return 0;
  252. out:
  253. errno = EFTYPE;
  254. return -1;
  255. }
  256. ssize_t
  257. cdf_read_sector(const cdf_info_t *info, void *buf, size_t offs, size_t len,
  258. const cdf_header_t *h, cdf_secid_t id)
  259. {
  260. assert((size_t)CDF_SEC_SIZE(h) == len);
  261. return cdf_read(info, (off_t)CDF_SEC_POS(h, id),
  262. ((char *)buf) + offs, len);
  263. }
  264. ssize_t
  265. cdf_read_short_sector(const cdf_stream_t *sst, void *buf, size_t offs,
  266. size_t len, const cdf_header_t *h, cdf_secid_t id)
  267. {
  268. assert((size_t)CDF_SHORT_SEC_SIZE(h) == len);
  269. (void)memcpy(((char *)buf) + offs,
  270. ((const char *)sst->sst_tab) + CDF_SHORT_SEC_POS(h, id), len);
  271. return len;
  272. }
  273. /*
  274. * Read the sector allocation table.
  275. */
  276. int
  277. cdf_read_sat(const cdf_info_t *info, cdf_header_t *h, cdf_sat_t *sat)
  278. {
  279. size_t i, j, k;
  280. size_t ss = CDF_SEC_SIZE(h);
  281. cdf_secid_t *msa, mid, sec;
  282. size_t nsatpersec = (ss / sizeof(mid)) - 1;
  283. for (i = 0; i < __arraycount(h->h_master_sat); i++)
  284. if (h->h_master_sat[i] == CDF_SECID_FREE)
  285. break;
  286. #define CDF_SEC_LIMIT (UINT32_MAX / (4 * ss))
  287. if (h->h_num_sectors_in_master_sat > CDF_SEC_LIMIT ||
  288. i > CDF_SEC_LIMIT / nsatpersec) {
  289. DPRINTF(("Number of sectors in master SAT too big %u %zu\n",
  290. h->h_num_sectors_in_master_sat, i));
  291. errno = EFTYPE;
  292. return -1;
  293. }
  294. sat->sat_len = h->h_num_sectors_in_master_sat + i * nsatpersec;
  295. DPRINTF(("sat_len = %zu ss = %zu\n", sat->sat_len, ss));
  296. if ((sat->sat_tab = calloc(sat->sat_len, ss)) == NULL)
  297. return -1;
  298. for (i = 0; i < __arraycount(h->h_master_sat); i++) {
  299. if (h->h_master_sat[i] < 0)
  300. break;
  301. if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
  302. h->h_master_sat[i]) != (ssize_t)ss) {
  303. DPRINTF(("Reading sector %d", h->h_master_sat[i]));
  304. goto out1;
  305. }
  306. }
  307. if ((msa = calloc(1, ss)) == NULL)
  308. goto out1;
  309. mid = h->h_secid_first_sector_in_master_sat;
  310. for (j = 0; j < h->h_num_sectors_in_master_sat; j++) {
  311. if (j >= CDF_LOOP_LIMIT) {
  312. DPRINTF(("Reading master sector loop limit"));
  313. errno = EFTYPE;
  314. goto out2;
  315. }
  316. if (cdf_read_sector(info, msa, 0, ss, h, mid) != (ssize_t)ss) {
  317. DPRINTF(("Reading master sector %d", mid));
  318. goto out2;
  319. }
  320. for (k = 0; k < nsatpersec; k++, i++) {
  321. sec = CDF_TOLE4(msa[k]);
  322. if (sec < 0) {
  323. sat->sat_len = i;
  324. break;
  325. }
  326. if (i >= sat->sat_len) {
  327. DPRINTF(("Out of bounds reading MSA %u >= %u",
  328. i, sat->sat_len));
  329. errno = EFTYPE;
  330. goto out2;
  331. }
  332. if (cdf_read_sector(info, sat->sat_tab, ss * i, ss, h,
  333. sec) != (ssize_t)ss) {
  334. DPRINTF(("Reading sector %d",
  335. CDF_TOLE4(msa[k])));
  336. goto out2;
  337. }
  338. }
  339. mid = CDF_TOLE4(msa[nsatpersec]);
  340. }
  341. free(msa);
  342. return 0;
  343. out2:
  344. free(msa);
  345. out1:
  346. free(sat->sat_tab);
  347. return -1;
  348. }
  349. size_t
  350. cdf_count_chain(const cdf_sat_t *sat, cdf_secid_t sid, size_t size)
  351. {
  352. size_t i, j;
  353. cdf_secid_t maxsector = (cdf_secid_t)(sat->sat_len * size);
  354. DPRINTF(("Chain:"));
  355. for (j = i = 0; sid >= 0; i++, j++) {
  356. DPRINTF((" %d", sid));
  357. if (j >= CDF_LOOP_LIMIT) {
  358. DPRINTF(("Counting chain loop limit"));
  359. errno = EFTYPE;
  360. return (size_t)-1;
  361. }
  362. if (sid > maxsector) {
  363. DPRINTF(("Sector %d > %d\n", sid, maxsector));
  364. errno = EFTYPE;
  365. return (size_t)-1;
  366. }
  367. sid = CDF_TOLE4(sat->sat_tab[sid]);
  368. }
  369. DPRINTF(("\n"));
  370. return i;
  371. }
  372. int
  373. cdf_read_long_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
  374. const cdf_sat_t *sat, cdf_secid_t sid, size_t len, cdf_stream_t *scn)
  375. {
  376. size_t ss = CDF_SEC_SIZE(h), i, j;
  377. ssize_t nr;
  378. scn->sst_len = cdf_count_chain(sat, sid, ss);
  379. scn->sst_dirlen = len;
  380. if (scn->sst_len == (size_t)-1)
  381. return -1;
  382. scn->sst_tab = calloc(scn->sst_len, ss);
  383. if (scn->sst_tab == NULL)
  384. return -1;
  385. for (j = i = 0; sid >= 0; i++, j++) {
  386. if (j >= CDF_LOOP_LIMIT) {
  387. DPRINTF(("Read long sector chain loop limit"));
  388. errno = EFTYPE;
  389. goto out;
  390. }
  391. if (i >= scn->sst_len) {
  392. DPRINTF(("Out of bounds reading long sector chain "
  393. "%u > %u\n", i, scn->sst_len));
  394. errno = EFTYPE;
  395. goto out;
  396. }
  397. if ((nr = cdf_read_sector(info, scn->sst_tab, i * ss, ss, h,
  398. sid)) != (ssize_t)ss) {
  399. if (i == scn->sst_len - 1 && nr > 0) {
  400. /* Last sector might be truncated */
  401. return 0;
  402. }
  403. DPRINTF(("Reading long sector chain %d", sid));
  404. goto out;
  405. }
  406. sid = CDF_TOLE4(sat->sat_tab[sid]);
  407. }
  408. return 0;
  409. out:
  410. free(scn->sst_tab);
  411. return -1;
  412. }
  413. int
  414. cdf_read_short_sector_chain(const cdf_header_t *h,
  415. const cdf_sat_t *ssat, const cdf_stream_t *sst,
  416. cdf_secid_t sid, size_t len, cdf_stream_t *scn)
  417. {
  418. size_t ss = CDF_SHORT_SEC_SIZE(h), i, j;
  419. scn->sst_len = cdf_count_chain(ssat, sid, CDF_SEC_SIZE(h));
  420. scn->sst_dirlen = len;
  421. if (scn->sst_len == (size_t)-1)
  422. return -1;
  423. scn->sst_tab = calloc(scn->sst_len, ss);
  424. if (scn->sst_tab == NULL)
  425. return -1;
  426. for (j = i = 0; sid >= 0; i++, j++) {
  427. if (j >= CDF_LOOP_LIMIT) {
  428. DPRINTF(("Read short sector chain loop limit"));
  429. errno = EFTYPE;
  430. goto out;
  431. }
  432. if (i >= scn->sst_len) {
  433. DPRINTF(("Out of bounds reading short sector chain "
  434. "%u > %u\n", i, scn->sst_len));
  435. errno = EFTYPE;
  436. goto out;
  437. }
  438. if (cdf_read_short_sector(sst, scn->sst_tab, i * ss, ss, h,
  439. sid) != (ssize_t)ss) {
  440. DPRINTF(("Reading short sector chain %d", sid));
  441. goto out;
  442. }
  443. sid = CDF_TOLE4(ssat->sat_tab[sid]);
  444. }
  445. return 0;
  446. out:
  447. free(scn->sst_tab);
  448. return -1;
  449. }
  450. int
  451. cdf_read_sector_chain(const cdf_info_t *info, const cdf_header_t *h,
  452. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  453. cdf_secid_t sid, size_t len, cdf_stream_t *scn)
  454. {
  455. if (len < h->h_min_size_standard_stream)
  456. return cdf_read_short_sector_chain(h, ssat, sst, sid, len,
  457. scn);
  458. else
  459. return cdf_read_long_sector_chain(info, h, sat, sid, len, scn);
  460. }
  461. int
  462. cdf_read_dir(const cdf_info_t *info, const cdf_header_t *h,
  463. const cdf_sat_t *sat, cdf_dir_t *dir)
  464. {
  465. size_t i, j;
  466. size_t ss = CDF_SEC_SIZE(h), ns, nd;
  467. char *buf;
  468. cdf_secid_t sid = h->h_secid_first_directory;
  469. ns = cdf_count_chain(sat, sid, ss);
  470. if (ns == (size_t)-1)
  471. return -1;
  472. nd = ss / CDF_DIRECTORY_SIZE;
  473. dir->dir_len = ns * nd;
  474. dir->dir_tab = calloc(dir->dir_len, sizeof(dir->dir_tab[0]));
  475. if (dir->dir_tab == NULL)
  476. return -1;
  477. if ((buf = malloc(ss)) == NULL) {
  478. free(dir->dir_tab);
  479. return -1;
  480. }
  481. for (j = i = 0; i < ns; i++, j++) {
  482. if (j >= CDF_LOOP_LIMIT) {
  483. DPRINTF(("Read dir loop limit"));
  484. errno = EFTYPE;
  485. goto out;
  486. }
  487. if (cdf_read_sector(info, buf, 0, ss, h, sid) != (ssize_t)ss) {
  488. DPRINTF(("Reading directory sector %d", sid));
  489. goto out;
  490. }
  491. for (j = 0; j < nd; j++) {
  492. cdf_unpack_dir(&dir->dir_tab[i * nd + j],
  493. &buf[j * CDF_DIRECTORY_SIZE]);
  494. }
  495. sid = CDF_TOLE4(sat->sat_tab[sid]);
  496. }
  497. if (NEED_SWAP)
  498. for (i = 0; i < dir->dir_len; i++)
  499. cdf_swap_dir(&dir->dir_tab[i]);
  500. free(buf);
  501. return 0;
  502. out:
  503. free(dir->dir_tab);
  504. free(buf);
  505. return -1;
  506. }
  507. int
  508. cdf_read_ssat(const cdf_info_t *info, const cdf_header_t *h,
  509. const cdf_sat_t *sat, cdf_sat_t *ssat)
  510. {
  511. size_t i, j;
  512. size_t ss = CDF_SEC_SIZE(h);
  513. cdf_secid_t sid = h->h_secid_first_sector_in_short_sat;
  514. ssat->sat_len = cdf_count_chain(sat, sid, CDF_SEC_SIZE(h));
  515. if (ssat->sat_len == (size_t)-1)
  516. return -1;
  517. ssat->sat_tab = calloc(ssat->sat_len, ss);
  518. if (ssat->sat_tab == NULL)
  519. return -1;
  520. for (j = i = 0; sid >= 0; i++, j++) {
  521. if (j >= CDF_LOOP_LIMIT) {
  522. DPRINTF(("Read short sat sector loop limit"));
  523. errno = EFTYPE;
  524. goto out;
  525. }
  526. if (i >= ssat->sat_len) {
  527. DPRINTF(("Out of bounds reading short sector chain "
  528. "%u > %u\n", i, ssat->sat_len));
  529. errno = EFTYPE;
  530. goto out;
  531. }
  532. if (cdf_read_sector(info, ssat->sat_tab, i * ss, ss, h, sid) !=
  533. (ssize_t)ss) {
  534. DPRINTF(("Reading short sat sector %d", sid));
  535. goto out;
  536. }
  537. sid = CDF_TOLE4(sat->sat_tab[sid]);
  538. }
  539. return 0;
  540. out:
  541. free(ssat->sat_tab);
  542. return -1;
  543. }
  544. int
  545. cdf_read_short_stream(const cdf_info_t *info, const cdf_header_t *h,
  546. const cdf_sat_t *sat, const cdf_dir_t *dir, cdf_stream_t *scn)
  547. {
  548. size_t i;
  549. const cdf_directory_t *d;
  550. for (i = 0; i < dir->dir_len; i++)
  551. if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_ROOT_STORAGE)
  552. break;
  553. /* If the it is not there, just fake it; some docs don't have it */
  554. if (i == dir->dir_len) {
  555. scn->sst_tab = NULL;
  556. scn->sst_len = 0;
  557. return 0;
  558. }
  559. d = &dir->dir_tab[i];
  560. /* If the it is not there, just fake it; some docs don't have it */
  561. if (d->d_stream_first_sector < 0) {
  562. scn->sst_tab = NULL;
  563. scn->sst_len = 0;
  564. return 0;
  565. }
  566. return cdf_read_long_sector_chain(info, h, sat,
  567. d->d_stream_first_sector, d->d_size, scn);
  568. }
  569. static int
  570. cdf_namecmp(const char *d, const uint16_t *s, size_t l)
  571. {
  572. for (; l--; d++, s++)
  573. if (*d != CDF_TOLE2(*s))
  574. return (unsigned char)*d - CDF_TOLE2(*s);
  575. return 0;
  576. }
  577. int
  578. cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
  579. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  580. const cdf_dir_t *dir, cdf_stream_t *scn)
  581. {
  582. size_t i;
  583. const cdf_directory_t *d;
  584. static const char name[] = "\05SummaryInformation";
  585. for (i = 0; i < dir->dir_len; i++)
  586. if (dir->dir_tab[i].d_type == CDF_DIR_TYPE_USER_STREAM &&
  587. cdf_namecmp(name, dir->dir_tab[i].d_name, sizeof(name))
  588. == 0)
  589. break;
  590. if (i == dir->dir_len) {
  591. DPRINTF(("Cannot find summary information section\n"));
  592. errno = EFTYPE;
  593. return -1;
  594. }
  595. d = &dir->dir_tab[i];
  596. return cdf_read_sector_chain(info, h, sat, ssat, sst,
  597. d->d_stream_first_sector, d->d_size, scn);
  598. }
  599. int
  600. cdf_read_property_info(const cdf_stream_t *sst, uint32_t offs,
  601. cdf_property_info_t **info, size_t *count, size_t *maxcount)
  602. {
  603. const cdf_section_header_t *shp;
  604. cdf_section_header_t sh;
  605. const uint32_t *p, *q, *e;
  606. int16_t s16;
  607. int32_t s32;
  608. uint32_t u32;
  609. int64_t s64;
  610. uint64_t u64;
  611. cdf_timestamp_t tp;
  612. size_t i, o, nelements, j;
  613. cdf_property_info_t *inp;
  614. shp = (const void *)((const char *)sst->sst_tab + offs);
  615. sh.sh_len = CDF_TOLE4(shp->sh_len);
  616. sh.sh_properties = CDF_TOLE4(shp->sh_properties);
  617. #define CDF_PROP_LIM (UINT32_MAX / (4 * sizeof(*inp)))
  618. if (sh.sh_properties > CDF_PROP_LIM)
  619. goto out;
  620. DPRINTF(("section len: %u properties %u\n", sh.sh_len,
  621. sh.sh_properties));
  622. if (*maxcount) {
  623. if (*maxcount > CDF_PROP_LIM)
  624. goto out;
  625. *maxcount += sh.sh_properties;
  626. inp = realloc(*info, *maxcount * sizeof(*inp));
  627. } else {
  628. *maxcount = sh.sh_properties;
  629. inp = malloc(*maxcount * sizeof(*inp));
  630. }
  631. if (inp == NULL)
  632. goto out;
  633. *info = inp;
  634. inp += *count;
  635. *count += sh.sh_properties;
  636. p = (const void *)((const char *)sst->sst_tab + offs + sizeof(sh));
  637. e = (const void *)(((const char *)shp) + sh.sh_len);
  638. for (i = 0; i < sh.sh_properties; i++) {
  639. q = (const uint32_t *)((const char *)p +
  640. CDF_TOLE4(p[(i << 1) + 1])) - 2;
  641. if (q > e) {
  642. DPRINTF(("Ran of the end %p > %p\n", q, e));
  643. goto out;
  644. }
  645. inp[i].pi_id = CDF_TOLE4(p[i << 1]);
  646. inp[i].pi_type = CDF_TOLE4(q[0]);
  647. DPRINTF(("%d) id=%x type=%x offs=%x\n", i, inp[i].pi_id,
  648. inp[i].pi_type, (const char *)q - (const char *)p));
  649. if (inp[i].pi_type & CDF_VECTOR) {
  650. nelements = CDF_TOLE4(q[1]);
  651. o = 2;
  652. } else {
  653. nelements = 1;
  654. o = 1;
  655. }
  656. if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
  657. goto unknown;
  658. switch (inp[i].pi_type & CDF_TYPEMASK) {
  659. case CDF_EMPTY:
  660. break;
  661. case CDF_SIGNED16:
  662. if (inp[i].pi_type & CDF_VECTOR)
  663. goto unknown;
  664. (void)memcpy(&s16, &q[o], sizeof(s16));
  665. inp[i].pi_s16 = CDF_TOLE2(s16);
  666. break;
  667. case CDF_SIGNED32:
  668. if (inp[i].pi_type & CDF_VECTOR)
  669. goto unknown;
  670. (void)memcpy(&s32, &q[o], sizeof(s32));
  671. inp[i].pi_s32 = CDF_TOLE4(s32);
  672. break;
  673. case CDF_BOOL:
  674. case CDF_UNSIGNED32:
  675. if (inp[i].pi_type & CDF_VECTOR)
  676. goto unknown;
  677. (void)memcpy(&u32, &q[o], sizeof(u32));
  678. inp[i].pi_u32 = CDF_TOLE4(u32);
  679. break;
  680. case CDF_SIGNED64:
  681. if (inp[i].pi_type & CDF_VECTOR)
  682. goto unknown;
  683. (void)memcpy(&s64, &q[o], sizeof(s64));
  684. inp[i].pi_s64 = CDF_TOLE4(s64);
  685. break;
  686. case CDF_UNSIGNED64:
  687. if (inp[i].pi_type & CDF_VECTOR)
  688. goto unknown;
  689. (void)memcpy(&u64, &q[o], sizeof(u64));
  690. inp[i].pi_u64 = CDF_TOLE4(u64);
  691. break;
  692. case CDF_LENGTH32_STRING:
  693. if (nelements > 1) {
  694. size_t nelem = inp - *info;
  695. if (*maxcount > CDF_PROP_LIM
  696. || nelements > CDF_PROP_LIM)
  697. goto out;
  698. *maxcount += nelements;
  699. inp = realloc(*info, *maxcount * sizeof(*inp));
  700. if (inp == NULL)
  701. goto out;
  702. *info = inp;
  703. inp = *info + nelem;
  704. }
  705. DPRINTF(("nelements = %d\n", nelements));
  706. for (j = 0; j < nelements; j++, i++) {
  707. uint32_t l = CDF_TOLE4(q[o]);
  708. inp[i].pi_str.s_len = l;
  709. inp[i].pi_str.s_buf = (const char *)(&q[o+1]);
  710. DPRINTF(("l = %d, r = %d, s = %s\n", l,
  711. CDF_ROUND(l, sizeof(l)),
  712. inp[i].pi_str.s_buf));
  713. l = 4 + CDF_ROUND(l, sizeof(l));
  714. o += l >> 2;
  715. }
  716. i--;
  717. break;
  718. case CDF_FILETIME:
  719. if (inp[i].pi_type & CDF_VECTOR)
  720. goto unknown;
  721. (void)memcpy(&tp, &q[o], sizeof(tp));
  722. inp[i].pi_tp = CDF_TOLE8(tp);
  723. break;
  724. case CDF_CLIPBOARD:
  725. if (inp[i].pi_type & CDF_VECTOR)
  726. goto unknown;
  727. break;
  728. default:
  729. unknown:
  730. DPRINTF(("Don't know how to deal with %x\n",
  731. inp[i].pi_type));
  732. goto out;
  733. }
  734. }
  735. return 0;
  736. out:
  737. free(*info);
  738. return -1;
  739. }
  740. int
  741. cdf_unpack_summary_info(const cdf_stream_t *sst, cdf_summary_info_header_t *ssi,
  742. cdf_property_info_t **info, size_t *count)
  743. {
  744. size_t i, maxcount;
  745. const cdf_summary_info_header_t *si = sst->sst_tab;
  746. const cdf_section_declaration_t *sd = (const void *)
  747. ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET);
  748. ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
  749. ssi->si_os_version = CDF_TOLE2(si->si_os_version);
  750. ssi->si_os = CDF_TOLE2(si->si_os);
  751. ssi->si_class = si->si_class;
  752. cdf_swap_class(&ssi->si_class);
  753. ssi->si_count = CDF_TOLE2(si->si_count);
  754. *count = 0;
  755. maxcount = 0;
  756. *info = NULL;
  757. for (i = 0; i < CDF_TOLE4(si->si_count); i++) {
  758. if (i >= CDF_LOOP_LIMIT) {
  759. DPRINTF(("Unpack summary info loop limit"));
  760. errno = EFTYPE;
  761. return -1;
  762. }
  763. if (cdf_read_property_info(sst, CDF_TOLE4(sd->sd_offset),
  764. info, count, &maxcount) == -1)
  765. return -1;
  766. }
  767. return 0;
  768. }
  769. int
  770. cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
  771. {
  772. return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
  773. "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
  774. id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
  775. id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
  776. id->cl_six[5]);
  777. }
  778. static const struct {
  779. uint32_t v;
  780. const char *n;
  781. } vn[] = {
  782. { CDF_PROPERTY_CODE_PAGE, "Code page" },
  783. { CDF_PROPERTY_TITLE, "Title" },
  784. { CDF_PROPERTY_SUBJECT, "Subject" },
  785. { CDF_PROPERTY_AUTHOR, "Author" },
  786. { CDF_PROPERTY_KEYWORDS, "Keywords" },
  787. { CDF_PROPERTY_COMMENTS, "Comments" },
  788. { CDF_PROPERTY_TEMPLATE, "Template" },
  789. { CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
  790. { CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
  791. { CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
  792. { CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
  793. { CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
  794. { CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
  795. { CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
  796. { CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
  797. { CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
  798. { CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
  799. { CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
  800. { CDF_PROPERTY_SECURITY, "Security" },
  801. { CDF_PROPERTY_LOCALE_ID, "Locale ID" },
  802. };
  803. int
  804. cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
  805. {
  806. size_t i;
  807. for (i = 0; i < __arraycount(vn); i++)
  808. if (vn[i].v == p)
  809. return snprintf(buf, bufsiz, "%s", vn[i].n);
  810. return snprintf(buf, bufsiz, "0x%x", p);
  811. }
  812. int
  813. cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
  814. {
  815. size_t len = 0;
  816. int days, hours, mins, secs;
  817. ts /= CDF_TIME_PREC;
  818. secs = ts % 60;
  819. ts /= 60;
  820. mins = ts % 60;
  821. ts /= 60;
  822. hours = ts % 24;
  823. ts /= 24;
  824. days = ts;
  825. if (days) {
  826. len += snprintf(buf + len, bufsiz - len, "%dd+", days);
  827. if (len >= bufsiz)
  828. return len;
  829. }
  830. if (days || hours) {
  831. len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
  832. if (len >= bufsiz)
  833. return len;
  834. }
  835. len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
  836. if (len >= bufsiz)
  837. return len;
  838. len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
  839. return len;
  840. }
  841. #ifdef CDF_DEBUG
  842. void
  843. cdf_dump_header(const cdf_header_t *h)
  844. {
  845. size_t i;
  846. #define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
  847. DUMP("%d", revision);
  848. DUMP("%d", version);
  849. DUMP("0x%x", byte_order);
  850. DUMP("%d", sec_size_p2);
  851. DUMP("%d", short_sec_size_p2);
  852. DUMP("%d", num_sectors_in_sat);
  853. DUMP("%d", secid_first_directory);
  854. DUMP("%d", min_size_standard_stream);
  855. DUMP("%d", secid_first_sector_in_short_sat);
  856. DUMP("%d", num_sectors_in_short_sat);
  857. DUMP("%d", secid_first_sector_in_master_sat);
  858. DUMP("%d", num_sectors_in_master_sat);
  859. for (i = 0; i < __arraycount(h->h_master_sat); i++) {
  860. if (h->h_master_sat[i] == CDF_SECID_FREE)
  861. break;
  862. (void)fprintf(stderr, "%35.35s[%.3zu] = %d\n",
  863. "master_sat", i, h->h_master_sat[i]);
  864. }
  865. }
  866. void
  867. cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
  868. {
  869. size_t i, j, s = size / sizeof(cdf_secid_t);
  870. for (i = 0; i < sat->sat_len; i++) {
  871. (void)fprintf(stderr, "%s[%zu]:\n%.6d: ", prefix, i, i * s);
  872. for (j = 0; j < s; j++) {
  873. (void)fprintf(stderr, "%5d, ",
  874. CDF_TOLE4(sat->sat_tab[s * i + j]));
  875. if ((j + 1) % 10 == 0)
  876. (void)fprintf(stderr, "\n%.6d: ",
  877. i * s + j + 1);
  878. }
  879. (void)fprintf(stderr, "\n");
  880. }
  881. }
  882. void
  883. cdf_dump(void *v, size_t len)
  884. {
  885. size_t i, j;
  886. unsigned char *p = v;
  887. char abuf[16];
  888. (void)fprintf(stderr, "%.4x: ", 0);
  889. for (i = 0, j = 0; i < len; i++, p++) {
  890. (void)fprintf(stderr, "%.2x ", *p);
  891. abuf[j++] = isprint(*p) ? *p : '.';
  892. if (j == 16) {
  893. j = 0;
  894. abuf[15] = '\0';
  895. (void)fprintf(stderr, "%s\n%.4x: ", abuf, i + 1);
  896. }
  897. }
  898. (void)fprintf(stderr, "\n");
  899. }
  900. void
  901. cdf_dump_stream(const cdf_header_t *h, const cdf_stream_t *sst)
  902. {
  903. size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ?
  904. CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h);
  905. cdf_dump(sst->sst_tab, ss * sst->sst_len);
  906. }
  907. void
  908. cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
  909. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  910. const cdf_dir_t *dir)
  911. {
  912. size_t i, j;
  913. cdf_directory_t *d;
  914. char name[__arraycount(d->d_name)];
  915. cdf_stream_t scn;
  916. struct timespec ts;
  917. static const char *types[] = { "empty", "user storage",
  918. "user stream", "lockbytes", "property", "root storage" };
  919. for (i = 0; i < dir->dir_len; i++) {
  920. d = &dir->dir_tab[i];
  921. for (j = 0; j < sizeof(name); j++)
  922. name[j] = (char)CDF_TOLE2(d->d_name[j]);
  923. (void)fprintf(stderr, "Directory %zu: %s\n", i, name);
  924. if (d->d_type < __arraycount(types))
  925. (void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
  926. else
  927. (void)fprintf(stderr, "Type: %d\n", d->d_type);
  928. (void)fprintf(stderr, "Color: %s\n",
  929. d->d_color ? "black" : "red");
  930. (void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
  931. (void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
  932. (void)fprintf(stderr, "Flags: 0x%x\n", d->d_flags);
  933. cdf_timestamp_to_timespec(&ts, d->d_created);
  934. (void)fprintf(stderr, "Created %s", ctime(&ts.tv_sec));
  935. cdf_timestamp_to_timespec(&ts, d->d_modified);
  936. (void)fprintf(stderr, "Modified %s", ctime(&ts.tv_sec));
  937. (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
  938. (void)fprintf(stderr, "Size %d\n", d->d_size);
  939. switch (d->d_type) {
  940. case CDF_DIR_TYPE_USER_STORAGE:
  941. (void)fprintf(stderr, "Storage: %d\n", d->d_storage);
  942. break;
  943. case CDF_DIR_TYPE_USER_STREAM:
  944. if (sst == NULL)
  945. break;
  946. if (cdf_read_sector_chain(info, h, sat, ssat, sst,
  947. d->d_stream_first_sector, d->d_size, &scn) == -1) {
  948. warn("Can't read stream for %s at %d len %d",
  949. name, d->d_stream_first_sector, d->d_size);
  950. break;
  951. }
  952. cdf_dump_stream(h, &scn);
  953. free(scn.sst_tab);
  954. break;
  955. default:
  956. break;
  957. }
  958. }
  959. }
  960. void
  961. cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
  962. {
  963. cdf_timestamp_t tp;
  964. struct timespec ts;
  965. char buf[64];
  966. size_t i;
  967. for (i = 0; i < count; i++) {
  968. cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
  969. (void)fprintf(stderr, "%zu) %s: ", i, buf);
  970. switch (info[i].pi_type) {
  971. case CDF_SIGNED16:
  972. (void)fprintf(stderr, "signed 16 [%hd]\n",
  973. info[i].pi_s16);
  974. break;
  975. case CDF_SIGNED32:
  976. (void)fprintf(stderr, "signed 32 [%d]\n",
  977. info[i].pi_s32);
  978. break;
  979. case CDF_UNSIGNED32:
  980. (void)fprintf(stderr, "unsigned 32 [%u]\n",
  981. info[i].pi_u32);
  982. break;
  983. case CDF_LENGTH32_STRING:
  984. (void)fprintf(stderr, "string %u [%.*s]\n",
  985. info[i].pi_str.s_len,
  986. info[i].pi_str.s_len, info[i].pi_str.s_buf);
  987. break;
  988. case CDF_FILETIME:
  989. tp = info[i].pi_tp;
  990. if (tp < 1000000000000000LL) {
  991. cdf_print_elapsed_time(buf, sizeof(buf), tp);
  992. (void)fprintf(stderr, "timestamp %s\n", buf);
  993. } else {
  994. cdf_timestamp_to_timespec(&ts, tp);
  995. (void)fprintf(stderr, "timestamp %s",
  996. ctime(&ts.tv_sec));
  997. }
  998. break;
  999. case CDF_CLIPBOARD:
  1000. (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
  1001. break;
  1002. default:
  1003. DPRINTF(("Don't know how to deal with %x\n",
  1004. info[i].pi_type));
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. void
  1010. cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
  1011. {
  1012. char buf[128];
  1013. cdf_summary_info_header_t ssi;
  1014. cdf_property_info_t *info;
  1015. size_t count;
  1016. (void)&h;
  1017. if (cdf_unpack_summary_info(sst, &ssi, &info, &count) == -1)
  1018. return;
  1019. (void)fprintf(stderr, "Endian: %x\n", ssi.si_byte_order);
  1020. (void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
  1021. ssi.si_os_version >> 8);
  1022. (void)fprintf(stderr, "Os %d\n", ssi.si_os);
  1023. cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
  1024. (void)fprintf(stderr, "Class %s\n", buf);
  1025. (void)fprintf(stderr, "Count %d\n", ssi.si_count);
  1026. cdf_dump_property_info(info, count);
  1027. free(info);
  1028. }
  1029. #endif
  1030. #ifdef TEST
  1031. int
  1032. main(int argc, char *argv[])
  1033. {
  1034. int i;
  1035. cdf_header_t h;
  1036. cdf_sat_t sat, ssat;
  1037. cdf_stream_t sst, scn;
  1038. cdf_dir_t dir;
  1039. cdf_info_t info;
  1040. if (argc < 2) {
  1041. (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
  1042. return -1;
  1043. }
  1044. info.i_buf = NULL;
  1045. info.i_len = 0;
  1046. for (i = 1; i < argc; i++) {
  1047. if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
  1048. err(1, "Cannot open `%s'", argv[1]);
  1049. if (cdf_read_header(&info, &h) == -1)
  1050. err(1, "Cannot read header");
  1051. #ifdef CDF_DEBUG
  1052. cdf_dump_header(&h);
  1053. #endif
  1054. if (cdf_read_sat(&info, &h, &sat) == -1)
  1055. err(1, "Cannot read sat");
  1056. #ifdef CDF_DEBUG
  1057. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  1058. #endif
  1059. if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
  1060. err(1, "Cannot read ssat");
  1061. #ifdef CDF_DEBUG
  1062. cdf_dump_sat("SSAT", &h, &ssat, CDF_SHORT_SEC_SIZE(&h));
  1063. #endif
  1064. if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
  1065. err(1, "Cannot read dir");
  1066. if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst) == -1)
  1067. err(1, "Cannot read short stream");
  1068. #ifdef CDF_DEBUG
  1069. cdf_dump_stream(&h, &sst);
  1070. #endif
  1071. #ifdef CDF_DEBUG
  1072. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  1073. #endif
  1074. if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  1075. &scn) == -1)
  1076. err(1, "Cannot read summary info");
  1077. #ifdef CDF_DEBUG
  1078. cdf_dump_summary_info(&h, &scn);
  1079. #endif
  1080. (void)close(info.i_fd);
  1081. }
  1082. return 0;
  1083. }
  1084. #endif