Jump to content
Tuts 4 You
Sign in to follow this  
Pancake

Force compiler to count hash during compilation

Recommended Posts

Pancake

Hello. I got a function which returns hash from const char*, and obviously the same input gives same 32 byte hash. My problem is that after compilation program recounts the hash everytime, is it possible to tell compiler to count it during compilation time and just save the hash value? Im creating wrappers for common functions like getmodulehandle, getprocaddress etc with PE parsing, and the problem is that such piece of code for example myGetModuleHandle(hash("kernel32.dll")) generates assembly which counts the const char* hash everytime, and i would like the compiler to make it myGetModuleHandle(0xdeadbeef) where its the counted hash.


 


How can i achive that?


Share this post


Link to post
simple

Yes ideally you can re write your "function which returns hash from const char*" as a macro, although you may be able to get the same effect by making it in inline.


Share this post


Link to post
kao

Preprocessor macro - maybe, but it's hard to write. Inline function - definitely no, as it doesn't do what Pancake wants.


 


Easiest solution is to make a small tool that calculate those hashes in advance and use magic constants in the code:



#define KERNEL32 0xE0F38342
#define _hVirtualProtect 0x15f8ef80
#define _hVirtualAlloc 0x19BC06C0
#define _hVirtualFree 0xEA43A878
#define _hCreateThread 0x15B87EA2
 
...
 
HMODULE k32=GetModuleHandle(KERNEL32);
_tVirtualProtect _VirtualProtect=(_tVirtualProtect)GetGetProcAddress(k32,_hVirtualProtect);
_tCreateThread _CreateThread=(_tCreateThread)GetGetProcAddress(k32,_hCreateThread);

Sample code: http://spth.virii.lu/inception1/content/sources/pest/beetle/beetle/  check loader.h and loader.cpp


 


Share this post


Link to post
simple

I'm sure OP knows he/she can hard code static values kao (and deal w/disadvantages that come w/that...)..


 


Macro - definitely, inline - definitely...


 


http://lolengine.net/blog/2011/12/20/cpp-constant-string-hash


 


gcc also has {} macro extensions which will make writing it more c like, but this wont work on VS.


Share this post


Link to post
kao

@Simple: While theoretically you are correct, there's a reason for using hardcoded constants - they work everywhere and don't cause problems like "require heavy optimisation flags (/O2 with Visual Studio)" or "limited to 10-character strings"

 

The preprocessor is where c programmers go to satisfy their perverse-complexity desires instead of program in c++/perl.

© Oxc0ffea

Share this post


Link to post
simple

@Simple: While theoretically you are correct, there's a reason for using hardcoded constants - they work everywhere and don't cause problems like "require heavy optimisation flags (/O2 with Visual Studio)" or "limited to 10-character strings"

 

 

Read the rest of the article kao, what you copy/pasted is true only for the inline version, not the macro...  this "works everywhere" too, 512 byte limit, no /O2...

#define H1(s,i,x) (x*65599u+(uint8_t)s[(i)<strlen(s)?strlen(s)-1-(i):strlen(s)]) // inside loop#define H4(s,i,x) H1(s,i,H1(s,i+1,H1(s,i+2,H1(s,i+3,x))))#define H16(s,i,x) H4(s,i,H4(s,i+4,H4(s,i+8,H4(s,i+12,x))))#define H64(s,i,x) H16(s,i,H16(s,i+16,H16(s,i+32,H16(s,i+48,x))))#define H256(s,i,x) H64(s,i,H64(s,i+64,H64(s,i+128,H64(s,i+192,x))))#define H512(s,i,x) H256(s,i,H256(s,i+256,H256(s,i+512,H256(s, i+768,x))))      // added this line to double to 512 limit, to double to 1024 add another...#define HASH(s) ((uint32_t)(H512(s,0,0)))                                       // last line of normal C code return valueprintf("%08x\n", HASH("WinApiFunction");

OP should do what works best for OP, but If I personally have 1000 strings, IMHO, writing 7 lines of macro functions looks a lot nicer than writing 1000 lines of constants (it's also going to be much faster too)...

Share this post


Link to post
atom0s
Pancake

Wow thats a lot of answers boys! I am coding in c++, definitely gonna read the links you posted, thanks!


Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×
×
  • Create New...