📄 callback.xs
字号:
/*
# Win32::API::Callback - Perl Win32 API Import Facility
#
# Version: 0.40
# Date: 07 Mar 2003
# Author: Aldo Calpini <dada@perl.it>
# $Id: Callback.xs,v 1.0 2002/03/19 10:25:00 dada Exp $
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <memory.h>
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define CROAK croak
#include "../API.h"
#pragma optimize("", off)
/*
* some Perl macros for backward compatibility
*/
#ifdef NT_BUILD_NUMBER
#define boolSV(b) ((b) ? &sv_yes : &sv_no)
#endif
#ifndef PL_na
# define PL_na na
#endif
#ifndef SvPV_nolen
# define SvPV_nolen(sv) SvPV(sv, PL_na)
#endif
#ifndef call_pv
# define call_pv(name, flags) perl_call_pv(name, flags)
#endif
#ifndef call_sv
# define call_sv(name, flags) perl_call_sv(name, flags)
#endif
int PerformCallback(SV* self, int nparams, APIPARAM* params);
SV* fakesv;
int fakeint;
char *fakepointer;
int RelocateCode(unsigned char* cursor, unsigned int displacement) {
int skip;
unsigned int reladdr;
switch(*cursor) {
// skip +0
case 0x50: // push eax
case 0x51: // push ecx
case 0x55: // push ebp
case 0x56: // push esi
case 0x59: // pop ecx
case 0x5E: // pop esi
case 0xC3: // ret
case 0xC9: // leave
skip = 0;
break;
// skip +1
case 0x33: // xor
case 0x3B: // cmp
case 0x6A: // push (1 byte)
case 0x74: // je
case 0x75: // jne
case 0x7D: // jge
case 0x7E: // jle
case 0x85: // test
case 0xEB: // jmp
skip = 1;
break;
// skip +1/+2
case 0x2B:
if(*(cursor+1) == 0x30 // sub esi, dword ptr [eax]
) {
skip = 1;
break;
} else
if (*(cursor+1) == 0x45 // sub eax, dword ptr [ebp+1 byte]
) {
skip = 2;
break;
}
// skip +1/+2
case 0x89:
if(*(cursor+1) == 0x01 // mov dword ptr [ecx], eax
|| *(cursor+1) == 0x08 // mov dword ptr [eax], ecx
|| *(cursor+1) == 0x30 // mov dword ptr [eax], esi
) {
skip = 1;
break;
}
if(*(cursor+1) == 0x45 // mov dword ptr [ebp+1 byte], eax
|| *(cursor+1) == 0x4D // mov dword ptr [ebp+1 byte], ecx
|| *(cursor+1) == 0x04 // mov dword ptr [edx+ecx*1 byte], eax
) {
skip = 2;
break;
}
// skip +1/+2
case 0x8B:
if(*(cursor+1) == 0x00 // mov eax,dword ptr [eax]
|| *(cursor+1) == 0x09 // mov ecx,dword ptr [ecx]
|| *(cursor+1) == 0x0E // mov ecx,dword ptr [esi]
|| *(cursor+1) == 0xEC // mov ebp,esp
|| *(cursor+1) == 0xF0 // mov esi,eax
) {
skip = 1;
break;
} else
if(*(cursor+1) == 0x40 // mov eax,dword ptr [eax+1 byte]
|| *(cursor+1) == 0x45 // mov eax,dword ptr [ebp+1 byte]
|| *(cursor+1) == 0x4D // mov ecx,dword ptr [ebp+1 byte]
|| *(cursor+1) == 0x55 // mov edx,dword ptr [ebp+1 byte]
|| *(cursor+1) == 0x75 // mov esi,dword ptr [ebp+1 byte]
) {
skip = 2;
break;
}
case 0xFF:
if(*(cursor+1) == 0x30 // push dword ptr [eax]
) {
skip = 1;
break;
} else
if(*(cursor+1) == 0x75 // push dword ptr [epb+1 byte]
|| *(cursor+1) == 0x34 // push dword ptr [ecx+eax*4]
) {
skip = 2;
break;
} else
if(*(cursor+1) == 0x15 // call dword ptr ds:(4 byte)
|| *(cursor+1) == 0x35 // push dword ptr ds:(4 byte)
) {
skip = 5;
break;
}
// skip +2
case 0xC1: // sar
skip = 2;
break;
// skip +2/+3
case 0x83:
if(*(cursor+1) != 0x65 // add|sub|cmp
) {
skip = 2;
break;
} else
if(*(cursor+1) == 0x65 // add|sub|cmp
) {
skip = 3;
break;
}
// skip +4
case 0x25: // and
case 0x68: // push (4 bytes)
skip = 4;
break;
// skip +6
case 0xC7: // mov dword ptr (ebp+1 byte), (4 byte)
skip = 6;
break;
case 0xE8:
// we relocate here!
reladdr = *((int*)(cursor + 1));
*((int*)(cursor + 1)) = (unsigned int) (reladdr - displacement);
skip = 4;
break;
default:
#ifdef WIN32_API_DEBUG
printf("(C)RelocateCode: %08X ???? 0x%x\n", cursor, *cursor);
#endif
skip = 0;
break;
}
#ifdef WIN32_API_DEBUG
{
int i;
printf("(C)RelocateCode: %08X skip +%1d ", cursor, skip);
for(i = 0; i <= skip; i++) {
printf("%02X ", *(cursor+i));
}
printf("\n");
}
#endif
return 1 + skip;
}
// SV* CallbackMakeStruct(SV* self, int nparam, char *addr) {
int CallbackMakeStruct(SV* self, int nparam, char *addr) {
#ifdef dTHX
dTHX;
#endif
dSP;
SV* structobj = NULL;
char ikey[80];
/*
#ifdef WIN32_API_DEBUG
printf("(C)CallbackMakeStruct: got self='%s'\n", SvPV_nolen(self));
sv_dump(self);
if(SvROK(self)) sv_dump(SvRV(self));
printf("(C)CallbackMakeStruct: got nparam=%d\n", nparam);
printf("(C)CallbackMakeStruct: got addr=0x%08x\n", addr);
// memcpy( SvPV_nolen(self), 0, 1000);
#endif
*/
ENTER;
SAVETMPS;
// XPUSHs(sv_2mortal(newSVrv(self, "Win32::API::Callback")));
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVsv(self)));
XPUSHs(sv_2mortal(newSViv(nparam)));
XPUSHs(sv_2mortal(newSViv((long) addr)));
PUTBACK;
call_pv("Win32::API::Callback::MakeStruct", G_SCALAR);
SPAGAIN;
structobj = newSVsv(POPs);
itoa(nparam, ikey, 10);
hv_store( (HV*)SvRV(self), ikey, strlen(ikey), structobj, 0 );
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: self{'%s'}='%s'\n", ikey, SvPV_nolen(*(hv_fetch( (HV*)SvRV(self), ikey, strlen(ikey), 0 ))));
#endif
/*
#ifdef WIN32_API_DEBUG
printf("(C)CallbackMakeStruct: structobj='%s'\n", SvPV_nolen(structobj));
sv_dump(structobj);
if(SvROK(structobj)) sv_dump(SvRV(structobj));
#endif
*/
PUTBACK;
FREETMPS;
LEAVE;
return 1;
// return structobj;
}
int CALLBACK CallbackTemplate() {
SV* myself = (SV*) 0xC0DE0001; // checkpoint_SELFPOS
int nparams = 0xC0DE0002; // checkpoint_NPARAMS
APIPARAM* params;
unsigned int checkpoint;
int i, r;
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: nparams=%d\n", nparams);
// printf("(C)CallbackTemplate: myself='%s'\n", SvPV_nolen(myself));
// sv_dump(myself);
// if(SvROK(myself)) sv_dump(SvRV(myself));
#endif
params = (APIPARAM*) safemalloc( nparams * sizeof(APIPARAM) );
checkpoint = 0xC0DE0010; // checkpoint_PUSHI
i = 0xC0DE0003; // checkpoint_IPOS
params[i].t = T_INTEGER;
params[i].l = fakeint;
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: PUSHI(%d)=", i);
printf("%d\n", params[i].l);
#endif
checkpoint = 0xC0DE0020; // checkpoint_PUSHL
i = 0xC0DE0003; // checkpoint_IPOS
params[i].t = T_NUMBER;
params[i].l = fakeint;
checkpoint = 0xC0DE0030; // checkpoint_PUSHP
i = 0xC0DE0003; // checkpoint_IPOS
params[i].t = T_POINTER;
params[i].p = (char*) fakeint;
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: PUSHP(%d)=", i);
printf("%08x (%s)\n", params[i].p, params[i].p);
#endif
checkpoint = 0xC0DE0040; // checkpoint_PUSHS
i = 0xC0DE0003; // checkpoint_IPOS
params[i].t = T_STRUCTURE;
params[i].p = (char*) fakeint;
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: PUSHS(%d)=", i);
printf("%08x\n", params[i].p);
#endif
/*
checkpoint = 0xC0DE0050; // checkpoint_PUSHD
i = 0xC0DE0003; // checkpoint_IPOS
params[i].t = T_DOUBLE;
params[i].d = fakedouble;
*/
checkpoint = 0xC0DE9999; // checkpoint_END
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: Calling PerformCallback...\n");
#endif
r = PerformCallback(myself, nparams, params);
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: r=%d\n", r);
#endif
safefree(params);
#ifdef WIN32_API_DEBUG
printf("(C)CallbackTemplate: RETURNING\n");
#endif
return r;
}
int PerformCallback(SV* self, int nparams, APIPARAM* params) {
SV* mycode;
int i = 0;
char ikey[80];
unsigned int checkpoint;
I32 size;
int r;
#ifdef dTHX
dTHX;
#endif
dSP;
mycode = *(hv_fetch((HV*)SvRV(self), "sub", 3, FALSE));
for(i=0; i < nparams; i++) {
if(params[i].t == T_STRUCTURE) {
CallbackMakeStruct(self, i, params[i].p);
}
}
ENTER;
SAVETMPS;
PUSHMARK(SP);
for(i=0; i < nparams; i++) {
switch(params[i].t) {
case T_STRUCTURE:
itoa(i, ikey, 10);
XPUSHs(sv_2mortal(*(hv_fetch((HV*)SvRV(self), ikey, strlen(ikey), 0))));
break;
case T_POINTER:
XPUSHs(sv_2mortal(newSVpv((char *) params[i].p, 0)));
break;
case T_INTEGER:
case T_NUMBER:
XPUSHs(sv_2mortal(newSViv((int) params[i].l)));
break;
}
}
PUTBACK;
call_sv(mycode, G_EVAL | G_SCALAR);
SPAGAIN;
r = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return r;
}
unsigned char * CallbackCreate(int nparams, APIPARAM *params, SV* self, SV* callback) {
unsigned char * code;
unsigned char * cursor;
unsigned int i, j, r, toalloc, displacement;
unsigned char ebpcounter = 8;
unsigned char * source = (unsigned char *) (void *) CallbackTemplate;
BOOL done = FALSE;
unsigned int distance = 0;
BOOL added_INIT_STRUCT = FALSE;
int N_structs = 0;
unsigned int
checkpoint_PUSHI = 0,
checkpoint_PUSHL = 0,
checkpoint_PUSHP = 0,
checkpoint_PUSHS = 0,
checkpoint_END = 0,
checkpoint_DONE = 0;
unsigned int
section_START,
section_PUSHI,
section_PUSHL,
section_PUSHP,
section_PUSHS,
section_END;
cursor = source;
while(!done) {
if(*(cursor+0) == 0x10
&& *(cursor+1) == 0x00
&& *(cursor+2) == 0xDE
&& *(cursor+3) == 0xC0
) {
#ifdef WIN32_API_DEBUG
printf("(C)CallbackCreate.Study: checkpoint_PUSHI=%d\n", distance);
#endif
checkpoint_PUSHI = distance - 3;
}
if(*(cursor+0) == 0x20
&& *(cursor+1) == 0x00
&& *(cursor+2) == 0xDE
&& *(cursor+3) == 0xC0
) {
#ifdef WIN32_API_DEBUG
printf("(C)CallbackCreate.Study: checkpoint_PUSHL=%d\n", distance);
#endif
checkpoint_PUSHL = distance - 3;
}
if(*(cursor+0) == 0x30
&& *(cursor+1) == 0x00
&& *(cursor+2) == 0xDE
&& *(cursor+3) == 0xC0
) {
#ifdef WIN32_API_DEBUG
printf("(C)CallbackCreate.Study: checkpoint_PUSHP=%d\n", distance);
#endif
checkpoint_PUSHP = distance - 3;
}
if(*(cursor+0) == 0x40
&& *(cursor+1) == 0x00
&& *(cursor+2) == 0xDE
&& *(cursor+3) == 0xC0
) {
#ifdef WIN32_API_DEBUG
printf("(C)CallbackCreate.Study: checkpoint_PUSHS=%d\n", distance);
#endif
checkpoint_PUSHS = distance - 3;
}
if(*(cursor+0) == 0x99
&& *(cursor+1) == 0x99
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -