From 54d94a1f3d04b738626968e62173822fdac638a3 Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 9 Feb 2026 00:29:50 +0100 Subject: [PATCH] Some stdlib refactoring removing duplicate code. (0.38.8) --- CMakeLists.txt | 2 +- lib/crypt/aes256.fun | 41 ++------------------- lib/crypt/crc32.fun | 41 ++------------------- lib/crypt/crc32c.fun | 41 ++------------------- lib/crypt/md5.fun | 41 ++------------------- lib/crypt/md5_legacy.fun | 37 +------------------ lib/crypt/ripemd160.fun | 41 ++------------------- lib/crypt/ripemd160_new_try.fun | 41 ++------------------- lib/crypt/sha1.fun | 41 ++------------------- lib/crypt/sha256.fun | 41 ++------------------- lib/crypt/sha384.fun | 63 +++------------------------------ lib/crypt/sha512.fun | 41 ++------------------- 12 files changed, 34 insertions(+), 437 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b09c23..3fa1c0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(fun VERSION 0.38.7 LANGUAGES C) +project(fun VERSION 0.38.8 LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/lib/crypt/aes256.fun b/lib/crypt/aes256.fun index 89c7767..3833399 100644 --- a/lib/crypt/aes256.fun +++ b/lib/crypt/aes256.fun @@ -22,49 +22,14 @@ * ct : 8ea2b7ca516745bfeafc49904b496089 */ +#include #include class AES256() // ---------- Hex helpers ---------- - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/crc32.fun b/lib/crypt/crc32.fun index 0759a90..c581b9c 100644 --- a/lib/crypt/crc32.fun +++ b/lib/crypt/crc32.fun @@ -23,6 +23,7 @@ // c = CRC32() // print(c.crc32_str("123456789")) +#include #include class CRC32() @@ -48,45 +49,9 @@ class CRC32() return band(this.u32(a), this.u32(b)) // hex helpers (mirroring style from lib/crypt/md5.fun) - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/crc32c.fun b/lib/crypt/crc32c.fun index 8a9cbd0..458c597 100644 --- a/lib/crypt/crc32c.fun +++ b/lib/crypt/crc32c.fun @@ -23,6 +23,7 @@ // c = CRC32C() // print(c.crc32c_hex("313233343536373839")) +#include #include class CRC32C() @@ -48,45 +49,9 @@ class CRC32C() return band(this.u32(a), this.u32(b)) // hex helpers (mirroring style from lib/crypt/md5.fun) - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/md5.fun b/lib/crypt/md5.fun index 41ab461..717b27a 100644 --- a/lib/crypt/md5.fun +++ b/lib/crypt/md5.fun @@ -20,6 +20,7 @@ // Class wrapper to avoid global name collisions and show class usage +#include #include class MD5() @@ -79,45 +80,9 @@ class MD5() return this.u32(this.u32(this.u32(a + b) + c) + d) // hex helpers - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/md5_legacy.fun b/lib/crypt/md5_legacy.fun index dd27ddc..9565430 100644 --- a/lib/crypt/md5_legacy.fun +++ b/lib/crypt/md5_legacy.fun @@ -9,6 +9,7 @@ * Added: 2025-10-01 */ +#include #include // Legacy global API and helpers. Please use the MD5 class (see lib/crypt/md5.fun). @@ -48,42 +49,6 @@ fun add32(a, b) fun add32_4(a, b, c, d) return u32(u32(u32(a + b) + c) + d) -fun hex_val(ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(hh) // hh is 2-char string hi = hex_val(substr(hh, 0, 1)) diff --git a/lib/crypt/ripemd160.fun b/lib/crypt/ripemd160.fun index bfb4d8b..773aed7 100644 --- a/lib/crypt/ripemd160.fun +++ b/lib/crypt/ripemd160.fun @@ -11,6 +11,7 @@ // THIS IS BROKEN ACTUALLY! IT DOES NOT CALCULATE THE EXPECTED HASHES! +#include #include class RIPEMD160() @@ -79,45 +80,9 @@ class RIPEMD160() else return this.u32(bxor(x, bor(y, bnot(z)))) - fun hex_val(this, ch) - if ch == "0" - return 0 - else if ch == "1" - return 1 - else if ch == "2" - return 2 - else if ch == "3" - return 3 - else if ch == "4" - return 4 - else if ch == "5" - return 5 - else if ch == "6" - return 6 - else if ch == "7" - return 7 - else if ch == "8" - return 8 - else if ch == "9" - return 9 - else if ch == "a" || ch == "A" - return 10 - else if ch == "b" || ch == "B" - return 11 - else if ch == "c" || ch == "C" - return 12 - else if ch == "d" || ch == "D" - return 13 - else if ch == "e" || ch == "E" - return 14 - else if ch == "f" || ch == "F" - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/ripemd160_new_try.fun b/lib/crypt/ripemd160_new_try.fun index 41ba167..9489f17 100644 --- a/lib/crypt/ripemd160_new_try.fun +++ b/lib/crypt/ripemd160_new_try.fun @@ -19,6 +19,7 @@ // rip.ripemd160_hex(hexStr) -> digest hex string (lowercase) // rip.ripemd160_str("abc") -> digest hex string of ASCII bytes +#include #include class RIPEMD160() @@ -53,45 +54,9 @@ class RIPEMD160() return bnot(this.u32(x)) // hex helpers (same as md5/sha files) - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/sha1.fun b/lib/crypt/sha1.fun index 41a048a..398a3a9 100644 --- a/lib/crypt/sha1.fun +++ b/lib/crypt/sha1.fun @@ -17,6 +17,7 @@ // sha.sha1_hex(hexStr) -> digest hex string (lowercase) // sha.sha1_str("abc") -> digest hex string of ASCII bytes +#include #include class SHA1() @@ -37,45 +38,9 @@ class SHA1() return rol(x, s) // hex helpers - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/sha256.fun b/lib/crypt/sha256.fun index f9743d5..04f102b 100644 --- a/lib/crypt/sha256.fun +++ b/lib/crypt/sha256.fun @@ -19,6 +19,7 @@ // Example: // print(SHA256().sha256_hex("616263")) // "abc" => ba7816bf... +#include #include class SHA256() @@ -86,45 +87,9 @@ class SHA256() ] // hex helpers - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - hi = this.hex_val(substr(hh, 0, 1)) - lo = this.hex_val(substr(hh, 1, 1)) + hi = hex_val(substr(hh, 0, 1)) + lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) diff --git a/lib/crypt/sha384.fun b/lib/crypt/sha384.fun index 2373a30..7696160 100644 --- a/lib/crypt/sha384.fun +++ b/lib/crypt/sha384.fun @@ -23,49 +23,14 @@ // cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163 // 1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7 +#include #include class SHA384() // -------- hex helpers -------- - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - number hi = this.hex_val(substr(hh, 0, 1)) - number lo = this.hex_val(substr(hh, 1, 1)) + number hi = hex_val(substr(hh, 0, 1)) + number lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex) @@ -79,24 +44,6 @@ class SHA384() i = i + 2 return arr - fun two_hex(this, n) - n = n % 256 - hexd = "0123456789abcdef" - number hi = (n / 16) % 16 - number lo = n % 16 - c1 = substr(hexd, hi, 1) - c2 = substr(hexd, lo, 1) - return join([c1, c2], "") - - fun bytes_to_hex(this, arr) - out = [] - number i = 0 - number L = len(arr) - while i < L - push(out, this.two_hex(arr[i])) - i = i + 1 - return join(out, "") - // -------- 32/64 helpers -------- fun u32(this, x) m = 4294967296 @@ -359,10 +306,10 @@ class SHA384() fun sha384_hex(this, hexStr) bytes = this.from_hex(hexStr) digest = this.sha384_bytes(bytes) - return this.bytes_to_hex(digest) + return bytes_to_hex(digest) // Hash raw string bytes (printable ASCII) fun sha384_str(this, str) bytes = string_to_bytes_ascii(str) digest = this.sha384_bytes(bytes) - return this.bytes_to_hex(digest) + return bytes_to_hex(digest) diff --git a/lib/crypt/sha512.fun b/lib/crypt/sha512.fun index eda388f..5401c0a 100644 --- a/lib/crypt/sha512.fun +++ b/lib/crypt/sha512.fun @@ -24,49 +24,14 @@ // ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a // 2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f +#include #include class SHA512() // -------- hex helpers -------- - fun hex_val(this, ch) - if (ch == "0") - return 0 - else if (ch == "1") - return 1 - else if (ch == "2") - return 2 - else if (ch == "3") - return 3 - else if (ch == "4") - return 4 - else if (ch == "5") - return 5 - else if (ch == "6") - return 6 - else if (ch == "7") - return 7 - else if (ch == "8") - return 8 - else if (ch == "9") - return 9 - else if (ch == "a" || ch == "A") - return 10 - else if (ch == "b" || ch == "B") - return 11 - else if (ch == "c" || ch == "C") - return 12 - else if (ch == "d" || ch == "D") - return 13 - else if (ch == "e" || ch == "E") - return 14 - else if (ch == "f" || ch == "F") - return 15 - else - return 0 - fun byte_from_hex_pair(this, hh) - number hi = this.hex_val(substr(hh, 0, 1)) - number lo = this.hex_val(substr(hh, 1, 1)) + number hi = hex_val(substr(hh, 0, 1)) + number lo = hex_val(substr(hh, 1, 1)) return hi * 16 + lo fun from_hex(this, hex)