Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
btreedecode.cpp
Go to the documentation of this file.
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
19// Copyright (C) Electronic Arts Canada Inc. 1995-2002. All rights reserved.
20
21#ifndef __BTRREAD
22#define __BTRREAD 1
23
24#include <string.h>
25#include "codex.h"
26#include "btreecodex.h"
27
28/****************************************************************/
29/* Internal Functions */
30/****************************************************************/
31
33{
34 signed char cluetbl[256];
35 unsigned char left[256];
36 unsigned char right[256];
37 unsigned char *d;
38};
39
40static void BTREE_chase(struct BTreeDecodeContext *DC, unsigned char node)
41{
42 if (DC->cluetbl[node])
43 {
44 BTREE_chase(DC,DC->left[node]);
45 BTREE_chase(DC,DC->right[node]);
46 return;
47 }
48 *DC->d++ = node;
49}
50
51static int BTREE_decompress(unsigned char *packbuf,unsigned char *unpackbuf)
52{
53 int node;
54 int i;
55 int nodes;
56 int clue;
57 int ulen;
58 unsigned char *s;
59 signed char c;
60 unsigned int type;
61 struct BTreeDecodeContext DC;
62
63 s = packbuf;
64 DC.d = unpackbuf;
65 ulen = 0L;
66
67 if (s)
68 {
69 type = ggetm(s,2);
70 s += 2;
71
72 /* (skip nothing for 0x46fb) */
73 if (type==0x47fb) /* skip ulen */
74 s += 3;
75
76 ulen = ggetm(s,3);
77 s += 3;
78
79 for (i=0;i<256;++i) /* 0 means a code is a leaf */
80 DC.cluetbl[i] = 0;
81
82 clue = *s++;
83 DC.cluetbl[clue] = 1; /* mark clue as special */
84
85 nodes = *s++;
86 for (i=0;i<nodes;++i)
87 { node = *s++;
88 DC.left[node] = *s++;
89 DC.right[node] = *s++;
90 DC.cluetbl[node] = (signed char)-1;
91 }
92
93 for (;;)
94 {
95 node = (int) *s++;
96 c=DC.cluetbl[node];
97 if (!c)
98 {
99 *DC.d++ = (unsigned char) node;
100 continue;
101 }
102 if (c<0)
103 {
104 BTREE_chase(&DC,DC.left[node]);
105 BTREE_chase(&DC,DC.right[node]);
106 continue;
107 }
108 node = (int) *s++;
109 if (node)
110 {
111 *DC.d++ = (char) node;
112 continue;
113 }
114 break;
115 }
116 }
117 return(ulen);
118}
119
120/****************************************************************/
121/* Information Functions */
122/****************************************************************/
123
124/* check for reasonable header: */
125/* 46fb header */
126
127bool GCALL BTREE_is(const void *compresseddata)
128{
129 bool ok=false;
130
131 if (ggetm(compresseddata,2)==0x46fb
132 || ggetm(compresseddata,2)==0x47fb)
133 ok = true;
134
135 return(ok);
136}
137
138
139/****************************************************************/
140/* Decode Functions */
141/****************************************************************/
142
143int GCALL BTREE_size(const void *compresseddata)
144{
145 int len=0;
146
147 if (ggetm(compresseddata,2)==0x46fb)
148 {
149 len = ggetm((char *)compresseddata+2,3);
150 }
151 else
152 {
153 len = ggetm((char *)compresseddata+2+3,3);
154 }
155
156 return(len);
157}
158
159int GCALL BTREE_decode(void *dest, const void *compresseddata, int *compressedsize)
160{
161 return(BTREE_decompress((unsigned char *)compresseddata,(unsigned char *)dest));
162}
163
164#endif
165
int GCALL BTREE_decode(void *dest, const void *compresseddata, int *compressedsize)
int GCALL BTREE_size(const void *compresseddata)
bool GCALL BTREE_is(const void *compresseddata)
#define GCALL
Definition codex.h:81
signed char cluetbl[256]
unsigned char left[256]
unsigned char right[256]
unsigned char * d