LCWUNCMP.CPP 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/LCWUNCMP.CPP 1 3/03/97 10:25a Joe_bostic $ */
  19. /***************************************************************************
  20. ** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S **
  21. ***************************************************************************
  22. * *
  23. * Project Name : WESTWOOD LIBRARY (PSX) *
  24. * *
  25. * File Name : LCWUNCMP.CPP *
  26. * *
  27. * Programmer : Ian M. Leslie *
  28. * *
  29. * Start Date : May 17, 1995 *
  30. * *
  31. * Last Update : May 17, 1995 [IML] *
  32. * *
  33. *-------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. extern "C" {
  37. /***************************************************************************
  38. * LCW_UNCOMPRESS -- Decompress an LCW encoded data block. *
  39. * *
  40. * Uncompress data to the following codes in the format b = byte, w = word *
  41. * n = byte code pulled from compressed data. *
  42. * *
  43. * Command code, n |Description *
  44. * ------------------------------------------------------------------------*
  45. * n=0xxxyyyy,yyyyyyyy |short copy back y bytes and run x+3 from dest *
  46. * n=10xxxxxx,n1,n2,...,nx+1|med length copy the next x+1 bytes from source*
  47. * n=11xxxxxx,w1 |med copy from dest x+3 bytes from offset w1 *
  48. * n=11111111,w1,w2 |long copy from dest w1 bytes from offset w2 *
  49. * n=11111110,w1,b1 |long run of byte b1 for w1 bytes *
  50. * n=10000000 |end of data reached *
  51. * *
  52. * *
  53. * INPUT: *
  54. * void * source ptr *
  55. * void * destination ptr *
  56. * unsigned long length of uncompressed data *
  57. * *
  58. * *
  59. * OUTPUT: *
  60. * unsigned long # of destination bytes written *
  61. * *
  62. * WARNINGS: *
  63. * 3rd argument is dummy. It exists to provide cross-platform *
  64. * compatibility. Note therefore that this implementation does not *
  65. * check for corrupt source data by testing the uncompressed length. *
  66. * *
  67. * HISTORY: *
  68. * 03/20/1995 IML : Created. *
  69. *=========================================================================*/
  70. unsigned long __cdecl LCW_Uncompress (void * source, void * dest, unsigned long )
  71. //unsigned long LCW_Uncompress (void * source, void * dest, unsigned long length)
  72. {
  73. unsigned char * source_ptr, * dest_ptr, * copy_ptr, op_code, data;
  74. unsigned count, * word_dest_ptr, word_data;
  75. /* Copy the source and destination ptrs. */
  76. source_ptr = (unsigned char*) source;
  77. dest_ptr = (unsigned char*) dest;
  78. while (1 /*TRUE*/) {
  79. /* Read in the operation code. */
  80. op_code = *source_ptr++;
  81. if (!(op_code & 0x80)) {
  82. /* Do a short copy from destination. */
  83. count = (op_code >> 4) + 3;
  84. copy_ptr = dest_ptr - ((unsigned) *source_ptr++ + (((unsigned) op_code & 0x0f) << 8));
  85. while (count--) *dest_ptr++ = *copy_ptr++;
  86. } else {
  87. if (!(op_code & 0x40)) {
  88. if (op_code == 0x80) {
  89. /* Return # of destination bytes written. */
  90. return ((unsigned long) (dest_ptr - (unsigned char*) dest));
  91. } else {
  92. /* Do a medium copy from source. */
  93. count = op_code & 0x3f;
  94. while (count--) *dest_ptr++ = *source_ptr++;
  95. }
  96. } else {
  97. if (op_code == 0xfe) {
  98. /* Do a long run. */
  99. count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
  100. word_data = data = *(source_ptr + 2);
  101. word_data = (word_data << 24) + (word_data << 16) + (word_data << 8) + word_data;
  102. source_ptr += 3;
  103. copy_ptr = dest_ptr + 4 - ((unsigned) dest_ptr & 0x3);
  104. count -= (copy_ptr - dest_ptr);
  105. while (dest_ptr < copy_ptr) *dest_ptr++ = data;
  106. word_dest_ptr = (unsigned*) dest_ptr;
  107. dest_ptr += (count & 0xfffffffc);
  108. while (word_dest_ptr < (unsigned*) dest_ptr) {
  109. *word_dest_ptr = word_data;
  110. *(word_dest_ptr + 1) = word_data;
  111. word_dest_ptr += 2;
  112. }
  113. copy_ptr = dest_ptr + (count & 0x3);
  114. while (dest_ptr < copy_ptr) *dest_ptr++ = data;
  115. } else {
  116. if (op_code == 0xff) {
  117. /* Do a long copy from destination. */
  118. count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
  119. copy_ptr = (unsigned char*) dest + *(source_ptr + 2) + ((unsigned) *(source_ptr + 3) << 8);
  120. source_ptr += 4;
  121. while (count--) *dest_ptr++ = *copy_ptr++;
  122. } else {
  123. /* Do a medium copy from destination. */
  124. count = (op_code & 0x3f) + 3;
  125. copy_ptr = (unsigned char*) dest + *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
  126. source_ptr += 2;
  127. while (count--) *dest_ptr++ = *copy_ptr++;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }
  134. }