Files
2026-01-29 14:33:44 +04:00

68 lines
3.0 KiB
C

#ifndef PIC_STR_H
#define PIC_STR_H
#ifdef _MSC_VER
#error "pic_str requires GNU extensions (statement expressions). Please use Clang or MinGW on Windows instead of MSVC."
#endif
#include <stdint.h>
#include <wchar.h>
/*
* pic_str() - Position Independent Code String Obfuscator
*
* SAFE USAGE CONTEXTS (Immediate Consumption):
* The deobfuscated string must be consumed by a function or expression
* before the statement ends.
* - Function arguments: printf(pic_str("Hello %s"), name);
* - Windows API calls: MessageBoxW(NULL, pic_str(L"Text"), pic_str(L"Title"), 0);
* - Comparisons: if (strcmp(input, pic_str("password")) == 0)
* - Conditionals: if (pic_str("is_enabled")[0] == '1')
*
* UNSAFE USAGE CONTEXTS (Leads to dangling pointers or memory corruption):
* Do NOT store the pointer returned by this macro for use in later statements.
* - Pointer assignments: const char *msg = pic_str("test");
* (The pointer 'msg' is dangling and points to invalid memory on the next line)
* - Return values: return pic_str("error");
* (Returns an address to a local stack frame that is destroyed upon return)
* - Struct member initialization: s.msg = pic_str("data");
* (The member points to memory that will be overwritten by the next function call)
* - Array initialization: const char *arr[] = {pic_str("a"), pic_str("b")};
* (The array elements become invalid pointers as soon as the line completes)
*
* INVALID COMPILE-TIME CONTEXTS:
* - Static/global initialization: static const char *msg = pic_str("x");
* - Compile-time constants: #if pic_str("x")
* - String literal concatenation: pic_str("hello") "world"
*
* SUPPORTED STRING TYPES:
* - Regular strings: pic_str("hello") -> const char*
* - Wide strings (Windows): pic_str(L"hello") -> const wchar_t*
*/
#ifdef __cplusplus
extern "C" {
#endif
// Declare functions that return const char* - this provides type checking
// and autocomplete, and will cause errors for invalid usage (like initializing
// fixed-size char arrays, static initialization, etc.)
static inline const char *pic_str_impl(const char *s) { return s; }
static inline const wchar_t *pic_str_impl_w(const wchar_t *s) { return s; }
// Use _Generic to dispatch to the right function based on string type
// This handles char* (regular strings) and wchar_t* (Windows wide strings)
#define pic_str(x) \
_Generic((x), \
char *: pic_str_impl, \
const char *: pic_str_impl, \
wchar_t *: pic_str_impl_w, \
const wchar_t *: pic_str_impl_w)(x)
#ifdef __cplusplus
}
#endif
#endif // PIC_STR_H