cdf.c 38 KB

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