Jump to content
View in the app

A better way to browse. Learn more.

Tuts 4 You

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Force compiler to count hash during compilation

Featured Replies

Posted

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?


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.


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


 


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.


@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

@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)...

  • Author

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


Create an account or sign in to comment

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.