cdf.c 40 KB

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