diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..121877a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode +dist +src/challenge.c \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f31d8a8 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +include tools/Makefile +include src/Makefile + +default: all + +all: embed libdzonerzy libdzonerzy-gen + +clean: clean-tools clean-libdzonerzy \ No newline at end of file diff --git a/res/dzonerzy.jpg b/res/dzonerzy.jpg new file mode 100644 index 0000000..2c20f97 Binary files /dev/null and b/res/dzonerzy.jpg differ diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..df43b80 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,50 @@ +CC = $(shell which gcc) +PANDOC = $(shell which pandoc) +OBJDUMP = $(shell which objdump) +OBJCOPY = $(shell which objcopy) +READELF = $(shell which readelf) + +OUTPUT = libdzonerzy.so + +SRC_INC_DIR = src/include +CFLAGS = -Wno-error=unused-parameter -Wno-error=unused-const-variable -std=c99 -O0 -shared -fPIC -I$(SRC_INC_DIR) -Wno-int-conversion -Wno-div-by-zero +LDFLAGS = + +EMBED_TOOL = tools/bin/embed +EMBED_ARGS = -f=h + +DIST = dist +SRC_DIR = src +RAW_DIST = dist/raw +SITE_DIST = dist/site + +AWK_UTIL = awk '/^\s*\[[0-9]+\]\s+([^\.].+)/ {print $$2}' +AWK_UTIL_NOCRLF = awk '/^\s*\[[0-9]+\]\s+([^\.].+)/ {printf $$2" "}' + +PANDOC_ARGS = --standalone --table-of-contents --section-divs --email-obfuscation=references --css=main.css --include-after-body=$(RAW_DIST)/footer.html + +libdzonerzy: + @echo "/* DO NOT EDIT THIS FILE - it is machine generated */" > $(SRC_DIR)/res.h + @mkdir -p $(DIST) + @mkdir -p $(RAW_DIST) + @mkdir -p $(SITE_DIST) + @$(foreach file, $(wildcard res/*), $(EMBED_TOOL) -i $(file) -o $(SRC_INC_DIR)/$(notdir $(file)).h $(EMBED_ARGS);) + @ls $(SRC_INC_DIR)/*.h | xargs -n1 basename | sed 's/^/#include /' >> $(SRC_DIR)/res.h + $(CC) $(CFLAGS) $(LDFLAGS) -o $(DIST)/$(OUTPUT) $(SRC_DIR)/libdzonerzy.so.c $(SRC_DIR)/challenge.o + +libdzonerzy-dump: + @$(READELF) -S -W $(DIST)/$(OUTPUT) | $(AWK_UTIL) | xargs dirname {} | sort | uniq | xargs -I{} mkdir -p $(RAW_DIST)/{} + @$(foreach fn, $(shell $(READELF) -S -W $(DIST)/$(OUTPUT) | $(AWK_UTIL_NOCRLF)), $(OBJCOPY) $(DIST)/$(OUTPUT) --dump-section $(fn)=/dev/stdout | cat | head -c -1 > $(RAW_DIST)/$(fn);) + +libdzonerzy-gen: libdzonerzy-dump + @mkdir -p $(SITE_DIST)/assets/ + @$(foreach page, $(wildcard $(RAW_DIST)/pages/*), $(PANDOC) $(PANDOC_ARGS) --from=markdown --to=html --output=$(shell echo $(SITE_DIST)/$(notdir $(page)) | sed 's/\.md/\.html/') $(page);) + @cp $(RAW_DIST)/main.css $(SITE_DIST)/main.css + @cp $(RAW_DIST)/sitemap.xml $(SITE_DIST)/sitemap.xml + @cp -r $(RAW_DIST)/embed/* $(SITE_DIST)/assets/ + @chmod -R 666 $(SITE_DIST)/* + +clean-libdzonerzy: + @echo "/* DO NOT EDIT THIS FILE - it is machine generated */" > $(SRC_DIR)/res.h + rm -rf $(DIST) + rm -f $(SRC_INC_DIR)/*.h \ No newline at end of file diff --git a/src/challenge.o b/src/challenge.o new file mode 100644 index 0000000..1735549 Binary files /dev/null and b/src/challenge.o differ diff --git a/src/libdzonerzy.so.c b/src/libdzonerzy.so.c new file mode 100644 index 0000000..6accf17 --- /dev/null +++ b/src/libdzonerzy.so.c @@ -0,0 +1,261 @@ +/* + +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. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +*/ + +#define _POSIX_C_SOURCE 200809L +#include "libdzonerzy.so.h" +#include "res.h" + +// this is a shared object library + +#define MAIN_CSS \ + CSS_COMMENT("main.css") \ + CSS_NEWLINE() \ + CSS_SELECTOR("html,body") \ + CSS_PROPERTY("margin", "0") \ + CSS_PROPERTY("padding", "0") \ + CSS_PROPERTY("width", "100%") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("body") \ + CSS_PROPERTY("font-family", "monospace") \ + CSS_PROPERTY("font-size", "1.23em") \ + CSS_PROPERTY("background-color", "#ddd") \ + CSS_PROPERTY("display", "flex") \ + CSS_PROPERTY("flex-direction", "column") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("#title-block-header, #TOC") \ + CSS_PROPERTY("border", "1px solid #222") \ + CSS_PROPERTY("border-radius", "15px") \ + CSS_PROPERTY("background-color", "#ccc") \ + CSS_PROPERTY("color", "#000") \ + CSS_PROPERTY("margin", "1em") \ + CSS_PROPERTY("padding", "1em") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("footer") \ + CSS_PROPERTY("border", "1px solid #222") \ + CSS_PROPERTY("border-radius", "15px") \ + CSS_PROPERTY("background-color", "#ccc") \ + CSS_PROPERTY("color", "#000") \ + CSS_PROPERTY("margin", "1em") \ + CSS_PROPERTY("padding", "1em") \ + CSS_PROPERTY("flex-shrink", "0") \ + CSS_PROPERTY("display", "flex") \ + CSS_PROPERTY("flex-direction", "column") \ + CSS_PROPERTY("justify-content", "center") \ + CSS_PROPERTY("align-items", "center") \ + CSS_PROPERTY("font-size", "0.9em") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("div.hearth") \ + CSS_PROPERTY("display", "flex") \ + CSS_PROPERTY("justify-content", "center") \ + CSS_PROPERTY("align-items", "center") \ + CSS_PROPERTY("margin-left", "0.2em") \ + CSS_PROPERTY("margin-right", "0.2em") \ + CSS_PROPERTY("font-size", "1.5em") \ + CSS_PROPERTY("padding", "0px") \ + CSS_PROPERTY("animation", "bounce 1s infinite") \ + CSS_PROPERTY("color", "#d00000") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("b.author") \ + CSS_PROPERTY("color", "#000") \ + CSS_PROPERTY("text-transform", "uppercase") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("p.author::before") \ + CSS_PROPERTY("text-transform", "lowercase") \ + CSS_PROPERTY("font-weight", "normal") \ + CSS_PROPERTY("content", "'by '") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("p.date::before") \ + CSS_PROPERTY("text-transform", "lowercase") \ + CSS_PROPERTY("font-weight", "normal") \ + CSS_PROPERTY("content", "'written on '") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("p.date") \ + CSS_PROPERTY("text-transform", "capitalize") \ + CSS_PROPERTY("font-weight", "bold") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("p.author") \ + CSS_PROPERTY("text-transform", "uppercase") \ + CSS_PROPERTY("font-weight", "bold") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("h1.title") \ + CSS_PROPERTY("text-align", "center") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("section") \ + CSS_PROPERTY("margin", "1em") \ + CSS_PROPERTY("flex", "1 0 auto") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("h1 h2 h3 h4 h5 h6") \ + CSS_PROPERTY("font-family", "monospace") \ + CSS_PROPERTY("clear", "both") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("img") \ + CSS_PROPERTY("padding", "2px") \ + CSS_PROPERTY("border", "2px dashed #333") \ + CSS_PROPERTY("background-color", "#fff") \ + CSS_PROPERTY("color", "#000") \ + CSS_PROPERTY("height", "12em") \ + CSS_PROPERTY("float", "right") \ + CSS_PROPERTY("clear", "right") \ + CSS_PROPERTY("margin-left", "1em") \ + CSS_PROPERTY("margin-bottom", "1em") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("figcaption") \ + CSS_PROPERTY("display", "none") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("@keyframes bounce") \ + CSS_SELECTOR("from, to") \ + CSS_PROPERTY("transform", "scale(1, 1)") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("25%") \ + CSS_PROPERTY("transform", "scale(0.9, 1.1)") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("50%") \ + CSS_PROPERTY("transform", "scale(1.1, 0.9)") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("75%") \ + CSS_PROPERTY("transform", "scale(0.95, 1.05)") \ + CSS_END_SELECTOR() \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("footer > span") \ + CSS_PROPERTY("display", "flex") \ + CSS_PROPERTY("text-align", "center") \ + CSS_PROPERTY("justify-content", "center") \ + CSS_PROPERTY("align-items", "center") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("pre, code") \ + CSS_PROPERTY("min-width", "60vw") \ + CSS_PROPERTY("max-width", "80vw") \ + CSS_PROPERTY("max-height", "500px") \ + CSS_PROPERTY("overflow", "auto") \ + CSS_PROPERTY("font-size", "1.1em") \ + CSS_PROPERTY("font-family", "monospace") \ + CSS_PROPERTY("border-radius", "0.5em") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("blockquote, pre") \ + CSS_PROPERTY("padding", "1em") \ + CSS_PROPERTY("border", "1px solid #111") \ + CSS_PROPERTY("background-color", "#9a9091") \ + CSS_PROPERTY("color", "#111") \ + CSS_PROPERTY("clear", "both") \ + CSS_PROPERTY("padding-top", "0.25em") \ + CSS_PROPERTY("padding-bottom", "0.25em") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("div.sourceCode") \ + CSS_PROPERTY("display", "flex") \ + CSS_PROPERTY("flex-direction", "column") \ + CSS_PROPERTY("justify-content", "center") \ + CSS_PROPERTY("align-items", "center") \ + CSS_END_SELECTOR() \ + CSS_SELECTOR("pre::-webkit-scrollbar") \ + CSS_PROPERTY("display", "none") \ + CSS_END_SELECTOR() + +#define INDEX_MD \ + MD_META("DZONERZY's Blog", "DZONERZY") \ + MD_META_CUSTOM("Thursday 12 July 2023") \ + MD_NEWLINE() \ + MD_SECTION1("About") \ + MD_IMAGE("Picture of DZONERZY", "assets/dzonerzy.jpg") \ + MD_TEXT("Hello world here is dzonerzy, also known as Daniele Linguaglossa,") \ + MD_TEXT("I'm an offensive security researcher and developer.") \ + MD_NEWLINE() \ + MD_TEXT("I'm the author of this blog, and I'm using it to share my") \ + MD_TEXT("researches and my projects.") \ + MD_NEWLINE() \ + MD_TEXT("I'm also the author of the library that powers this blog, ") \ + MD_TEXT("yes you heard right, this blog is powered by a ") \ + MD_BOLD("shared object") \ + MD_TEXT(" that I wrote in C!") \ + MD_NEWLINE() \ + MD_TEXT("You can find the source code of this blog on my ") \ + MD_LINK("git repository", "https://cgit.libdzonerzy.so/libdzonerzy.so/tree") \ + MD_TEXT(".") \ + MD_NEWLINE() \ + MD_SECTION1("Projects") \ + MD_TEXT("Here is a list of my projects, that I've made in my spare time, some of them are ") \ + MD_TEXT("still in development, but I'm working hard to finish them.") \ + MD_NEWLINE() \ + MD_LIST(MD_BOLD(MD_LINK("libdzonerzy.so", "https://cgit.libdzonerzy.so/libdzonersy.so/tree/"))) \ + MD_TEXT(" -- libdzonerzy.so is a shared object that I wrote in C, it's a library ") \ + MD_TEXT("that I use to power this blog.") \ + MD_NEWLINE() \ + MD_LIST(MD_BOLD(MD_LINK("CaptainHook", "https://cgit.libdzonerzy.so/captainhook/tree/"))) \ + MD_TEXT(" -- CaptainHook is the next-gen hooking framework, it's a library that ") \ + MD_TEXT("allow to hook functions in a very easy way without knowing the function prototype.") \ + MD_NEWLINE() \ + MD_SECTION1("FAQ") \ + MD_TEXT("Here is a list of frequently asked questions, if you have any other questions ") \ + MD_TEXT("feel free to contact me at ") \ + MD_BOLD("dzonerzy[at]gmail.com") \ + MD_TEXT(".") \ + MD_NEWLINE() \ + MD_SECTION4(" ") \ + MD_NEWLINE() \ + MD_LIST(MD_BOLD("Q: ") MD_TEXT("Why this blog is so ugly?")) \ + MD_NEWLINE() \ + MD_TEXT("Because I'm not a web designer, I'm a developer, and I'm not good at ") \ + MD_TEXT("designing things, but I'm working on it, I promise.") \ + MD_NEWLINE() \ + MD_LIST(MD_BOLD("Q: ") MD_TEXT("Why this blog is so fast?")) \ + MD_NEWLINE() \ + MD_TEXT("Actually this is a static website, so it's very fast.") \ + MD_NEWLINE() \ + MD_LIST(MD_BOLD("Q: ") MD_TEXT("What do you do for living?")) \ + MD_NEWLINE() \ + MD_TEXT("I'm the lead offensive security researcher at ") \ + MD_LINK("Sysdig", "https://sysdig.com") \ + MD_TEXT(", a cloud security company.") \ + MD_NEWLINE() \ + MD_LIST(MD_BOLD("Q: ") MD_TEXT("Is Sysdig hiring?")) \ + MD_NEWLINE() \ + MD_TEXT("Yes, we are always looking for new talents, if you are interested in working ") \ + MD_TEXT("with me, feel free to send your resume at ... try to find it :D , ") \ + MD_TEXT("hint (") \ + MD_BOLD("libdzonerzy.so") \ + MD_TEXT(").") \ + MD_NEWLINE() \ + MD_TEXT("Michele is gayyyyyyyyyyyyyyyyyyyyyyyy") + +#define FOOTER_MD \ + MD_RAWTAG("footer", "" \ + "" \ + "Powered by libdzonerzy.so" \ + "" \ + "
" \ + "made with
by dzonerzy
") \ + MD_NEWLINE() + +#define SITEMAP_XML \ + XML_URLSET() \ + XML_URL("https://libdzonerzy.so/index.html") \ + XML_URL("https://libdzonerzy.so/sitemap.xml") \ + XML_END_URLSET() + +int main(int argc, char **argv) +{ + fprintf(stdout, "https://libdzonerzy.so\n"); + return EXIT_SUCCESS; +} + +// resources +SECTION("main.css") +static const char maincss[] = MAIN_CSS; +SECTION("pages/index.md") +static const char indexmd[] = INDEX_MD; +SECTION("footer.html") +static const char footermd[] = FOOTER_MD; +SECTION("sitemap.xml") +static const char sitemapxml[] = SITEMAP_XML; \ No newline at end of file diff --git a/src/libdzonerzy.so.h b/src/libdzonerzy.so.h new file mode 100644 index 0000000..af30960 --- /dev/null +++ b/src/libdzonerzy.so.h @@ -0,0 +1,282 @@ +/* + +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. + 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 +#include +#include +#include +#include +#include +#include +#include +#include + +#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) "\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 "" +// 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() "\n" +// url macros +#define XML_URL(x) " \n" \ + " " x "\n" \ + " " __DATE__ "\n" \ + " \n" +// end urlset macros +#define XML_END_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*/ \ No newline at end of file diff --git a/src/res.h b/src/res.h new file mode 100644 index 0000000..aa7489e --- /dev/null +++ b/src/res.h @@ -0,0 +1 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..e203fa8 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,13 @@ +CC = $(shell which gcc) + +EMBED_CFLAGS = -Wall -Wextra -Werror -pedantic -g -O0 +EMBED_LDFLAGS = +TOOLSFOLDER = tools/bin + +embed: tools/embed/embed.c + $(CC) $(EMBED_CFLAGS) $(EMBED_LDFLAGS) -o $(TOOLSFOLDER)/$@ $^ + +all: embed + +clean-tools: + rm -f $(TOOLSFOLDER)/* \ No newline at end of file diff --git a/tools/bin/.gitkeep b/tools/bin/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tools/embed/embed.c b/tools/embed/embed.c new file mode 100644 index 0000000..be1114e --- /dev/null +++ b/tools/embed/embed.c @@ -0,0 +1,333 @@ +/* + +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. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +*/ + +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include + +#define VERSION "0.0.1" + +typedef struct _embed +{ + char *input; + char *output; + char *format; + int verbose; + int quiet; +} embed_t; + +static void usage(char **argv); +static void version(char **argv); +static int embed(embed_t *args); +static char *clean_filename(char *filename); + +int main(int argc, char **argv) +{ + int c = 0; + embed_t args = {0}; + + while ((c = getopt(argc, argv, "hvi:o:f:Vq")) != -1) + { + switch (c) + { + case 'h': + usage(argv); + exit(EXIT_SUCCESS); + break; + case 'v': + version(argv); + exit(EXIT_SUCCESS); + break; + case 'i': + args.input = optarg; + break; + case 'o': + args.output = optarg; + break; + case 'f': + // check -f=c -f=h handle equal sign and skip it + if (optarg[0] == '=') + { + optarg++; + } + + switch (optarg[0]) + { + case 'c': + args.format = "c"; + break; + case 'h': + + args.format = "h"; + break; + default: + usage(argv); + exit(EXIT_FAILURE); + break; + } + break; + case 'V': + args.verbose = 1; + break; + case 'q': + args.quiet = 1; + break; + default: + usage(argv); + exit(EXIT_FAILURE); + break; + } + } + + return embed(&args); +} + +void usage(char **argv) +{ + char *name = strrchr(argv[0], '/'); + if (name == NULL) + { + name = argv[0]; + } + else + { + name++; + } + + printf("Usage: %s [OPTIONS]\n", name); + printf("Embed a file into a C source file.\n"); + printf("\n"); + printf("Options:\n"); + printf(" -h, --help Print this help and exit\n"); + printf(" -v, --version Print version and exit\n"); + printf(" -i, --input=FILE Input file\n"); + printf(" -o, --output=FILE Output file\n"); + printf(" -f, --format=FORMAT Output format (c, h)\n"); + printf(" -V, --verbose Verbose output\n"); + printf(" -q, --quiet Quiet output\n"); + printf("\n"); +} + +void version(char **argv) +{ + char *name = strrchr(argv[0], '/'); + if (name == NULL) + { + name = argv[0]; + } + else + { + name++; + } + + printf("%s version %s\n", name, VERSION); + printf("\n"); +} + +char *clean_filename(char *filename) +{ + char *p = filename; + while (*p) + { + // replace with _ + // replace symbols with _ + // replace - with _ + // replace . with _ + // all characters to uppercase + + switch (*p) + { + case ' ': + case '-': + case '.': + case '/': + case '\\': + case ':': + case ';': + case ',': + case '\'': + case '"': + case '[': + case ']': + case '{': + case '}': + case '(': + case ')': + case '<': + case '>': + case '?': + case '!': + case '@': + case '#': + case '$': + case '%': + case '^': + case '&': + case '*': + case '+': + case '=': + case '|': + case '`': + case '~': + *p = '_'; + break; + default: + *p = toupper(*p); + break; + } + + p++; + } + + return filename; +} + +int embed(embed_t *args) +{ + if (args->input == NULL) + { + fprintf(stderr, "Input file not specified\n"); + return EXIT_FAILURE; + } + + if (args->output == NULL) + { + fprintf(stderr, "Output file not specified\n"); + return EXIT_FAILURE; + } + + if (args->format == NULL) + { + fprintf(stderr, "Output format not specified\n"); + return EXIT_FAILURE; + } + + if (args->verbose) + { + printf("Input file: %s\n", args->input); + printf("Output file: %s\n", args->output); + printf("Output format: %s\n", args->format); + } + + FILE *input = fopen(args->input, "rb"); + + if (input == NULL) + { + if (!args->quiet) + fprintf(stderr, "Unable to open input file: %s\n", args->input); + return EXIT_FAILURE; + } + + FILE *output = fopen(args->output, "w"); + + if (output == NULL) + { + if (!args->quiet) + fprintf(stderr, "Unable to open output file: %s\n", args->output); + return EXIT_FAILURE; + } + + char *name_only = strdup(args->input); + name_only = strrchr(name_only, '/'); + if (name_only == NULL) + { + name_only = args->input; + } + else + { + name_only++; + } + + char *input_name = strdup(args->input); + input_name = strrchr(input_name, '/'); + if (input_name == NULL) + { + input_name = args->input; + } + else + { + input_name++; + } + + input_name = clean_filename(input_name); + + fprintf(output, "/*\n"); + fprintf(output, " * This file was automatically generated by embed - DO NOT EDIT\n"); + fprintf(output, " * https://libdzonerzy.so\n"); + fprintf(output, " */\n"); + fprintf(output, "\n"); + + if (args->format[0] == 'c') + { + fprintf(output, "__attribute__((section(\"embed/%s\"))) const unsigned char %s[] = {\n", name_only, input_name); + } + else if (args->format[0] == 'h') + { + fprintf(output, "#pragma once\n"); + fprintf(output, "#ifndef %s_H\n", input_name); + fprintf(output, "__attribute__((section(\"embed/%s\"))) const unsigned char %s[] = {\n", name_only, input_name); + } + + int c = 0; + int i = 0; + + while ((c = fgetc(input)) != EOF) + { + if (i == 0) + { + fprintf(output, " "); + } + + fprintf(output, "0x%02X", c); + + if (i < 15) + { + fprintf(output, ", "); + } + else + { + fprintf(output, ",\n"); + i = -1; + } + + i++; + } + + if (i > 0) + { + fprintf(output, "\n"); + } + + fprintf(output, " 0x00,\n"); + fprintf(output, "};\n"); + + if (args->format[0] == 'h') + { + fprintf(output, "#endif /* %s_H */\n", input_name); + } + + fclose(input); + fclose(output); + + if (!args->quiet) + printf("Generated resource '%s'\n", input_name); + + return EXIT_SUCCESS; +} \ No newline at end of file