Compare commits
3 Commits
e518029485
...
6f9e7db4ee
Author | SHA1 | Date | |
---|---|---|---|
|
6f9e7db4ee | ||
|
19b6c69237 | ||
|
7b972fc406 |
@ -102,7 +102,6 @@ class VMAssembler:
|
||||
symcall = symcall_re.match(str(i))
|
||||
if symcall:
|
||||
symname = symcall.group(1)
|
||||
|
||||
# checking if it's a jump to a label
|
||||
if symname in [y.label for x in self.functions for y in x.instructions]:
|
||||
fun = next(
|
||||
@ -126,7 +125,7 @@ class VMAssembler:
|
||||
for f in self.functions:
|
||||
print("FUNCTION {}".format(f.name))
|
||||
for idx, ins in enumerate(f.instructions):
|
||||
print("{}:\t{}".format(hex(f.offset_of_instruction(idx)), ins))
|
||||
print("{}:\t{}".format(hex(f.offset+ f.offset_of_instruction(idx)), ins))
|
||||
|
||||
def imm2reg(self, instruction):
|
||||
"""
|
||||
@ -235,7 +234,6 @@ class VMAssembler:
|
||||
reg_op_re = re.compile(".*[rR]$")
|
||||
symcall = symcall_re.match(str(instruction))
|
||||
dst = instruction.args[0]
|
||||
print(instruction)
|
||||
# define the kind of jump: to immediate or to register
|
||||
if imm_op_re.match(instruction.opcode.name):
|
||||
self.immonly(instruction)
|
||||
@ -347,7 +345,6 @@ class VMInstruction:
|
||||
raise InvalidOperation(opcode)
|
||||
self.size = ops_sizes[self.opcode.method]
|
||||
args = [x for x in ins.groups()[1:] if x is not None]
|
||||
print("OP: {} | ARGS: {} | SYMCALL: {}".format(self.opcode, args,symcall))
|
||||
for a in args:
|
||||
if immediate_re.match(a) or symcall:
|
||||
# directly append the immediate
|
||||
@ -478,20 +475,20 @@ ops_sizes = {"reg2reg": 2,
|
||||
"imm2reg": 4,
|
||||
"reg2imm": 4,
|
||||
"byt2reg": 3,
|
||||
"regonly": 3,
|
||||
"regonly": 2,
|
||||
"immonly": 3,
|
||||
"jump": 3,
|
||||
"single": 1}
|
||||
ops = [VMComponent(le[0], i, le[1]) for i, le in enumerate(op_names)]
|
||||
regs = [VMComponent(s.casefold(), i) for i, s in enumerate(reg_names)]
|
||||
instruction_re = re.compile(
|
||||
"^([\w]{4})(?:\ +(?:([\w]+)\ *(?:,[\ ]*([\w]+))*))?$") # 1: opcode 2+: args
|
||||
function_re = re.compile("(?:def\ )([a-zA-Z]*)\:")
|
||||
"^([\w]{4})(?:(?:\ *\#.*)|(?:\ +(?:([\w]+)\ *(?:,[\ ]*([\w]+))?)(?:\ *\#.*)?))?$") # 1: opcode 2+: args
|
||||
function_re = re.compile("(?:def\ )([a-zA-Z]*)\:(?:\ *\#.*)?$")
|
||||
immediate_re = re.compile("(?:0x)?[0-9a-fA-F]+$")
|
||||
alpha_re = re.compile("^[a-zA-Z]*$")
|
||||
register_re = re.compile("(^[rRsS][0-4]$)|([iIrRsS][pP]$)")
|
||||
label_re = re.compile("^([a-zA-Z]+)\:$")
|
||||
symcall_re = re.compile("^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]*)$")
|
||||
label_re = re.compile("^([a-zA-Z]+)\:(?:\ *\#.*)?$")
|
||||
symcall_re = re.compile("^(?:[jJ][pPmM][pPaAbBeEnN][iIrR]|(?:[cC][aA][lL]{2}))\ +([\w]+)(?:\ *\#.*)?$")
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -4,16 +4,18 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void encrypt(uint16_t *v, uint16_t *k) {
|
||||
void encrypt(uint16_t *v) {
|
||||
uint16_t v0 = v[0], v1 = v[1], sum = 0, i; /* set up */
|
||||
//uint32_t delta = 0x9e3779b9; /* a key schedule constant */
|
||||
uint16_t delta= 0x9e37;
|
||||
uint16_t k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3]; /* cache key */
|
||||
for (i = 0; i < 64; i++) { /* basic cycle start */
|
||||
uint16_t delta= 0x626f;
|
||||
uint16_t k0 = 0x7065; // "pe"
|
||||
uint16_t k1 = 0x7065; // "pe"
|
||||
uint16_t k2 = 0x7275; // "ru"
|
||||
uint16_t k3 = 0x6e73; // "ns"
|
||||
for (i = 0; i < 128; i++) {
|
||||
sum += delta;
|
||||
v0 += ((v1 << 4) + k0) ^ (v1 + sum) ^ ((v1 >> 5) + k1);
|
||||
v1 += ((v0 << 4) + k2) ^ (v0 + sum) ^ ((v0 >> 5) + k3);
|
||||
} /* end cycle */
|
||||
}
|
||||
v[0] = v0;
|
||||
v[1] = v1;
|
||||
}
|
||||
@ -22,14 +24,14 @@ int main(int argc, char *argv[]) {
|
||||
uint8_t *buf;
|
||||
uint32_t buflen, i;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("Usage: %s text_to_encrypt key", argv[0]);
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s text_to_encrypt", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
buflen = strlen(argv[1]);
|
||||
buf = (uint8_t *)malloc(buflen);
|
||||
memcpy(buf, argv[1], buflen);
|
||||
encrypt((uint16_t *)buf, (uint16_t *)argv[2]);
|
||||
encrypt((uint16_t *)buf);
|
||||
printf("Result:\n");
|
||||
for (i = 0; i < buflen; i++) {
|
||||
printf("%02x", buf[i]);
|
||||
|
20
vm/vm.cpp
20
vm/vm.cpp
@ -577,9 +577,9 @@ bool VM::execCMPB(void) {
|
||||
flags.ZF = 0;
|
||||
}
|
||||
if (*((uint8_t *)®s[reg]) > imm) {
|
||||
flags.CF = 1;
|
||||
} else {
|
||||
flags.CF = 0;
|
||||
} else {
|
||||
flags.CF = 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -599,9 +599,9 @@ bool VM::execCMPW(void) {
|
||||
flags.ZF = 0;
|
||||
}
|
||||
if (regs[reg] > imm) {
|
||||
flags.CF = 1;
|
||||
} else {
|
||||
flags.CF = 0;
|
||||
} else {
|
||||
flags.CF = 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -621,9 +621,9 @@ bool VM::execCMPR(void) {
|
||||
flags.ZF = 0;
|
||||
}
|
||||
if (regs[r1] > regs[r2]) {
|
||||
flags.CF = 1;
|
||||
} else {
|
||||
flags.CF = 0;
|
||||
} else {
|
||||
flags.CF = 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -657,7 +657,7 @@ bool VM::execJPAI(void) {
|
||||
|
||||
imm = *(uint16_t *)&as.code[regs[IP] + 1];
|
||||
DBG_INFO(("JPAI 0x%x\n", imm));
|
||||
if (flags.CF == 1) {
|
||||
if (flags.CF == 0 && flags.ZF == 0) {
|
||||
regs[IP] = imm;
|
||||
return true;
|
||||
}
|
||||
@ -671,7 +671,7 @@ bool VM::execJPAR(void) {
|
||||
|
||||
reg = as.code[regs[IP] + 1];
|
||||
DBG_INFO(("JPAR %s = 0x%x\n", getRegName(reg), regs[reg]));
|
||||
if (flags.CF == 1) {
|
||||
if (flags.CF == 0 && flags.ZF == 0) {
|
||||
regs[IP] = reg;
|
||||
return true;
|
||||
}
|
||||
@ -685,7 +685,7 @@ bool VM::execJPBI(void) {
|
||||
|
||||
imm = *(uint16_t *)&as.code[regs[IP] + 1];
|
||||
DBG_INFO(("JPBI 0x%x\n", imm));
|
||||
if (flags.CF == 0) {
|
||||
if (flags.CF == 1) {
|
||||
regs[IP] = imm;
|
||||
return true;
|
||||
}
|
||||
@ -699,7 +699,7 @@ bool VM::execJPBR(void) {
|
||||
|
||||
reg = as.code[regs[IP] + 1];
|
||||
DBG_INFO(("JPBR %s = 0x%x\n", getRegName(reg), regs[reg]));
|
||||
if (flags.CF == 0) {
|
||||
if (flags.CF == 1) {
|
||||
regs[IP] = reg;
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user