cdf.c 41 KB

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