cdf.c 41 KB

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