cdf.c 40 KB

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