rarvm.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. #include "rar.hpp"
  2. #include "rarvmtbl.cpp"
  3. RarVM::RarVM()
  4. {
  5. Mem=NULL;
  6. }
  7. RarVM::~RarVM()
  8. {
  9. delete[] Mem;
  10. }
  11. void RarVM::Init()
  12. {
  13. if (Mem==NULL)
  14. Mem=new byte[VM_MEMSIZE+4];
  15. }
  16. /*********************************************************************
  17. IS_VM_MEM macro checks if address belongs to VM memory pool (Mem).
  18. Only Mem data are always low endian regardless of machine architecture,
  19. so we need to convert them to native format when reading or writing.
  20. VM registers have endianness of host machine.
  21. **********************************************************************/
  22. #define IS_VM_MEM(a) (((byte*)a)>=Mem && ((byte*)a)<Mem+VM_MEMSIZE)
  23. inline uint RarVM::GetValue(bool ByteMode,uint *Addr)
  24. {
  25. if (ByteMode)
  26. {
  27. #ifdef BIG_ENDIAN
  28. if (IS_VM_MEM(Addr))
  29. return(*(byte *)Addr);
  30. else
  31. return(*Addr & 0xff);
  32. #else
  33. return(*(byte *)Addr);
  34. #endif
  35. }
  36. else
  37. {
  38. #if defined(BIG_ENDIAN) || !defined(ALLOW_NOT_ALIGNED_INT)
  39. if (IS_VM_MEM(Addr))
  40. {
  41. byte *B=(byte *)Addr;
  42. return UINT32((uint)B[0]|((uint)B[1]<<8)|((uint)B[2]<<16)|((uint)B[3]<<24));
  43. }
  44. else
  45. return UINT32(*Addr);
  46. #else
  47. return UINT32(*Addr);
  48. #endif
  49. }
  50. }
  51. #if defined(BIG_ENDIAN) || !defined(ALLOW_NOT_ALIGNED_INT)
  52. #define GET_VALUE(ByteMode,Addr) GetValue(ByteMode,(uint *)Addr)
  53. #else
  54. #define GET_VALUE(ByteMode,Addr) ((ByteMode) ? (*(byte *)(Addr)):UINT32(*(uint *)(Addr)))
  55. #endif
  56. inline void RarVM::SetValue(bool ByteMode,uint *Addr,uint Value)
  57. {
  58. if (ByteMode)
  59. {
  60. #ifdef BIG_ENDIAN
  61. if (IS_VM_MEM(Addr))
  62. *(byte *)Addr=Value;
  63. else
  64. *Addr=(*Addr & ~0xff)|(Value & 0xff);
  65. #else
  66. *(byte *)Addr=Value;
  67. #endif
  68. }
  69. else
  70. {
  71. #if defined(BIG_ENDIAN) || !defined(ALLOW_NOT_ALIGNED_INT) || !defined(PRESENT_INT32)
  72. if (IS_VM_MEM(Addr))
  73. {
  74. ((byte *)Addr)[0]=(byte)Value;
  75. ((byte *)Addr)[1]=(byte)(Value>>8);
  76. ((byte *)Addr)[2]=(byte)(Value>>16);
  77. ((byte *)Addr)[3]=(byte)(Value>>24);
  78. }
  79. else
  80. *(uint *)Addr=Value;
  81. #else
  82. *(uint32 *)Addr=Value;
  83. #endif
  84. }
  85. }
  86. #if defined(BIG_ENDIAN) || !defined(ALLOW_NOT_ALIGNED_INT) || !defined(PRESENT_INT32)
  87. #define SET_VALUE(ByteMode,Addr,Value) SetValue(ByteMode,(uint *)Addr,Value)
  88. #else
  89. #define SET_VALUE(ByteMode,Addr,Value) ((ByteMode) ? (*(byte *)(Addr)=((byte)(Value))):(*(uint32 *)(Addr)=((uint32)(Value))))
  90. #endif
  91. void RarVM::SetLowEndianValue(uint *Addr,uint Value)
  92. {
  93. #if defined(BIG_ENDIAN) || !defined(ALLOW_NOT_ALIGNED_INT) || !defined(PRESENT_INT32)
  94. ((byte *)Addr)[0]=(byte)Value;
  95. ((byte *)Addr)[1]=(byte)(Value>>8);
  96. ((byte *)Addr)[2]=(byte)(Value>>16);
  97. ((byte *)Addr)[3]=(byte)(Value>>24);
  98. #else
  99. *(uint32 *)Addr=Value;
  100. #endif
  101. }
  102. inline uint* RarVM::GetOperand(VM_PreparedOperand *CmdOp)
  103. {
  104. if (CmdOp->Type==VM_OPREGMEM)
  105. return((uint *)&Mem[(*CmdOp->Addr+CmdOp->Base)&VM_MEMMASK]);
  106. else
  107. return(CmdOp->Addr);
  108. }
  109. void RarVM::Execute(VM_PreparedProgram *Prg)
  110. {
  111. memcpy(R,Prg->InitR,sizeof(Prg->InitR));
  112. size_t GlobalSize=Min(Prg->GlobalData.Size(),VM_GLOBALMEMSIZE);
  113. if (GlobalSize)
  114. memcpy(Mem+VM_GLOBALMEMADDR,&Prg->GlobalData[0],GlobalSize);
  115. size_t StaticSize=Min(Prg->StaticData.Size(),VM_GLOBALMEMSIZE-GlobalSize);
  116. if (StaticSize)
  117. memcpy(Mem+VM_GLOBALMEMADDR+GlobalSize,&Prg->StaticData[0],StaticSize);
  118. R[7]=VM_MEMSIZE;
  119. Flags=0;
  120. VM_PreparedCommand *PreparedCode=Prg->AltCmd ? Prg->AltCmd:&Prg->Cmd[0];
  121. if (Prg->CmdCount>0 && !ExecuteCode(PreparedCode,Prg->CmdCount))
  122. {
  123. // Invalid VM program. Let's replace it with 'return' command.
  124. PreparedCode[0].OpCode=VM_RET;
  125. }
  126. uint NewBlockPos=GET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x20])&VM_MEMMASK;
  127. uint NewBlockSize=GET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x1c])&VM_MEMMASK;
  128. if (NewBlockPos+NewBlockSize>=VM_MEMSIZE)
  129. NewBlockPos=NewBlockSize=0;
  130. Prg->FilteredData=Mem+NewBlockPos;
  131. Prg->FilteredDataSize=NewBlockSize;
  132. Prg->GlobalData.Reset();
  133. uint DataSize=Min(GET_VALUE(false,(uint*)&Mem[VM_GLOBALMEMADDR+0x30]),VM_GLOBALMEMSIZE-VM_FIXEDGLOBALSIZE);
  134. if (DataSize!=0)
  135. {
  136. Prg->GlobalData.Add(DataSize+VM_FIXEDGLOBALSIZE);
  137. memcpy(&Prg->GlobalData[0],&Mem[VM_GLOBALMEMADDR],DataSize+VM_FIXEDGLOBALSIZE);
  138. }
  139. }
  140. /*
  141. Note:
  142. Due to performance considerations RAR VM may set VM_FS, VM_FC, VM_FZ
  143. incorrectly for byte operands. These flags are always valid only
  144. for 32-bit operands. Check implementation of concrete VM command
  145. to see if it sets flags right.
  146. */
  147. #define SET_IP(IP) \
  148. if ((IP)>=CodeSize) \
  149. return(true); \
  150. if (--MaxOpCount<=0) \
  151. return(false); \
  152. Cmd=PreparedCode+(IP);
  153. bool RarVM::ExecuteCode(VM_PreparedCommand *PreparedCode,uint CodeSize)
  154. {
  155. int MaxOpCount=25000000;
  156. VM_PreparedCommand *Cmd=PreparedCode;
  157. while (1)
  158. {
  159. #ifndef NORARVM
  160. // Get addresses to quickly access operands.
  161. uint *Op1=GetOperand(&Cmd->Op1);
  162. uint *Op2=GetOperand(&Cmd->Op2);
  163. #endif
  164. switch(Cmd->OpCode)
  165. {
  166. #ifndef NORARVM
  167. case VM_MOV:
  168. SET_VALUE(Cmd->ByteMode,Op1,GET_VALUE(Cmd->ByteMode,Op2));
  169. break;
  170. #ifdef VM_OPTIMIZE
  171. case VM_MOVB:
  172. SET_VALUE(true,Op1,GET_VALUE(true,Op2));
  173. break;
  174. case VM_MOVD:
  175. SET_VALUE(false,Op1,GET_VALUE(false,Op2));
  176. break;
  177. #endif
  178. case VM_CMP:
  179. {
  180. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  181. uint Result=UINT32(Value1-GET_VALUE(Cmd->ByteMode,Op2));
  182. Flags=Result==0 ? VM_FZ:(Result>Value1)|(Result&VM_FS);
  183. }
  184. break;
  185. #ifdef VM_OPTIMIZE
  186. case VM_CMPB:
  187. {
  188. uint Value1=GET_VALUE(true,Op1);
  189. uint Result=UINT32(Value1-GET_VALUE(true,Op2));
  190. Flags=Result==0 ? VM_FZ:(Result>Value1)|(Result&VM_FS);
  191. }
  192. break;
  193. case VM_CMPD:
  194. {
  195. uint Value1=GET_VALUE(false,Op1);
  196. uint Result=UINT32(Value1-GET_VALUE(false,Op2));
  197. Flags=Result==0 ? VM_FZ:(Result>Value1)|(Result&VM_FS);
  198. }
  199. break;
  200. #endif
  201. case VM_ADD:
  202. {
  203. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  204. uint Result=UINT32(Value1+GET_VALUE(Cmd->ByteMode,Op2));
  205. if (Cmd->ByteMode)
  206. {
  207. Result&=0xff;
  208. Flags=(Result<Value1)|(Result==0 ? VM_FZ:((Result&0x80) ? VM_FS:0));
  209. }
  210. else
  211. Flags=(Result<Value1)|(Result==0 ? VM_FZ:(Result&VM_FS));
  212. SET_VALUE(Cmd->ByteMode,Op1,Result);
  213. }
  214. break;
  215. #ifdef VM_OPTIMIZE
  216. case VM_ADDB:
  217. SET_VALUE(true,Op1,GET_VALUE(true,Op1)+GET_VALUE(true,Op2));
  218. break;
  219. case VM_ADDD:
  220. SET_VALUE(false,Op1,GET_VALUE(false,Op1)+GET_VALUE(false,Op2));
  221. break;
  222. #endif
  223. case VM_SUB:
  224. {
  225. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  226. uint Result=UINT32(Value1-GET_VALUE(Cmd->ByteMode,Op2));
  227. Flags=Result==0 ? VM_FZ:(Result>Value1)|(Result&VM_FS);
  228. SET_VALUE(Cmd->ByteMode,Op1,Result);
  229. }
  230. break;
  231. #ifdef VM_OPTIMIZE
  232. case VM_SUBB:
  233. SET_VALUE(true,Op1,GET_VALUE(true,Op1)-GET_VALUE(true,Op2));
  234. break;
  235. case VM_SUBD:
  236. SET_VALUE(false,Op1,GET_VALUE(false,Op1)-GET_VALUE(false,Op2));
  237. break;
  238. #endif
  239. case VM_JZ:
  240. if ((Flags & VM_FZ)!=0)
  241. {
  242. SET_IP(GET_VALUE(false,Op1));
  243. continue;
  244. }
  245. break;
  246. case VM_JNZ:
  247. if ((Flags & VM_FZ)==0)
  248. {
  249. SET_IP(GET_VALUE(false,Op1));
  250. continue;
  251. }
  252. break;
  253. case VM_INC:
  254. {
  255. uint Result=UINT32(GET_VALUE(Cmd->ByteMode,Op1)+1);
  256. if (Cmd->ByteMode)
  257. Result&=0xff;
  258. SET_VALUE(Cmd->ByteMode,Op1,Result);
  259. Flags=Result==0 ? VM_FZ:Result&VM_FS;
  260. }
  261. break;
  262. #ifdef VM_OPTIMIZE
  263. case VM_INCB:
  264. SET_VALUE(true,Op1,GET_VALUE(true,Op1)+1);
  265. break;
  266. case VM_INCD:
  267. SET_VALUE(false,Op1,GET_VALUE(false,Op1)+1);
  268. break;
  269. #endif
  270. case VM_DEC:
  271. {
  272. uint Result=UINT32(GET_VALUE(Cmd->ByteMode,Op1)-1);
  273. SET_VALUE(Cmd->ByteMode,Op1,Result);
  274. Flags=Result==0 ? VM_FZ:Result&VM_FS;
  275. }
  276. break;
  277. #ifdef VM_OPTIMIZE
  278. case VM_DECB:
  279. SET_VALUE(true,Op1,GET_VALUE(true,Op1)-1);
  280. break;
  281. case VM_DECD:
  282. SET_VALUE(false,Op1,GET_VALUE(false,Op1)-1);
  283. break;
  284. #endif
  285. case VM_JMP:
  286. SET_IP(GET_VALUE(false,Op1));
  287. continue;
  288. case VM_XOR:
  289. {
  290. uint Result=UINT32(GET_VALUE(Cmd->ByteMode,Op1)^GET_VALUE(Cmd->ByteMode,Op2));
  291. Flags=Result==0 ? VM_FZ:Result&VM_FS;
  292. SET_VALUE(Cmd->ByteMode,Op1,Result);
  293. }
  294. break;
  295. case VM_AND:
  296. {
  297. uint Result=UINT32(GET_VALUE(Cmd->ByteMode,Op1)&GET_VALUE(Cmd->ByteMode,Op2));
  298. Flags=Result==0 ? VM_FZ:Result&VM_FS;
  299. SET_VALUE(Cmd->ByteMode,Op1,Result);
  300. }
  301. break;
  302. case VM_OR:
  303. {
  304. uint Result=UINT32(GET_VALUE(Cmd->ByteMode,Op1)|GET_VALUE(Cmd->ByteMode,Op2));
  305. Flags=Result==0 ? VM_FZ:Result&VM_FS;
  306. SET_VALUE(Cmd->ByteMode,Op1,Result);
  307. }
  308. break;
  309. case VM_TEST:
  310. {
  311. uint Result=UINT32(GET_VALUE(Cmd->ByteMode,Op1)&GET_VALUE(Cmd->ByteMode,Op2));
  312. Flags=Result==0 ? VM_FZ:Result&VM_FS;
  313. }
  314. break;
  315. case VM_JS:
  316. if ((Flags & VM_FS)!=0)
  317. {
  318. SET_IP(GET_VALUE(false,Op1));
  319. continue;
  320. }
  321. break;
  322. case VM_JNS:
  323. if ((Flags & VM_FS)==0)
  324. {
  325. SET_IP(GET_VALUE(false,Op1));
  326. continue;
  327. }
  328. break;
  329. case VM_JB:
  330. if ((Flags & VM_FC)!=0)
  331. {
  332. SET_IP(GET_VALUE(false,Op1));
  333. continue;
  334. }
  335. break;
  336. case VM_JBE:
  337. if ((Flags & (VM_FC|VM_FZ))!=0)
  338. {
  339. SET_IP(GET_VALUE(false,Op1));
  340. continue;
  341. }
  342. break;
  343. case VM_JA:
  344. if ((Flags & (VM_FC|VM_FZ))==0)
  345. {
  346. SET_IP(GET_VALUE(false,Op1));
  347. continue;
  348. }
  349. break;
  350. case VM_JAE:
  351. if ((Flags & VM_FC)==0)
  352. {
  353. SET_IP(GET_VALUE(false,Op1));
  354. continue;
  355. }
  356. break;
  357. case VM_PUSH:
  358. R[7]-=4;
  359. SET_VALUE(false,(uint *)&Mem[R[7]&VM_MEMMASK],GET_VALUE(false,Op1));
  360. break;
  361. case VM_POP:
  362. SET_VALUE(false,Op1,GET_VALUE(false,(uint *)&Mem[R[7] & VM_MEMMASK]));
  363. R[7]+=4;
  364. break;
  365. case VM_CALL:
  366. R[7]-=4;
  367. SET_VALUE(false,(uint *)&Mem[R[7]&VM_MEMMASK],Cmd-PreparedCode+1);
  368. SET_IP(GET_VALUE(false,Op1));
  369. continue;
  370. case VM_NOT:
  371. SET_VALUE(Cmd->ByteMode,Op1,~GET_VALUE(Cmd->ByteMode,Op1));
  372. break;
  373. case VM_SHL:
  374. {
  375. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  376. uint Value2=GET_VALUE(Cmd->ByteMode,Op2);
  377. uint Result=UINT32(Value1<<Value2);
  378. Flags=(Result==0 ? VM_FZ:(Result&VM_FS))|((Value1<<(Value2-1))&0x80000000 ? VM_FC:0);
  379. SET_VALUE(Cmd->ByteMode,Op1,Result);
  380. }
  381. break;
  382. case VM_SHR:
  383. {
  384. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  385. uint Value2=GET_VALUE(Cmd->ByteMode,Op2);
  386. uint Result=UINT32(Value1>>Value2);
  387. Flags=(Result==0 ? VM_FZ:(Result&VM_FS))|((Value1>>(Value2-1))&VM_FC);
  388. SET_VALUE(Cmd->ByteMode,Op1,Result);
  389. }
  390. break;
  391. case VM_SAR:
  392. {
  393. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  394. uint Value2=GET_VALUE(Cmd->ByteMode,Op2);
  395. uint Result=UINT32(((int)Value1)>>Value2);
  396. Flags=(Result==0 ? VM_FZ:(Result&VM_FS))|((Value1>>(Value2-1))&VM_FC);
  397. SET_VALUE(Cmd->ByteMode,Op1,Result);
  398. }
  399. break;
  400. case VM_NEG:
  401. {
  402. // We use "0-value" expression to suppress "unary minus to unsigned"
  403. // compiler warning.
  404. uint Result=UINT32(0-GET_VALUE(Cmd->ByteMode,Op1));
  405. Flags=Result==0 ? VM_FZ:VM_FC|(Result&VM_FS);
  406. SET_VALUE(Cmd->ByteMode,Op1,Result);
  407. }
  408. break;
  409. #ifdef VM_OPTIMIZE
  410. case VM_NEGB:
  411. SET_VALUE(true,Op1,0-GET_VALUE(true,Op1));
  412. break;
  413. case VM_NEGD:
  414. SET_VALUE(false,Op1,0-GET_VALUE(false,Op1));
  415. break;
  416. #endif
  417. case VM_PUSHA:
  418. {
  419. const int RegCount=sizeof(R)/sizeof(R[0]);
  420. for (int I=0,SP=R[7]-4;I<RegCount;I++,SP-=4)
  421. SET_VALUE(false,(uint *)&Mem[SP & VM_MEMMASK],R[I]);
  422. R[7]-=RegCount*4;
  423. }
  424. break;
  425. case VM_POPA:
  426. {
  427. const int RegCount=sizeof(R)/sizeof(R[0]);
  428. for (uint I=0,SP=R[7];I<RegCount;I++,SP+=4)
  429. R[7-I]=GET_VALUE(false,(uint *)&Mem[SP & VM_MEMMASK]);
  430. }
  431. break;
  432. case VM_PUSHF:
  433. R[7]-=4;
  434. SET_VALUE(false,(uint *)&Mem[R[7]&VM_MEMMASK],Flags);
  435. break;
  436. case VM_POPF:
  437. Flags=GET_VALUE(false,(uint *)&Mem[R[7] & VM_MEMMASK]);
  438. R[7]+=4;
  439. break;
  440. case VM_MOVZX:
  441. SET_VALUE(false,Op1,GET_VALUE(true,Op2));
  442. break;
  443. case VM_MOVSX:
  444. SET_VALUE(false,Op1,(signed char)GET_VALUE(true,Op2));
  445. break;
  446. case VM_XCHG:
  447. {
  448. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  449. SET_VALUE(Cmd->ByteMode,Op1,GET_VALUE(Cmd->ByteMode,Op2));
  450. SET_VALUE(Cmd->ByteMode,Op2,Value1);
  451. }
  452. break;
  453. case VM_MUL:
  454. {
  455. uint Result=GET_VALUE(Cmd->ByteMode,Op1)*GET_VALUE(Cmd->ByteMode,Op2);
  456. SET_VALUE(Cmd->ByteMode,Op1,Result);
  457. }
  458. break;
  459. case VM_DIV:
  460. {
  461. uint Divider=GET_VALUE(Cmd->ByteMode,Op2);
  462. if (Divider!=0)
  463. {
  464. uint Result=GET_VALUE(Cmd->ByteMode,Op1)/Divider;
  465. SET_VALUE(Cmd->ByteMode,Op1,Result);
  466. }
  467. }
  468. break;
  469. case VM_ADC:
  470. {
  471. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  472. uint FC=(Flags&VM_FC);
  473. uint Result=UINT32(Value1+GET_VALUE(Cmd->ByteMode,Op2)+FC);
  474. if (Cmd->ByteMode)
  475. Result&=0xff;
  476. Flags=(Result<Value1 || Result==Value1 && FC)|(Result==0 ? VM_FZ:(Result&VM_FS));
  477. SET_VALUE(Cmd->ByteMode,Op1,Result);
  478. }
  479. break;
  480. case VM_SBB:
  481. {
  482. uint Value1=GET_VALUE(Cmd->ByteMode,Op1);
  483. uint FC=(Flags&VM_FC);
  484. uint Result=UINT32(Value1-GET_VALUE(Cmd->ByteMode,Op2)-FC);
  485. if (Cmd->ByteMode)
  486. Result&=0xff;
  487. Flags=(Result>Value1 || Result==Value1 && FC)|(Result==0 ? VM_FZ:(Result&VM_FS));
  488. SET_VALUE(Cmd->ByteMode,Op1,Result);
  489. }
  490. break;
  491. #endif // for #ifndef NORARVM
  492. case VM_RET:
  493. if (R[7]>=VM_MEMSIZE)
  494. return(true);
  495. SET_IP(GET_VALUE(false,(uint *)&Mem[R[7] & VM_MEMMASK]));
  496. R[7]+=4;
  497. continue;
  498. #ifdef VM_STANDARDFILTERS
  499. case VM_STANDARD:
  500. ExecuteStandardFilter((VM_StandardFilters)Cmd->Op1.Data);
  501. break;
  502. #endif
  503. case VM_PRINT:
  504. break;
  505. }
  506. Cmd++;
  507. --MaxOpCount;
  508. }
  509. }
  510. void RarVM::Prepare(byte *Code,uint CodeSize,VM_PreparedProgram *Prg)
  511. {
  512. InitBitInput();
  513. memcpy(InBuf,Code,Min(CodeSize,BitInput::MAX_SIZE));
  514. // Calculate the single byte XOR checksum to check validity of VM code.
  515. byte XorSum=0;
  516. for (uint I=1;I<CodeSize;I++)
  517. XorSum^=Code[I];
  518. faddbits(8);
  519. Prg->CmdCount=0;
  520. if (XorSum==Code[0]) // VM code is valid if equal.
  521. {
  522. #ifdef VM_STANDARDFILTERS
  523. VM_StandardFilters FilterType=IsStandardFilter(Code,CodeSize);
  524. if (FilterType!=VMSF_NONE)
  525. {
  526. // VM code is found among standard filters.
  527. Prg->Cmd.Add(1);
  528. VM_PreparedCommand *CurCmd=&Prg->Cmd[Prg->CmdCount++];
  529. CurCmd->OpCode=VM_STANDARD;
  530. CurCmd->Op1.Data=FilterType;
  531. CurCmd->Op1.Addr=&CurCmd->Op1.Data;
  532. CurCmd->Op2.Addr=&CurCmd->Op2.Data;
  533. CurCmd->Op1.Type=CurCmd->Op2.Type=VM_OPNONE;
  534. CodeSize=0;
  535. }
  536. #endif
  537. uint DataFlag=fgetbits();
  538. faddbits(1);
  539. // Read static data contained in DB operators. This data cannot be
  540. // changed, it is a part of VM code, not a filter parameter.
  541. if (DataFlag&0x8000)
  542. {
  543. uint DataSize=ReadData(*this)+1;
  544. for (uint I=0;(uint)InAddr<CodeSize && I<DataSize;I++)
  545. {
  546. Prg->StaticData.Add(1);
  547. Prg->StaticData[I]=fgetbits()>>8;
  548. faddbits(8);
  549. }
  550. }
  551. while ((uint)InAddr<CodeSize)
  552. {
  553. Prg->Cmd.Add(1);
  554. VM_PreparedCommand *CurCmd=&Prg->Cmd[Prg->CmdCount];
  555. uint Data=fgetbits();
  556. if ((Data&0x8000)==0)
  557. {
  558. CurCmd->OpCode=(VM_Commands)(Data>>12);
  559. faddbits(4);
  560. }
  561. else
  562. {
  563. CurCmd->OpCode=(VM_Commands)((Data>>10)-24);
  564. faddbits(6);
  565. }
  566. if (VM_CmdFlags[CurCmd->OpCode] & VMCF_BYTEMODE)
  567. {
  568. CurCmd->ByteMode=(fgetbits()>>15)!=0;
  569. faddbits(1);
  570. }
  571. else
  572. CurCmd->ByteMode=0;
  573. CurCmd->Op1.Type=CurCmd->Op2.Type=VM_OPNONE;
  574. int OpNum=(VM_CmdFlags[CurCmd->OpCode] & VMCF_OPMASK);
  575. CurCmd->Op1.Addr=CurCmd->Op2.Addr=NULL;
  576. if (OpNum>0)
  577. {
  578. DecodeArg(CurCmd->Op1,CurCmd->ByteMode); // reading the first operand
  579. if (OpNum==2)
  580. DecodeArg(CurCmd->Op2,CurCmd->ByteMode); // reading the second operand
  581. else
  582. {
  583. if (CurCmd->Op1.Type==VM_OPINT && (VM_CmdFlags[CurCmd->OpCode]&(VMCF_JUMP|VMCF_PROC)))
  584. {
  585. // Calculating jump distance.
  586. int Distance=CurCmd->Op1.Data;
  587. if (Distance>=256)
  588. Distance-=256;
  589. else
  590. {
  591. if (Distance>=136)
  592. Distance-=264;
  593. else
  594. if (Distance>=16)
  595. Distance-=8;
  596. else
  597. if (Distance>=8)
  598. Distance-=16;
  599. Distance+=Prg->CmdCount;
  600. }
  601. CurCmd->Op1.Data=Distance;
  602. }
  603. }
  604. }
  605. Prg->CmdCount++;
  606. }
  607. }
  608. // Adding RET command at the end of program.
  609. Prg->Cmd.Add(1);
  610. VM_PreparedCommand *CurCmd=&Prg->Cmd[Prg->CmdCount++];
  611. CurCmd->OpCode=VM_RET;
  612. CurCmd->Op1.Addr=&CurCmd->Op1.Data;
  613. CurCmd->Op2.Addr=&CurCmd->Op2.Data;
  614. CurCmd->Op1.Type=CurCmd->Op2.Type=VM_OPNONE;
  615. // If operand 'Addr' field has not been set by DecodeArg calls above,
  616. // let's set it to point to operand 'Data' field. It is necessary for
  617. // VM_OPINT type operands (usual integers) or maybe if something was
  618. // not set properly for other operands. 'Addr' field is required
  619. // for quicker addressing of operand data.
  620. for (int I=0;I<Prg->CmdCount;I++)
  621. {
  622. VM_PreparedCommand *Cmd=&Prg->Cmd[I];
  623. if (Cmd->Op1.Addr==NULL)
  624. Cmd->Op1.Addr=&Cmd->Op1.Data;
  625. if (Cmd->Op2.Addr==NULL)
  626. Cmd->Op2.Addr=&Cmd->Op2.Data;
  627. }
  628. #ifdef VM_OPTIMIZE
  629. if (CodeSize!=0)
  630. Optimize(Prg);
  631. #endif
  632. }
  633. void RarVM::DecodeArg(VM_PreparedOperand &Op,bool ByteMode)
  634. {
  635. uint Data=fgetbits();
  636. if (Data & 0x8000)
  637. {
  638. Op.Type=VM_OPREG; // Operand is register (R[0]..R[7])
  639. Op.Data=(Data>>12)&7; // Register number
  640. Op.Addr=&R[Op.Data]; // Register address
  641. faddbits(4); // 1 flag bit and 3 register number bits
  642. }
  643. else
  644. if ((Data & 0xc000)==0)
  645. {
  646. Op.Type=VM_OPINT; // Operand is integer
  647. if (ByteMode)
  648. {
  649. Op.Data=(Data>>6) & 0xff; // Byte integer.
  650. faddbits(10);
  651. }
  652. else
  653. {
  654. faddbits(2);
  655. Op.Data=ReadData(*this); // 32 bit integer.
  656. }
  657. }
  658. else
  659. {
  660. // Operand is data addressed by register data, base address or both.
  661. Op.Type=VM_OPREGMEM;
  662. if ((Data & 0x2000)==0)
  663. {
  664. // Base address is zero, just use the address from register.
  665. Op.Data=(Data>>10)&7;
  666. Op.Addr=&R[Op.Data];
  667. Op.Base=0;
  668. faddbits(6);
  669. }
  670. else
  671. {
  672. if ((Data & 0x1000)==0)
  673. {
  674. // Use both register and base address.
  675. Op.Data=(Data>>9)&7;
  676. Op.Addr=&R[Op.Data];
  677. faddbits(7);
  678. }
  679. else
  680. {
  681. // Use base address only. Access memory by fixed address.
  682. Op.Data=0;
  683. faddbits(4);
  684. }
  685. Op.Base=ReadData(*this); // Read base address.
  686. }
  687. }
  688. }
  689. uint RarVM::ReadData(BitInput &Inp)
  690. {
  691. uint Data=Inp.fgetbits();
  692. switch(Data&0xc000)
  693. {
  694. case 0:
  695. Inp.faddbits(6);
  696. return((Data>>10)&0xf);
  697. case 0x4000:
  698. if ((Data&0x3c00)==0)
  699. {
  700. Data=0xffffff00|((Data>>2)&0xff);
  701. Inp.faddbits(14);
  702. }
  703. else
  704. {
  705. Data=(Data>>6)&0xff;
  706. Inp.faddbits(10);
  707. }
  708. return(Data);
  709. case 0x8000:
  710. Inp.faddbits(2);
  711. Data=Inp.fgetbits();
  712. Inp.faddbits(16);
  713. return(Data);
  714. default:
  715. Inp.faddbits(2);
  716. Data=(Inp.fgetbits()<<16);
  717. Inp.faddbits(16);
  718. Data|=Inp.fgetbits();
  719. Inp.faddbits(16);
  720. return(Data);
  721. }
  722. }
  723. void RarVM::SetMemory(uint Pos,byte *Data,uint DataSize)
  724. {
  725. if (Pos<VM_MEMSIZE && Data!=Mem+Pos)
  726. memmove(Mem+Pos,Data,Min(DataSize,VM_MEMSIZE-Pos));
  727. }
  728. #ifdef VM_OPTIMIZE
  729. void RarVM::Optimize(VM_PreparedProgram *Prg)
  730. {
  731. VM_PreparedCommand *Code=&Prg->Cmd[0];
  732. uint CodeSize=Prg->CmdCount;
  733. for (uint I=0;I<CodeSize;I++)
  734. {
  735. VM_PreparedCommand *Cmd=Code+I;
  736. // Replace universal opcodes with faster byte or word only processing
  737. // opcodes.
  738. switch(Cmd->OpCode)
  739. {
  740. case VM_MOV:
  741. Cmd->OpCode=Cmd->ByteMode ? VM_MOVB:VM_MOVD;
  742. continue;
  743. case VM_CMP:
  744. Cmd->OpCode=Cmd->ByteMode ? VM_CMPB:VM_CMPD;
  745. continue;
  746. }
  747. if ((VM_CmdFlags[Cmd->OpCode] & VMCF_CHFLAGS)==0)
  748. continue;
  749. // If we do not have jump commands between the current operation
  750. // and next command which will modify processor flags, we can replace
  751. // the current command with faster version which does not need to
  752. // modify flags.
  753. bool FlagsRequired=false;
  754. for (uint J=I+1;J<CodeSize;J++)
  755. {
  756. int Flags=VM_CmdFlags[Code[J].OpCode];
  757. if (Flags & (VMCF_JUMP|VMCF_PROC|VMCF_USEFLAGS))
  758. {
  759. FlagsRequired=true;
  760. break;
  761. }
  762. if (Flags & VMCF_CHFLAGS)
  763. break;
  764. }
  765. // Below we'll replace universal opcodes with faster byte or word only
  766. // processing opcodes, which also do not modify processor flags to
  767. // provide better performance.
  768. if (FlagsRequired)
  769. continue;
  770. switch(Cmd->OpCode)
  771. {
  772. case VM_ADD:
  773. Cmd->OpCode=Cmd->ByteMode ? VM_ADDB:VM_ADDD;
  774. continue;
  775. case VM_SUB:
  776. Cmd->OpCode=Cmd->ByteMode ? VM_SUBB:VM_SUBD;
  777. continue;
  778. case VM_INC:
  779. Cmd->OpCode=Cmd->ByteMode ? VM_INCB:VM_INCD;
  780. continue;
  781. case VM_DEC:
  782. Cmd->OpCode=Cmd->ByteMode ? VM_DECB:VM_DECD;
  783. continue;
  784. case VM_NEG:
  785. Cmd->OpCode=Cmd->ByteMode ? VM_NEGB:VM_NEGD;
  786. continue;
  787. }
  788. }
  789. }
  790. #endif
  791. #ifdef VM_STANDARDFILTERS
  792. VM_StandardFilters RarVM::IsStandardFilter(byte *Code,uint CodeSize)
  793. {
  794. struct StandardFilterSignature
  795. {
  796. int Length;
  797. uint CRC;
  798. VM_StandardFilters Type;
  799. } static StdList[]={
  800. 53, 0xad576887, VMSF_E8,
  801. 57, 0x3cd7e57e, VMSF_E8E9,
  802. 120, 0x3769893f, VMSF_ITANIUM,
  803. 29, 0x0e06077d, VMSF_DELTA,
  804. 149, 0x1c2c5dc8, VMSF_RGB,
  805. 216, 0xbc85e701, VMSF_AUDIO,
  806. 40, 0x46b9c560, VMSF_UPCASE
  807. };
  808. uint CodeCRC=CRC(0xffffffff,Code,CodeSize)^0xffffffff;
  809. for (uint I=0;I<ASIZE(StdList);I++)
  810. if (StdList[I].CRC==CodeCRC && StdList[I].Length==CodeSize)
  811. return(StdList[I].Type);
  812. return(VMSF_NONE);
  813. }
  814. void RarVM::ExecuteStandardFilter(VM_StandardFilters FilterType)
  815. {
  816. switch(FilterType)
  817. {
  818. case VMSF_E8:
  819. case VMSF_E8E9:
  820. {
  821. byte *Data=Mem;
  822. int DataSize=R[4];
  823. uint FileOffset=R[6];
  824. if ((uint)DataSize>=VM_GLOBALMEMADDR || DataSize<4)
  825. break;
  826. const int FileSize=0x1000000;
  827. byte CmpByte2=FilterType==VMSF_E8E9 ? 0xe9:0xe8;
  828. for (int CurPos=0;CurPos<DataSize-4;)
  829. {
  830. byte CurByte=*(Data++);
  831. CurPos++;
  832. if (CurByte==0xe8 || CurByte==CmpByte2)
  833. {
  834. #ifdef PRESENT_INT32
  835. int32 Offset=CurPos+FileOffset;
  836. int32 Addr=GET_VALUE(false,Data);
  837. if (Addr<0)
  838. {
  839. if (Addr+Offset>=0)
  840. SET_VALUE(false,Data,Addr+FileSize);
  841. }
  842. else
  843. if (Addr<FileSize)
  844. SET_VALUE(false,Data,Addr-Offset);
  845. #else
  846. long Offset=CurPos+FileOffset;
  847. long Addr=GET_VALUE(false,Data);
  848. if ((Addr & 0x80000000)!=0)
  849. {
  850. if (((Addr+Offset) & 0x80000000)==0)
  851. SET_VALUE(false,Data,Addr+FileSize);
  852. }
  853. else
  854. if (((Addr-FileSize) & 0x80000000)!=0)
  855. SET_VALUE(false,Data,Addr-Offset);
  856. #endif
  857. Data+=4;
  858. CurPos+=4;
  859. }
  860. }
  861. }
  862. break;
  863. case VMSF_ITANIUM:
  864. {
  865. byte *Data=Mem;
  866. int DataSize=R[4];
  867. uint FileOffset=R[6];
  868. if ((uint)DataSize>=VM_GLOBALMEMADDR || DataSize<21)
  869. break;
  870. int CurPos=0;
  871. FileOffset>>=4;
  872. while (CurPos<DataSize-21)
  873. {
  874. int Byte=(Data[0]&0x1f)-0x10;
  875. if (Byte>=0)
  876. {
  877. static byte Masks[16]={4,4,6,6,0,0,7,7,4,4,0,0,4,4,0,0};
  878. byte CmdMask=Masks[Byte];
  879. if (CmdMask!=0)
  880. for (int I=0;I<=2;I++)
  881. if (CmdMask & (1<<I))
  882. {
  883. int StartPos=I*41+5;
  884. int OpType=FilterItanium_GetBits(Data,StartPos+37,4);
  885. if (OpType==5)
  886. {
  887. int Offset=FilterItanium_GetBits(Data,StartPos+13,20);
  888. FilterItanium_SetBits(Data,(Offset-FileOffset)&0xfffff,StartPos+13,20);
  889. }
  890. }
  891. }
  892. Data+=16;
  893. CurPos+=16;
  894. FileOffset++;
  895. }
  896. }
  897. break;
  898. case VMSF_DELTA:
  899. {
  900. int DataSize=R[4],Channels=R[0],SrcPos=0,Border=DataSize*2;
  901. SET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x20],DataSize);
  902. if ((uint)DataSize>=VM_GLOBALMEMADDR/2)
  903. break;
  904. // Bytes from same channels are grouped to continual data blocks,
  905. // so we need to place them back to their interleaving positions.
  906. for (int CurChannel=0;CurChannel<Channels;CurChannel++)
  907. {
  908. byte PrevByte=0;
  909. for (int DestPos=DataSize+CurChannel;DestPos<Border;DestPos+=Channels)
  910. Mem[DestPos]=(PrevByte-=Mem[SrcPos++]);
  911. }
  912. }
  913. break;
  914. case VMSF_RGB:
  915. {
  916. int DataSize=R[4],Width=R[0]-3,PosR=R[1];
  917. byte *SrcData=Mem,*DestData=SrcData+DataSize;
  918. const int Channels=3;
  919. SET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x20],DataSize);
  920. if ((uint)DataSize>=VM_GLOBALMEMADDR/2 || PosR<0)
  921. break;
  922. for (int CurChannel=0;CurChannel<Channels;CurChannel++)
  923. {
  924. uint PrevByte=0;
  925. for (int I=CurChannel;I<DataSize;I+=Channels)
  926. {
  927. uint Predicted;
  928. int UpperPos=I-Width;
  929. if (UpperPos>=3)
  930. {
  931. byte *UpperData=DestData+UpperPos;
  932. uint UpperByte=*UpperData;
  933. uint UpperLeftByte=*(UpperData-3);
  934. Predicted=PrevByte+UpperByte-UpperLeftByte;
  935. int pa=abs((int)(Predicted-PrevByte));
  936. int pb=abs((int)(Predicted-UpperByte));
  937. int pc=abs((int)(Predicted-UpperLeftByte));
  938. if (pa<=pb && pa<=pc)
  939. Predicted=PrevByte;
  940. else
  941. if (pb<=pc)
  942. Predicted=UpperByte;
  943. else
  944. Predicted=UpperLeftByte;
  945. }
  946. else
  947. Predicted=PrevByte;
  948. DestData[I]=PrevByte=(byte)(Predicted-*(SrcData++));
  949. }
  950. }
  951. for (int I=PosR,Border=DataSize-2;I<Border;I+=3)
  952. {
  953. byte G=DestData[I+1];
  954. DestData[I]+=G;
  955. DestData[I+2]+=G;
  956. }
  957. }
  958. break;
  959. case VMSF_AUDIO:
  960. {
  961. int DataSize=R[4],Channels=R[0];
  962. byte *SrcData=Mem,*DestData=SrcData+DataSize;
  963. SET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x20],DataSize);
  964. if ((uint)DataSize>=VM_GLOBALMEMADDR/2)
  965. break;
  966. for (int CurChannel=0;CurChannel<Channels;CurChannel++)
  967. {
  968. uint PrevByte=0,PrevDelta=0,Dif[7];
  969. int D1=0,D2=0,D3;
  970. int K1=0,K2=0,K3=0;
  971. memset(Dif,0,sizeof(Dif));
  972. for (int I=CurChannel,ByteCount=0;I<DataSize;I+=Channels,ByteCount++)
  973. {
  974. D3=D2;
  975. D2=PrevDelta-D1;
  976. D1=PrevDelta;
  977. uint Predicted=8*PrevByte+K1*D1+K2*D2+K3*D3;
  978. Predicted=(Predicted>>3) & 0xff;
  979. uint CurByte=*(SrcData++);
  980. Predicted-=CurByte;
  981. DestData[I]=Predicted;
  982. PrevDelta=(signed char)(Predicted-PrevByte);
  983. PrevByte=Predicted;
  984. int D=((signed char)CurByte)<<3;
  985. Dif[0]+=abs(D);
  986. Dif[1]+=abs(D-D1);
  987. Dif[2]+=abs(D+D1);
  988. Dif[3]+=abs(D-D2);
  989. Dif[4]+=abs(D+D2);
  990. Dif[5]+=abs(D-D3);
  991. Dif[6]+=abs(D+D3);
  992. if ((ByteCount & 0x1f)==0)
  993. {
  994. uint MinDif=Dif[0],NumMinDif=0;
  995. Dif[0]=0;
  996. for (int J=1;J<sizeof(Dif)/sizeof(Dif[0]);J++)
  997. {
  998. if (Dif[J]<MinDif)
  999. {
  1000. MinDif=Dif[J];
  1001. NumMinDif=J;
  1002. }
  1003. Dif[J]=0;
  1004. }
  1005. switch(NumMinDif)
  1006. {
  1007. case 1: if (K1>=-16) K1--; break;
  1008. case 2: if (K1 < 16) K1++; break;
  1009. case 3: if (K2>=-16) K2--; break;
  1010. case 4: if (K2 < 16) K2++; break;
  1011. case 5: if (K3>=-16) K3--; break;
  1012. case 6: if (K3 < 16) K3++; break;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. break;
  1019. case VMSF_UPCASE:
  1020. {
  1021. int DataSize=R[4],SrcPos=0,DestPos=DataSize;
  1022. if ((uint)DataSize>=VM_GLOBALMEMADDR/2)
  1023. break;
  1024. while (SrcPos<DataSize)
  1025. {
  1026. byte CurByte=Mem[SrcPos++];
  1027. if (CurByte==2 && (CurByte=Mem[SrcPos++])!=2)
  1028. CurByte-=32;
  1029. Mem[DestPos++]=CurByte;
  1030. }
  1031. SET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x1c],DestPos-DataSize);
  1032. SET_VALUE(false,&Mem[VM_GLOBALMEMADDR+0x20],DataSize);
  1033. }
  1034. break;
  1035. }
  1036. }
  1037. uint RarVM::FilterItanium_GetBits(byte *Data,int BitPos,int BitCount)
  1038. {
  1039. int InAddr=BitPos/8;
  1040. int InBit=BitPos&7;
  1041. uint BitField=(uint)Data[InAddr++];
  1042. BitField|=(uint)Data[InAddr++] << 8;
  1043. BitField|=(uint)Data[InAddr++] << 16;
  1044. BitField|=(uint)Data[InAddr] << 24;
  1045. BitField >>= InBit;
  1046. return(BitField & (0xffffffff>>(32-BitCount)));
  1047. }
  1048. void RarVM::FilterItanium_SetBits(byte *Data,uint BitField,int BitPos,int BitCount)
  1049. {
  1050. int InAddr=BitPos/8;
  1051. int InBit=BitPos&7;
  1052. uint AndMask=0xffffffff>>(32-BitCount);
  1053. AndMask=~(AndMask<<InBit);
  1054. BitField<<=InBit;
  1055. for (uint I=0;I<4;I++)
  1056. {
  1057. Data[InAddr+I]&=AndMask;
  1058. Data[InAddr+I]|=BitField;
  1059. AndMask=(AndMask>>8)|0xff000000;
  1060. BitField>>=8;
  1061. }
  1062. }
  1063. #endif