cdf.c 33 KB

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