cdf.c 41 KB

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