public function testLe2be() { $this->assertEquals(0x41, Num::le2be(0x41)); $this->assertEquals(0x41, Num::le2be(0x4100)); $this->assertEquals(0x4142, Num::le2be(0x4241)); $this->assertEquals(0x414243, Num::le2be(0x434241)); }
public function assemble() { #print 'jmp assemble'."\n"; $dst = $this->dst; $isStrDst = is_string($dst); if ($dst instanceof Instruction) { #\Doctrine\Common\Util\Debug::dump($dst, 1); #\Doctrine\Common\Util\Debug::dump($this, 1); #print "\t".'dst offset: '.$dst->getOffset()."\n"; #print "\t".'this offset: '.$this->getOffset()."\n"; $offset = $dst->getOffset() - $this->getOffset(); #print "\t".'diff: '.$offset."\n"; $orgOffset = $offset; $offset -= 2; if ($orgOffset < 0) { if ($offset < -0x80) { $offset -= 3; } #print "\t".'<- '.$offset."\n"; } $jmp = new Jmp($offset); $this->setOpcode($jmp->assemble()); $this->setLen($jmp->getLen()); } elseif (is_numeric($dst)) { #print "\t".'is numeric: '.$dst.' '.dechex($dst)."\n"; $base = 0; #$absDst = abs($dst); if ($dst >= -0x80 && $dst <= 0x7f) { $base = 0xeb00; $dst &= 0xff; $dst |= $base; #print "\t".'short label: '.dechex($dst)."\n"; } elseif ($dst >= -0x80000000 && $dst <= 0x7fffffff) { $base = 0xe900000000; $dst &= 0xffffffff; $dst = Num::be2le($dst, 4); $dst |= $base; #print "\t".'long label: '.dechex($dst)."\n"; } if ($base) { $opcode = dechex($dst); $opcodeLen = strlen($opcode); $this->setOpcode(pack('H*', $opcode)); $this->setLen($opcodeLen / 2); } } elseif ($isStrDst) { $strLenDst = strlen($dst); if ($strLenDst == 2) { $jmp = new X86Jmp($dst); $this->setOpcode(pack('H*', '66') . $jmp->assemble()); $this->setLen($jmp->getLen() + 1); } elseif ($strLenDst == 3) { $base = 0xffe0; switch ($dst[1]) { /*case 'a': $base += 0; break;*/ case 'c': $base++; break; case 'd': $base += 2; break; case 'b': $base += 3; break; } $opcode = dechex($base); $opcodeLen = strlen($opcode); $this->setOpcode(pack('H*', $opcode)); $this->setLen($opcodeLen / 2); } } return $this->getOpcode(); }
public function __construct($src, $dst) { $this->src = strtolower($src); $this->dst = strtolower($dst); $lenSrc = strlen($this->src); $isNumSrc = is_numeric($this->src); $isStrSrc = is_string($this->src); $lenDst = strlen($this->dst); #$isNumDst = is_numeric($this->dst); $isStrDst = is_string($this->dst); if ($isNumSrc && $isStrDst && $lenDst == 2) { $mask = 0xff; $base = 0xb0; $len = 1; switch ($this->dst[1]) { /*case 'l': $mask = 0xff; $base += 0; break;*/ case 'h': #$mask = 0xff; $base += 4; break; case 'x': $mask = 0xffff; $base += 8; $len = 2; break; } switch ($this->dst[0]) { /*case 'a': $base += 0; break;*/ case 'c': $base++; break; case 'd': $base += 2; break; case 'b': $base += 3; break; } $this->src &= $mask; $this->src = Num::be2le($this->src, $len); $base <<= $len * 8; $opcode = dechex($base | $this->src); $opcodeLen = strlen($opcode); $this->setOpcode(pack('H*', $opcode)); $this->setLen($opcodeLen / 2); } elseif ($isStrSrc && $isStrDst && $lenSrc == 2 && $lenDst == 2) { if ($this->isValidRegisterSize()) { $base = 0x8800; switch ($this->src[1]) { case 'x': $base += 0x100; case 'l': $base += 0xc0; break; case 'h': $base += 0xe0; break; } switch ($this->src[0]) { /*case 'a': $base += 0; break;*/ case 'c': $base += 8; break; case 'd': $base += 0x10; break; case 'b': $base += 0x18; break; } switch ($this->dst[1]) { /*case 'l': $base += 0; break;*/ case 'h': $base += 4; break; } switch ($this->dst[0]) { /*case 'a': $base += 0; break;*/ case 'c': $base++; break; case 'd': $base += 2; break; case 'b': $base += 3; break; } $opcode = dechex($base); $opcodeLen = strlen($opcode); $this->setOpcode(pack('H*', $opcode)); $this->setLen($opcodeLen / 2); } } }
public function __construct($src, $dst) { $this->src = strtolower($src); $this->dst = strtolower($dst); $lenSrc = strlen($this->src); $isNumSrc = is_numeric($this->src); $isStrSrc = is_string($this->src); $lenDst = strlen($this->dst); $isNumDst = is_numeric($this->dst); $isStrDst = is_string($this->dst); if ($isNumSrc && $isStrDst && $lenDst == 2) { $pre = ''; $preLen = 0; switch ($this->dst) { case 'ax': case 'cx': case 'dx': case 'bx': $pre = pack('H*', '66'); $preLen++; break; } $instr = new X86Mov($this->src, $this->dst); $this->setOpcode($pre . $instr->assemble()); $this->setLen($instr->getLen() + $preLen); } elseif ($isNumSrc && $isStrDst && $lenDst == 3) { $mask = 0xffffffff; $base = 0xb8; $len = 4; switch ($this->dst[1]) { /*case 'a': $base += 0; break;*/ case 'c': $base++; break; case 'd': $base += 2; break; case 'b': $base += 3; break; } $this->src &= $mask; $this->src = Num::be2le($this->src, $len); $base <<= $len * 8; $opcode = dechex($base | $this->src); $opcodeLen = strlen($opcode); $this->setOpcode(pack('H*', $opcode)); $this->setLen($opcodeLen / 2); } elseif ($isStrSrc && $isStrDst && $lenSrc == 2 && $lenDst == 2) { $pre = ''; $preLen = 0; switch ($this->dst) { case 'ax': case 'cx': case 'dx': case 'bx': $pre = pack('H*', '66'); $preLen++; break; } $instr = new X86Mov($this->src, $this->dst); $this->setOpcode($pre . $instr->assemble()); $this->setLen($instr->getLen() + $preLen); } elseif ($isStrSrc && $isStrDst && $lenSrc == 3 && $lenDst == 3) { if ($this->isValidRegisterSize()) { #\Doctrine\Common\Util\Debug::dump($this->src); $tSrc = $this->src; $tDst = $this->dst; $tSrc = $tSrc[1] . $tSrc[2]; $tDst = $tDst[1] . $tDst[2]; $instr = new X86Mov($tSrc, $tDst); $this->setOpcode($instr->assemble()); $this->setLen($instr->getLen()); } } }
public function __construct($src, $dst) { $this->src = strtolower($src); $this->dst = strtolower($dst); $lenSrc = strlen($this->src); $isNumSrc = is_numeric($this->src); $isStrSrc = is_string($this->src); $lenDst = strlen($this->dst); $isStrDst = is_string($this->dst); if ($isNumSrc && $isStrDst && $lenDst == 2) { $instr = new I386Mov($this->src, $this->dst); $this->setOpcode($instr->assemble()); $this->setLen($instr->getLen()); } elseif ($isNumSrc && $isStrDst && $lenDst == 3) { $base = 0; #print "\n\nx64: ".dechex($this->src).", $dst\n"; switch ($dst[0]) { case 'e': #print "\t 32 bit\n"; $this->src &= 0xffffffff; $base = 0xb8; $this->src = Num::be2le($this->src, 4); $this->src = dechex($this->src); #$lenSrc = strlen($this->src); /*if($lenSrc < 8){ $this->src = str_repeat('0', 8 - $lenSrc).$this->src; }*/ $this->src = sprintf('%08s', $this->src); #print "\t src: ".$this->src."\n"; break; case 'r': $srcHigh = $this->src >> 32 & 0xffffffff; $srcLow = $this->src & 0xffffffff; #print "\t 64 bit h=".dechex($srcHigh)." l=".dechex($srcLow)."\n"; if ($this->src > 0x7fffffff | $srcHigh) { #print "\t 64 bit: 64\n"; $base = 0x48b8; $this->src = Num::be2le($this->src, 8); $this->src = dechex($this->src); /*$lenSrc = strlen($this->src); if($lenSrc < 16){ $this->src = str_repeat('0', 16 - $lenSrc).$this->src; }*/ $this->src = sprintf('%016s', $this->src); } else { #print "\t 64 bit: 32\n"; $base = 0x48c7c0; $this->src = $srcLow; #print "\t src: ".dechex($this->src)."\n"; $this->src = Num::be2le($this->src, 4); $this->src = dechex($this->src); /*$lenSrc = strlen($this->src); if($lenSrc < 8){ $this->src = str_repeat('0', 8 - $lenSrc).$this->src; }*/ $this->src = sprintf('%08s', $this->src); #print "\t 64 bit: ".$this->src."\n"; } break; } switch ($dst[1]) { /*case 'a': $base += 0; break;*/ case 'c': $base++; break; case 'd': $base += 2; break; case 'b': $base += 3; break; } $opcode = dechex($base) . $this->src; $opcodeLen = strlen($opcode); $this->setOpcode(pack('H*', $opcode)); $this->setLen($opcodeLen / 2); } elseif ($isStrSrc && $isStrDst && $lenSrc == 2 && $lenDst == 2) { $instr = new I386Mov($this->src, $this->dst); $this->setOpcode($instr->assemble()); $this->setLen($instr->getLen()); } elseif ($isStrSrc && $isStrDst && $lenSrc == 3 && $lenDst == 3) { if ($this->isValidRegisterSize($src, $dst)) { $pre = ''; $preLen = 0; switch ($this->src[0]) { /*case 'e': break;*/ case 'r': $pre = pack('H*', '48'); $preLen++; break; } $tSrc = $this->src; $tDst = $this->dst; $tSrc = $tSrc[1] . $tSrc[2]; $tDst = $tDst[1] . $tDst[2]; $instr = new X86Mov($tSrc, $tDst); $this->setOpcode($pre . $instr->assemble()); $this->setLen($instr->getLen() + $preLen); } } }