154{
155 puts("#include <stdio.h>\nstatic int stack[128 * 1024];");
156#ifdef _MSC_VER
157 printf(
"static __declspec(align(4096)) ");
158#else
159 printf(
"static __attribute__((aligned(4096)))");
160#endif
161 puts("const unsigned char code[] = {");
162 for (size_t i = 0; i < size; i++) {
163 printf(
"0x%02x,", code[i]);
if ((i % 16) == 15) putchar(
'\n');
164 }
165 puts("\n};");
166#ifdef _MSC_VER
167 puts("#include <windows.h>");
168#else
169 puts("#include <unistd.h>");
170 puts("#include <sys/mman.h>");
171#endif
172 puts("int main()\n{");
173#ifdef _MSC_VER
174 puts("\tDWORD oldProtect;");
175 puts("\tVirtualProtect((void*)code, sizeof(code), PAGE_EXECUTE_READWRITE, &oldProtect);");
176#else
177 puts("\tlong pageSize = sysconf(_SC_PAGESIZE) - 1;");
178 puts("\tmprotect((void*)code, (sizeof(code) + pageSize) & ~pageSize, PROT_READ | PROT_EXEC);");
179#endif
180 puts(
181 "\t((void (*)(void*, void*, int *))code)((void*)putchar, (void*)getchar, stack);\n"
182 "}"
183 );
184}
LOGGING_API void printf(Category category, const char *format,...)