Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
refdecode.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 __REFREAD
22#define __REFREAD 1
23
24#include <string.h>
25#include "codex.h"
26#include "refcodex.h"
27
28/****************************************************************/
29/* Information Functions */
30/****************************************************************/
31
32/* check for reasonable header: */
33/* 10fb header */
34
35bool GCALL REF_is(const void *compresseddata)
36{
37 bool ok=false;
38 int packtype=ggetm(compresseddata,2);
39
40 if (packtype==0x10fb
41 || packtype==0x11fb
42 || packtype==0x90fb
43 || packtype==0x91fb)
44 ok = true;
45
46 return(ok);
47}
48
49
50/****************************************************************/
51/* Decode Functions */
52/****************************************************************/
53
54int GCALL REF_size(const void *compresseddata)
55{
56 int len=0;
57 int packtype=ggetm(compresseddata,2);
58 int ssize=(packtype&0x8000)?4:3;
59
60 if (packtype&0x100) /* 11fb */
61 {
62 len = ggetm((char *)compresseddata+2+ssize,ssize);
63 }
64 else /* 10fb */
65 {
66 len = ggetm((char *)compresseddata+2,ssize);
67 }
68
69 return(len);
70}
71
72
73int GCALL REF_decode(void *dest, const void *compresseddata, int *compressedsize)
74{
75 unsigned char *s;
76 unsigned char *ref;
77 unsigned char *d;
78 unsigned char first;
79 unsigned char second;
80 unsigned char third;
81 unsigned char forth;
82 unsigned int run;
83 unsigned int type;
84 int ulen;
85
86 s = (unsigned char *) compresseddata;
87 d = (unsigned char *) dest;
88 ulen = 0L;
89
90 if (s)
91 {
92 type = *s++;
93 type = (type<<8) + *s++;
94
95 if (type&0x8000) /* 4 byte size field */
96 {
97 if (type&0x100) /* skip ulen */
98 s += 4;
99
100 ulen = *s++;
101 ulen = (ulen<<8) + *s++;
102 ulen = (ulen<<8) + *s++;
103 ulen = (ulen<<8) + *s++;
104 }
105 else
106 {
107 if (type&0x100) /* skip ulen */
108 s += 3;
109
110 ulen = *s++;
111 ulen = (ulen<<8) + *s++;
112 ulen = (ulen<<8) + *s++;
113 }
114
115 for (;;)
116 {
117 first = *s++;
118 if (!(first&0x80)) /* short form */
119 {
120 second = *s++;
121 run = first&3;
122 while (run--)
123 *d++ = *s++;
124 ref = d-1 - (((first&0x60)<<3) + second);
125 run = ((first&0x1c)>>2)+3-1;
126 do
127 {
128 *d++ = *ref++;
129 } while (run--);
130 continue;
131 }
132 if (!(first&0x40)) /* int form */
133 {
134 second = *s++;
135 third = *s++;
136 run = second>>6;
137 while (run--)
138 *d++ = *s++;
139
140 ref = d-1 - (((second&0x3f)<<8) + third);
141
142 run = (first&0x3f)+4-1;
143 do
144 {
145 *d++ = *ref++;
146 } while (run--);
147 continue;
148 }
149 if (!(first&0x20)) /* very int form */
150 {
151 second = *s++;
152 third = *s++;
153 forth = *s++;
154 run = first&3;
155 while (run--)
156 *d++ = *s++;
157
158 ref = d-1 - (((first&0x10)>>4<<16) + (second<<8) + third);
159
160 run = ((first&0x0c)>>2<<8) + forth + 5-1;
161 do
162 {
163 *d++ = *ref++;
164 } while (run--);
165 continue;
166 }
167 run = ((first&0x1f)<<2)+4; /* literal */
168 if (run<=112)
169 {
170 while (run--)
171 *d++ = *s++;
172 continue;
173 }
174 run = first&3; /* eof (+0..3 literal) */
175 while (run--)
176 *d++ = *s++;
177 break;
178 }
179 }
180 if (compressedsize)
181 *compressedsize = (int)((char *)s-(char *)compresseddata);
182 return(ulen);
183}
184
185#endif
186
#define GCALL
Definition codex.h:81
int GCALL REF_decode(void *dest, const void *compresseddata, int *compressedsize)
Definition refdecode.cpp:73
bool GCALL REF_is(const void *compresseddata)
Definition refdecode.cpp:35
int GCALL REF_size(const void *compresseddata)
Definition refdecode.cpp:54