cdf.c 34 KB

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