cdf.c 37 KB

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