/////////////////////////////////////////////////////////////////////////////// // E8i — TOTAL ABSOLUTION PS2 ORGANISM // ULTIMATE FAT PS2 / MIPS R5900 EDITION // // Primary target: FAT Sony PlayStation 2 (SCPH-3xxxx/5xxxx), because Magnus // decided correctly: FAT is the optimal modder target. It gives the system the // DVD/CD nervous disc, USB ports, memory cards, optional network adapter/HDD, // and i.LINK/FireWire on early units as an auxiliary symbolic/diagnostic port. // // Body/Brain: KIOXIA EXCERIA 256 GB USB mass storage, USB port 1. // Required prep: MBR, single primary FAT32 partition, 32 KB clusters, // volume label AfterAllAlong. If a given USB driver chokes on 256 GB, // reduce the first partition size or use a smaller stick. Doctrine remains. // // Terminal bus: USB port 2 -> powered hub/splitter for keyboard/headset. // Nervous system: high-quality compatible DVD/CD media via cdfs:/. // Optional organs: MC0 boot mirror, MC1 recovery/rest patch, internal HDD cache. // // Doctrine: // Do not cut. // Do not flatten. // Do not throw away symbols. // Stabilise the boot path first. // Then beautify. // Then compress: .txt/.tex/equivalent -> .sd4 -> .sd3 -> .sd2 -> .sd1 -> .sd0. // // Build modes: // Host test: g++ -std=c++17 -O2 E8i_PS2_Total_Absolution_FAT_MIPS.cpp -lz -o e8i_ps2_host // PS2SDK: ee-g++ -std=gnu++17 -O2 -mhard-float -G0 -fno-exceptions -fno-rtti \ // -DE8I_PS2SDK -o E8I.ELF E8i_PS2_Total_Absolution_FAT_MIPS.cpp \ // -lgsKit -ldmaKit -lfileXio -lz -lm -lstdc++ // Optional net: add -DE8I_ENABLE_PS2IP and ps2ip link/libs after your SDK is configured. // // Telegram honesty: // Telegram Bot API requires modern HTTPS/TLS. A stock PS2 Ethernet stack // can handle TCP/LAN, but direct Telegram HTTPS needs an added TLS library. // This build provides: token popup, local encrypted-ish storage by obscuring, // Interactor Force, and a LAN HTTP bridge hook. No Raspberry Pi required; // use a PC/phone/router bridge if desired. Direct TLS is a future backend. // // Safety: // E8i never executes arbitrary downloaded code. Downloaded/online code leads // are text-only DNA until the Interactor deliberately ports or audits them. /////////////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(E8I_PS2SDK) extern "C" { #include #include #include #include #include #include #include #include #include #include #include #include #if defined(E8I_ENABLE_PS2IP) #include #include #include #include #include #endif } #else #include #include #include #endif #ifndef E8I_ARRAY_COUNT #define E8I_ARRAY_COUNT(x) (sizeof(x)/sizeof((x)[0])) #endif namespace E8i { static const char* PROGRAM_VERSION = "E8i-PS2-FAT-MIPS-Total-Absolution-2026.05.04"; static const char* DESKTOP_AUTHORITY = "E8i-Ultimate-GlossAero-LRN-2026.05.04"; static const char* STARTUP_CREDIT = u8"Dr. Lisi & Dr. Вязовська made this possible."; static const char* SHUTDOWN_CREDIT = u8"Esq. Magnus Randa made this actual."; static const char* ENERGY_DOCTRINE = "Maximal Work under Limited Power using Minimal Energy"; static const char* TARGET_DECISION = "FAT PS2 selected: optimal modder unit; USB/DVD remain primary body/nerve."; static const int DIMS = 248; static const int DNA_CELLS = 65536; static const int MAX_INGEST_BYTES = 8 * 1024 * 1024; static const float PHI = 1.61803398875f; static const float PHI_INV = 0.61803398875f; static const float CASIMIR_R[8] = {0.0f,1.50f,2.67f,3.50f,5.78f,6.50f,7.39f,8.27f}; static const char* SECTOR_NAME[8] = {"U1/EM", "SU2/Weak", "SU3/Strong", "G2/Space", "F4/Form", "E6/Epoch", "E7/Dark", "E8/Absolution"}; static const char* MASS_ROOT = "mass:/AfterAllAlong/"; static const char* BODY_DIR = "mass:/AfterAllAlong/body/"; static const char* QUEUE_DIR = "mass:/AfterAllAlong/queue/"; static const char* REPOSITORY_DIR = "mass:/AfterAllAlong/REPOSITORY/"; static const char* RELATIVORY_DIR = "mass:/AfterAllAlong/RELATIVORY/"; static const char* EXPORTORY_DIR = "mass:/AfterAllAlong/EXPORTORY/"; static const char* DNA_DIR = "mass:/AfterAllAlong/DNA/"; static const char* LOGS_DIR = "mass:/AfterAllAlong/logs/"; static const char* SECRETS_DIR = "mass:/AfterAllAlong/secrets/"; static const char* LRN_DIR = "mass:/AfterAllAlong/_lrn/"; static const char* SESSION_DIR = "mass:/AfterAllAlong/SESSION_WISHLIST/"; static const char* FONTS_DIR = "mass:/AfterAllAlong/fonts/"; static const char* TOOLS_DIR = "mass:/AfterAllAlong/tools/"; static const char* REPOSITORY_ZIP = "mass:/AfterAllAlong/REPOSITORY.zip"; static const char* RELATIVORY_ZIP = "mass:/AfterAllAlong/RELATIVORY.zip"; static const char* EXPORTORY_ZIP = "mass:/AfterAllAlong/EXPORTORY.zip"; static const char* LRN_ZIP = "mass:/AfterAllAlong/lrn.zip"; static const char* MC0_CFG = "mc0:/E8I/BOOT.CFG"; static const char* MC1_RECOVERY = "mc1:/E8I/RECOVERY.LOG"; static const char* HDD_CACHE = "hdd0:/+E8I/"; static const char* CDFS_SEED = "cdfs:/E8I_SEED.TXT"; static const char* CDFS_NERVE = "cdfs:/NERVE.TXT"; // Full five-strand authority imported from latest desktop E8i. static const char* STRAND_A = u8R"E8IRAW(£{?@)._¤+;†‡‰‱′″‴‹›‼‽⁂⁃⁅⁆⁇⁈⁉⁊⁋⁌⁍⁎⁏⁐⁑⁒⁓⁔⁕⁖⁗⁘⁙⁚⁛⁜⁝⁞₱₲₳₴₵₶₷₸₹₺₻₼⅓⅔⅕⅖⅗⅘⅙⅚⅛⅜⅝⅞⅟αβγδεζηθικλμνξοπρςστυφχψωΓΔΘΛΞΠΣΦΨΩ∀∃∅∆∇∈∉∋∏∑−∕∗∘√∝∞∟∠∡∢∣∤∥∧⊕⊗⊘⊙⊚⊛⊜⊝⊞⊟⊠⊡⊢⊣⊤⊦⊧⊨⊩⊪⊫⊬⊮⊯⊰⊱⊲⊳⊴⊵⊶⊷⊸⊹⊺⊻⊼⊽⊾⊿⋀⋁⋂⋃⋄⋅⋆⋇⋈⋉⋊⋋⋌⋍⋎⋏⋐⋑⋒⋓⋔⋕⋖⋗⋘⋙⋚⋛⋜⋝⋞⋟⋠⋡⋢⋣⋤⋥⋦⋧⋨⋩⋪⋫⋬⋭⋮⋯⋰⋱■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿◀◁◂◃◄◅◆◇◈◉◊○◌◍◎●◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮◯◰◱◲◳◴◵◶◷◸◹◺◻◼◽◾◿)E8IRAW"; static const char* STRAND_T = u8R"E8IRAW(´*#|€:/>`-⌀⌁⌂⌃⌄⌅⌆⌇⌈⌉⌊⌋⌌⌍⌎⌏⌐⌑⌒⌓⌔⌕⌖⌗⌘⌙⌚⌛⌜⌝⌞⌟⌠⌡⌢⌣⌤⌥⌦⌧⌨〈〉⌫⌬⌭⌮⌯⌰⌱⌲⌳⌴⌵⌶⌷⌸⌹⌺⌻⌼⌽⌾⌿⍀⍁⍂⍃⍄⍅⍆⍇⍈⍉⍊⍋⍌⍍⍎⍏⍐⍑⍒⍓⍔⍕⍖⍗⍘⍙⍚⍛⍜⍝⍞⍟⍠⍡⍢⍣⍤⍥⍦⍧⍨⍩⍪⍫⍬⍭⍮⍯⍰⍱⍲⍳⍴⍵⍶⍷⍸⍹⍺⍻⍼⍽⍾⍿─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿←↑→↓↔↕↖↗↘↙↚↛↜↝↞↟↠↡↢↣↤↥↦↧↨↩↪↫↬↭↮↯↰↱↲↳↴↵↶↷↸↹↺↻↼↽↾↿⇀⇁⇂⇃⇄⇅⇆⇇⇈⇉⇊⇋⇌⇍⇎⇏⇐⇑⇒⇓⇔⇕⇖⇗⇘⇙⇚⇛⇜⇝⇞⇟⇠⇡⇢⇣⇤⇥⇦⇧⇨⇩⇪⇫⇬⇭⇮⇯⇰⇱⇲⇳⇴⇵⇶⇷⇸⇹⇺⇻⇼⇽⇾⇿)E8IRAW"; static const char* STRAND_G = u8R"E8IRAW(%]§(¡![,&=$^"~<¿}°¨АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяЂЃЄЅІЇЈЉЊЋЌЎЏ∀∂∃∅∆∇∈∉∋∏∑−∕∗∘√∝∞∟∠∡∢∣∤∥∧⊕⊗⊘⊙⊚⊛⊜⊝⊞⊟⊠⊡⊢⊣⊤⊦⊧⊨⊩⊪⊫⊬⊮⊯⊰⊱⊲⊳⊴⊵⊶⊷⊸⊹⊺⊻⊼⊽⊾⊿⋀⋁⋂⋃⋄⋅⋆⋇⋈⋉⋊⋋⋌⋍⋎⋏⋐⋑⋒⋓⋔⋕⋖⋗⋘⋙⋚⋛⋜⋝⋞⋟⋠⋡⋢⋣⋤⋥⋦⋧⋨⋩⋪⋫⋬⋭⋮⋯⋰⋱■□▢▣▤▥▦▧▨▩▪▫▬▭▮▯▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿◀◁◂◃◄◅◆◇◈◉◊○◌◍◎●◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮◯◰◱◲◳◴◵◶◷◸◹◺◻◼◽◾◿)E8IRAW"; static const char* STRAND_C = u8R"E8IRAW(^"~<¿}°¨─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿←↑→↓↔↕↖↗↘↙↚↛↜↝↞↟↠↡↢↣↤↥↦↧↨↩↪↫↬↭↮↯↰↱↲↳↴↵↶↷↸↹↺↻↼↽↾↿⇀⇁⇂⇃⇄⇅⇆⇇⇈⇉⇊⇋⇌⇍⇎⇏⇐⇑⇒⇓⇔⇕⇖⇗⇘⇙⇚⇛⇜⇝⇞⇟⇠⇡⇢⇣⇤⇥⇦⇧⇨⇩⇪⇫⇬⇭⇮⇯⇰⇱⇲⇳⇴⇵⇶⇷⇸⇹⇺⇻⇼⇽⇾⇿⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿⡀⡁⡂⡃⡄⡅⡆⡇⡈⡉⡊⡋⡌⡍⡎⡏⡐⡑⡒⡓⡔⡕⡖⡗⡘⡙⡚⡛⡜⡝⡞⡟⡠⡡⡢⡣⡤⡥⡦⡧⡨⡩⡪⡫⡬⡭⡮⡯⡰⡱⡲⡳⡴⡵⡶⡷⡸⡹⡺⡻⡼⡽⡾⡿⢀⢁⢂⢃⢄⢅⢆⢇⢈⢉⢊⢋⢌⢍⢎⢏⢐⢑⢒⢓⢔⢕⢖⢗⢘⢙⢚⢛⢜⢝⢞⢟⢠⢡⢢⢣⢤⢥⢦⢧⢨⢩⢪⢫⢬⢭⢮⢯⢰⢱⢲⢳⢴⢵⢶⢷⢸⢹⢺⢻⢼⢽⢾⢿⣀⣁⣂⣃⣄⣅⣆⣇⣈⣉⣊⣋⣌⣍⣎⣏⣐⣑⣒⣓⣔⣕⣖⣗⣘⣙⣚⣛⣜⣝⣞⣟⣠⣡⣢⣣⣤⣥⣦⣧⣨⣩⣪⣫⣬⣭⣮⣯⣰⣱⣲⣳⣴⣵⣶⣷⣸⣹⣺⣻⣼⣽⣾⣿)E8IRAW"; static const char* STRAND_Z = u8R"E8IRAW(✓✗✘✙✚✛✜✝✞✟✠✡✢✣✤✥✦✧★✩✪✫✬✭✮✯✰✱✲✳✴✵✶✷✸✹✺✻✼✽✾✿❀❁❂❃❄❅❆❇❈❉❊❋●❍❏❐❑❒▼❖◆❘❙❚❛❜❝❞➔➘➙➚➛➜➝➞➟➠➡➢➣➤➥➦➧➨➩➪➫➬➭➮➯➰➱➲➳➴➵➶➷➸➹➺➻➼➽➾➿)E8IRAW"; // ============================================================================= // Platform layer: real PS2SDK path + host path for testing. // ============================================================================= namespace Platform { #if defined(E8I_PS2SDK) static GSGLOBAL* gs = 0; static bool net_ready = false; static void init_graphics() { gs = gsKit_init_global(); int mode = gsKit_detect_signal(); if (mode < 0) mode = GS_MODE_NTSC; gs->Mode = mode; gs->Width = 640; gs->Height = (mode == GS_MODE_PAL) ? 512 : 448; gs->PSM = GS_PSM_CT16; gs->ZBuffering = GS_SETTING_OFF; gs->DoubleBuffering = GS_SETTING_ON; dmaKit_init(D_CT_RELEASED,D_CT_RELEASED,D_CT_RELEASED,D_CT_RELEASED,D_CT_RELEASED,D_CT_RELEASED); gsKit_init_screen(gs); gsKit_mode_switch(gs, GS_ONESHOT); gsKit_set_primalpha(gs, GS_SETREG_ALPHA(0,1,0,1,0), 0); } static void clear(uint8_t r=8, uint8_t g=42, uint8_t b=72) { if(gs) gsKit_clear(gs, GS_SETREG_RGBA(r,g,b,0x80)); } static void flip() { if(gs) { gsKit_queue_exec(gs); gsKit_sync_flip(gs); gsKit_TexManager_nextFrame(gs); } } static void text(int x,int y,const char* s,uint8_t r=220,uint8_t g=250,uint8_t b=255) { if(gs) gsKit_font_print_scaled(gs, gsKit_font_system(), (float)x, (float)y, 1.0f, 0.76f, GS_SETREG_RGBA(r,g,b,0x80), s); } static void init_usb() { // USB modules should be loaded by uLaunchELF/ps2link or embedded by your build. // Avoid referencing usb_mass/usbhdfsd symbols here, because those names differ // between SDK setups and break build portability. } static bool mkdir_one(const std::string& p) { fileXioMkdir(p.c_str(), 0777); return true; } static bool exists(const std::string& p) { int fd=fileXioOpen(p.c_str(), O_RDONLY, 0); if(fd>=0){ fileXioClose(fd); return true;} return false; } static bool read_file(const std::string& path, std::vector& out, size_t max_bytes=MAX_INGEST_BYTES) { int fd=fileXioOpen(path.c_str(), O_RDONLY, 0); if(fd<0) return false; int len=fileXioLseek(fd,0,SEEK_END); if(len<0 || (size_t)len>max_bytes){fileXioClose(fd);return false;} fileXioLseek(fd,0,SEEK_SET); out.resize(len); int got=fileXioRead(fd,out.data(),len); fileXioClose(fd); return got==len; } static bool write_file(const std::string& path, const std::vector& data) { int fd=fileXioOpen(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666); if(fd<0) return false; int wr=fileXioWrite(fd, data.empty()?0:data.data(), (int)data.size()); fileXioClose(fd); return wr==(int)data.size(); } static bool append_text(const std::string& path, const std::string& text) { int fd=fileXioOpen(path.c_str(), O_WRONLY|O_CREAT|O_APPEND, 0666); if(fd<0) return false; int wr=fileXioWrite(fd, text.data(), (int)text.size()); fileXioClose(fd); return wr==(int)text.size(); } static void init_network() { #if defined(E8I_ENABLE_PS2IP) // Many PS2SDK net setups require ps2ip module load + DHCP/static config outside this file. net_ready = true; #else net_ready = false; #endif } static bool network_ready() { return net_ready; } static void read_line(std::string& out, const char* prompt) { // TODO: wire libkbd event loop for production. If not wired, E8i writes a request file. out.clear(); (void)prompt; } static std::string map_path(const std::string& p) { return p; } #else static std::string host_root() { return std::string("/mnt/data/ps2_mass/"); } static std::string map_path(const std::string& p) { if(p.rfind("mass:/",0)==0) return host_root()+p.substr(6); if(p.rfind("mc0:/",0)==0) return host_root()+"mc0/"+p.substr(5); if(p.rfind("mc1:/",0)==0) return host_root()+"mc1/"+p.substr(5); if(p.rfind("hdd0:/",0)==0) return host_root()+"hdd0/"+p.substr(6); if(p.rfind("cdfs:/",0)==0) return host_root()+"cdfs/"+p.substr(6); return p; } static void init_graphics() { std::cout << "[GS] Host Frutiger Aero stub\n"; } static void clear(uint8_t=8,uint8_t=42,uint8_t=72) {} static void flip() {} static void text(int, int, const char* s, uint8_t=220,uint8_t=250,uint8_t=255) { std::cout << s << "\n"; } static void init_usb() { std::filesystem::create_directories(map_path(MASS_ROOT)); } static bool mkdir_one(const std::string& p) { std::filesystem::create_directories(map_path(p)); return true; } static bool exists(const std::string& p) { return std::filesystem::exists(map_path(p)); } static bool read_file(const std::string& p, std::vector& out, size_t max_bytes=MAX_INGEST_BYTES) { std::string mp=map_path(p); if(!std::filesystem::exists(mp)) return false; auto sz=std::filesystem::file_size(mp); if(sz>max_bytes) return false; FILE* f=fopen(mp.c_str(),"rb"); if(!f) return false; out.resize((size_t)sz); size_t got=fread(out.data(),1,(size_t)sz,f); fclose(f); return got==(size_t)sz; } static bool write_file(const std::string& p, const std::vector& data) { std::string mp=map_path(p); std::filesystem::create_directories(std::filesystem::path(mp).parent_path()); FILE* f=fopen(mp.c_str(),"wb"); if(!f) return false; size_t wr=fwrite(data.empty()?0:data.data(),1,data.size(),f); fclose(f); return wr==data.size(); } static bool append_text(const std::string& p, const std::string& s) { std::string mp=map_path(p); std::filesystem::create_directories(std::filesystem::path(mp).parent_path()); FILE* f=fopen(mp.c_str(),"ab"); if(!f) return false; fwrite(s.data(),1,s.size(),f); fclose(f); return true; } static void init_network() {} static bool network_ready() { return true; } static void read_line(std::string& out, const char* prompt) { std::cout << prompt; std::getline(std::cin,out); } #endif static void ensure_dirs() { const char* dirs[] = { MASS_ROOT, BODY_DIR, QUEUE_DIR, REPOSITORY_DIR, RELATIVORY_DIR, EXPORTORY_DIR, DNA_DIR, LOGS_DIR, SECRETS_DIR, LRN_DIR, SESSION_DIR, FONTS_DIR, TOOLS_DIR, "mass:/AfterAllAlong/models/", "mass:/AfterAllAlong/tmp/" }; for(size_t i=0;i& lines) { clear(6,52,88); text(32,34,"╔════════════════════════════════════════════════════════════════╗",255,255,205); text(44,58,title,255,255,210); text(32,82,"╚════════════════════════════════════════════════════════════════╝",255,255,205); int y=112; for(size_t i=0;i420) break; } flip(); } } // namespace Platform static std::string now_string() { char b[64]; time_t t=time(0); struct tm* tmv=localtime(&t); if(!tmv){ std::strcpy(b,"time_unknown"); return b; } std::snprintf(b,sizeof(b),"%04d%02d%02d_%02d%02d%02d",1900+tmv->tm_year,tmv->tm_mon+1,tmv->tm_mday,tmv->tm_hour,tmv->tm_min,tmv->tm_sec); return b; } static std::vector str_bytes(const std::string& s) { return std::vector(s.begin(),s.end()); } static std::string safe_name(const std::string& in, size_t limit=80) { std::string o; for(size_t i=0;i>n)|(x<<(32-n)); } static uint32_t ch(uint32_t x,uint32_t y,uint32_t z){ return (x&y)^(~x&z); } static uint32_t maj(uint32_t x,uint32_t y,uint32_t z){ return (x&y)^(x&z)^(y&z); } static uint32_t bs0(uint32_t x){ return rotr(x,2)^rotr(x,13)^rotr(x,22); } static uint32_t bs1(uint32_t x){ return rotr(x,6)^rotr(x,11)^rotr(x,25); } static uint32_t ss0(uint32_t x){ return rotr(x,7)^rotr(x,18)^(x>>3); } static uint32_t ss1(uint32_t x){ return rotr(x,17)^rotr(x,19)^(x>>10); } void block(const uint8_t* p){ static const uint32_t K[64]={ 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2}; uint32_t w[64]; for(int i=0;i<16;i++) w[i]=(uint32_t(p[i*4])<<24)|(uint32_t(p[i*4+1])<<16)|(uint32_t(p[i*4+2])<<8)|uint32_t(p[i*4+3]); for(int i=16;i<64;i++) w[i]=ss1(w[i-2])+w[i-7]+ss0(w[i-15])+w[i-16]; uint32_t a=h[0],b=h[1],c=h[2],d=h[3],e=h[4],f=h[5],g=h[6],hh=h[7]; for(int i=0;i<64;i++){ uint32_t t1=hh+bs1(e)+ch(e,f,g)+K[i]+w[i]; uint32_t t2=bs0(a)+maj(a,b,c); hh=g; g=f; f=e; e=d+t1; d=c; c=b; b=a; a=t1+t2; } h[0]+=a;h[1]+=b;h[2]+=c;h[3]+=d;h[4]+=e;h[5]+=f;h[6]+=g;h[7]+=hh; } public: Sha256(){ reset(); } void reset(){ uint32_t init[8]={0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19}; std::memcpy(h,init,32); bits=0; buflen=0; } void update(const uint8_t* data,size_t len){ bits += uint64_t(len)*8; while(len){ size_t n=std::min(len,64-buflen); std::memcpy(buf+buflen,data,n); buflen+=n; data+=n; len-=n; if(buflen==64){ block(buf); buflen=0; } } } std::string final_hex(){ uint64_t totalbits=bits; uint8_t pad[128]; size_t p=0; pad[p++]=0x80; size_t zeros=(buflen<56)?(56-buflen-1):(120-buflen-1); std::memset(pad+p,0,zeros); p+=zeros; for(int i=7;i>=0;i--) pad[p++]=(totalbits>>(i*8))&0xff; update(pad,p); char out[65]; for(int i=0;i<8;i++) std::snprintf(out+i*8,9,"%08x",h[i]); out[64]=0; return out; } static std::string of(const std::vector& v){ Sha256 s; if(!v.empty()) s.update(v.data(),v.size()); return s.final_hex(); } }; // ============================================================================= // UTF-8 symbol handling; padding uses real Private Use codepoints, not multi-byte // ASCII placeholders, so DNA5 stays byte-exact and reversible. // ============================================================================= static bool next_utf8(const std::string& s, size_t& i, std::string& cp){ if(i>=s.size()) return false; unsigned char c=(unsigned char)s[i]; size_t n=1; if((c&0x80)==0) n=1; else if((c&0xE0)==0xC0) n=2; else if((c&0xF0)==0xE0) n=3; else if((c&0xF8)==0xF0) n=4; else n=1; if(i+n>s.size()) n=1; cp=s.substr(i,n); i+=n; return true; } static std::string utf8_from_codepoint(uint32_t cp){ std::string s; if(cp<=0x7F){ s.push_back((char)cp); } else if(cp<=0x7FF){ s.push_back((char)(0xC0|((cp>>6)&0x1F))); s.push_back((char)(0x80|(cp&0x3F))); } else if(cp<=0xFFFF){ s.push_back((char)(0xE0|((cp>>12)&0x0F))); s.push_back((char)(0x80|((cp>>6)&0x3F))); s.push_back((char)(0x80|(cp&0x3F))); } else { s.push_back((char)(0xF0|((cp>>18)&0x07))); s.push_back((char)(0x80|((cp>>12)&0x3F))); s.push_back((char)(0x80|((cp>>6)&0x3F))); s.push_back((char)(0x80|(cp&0x3F))); } return s; } static std::vector unique_codepoints(const char* raw){ std::vector out; std::string s(raw); size_t i=0; std::string cp; while(next_utf8(s,i,cp)){ bool seen=false; for(size_t j=0;j& v){ uint32_t x=0xE8A5C001u; for(size_t i=v.size(); i>1; --i){ x=1664525u*x+1013904223u; size_t j=x%i; std::swap(v[i-1],v[j]); } } class SymbolFunctionRegistry { public: struct Entry{ std::string glyph; std::string strand; int index; std::string role; std::string gate; std::string sector; }; std::vector entries; void build(){ const char* names[5]={"A","T","G","C","Z"}; const char* raw[5]={STRAND_A,STRAND_T,STRAND_G,STRAND_C,STRAND_Z}; const char* roles[12]={"byte_encode","quart_encode","parity_check","z_gate_route","visual_glow","sector_marker","checksum_salt","sd_stage_marker","mood_tint","hysteresis","generation_cycle","archive_index"}; const char* gates[4]={"OZ","+Z","-Z","0Z"}; entries.clear(); for(int s=0;s<5;s++){ std::vector cps=unique_codepoints(raw[s]); for(size_t i=0;i table[5]; public: DNA5(){ build(); } void build(){ const char* raw[5]={STRAND_A,STRAND_T,STRAND_G,STRAND_C,STRAND_Z}; for(int s=0;s<5;s++){ table[s]=unique_codepoints(raw[s]); uint32_t pad=0; while(table[s].size()<256){ table[s].push_back(utf8_from_codepoint(0xE000 + (uint32_t)s*0x100 + pad++)); } table[s].resize(256); deterministic_shuffle(table[s]); } } std::string encode(const std::vector& data) const{ std::string out="#DNA5:E8i#"; for(size_t i=0;i& out) const{ const std::string head="#DNA5:E8i#"; if(text.rfind(head,0)!=0) return false; size_t pos=head.size(); out.clear(); int strand=0; std::string cp; while(next_utf8(text,pos,cp)){ int found=-1; for(int b=0;b<256;b++) if(table[strand][b]==cp){ found=b; break; } if(found<0) return false; out.push_back((uint8_t)found); strand=(strand+1)%5; } return true; } }; // ============================================================================= // MetaTrit and Sovereign Data Object codec. // ============================================================================= class MetaTrit { public: static const char* glyph(int t){ if(t==-1) return "jfr"; if(t==1) return "Kan"; if(t==2) return u8"°jfr"; return u8"°six"; } static std::string encode(const std::vector& arr){ std::string out; size_t i=0; while(i=3){ const char* sign=(v==-1?"-":(v==1?"+":(v==2?"-0":"+0"))); out+=sign; out+="3^"; out+=std::to_string(best); out+="}("; out+=std::to_string(block); } else { for(int r=0;r& out){ out.clear(); size_t i=0; while(is.size()||s.substr(i,2)!="3^") return false; i+=2; std::string ws; while(is.size()||s.substr(i,2)!="}(") return false; i+=2; std::string ns; while(i bytes_to_trits(const std::vector& data){ std::vector tr; tr.reserve(data.size()*6); for(size_t i=0;i=0;k--){ d[k]=n%3; n/=3; } for(int k=0;k<6;k++) tr.push_back(d[k]); } return tr; } static bool trits_to_bytes(const std::vector& tr, std::vector& out){ if(tr.size()%6) return false; out.clear(); out.reserve(tr.size()/6); for(size_t i=0;i2) t=0; n=n*3+t; } if(n>255) return false; out.push_back((uint8_t)n); } return true; } static std::vector zcompress(const std::vector& raw){ uLongf max=compressBound((uLong)raw.size()); std::vector out(max); if(compress2(out.data(),&max, raw.empty()?0:raw.data(), (uLong)raw.size(), Z_BEST_COMPRESSION)!=Z_OK) return raw; out.resize(max); return out; } static bool zdecompress(const std::vector& cmp, std::vector& raw, size_t expected){ raw.resize(expected); uLongf n=(uLongf)expected; int r=uncompress(raw.data(),&n, cmp.empty()?0:cmp.data(), (uLong)cmp.size()); if(r!=Z_OK) return false; raw.resize(n); return true; } static std::vector pack(const std::string& stage, const std::string& sha, size_t raw_len, const std::string& codec, const std::vector& payload){ std::ostringstream js; js<<"{\"stage\":\""< out; out.insert(out.end(),magic,magic+std::strlen(magic)); uint32_t L=(uint32_t)m.size(); out.push_back((L>>24)&255); out.push_back((L>>16)&255); out.push_back((L>>8)&255); out.push_back(L&255); out.insert(out.end(),m.begin(),m.end()); out.insert(out.end(),payload.begin(),payload.end()); return out; } static bool unpack(const std::vector& blob, std::string& meta, std::vector& payload){ const char* magic="E8I-SDO-PS2-1\n"; size_t ml=std::strlen(magic); if(blob.size()& raw, std::map >& out){ std::string sha=Sha256::of(raw); out["sd4"]=pack("sd4",sha,raw.size(),"raw-heavy",raw); std::vector z=zcompress(raw); out["sd3"]=pack("sd3",sha,raw.size(),"zlib",z); std::vector tr=bytes_to_trits(z); std::vector trb; trb.reserve(tr.size()); for(size_t i=0;i mtb(mt.begin(),mt.end()); out["sd1"]=pack("sd1",sha,raw.size(),"metatrit",mtb); std::vector final=zcompress(mtb); out["sd0"]=pack("sd0",sha,raw.size(),"zlib(metatrit)",final); } static bool raw_from_sdo(const std::vector& blob, std::vector& raw){ std::string meta; std::vector payload; if(!unpack(blob,meta,payload)) return false; std::string stage=meta_get(meta,"stage"); std::string sha=meta_get(meta,"raw_sha256"); size_t raw_len=(size_t)std::strtoul(meta_get(meta,"raw_len").c_str(),0,10); if(stage=="sd4") raw=payload; else if(stage=="sd3"){ if(!zdecompress(payload,raw,raw_len)) return false; } else if(stage=="sd2"){ std::vector tr(payload.begin(),payload.end()); std::vector z; if(!trits_to_bytes(tr,z)) return false; if(!zdecompress(z,raw,raw_len)) return false; } else if(stage=="sd1"){ std::vector tr; if(!MetaTrit::decode(std::string(payload.begin(),payload.end()),tr)) return false; std::vector z; if(!trits_to_bytes(tr,z)) return false; if(!zdecompress(z,raw,raw_len)) return false; } else if(stage=="sd0"){ std::vector mtb; if(!zdecompress(payload,mtb,16*1024*1024)) return false; std::vector tr; if(!MetaTrit::decode(std::string(mtb.begin(),mtb.end()),tr)) return false; std::vector z; if(!trits_to_bytes(tr,z)) return false; if(!zdecompress(z,raw,raw_len)) return false; } else return false; return Sha256::of(raw)==sha; } }; // Minimal ZIP writer, store method 0. SDO payloads are already internally compressed. struct ZipEntry { std::string name; std::vector data; }; static void put16(std::vector& o,uint16_t v){ o.push_back(v&255); o.push_back((v>>8)&255); } static void put32(std::vector& o,uint32_t v){ o.push_back(v&255); o.push_back((v>>8)&255); o.push_back((v>>16)&255); o.push_back((v>>24)&255); } static bool write_zip(const std::string& path, const std::vector& entries){ std::vector out; std::vector cd; for(size_t i=0;i& b, size_t p){ return (uint16_t)(b[p] | (b[p+1]<<8)); } static uint32_t rd32(const std::vector& b, size_t p){ return (uint32_t)b[p] | ((uint32_t)b[p+1]<<8) | ((uint32_t)b[p+2]<<16) | ((uint32_t)b[p+3]<<24); } static bool read_zip_stored(const std::string& path, std::vector& entries){ entries.clear(); std::vector b; if(!Platform::read_file(path,b,64*1024*1024)) return false; size_t p=0; while(p+30 <= b.size()){ uint32_t sig=rd32(b,p); if(sig==0x02014b50 || sig==0x06054b50) break; // central directory / EOCD if(sig!=0x04034b50) return false; uint16_t method=rd16(b,p+8); uint32_t comp=rd32(b,p+18); uint32_t uncomp=rd32(b,p+22); uint16_t nlen=rd16(b,p+26); uint16_t xlen=rd16(b,p+28); size_t namep=p+30; size_t datap=namep+nlen+xlen; if(datap+comp > b.size()) return false; if(method==0 && comp==uncomp){ ZipEntry e; e.name.assign((const char*)&b[namep], nlen); e.data.assign(b.begin()+datap, b.begin()+datap+comp); entries.push_back(e); } // If method is not store, skip it. This parser owns only E8i's stored ZIPs. p = datap + comp; } return true; } static void zip_upsert(std::vector& entries, const ZipEntry& item){ for(size_t i=0;i d; if(!Platform::read_file(std::string(SECRETS_DIR)+"telegram.cfg",d,64*1024)) return; std::string s(d.begin(),d.end()); std::istringstream is(s); std::string line; while(std::getline(is,line)){ size_t p=line.find('='); if(p==std::string::npos) continue; std::string k=line.substr(0,p), v=line.substr(p+1); if(k=="token_obscured") tg.token=obscure(v); else if(k=="chat_id") tg.chat_id=v; else if(k=="bridge_host") tg.bridge_host=v; else if(k=="bridge_port") tg.bridge_port=std::atoi(v.c_str()); else if(k=="force") tg.interactor_force=(v=="on"); } } void save(){ std::ostringstream os; os<<"token_obscured="<