cdf.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  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.82 2016/06/01 22:25:25 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. static 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. goto out;
  661. d = &dir->dir_tab[i];
  662. *root = d;
  663. /* If the it is not there, just fake it; some docs don't have it */
  664. if (d->d_stream_first_sector < 0)
  665. goto out;
  666. return cdf_read_long_sector_chain(info, h, sat,
  667. d->d_stream_first_sector, d->d_size, scn);
  668. out:
  669. scn->sst_tab = NULL;
  670. (void)cdf_zero_stream(scn);
  671. errno = EFTYPE;
  672. return -1;
  673. }
  674. static int
  675. cdf_namecmp(const char *d, const uint16_t *s, size_t l)
  676. {
  677. for (; l--; d++, s++)
  678. if (*d != CDF_TOLE2(*s))
  679. return (unsigned char)*d - CDF_TOLE2(*s);
  680. return 0;
  681. }
  682. int
  683. cdf_read_summary_info(const cdf_info_t *info, const cdf_header_t *h,
  684. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  685. const cdf_dir_t *dir, cdf_stream_t *scn)
  686. {
  687. return cdf_read_user_stream(info, h, sat, ssat, sst, dir,
  688. "\05SummaryInformation", scn);
  689. }
  690. int
  691. cdf_read_user_stream(const cdf_info_t *info, const cdf_header_t *h,
  692. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  693. const cdf_dir_t *dir, const char *name, cdf_stream_t *scn)
  694. {
  695. const cdf_directory_t *d;
  696. int i = cdf_find_stream(dir, name, CDF_DIR_TYPE_USER_STREAM);
  697. if (i <= 0) {
  698. memset(scn, 0, sizeof(*scn));
  699. return -1;
  700. }
  701. d = &dir->dir_tab[i - 1];
  702. return cdf_read_sector_chain(info, h, sat, ssat, sst,
  703. d->d_stream_first_sector, d->d_size, scn);
  704. }
  705. int
  706. cdf_find_stream(const cdf_dir_t *dir, const char *name, int type)
  707. {
  708. size_t i, name_len = strlen(name) + 1;
  709. for (i = dir->dir_len; i > 0; i--)
  710. if (dir->dir_tab[i - 1].d_type == type &&
  711. cdf_namecmp(name, dir->dir_tab[i - 1].d_name, name_len)
  712. == 0)
  713. break;
  714. if (i > 0)
  715. return i;
  716. DPRINTF(("Cannot find type %d `%s'\n", type, name));
  717. errno = ESRCH;
  718. return 0;
  719. }
  720. int
  721. cdf_read_property_info(const cdf_stream_t *sst, const cdf_header_t *h,
  722. uint32_t offs, cdf_property_info_t **info, size_t *count, size_t *maxcount)
  723. {
  724. const cdf_section_header_t *shp;
  725. cdf_section_header_t sh;
  726. const uint8_t *p, *q, *e;
  727. int16_t s16;
  728. int32_t s32;
  729. uint32_t u32;
  730. int64_t s64;
  731. uint64_t u64;
  732. cdf_timestamp_t tp;
  733. size_t i, o, o4, nelements, j;
  734. cdf_property_info_t *inp;
  735. if (offs > UINT32_MAX / 4) {
  736. errno = EFTYPE;
  737. goto out;
  738. }
  739. shp = CAST(const cdf_section_header_t *, (const void *)
  740. ((const char *)sst->sst_tab + offs));
  741. if (cdf_check_stream_offset(sst, h, shp, sizeof(*shp), __LINE__) == -1)
  742. goto out;
  743. sh.sh_len = CDF_TOLE4(shp->sh_len);
  744. #define CDF_SHLEN_LIMIT (UINT32_MAX / 8)
  745. if (sh.sh_len > CDF_SHLEN_LIMIT) {
  746. errno = EFTYPE;
  747. goto out;
  748. }
  749. sh.sh_properties = CDF_TOLE4(shp->sh_properties);
  750. #define CDF_PROP_LIMIT (UINT32_MAX / (4 * sizeof(*inp)))
  751. if (sh.sh_properties > CDF_PROP_LIMIT)
  752. goto out;
  753. DPRINTF(("section len: %u properties %u\n", sh.sh_len,
  754. sh.sh_properties));
  755. if (*maxcount) {
  756. if (*maxcount > CDF_PROP_LIMIT)
  757. goto out;
  758. *maxcount += sh.sh_properties;
  759. inp = CAST(cdf_property_info_t *,
  760. realloc(*info, *maxcount * sizeof(*inp)));
  761. } else {
  762. *maxcount = sh.sh_properties;
  763. inp = CAST(cdf_property_info_t *,
  764. malloc(*maxcount * sizeof(*inp)));
  765. }
  766. if (inp == NULL)
  767. goto out1;
  768. *info = inp;
  769. inp += *count;
  770. *count += sh.sh_properties;
  771. p = CAST(const uint8_t *, (const void *)
  772. ((const char *)(const void *)sst->sst_tab +
  773. offs + sizeof(sh)));
  774. e = CAST(const uint8_t *, (const void *)
  775. (((const char *)(const void *)shp) + sh.sh_len));
  776. if (cdf_check_stream_offset(sst, h, e, 0, __LINE__) == -1)
  777. goto out;
  778. for (i = 0; i < sh.sh_properties; i++) {
  779. size_t tail = (i << 1) + 1;
  780. size_t ofs;
  781. if (cdf_check_stream_offset(sst, h, p, tail * sizeof(uint32_t),
  782. __LINE__) == -1)
  783. goto out;
  784. ofs = CDF_GETUINT32(p, tail);
  785. q = (const uint8_t *)(const void *)
  786. ((const char *)(const void *)p + ofs
  787. - 2 * sizeof(uint32_t));
  788. if (q < p) {
  789. DPRINTF(("Wrapped around %p < %p\n", q, p));
  790. goto out;
  791. }
  792. if (q > e) {
  793. DPRINTF(("Ran of the end %p > %p\n", q, e));
  794. goto out;
  795. }
  796. inp[i].pi_id = CDF_GETUINT32(p, i << 1);
  797. inp[i].pi_type = CDF_GETUINT32(q, 0);
  798. DPRINTF(("%" SIZE_T_FORMAT "u) id=%x type=%x offs=0x%tx,0x%x\n",
  799. i, inp[i].pi_id, inp[i].pi_type, q - p, offs));
  800. if (inp[i].pi_type & CDF_VECTOR) {
  801. nelements = CDF_GETUINT32(q, 1);
  802. if (nelements == 0) {
  803. DPRINTF(("CDF_VECTOR with nelements == 0\n"));
  804. goto out;
  805. }
  806. o = 2;
  807. } else {
  808. nelements = 1;
  809. o = 1;
  810. }
  811. o4 = o * sizeof(uint32_t);
  812. if (inp[i].pi_type & (CDF_ARRAY|CDF_BYREF|CDF_RESERVED))
  813. goto unknown;
  814. switch (inp[i].pi_type & CDF_TYPEMASK) {
  815. case CDF_NULL:
  816. case CDF_EMPTY:
  817. break;
  818. case CDF_SIGNED16:
  819. if (inp[i].pi_type & CDF_VECTOR)
  820. goto unknown;
  821. (void)memcpy(&s16, &q[o4], sizeof(s16));
  822. inp[i].pi_s16 = CDF_TOLE2(s16);
  823. break;
  824. case CDF_SIGNED32:
  825. if (inp[i].pi_type & CDF_VECTOR)
  826. goto unknown;
  827. (void)memcpy(&s32, &q[o4], sizeof(s32));
  828. inp[i].pi_s32 = CDF_TOLE4((uint32_t)s32);
  829. break;
  830. case CDF_BOOL:
  831. case CDF_UNSIGNED32:
  832. if (inp[i].pi_type & CDF_VECTOR)
  833. goto unknown;
  834. (void)memcpy(&u32, &q[o4], sizeof(u32));
  835. inp[i].pi_u32 = CDF_TOLE4(u32);
  836. break;
  837. case CDF_SIGNED64:
  838. if (inp[i].pi_type & CDF_VECTOR)
  839. goto unknown;
  840. (void)memcpy(&s64, &q[o4], sizeof(s64));
  841. inp[i].pi_s64 = CDF_TOLE8((uint64_t)s64);
  842. break;
  843. case CDF_UNSIGNED64:
  844. if (inp[i].pi_type & CDF_VECTOR)
  845. goto unknown;
  846. (void)memcpy(&u64, &q[o4], sizeof(u64));
  847. inp[i].pi_u64 = CDF_TOLE8((uint64_t)u64);
  848. break;
  849. case CDF_FLOAT:
  850. if (inp[i].pi_type & CDF_VECTOR)
  851. goto unknown;
  852. (void)memcpy(&u32, &q[o4], sizeof(u32));
  853. u32 = CDF_TOLE4(u32);
  854. memcpy(&inp[i].pi_f, &u32, sizeof(inp[i].pi_f));
  855. break;
  856. case CDF_DOUBLE:
  857. if (inp[i].pi_type & CDF_VECTOR)
  858. goto unknown;
  859. (void)memcpy(&u64, &q[o4], sizeof(u64));
  860. u64 = CDF_TOLE8((uint64_t)u64);
  861. memcpy(&inp[i].pi_d, &u64, sizeof(inp[i].pi_d));
  862. break;
  863. case CDF_LENGTH32_STRING:
  864. case CDF_LENGTH32_WSTRING:
  865. if (nelements > 1) {
  866. size_t nelem = inp - *info;
  867. if (*maxcount > CDF_PROP_LIMIT
  868. || nelements > CDF_PROP_LIMIT)
  869. goto out;
  870. *maxcount += nelements;
  871. inp = CAST(cdf_property_info_t *,
  872. realloc(*info, *maxcount * sizeof(*inp)));
  873. if (inp == NULL)
  874. goto out1;
  875. *info = inp;
  876. inp = *info + nelem;
  877. }
  878. DPRINTF(("nelements = %" SIZE_T_FORMAT "u\n",
  879. nelements));
  880. for (j = 0; j < nelements && i < sh.sh_properties;
  881. j++, i++)
  882. {
  883. uint32_t l = CDF_GETUINT32(q, o);
  884. inp[i].pi_str.s_len = l;
  885. inp[i].pi_str.s_buf = (const char *)
  886. (const void *)(&q[o4 + sizeof(l)]);
  887. DPRINTF(("l = %d, r = %" SIZE_T_FORMAT
  888. "u, s = %s\n", l,
  889. CDF_ROUND(l, sizeof(l)),
  890. inp[i].pi_str.s_buf));
  891. if (l & 1)
  892. l++;
  893. o += l >> 1;
  894. if (q + o >= e)
  895. goto out;
  896. o4 = o * sizeof(uint32_t);
  897. }
  898. i--;
  899. break;
  900. case CDF_FILETIME:
  901. if (inp[i].pi_type & CDF_VECTOR)
  902. goto unknown;
  903. (void)memcpy(&tp, &q[o4], sizeof(tp));
  904. inp[i].pi_tp = CDF_TOLE8((uint64_t)tp);
  905. break;
  906. case CDF_CLIPBOARD:
  907. if (inp[i].pi_type & CDF_VECTOR)
  908. goto unknown;
  909. break;
  910. default:
  911. unknown:
  912. DPRINTF(("Don't know how to deal with %x\n",
  913. inp[i].pi_type));
  914. break;
  915. }
  916. }
  917. return 0;
  918. out:
  919. errno = EFTYPE;
  920. out1:
  921. free(*info);
  922. return -1;
  923. }
  924. int
  925. cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h,
  926. cdf_summary_info_header_t *ssi, cdf_property_info_t **info, size_t *count)
  927. {
  928. size_t maxcount;
  929. const cdf_summary_info_header_t *si =
  930. CAST(const cdf_summary_info_header_t *, sst->sst_tab);
  931. const cdf_section_declaration_t *sd =
  932. CAST(const cdf_section_declaration_t *, (const void *)
  933. ((const char *)sst->sst_tab + CDF_SECTION_DECLARATION_OFFSET));
  934. if (cdf_check_stream_offset(sst, h, si, sizeof(*si), __LINE__) == -1 ||
  935. cdf_check_stream_offset(sst, h, sd, sizeof(*sd), __LINE__) == -1)
  936. return -1;
  937. ssi->si_byte_order = CDF_TOLE2(si->si_byte_order);
  938. ssi->si_os_version = CDF_TOLE2(si->si_os_version);
  939. ssi->si_os = CDF_TOLE2(si->si_os);
  940. ssi->si_class = si->si_class;
  941. cdf_swap_class(&ssi->si_class);
  942. ssi->si_count = CDF_TOLE4(si->si_count);
  943. *count = 0;
  944. maxcount = 0;
  945. *info = NULL;
  946. if (cdf_read_property_info(sst, h, CDF_TOLE4(sd->sd_offset), info,
  947. count, &maxcount) == -1)
  948. return -1;
  949. return 0;
  950. }
  951. #define extract_catalog_field(t, f, l) \
  952. if (b + l + sizeof(cep->f) > eb) { \
  953. cep->ce_namlen = 0; \
  954. break; \
  955. } \
  956. memcpy(&cep->f, b + (l), sizeof(cep->f)); \
  957. ce[i].f = CAST(t, CDF_TOLE(cep->f))
  958. int
  959. cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst,
  960. cdf_catalog_t **cat)
  961. {
  962. size_t ss = cdf_check_stream(sst, h);
  963. const char *b = CAST(const char *, sst->sst_tab);
  964. const char *eb = b + ss * sst->sst_len;
  965. size_t nr, i, j, k;
  966. cdf_catalog_entry_t *ce;
  967. uint16_t reclen;
  968. const uint16_t *np;
  969. for (nr = 0;; nr++) {
  970. memcpy(&reclen, b, sizeof(reclen));
  971. reclen = CDF_TOLE2(reclen);
  972. if (reclen == 0)
  973. break;
  974. b += reclen;
  975. if (b > eb)
  976. break;
  977. }
  978. if (nr == 0)
  979. return -1;
  980. nr--;
  981. *cat = CAST(cdf_catalog_t *,
  982. malloc(sizeof(cdf_catalog_t) + nr * sizeof(*ce)));
  983. if (*cat == NULL)
  984. return -1;
  985. ce = (*cat)->cat_e;
  986. memset(ce, 0, nr * sizeof(*ce));
  987. b = CAST(const char *, sst->sst_tab);
  988. for (j = i = 0; i < nr; b += reclen) {
  989. cdf_catalog_entry_t *cep = &ce[j];
  990. uint16_t rlen;
  991. extract_catalog_field(uint16_t, ce_namlen, 0);
  992. extract_catalog_field(uint16_t, ce_num, 4);
  993. extract_catalog_field(uint64_t, ce_timestamp, 8);
  994. reclen = cep->ce_namlen;
  995. if (reclen < 14) {
  996. cep->ce_namlen = 0;
  997. continue;
  998. }
  999. cep->ce_namlen = __arraycount(cep->ce_name) - 1;
  1000. rlen = reclen - 14;
  1001. if (cep->ce_namlen > rlen)
  1002. cep->ce_namlen = rlen;
  1003. np = CAST(const uint16_t *, CAST(const void *, (b + 16)));
  1004. if (CAST(const char *, np + cep->ce_namlen) > eb) {
  1005. cep->ce_namlen = 0;
  1006. break;
  1007. }
  1008. for (k = 0; k < cep->ce_namlen; k++)
  1009. cep->ce_name[k] = np[k]; /* XXX: CDF_TOLE2? */
  1010. cep->ce_name[cep->ce_namlen] = 0;
  1011. j = i;
  1012. i++;
  1013. }
  1014. (*cat)->cat_num = j;
  1015. return 0;
  1016. }
  1017. int
  1018. cdf_print_classid(char *buf, size_t buflen, const cdf_classid_t *id)
  1019. {
  1020. return snprintf(buf, buflen, "%.8x-%.4x-%.4x-%.2x%.2x-"
  1021. "%.2x%.2x%.2x%.2x%.2x%.2x", id->cl_dword, id->cl_word[0],
  1022. id->cl_word[1], id->cl_two[0], id->cl_two[1], id->cl_six[0],
  1023. id->cl_six[1], id->cl_six[2], id->cl_six[3], id->cl_six[4],
  1024. id->cl_six[5]);
  1025. }
  1026. static const struct {
  1027. uint32_t v;
  1028. const char *n;
  1029. } vn[] = {
  1030. { CDF_PROPERTY_CODE_PAGE, "Code page" },
  1031. { CDF_PROPERTY_TITLE, "Title" },
  1032. { CDF_PROPERTY_SUBJECT, "Subject" },
  1033. { CDF_PROPERTY_AUTHOR, "Author" },
  1034. { CDF_PROPERTY_KEYWORDS, "Keywords" },
  1035. { CDF_PROPERTY_COMMENTS, "Comments" },
  1036. { CDF_PROPERTY_TEMPLATE, "Template" },
  1037. { CDF_PROPERTY_LAST_SAVED_BY, "Last Saved By" },
  1038. { CDF_PROPERTY_REVISION_NUMBER, "Revision Number" },
  1039. { CDF_PROPERTY_TOTAL_EDITING_TIME, "Total Editing Time" },
  1040. { CDF_PROPERTY_LAST_PRINTED, "Last Printed" },
  1041. { CDF_PROPERTY_CREATE_TIME, "Create Time/Date" },
  1042. { CDF_PROPERTY_LAST_SAVED_TIME, "Last Saved Time/Date" },
  1043. { CDF_PROPERTY_NUMBER_OF_PAGES, "Number of Pages" },
  1044. { CDF_PROPERTY_NUMBER_OF_WORDS, "Number of Words" },
  1045. { CDF_PROPERTY_NUMBER_OF_CHARACTERS, "Number of Characters" },
  1046. { CDF_PROPERTY_THUMBNAIL, "Thumbnail" },
  1047. { CDF_PROPERTY_NAME_OF_APPLICATION, "Name of Creating Application" },
  1048. { CDF_PROPERTY_SECURITY, "Security" },
  1049. { CDF_PROPERTY_LOCALE_ID, "Locale ID" },
  1050. };
  1051. int
  1052. cdf_print_property_name(char *buf, size_t bufsiz, uint32_t p)
  1053. {
  1054. size_t i;
  1055. for (i = 0; i < __arraycount(vn); i++)
  1056. if (vn[i].v == p)
  1057. return snprintf(buf, bufsiz, "%s", vn[i].n);
  1058. return snprintf(buf, bufsiz, "0x%x", p);
  1059. }
  1060. int
  1061. cdf_print_elapsed_time(char *buf, size_t bufsiz, cdf_timestamp_t ts)
  1062. {
  1063. int len = 0;
  1064. int days, hours, mins, secs;
  1065. ts /= CDF_TIME_PREC;
  1066. secs = (int)(ts % 60);
  1067. ts /= 60;
  1068. mins = (int)(ts % 60);
  1069. ts /= 60;
  1070. hours = (int)(ts % 24);
  1071. ts /= 24;
  1072. days = (int)ts;
  1073. if (days) {
  1074. len += snprintf(buf + len, bufsiz - len, "%dd+", days);
  1075. if ((size_t)len >= bufsiz)
  1076. return len;
  1077. }
  1078. if (days || hours) {
  1079. len += snprintf(buf + len, bufsiz - len, "%.2d:", hours);
  1080. if ((size_t)len >= bufsiz)
  1081. return len;
  1082. }
  1083. len += snprintf(buf + len, bufsiz - len, "%.2d:", mins);
  1084. if ((size_t)len >= bufsiz)
  1085. return len;
  1086. len += snprintf(buf + len, bufsiz - len, "%.2d", secs);
  1087. return len;
  1088. }
  1089. char *
  1090. cdf_u16tos8(char *buf, size_t len, const uint16_t *p)
  1091. {
  1092. size_t i;
  1093. for (i = 0; i < len && p[i]; i++)
  1094. buf[i] = (char)p[i];
  1095. buf[i] = '\0';
  1096. return buf;
  1097. }
  1098. #ifdef CDF_DEBUG
  1099. void
  1100. cdf_dump_header(const cdf_header_t *h)
  1101. {
  1102. size_t i;
  1103. #define DUMP(a, b) (void)fprintf(stderr, "%40.40s = " a "\n", # b, h->h_ ## b)
  1104. #define DUMP2(a, b) (void)fprintf(stderr, "%40.40s = " a " (" a ")\n", # b, \
  1105. h->h_ ## b, 1 << h->h_ ## b)
  1106. DUMP("%d", revision);
  1107. DUMP("%d", version);
  1108. DUMP("0x%x", byte_order);
  1109. DUMP2("%d", sec_size_p2);
  1110. DUMP2("%d", short_sec_size_p2);
  1111. DUMP("%d", num_sectors_in_sat);
  1112. DUMP("%d", secid_first_directory);
  1113. DUMP("%d", min_size_standard_stream);
  1114. DUMP("%d", secid_first_sector_in_short_sat);
  1115. DUMP("%d", num_sectors_in_short_sat);
  1116. DUMP("%d", secid_first_sector_in_master_sat);
  1117. DUMP("%d", num_sectors_in_master_sat);
  1118. for (i = 0; i < __arraycount(h->h_master_sat); i++) {
  1119. if (h->h_master_sat[i] == CDF_SECID_FREE)
  1120. break;
  1121. (void)fprintf(stderr, "%35.35s[%.3" SIZE_T_FORMAT "u] = %d\n",
  1122. "master_sat", i, h->h_master_sat[i]);
  1123. }
  1124. }
  1125. void
  1126. cdf_dump_sat(const char *prefix, const cdf_sat_t *sat, size_t size)
  1127. {
  1128. size_t i, j, s = size / sizeof(cdf_secid_t);
  1129. for (i = 0; i < sat->sat_len; i++) {
  1130. (void)fprintf(stderr, "%s[%" SIZE_T_FORMAT "u]:\n%.6"
  1131. SIZE_T_FORMAT "u: ", prefix, i, i * s);
  1132. for (j = 0; j < s; j++) {
  1133. (void)fprintf(stderr, "%5d, ",
  1134. CDF_TOLE4(sat->sat_tab[s * i + j]));
  1135. if ((j + 1) % 10 == 0)
  1136. (void)fprintf(stderr, "\n%.6" SIZE_T_FORMAT
  1137. "u: ", i * s + j + 1);
  1138. }
  1139. (void)fprintf(stderr, "\n");
  1140. }
  1141. }
  1142. void
  1143. cdf_dump(const void *v, size_t len)
  1144. {
  1145. size_t i, j;
  1146. const unsigned char *p = v;
  1147. char abuf[16];
  1148. (void)fprintf(stderr, "%.4x: ", 0);
  1149. for (i = 0, j = 0; i < len; i++, p++) {
  1150. (void)fprintf(stderr, "%.2x ", *p);
  1151. abuf[j++] = isprint(*p) ? *p : '.';
  1152. if (j == 16) {
  1153. j = 0;
  1154. abuf[15] = '\0';
  1155. (void)fprintf(stderr, "%s\n%.4" SIZE_T_FORMAT "x: ",
  1156. abuf, i + 1);
  1157. }
  1158. }
  1159. (void)fprintf(stderr, "\n");
  1160. }
  1161. void
  1162. cdf_dump_stream(const cdf_header_t *h, const cdf_stream_t *sst)
  1163. {
  1164. size_t ss = sst->sst_ss;
  1165. cdf_dump(sst->sst_tab, ss * sst->sst_len);
  1166. }
  1167. void
  1168. cdf_dump_dir(const cdf_info_t *info, const cdf_header_t *h,
  1169. const cdf_sat_t *sat, const cdf_sat_t *ssat, const cdf_stream_t *sst,
  1170. const cdf_dir_t *dir)
  1171. {
  1172. size_t i, j;
  1173. cdf_directory_t *d;
  1174. char name[__arraycount(d->d_name)];
  1175. cdf_stream_t scn;
  1176. struct timespec ts;
  1177. static const char *types[] = { "empty", "user storage",
  1178. "user stream", "lockbytes", "property", "root storage" };
  1179. for (i = 0; i < dir->dir_len; i++) {
  1180. char buf[26];
  1181. d = &dir->dir_tab[i];
  1182. for (j = 0; j < sizeof(name); j++)
  1183. name[j] = (char)CDF_TOLE2(d->d_name[j]);
  1184. (void)fprintf(stderr, "Directory %" SIZE_T_FORMAT "u: %s\n",
  1185. i, name);
  1186. if (d->d_type < __arraycount(types))
  1187. (void)fprintf(stderr, "Type: %s\n", types[d->d_type]);
  1188. else
  1189. (void)fprintf(stderr, "Type: %d\n", d->d_type);
  1190. (void)fprintf(stderr, "Color: %s\n",
  1191. d->d_color ? "black" : "red");
  1192. (void)fprintf(stderr, "Left child: %d\n", d->d_left_child);
  1193. (void)fprintf(stderr, "Right child: %d\n", d->d_right_child);
  1194. (void)fprintf(stderr, "Flags: 0x%x\n", d->d_flags);
  1195. cdf_timestamp_to_timespec(&ts, d->d_created);
  1196. (void)fprintf(stderr, "Created %s", cdf_ctime(&ts.tv_sec, buf));
  1197. cdf_timestamp_to_timespec(&ts, d->d_modified);
  1198. (void)fprintf(stderr, "Modified %s",
  1199. cdf_ctime(&ts.tv_sec, buf));
  1200. (void)fprintf(stderr, "Stream %d\n", d->d_stream_first_sector);
  1201. (void)fprintf(stderr, "Size %d\n", d->d_size);
  1202. switch (d->d_type) {
  1203. case CDF_DIR_TYPE_USER_STORAGE:
  1204. (void)fprintf(stderr, "Storage: %d\n", d->d_storage);
  1205. break;
  1206. case CDF_DIR_TYPE_USER_STREAM:
  1207. if (sst == NULL)
  1208. break;
  1209. if (cdf_read_sector_chain(info, h, sat, ssat, sst,
  1210. d->d_stream_first_sector, d->d_size, &scn) == -1) {
  1211. warn("Can't read stream for %s at %d len %d",
  1212. name, d->d_stream_first_sector, d->d_size);
  1213. break;
  1214. }
  1215. cdf_dump_stream(h, &scn);
  1216. free(scn.sst_tab);
  1217. break;
  1218. default:
  1219. break;
  1220. }
  1221. }
  1222. }
  1223. void
  1224. cdf_dump_property_info(const cdf_property_info_t *info, size_t count)
  1225. {
  1226. cdf_timestamp_t tp;
  1227. struct timespec ts;
  1228. char buf[64];
  1229. size_t i, j;
  1230. for (i = 0; i < count; i++) {
  1231. cdf_print_property_name(buf, sizeof(buf), info[i].pi_id);
  1232. (void)fprintf(stderr, "%" SIZE_T_FORMAT "u) %s: ", i, buf);
  1233. switch (info[i].pi_type) {
  1234. case CDF_NULL:
  1235. break;
  1236. case CDF_SIGNED16:
  1237. (void)fprintf(stderr, "signed 16 [%hd]\n",
  1238. info[i].pi_s16);
  1239. break;
  1240. case CDF_SIGNED32:
  1241. (void)fprintf(stderr, "signed 32 [%d]\n",
  1242. info[i].pi_s32);
  1243. break;
  1244. case CDF_UNSIGNED32:
  1245. (void)fprintf(stderr, "unsigned 32 [%u]\n",
  1246. info[i].pi_u32);
  1247. break;
  1248. case CDF_FLOAT:
  1249. (void)fprintf(stderr, "float [%g]\n",
  1250. info[i].pi_f);
  1251. break;
  1252. case CDF_DOUBLE:
  1253. (void)fprintf(stderr, "double [%g]\n",
  1254. info[i].pi_d);
  1255. break;
  1256. case CDF_LENGTH32_STRING:
  1257. (void)fprintf(stderr, "string %u [%.*s]\n",
  1258. info[i].pi_str.s_len,
  1259. info[i].pi_str.s_len, info[i].pi_str.s_buf);
  1260. break;
  1261. case CDF_LENGTH32_WSTRING:
  1262. (void)fprintf(stderr, "string %u [",
  1263. info[i].pi_str.s_len);
  1264. for (j = 0; j < info[i].pi_str.s_len - 1; j++)
  1265. (void)fputc(info[i].pi_str.s_buf[j << 1], stderr);
  1266. (void)fprintf(stderr, "]\n");
  1267. break;
  1268. case CDF_FILETIME:
  1269. tp = info[i].pi_tp;
  1270. if (tp < 1000000000000000LL) {
  1271. cdf_print_elapsed_time(buf, sizeof(buf), tp);
  1272. (void)fprintf(stderr, "timestamp %s\n", buf);
  1273. } else {
  1274. char tbuf[26];
  1275. cdf_timestamp_to_timespec(&ts, tp);
  1276. (void)fprintf(stderr, "timestamp %s",
  1277. cdf_ctime(&ts.tv_sec, tbuf));
  1278. }
  1279. break;
  1280. case CDF_CLIPBOARD:
  1281. (void)fprintf(stderr, "CLIPBOARD %u\n", info[i].pi_u32);
  1282. break;
  1283. default:
  1284. DPRINTF(("Don't know how to deal with %x\n",
  1285. info[i].pi_type));
  1286. break;
  1287. }
  1288. }
  1289. }
  1290. void
  1291. cdf_dump_summary_info(const cdf_header_t *h, const cdf_stream_t *sst)
  1292. {
  1293. char buf[128];
  1294. cdf_summary_info_header_t ssi;
  1295. cdf_property_info_t *info;
  1296. size_t count;
  1297. (void)&h;
  1298. if (cdf_unpack_summary_info(sst, h, &ssi, &info, &count) == -1)
  1299. return;
  1300. (void)fprintf(stderr, "Endian: %x\n", ssi.si_byte_order);
  1301. (void)fprintf(stderr, "Os Version %d.%d\n", ssi.si_os_version & 0xff,
  1302. ssi.si_os_version >> 8);
  1303. (void)fprintf(stderr, "Os %d\n", ssi.si_os);
  1304. cdf_print_classid(buf, sizeof(buf), &ssi.si_class);
  1305. (void)fprintf(stderr, "Class %s\n", buf);
  1306. (void)fprintf(stderr, "Count %d\n", ssi.si_count);
  1307. cdf_dump_property_info(info, count);
  1308. free(info);
  1309. }
  1310. void
  1311. cdf_dump_catalog(const cdf_header_t *h, const cdf_stream_t *sst)
  1312. {
  1313. cdf_catalog_t *cat;
  1314. cdf_unpack_catalog(h, sst, &cat);
  1315. const cdf_catalog_entry_t *ce = cat->cat_e;
  1316. struct timespec ts;
  1317. char tbuf[64], sbuf[256];
  1318. size_t i;
  1319. printf("Catalog:\n");
  1320. for (i = 0; i < cat->cat_num; i++) {
  1321. cdf_timestamp_to_timespec(&ts, ce[i].ce_timestamp);
  1322. printf("\t%d %s %s", ce[i].ce_num,
  1323. cdf_u16tos8(sbuf, ce[i].ce_namlen, ce[i].ce_name),
  1324. cdf_ctime(&ts.tv_sec, tbuf));
  1325. }
  1326. free(cat);
  1327. }
  1328. #endif
  1329. #ifdef TEST
  1330. int
  1331. main(int argc, char *argv[])
  1332. {
  1333. int i;
  1334. cdf_header_t h;
  1335. cdf_sat_t sat, ssat;
  1336. cdf_stream_t sst, scn;
  1337. cdf_dir_t dir;
  1338. cdf_info_t info;
  1339. const cdf_directory_t *root;
  1340. #ifdef __linux__
  1341. #define getprogname() __progname
  1342. extern char *__progname;
  1343. #endif
  1344. if (argc < 2) {
  1345. (void)fprintf(stderr, "Usage: %s <filename>\n", getprogname());
  1346. return -1;
  1347. }
  1348. info.i_buf = NULL;
  1349. info.i_len = 0;
  1350. for (i = 1; i < argc; i++) {
  1351. if ((info.i_fd = open(argv[1], O_RDONLY)) == -1)
  1352. err(1, "Cannot open `%s'", argv[1]);
  1353. if (cdf_read_header(&info, &h) == -1)
  1354. err(1, "Cannot read header");
  1355. #ifdef CDF_DEBUG
  1356. cdf_dump_header(&h);
  1357. #endif
  1358. if (cdf_read_sat(&info, &h, &sat) == -1)
  1359. err(1, "Cannot read sat");
  1360. #ifdef CDF_DEBUG
  1361. cdf_dump_sat("SAT", &sat, CDF_SEC_SIZE(&h));
  1362. #endif
  1363. if (cdf_read_ssat(&info, &h, &sat, &ssat) == -1)
  1364. err(1, "Cannot read ssat");
  1365. #ifdef CDF_DEBUG
  1366. cdf_dump_sat("SSAT", &ssat, CDF_SHORT_SEC_SIZE(&h));
  1367. #endif
  1368. if (cdf_read_dir(&info, &h, &sat, &dir) == -1)
  1369. err(1, "Cannot read dir");
  1370. if (cdf_read_short_stream(&info, &h, &sat, &dir, &sst, &root)
  1371. == -1)
  1372. err(1, "Cannot read short stream");
  1373. #ifdef CDF_DEBUG
  1374. cdf_dump_stream(&h, &sst);
  1375. #endif
  1376. #ifdef CDF_DEBUG
  1377. cdf_dump_dir(&info, &h, &sat, &ssat, &sst, &dir);
  1378. #endif
  1379. if (cdf_read_summary_info(&info, &h, &sat, &ssat, &sst, &dir,
  1380. &scn) == -1)
  1381. warn("Cannot read summary info");
  1382. #ifdef CDF_DEBUG
  1383. else
  1384. cdf_dump_summary_info(&h, &scn);
  1385. #endif
  1386. if (cdf_read_user_stream(&info, &h, &sat, &ssat, &sst,
  1387. &dir, "Catalog", &scn) == -1)
  1388. warn("Cannot read catalog");
  1389. #ifdef CDF_DEBUG
  1390. else
  1391. cdf_dump_catalog(&h, &scn);
  1392. #endif
  1393. (void)close(info.i_fd);
  1394. }
  1395. return 0;
  1396. }
  1397. #endif