cdf.c 38 KB

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