cdf.c 34 KB

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