cdf.c 42 KB

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