Jeg fandt også denne C++ kode... måske kunne en eller anden uddrage noget af
den ? (ved INTET om C++)
-------------------------------------------------
#include <io.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <windows.h>
#define PATCHMAX 16384
#define COPYBUFSIZE 1024
// identification line in .dif file
const char szIDAHeader[]="This difference file is created by The Interactive
Disassembler";
// filecopy buffer
char buf[COPYBUFSIZE];
// holds patch information
struct {
DWORD addr;
unsigned char original,patched;
} patch[PATCHMAX];
/* file copy */
int copyfile(char *from_file, char *to_file)
{
FILE *from_fp, *to_fp;
size_t num;
if ((from_fp = fopen(from_file, "rb")) == NULL) return 1;
if ((to_fp = fopen(to_file, "wb")) == NULL) {
fclose(from_fp);
return 1;
}
while ((num = fread(buf, sizeof(char), COPYBUFSIZE, from_fp)) > 0) {
if (fwrite(buf, sizeof(char), num, to_fp) != num) {
fclose(from_fp);
fclose(to_fp);
return 1;
}
}
fclose(from_fp);
fclose(to_fp);
return 0;
}
/* main */
int main(int argc, char *argv[])
{
FILE *stream;
int inhandle,outhandle;
int patchcount,i;
char szLine[256],inname[256],outname[256];
unsigned char inchar;
_fmode=_O_TEXT;
// print info and check cmdline parameters
fprintf(stderr,"IDAPAT v1 by SE (built %s)\n\n",__DATE__);
if (argc==1) {
fprintf(stderr,"Syntax: %s [IDA .dif-file] <output-file>\n",argv[0]);
return -1;
}
// open diff-file
stream=fopen(argv[1],"rt");
if (!stream) {
fprintf(stderr,"Cannot open input file %s.\n",argv[1]);
return -1;
}
// read header and check
fgets(szLine,256,stream);
if (!strstr(szLine,szIDAHeader)) {
fprintf(stderr,"File is no IDA diff-file.\n");
fclose(stream);
}
fgets(szLine,256,stream); // empty line skip
fgets(szLine,256,stream); // original file name
sscanf(szLine,"%s",inname);
if ((inhandle=_open(inname,O_RDONLY|O_BINARY))==-1) {
fprintf(stderr,"Cannot open %s. Stop.\n",inname);
fclose(stream);
return -1;
}
// run through diff file and check original for exact match
fprintf(stdout,"PASS 1: Verifying original file...");
patchcount=0;
while (!feof(stream)) {
fscanf(stream,"%08lX: %02X %02X", // read single line
&patch[patchcount].addr,
&patch[patchcount].original,
&patch[patchcount].patched);
// end of file, exit
if
((!patch[patchcount].addr)&&(!patch[patchcount].original)&&(!patch[patchcoun
t].patched)) {
fprintf(stdout," %u bytes to be patched.\n",patchcount);
break;
}
// seek into original and check byte
lseek(inhandle,patch[patchcount].addr,SEEK_SET);
_read(inhandle,&inchar,1);
if (inchar!=patch[patchcount].original) {
fprintf(stderr,"\nByte mismatch at %08lX, byte %02X should be %02X!",
patch[patchcount].addr,inchar,
patch[patchcount].original);
}
// increment byte counter and check for overflow
patchcount++;
if (patchcount==PATCHMAX) {
fprintf(stderr," patch too big (>%u).\n",PATCHMAX);
close(inhandle);
fclose(stream);
return -1;
}
}
// close files we no longer need
close(inhandle);
fclose(stream);
// copy original file to output file
if (argc<3) {
strcpy(outname,inname);
strcat(outname,".patched");
} else strcpy(outname,argv[2]);
fprintf(stdout,"PASS 2: Copying file (%s -> %s)...",inname,outname);
if (copyfile(inname,outname)) {
fprintf(stderr," failed!\n");
return -1;
}
// open new file r/w mode
if ((outhandle=_open(outname,_O_RDWR,_S_IREAD|_S_IWRITE))==-1) {
fprintf(stderr,"\nHuh!? Cannot open %s. Stop.\n",outname);
return -1;
}
// patch bytes
fprintf(stdout,"\nPASS 3: Patching bytes...");
for (i=0;i<patchcount;i++) {
lseek(outhandle,patch[i].addr,SEEK_SET);
_write(outhandle,&patch[i].patched,1
|