cdf.c 33 KB

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