btreedecode.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ** Command & Conquer Generals(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 __BTRREAD
  20. #define __BTRREAD 1
  21. #include <string.h>
  22. #include "codex.h"
  23. #include "btreecodex.h"
  24. /****************************************************************/
  25. /* Internal Functions */
  26. /****************************************************************/
  27. struct BTreeDecodeContext
  28. {
  29. signed char cluetbl[256];
  30. unsigned char left[256];
  31. unsigned char right[256];
  32. unsigned char *d;
  33. };
  34. static void BTREE_chase(struct BTreeDecodeContext *DC, unsigned char node)
  35. {
  36. if (DC->cluetbl[node])
  37. {
  38. BTREE_chase(DC,DC->left[node]);
  39. BTREE_chase(DC,DC->right[node]);
  40. return;
  41. }
  42. *DC->d++ = node;
  43. }
  44. static int BTREE_decompress(unsigned char *packbuf,unsigned char *unpackbuf)
  45. {
  46. int node;
  47. int i;
  48. int nodes;
  49. int clue;
  50. int ulen;
  51. unsigned char *s;
  52. signed char c;
  53. unsigned int type;
  54. struct BTreeDecodeContext DC;
  55. s = packbuf;
  56. DC.d = unpackbuf;
  57. ulen = 0L;
  58. if (s)
  59. {
  60. type = ggetm(s,2);
  61. s += 2;
  62. /* (skip nothing for 0x46fb) */
  63. if (type==0x47fb) /* skip ulen */
  64. s += 3;
  65. ulen = ggetm(s,3);
  66. s += 3;
  67. for (i=0;i<256;++i) /* 0 means a code is a leaf */
  68. DC.cluetbl[i] = 0;
  69. clue = *s++;
  70. DC.cluetbl[clue] = 1; /* mark clue as special */
  71. nodes = *s++;
  72. for (i=0;i<nodes;++i)
  73. { node = *s++;
  74. DC.left[node] = *s++;
  75. DC.right[node] = *s++;
  76. DC.cluetbl[node] = (signed char)-1;
  77. }
  78. for (;;)
  79. {
  80. node = (int) *s++;
  81. c=DC.cluetbl[node];
  82. if (!c)
  83. {
  84. *DC.d++ = (unsigned char) node;
  85. continue;
  86. }
  87. if (c<0)
  88. {
  89. BTREE_chase(&DC,DC.left[node]);
  90. BTREE_chase(&DC,DC.right[node]);
  91. continue;
  92. }
  93. node = (int) *s++;
  94. if (node)
  95. {
  96. *DC.d++ = (char) node;
  97. continue;
  98. }
  99. break;
  100. }
  101. }
  102. return(ulen);
  103. }
  104. /****************************************************************/
  105. /* Information Functions */
  106. /****************************************************************/
  107. /* check for reasonable header: */
  108. /* 46fb header */
  109. bool GCALL BTREE_is(const void *compresseddata)
  110. {
  111. bool ok=false;
  112. if (ggetm(compresseddata,2)==0x46fb
  113. || ggetm(compresseddata,2)==0x47fb)
  114. ok = true;
  115. return(ok);
  116. }
  117. /****************************************************************/
  118. /* Decode Functions */
  119. /****************************************************************/
  120. int GCALL BTREE_size(const void *compresseddata)
  121. {
  122. int len=0;
  123. if (ggetm(compresseddata,2)==0x46fb)
  124. {
  125. len = ggetm((char *)compresseddata+2,3);
  126. }
  127. else
  128. {
  129. len = ggetm((char *)compresseddata+2+3,3);
  130. }
  131. return(len);
  132. }
  133. int GCALL BTREE_decode(void *dest, const void *compresseddata, int *compressedsize)
  134. {
  135. return(BTREE_decompress((unsigned char *)compresseddata,(unsigned char *)dest));
  136. }
  137. #endif