cdf.c 39 KB

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