Fix
[ryzomcore.git] / nel / 3rdparty / seven_zip / Bcj2.c
blob9a0046a6508d73201e7355e83113938f12f2a068
1 /* Bcj2.c -- BCJ2 Decoder (Converter for x86 code)
2 2018-04-28 : Igor Pavlov : Public domain */
4 #include "Precomp.h"
6 #include "Bcj2.h"
7 #include "CpuArch.h"
9 #define CProb UInt16
11 #define kTopValue ((UInt32)1 << 24)
12 #define kNumModelBits 11
13 #define kBitModelTotal (1 << kNumModelBits)
14 #define kNumMoveBits 5
16 #define _IF_BIT_0 ttt = *prob; bound = (p->range >> kNumModelBits) * ttt; if (p->code < bound)
17 #define _UPDATE_0 p->range = bound; *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
18 #define _UPDATE_1 p->range -= bound; p->code -= bound; *prob = (CProb)(ttt - (ttt >> kNumMoveBits));
20 void Bcj2Dec_Init(CBcj2Dec *p)
22 unsigned i;
24 p->state = BCJ2_DEC_STATE_OK;
25 p->ip = 0;
26 p->temp[3] = 0;
27 p->range = 0;
28 p->code = 0;
29 for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++)
30 p->probs[i] = kBitModelTotal >> 1;
33 SRes Bcj2Dec_Decode(CBcj2Dec *p)
35 if (p->range <= 5)
37 p->state = BCJ2_DEC_STATE_OK;
38 for (; p->range != 5; p->range++)
40 if (p->range == 1 && p->code != 0)
41 return SZ_ERROR_DATA;
43 if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
45 p->state = BCJ2_STREAM_RC;
46 return SZ_OK;
49 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
52 if (p->code == 0xFFFFFFFF)
53 return SZ_ERROR_DATA;
55 p->range = 0xFFFFFFFF;
57 else if (p->state >= BCJ2_DEC_STATE_ORIG_0)
59 while (p->state <= BCJ2_DEC_STATE_ORIG_3)
61 Byte *dest = p->dest;
62 if (dest == p->destLim)
63 return SZ_OK;
64 *dest = p->temp[(size_t)p->state - BCJ2_DEC_STATE_ORIG_0];
65 p->state++;
66 p->dest = dest + 1;
71 if (BCJ2_IS_32BIT_STREAM(p->state))
73 const Byte *cur = p->bufs[p->state];
74 if (cur == p->lims[p->state])
75 return SZ_OK;
76 p->bufs[p->state] = cur + 4;
79 UInt32 val;
80 Byte *dest;
81 SizeT rem;
83 p->ip += 4;
84 val = GetBe32(cur) - p->ip;
85 dest = p->dest;
86 rem = p->destLim - dest;
87 if (rem < 4)
89 SizeT i;
90 SetUi32(p->temp, val);
91 for (i = 0; i < rem; i++)
92 dest[i] = p->temp[i];
93 p->dest = dest + rem;
94 p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
95 return SZ_OK;
97 SetUi32(dest, val);
98 p->temp[3] = (Byte)(val >> 24);
99 p->dest = dest + 4;
100 p->state = BCJ2_DEC_STATE_OK;
105 for (;;)
107 if (BCJ2_IS_32BIT_STREAM(p->state))
108 p->state = BCJ2_DEC_STATE_OK;
109 else
111 if (p->range < kTopValue)
113 if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
115 p->state = BCJ2_STREAM_RC;
116 return SZ_OK;
118 p->range <<= 8;
119 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
123 const Byte *src = p->bufs[BCJ2_STREAM_MAIN];
124 const Byte *srcLim;
125 Byte *dest;
126 SizeT num = p->lims[BCJ2_STREAM_MAIN] - src;
128 if (num == 0)
130 p->state = BCJ2_STREAM_MAIN;
131 return SZ_OK;
134 dest = p->dest;
135 if (num > (SizeT)(p->destLim - dest))
137 num = p->destLim - dest;
138 if (num == 0)
140 p->state = BCJ2_DEC_STATE_ORIG;
141 return SZ_OK;
145 srcLim = src + num;
147 if (p->temp[3] == 0x0F && (src[0] & 0xF0) == 0x80)
148 *dest = src[0];
149 else for (;;)
151 Byte b = *src;
152 *dest = b;
153 if (b != 0x0F)
155 if ((b & 0xFE) == 0xE8)
156 break;
157 dest++;
158 if (++src != srcLim)
159 continue;
160 break;
162 dest++;
163 if (++src == srcLim)
164 break;
165 if ((*src & 0xF0) != 0x80)
166 continue;
167 *dest = *src;
168 break;
171 num = src - p->bufs[BCJ2_STREAM_MAIN];
173 if (src == srcLim)
175 p->temp[3] = src[-1];
176 p->bufs[BCJ2_STREAM_MAIN] = src;
177 p->ip += (UInt32)num;
178 p->dest += num;
179 p->state =
180 p->bufs[BCJ2_STREAM_MAIN] ==
181 p->lims[BCJ2_STREAM_MAIN] ?
182 (unsigned)BCJ2_STREAM_MAIN :
183 (unsigned)BCJ2_DEC_STATE_ORIG;
184 return SZ_OK;
188 UInt32 bound, ttt;
189 CProb *prob;
190 Byte b = src[0];
191 Byte prev = (Byte)(num == 0 ? p->temp[3] : src[-1]);
193 p->temp[3] = b;
194 p->bufs[BCJ2_STREAM_MAIN] = src + 1;
195 num++;
196 p->ip += (UInt32)num;
197 p->dest += num;
199 prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)prev : (b == 0xE9 ? 1 : 0));
201 _IF_BIT_0
203 _UPDATE_0
204 continue;
206 _UPDATE_1
213 UInt32 val;
214 unsigned cj = (p->temp[3] == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP;
215 const Byte *cur = p->bufs[cj];
216 Byte *dest;
217 SizeT rem;
219 if (cur == p->lims[cj])
221 p->state = cj;
222 break;
225 val = GetBe32(cur);
226 p->bufs[cj] = cur + 4;
228 p->ip += 4;
229 val -= p->ip;
230 dest = p->dest;
231 rem = p->destLim - dest;
233 if (rem < 4)
235 p->temp[0] = (Byte)val; if (rem > 0) dest[0] = (Byte)val; val >>= 8;
236 p->temp[1] = (Byte)val; if (rem > 1) dest[1] = (Byte)val; val >>= 8;
237 p->temp[2] = (Byte)val; if (rem > 2) dest[2] = (Byte)val; val >>= 8;
238 p->temp[3] = (Byte)val;
239 p->dest = dest + rem;
240 p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
241 break;
244 SetUi32(dest, val);
245 p->temp[3] = (Byte)(val >> 24);
246 p->dest = dest + 4;
250 if (p->range < kTopValue && p->bufs[BCJ2_STREAM_RC] != p->lims[BCJ2_STREAM_RC])
252 p->range <<= 8;
253 p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
256 return SZ_OK;