LCW.CPP 7.0 KB

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