cdf.c 40 KB

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