cdf.c 41 KB

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