282 lines
8.8 KiB
C
282 lines
8.8 KiB
C
|
/*
|
||
|
|
||
|
Copyright:
|
||
|
(C) 2022-2023 Daniele 'dzonerzy' Linguaglossa - http://libdzonerzy.so
|
||
|
|
||
|
This file is part of libdzonerzy.so.
|
||
|
|
||
|
GNU GENERAL PUBLIC LICENSE
|
||
|
Version 3, 29 June 2007
|
||
|
|
||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||
|
Everyone is permitted to copy and distribute verbatim copies
|
||
|
of this license document, but changing it is not allowed.
|
||
|
|
||
|
*/
|
||
|
|
||
|
#pragma once
|
||
|
#define _GNU_SOURCE
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <unistd.h>
|
||
|
#include <signal.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/mman.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <stdint.h>
|
||
|
|
||
|
#define EXPORT_SYMBOL(return, name) __attribute__((visibility("default"))) return name
|
||
|
#define HIDE_SYMBOL __attribute__((visibility("hidden")))
|
||
|
|
||
|
#define SECTION(x) __attribute__((section(x)))
|
||
|
// section name should be like "articles/article_name.md"
|
||
|
#define ARTICLE(x) SECTION("articles/" x ".md")
|
||
|
|
||
|
/* CSS macros */
|
||
|
// comment macros
|
||
|
#define CSS_COMMENT(x) "/* " x " */\n"
|
||
|
// property macros
|
||
|
#define CSS_PROPERTY(n, v) " " n ": " v ";\n"
|
||
|
// property macros with unescaped values
|
||
|
#define CSS_PROPERTY_UNESCAPED(n, v) " " n " : " v ";\n"
|
||
|
// selector macros
|
||
|
#define CSS_SELECTOR(n) n "{\n"
|
||
|
// end selector macros
|
||
|
#define CSS_END_SELECTOR() "}\n"
|
||
|
// rgb macros
|
||
|
#define CSS_RGB(r, g, b) "rgb(" #r "," #g "," #b ")"
|
||
|
// rgba macros
|
||
|
#define CSS_RGBA(r, g, b, a) "rgba(" #r "," #g "," #b "," #a ")"
|
||
|
// hsl macros
|
||
|
#define CSS_HSL(h, s, l) "hsl(" #h "," #s "," #l ")"
|
||
|
// hsla macros
|
||
|
#define CSS_HSLA(h, s, l, a) "hsla(" #h "," #s "," #l "," #a ")"
|
||
|
// url macros
|
||
|
#define CSS_URL(x) "url(" #x ")"
|
||
|
// new line macros
|
||
|
#define CSS_NEWLINE() "\n"
|
||
|
|
||
|
/* MD macros */
|
||
|
// meta info
|
||
|
#define MD_META(title, author) "% " title "\n" \
|
||
|
"% " author "\n"
|
||
|
#define MD_META_CUSTOM(v) "% " v "\n"
|
||
|
#define MD_META_NAMED(p, v) "% " p ": " v "\n"
|
||
|
#define MD_COMMENT(x) "<!-- " x " -->\n"
|
||
|
// section macros from 1 to 4
|
||
|
#define MD_SECTION1(x) "# " x "\n"
|
||
|
#define MD_SECTION2(x) "## " x "\n"
|
||
|
#define MD_SECTION3(x) "### " x "\n"
|
||
|
#define MD_SECTION4(x) "#### " x "\n"
|
||
|
// bold macros
|
||
|
#define MD_BOLD(x) "**" x "**"
|
||
|
// italic macros
|
||
|
#define MD_ITALIC(x) "*" x "*"
|
||
|
// link macros
|
||
|
#define MD_LINK(x, y) "[" x "](" y ")"
|
||
|
// image macros
|
||
|
#define MD_IMAGE(x, y) "![" x "](" y ")\n\n"
|
||
|
// raw html tag macros
|
||
|
#define MD_RAWTAG(t, c) "<" t ">" c "</" t ">"
|
||
|
// new line macros
|
||
|
#define MD_NEWLINE() "\n\n"
|
||
|
// code macros
|
||
|
#define MD_CODE(x) "`" x "`"
|
||
|
// code block macros
|
||
|
#define MD_CODEBLOCK(x) "```" x "```"
|
||
|
// code block macros with language
|
||
|
#define MD_CODEBLOCK_LANG(l, c) "```" l "\n" c "\n```"
|
||
|
// text macros
|
||
|
#define MD_TEXT(x) x
|
||
|
// list macros
|
||
|
#define MD_LIST(x) "- " x "\n"
|
||
|
|
||
|
// XML macros
|
||
|
// urlset macros
|
||
|
#define XML_URLSET() "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"
|
||
|
// url macros
|
||
|
#define XML_URL(x) " <url>\n" \
|
||
|
" <loc>" x "</loc>\n" \
|
||
|
" <lastmod>" __DATE__ "</lastmod>\n" \
|
||
|
" </url>\n"
|
||
|
// end urlset macros
|
||
|
#define XML_END_URLSET() "</urlset>\n"
|
||
|
|
||
|
/*#define STACK_SIZE 10 * 1024
|
||
|
|
||
|
enum Flags
|
||
|
{
|
||
|
EQ = 1 << 0,
|
||
|
GT = 1 << 1,
|
||
|
LT = 1 << 2,
|
||
|
};
|
||
|
|
||
|
typedef struct machine
|
||
|
{
|
||
|
uint32_t R;
|
||
|
uint32_t SP;
|
||
|
uint32_t FL;
|
||
|
uint8_t *stack;
|
||
|
} machine_t, *pmachine_t;
|
||
|
|
||
|
#define C_START \
|
||
|
int fd[2]; \
|
||
|
char *b = "a"; \
|
||
|
int fd2; \
|
||
|
int *m; \
|
||
|
int a; \
|
||
|
char *c; \
|
||
|
int *p;
|
||
|
|
||
|
#define sys_write(x, y, z) \
|
||
|
__asm__ __volatile__("syscall" \
|
||
|
: \
|
||
|
: "a"(1), "D"(x), "S"(y), "d"(z))
|
||
|
|
||
|
#define sys_pipe(x) \
|
||
|
__asm__ __volatile__("syscall" \
|
||
|
: \
|
||
|
: "a"(22), "D"(x))
|
||
|
|
||
|
#define sys_close(x) \
|
||
|
__asm__ __volatile__("syscall" \
|
||
|
: \
|
||
|
: "a"(3), "D"(x))
|
||
|
|
||
|
#define sys_fork() \
|
||
|
({ \
|
||
|
int ret; \
|
||
|
__asm__ __volatile__("syscall" \
|
||
|
: "=a"(ret) \
|
||
|
: "a"(57)); \
|
||
|
ret; \
|
||
|
})
|
||
|
|
||
|
#define sys_kill(x, y) \
|
||
|
__asm__ __volatile__("movl %0, %%edi;" \
|
||
|
"movl %1, %%esi;" \
|
||
|
"mov $62, %%rax;" \
|
||
|
"syscall;" \
|
||
|
: \
|
||
|
: "r"(x), "r"(y) \
|
||
|
: "edi", "esi", "rax")
|
||
|
|
||
|
// getpid and save to pid variable
|
||
|
#define sys_getpid() \
|
||
|
({ \
|
||
|
int ret; \
|
||
|
__asm__ __volatile__("syscall" \
|
||
|
: "=a"(ret) \
|
||
|
: "a"(39)); \
|
||
|
ret; \
|
||
|
})
|
||
|
|
||
|
#define sys_open(a1, a2, a3) \
|
||
|
({ \
|
||
|
unsigned long ret; \
|
||
|
__asm__ __volatile__("syscall" \
|
||
|
: "=a"(ret) \
|
||
|
: "a"(2), "D"(a1), "S"(a2), "d"(a3) \
|
||
|
: "rcx", "r11", "memory"); \
|
||
|
ret; \
|
||
|
})
|
||
|
|
||
|
#define sys_mmap(a1, a2, a3, a4, a5, a6) \
|
||
|
({ \
|
||
|
unsigned long ret; \
|
||
|
register long r10 __asm__("r10") = a4; \
|
||
|
register long r8 __asm__("r8") = a5; \
|
||
|
register long r9 __asm__("r9") = a6; \
|
||
|
__asm__ __volatile__("syscall" \
|
||
|
: "=a"(ret) \
|
||
|
: "a"(9), "D"(a1), "S"(a2), \
|
||
|
"d"(a3), "r"(r10), "r"(r8), "r"(r9) \
|
||
|
: "rcx", "r11", "memory"); \
|
||
|
ret; \
|
||
|
})
|
||
|
|
||
|
#define sys_munmap(a1, a2) \
|
||
|
asm volatile("syscall" \
|
||
|
: \
|
||
|
: "a"(11), "D"(a1), "S"(a2) \
|
||
|
: "rcx", "r11", "memory");
|
||
|
|
||
|
#define C_SIGPIPE(c) \
|
||
|
sys_pipe(fd); \
|
||
|
sys_close(fd[0]); \
|
||
|
sys_write(fd[1], b, 1); \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_SIGTRAP(c) \
|
||
|
__asm__("int3"); \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_SIGILL(j1, j2, x, k) \
|
||
|
__asm__("ud2"); \
|
||
|
__asm__(".byte " #x); \
|
||
|
if (k) \
|
||
|
{ \
|
||
|
__asm__ __volatile__ goto("jmp %l0" \
|
||
|
: \
|
||
|
: \
|
||
|
: \
|
||
|
: j1); \
|
||
|
} \
|
||
|
else \
|
||
|
{ \
|
||
|
__asm__ __volatile__ goto("jmp %l0" \
|
||
|
: \
|
||
|
: \
|
||
|
: \
|
||
|
: j2); \
|
||
|
}
|
||
|
|
||
|
#define C_SIGFPE(c) \
|
||
|
__asm__("mov $1, %rax"); \
|
||
|
__asm__("mov $0, %rbx"); \
|
||
|
__asm__("div %rbx"); \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_SIGSEGV(c) \
|
||
|
*(int *)0xffffffffffff = 1; \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_SIGTERM(c) \
|
||
|
sys_kill(process_pid, SIGTERM); \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_SIGXCPU(c) \
|
||
|
sys_kill(process_pid, SIGXCPU); \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_SIGSYS(c) \
|
||
|
sys_kill(process_pid, SIGSYS); \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_SIGBUS(v) \
|
||
|
__asm__("pushf\norl $0x40000,(%rsp)\npopf"); \
|
||
|
a = 0xffffff; \
|
||
|
c = (char *)&a; \
|
||
|
c++; \
|
||
|
p = (int *)c; \
|
||
|
*p = 10; \
|
||
|
__asm__(".byte " #v); \
|
||
|
__asm__("pushf\nxorl $0x40000,(%rsp)\npopf");
|
||
|
|
||
|
#define C_SIGXFSZ(c) \
|
||
|
sys_kill(process_pid, SIGXFSZ); \
|
||
|
__asm__(".byte " #c);
|
||
|
|
||
|
#define C_END \
|
||
|
sys_close(fd[0]); \
|
||
|
sys_close(fd[1]);
|
||
|
|
||
|
#define OP_ADD 0xd1
|
||
|
#define OP_SUB 0x71
|
||
|
#define OP_MUL 0xf7
|
||
|
#define OP_DIV 0x7f
|
||
|
#define OP_MOD 0x3c
|
||
|
#define OP_XOR 0x5a
|
||
|
#define OP_AND 0x2d
|
||
|
#define OP_OR 0x6e*/
|