LCW.CPP 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/LCW.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. /***************************************************************************
  37. * LCW_Uncomp -- Decompress an LCW encoded data block. *
  38. * *
  39. * Uncompress data to the following codes in the format b = byte, w = word *
  40. * n = byte code pulled from compressed data. *
  41. * *
  42. * Command code, n |Description *
  43. * ------------------------------------------------------------------------*
  44. * n=0xxxyyyy,yyyyyyyy |short copy back y bytes and run x+3 from dest *
  45. * n=10xxxxxx,n1,n2,...,nx+1|med length copy the next x+1 bytes from source*
  46. * n=11xxxxxx,w1 |med copy from dest x+3 bytes from offset w1 *
  47. * n=11111111,w1,w2 |long copy from dest w1 bytes from offset w2 *
  48. * n=11111110,w1,b1 |long run of byte b1 for w1 bytes *
  49. * n=10000000 |end of data reached *
  50. * *
  51. * *
  52. * INPUT: *
  53. * void * source ptr *
  54. * void * destination ptr *
  55. * unsigned long length of uncompressed data *
  56. * *
  57. * *
  58. * OUTPUT: *
  59. * unsigned long # of destination bytes written *
  60. * *
  61. * WARNINGS: *
  62. * 3rd argument is dummy. It exists to provide cross-platform *
  63. * compatibility. Note therefore that this implementation does not *
  64. * check for corrupt source data by testing the uncompressed length. *
  65. * *
  66. * HISTORY: *
  67. * 03/20/1995 IML : Created. *
  68. *=========================================================================*/
  69. int LCW_Uncomp(void const * source, void * dest, unsigned long )
  70. {
  71. unsigned char * source_ptr, * dest_ptr, * copy_ptr, op_code, data;
  72. unsigned count, * word_dest_ptr, word_data;
  73. /* Copy the source and destination ptrs. */
  74. source_ptr = (unsigned char*) source;
  75. dest_ptr = (unsigned char*) dest;
  76. while (1 /*TRUE*/) {
  77. /* Read in the operation code. */
  78. op_code = *source_ptr++;
  79. if (!(op_code & 0x80)) {
  80. /* Do a short copy from destination. */
  81. count = (op_code >> 4) + 3;
  82. copy_ptr = dest_ptr - ((unsigned) *source_ptr++ + (((unsigned) op_code & 0x0f) << 8));
  83. while (count--) *dest_ptr++ = *copy_ptr++;
  84. } else {
  85. if (!(op_code & 0x40)) {
  86. if (op_code == 0x80) {
  87. /* Return # of destination bytes written. */
  88. return ((unsigned long) (dest_ptr - (unsigned char*) dest));
  89. } else {
  90. /* Do a medium copy from source. */
  91. count = op_code & 0x3f;
  92. while (count--) *dest_ptr++ = *source_ptr++;
  93. }
  94. } else {
  95. if (op_code == 0xfe) {
  96. /* Do a long run. */
  97. count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
  98. word_data = data = *(source_ptr + 2);
  99. word_data = (word_data << 24) + (word_data << 16) + (word_data << 8) + word_data;
  100. source_ptr += 3;
  101. copy_ptr = dest_ptr + 4 - ((unsigned) dest_ptr & 0x3);
  102. count -= (copy_ptr - dest_ptr);
  103. while (dest_ptr < copy_ptr) *dest_ptr++ = data;
  104. word_dest_ptr = (unsigned*) dest_ptr;
  105. dest_ptr += (count & 0xfffffffc);
  106. while (word_dest_ptr < (unsigned*) dest_ptr) {
  107. *word_dest_ptr = word_data;
  108. *(word_dest_ptr + 1) = word_data;
  109. word_dest_ptr += 2;
  110. }
  111. copy_ptr = dest_ptr + (count & 0x3);
  112. while (dest_ptr < copy_ptr) *dest_ptr++ = data;
  113. } else {
  114. if (op_code == 0xff) {
  115. /* Do a long copy from destination. */
  116. count = *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
  117. copy_ptr = (unsigned char*) dest + *(source_ptr + 2) + ((unsigned) *(source_ptr + 3) << 8);
  118. source_ptr += 4;
  119. while (count--) *dest_ptr++ = *copy_ptr++;
  120. } else {
  121. /* Do a medium copy from destination. */
  122. count = (op_code & 0x3f) + 3;
  123. copy_ptr = (unsigned char*) dest + *source_ptr + ((unsigned) *(source_ptr + 1) << 8);
  124. source_ptr += 2;
  125. while (count--) *dest_ptr++ = *copy_ptr++;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }