我编写了一些代码来将补丁应用于二进制文件,补丁是十六进制值的定界 ascii 表示,例如:00FF00:A155F3;9210BC。
我的问题是我将十六进制的值误算为 256,但仅限于分隔符 ':' 和 ';' 因为无论代码中的位置如何,我都使用相同的 hex2char() 函数,所以我无法弄清楚为什么它在这一特定区域失败了。我想与其浪费太多时间,不如注册 stackoverflow 并获得一些帮助。
我不想使用任何外部库,我不想包含除 stdio.h 和 stdlib.h 之外的任何内容(我将删除 sys/stat.h 以及 fread() 和 realloc() 直到 EOF,但这是另一个问题,我不担心)
这是代码,我的主循环接近尾声,我清楚地标记了我得到错误输出的位置:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
//I'll populate this later
struct _pos {
unsigned pos;
} pos;
//raise x to the power of y
unsigned power( unsigned x, unsigned y )
{
if ( y == 0 )
{
return 1;
}
else
{
return x * power( x, y - 1 );
}
}
//convert ascii hex values to integers
unsigned h2i(const char *input, int len) {
unsigned retval = 0;
char cur = 0;
int loops = 0;
while (0 < len) {
cur = input[--len];
//check if we are out of the range hex would be in
//we assume capital letters A - F
if ( cur < 0x30 || cur > 0x46 ) {
printf("Hex out of range: %X\n", cur);
exit(EXIT_FAILURE);
}
//numerical
if ( cur < 0x3a ) {
cur -= 0x30;
}
//alphabetical
else {
cur -= 0x37;
}
retval += (cur * power(16, loops));
++loops;
}
return retval;
}
//take 2 chars from the input and get a hex value from it
char hex2char(const char *input) {
char buff[2];
buff[0] = input[pos.pos++];
buff[1] = input[pos.pos++];
printf("\nBUFF: %c%c : POS: %i\n", buff[0], buff[1], pos.pos);
return h2i(buff, 2);
}
//turn a delimiter terminated string into a number
unsigned hex2int(const char *input, char delimiter) {
int offset = 0;
char buff[13]; //max int length if represented by string
//copy string into the buffer
while (input[offset] != delimiter) {
if (offset == 12) {
printf("parse error: offset\n");
exit(EXIT_FAILURE);
}
buff[offset] = input[offset];
++offset;
}
//printf("BUFF: %s : OFFSET: %i\n", buff, offset);
pos.pos += offset;
return h2i(buff, offset);
}
char *fast_cache( char *Filename, unsigned long *size ) {
FILE *FilePointer;
FilePointer = fopen( Filename, "r" );
if ( FilePointer != NULL ) {
char *ptr;
struct stat FileStat;
fstat( fileno( FilePointer ), &FileStat );
*size = FileStat.st_size;
ptr = ( char * ) malloc( *size );
fread( ptr, 1, *size, FilePointer );
fclose( FilePointer );
return ptr;
}
else {
return NULL;
}
}
int main(int argc, char **argv)
{
// print greeting
printf("Hello World!\n");
unsigned long size;
char *file = fast_cache(argv[1], &size);
if (file == NULL) {
printf("You must specify an input!\n");
exit(EXIT_FAILURE);
}
//okay, patchfile found, opened, and cached, time to convert
pos.pos = 0; //file position
//this will be the output, unused right now
FILE *out = fopen("output", "w");
//a line in the file is formatted like this
//0000FF:A9DF23;A7FF11
unsigned fileoffset = 0;
unsigned trash = 0;
//loop through all lines in the patchfile
while (pos.pos < size) {
fileoffset = hex2int(file, 0x3a);
++pos.pos;
printf("offset: %u CUR: %c\n", fileoffset, file[pos.pos]);
while(file[pos.pos] != 0x3b ) {
//check current values with file values
//not implimented yet, i just print it out
///////////////////////////////////////////////////////////////////
///////////This is the issue right here///////////////////////////
//printf prints a near MAX_INT number hex value > 0xFFFFFF00
//and it is off by exactly 256 every time, but that would mean overflowing
printf("%X ", hex2char(file));
}
//////////////////////////////////////////////////////////////////
//but right here until EOF, everything is fine and works normally
//and it's the same freaking function
printf("\nfound delimiter: %c @ %u\n", file[pos.pos], pos.pos);
++pos.pos;
//second half of the line
while(file[pos.pos] != '\n' && pos.pos < size) {
printf("%X ", hex2char(file));
}
//eat the new line if there is one
++pos.pos;
printf("\nLine complete\n");
}
return 0;
}