refdecode.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // Copyright (C) Electronic Arts Canada Inc. 1995-2002. All rights reserved.
  19. #ifndef __REFREAD
  20. #define __REFREAD 1
  21. #include <string.h>
  22. #include "codex.h"
  23. #include "refcodex.h"
  24. /****************************************************************/
  25. /* Information Functions */
  26. /****************************************************************/
  27. /* check for reasonable header: */
  28. /* 10fb header */
  29. bool GCALL REF_is(const void *compresseddata)
  30. {
  31. bool ok=false;
  32. int packtype=ggetm(compresseddata,2);
  33. if (packtype==0x10fb
  34. || packtype==0x11fb
  35. || packtype==0x90fb
  36. || packtype==0x91fb)
  37. ok = true;
  38. return(ok);
  39. }
  40. /****************************************************************/
  41. /* Decode Functions */
  42. /****************************************************************/
  43. int GCALL REF_size(const void *compresseddata)
  44. {
  45. int len=0;
  46. int packtype=ggetm(compresseddata,2);
  47. int ssize=(packtype&0x8000)?4:3;
  48. if (packtype&0x100) /* 11fb */
  49. {
  50. len = ggetm((char *)compresseddata+2+ssize,ssize);
  51. }
  52. else /* 10fb */
  53. {
  54. len = ggetm((char *)compresseddata+2,ssize);
  55. }
  56. return(len);
  57. }
  58. int GCALL REF_decode(void *dest, const void *compresseddata, int *compressedsize)
  59. {
  60. unsigned char *s;
  61. unsigned char *ref;
  62. unsigned char *d;
  63. unsigned char first;
  64. unsigned char second;
  65. unsigned char third;
  66. unsigned char forth;
  67. unsigned int run;
  68. unsigned int type;
  69. int ulen;
  70. s = (unsigned char *) compresseddata;
  71. d = (unsigned char *) dest;
  72. ulen = 0L;
  73. if (s)
  74. {
  75. type = *s++;
  76. type = (type<<8) + *s++;
  77. if (type&0x8000) /* 4 byte size field */
  78. {
  79. if (type&0x100) /* skip ulen */
  80. s += 4;
  81. ulen = *s++;
  82. ulen = (ulen<<8) + *s++;
  83. ulen = (ulen<<8) + *s++;
  84. ulen = (ulen<<8) + *s++;
  85. }
  86. else
  87. {
  88. if (type&0x100) /* skip ulen */
  89. s += 3;
  90. ulen = *s++;
  91. ulen = (ulen<<8) + *s++;
  92. ulen = (ulen<<8) + *s++;
  93. }
  94. for (;;)
  95. {
  96. first = *s++;
  97. if (!(first&0x80)) /* short form */
  98. {
  99. second = *s++;
  100. run = first&3;
  101. while (run--)
  102. *d++ = *s++;
  103. ref = d-1 - (((first&0x60)<<3) + second);
  104. run = ((first&0x1c)>>2)+3-1;
  105. do
  106. {
  107. *d++ = *ref++;
  108. } while (run--);
  109. continue;
  110. }
  111. if (!(first&0x40)) /* int form */
  112. {
  113. second = *s++;
  114. third = *s++;
  115. run = second>>6;
  116. while (run--)
  117. *d++ = *s++;
  118. ref = d-1 - (((second&0x3f)<<8) + third);
  119. run = (first&0x3f)+4-1;
  120. do
  121. {
  122. *d++ = *ref++;
  123. } while (run--);
  124. continue;
  125. }
  126. if (!(first&0x20)) /* very int form */
  127. {
  128. second = *s++;
  129. third = *s++;
  130. forth = *s++;
  131. run = first&3;
  132. while (run--)
  133. *d++ = *s++;
  134. ref = d-1 - (((first&0x10)>>4<<16) + (second<<8) + third);
  135. run = ((first&0x0c)>>2<<8) + forth + 5-1;
  136. do
  137. {
  138. *d++ = *ref++;
  139. } while (run--);
  140. continue;
  141. }
  142. run = ((first&0x1f)<<2)+4; /* literal */
  143. if (run<=112)
  144. {
  145. while (run--)
  146. *d++ = *s++;
  147. continue;
  148. }
  149. run = first&3; /* eof (+0..3 literal) */
  150. while (run--)
  151. *d++ = *s++;
  152. break;
  153. }
  154. }
  155. if (compressedsize)
  156. *compressedsize = (int)((char *)s-(char *)compresseddata);
  157. return(ulen);
  158. }
  159. #endif