我有以下代码:
typedef void * (__stdcall * call_generic)(...);
typedef void * (__stdcall * call_push2)(unsigned long,unsigned long);
void * pfunc;
// assume pfunc is a valid pointer to external function
// this is a logically correct way of calling, however this includes:
// add esp, 8
// after the call, and that breaks my stack.
((call_generic)pfunc)(1,1);
// however, if i use this call:
((call_push2)pfunc)(1,1);
// this does not happen and code works properly.
Run Code Online (Sandbox Code Playgroud)
跟踪所有调用并手动计算args是很痛苦的(前面有很多这样的调用),我更喜欢宏或类似的东西,但有了这个bug,它是不可能的.
有解决方案吗?是否有另一种创建call_generic类型的方法来做这些事情?
我真的不明白为什么它会"清理"但是会严重破坏我的堆栈,从而导致先前定义的变量丢失.
好的,所以我可以通过声明来调用函数作为fastcall CC __attribute__((fastcall)).如何将函数本身定义为fastcall?
就像,我有来电代码:
// caller.c
unsigned long func(unsigned long i) __attribute__((fastcall));
void caller() {
register unsigned long i = 0;
while ( i != 0xFFFFFFD0 ) {
i = func(i);
}
}
Run Code Online (Sandbox Code Playgroud)
功能:
// func.c
unsigned long func(unsigned long i) {
return i++;
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,func()正在编译为cdecl,它从堆栈中提取i,而不是从ecx(这是i386)中提取i.
如果我unsigned long func(unsigned long i) __attribute__((fastcall));在func.c中写,它就不会编译,说
error: expected ‘,’ or ‘;’ before ‘{’ token
Run Code Online (Sandbox Code Playgroud)
如果我在func.c中以与我在caller.c中相同的方式声明它,它会以另一种方式抱怨:
error: previous declaration …Run Code Online (Sandbox Code Playgroud) 在Windows中,有SetEndOfFile()API可以最终删除一些数据.
我如何在Linux中执行此操作?
我正在寻找的伪代码示例(特定于Linux):
int fd = open("/path/to/file",O_RDWR);
// file contents: "0123456789ABCDEF", 16 bytes
lseek(fd,10,SEEK_CUR);
// what's in the next line? (imaginary code)
syscall(what,fd,FD_SET_EOF);
close(fd);
//sync();
// now file on disk looks like "0123456789", 10 bytes
Run Code Online (Sandbox Code Playgroud) 我有一个创建二进制sha1的函数,但我需要一个40字节的字符串结果(对于null-t为+1).有没有更好的方法将它转换为字符串,而不是这个?
unsigned char hash_binary[20] = "\xFF\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01";
char hash_string[41];
int i;
for(i = 0;i < 20; i++)
sprintf( hash_string + i*2, "%02X", hash_binary[i] );
hash_string[40] = 0;
Run Code Online (Sandbox Code Playgroud)
这称为sprintf 20次.我怎么能避免呢?