cdf.c 32 KB

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