60 return (uint32_t)((br->
bits >> (br->
available -= bits)) & (((uint64_t)1 << bits) - 1));
78 return 0xFFFFFF00 | (val << 4) |
br_bits(br, 4);
88 dst[0] = value & 0xFF;
89 dst[1] = (value >> 8) & 0xFF;
90 dst[2] = (value >> 16) & 0xFF;
91 dst[3] = (value >> 24) & 0xFF;
137 else if (*value >= 16)
139 else if (*value >= 8)
141 *value += instrcount;
152 uint32_t instrcount = 0;
157 for (i = 1; i < length; i++)
159 if (!length || xor != bytes[0])
166 prog = calloc(1,
sizeof(*prog));
189 uint8_t instruction = (uint8_t)
br_bits(&br, 4);
190 bool bytemode =
false;
192 uint8_t addrmode1 = 0, addrmode2 = 0;
193 uint32_t value1 = 0, value2 = 0;
195 if ((instruction & 0x08))
196 instruction = ((instruction << 2) | (uint8_t)
br_bits(&br, 2)) - 24;
198 bytemode =
br_bits(&br, 1) != 0;
201 if (ok && numargs >= 1)
202 ok =
rar_parse_operand(&br, instruction, bytemode, instrcount, &addrmode1, &value1);
203 if (ok && numargs == 2)
204 ok =
rar_parse_operand(&br, instruction, bytemode, (uint32_t)-1, &addrmode2, &value2);
208 warn(
"Invalid RAR program instruction");
227 uint32_t newgloballength;
241 warn(
"Error while executing program in RAR VM");
248 if (newgloballength > 0) {
251 uint8_t *newglobaldata = malloc(newglobaldatalength);
270 filter = calloc(1,
sizeof(*filter));
309 dst = &vm->
memory[length];
310 for (i = 0; i < numchannels; i++) {
311 uint8_t lastbyte = 0;
312 for (idx = i; idx < length; idx += numchannels)
313 lastbyte = dst[idx] = lastbyte - *src++;
325 uint32_t filesize = 0x1000000;
331 for (i = 0; i <= length - 5; i++) {
332 if (vm->
memory[i] == 0xE8 || (e9also && vm->
memory[i] == 0xE9)) {
333 uint32_t currpos = (uint32_t)pos + i + 1;
335 if (address < 0 && currpos >= (uint32_t)-address)
337 else if (address >= 0 && (uint32_t)address < filesize)
362 for (i = 0; i < 3; i++) {
364 uint8_t *prev = dst + i - stride;
367 uint32_t delta1 = abs(prev[3] - prev[0]);
368 uint32_t delta2 = abs(byte - prev[0]);
369 uint32_t delta3 = abs(prev[3] - prev[0] + byte - prev[0]);
370 if (delta1 > delta2 || delta1 > delta3)
371 byte = delta2 <= delta3 ? prev[3] : prev[0];
378 for (i = byteoffset; i < blocklength - 2; i += 3) {
379 dst[i] += dst[i + 1];
380 dst[i + 2] += dst[i + 1];
400 dst = &vm->
memory[length];
401 for (i = 0; i < numchannels; i++) {
403 memset(&state, 0,
sizeof(state));
404 for (j = i; j < length; j += numchannels) {
405 int8_t
delta = (int8_t)*src++;
406 uint8_t predbyte, byte;
412 byte = (predbyte -
delta) & 0xFF;
413 prederror = delta << 3;
414 state.
error[0] += abs(prederror);
415 state.
error[1] += abs(prederror - state.
delta[0]); state.
error[2] += abs(prederror + state.
delta[0]);
416 state.
error[3] += abs(prederror - state.
delta[1]); state.
error[4] += abs(prederror + state.
delta[1]);
417 state.
error[5] += abs(prederror - state.
delta[2]); state.
error[6] += abs(prederror + state.
delta[2]);
420 if (!(state.
count++ & 0x1F)) {
422 for (k = 1; k < 7; k++) {
428 case 1:
if (state.
weight[0] >= -16) state.
weight[0]--;
break;
429 case 2:
if (state.
weight[0] < 16) state.
weight[0]++;
break;
430 case 3:
if (state.
weight[1] >= -16) state.
weight[1]--;
break;
431 case 4:
if (state.
weight[1] < 16) state.
weight[1]++;
break;
432 case 5:
if (state.
weight[2] >= -16) state.
weight[2]--;
break;
433 case 6:
if (state.
weight[2] < 16) state.
weight[2]++;
break;
486 if (newglobalbackup) {
511 uint32_t registers[8] = { 0 };
518 for (prog = filters->
progs; prog; prog = prog->
next)
521 if ((flags & 0x80)) {
525 filters->
stack = NULL;
527 filters->
progs = NULL;
531 if (num > numprogs) {
532 warn(
"Invalid program number");
540 prog = filters->
progs;
541 for (i = 0; i < num; i++)
548 blockstartpos += 258;
559 if ((flags & 0x10)) {
560 uint8_t mask = (uint8_t)
br_bits(&br, 7);
561 for (i = 0; i < 7; i++) {
562 if ((mask & (1 << i)))
572 if (len == 0 || len > 0x10000) {
573 warn(
"Invalid RARVM bytecode length");
576 bytecode = malloc(len);
579 for (i = 0; i < len; i++)
580 bytecode[i] = (uint8_t)
br_bits(&br, 8);
587 next = &filters->
progs;
589 next = &(*next)->
next;
596 if ((flags & 0x08)) {
599 warn(
"Invalid RARVM data length");
605 for (i = 0; i < globaldatalen; i++)
614 filter =
rar_create_filter(prog, globaldata, globaldatalen, registers, blockstartpos, blocklength);
619 for (i = 0; i < 7; i++)
625 nextfilter = &filters->
stack;
627 nextfilter = &(*nextfilter)->
next;
628 *nextfilter = filter;
642 uint32_t lastfilteraddress;
643 uint32_t lastfilterlength;
648 warn(
"Failed to expand the expected amout of bytes");
653 filters->
vm = calloc(1,
sizeof(*filters->
vm));
660 warn(
"Failed to execute parsing filter");
673 warn(
"Failed to execute parsing filter");
684 if (filters->
stack) {
686 warn(
"Bad filter order");