-
Notifications
You must be signed in to change notification settings - Fork 2
/
rarvm.cpp
364 lines (325 loc) · 9.51 KB
/
rarvm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "rar.hpp"
RarVM::RarVM()
{
Mem=NULL;
}
RarVM::~RarVM()
{
delete[] Mem;
}
void RarVM::Init()
{
if (Mem==NULL)
Mem=new byte[VM_MEMSIZE+4];
}
void RarVM::Execute(VM_PreparedProgram *Prg)
{
memcpy(R,Prg->InitR,sizeof(Prg->InitR));
Prg->FilteredData=NULL;
if (Prg->Type!=VMSF_NONE)
{
bool Success=ExecuteStandardFilter(Prg->Type);
uint BlockSize=Prg->InitR[4] & VM_MEMMASK;
Prg->FilteredDataSize=BlockSize;
if (Prg->Type==VMSF_DELTA || Prg->Type==VMSF_RGB || Prg->Type==VMSF_AUDIO)
Prg->FilteredData=2*BlockSize>VM_MEMSIZE || !Success ? Mem:Mem+BlockSize;
else
Prg->FilteredData=Mem;
}
}
void RarVM::Prepare(byte *Code,uint CodeSize,VM_PreparedProgram *Prg)
{
// Calculate the single byte XOR checksum to check validity of VM code.
byte XorSum=0;
for (uint I=1;I<CodeSize;I++)
XorSum^=Code[I];
if (XorSum!=Code[0])
return;
struct StandardFilters
{
uint Length;
uint CRC;
VM_StandardFilters Type;
} static StdList[]={
53, 0xad576887, VMSF_E8,
57, 0x3cd7e57e, VMSF_E8E9,
120, 0x3769893f, VMSF_ITANIUM,
29, 0x0e06077d, VMSF_DELTA,
149, 0x1c2c5dc8, VMSF_RGB,
216, 0xbc85e701, VMSF_AUDIO
};
uint CodeCRC=CRC32(0xffffffff,Code,CodeSize)^0xffffffff;
for (uint I=0;I<ASIZE(StdList);I++)
if (StdList[I].CRC==CodeCRC && StdList[I].Length==CodeSize)
{
Prg->Type=StdList[I].Type;
break;
}
}
uint RarVM::ReadData(BitInput &Inp)
{
uint Data=Inp.fgetbits();
switch(Data&0xc000)
{
case 0:
Inp.faddbits(6);
return (Data>>10)&0xf;
case 0x4000:
if ((Data&0x3c00)==0)
{
Data=0xffffff00|((Data>>2)&0xff);
Inp.faddbits(14);
}
else
{
Data=(Data>>6)&0xff;
Inp.faddbits(10);
}
return Data;
case 0x8000:
Inp.faddbits(2);
Data=Inp.fgetbits();
Inp.faddbits(16);
return Data;
default:
Inp.faddbits(2);
Data=(Inp.fgetbits()<<16);
Inp.faddbits(16);
Data|=Inp.fgetbits();
Inp.faddbits(16);
return Data;
}
}
void RarVM::SetMemory(size_t Pos,byte *Data,size_t DataSize)
{
if (Pos<VM_MEMSIZE && Data!=Mem+Pos)
{
// We can have NULL Data for invalid filters with DataSize==0. While most
// sensible memmove implementations do not care about data if size is 0,
// let's follow the standard and check the size first.
size_t CopySize=Min(DataSize,VM_MEMSIZE-Pos);
if (CopySize!=0)
memmove(Mem+Pos,Data,CopySize);
}
}
bool RarVM::ExecuteStandardFilter(VM_StandardFilters FilterType)
{
switch(FilterType)
{
case VMSF_E8:
case VMSF_E8E9:
{
byte *Data=Mem;
uint DataSize=R[4],FileOffset=R[6];
if (DataSize>VM_MEMSIZE || DataSize<4)
return false;
const uint FileSize=0x1000000;
byte CmpByte2=FilterType==VMSF_E8E9 ? 0xe9:0xe8;
for (uint CurPos=0;CurPos<DataSize-4;)
{
byte CurByte=*(Data++);
CurPos++;
if (CurByte==0xe8 || CurByte==CmpByte2)
{
uint Offset=CurPos+FileOffset;
uint Addr=RawGet4(Data);
// We check 0x80000000 bit instead of '< 0' comparison
// not assuming int32 presence or uint size and endianness.
if ((Addr & 0x80000000)!=0) // Addr<0
{
if (((Addr+Offset) & 0x80000000)==0) // Addr+Offset>=0
RawPut4(Addr+FileSize,Data);
}
else
if (((Addr-FileSize) & 0x80000000)!=0) // Addr<FileSize
RawPut4(Addr-Offset,Data);
Data+=4;
CurPos+=4;
}
}
}
break;
case VMSF_ITANIUM:
{
byte *Data=Mem;
uint DataSize=R[4],FileOffset=R[6];
if (DataSize>VM_MEMSIZE || DataSize<21)
return false;
uint CurPos=0;
FileOffset>>=4;
while (CurPos<DataSize-21)
{
int Byte=(Data[0]&0x1f)-0x10;
if (Byte>=0)
{
static byte Masks[16]={4,4,6,6,0,0,7,7,4,4,0,0,4,4,0,0};
byte CmdMask=Masks[Byte];
if (CmdMask!=0)
for (uint I=0;I<=2;I++)
if (CmdMask & (1<<I))
{
uint StartPos=I*41+5;
uint OpType=FilterItanium_GetBits(Data,StartPos+37,4);
if (OpType==5)
{
uint Offset=FilterItanium_GetBits(Data,StartPos+13,20);
FilterItanium_SetBits(Data,(Offset-FileOffset)&0xfffff,StartPos+13,20);
}
}
}
Data+=16;
CurPos+=16;
FileOffset++;
}
}
break;
case VMSF_DELTA:
{
uint DataSize=R[4],Channels=R[0],SrcPos=0,Border=DataSize*2;
if (DataSize>VM_MEMSIZE/2 || Channels>MAX3_UNPACK_CHANNELS || Channels==0)
return false;
// Bytes from same channels are grouped to continual data blocks,
// so we need to place them back to their interleaving positions.
for (uint CurChannel=0;CurChannel<Channels;CurChannel++)
{
byte PrevByte=0;
for (uint DestPos=DataSize+CurChannel;DestPos<Border;DestPos+=Channels)
Mem[DestPos]=(PrevByte-=Mem[SrcPos++]);
}
}
break;
case VMSF_RGB:
{
uint DataSize=R[4],Width=R[0]-3,PosR=R[1];
if (DataSize>VM_MEMSIZE/2 || DataSize<3 || Width>DataSize || PosR>2)
return false;
byte *SrcData=Mem,*DestData=SrcData+DataSize;
const uint Channels=3;
for (uint CurChannel=0;CurChannel<Channels;CurChannel++)
{
uint PrevByte=0;
for (uint I=CurChannel;I<DataSize;I+=Channels)
{
uint Predicted;
if (I>=Width+3)
{
byte *UpperData=DestData+I-Width;
uint UpperByte=*UpperData;
uint UpperLeftByte=*(UpperData-3);
Predicted=PrevByte+UpperByte-UpperLeftByte;
int pa=abs((int)(Predicted-PrevByte));
int pb=abs((int)(Predicted-UpperByte));
int pc=abs((int)(Predicted-UpperLeftByte));
if (pa<=pb && pa<=pc)
Predicted=PrevByte;
else
if (pb<=pc)
Predicted=UpperByte;
else
Predicted=UpperLeftByte;
}
else
Predicted=PrevByte;
PrevByte=DestData[I]=(byte)(Predicted-*(SrcData++));
}
}
for (uint I=PosR,Border=DataSize-2;I<Border;I+=3)
{
byte G=DestData[I+1];
DestData[I]+=G;
DestData[I+2]+=G;
}
}
break;
case VMSF_AUDIO:
{
uint DataSize=R[4],Channels=R[0];
byte *SrcData=Mem,*DestData=SrcData+DataSize;
// In fact, audio channels never exceed 4.
if (DataSize>VM_MEMSIZE/2 || Channels>128 || Channels==0)
return false;
for (uint CurChannel=0;CurChannel<Channels;CurChannel++)
{
uint PrevByte=0,PrevDelta=0,Dif[7];
int D1=0,D2=0,D3;
int K1=0,K2=0,K3=0;
memset(Dif,0,sizeof(Dif));
for (uint I=CurChannel,ByteCount=0;I<DataSize;I+=Channels,ByteCount++)
{
D3=D2;
D2=PrevDelta-D1;
D1=PrevDelta;
uint Predicted=8*PrevByte+K1*D1+K2*D2+K3*D3;
Predicted=(Predicted>>3) & 0xff;
uint CurByte=*(SrcData++);
Predicted-=CurByte;
DestData[I]=Predicted;
PrevDelta=(signed char)(Predicted-PrevByte);
PrevByte=Predicted;
int D=(signed char)CurByte;
// Left shift of negative value is undefined behavior in C++,
// so we cast it to unsigned to follow the standard.
D=(uint)D<<3;
Dif[0]+=abs(D);
Dif[1]+=abs(D-D1);
Dif[2]+=abs(D+D1);
Dif[3]+=abs(D-D2);
Dif[4]+=abs(D+D2);
Dif[5]+=abs(D-D3);
Dif[6]+=abs(D+D3);
if ((ByteCount & 0x1f)==0)
{
uint MinDif=Dif[0],NumMinDif=0;
Dif[0]=0;
for (uint J=1;J<ASIZE(Dif);J++)
{
if (Dif[J]<MinDif)
{
MinDif=Dif[J];
NumMinDif=J;
}
Dif[J]=0;
}
switch(NumMinDif)
{
case 1: if (K1>=-16) K1--; break;
case 2: if (K1 < 16) K1++; break;
case 3: if (K2>=-16) K2--; break;
case 4: if (K2 < 16) K2++; break;
case 5: if (K3>=-16) K3--; break;
case 6: if (K3 < 16) K3++; break;
}
}
}
}
}
break;
}
return true;
}
uint RarVM::FilterItanium_GetBits(byte *Data,uint BitPos,uint BitCount)
{
uint InAddr=BitPos/8;
uint InBit=BitPos&7;
uint BitField=(uint)Data[InAddr++];
BitField|=(uint)Data[InAddr++] << 8;
BitField|=(uint)Data[InAddr++] << 16;
BitField|=(uint)Data[InAddr] << 24;
BitField >>= InBit;
return BitField & (0xffffffff>>(32-BitCount));
}
void RarVM::FilterItanium_SetBits(byte *Data,uint BitField,uint BitPos,uint BitCount)
{
uint InAddr=BitPos/8;
uint InBit=BitPos&7;
uint AndMask=0xffffffff>>(32-BitCount);
AndMask=~(AndMask<<InBit);
BitField<<=InBit;
for (uint I=0;I<4;I++)
{
Data[InAddr+I]&=AndMask;
Data[InAddr+I]|=BitField;
AndMask=(AndMask>>8)|0xff000000;
BitField>>=8;
}
}