From 74111495622a13104b4f6500f6dc8e9db6aec9bd Mon Sep 17 00:00:00 2001 From: David Moc Date: Thu, 19 Nov 2026 02:10:24 +0100 Subject: Hello World! --- .gitignore | 3 + Makefile | 55 +++++ README.org | 89 ++++++++ ccdjit.h | 601 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ compile_flags.txt | 4 + lang.h | 119 +++++++++++ langs/english.c | 80 ++++++++ langs/pinyin.c | 74 +++++++ main.c | 190 +++++++++++++++++ samples.txt | 26 +++ 10 files changed, 1241 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.org create mode 100644 ccdjit.h create mode 100644 compile_flags.txt create mode 100644 lang.h create mode 100644 langs/english.c create mode 100644 langs/pinyin.c create mode 100644 main.c create mode 100644 samples.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..24a74a6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.o +*.d +/qiuyu diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1e3145e --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +CC ?= cc + +# Path to your CCDJIT checkout (needs bin/libccdjit.a built already) +CCDJIT ?= $(HOME)/opt/ccdjit + +CFLAGS ?= -Wall -Wextra -O2 +CPPFLAGS += -I$(CCDJIT)/src -MMD -MP +LDFLAGS += -rdynamic +LDLIBS += $(CCDJIT)/bin/libccdjit.a + +# Test cases for `make check`, see README +SAMPLES ?= samples.txt + +TARGET := qiuyu +SRC := main.c +OBJ := $(SRC:.c=.o) +DEP := $(OBJ:.o=.d) + +.PHONY: all check clean + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@ + +%.o: %.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@ + +# Encrypts every line of $(SAMPLES), compares with the expected ciphertext +# if given, then decrypts and compares with the original. +check: $(TARGET) + @fail=0; ok=0; \ + while IFS='|' read -r lang plain cipher; do \ + case "$$lang" in ''|'#'*) continue ;; esac; \ + enc=$$(./$(TARGET) -l "$$lang" -e "$$plain") || { echo "FAIL $$lang: encrypt '$$plain'"; fail=$$((fail+1)); continue; }; \ + if [ -n "$$cipher" ] && [ "$$enc" != "$$cipher" ]; then \ + echo "FAIL $$lang: '$$plain' encrypted to '$$enc', expected '$$cipher'"; fail=$$((fail+1)); continue; \ + fi; \ + dec=$$(./$(TARGET) -l "$$lang" -d "$$enc") || { echo "FAIL $$lang: decrypt '$$enc'"; fail=$$((fail+1)); continue; }; \ + if [ "$$dec" != "$$plain" ]; then \ + echo "FAIL $$lang: '$$plain' -> '$$enc' -> '$$dec'"; fail=$$((fail+1)); continue; \ + fi; \ + ok=$$((ok+1)); \ + done < $(SAMPLES); \ + for f in langs/*.c; do \ + name=$$(basename $$f .c); \ + grep -q "^$$name|" $(SAMPLES) || echo "WARN $$name: no samples in $(SAMPLES)"; \ + done; \ + echo "$$ok passed, $$fail failed"; \ + [ $$fail -eq 0 ] + +clean: + rm -f $(TARGET) $(OBJ) $(DEP) + +-include $(DEP) diff --git a/README.org b/README.org new file mode 100644 index 0000000..087440d --- /dev/null +++ b/README.org @@ -0,0 +1,89 @@ +#+TITLE: QiuYu +#+AUTHOR: David Moc (no. 243) +#+DESCRIPTION: A project for de/encoding QiuYu, a cypher from the amazing artists *No.006* <3 + +* What is QiuYu? +It's a cypher that's mainly used in the titles of *No.006*'s songs and on their site [[https://no006.org/][no006.org]]. +It's a neat thing that really sparked my curiosity in implementing a tool that could do this for universally any language. + +It works by representing tones via =[1..4]=: +- =u1ch.ing2m.ao4y.en4ch.ao3z= in normal pinyin would be =chu1 ming2 yao4 chen4 zao3=. + +The same can be applied to other languages, e.g. English: +- =Hello world= -> =ello@h.orld@w= +- Side note: the numbers in pinyin function as a delimiter. Because there are no tones nor diacritics in English, =@= is used as one. + +In both cases the first letter (or initial) of a word moves to the back, behind the delimiter, and words are separated by =.= instead of a space. + +These two are already working systems on their site, but I'd like to extend it a bit. + +* Supported languages +- Chinese (pinyin) +- English + +* Usage +#+begin_src sh +./qiuyu [-e | -d] [-l ] +#+end_src + +- =-e= encrypt (default) +- =-d= decrypt +- =-l= language, the name of a file in =langs/= without the extension (default: =english=) + +#+begin_src sh +./qiuyu -l english -e "Hello world" # ello@h.orld@w +./qiuyu -l english -d "ello@h.orld@w" # Hello world +./qiuyu -l pinyin -e "chu1 ming2 yao4" # u1ch.ing2m.ao4y +#+end_src + +Run it from the project root, since =langs/= is looked up relative to the working directory. + +* Building +QiuYu needs [[https://git.cdatgoose.org/ccdjit][CCDJIT]] (the language files are compiled at runtime), built beforehand with =./build.sh release=. + +#+begin_src sh +make CCDJIT=/path/to/ccdjit +make check # loads every langs/*.c once to catch compile errors +#+end_src + +* How languages work +Every file in =langs/= is a language. They are not part of the program: =qiuyu= compiles =langs/.c= with CCDJIT when =-l = is given, so adding a language needs no rebuild. + +A language file includes =lang.h= and defines two functions. Both return a =malloc='d string, or =NULL= on failure: + +#+begin_src c +char *lang_encrypt(const char *input); +char *lang_decrypt(const char *input); +#+end_src + +Do not define =main= in a language file, it is added automatically. + +=lang.h= also provides a small toolkit, since every language boils down to finding a delimiter and reordering the pieces: +- =SB=: a growable string buffer (=sb_init=, =sb_append=, =sb_push=, =sb_take=, =sb_free=) +- =Span=: a view into a string, so pieces can be moved around without copying +- =split_spans= / =sb_join_spans=: split on a delimiter and join back + +* Tests +=make check= runs every sample in =samples.txt= through the matching language file. +Each sample is encrypted, compared with the expected ciphertext (if one is given), decrypted again, and compared with the original. + +A line is =language|plaintext= with an optional third field =|ciphertext=: + +#+begin_src +english|Hello world|ello@h.orld@w +pinyin|ni3 hao3|i3n.ao3h +#+end_src + +- Blank lines and lines starting with =#= are ignored. +- The language is the file name in =langs/= without the extension. +- With a ciphertext the exact output is checked too. Without one, only the round trip is. +- A language file with no samples gets a warning. +- Use about 10 samples per language, and cover its edge cases: digraphs, syllables or words that pass through unchanged, single letters. +- English plaintext must start with a capital and contain no other capitals, because decrypt capitalizes the first letter of the output. + + +* Adding a language +1. Create =langs/.c=. The name may only contain =a-z=, =0-9= and =_=. +2. Write up the rules. +3. Add about 10 samples to =samples.txt= and run =make check=. +4. Add it to the list of supported languages. diff --git a/ccdjit.h b/ccdjit.h new file mode 100644 index 0000000..bf1e239 --- /dev/null +++ b/ccdjit.h @@ -0,0 +1,601 @@ +#ifndef CCDJIT_PUBLIC_H +#define CCDJIT_PUBLIC_H + +/* + * CCDJIT public embedding API + * + * This header is the stable boundary for hosts that want to compile and run C + * code at runtime. The implementation lives in ccdjit.c, but the intended use + * should be clear from this file alone. + * + * Basic use: + * + * ccdjit_context *ctx = ccdjit_context_new(NULL); + * ccdjit_module *module = NULL; + * + * if (ccdjit_compile_string(ctx, source, "plugin.c", &module) == 0) { + * int result = 0; + * ccdjit_module_call_main(module, 0, NULL, &result); + * ccdjit_module_free(module); + * } + * ccdjit_context_free(ctx); + * + * Return convention: + * Functions returning int use 0 for success and -1 for failure unless + * documented otherwise. On failure, call ccdjit_context_last_error(). + * + * Lifetime: + * A context owns options, include paths, registered symbols, and the last + * diagnostic. A module owns generated code or a loaded CCDJIT binary. + * Modules keep their context state alive, so ccdjit_context_free() may be + * called before ccdjit_module_free(); the context is released when the + * last module using it is freed. + * Function pointers returned by ccdjit_module_symbol() are invalid after + * ccdjit_module_free(). + * + * Runtime policy: + * Module calls, symbol lookup, and emission intentionally consult the + * current context options, sandbox settings, resource limits, registered + * symbols, resolver callback, and loaded libraries. Machine code that + * has already been compiled may contain resolved external addresses; use + * a new module or function-handle replacement when changing symbol + * bindings must affect already compiled call sites. + * + * Threads: + * Public API entry points are internally serialized, and module calls, + * symbol lookup, emission, and lazy compilation take a module lock. Raw + * function pointers returned by ccdjit_module_symbol() are still host + * pointers: do not free or replace their module while those pointers may + * run, and prefer typed handle/call helpers for dynamic bindings. + * + * Safety: + * Normal calls run in the host process. Set sandbox_execution to + * CCDJIT_SANDBOX_PROCESS to run ccdjit_module_call_main() in a child + * process with optional resource, syscall, and filesystem limits. Child + * crashes are reported through ccdjit_diagnostic. + */ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define CCDJIT_VERSION_MAJOR 1 +#define CCDJIT_VERSION_MINOR 3 +#define CCDJIT_VERSION_PATCH 1 +#define CCDJIT_VERSION_STRING "1.3.1" +#define CCDJIT_API_ABI_VERSION 1 +#define CCDJITB2_FORMAT_VERSION 2 +#define CCDJITB2_FLAG_DEBUG_INFO 1u +/* + * CCDJITB2 fixed header layout, in bytes: + * magic "CCDJITB2" (8) + version (4) + flags (4) + text_size (8) + + * data_size (8) + rodata_size (8) + bss_size (8) + symbol_count (4) + + * reloc_count (4) + * Section payloads follow immediately. Every reader must check the file is + * at least this large before parsing the header fields, and the writer must + * emit exactly this many bytes before any payload. + */ +#define CCDJITB2_HEADER_SIZE 56 +#define CCDJITB2_MAGIC_SIZE 8 +#define CCDJITB2_SYMBOL_RECORD_SIZE 32 +#define CCDJITB2_RELOC_RECORD_SIZE 32 + +typedef struct ccdjit_context ccdjit_context; +typedef struct ccdjit_module ccdjit_module; +typedef struct ccdjit_function_handle ccdjit_function_handle; +typedef struct ccdjit_execution_result ccdjit_execution_result; + +typedef struct { + /* Host compiler or linker driver. NULL uses $CC, then cc. */ + const char *driver; + + /* Extra linker arguments such as -L, -l, or -Wl options. */ + const char *const *link_args; + size_t link_arg_count; + + /* Print the host linker command to stderr when nonzero. */ + int verbose; + + /* Wall-clock driver timeout. Zero uses the default. */ + unsigned int timeout_ms; + + /* Captured driver stdout/stderr cap. Zero uses the default. */ + size_t output_limit_bytes; +} ccdjit_link_options; + +/* + * Host symbol callback. Return NULL when the name is unknown. + * + * The returned pointer is used as an external function or object address by + * generated code. Keep any pointed-to object alive while modules using it may + * run. + */ +typedef void *(*ccdjit_symbol_resolver)(const char *name, void *userdata); + +typedef enum { + /* Run generated code directly in the host process. */ + CCDJIT_SANDBOX_OFF = 0, + + /* Run ccdjit_module_call_main() in a forked child process. */ + CCDJIT_SANDBOX_PROCESS = 1, +} ccdjit_sandbox_mode; + +typedef enum { + /* + * Minimal child profile: enough to write the result pipe, write + * captured output, and exit. Unexpected syscalls are trapped. + */ + CCDJIT_SANDBOX_SYSCALL_STRICT = 0, + + /* STRICT plus read/close for simple stdio-style code. */ + CCDJIT_SANDBOX_SYSCALL_STDIO = 1, + + /* STDIO plus mmap/munmap/mprotect/brk/clock_gettime. */ + CCDJIT_SANDBOX_SYSCALL_RUNTIME = 2, + + /* Disable the seccomp filter. Resource limits still apply. */ + CCDJIT_SANDBOX_SYSCALL_PERMISSIVE = 3, +} ccdjit_sandbox_syscall_policy; + +typedef enum { + /* Runtime file syscalls are denied by the syscall policy. */ + CCDJIT_SANDBOX_FS_NONE = 0, + + /* Permit read-only access under explicitly added roots. */ + CCDJIT_SANDBOX_FS_READ_ONLY_ROOTS = 1, + + /* Permit writes only for roots added with writable != 0. */ + CCDJIT_SANDBOX_FS_WRITABLE_ROOTS = 2, +} ccdjit_sandbox_filesystem_policy; + +typedef struct { + /* Allow #include to read files from include paths. */ + int allow_filesystem_includes; + + /* Allow the preprocessor to discover and read host system includes. */ + int allow_system_includes; + + /* Allow executable mappings for JIT code and loaded CCDJIT binaries. */ + int allow_executable_memory; + + /* ccdjit_sandbox_mode. Default is CCDJIT_SANDBOX_OFF. */ + int sandbox_execution; + + /* ccdjit_sandbox_syscall_policy. Default is STRICT. */ + int sandbox_syscalls; + + /* ccdjit_sandbox_filesystem_policy. Default is FS_NONE. */ + int sandbox_filesystem; + + /* + * In sandbox mode, default dlsym lookup is disabled unless this is set. + * Prefer explicit ccdjit_context_register_symbol() capabilities. + */ + int sandbox_allow_implicit_symbols; + + /* Wall-clock timeout for one sandboxed main call. Zero means no limit. + */ + unsigned int sandbox_timeout_ms; + + /* RLIMIT_CPU for sandboxed calls, rounded up to seconds by the OS. */ + unsigned int sandbox_cpu_time_ms; + + /* RLIMIT_AS for sandboxed calls. Zero means keep the host default. */ + size_t sandbox_address_space_bytes; + + /* RLIMIT_STACK for sandboxed calls. Zero means keep the host default. + */ + size_t sandbox_stack_bytes; + + /* RLIMIT_FSIZE for sandboxed calls. Zero means keep the host default. + */ + size_t sandbox_file_size_bytes; + + /* Captured stdout/stderr cap for sandboxed calls. Zero means 64 KiB. + */ + size_t sandbox_output_limit_bytes; + + /* Emit and retain optional debug/source mapping metadata. */ + int debug_info; + + /* + * Preprocessor output byte cap. Zero uses the default currently 128 + * MiB. + */ + size_t preprocessor_output_limit_bytes; + + /* Macro expansion recursion cap. Zero uses the default. */ + unsigned int preprocessor_macro_depth_limit; + + /* Nested include cap. Zero uses the default. */ + unsigned int preprocessor_include_depth_limit; + + /* + * Optional compiler driver used for host system include and builtin + * macro probes. NULL auto-detects from $CC, then common cc drivers. + */ + const char *preprocessor_compiler; + + /* Wall-clock timeout for one compiler probe. Zero uses the default. */ + unsigned int preprocessor_probe_timeout_ms; + + /* Captured compiler-probe output cap. Zero uses the default. */ + size_t preprocessor_probe_output_limit_bytes; +} ccdjit_options; + +typedef struct { + /* Compiler phase such as LEXER, PARSER, JIT, API, or SANDBOX. */ + const char *phase; + + /* Source file, binary path, symbol name, or "". */ + const char *filename; + + /* Human-readable error text owned by the context. */ + const char *message; + + /* Optional source excerpt for lexer/parser/type diagnostics. */ + const char *source_line; + const char *caret_line; + + /* One-based line and column when available, otherwise zero. */ + int line; + int column; + + /* ccdjit_diagnostic_code plus optional code-specific detail. */ + int code; + int subcode; + + /* Raw child wait status or errno-style value for sandbox/API failures. + */ + int status; + + /* Signal that terminated the sandbox child, or zero. */ + int signal_number; + + /* Faulting address for sandboxed crash diagnostics, when available. */ + int has_fault_address; + uintptr_t fault_address; +} ccdjit_diagnostic; + +typedef enum { + CCDJIT_DIAG_NONE = 0, + CCDJIT_DIAG_LEXER = 1, + CCDJIT_DIAG_PARSER = 2, + CCDJIT_DIAG_PREPROCESSOR = 3, + CCDJIT_DIAG_TYPE = 4, + CCDJIT_DIAG_JIT = 5, + CCDJIT_DIAG_UNRESOLVED_SYMBOL = 6, + CCDJIT_DIAG_SANDBOX = 7, + CCDJIT_DIAG_SANDBOX_TIMEOUT = 8, + CCDJIT_DIAG_SANDBOX_FORBIDDEN_SYSCALL = 9, + CCDJIT_DIAG_SANDBOX_CRASH = 10, + CCDJIT_DIAG_INVALID_OPTIONS = 11, + CCDJIT_DIAG_INVALID_ARGUMENT = 12, + CCDJIT_DIAG_UNSUPPORTED_ABI = 13, + CCDJIT_DIAG_MALFORMED_BINARY = 14, + CCDJIT_DIAG_HOST_LINKER = 15, + CCDJIT_DIAG_OUT_OF_MEMORY = 16, +} ccdjit_diagnostic_code; + +typedef enum { + CCDJIT_SYMBOL_UNKNOWN = 0, + CCDJIT_SYMBOL_FUNCTION = 1, + CCDJIT_SYMBOL_OBJECT = 2, +} ccdjit_symbol_kind; + +typedef enum { + CCDJIT_TYPE_INT = 0, + CCDJIT_TYPE_CHAR = 1, + CCDJIT_TYPE_BOOL = 2, + CCDJIT_TYPE_VOID = 3, + CCDJIT_TYPE_FLOAT = 4, + CCDJIT_TYPE_DOUBLE = 5, + CCDJIT_TYPE_LONG = 6, + CCDJIT_TYPE_SHORT = 7, + CCDJIT_TYPE_STRUCT = 8, + CCDJIT_TYPE_ENUM = 9, + CCDJIT_TYPE_UNION = 10, + CCDJIT_TYPE_FLOAT128 = 11, +} ccdjit_type_base; + +typedef struct { + int has_type; + int base; + int pointer_level; + int array_size; + int is_unsigned; + int is_complex; + int is_atomic; +} ccdjit_type_info; + +typedef struct { + int kind; + int has_signature; + ccdjit_type_info type; + ccdjit_type_info return_type; + size_t parameter_count; + int is_variadic; + size_t size; +} ccdjit_symbol_info; + +typedef struct { + int process; + int seccomp; + int landlock; +} ccdjit_sandbox_capabilities; + +typedef struct { + const char *filename; + const char *function; + int line; + int column; + uintptr_t address; + size_t size; +} ccdjit_debug_location; + +/* + * Create a compiler context. Passing NULL uses permissive CLI-like defaults. + * Invalid options return NULL; call ccdjit_context_last_error(NULL) for the + * thread-local creation diagnostic. + */ +ccdjit_context *ccdjit_context_new(const ccdjit_options *options); + +int ccdjit_version_major(void); +int ccdjit_version_minor(void); +int ccdjit_version_patch(void); +const char *ccdjit_version_string(void); +int ccdjit_api_abi_version(void); +int ccdjit_binary_format_version(void); +const char *ccdjit_git_commit(void); +int ccdjit_sandbox_capabilities_query(ccdjit_sandbox_capabilities *out); + +/* Free the context and all context-owned diagnostics/options/lists. */ +void ccdjit_context_free(ccdjit_context *ctx); + +/* Add a preprocessor include search path, like -I. */ +int ccdjit_context_add_include_path(ccdjit_context *ctx, const char *path); + +/* Restrict filesystem includes to this root when include roots are used. */ +int ccdjit_context_add_include_root(ccdjit_context *ctx, const char *path); + +/* Add a preprocessor definition, like -DNAME or -DNAME=value. */ +int ccdjit_context_add_define(ccdjit_context *ctx, const char *define); + +/* Grant generated code an explicit external symbol capability. */ +int ccdjit_context_register_symbol(ccdjit_context *ctx, const char *name, + void *addr); + +/* Add an explicit shared library path for external symbol lookup. */ +int ccdjit_context_add_library(ccdjit_context *ctx, const char *path); + +/* Install a fallback external symbol resolver. */ +void ccdjit_context_set_symbol_resolver(ccdjit_context *ctx, + ccdjit_symbol_resolver resolver, void *userdata); + +/* Update sandbox mode, syscall policy, filesystem policy, and symbol policy. */ +int ccdjit_context_set_sandbox_options(ccdjit_context *ctx, int mode, + int syscall_policy, int filesystem_policy, int allow_implicit_symbols); + +/* Update all sandbox resource limits in one call. */ +int ccdjit_context_set_sandbox_limits(ccdjit_context *ctx, + unsigned int timeout_ms, unsigned int cpu_time_ms, + size_t address_space_bytes, size_t stack_bytes, size_t file_size_bytes, + size_t output_limit_bytes); + +/* Update preprocessor resource limits. Passing zero keeps the default. */ +int ccdjit_context_set_preprocessor_limits(ccdjit_context *ctx, + size_t output_limit_bytes, unsigned int macro_depth, + unsigned int include_depth); + +/* Select the compiler driver used by system include and builtin macro probes. + */ +int ccdjit_context_set_preprocessor_compiler(ccdjit_context *ctx, + const char *compiler); + +/* Update compiler-probe timeout/output limits. Passing zero keeps defaults. */ +int ccdjit_context_set_preprocessor_probe_limits(ccdjit_context *ctx, + unsigned int timeout_ms, size_t output_limit_bytes); + +/* Free this thread's cached compiler probe results and system include paths. */ +void ccdjit_preprocessor_free_probe_cache(void); + +/* Enable or disable executable memory for future compile/load/call paths. */ +int ccdjit_context_set_executable_memory(ccdjit_context *ctx, int allow); + +/* Enable or disable optional debug/source mapping metadata. */ +int ccdjit_context_set_debug_info(ccdjit_context *ctx, int enable); + +/* + * Add a Landlock filesystem root for sandboxed execution. + * + * writable is only honored when sandbox_filesystem is + * CCDJIT_SANDBOX_FS_WRITABLE_ROOTS. On systems without Landlock, requesting a + * root-based filesystem policy fails while entering the sandbox. + */ +int ccdjit_context_add_sandbox_filesystem_root(ccdjit_context *ctx, + const char *path, int writable); + +/* + * Return the last error for this context. Passing NULL returns the + * thread-local diagnostic for context creation or NULL-context API failures. + * + * The result is a per-thread snapshot taken under the internal API lock, so it + * may be read safely while another thread compiles on the same context. The + * snapshot stays valid until the same accessor is called again on this thread + * (or the thread exits); copy the fields if they must outlive that window. + */ +const ccdjit_diagnostic *ccdjit_context_last_error(ccdjit_context *ctx); + +/* + * Return captured sandbox stdout/stderr from the last sandboxed main call. + * Like ccdjit_context_last_error(), the result is a per-thread snapshot that + * stays valid until the same accessor is called again on this thread. + */ +const char *ccdjit_context_last_stdout(ccdjit_context *ctx); +const char *ccdjit_context_last_stderr(ccdjit_context *ctx); + +/* + * Owned execution result snapshot for bindings and concurrent hosts. + * + * Result objects keep copies of the call status, integer result, diagnostic, + * captured stdout, and captured stderr. They are not invalidated by later + * calls on the same context. + */ +void ccdjit_execution_result_free(ccdjit_execution_result *result); +int ccdjit_execution_result_status(const ccdjit_execution_result *result); +int ccdjit_execution_result_has_value(const ccdjit_execution_result *result); +int ccdjit_execution_result_value(const ccdjit_execution_result *result); +const ccdjit_diagnostic *ccdjit_execution_result_diagnostic( + const ccdjit_execution_result *result); +const char *ccdjit_execution_result_stdout( + const ccdjit_execution_result *result); +const char *ccdjit_execution_result_stderr( + const ccdjit_execution_result *result); +int ccdjit_execution_result_signal(const ccdjit_execution_result *result); +int ccdjit_execution_result_has_fault_address( + const ccdjit_execution_result *result); +uintptr_t ccdjit_execution_result_fault_address( + const ccdjit_execution_result *result); + +/* Compile a source file into a live JIT module. */ +int ccdjit_compile_file(ccdjit_context *ctx, const char *path, + ccdjit_module **out); + +/* Compile a source string into a live JIT module. */ +int ccdjit_compile_string(ccdjit_context *ctx, const char *source, + const char *filename, ccdjit_module **out); + +/* + * Compile a source file into a live JIT module without requiring main(). + * Use ccdjit_module_symbol() to find and call exported config/plugin symbols. + */ +int ccdjit_compile_file_module(ccdjit_context *ctx, const char *path, + ccdjit_module **out); + +/* + * Compile a source string into a live JIT module without requiring main(). + * Use ccdjit_module_symbol() to find and call exported config/plugin symbols. + */ +int ccdjit_compile_string_module(ccdjit_context *ctx, const char *source, + const char *filename, ccdjit_module **out); + +/* Convenience wrapper: compile a source string, call main, free the module. */ +int ccdjit_eval_string(ccdjit_context *ctx, const char *source, + const char *filename, int argc, char **argv, int *result_out); + +/* + * Find a function or object symbol in a module. + * + * Calling the returned function pointer runs in the host process even when the + * context has sandbox_execution enabled. Use ccdjit_module_call_main() for + * sandboxed execution. + */ +void *ccdjit_module_symbol(ccdjit_module *module, const char *name); + +/* + * Inspect a module symbol without forcing the caller to cast blindly. + * + * JIT functions expose return type, parameter count, variadic flag, and + * per-parameter metadata through ccdjit_module_function_parameter_type(). + * Object symbols expose their object type and size. Loaded CCDJITB2 binaries + * expose function/object kind and size but not full C signatures. + */ +int ccdjit_module_symbol_info(ccdjit_module *module, const char *name, + ccdjit_symbol_info *out); + +int ccdjit_module_function_parameter_type(ccdjit_module *module, + const char *name, size_t index, ccdjit_type_info *out); + +/* + * Return a callable function pointer only when the JIT signature exactly + * matches the expected return and parameter types. + */ +void *ccdjit_module_function_symbol_checked(ccdjit_module *module, + const char *name, const ccdjit_type_info *return_type, + const ccdjit_type_info *parameter_types, size_t parameter_count, + int allow_variadic); + +/* Map a live JIT instruction address to lightweight module-owned debug info. */ +int ccdjit_module_debug_location(ccdjit_module *module, const void *address, + ccdjit_debug_location *out); + +/* Call module main(argc, argv). Honors CCDJIT_SANDBOX_PROCESS. */ +int ccdjit_module_call_main(ccdjit_module *module, int argc, char **argv, + int *result_out); +int ccdjit_module_call_main_result(ccdjit_module *module, int argc, char **argv, + ccdjit_execution_result **out); + +/* + * Call an exported JIT function with signature int name(int). + * + * Honors CCDJIT_SANDBOX_PROCESS like ccdjit_module_call_main(). Other + * signatures and binary modules are rejected with a diagnostic. + */ +int ccdjit_module_call_i32(ccdjit_module *module, const char *symbol, int arg, + int *result_out); +int ccdjit_module_call_i32_result(ccdjit_module *module, const char *symbol, + int arg, ccdjit_execution_result **out); + +/* + * Compile and publish a named function behind a stable handle. + * + * The initial source and every replacement must define the same function + * signature. Calls through the handle do not expose raw function pointers, so + * replacements can wait for active callers and then free old generated code. + */ +int ccdjit_function_handle_new(ccdjit_context *ctx, const char *source, + const char *filename, const char *symbol, ccdjit_function_handle **out); + +/* + * Compile replacement source and atomically publish it on success. + * + * On compile failure or signature mismatch the old implementation remains + * active and the context diagnostic describes the failure. + */ +int ccdjit_function_handle_replace(ccdjit_function_handle *handle, + const char *source, const char *filename); + +/* Call a handle whose published function has signature int name(int). */ +int ccdjit_function_handle_call_i32(ccdjit_function_handle *handle, int arg, + int *result_out); + +/* Free a function handle and the currently published implementation. */ +void ccdjit_function_handle_free(ccdjit_function_handle *handle); + +/* Emit textual assembly-like bytes for inspection. */ +int ccdjit_module_emit_assembly(ccdjit_module *module, FILE *out); + +/* Emit a CCDJITB2 binary image. */ +int ccdjit_module_emit_binary(ccdjit_module *module, FILE *out); + +/* Emit an ELF64 relocatable object. */ +int ccdjit_module_emit_object(ccdjit_module *module, FILE *out); + +/* + * Emit a linked host executable. + * + * This depends on the host compiler driver. It is intended for trusted build + * paths, not sandboxed execution. + */ +int ccdjit_module_emit_executable_file(ccdjit_module *module, const char *path, + const ccdjit_link_options *options); + +/* Emit a CCDJITB2 binary image directly to a path. */ +int ccdjit_module_emit_binary_file(ccdjit_module *module, const char *path); + +/* Load a CCDJITB2 binary image into executable memory. */ +int ccdjit_module_load_binary(ccdjit_context *ctx, const char *path, + ccdjit_module **out); + +/* Free generated code, loaded binary storage, AST, and module-owned source. */ +void ccdjit_module_free(ccdjit_module *module); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..9554002 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,4 @@ +-std=gnu11 +-Wall +-Wextra +-I. diff --git a/lang.h b/lang.h new file mode 100644 index 0000000..64589db --- /dev/null +++ b/lang.h @@ -0,0 +1,119 @@ +#ifndef LANG_H +#define LANG_H + +#include +#include +#include + +/* Every file in langs/ must define both. Return a malloc'd string, + or NULL on failure. */ +char *lang_encrypt(const char *input); +char *lang_decrypt(const char *input); + + +/* -------------------------------------------------- */ + + +typedef struct StringBuffer { + char *data; /* always NUL-terminated once allocated */ + size_t len; + size_t cap; +} SB; + +/* A non-owning view into some other string. Not NUL-terminated. */ +typedef struct Span { + const char *ptr; + size_t len; +} Span; + +static void sb_init(SB *sb) { + sb->data = NULL; + sb->len = 0; + sb->cap = 0; +} + +static void sb_free(SB *sb) { + free(sb->data); + sb_init(sb); +} + +/* Ensure room for `extra` more bytes plus the NUL. 0 on success, -1 on failure. */ +static int sb_reserve(SB *sb, size_t extra) { + if (extra >= SIZE_MAX - sb->len) return -1; + size_t need = sb->len + extra + 1; + if (need <= sb->cap) return 0; + + size_t cap = sb->cap ? sb->cap : 16; + while (cap < need) { + if (cap > SIZE_MAX / 2) { cap = need; break; } + cap *= 2; + } + char *p = realloc(sb->data, cap); + if (!p) return -1; + sb->data = p; + sb->cap = cap; + return 0; +} + +static int sb_append_n(SB *sb, const char *s, size_t n) { + if (sb_reserve(sb, n) != 0) return -1; + if (n) memcpy(sb->data + sb->len, s, n); + sb->len += n; + sb->data[sb->len] = '\0'; + return 0; +} + +static int sb_append(SB *sb, const char *s) { + return sb_append_n(sb, s, strlen(s)); +} + +static int sb_push(SB *sb, char c) { + return sb_append_n(sb, &c, 1); +} + +/* Hand the contents to the caller (free() it) and reset the buffer. + Always returns a valid string ("" if nothing was appended), NULL only on OOM. */ +static char *sb_take(SB *sb) { + if (sb_reserve(sb, 0) != 0) return NULL; + sb->data[sb->len] = '\0'; + char *out = sb->data; + sb_init(sb); + return out; +} + +/* Split on every `delim`. Empty fields are kept, so joining the spans + with the same delim gives back the original string exactly. + Returns the span count (always >= 1), or 0 on OOM. Caller frees *out. */ +static size_t split_spans(const char *s, char delim, Span **out) { + size_t n = 1; + for (const char *p = s; *p; p++) + if (*p == delim) n++; + + Span *spans = malloc(n * sizeof(Span)); + if (!spans) return 0; + + size_t i = 0; + const char *start = s; + for (const char *p = s; ; p++) { + if (*p == delim || *p == '\0') { + spans[i].ptr = start; + spans[i].len = (size_t)(p - start); + i++; + start = p + 1; + if (*p == '\0') break; + } + } + *out = spans; + return n; +} + +/* Append spans[0..n) separated by `delim`. */ +static int sb_join_spans(SB *sb, const Span *spans, size_t n, char delim) { + for (size_t i = 0; i < n; i++) { + if (i > 0 && sb_push(sb, delim) != 0) return -1; + if (sb_append_n(sb, spans[i].ptr, spans[i].len) != 0) return -1; + } + return 0; +} + +#endif diff --git a/langs/english.c b/langs/english.c new file mode 100644 index 0000000..966b2c9 --- /dev/null +++ b/langs/english.c @@ -0,0 +1,80 @@ +#include +#include "lang.h" + +static const char DIGRAPHS[] = "thsh"; + +static size_t first_letter_len(Span w) { + if (w.len == 0) return 0; + if (w.len >= 2) { + char a = (char)tolower((unsigned char)w.ptr[0]); + char b = (char)tolower((unsigned char)w.ptr[1]); + for (size_t i = 0; DIGRAPHS[i]; i += 2) + if (a == DIGRAPHS[i] && b == DIGRAPHS[i + 1]) return 2; + } + return 1; +} + +static int encrypt_word(SB *sb, Span w) { + size_t p = first_letter_len(w); + if (p == 0) return 0; + + if (sb_append_n(sb, w.ptr + p, w.len - p) != 0) return -1; + if (sb_push(sb, '@') != 0) return -1; + for (size_t i = 0; i < p; i++) + if (sb_push(sb, (char)tolower((unsigned char)w.ptr[i])) != 0) return -1; + return 0; +} + +static int decrypt_word(SB *sb, Span t) { + if (t.len == 0) return 0; + + size_t at = t.len; + for (size_t i = t.len; i-- > 0; ) { + if (t.ptr[i] == '@') { at = i; break; } + } + + if (at == t.len || at + 1 == t.len) + return sb_append_n(sb, t.ptr, t.len); + + if (sb_append_n(sb, t.ptr + at + 1, t.len - at - 1) != 0) return -1; + return sb_append_n(sb, t.ptr, at); +} + +char *lang_encrypt(const char *input) { + Span *words = NULL; + size_t n = split_spans(input, ' ', &words); + if (n == 0) return NULL; + + SB sb; + sb_init(&sb); + for (size_t i = 0; i < n; i++) { + if ((i > 0 && sb_push(&sb, '.') != 0) || encrypt_word(&sb, words[i]) != 0) { + sb_free(&sb); + free(words); + return NULL; + } + } + free(words); + return sb_take(&sb); +} + +char *lang_decrypt(const char *input) { + Span *tokens = NULL; + size_t n = split_spans(input, '.', &tokens); + if (n == 0) return NULL; + + SB sb; + sb_init(&sb); + for (size_t i = 0; i < n; i++) { + if ((i > 0 && sb_push(&sb, ' ') != 0) || decrypt_word(&sb, tokens[i]) != 0) { + sb_free(&sb); + free(tokens); + return NULL; + } + } + free(tokens); + + if (sb.len > 0) + sb.data[0] = (char)toupper((unsigned char)sb.data[0]); + return sb_take(&sb); +} diff --git a/langs/pinyin.c b/langs/pinyin.c new file mode 100644 index 0000000..c6d7f29 --- /dev/null +++ b/langs/pinyin.c @@ -0,0 +1,74 @@ +#include +#include "lang.h" + +/* Longest first, so "zh" is tried before "z". */ +static const char *INITIALS[] = { + "zh", "ch", "sh", + "b", "p", "m", "f", "d", "t", "n", "l", "g", "k", "h", + "j", "q", "x", "r", "z", "c", "s", "y", "w", + NULL +}; + +static int is_digit(char c) { return c >= '0' && c <= '9'; } + +/* Length of the initial consonant at the start of the syllable, 0 if none. */ +static size_t initial_len(Span w) { + for (size_t i = 0; INITIALS[i]; i++) { + size_t n = strlen(INITIALS[i]); + if (w.len < n) continue; + size_t k = 0; + while (k < n && tolower((unsigned char)w.ptr[k]) == INITIALS[i][k]) k++; + if (k == n) return n; + } + return 0; +} + +static size_t find_digit(Span w) { + for (size_t i = 0; i < w.len; i++) + if (is_digit(w.ptr[i])) return i; + return w.len; /* w.len means "none" */ +} + +/* "chu1" -> "u1ch", "yao4" -> "ao4y". Syllables with no initial ("an4") or no + tone digit ("de") pass through, because decrypt couldn't find the split. */ +static int encrypt_word(SB *sb, Span w) { + size_t p = initial_len(w); + if (p == 0 || find_digit(w) == w.len) + return sb_append_n(sb, w.ptr, w.len); + + if (sb_append_n(sb, w.ptr + p, w.len - p) != 0) return -1; + return sb_append_n(sb, w.ptr, p); +} + +/* "u1ch" -> "chu1": everything after the first digit moves back to the front. */ +static int decrypt_word(SB *sb, Span t) { + size_t d = find_digit(t); + if (d == t.len) + return sb_append_n(sb, t.ptr, t.len); + + if (sb_append_n(sb, t.ptr + d + 1, t.len - d - 1) != 0) return -1; + return sb_append_n(sb, t.ptr, d + 1); +} + +/* Split on `from`, run `fn` on each piece, join with `to`. */ +static char *translate(const char *input, char from, char to, + int (*fn)(SB *, Span)) { + Span *parts = NULL; + size_t n = split_spans(input, from, &parts); + if (n == 0) return NULL; + + SB sb; + sb_init(&sb); + for (size_t i = 0; i < n; i++) { + if ((i > 0 && sb_push(&sb, to) != 0) || fn(&sb, parts[i]) != 0) { + sb_free(&sb); + free(parts); + return NULL; + } + } + free(parts); + return sb_take(&sb); +} + +char *lang_encrypt(const char *input) { return translate(input, ' ', '.', encrypt_word); } +char *lang_decrypt(const char *input) { return translate(input, '.', ' ', decrypt_word); } diff --git a/main.c b/main.c new file mode 100644 index 0000000..6f39821 --- /dev/null +++ b/main.c @@ -0,0 +1,190 @@ +#include +#include +#include +#include +#include "ccdjit.h" + +typedef struct Ops { + enum Way { Encrypt, Decrypt } way; + const char *language; +} Ops; + +typedef char *(*lang_fn)(const char *); + +typedef struct Lang { + ccdjit_context *ctx; + ccdjit_module *module; + lang_fn encrypt; + lang_fn decrypt; +} Lang; + +typedef struct Crypt { + Ops *options; + Lang *lang; + char *input_text; + char *output_text; +} Crypt; + +Ops* create_ops(void); +Lang* lang_load(const char *name); +void lang_unload(Lang *lang); +Crypt* create_crypt(Ops *options, Lang *lang, const char *input_text); +int crypt_run(Crypt *crypt); +void free_crypt(Crypt *crypt); + +Ops* create_ops(void) { + Ops *ops = malloc(sizeof(Ops)); + if (!ops) return NULL; + ops->way = Encrypt; + ops->language = "english"; + return ops; +} + +// Only allow [a-z0-9_] so "../../etc/foo" can't escape langs/. +static int valid_lang_name(const char *name) { + if (!*name) return 0; + for (const char *p = name; *p; p++) + if (!islower((unsigned char)*p) && !isdigit((unsigned char)*p) && *p != '_') + return 0; + return 1; +} + +// Reads path and appends a stub main() so CCDJIT accepts it as a module. +static char *load_plugin_source(const char *path) { + static const char stub[] = "\nint main(void) { return 0; }\n"; + + FILE *f = fopen(path, "rb"); + if (!f) return NULL; + + if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; } + long n = ftell(f); + if (n < 0) { fclose(f); return NULL; } + rewind(f); + + char *buf = malloc((size_t)n + sizeof stub); + if (!buf) { fclose(f); return NULL; } + + if (fread(buf, 1, (size_t)n, f) != (size_t)n) { + free(buf); + fclose(f); + return NULL; + } + fclose(f); + + memcpy(buf + n, stub, sizeof stub); // includes the NUL terminator + return buf; +} + +Lang* lang_load(const char *name) { + if (!valid_lang_name(name)) { + fprintf(stderr, "Invalid language name: %s\n", name); + return NULL; + } + + Lang *lang = calloc(1, sizeof(Lang)); + if (!lang) return NULL; + + ccdjit_options opts = { + .allow_filesystem_includes = 1, + .allow_system_includes = 1, + .allow_executable_memory = 1, + }; + lang->ctx = ccdjit_context_new(&opts); + if (!lang->ctx) goto fail; + ccdjit_context_add_include_path(lang->ctx, "."); // finds lang.h + + char path[256]; + snprintf(path, sizeof path, "langs/%s.c", name); + + char *src = load_plugin_source(path); + if (!src) { + fprintf(stderr, "Cannot read %s\n", path); + goto fail; + } + + if (ccdjit_compile_string(lang->ctx, src, path, &lang->module) != 0) { + const ccdjit_diagnostic *d = ccdjit_context_last_error(lang->ctx); + fprintf(stderr, "[%s] %s:%d:%d: %s\n", + d->phase, d->filename, d->line, d->column, d->message); + free(src); + goto fail; + } + free(src); + + lang->encrypt = (lang_fn)ccdjit_module_symbol(lang->module, "lang_encrypt"); + lang->decrypt = (lang_fn)ccdjit_module_symbol(lang->module, "lang_decrypt"); + if (!lang->encrypt || !lang->decrypt) { + fprintf(stderr, "%s must define lang_encrypt and lang_decrypt\n", path); + goto fail; + } + return lang; + +fail: + lang_unload(lang); + return NULL; +} + +void lang_unload(Lang *lang) { + if (!lang) return; + if (lang->module) ccdjit_module_free(lang->module); + if (lang->ctx) ccdjit_context_free(lang->ctx); + free(lang); +} + +Crypt* create_crypt(Ops *options, Lang *lang, const char *input_text) { + Crypt *crypt = calloc(1, sizeof(Crypt)); + if (!crypt) return NULL; + crypt->options = options; + crypt->lang = lang; + crypt->input_text = strdup(input_text); + if (!crypt->input_text) { free(crypt); return NULL; } + return crypt; +} + +int crypt_run(Crypt *crypt) { + lang_fn fn = crypt->options->way == Encrypt ? crypt->lang->encrypt + : crypt->lang->decrypt; + crypt->output_text = fn(crypt->input_text); + return crypt->output_text ? 0 : -1; +} + +void free_crypt(Crypt *crypt) { + if (!crypt) return; + free(crypt->options); + lang_unload(crypt->lang); + free(crypt->input_text); + free(crypt->output_text); + free(crypt); +} + +static void print_usage(const char *prog) { + fprintf(stderr, "Usage: %s [-e | -d] [-l ] \n", prog); +} + +int main(int argc, char **argv) { + Ops *ops = create_ops(); + if (!ops) return EXIT_FAILURE; + + const char *text = NULL; + for (int i = 1; i < argc; i++) { + if (strcmp(argv[i], "-e") == 0) ops->way = Encrypt; + else if (strcmp(argv[i], "-d") == 0) ops->way = Decrypt; + else if (strcmp(argv[i], "-l") == 0 && i + 1 < argc) ops->language = argv[++i]; + else if (argv[i][0] != '-' && !text) text = argv[i]; + else { print_usage(argv[0]); free(ops); return EXIT_FAILURE; } + } + if (!text) { print_usage(argv[0]); free(ops); return EXIT_FAILURE; } + + Lang *lang = lang_load(ops->language); + if (!lang) { free(ops); return EXIT_FAILURE; } + + Crypt *crypt = create_crypt(ops, lang, text); + if (!crypt) { lang_unload(lang); free(ops); return EXIT_FAILURE; } + + int rc = crypt_run(crypt); + if (rc == 0) printf("%s\n", crypt->output_text); + else fprintf(stderr, "Operation failed\n"); + + free_crypt(crypt); + return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/samples.txt b/samples.txt new file mode 100644 index 0000000..6730cd7 --- /dev/null +++ b/samples.txt @@ -0,0 +1,26 @@ +# language | plaintext | expected ciphertext (optional) +# +# english: plaintext must start with a capital and have no other capitals, +# and no '.' or '@' (decrypt capitalizes the first letter of the output). + +english|Hello world|ello@h.orld@w +english|This is me|is@th.s@i.e@m +english|Chinese tea is great|hinese@c.ea@t.s@i.reat@g +english|Shall we dance|all@sh.e@w.ance@d +english|Quick brown fox|uick@q.rown@b.ox@f +english|Phone home|hone@p.ome@h +english|What is love|hat@w.s@i.ove@l +english|I am here|@i.m@a.ere@h +english|Nice to meet you|ice@n.o@t.eet@m.ou@y +english|Good night moon|ood@g.ight@n.oon@m + +pinyin|chu1 ming2 yao4 chen4 zao3|u1ch.ing2m.ao4y.en4ch.ao3z +pinyin|ni3 hao3|i3n.ao3h +pinyin|zhong1 guo2|ong1zh.uo2g +pinyin|wo3 ai4 ni3|o3w.ai4.i3n +pinyin|xie4 xie5|ie4x.ie5x +pinyin|shi4 jie4|i4sh.ie4j +pinyin|zai4 jian4|ai4z.ian4j +pinyin|qing1 ting2|ing1q.ing2t +pinyin|dong1 feng1|ong1d.eng1f +pinyin|xue2 xi2 zhong1 wen2|ue2x.i2x.ong1zh.en2w -- cgit v1.2.3