palcommon.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. INT
  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. INT
  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. #ifdef PAL_WIN95
  537. offset = ((lpSprite[iFrameNum] | (lpSprite[iFrameNum + 1] << 8)) << 1);
  538. #else
  539. offset = (WORD)((lpSprite[iFrameNum] | (lpSprite[iFrameNum + 1] << 8)) << 1);
  540. #endif
  541. return &lpSprite[offset];
  542. }
  543. INT
  544. PAL_MKFGetChunkCount(
  545. FILE *fp
  546. )
  547. /*++
  548. Purpose:
  549. Get the number of chunks in an MKF archive.
  550. Parameters:
  551. [IN] fp - pointer to an fopen'ed MKF file.
  552. Return value:
  553. Integer value which indicates the number of chunks in the specified MKF file.
  554. --*/
  555. {
  556. INT iNumChunk;
  557. if (fp == NULL)
  558. {
  559. return 0;
  560. }
  561. fseek(fp, 0, SEEK_SET);
  562. fread(&iNumChunk, sizeof(INT), 1, fp);
  563. iNumChunk = (SWAP32(iNumChunk) - 4) / 4;
  564. return iNumChunk;
  565. }
  566. INT
  567. PAL_MKFGetChunkSize(
  568. UINT uiChunkNum,
  569. FILE *fp
  570. )
  571. /*++
  572. Purpose:
  573. Get the size of a chunk in an MKF archive.
  574. Parameters:
  575. [IN] uiChunkNum - the number of the chunk in the MKF archive.
  576. [IN] fp - pointer to the fopen'ed MKF file.
  577. Return value:
  578. Integer value which indicates the size of the chunk.
  579. -1 if the chunk does not exist.
  580. --*/
  581. {
  582. UINT uiOffset = 0;
  583. UINT uiNextOffset = 0;
  584. UINT uiChunkCount = 0;
  585. //
  586. // Get the total number of chunks.
  587. //
  588. uiChunkCount = PAL_MKFGetChunkCount(fp);
  589. if (uiChunkNum >= uiChunkCount)
  590. {
  591. return -1;
  592. }
  593. //
  594. // Get the offset of the specified chunk and the next chunk.
  595. //
  596. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  597. fread(&uiOffset, sizeof(UINT), 1, fp);
  598. fread(&uiNextOffset, sizeof(UINT), 1, fp);
  599. uiOffset = SWAP32(uiOffset);
  600. uiNextOffset = SWAP32(uiNextOffset);
  601. //
  602. // Return the length of the chunk.
  603. //
  604. return uiNextOffset - uiOffset;
  605. }
  606. INT
  607. PAL_MKFReadChunk(
  608. LPBYTE lpBuffer,
  609. UINT uiBufferSize,
  610. UINT uiChunkNum,
  611. FILE *fp
  612. )
  613. /*++
  614. Purpose:
  615. Read a chunk from an MKF archive into lpBuffer.
  616. Parameters:
  617. [OUT] lpBuffer - pointer to the destination buffer.
  618. [IN] uiBufferSize - size of the destination buffer.
  619. [IN] uiChunkNum - the number of the chunk in the MKF archive to read.
  620. [IN] fp - pointer to the fopen'ed MKF file.
  621. Return value:
  622. Integer value which indicates the size of the chunk.
  623. -1 if there are error in parameters.
  624. -2 if buffer size is not enough.
  625. --*/
  626. {
  627. UINT uiOffset = 0;
  628. UINT uiNextOffset = 0;
  629. UINT uiChunkCount;
  630. UINT uiChunkLen;
  631. if (lpBuffer == NULL || fp == NULL || uiBufferSize == 0)
  632. {
  633. return -1;
  634. }
  635. //
  636. // Get the total number of chunks.
  637. //
  638. uiChunkCount = PAL_MKFGetChunkCount(fp);
  639. if (uiChunkNum >= uiChunkCount)
  640. {
  641. return -1;
  642. }
  643. //
  644. // Get the offset of the chunk.
  645. //
  646. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  647. fread(&uiOffset, 4, 1, fp);
  648. fread(&uiNextOffset, 4, 1, fp);
  649. uiOffset = SWAP32(uiOffset);
  650. uiNextOffset = SWAP32(uiNextOffset);
  651. //
  652. // Get the length of the chunk.
  653. //
  654. uiChunkLen = uiNextOffset - uiOffset;
  655. if (uiChunkLen > uiBufferSize)
  656. {
  657. return -2;
  658. }
  659. if (uiChunkLen != 0)
  660. {
  661. fseek(fp, uiOffset, SEEK_SET);
  662. fread(lpBuffer, uiChunkLen, 1, fp);
  663. }
  664. else
  665. {
  666. return -1;
  667. }
  668. return (INT)uiChunkLen;
  669. }
  670. INT
  671. PAL_MKFGetDecompressedSize(
  672. UINT uiChunkNum,
  673. FILE *fp
  674. )
  675. /*++
  676. Purpose:
  677. Get the decompressed size of a compressed chunk in an MKF archive.
  678. Parameters:
  679. [IN] uiChunkNum - the number of the chunk in the MKF archive.
  680. [IN] fp - pointer to the fopen'ed MKF file.
  681. Return value:
  682. Integer value which indicates the size of the chunk.
  683. -1 if the chunk does not exist.
  684. --*/
  685. {
  686. DWORD buf[2];
  687. UINT uiOffset;
  688. UINT uiChunkCount;
  689. if (fp == NULL)
  690. {
  691. return -1;
  692. }
  693. //
  694. // Get the total number of chunks.
  695. //
  696. uiChunkCount = PAL_MKFGetChunkCount(fp);
  697. if (uiChunkNum >= uiChunkCount)
  698. {
  699. return -1;
  700. }
  701. //
  702. // Get the offset of the chunk.
  703. //
  704. fseek(fp, 4 * uiChunkNum, SEEK_SET);
  705. fread(&uiOffset, 4, 1, fp);
  706. uiOffset = SWAP32(uiOffset);
  707. //
  708. // Read the header.
  709. //
  710. fseek(fp, uiOffset, SEEK_SET);
  711. #ifdef PAL_WIN95
  712. fread(buf, sizeof(DWORD), 1, fp);
  713. buf[0] = SWAP32(buf[0]);
  714. return (INT)buf[0];
  715. #else
  716. fread(buf, sizeof(DWORD), 2, fp);
  717. buf[0] = SWAP32(buf[0]);
  718. buf[1] = SWAP32(buf[1]);
  719. return (buf[0] != 0x315f4a59) ? -1 : (INT)buf[1];
  720. #endif
  721. }
  722. INT
  723. PAL_MKFDecompressChunk(
  724. LPBYTE lpBuffer,
  725. UINT uiBufferSize,
  726. UINT uiChunkNum,
  727. FILE *fp
  728. )
  729. /*++
  730. Purpose:
  731. Decompress a compressed chunk from an MKF archive into lpBuffer.
  732. Parameters:
  733. [OUT] lpBuffer - pointer to the destination buffer.
  734. [IN] uiBufferSize - size of the destination buffer.
  735. [IN] uiChunkNum - the number of the chunk in the MKF archive to read.
  736. [IN] fp - pointer to the fopen'ed MKF file.
  737. Return value:
  738. Integer value which indicates the size of the chunk.
  739. -1 if there are error in parameters, or buffer size is not enough.
  740. -3 if cannot allocate memory for decompression.
  741. --*/
  742. {
  743. LPBYTE buf;
  744. int len;
  745. len = PAL_MKFGetChunkSize(uiChunkNum, fp);
  746. if (len <= 0)
  747. {
  748. return len;
  749. }
  750. buf = (LPBYTE)malloc(len);
  751. if (buf == NULL)
  752. {
  753. return -3;
  754. }
  755. PAL_MKFReadChunk(buf, len, uiChunkNum, fp);
  756. len = Decompress(buf, lpBuffer, uiBufferSize);
  757. free(buf);
  758. return len;
  759. }