cdf.c 41 KB

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