Posted July 12, 201510 yr 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?
July 12, 201510 yr 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.
July 12, 201510 yr 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
July 12, 201510 yr 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.
July 12, 201510 yr C++11 has a feature for it http://www.cprogramming.com/c++11/c++11-compile-time-processing-with-constexpr.html. But what language does the topic starter use? This is totally language/compiler specific... Edited July 12, 201510 yr by Aguila
July 12, 201510 yr @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
July 12, 201510 yr @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)...
July 13, 201510 yr constexpr is what you want to use for compile time things like this. Here are some examples - hashing: http://stackoverflow.com/questions/2111667/compile-time-string-hashing - hashing: http://stackoverflow.com/questions/28675727/using-crc32-algorithm-to-hash-string-at-compile-time - encryption: http://www.unknowncheats.me/forum/c-and-c/113715-compile-time-string-encryption.html - misc: https://github.com/andrivet/ADVobfuscator
July 13, 201510 yr How about templates? Constants are very error-prone, unless you auto-generate source code files (and make sure to always update them when your constants change). http://www.codeproject.com/Articles/3743/A-gentle-introduction-to-Template-Metaprogramming
July 13, 201510 yr 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