cdf.c 34 KB

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