LCWUNCMP.CPP 7.1 KB

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