palcommon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // Based on PAL MapEditor by Baldur.
  7. //
  8. // This file is part of SDLPAL.
  9. //
  10. // SDLPAL is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #include "palcommon.h"
  24. INT
  25. PAL_RLEBlitToSurface(
  26. LPCBITMAPRLE lpBitmapRLE,
  27. SDL_Surface *lpDstSurface,
  28. PAL_POS pos
  29. )
  30. /*++
  31. Purpose:
  32. Blit an RLE-compressed bitmap to an SDL surface.
  33. NOTE: Assume the surface is already locked, and the surface is a 8-bit one.
  34. Parameters:
  35. [IN] lpBitmapRLE - pointer to the RLE-compressed bitmap to be decoded.
  36. [OUT] lpDstSurface - pointer to the destination SDL surface.
  37. [IN] pos - position of the destination area.
  38. Return value:
  39. 0 = success, -1 = error.
  40. --*/
  41. {
  42. UINT i, j;
  43. INT x, y;
  44. UINT uiLen = 0;
  45. UINT uiWidth = 0;
  46. UINT uiHeight = 0;
  47. BYTE T;
  48. INT dx = PAL_X(pos);
  49. INT dy = PAL_Y(pos);
  50. //
  51. // Check for NULL pointer.
  52. //
  53. if (lpBitmapRLE == NULL || lpDstSurface == NULL)
  54. {
  55. return -1;
  56. }
  57. //
  58. // Skip the 0x00000002 in the file header.
  59. //
  60. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  61. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  62. {
  63. lpBitmapRLE += 4;
  64. }
  65. //
  66. // Get the width and height of the bitmap.
  67. //
  68. uiWidth = lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  69. uiHeight = lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  70. //
  71. // Calculate the total length of the bitmap.
  72. // The bitmap is 8-bpp, each pixel will use 1 byte.
  73. //
  74. uiLen = uiWidth * uiHeight;
  75. //
  76. // Start decoding and blitting the bitmap.
  77. //
  78. lpBitmapRLE += 4;
  79. for (i = 0; i < uiLen;)
  80. {
  81. T = *lpBitmapRLE++;
  82. if ((T & 0x80) && T <= 0x80 + uiWidth)
  83. {
  84. i += T - 0x80;
  85. }
  86. else
  87. {
  88. for (j = 0; j < T; j++)
  89. {
  90. //
  91. // Calculate the destination coordination.
  92. // FIXME: This could be optimized
  93. //
  94. y = (i + j) / uiWidth + dy;
  95. x = (i + j) % uiWidth + dx;
  96. //
  97. // Skip the points which are out of the surface.
  98. //
  99. if (x < 0)
  100. {
  101. j += -x - 1;
  102. continue;
  103. }
  104. else if (x >= lpDstSurface->w)
  105. {
  106. j += x - lpDstSurface->w;
  107. continue;
  108. }
  109. if (y < 0)
  110. {
  111. j += -y * uiWidth - 1;
  112. continue;
  113. }
  114. else if (y >= lpDstSurface->h)
  115. {
  116. goto end; // No more pixels needed, break out
  117. }
  118. //
  119. // Put the pixel onto the surface (FIXME: inefficient).
  120. //
  121. ((LPBYTE)lpDstSurface->pixels)[y * lpDstSurface->pitch + x] = lpBitmapRLE[j];
  122. }
  123. lpBitmapRLE += T;
  124. i += T;
  125. }
  126. }
  127. end:
  128. //
  129. // Success
  130. //
  131. return 0;
  132. }
  133. INT
  134. PAL_RLEBlitWithColorShift(
  135. LPCBITMAPRLE lpBitmapRLE,
  136. SDL_Surface *lpDstSurface,
  137. PAL_POS pos,
  138. INT iColorShift
  139. )
  140. /*++
  141. Purpose:
  142. Blit an RLE-compressed bitmap to an SDL surface.
  143. NOTE: Assume the surface is already locked, and the surface is a 8-bit one.
  144. Parameters:
  145. [IN] lpBitmapRLE - pointer to the RLE-compressed bitmap to be decoded.
  146. [OUT] lpDstSurface - pointer to the destination SDL surface.
  147. [IN] pos - position of the destination area.
  148. [IN] iColorShift - shift the color by this value.
  149. Return value:
  150. 0 = success, -1 = error.
  151. --*/
  152. {
  153. UINT i, j;
  154. INT x, y;
  155. UINT uiLen = 0;
  156. UINT uiWidth = 0;
  157. UINT uiHeight = 0;
  158. BYTE T, b;
  159. INT dx = PAL_X(pos);
  160. INT dy = PAL_Y(pos);
  161. //
  162. // Check for NULL pointer.
  163. //
  164. if (lpBitmapRLE == NULL || lpDstSurface == NULL)
  165. {
  166. return -1;
  167. }
  168. //
  169. // Skip the 0x00000002 in the file header.
  170. //
  171. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  172. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  173. {
  174. lpBitmapRLE += 4;
  175. }
  176. //
  177. // Get the width and height of the bitmap.
  178. //
  179. uiWidth = lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  180. uiHeight = lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  181. //
  182. // Calculate the total length of the bitmap.
  183. // The bitmap is 8-bpp, each pixel will use 1 byte.
  184. //
  185. uiLen = uiWidth * uiHeight;
  186. //
  187. // Start decoding and blitting the bitmap.
  188. //
  189. lpBitmapRLE += 4;
  190. for (i = 0; i < uiLen;)
  191. {
  192. T = *lpBitmapRLE++;
  193. if ((T & 0x80) && T <= 0x80 + uiWidth)
  194. {
  195. i += T - 0x80;
  196. }
  197. else
  198. {
  199. for (j = 0; j < T; j++)
  200. {
  201. //
  202. // Calculate the destination coordination.
  203. // FIXME: This could be optimized
  204. //
  205. y = (i + j) / uiWidth + dy;
  206. x = (i + j) % uiWidth + dx;
  207. //
  208. // Skip the points which are out of the surface.
  209. //
  210. if (x < 0)
  211. {
  212. j += -x - 1;
  213. continue;
  214. }
  215. else if (x >= lpDstSurface->w)
  216. {
  217. j += x - lpDstSurface->w;
  218. continue;
  219. }
  220. if (y < 0)
  221. {
  222. j += -y * uiWidth - 1;
  223. continue;
  224. }
  225. else if (y >= lpDstSurface->h)
  226. {
  227. goto end; // No more pixels needed, break out
  228. }
  229. //
  230. // Put the pixel onto the surface (FIXME: inefficient).
  231. //
  232. b = (lpBitmapRLE[j] & 0x0F);
  233. if ((INT)b + iColorShift > 0x0F)
  234. {
  235. b = 0x0F;
  236. }
  237. else if ((INT)b + iColorShift < 0)
  238. {
  239. b = 0;
  240. }
  241. else
  242. {
  243. b += iColorShift;
  244. }
  245. ((LPBYTE)lpDstSurface->pixels)[y * lpDstSurface->pitch + x] =
  246. (b | (lpBitmapRLE[j] & 0xF0));
  247. }
  248. lpBitmapRLE += T;
  249. i += T;
  250. }
  251. }
  252. end:
  253. //
  254. // Success
  255. //
  256. return 0;
  257. }
  258. INT
  259. PAL_RLEBlitMonoColor(
  260. LPCBITMAPRLE lpBitmapRLE,
  261. SDL_Surface *lpDstSurface,
  262. PAL_POS pos,
  263. BYTE bColor,
  264. INT iColorShift
  265. )
  266. /*++
  267. Purpose:
  268. Blit an RLE-compressed bitmap to an SDL surface in mono-color form.
  269. NOTE: Assume the surface is already locked, and the surface is a 8-bit one.
  270. Parameters:
  271. [IN] lpBitmapRLE - pointer to the RLE-compressed bitmap to be decoded.
  272. [OUT] lpDstSurface - pointer to the destination SDL surface.
  273. [IN] pos - position of the destination area.
  274. [IN] bColor - the color to be used while drawing.
  275. [IN] iColorShift - shift the color by this value.
  276. Return value:
  277. 0 = success, -1 = error.
  278. --*/
  279. {
  280. UINT i, j;
  281. INT x, y;
  282. UINT uiLen = 0;
  283. UINT uiWidth = 0;
  284. UINT uiHeight = 0;
  285. BYTE T, b;
  286. INT dx = PAL_X(pos);
  287. INT dy = PAL_Y(pos);
  288. //
  289. // Check for NULL pointer.
  290. //
  291. if (lpBitmapRLE == NULL || lpDstSurface == NULL)
  292. {
  293. return -1;
  294. }
  295. //
  296. // Skip the 0x00000002 in the file header.
  297. //
  298. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  299. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  300. {
  301. lpBitmapRLE += 4;
  302. }
  303. //
  304. // Get the width and height of the bitmap.
  305. //
  306. uiWidth = lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  307. uiHeight = lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  308. //
  309. // Calculate the total length of the bitmap.
  310. // The bitmap is 8-bpp, each pixel will use 1 byte.
  311. //
  312. uiLen = uiWidth * uiHeight;
  313. //
  314. // Start decoding and blitting the bitmap.
  315. //
  316. lpBitmapRLE += 4;
  317. bColor &= 0xF0;
  318. for (i = 0; i < uiLen;)
  319. {
  320. T = *lpBitmapRLE++;
  321. if ((T & 0x80) && T <= 0x80 + uiWidth)
  322. {
  323. i += T - 0x80;
  324. }
  325. else
  326. {
  327. for (j = 0; j < T; j++)
  328. {
  329. //
  330. // Calculate the destination coordination.
  331. // FIXME: This could be optimized
  332. //
  333. y = (i + j) / uiWidth + dy;
  334. x = (i + j) % uiWidth + dx;
  335. //
  336. // Skip the points which are out of the surface.
  337. //
  338. if (x < 0)
  339. {
  340. j += -x - 1;
  341. continue;
  342. }
  343. else if (x >= lpDstSurface->w)
  344. {
  345. j += x - lpDstSurface->w;
  346. continue;
  347. }
  348. if (y < 0)
  349. {
  350. j += -y * uiWidth - 1;
  351. continue;
  352. }
  353. else if (y >= lpDstSurface->h)
  354. {
  355. goto end; // No more pixels needed, break out
  356. }
  357. //
  358. // Put the pixel onto the surface (FIXME: inefficient).
  359. //
  360. b = lpBitmapRLE[j] & 0x0F;
  361. if ((INT)b + iColorShift > 0x0F)
  362. {
  363. b = 0x0F;
  364. }
  365. else if ((INT)b + iColorShift < 0)
  366. {
  367. b = 0;
  368. }
  369. else
  370. {
  371. b += iColorShift;
  372. }
  373. ((LPBYTE)lpDstSurface->pixels)[y * lpDstSurface->pitch + x] = (b | bColor);
  374. }
  375. lpBitmapRLE += T;
  376. i += T;
  377. }
  378. }
  379. end:
  380. //
  381. // Success
  382. //
  383. return 0;
  384. }
  385. INT
  386. PAL_FBPBlitToSurface(
  387. LPBYTE lpBitmapFBP,
  388. SDL_Surface *lpDstSurface
  389. )
  390. /*++
  391. Purpose:
  392. Blit an uncompressed bitmap in FBP.MKF to an SDL surface.
  393. NOTE: Assume the surface is already locked, and the surface is a 8-bit 320x200 one.
  394. Parameters:
  395. [IN] lpBitmapFBP - pointer to the RLE-compressed bitmap to be decoded.
  396. [OUT] lpDstSurface - pointer to the destination SDL surface.
  397. Return value:
  398. 0 = success, -1 = error.
  399. --*/
  400. {
  401. int x, y;
  402. LPBYTE p;
  403. if (lpBitmapFBP == NULL || lpDstSurface == NULL ||
  404. lpDstSurface->w != 320 || lpDstSurface->h != 200)
  405. {
  406. return -1;
  407. }
  408. //
  409. // simply copy everything to the surface
  410. //
  411. for (y = 0; y < 200; y++)
  412. {
  413. p = (LPBYTE)(lpDstSurface->pixels) + y * lpDstSurface->pitch;
  414. for (x = 0; x < 320; x++)
  415. {
  416. *(p++) = *(lpBitmapFBP++);
  417. }
  418. }
  419. return 0;
  420. }
  421. UINT
  422. PAL_RLEGetWidth(
  423. LPCBITMAPRLE lpBitmapRLE
  424. )
  425. /*++
  426. Purpose:
  427. Get the width of an RLE-compressed bitmap.
  428. Parameters:
  429. [IN] lpBitmapRLE - pointer to an RLE-compressed bitmap.
  430. Return value:
  431. Integer value which indicates the height of the bitmap.
  432. --*/
  433. {
  434. if (lpBitmapRLE == NULL)
  435. {
  436. return 0;
  437. }
  438. //
  439. // Skip the 0x00000002 in the file header.
  440. //
  441. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  442. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  443. {
  444. lpBitmapRLE += 4;
  445. }
  446. //
  447. // Return the width of the bitmap.
  448. //
  449. return lpBitmapRLE[0] | (lpBitmapRLE[1] << 8);
  450. }
  451. UINT
  452. PAL_RLEGetHeight(
  453. LPCBITMAPRLE lpBitmapRLE
  454. )
  455. /*++
  456. Purpose:
  457. Get the height of an RLE-compressed bitmap.
  458. Parameters:
  459. [IN] lpBitmapRLE - pointer of an RLE-compressed bitmap.
  460. Return value:
  461. Integer value which indicates the height of the bitmap.
  462. --*/
  463. {
  464. if (lpBitmapRLE == NULL)
  465. {
  466. return 0;
  467. }
  468. //
  469. // Skip the 0x00000002 in the file header.
  470. //
  471. if (lpBitmapRLE[0] == 0x02 && lpBitmapRLE[1] == 0x00 &&
  472. lpBitmapRLE[2] == 0x00 && lpBitmapRLE[3] == 0x00)
  473. {
  474. lpBitmapRLE += 4;
  475. }
  476. //
  477. // Return the height of the bitmap.
  478. //
  479. return lpBitmapRLE[2] | (lpBitmapRLE[3] << 8);
  480. }
  481. WORD
  482. PAL_SpriteGetNumFrames(
  483. LPCSPRITE lpSprite
  484. )
  485. /*++
  486. Purpose:
  487. Get the total number of frames of a sprite.
  488. Parameters:
  489. [IN] lpSprite - pointer to the sprite.
  490. Return value:
  491. Number of frames of the sprite.
  492. --*/
  493. {
  494. if (lpSprite == NULL)
  495. {
  496. return 0;
  497. }
  498. return (lpSprite[0] | (lpSprite[1] << 8)) - 1;
  499. }
  500. LPCBITMAPRLE
  501. PAL_SpriteGetFrame(
  502. LPCSPRITE lpSprite,
  503. INT iFrameNum
  504. )
  505. /*++
  506. Purpose:
  507. Get the pointer to the specified frame from a sprite.
  508. Parameters:
  509. [IN] lpSprite - pointer to the sprite.
  510. [IN] iFrameNum - number of the frame.
  511. Return value:
  512. Pointer to the specified frame. NULL if the frame does not exist.
  513. --*/
  514. {
  515. int imagecount, offset;
  516. if (lpSprite == NULL)
  517. {
  518. return NULL;
  519. }
  520. //
  521. // Hack for broken sprites like the Bloody-Mouth Bug
  522. //
  523. // imagecount = (lpSprite[0] | (lpSprite[1] << 8)) - 1;
  524. imagecount = (lpSprite[0] | (lpSprite[1] << 8));
  525. if (iFrameNum < 0 || iFrameNum >= imagecount)
  526. {
  527. //
  528. // The frame does not exist
  529. //
  530. return NULL;
  531. }
  532. //
  533. // Get the offset of the frame
  534. //
  535. iFrameNum <<= 1;
  536. offset = (WORD)((lpSprite[iFrameNum] | (lpSprite[iFrameNum + 1] << 8)) << 1);
  537. return &lpSprite[offset];
  538. }
  539. INT
  540. PAL_MKFGetChunkCount(
  541. FILE *fp
  542. )
  543. /*++
  544. Purpose:
  545. Get the number of chunks in an MKF archive.
  546. Parameters:
  547. [IN] fp - pointer to an fopen'ed MKF file.
  548. Return value:
  549. Integer value which indicates the number of chunks in the specified MKF file.
  550. --*/
  551. {
  552. INT iNumChunk;
  553. if (fp == NULL)
  554. {
  555. return 0;
  556. }
  557. fseek(fp, 0, SEEK_SET);
  558. fread(&iNumChunk, sizeof(INT), 1, fp);
  559. iNumChunk = (SWAP32(iNumChunk) - 4) / 4;
  560. return iNumChunk;
  561. }
  562. INT
  563. PAL_MKFGetChunkSize(
  564. UINT uiChunkNum,
  565. FILE *fp
  566. )
  567. /*++
  568. Purpose:
  569. Get the size of a chunk in an MKF archive.
  570. Parameters:
  571. [IN] uiChunkNum - the number of the chunk in the MKF archive.
  572. [IN] fp - pointer to the fopen'ed MKF file.
  573. Return value:
  574. Integer value which indicates the size of the chunk.
  575. -1 if the chunk does not exist.
  576. --*/
  577. {
  578. UINT uiOffset = 0;
  579. UINT uiNextOffset = 0;
  580. UINT uiChunkCount = 0;
  581. //
  582. // Get the total number of chunks.
  583. //
  584. uiChunkCount = PAL_MKFGetChunkCount(fp);
  585. if (uiChunkNum >= uiChunkCount)
  586. {
  587. return -1;
  588. }
  589. //
  590. // Get the offset of the specified chunk and the next chunk.
  591. //
  592. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  593. fread(&uiOffset, sizeof(UINT), 1, fp);
  594. fread(&uiNextOffset, sizeof(UINT), 1, fp);
  595. uiOffset = SWAP32(uiOffset);
  596. uiNextOffset = SWAP32(uiNextOffset);
  597. //
  598. // Return the length of the chunk.
  599. //
  600. return uiNextOffset - uiOffset;
  601. }
  602. INT
  603. PAL_MKFReadChunk(
  604. LPBYTE lpBuffer,
  605. UINT uiBufferSize,
  606. UINT uiChunkNum,
  607. FILE *fp
  608. )
  609. /*++
  610. Purpose:
  611. Read a chunk from an MKF archive into lpBuffer.
  612. Parameters:
  613. [OUT] lpBuffer - pointer to the destination buffer.
  614. [IN] uiBufferSize - size of the destination buffer.
  615. [IN] uiChunkNum - the number of the chunk in the MKF archive to read.
  616. [IN] fp - pointer to the fopen'ed MKF file.
  617. Return value:
  618. Integer value which indicates the size of the chunk.
  619. -1 if there are error in parameters.
  620. -2 if buffer size is not enough.
  621. --*/
  622. {
  623. UINT uiOffset = 0;
  624. UINT uiNextOffset = 0;
  625. UINT uiChunkCount;
  626. UINT uiChunkLen;
  627. if (lpBuffer == NULL || fp == NULL || uiBufferSize == 0)
  628. {
  629. return -1;
  630. }
  631. //
  632. // Get the total number of chunks.
  633. //
  634. uiChunkCount = PAL_MKFGetChunkCount(fp);
  635. if (uiChunkNum >= uiChunkCount)
  636. {
  637. return -1;
  638. }
  639. //
  640. // Get the offset of the chunk.
  641. //
  642. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  643. fread(&uiOffset, 4, 1, fp);
  644. fread(&uiNextOffset, 4, 1, fp);
  645. uiOffset = SWAP32(uiOffset);
  646. uiNextOffset = SWAP32(uiNextOffset);
  647. //
  648. // Get the length of the chunk.
  649. //
  650. uiChunkLen = uiNextOffset - uiOffset;
  651. if (uiChunkLen > uiBufferSize)
  652. {
  653. return -2;
  654. }
  655. if (uiChunkLen != 0)
  656. {
  657. fseek(fp, uiOffset, SEEK_SET);
  658. fread(lpBuffer, uiChunkLen, 1, fp);
  659. }
  660. else
  661. {
  662. return -1;
  663. }
  664. return (INT)uiChunkLen;
  665. }
  666. INT
  667. PAL_MKFGetDecompressedSize(
  668. UINT uiChunkNum,
  669. FILE *fp
  670. )
  671. /*++
  672. Purpose:
  673. Get the decompressed size of a compressed chunk in an MKF archive.
  674. Parameters:
  675. [IN] uiChunkNum - the number of the chunk in the MKF archive.
  676. [IN] fp - pointer to the fopen'ed MKF file.
  677. Return value:
  678. Integer value which indicates the size of the chunk.
  679. -1 if the chunk does not exist.
  680. --*/
  681. {
  682. DWORD buf[2];
  683. UINT uiOffset;
  684. UINT uiChunkCount;
  685. if (fp == NULL)
  686. {
  687. return -1;
  688. }
  689. //
  690. // Get the total number of chunks.
  691. //
  692. uiChunkCount = PAL_MKFGetChunkCount(fp);
  693. if (uiChunkNum >= uiChunkCount)
  694. {
  695. return -1;
  696. }
  697. //
  698. // Get the offset of the chunk.
  699. //
  700. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  701. fread(&uiOffset, 4, 1, fp);
  702. uiOffset = SWAP32(uiOffset);
  703. //
  704. // Read the header.
  705. //
  706. fseek(fp, uiOffset, SEEK_SET);
  707. fread(buf, sizeof(DWORD), 2, fp);
  708. buf[0] = SWAP32(buf[0]);
  709. buf[1] = SWAP32(buf[1]);
  710. return (buf[0] != 0x315f4a59) ? -1 : (INT)buf[1];
  711. }
  712. INT
  713. PAL_MKFDecompressChunk(
  714. LPBYTE lpBuffer,
  715. UINT uiBufferSize,
  716. UINT uiChunkNum,
  717. FILE *fp
  718. )
  719. /*++
  720. Purpose:
  721. Decompress a compressed chunk from an MKF archive into lpBuffer.
  722. Parameters:
  723. [OUT] lpBuffer - pointer to the destination buffer.
  724. [IN] uiBufferSize - size of the destination buffer.
  725. [IN] uiChunkNum - the number of the chunk in the MKF archive to read.
  726. [IN] fp - pointer to the fopen'ed MKF file.
  727. Return value:
  728. Integer value which indicates the size of the chunk.
  729. -1 if there are error in parameters, or buffer size is not enough.
  730. -3 if cannot allocate memory for decompression.
  731. --*/
  732. {
  733. LPBYTE buf;
  734. int len;
  735. len = PAL_MKFGetChunkSize(uiChunkNum, fp);
  736. if (len <= 0)
  737. {
  738. return len;
  739. }
  740. buf = (LPBYTE)malloc(len);
  741. if (buf == NULL)
  742. {
  743. return -3;
  744. }
  745. PAL_MKFReadChunk(buf, len, uiChunkNum, fp);
  746. len = Decompress(buf, lpBuffer, uiBufferSize);
  747. free(buf);
  748. return len;
  749. }