cdf.c 35 KB

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