вторник, 3 сентября 2013 г.

crc32 binding for perl

I am tired calculating thousands crc32 hashes of exported functions so I made today binding crc32 for perl
Sample of using:
my $val = crc::my_crc32("GetProcAddress"); # 0xC97C1FFF

or even inside IDA Pro:
use IDA;
use crc;

sub form_ascii
{
  my $addr = shift;
  my $res = '';
  my $c;
  while( $c = Byte($addr) )
  {
    $res .= chr($c);
    $addr++;
  }
  return $res;
}

printf("%X\n", crc::my_crc32(form_ascii(ScreenEA())));

This binding uses swig
crc.asm
    .386
    .MODEL FLAT
    public _my_crc32
.code
CRC_32 proc
         push   ecx
         push   edx
         push   ebx
         xor    ecx, ecx
         dec    ecx
         mov    edx, ecx

NextByteCRC:
         xor    eax, eax
         xor    ebx, ebx
         lodsb
         test   al, al
         jz     short EndCRC32
         xor    al, cl
         mov    cl, ch
         mov    ch, dl
         mov    dl, dh
         mov    dh, 8

NextBitCRC:
         shr    bx, 1
         rcr    ax, 1
         jnc    short NoCRC
         xor    ax, 08320h
         xor    bx, 0EDB8h

NoCRC:
         dec    dh
         jnz    short NextBitCRC
         xor    ecx, eax
         xor    edx, ebx
         jmp    short NextByteCRC

EndCRC32:
         not    edx
         not    ecx
         pop    ebx
         mov    eax, edx
         rol    eax, 16
         mov    ax, cx
         pop    edx
         pop    ecx
         ret
CRC_32 endp

_my_crc32 proc
    push esi
    mov esi, [esp + 8]
    call CRC_32
    pop esi
    retn
_my_crc32 endp

END


crc.i  
%module crc

extern unsigned long my_crc32(const char *);


Makefile.pl
require 5.004 ;
use ExtUtils::MakeMaker;

WriteMakefile(
    'CCCDLFLAGS' => '/MT /EHsc',
    'INC'        =
> '-I.',
    'NAME'       =
> 'crc',
    'OBJECT'     =
> 'crc_wrap.obj crc.obj',
    'LIBS'       =
> '-llibcmt'
  );


Building of binding is very easy:  
swig.exe -perl -compat -noproxy crc.i
perl Makefile.pl
nmake
nmake install

Комментариев нет:

Отправить комментарий