Some more stdlib refactoring removing duplicate code. (0.38.9)
This commit is contained in:
parent
54d94a1f3d
commit
794a2354d6
11 changed files with 18 additions and 112 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.10)
|
cmake_minimum_required(VERSION 3.10)
|
||||||
project(fun VERSION 0.38.8 LANGUAGES C)
|
project(fun VERSION 0.38.9 LANGUAGES C)
|
||||||
|
|
||||||
set(CMAKE_C_STANDARD 99)
|
set(CMAKE_C_STANDARD 99)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
|
|
||||||
|
|
@ -27,33 +27,20 @@
|
||||||
|
|
||||||
class AES256()
|
class AES256()
|
||||||
// ---------- Hex helpers ----------
|
// ---------- Hex helpers ----------
|
||||||
fun byte_from_hex_pair(this, hh)
|
|
||||||
hi = hex_val(substr(hh, 0, 1))
|
|
||||||
lo = hex_val(substr(hh, 1, 1))
|
|
||||||
return hi * 16 + lo
|
|
||||||
|
|
||||||
fun from_hex(this, hex)
|
fun from_hex(this, hex)
|
||||||
arr = []
|
arr = []
|
||||||
i = 0
|
i = 0
|
||||||
n = len(hex)
|
n = len(hex)
|
||||||
while i + 1 < n
|
while i + 1 < n
|
||||||
push(arr, this.byte_from_hex_pair(substr(hex, i, 2)))
|
push(arr, byte_from_hex_pair(substr(hex, i, 2)))
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = n % 256
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
hi = n / 16
|
|
||||||
lo = n % 16
|
|
||||||
parts = [d[hi], d[lo]]
|
|
||||||
return join(parts, "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
fun bytes_to_hex(this, arr)
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(arr)
|
while i < len(arr)
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,34 +49,21 @@ class CRC32()
|
||||||
return band(this.u32(a), this.u32(b))
|
return band(this.u32(a), this.u32(b))
|
||||||
|
|
||||||
// hex helpers (mirroring style from lib/crypt/md5.fun)
|
// hex helpers (mirroring style from lib/crypt/md5.fun)
|
||||||
fun byte_from_hex_pair(this, hh)
|
|
||||||
hi = hex_val(substr(hh, 0, 1))
|
|
||||||
lo = hex_val(substr(hh, 1, 1))
|
|
||||||
return hi * 16 + lo
|
|
||||||
|
|
||||||
fun from_hex(this, hex)
|
fun from_hex(this, hex)
|
||||||
arr = []
|
arr = []
|
||||||
i = 0
|
i = 0
|
||||||
n = len(hex)
|
n = len(hex)
|
||||||
while i + 1 < n
|
while i + 1 < n
|
||||||
b = this.byte_from_hex_pair(substr(hex, i, 2))
|
b = byte_from_hex_pair(substr(hex, i, 2))
|
||||||
push(arr, b)
|
push(arr, b)
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = n % 256
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
hi = n / 16
|
|
||||||
lo = n % 16
|
|
||||||
parts = [d[hi], d[lo]]
|
|
||||||
return join(parts, "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
fun bytes_to_hex(this, arr)
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(arr)
|
while i < len(arr)
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -49,44 +49,23 @@ class CRC32C()
|
||||||
return band(this.u32(a), this.u32(b))
|
return band(this.u32(a), this.u32(b))
|
||||||
|
|
||||||
// hex helpers (mirroring style from lib/crypt/md5.fun)
|
// hex helpers (mirroring style from lib/crypt/md5.fun)
|
||||||
fun byte_from_hex_pair(this, hh)
|
|
||||||
hi = hex_val(substr(hh, 0, 1))
|
|
||||||
lo = hex_val(substr(hh, 1, 1))
|
|
||||||
return hi * 16 + lo
|
|
||||||
|
|
||||||
fun from_hex(this, hex)
|
fun from_hex(this, hex)
|
||||||
arr = []
|
arr = []
|
||||||
i = 0
|
i = 0
|
||||||
n = len(hex)
|
n = len(hex)
|
||||||
while i + 1 < n
|
while i + 1 < n
|
||||||
b = this.byte_from_hex_pair(substr(hex, i, 2))
|
b = byte_from_hex_pair(substr(hex, i, 2))
|
||||||
push(arr, b)
|
push(arr, b)
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = n % 256
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
hi = n / 16
|
|
||||||
lo = n % 16
|
|
||||||
parts = [d[hi], d[lo]]
|
|
||||||
return join(parts, "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
|
||||||
i = 0
|
|
||||||
out = []
|
|
||||||
while i < len(arr)
|
|
||||||
push(out, this.two_hex(arr[i]))
|
|
||||||
i = i + 1
|
|
||||||
return join(out, "")
|
|
||||||
|
|
||||||
fun u32_to_hex8(this, n)
|
fun u32_to_hex8(this, n)
|
||||||
// big-endian printing (MSB first), common for CRC displays
|
// big-endian printing (MSB first), common for CRC displays
|
||||||
b3 = this.and32(this.shr32(n, 24), 255)
|
b3 = this.and32(this.shr32(n, 24), 255)
|
||||||
b2 = this.and32(this.shr32(n, 16), 255)
|
b2 = this.and32(this.shr32(n, 16), 255)
|
||||||
b1 = this.and32(this.shr32(n, 8), 255)
|
b1 = this.and32(this.shr32(n, 8), 255)
|
||||||
b0 = this.and32(n, 255)
|
b0 = this.and32(n, 255)
|
||||||
return this.bytes_to_hex([b3, b2, b1, b0])
|
return bytes_to_hex([b3, b2, b1, b0])
|
||||||
|
|
||||||
// Bitwise update (no table needed) using reflected polynomial 0x82F63B78
|
// Bitwise update (no table needed) using reflected polynomial 0x82F63B78
|
||||||
POLY = 2197175160 // 0x82F63B78
|
POLY = 2197175160 // 0x82F63B78
|
||||||
|
|
|
||||||
|
|
@ -80,34 +80,21 @@ class MD5()
|
||||||
return this.u32(this.u32(this.u32(a + b) + c) + d)
|
return this.u32(this.u32(this.u32(a + b) + c) + d)
|
||||||
|
|
||||||
// hex helpers
|
// hex helpers
|
||||||
fun byte_from_hex_pair(this, hh)
|
|
||||||
hi = hex_val(substr(hh, 0, 1))
|
|
||||||
lo = hex_val(substr(hh, 1, 1))
|
|
||||||
return hi * 16 + lo
|
|
||||||
|
|
||||||
fun from_hex(this, hex)
|
fun from_hex(this, hex)
|
||||||
arr = []
|
arr = []
|
||||||
i = 0
|
i = 0
|
||||||
n = len(hex)
|
n = len(hex)
|
||||||
while i + 1 < n
|
while i + 1 < n
|
||||||
b = this.byte_from_hex_pair(substr(hex, i, 2))
|
b = byte_from_hex_pair(substr(hex, i, 2))
|
||||||
push(arr, b)
|
push(arr, b)
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = n % 256
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
hi = n / 16
|
|
||||||
lo = n % 16
|
|
||||||
parts = [d[hi], d[lo]]
|
|
||||||
return join(parts, "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
fun bytes_to_hex(this, arr)
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(arr)
|
while i < len(arr)
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,16 +94,11 @@ class RIPEMD160()
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = band(n, 0xFF)
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
return join([d[shr(n, 4)], d[band(n, 0xF)]], "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
fun bytes_to_hex(this, arr)
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(arr)
|
while i < len(arr)
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,19 +69,11 @@ class RIPEMD160()
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = n % 256
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
hi = n / 16
|
|
||||||
lo = n % 16
|
|
||||||
parts = [d[hi], d[lo]]
|
|
||||||
return join(parts, "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
fun bytes_to_hex(this, arr)
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(arr)
|
while i < len(arr)
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,19 +53,11 @@ class SHA1()
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = n % 256
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
hi = n / 16
|
|
||||||
lo = n % 16
|
|
||||||
parts = [d[hi], d[lo]]
|
|
||||||
return join(parts, "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
fun bytes_to_hex(this, arr)
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(arr)
|
while i < len(arr)
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -102,19 +102,11 @@ class SHA256()
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
return arr
|
||||||
|
|
||||||
fun two_hex(this, n)
|
|
||||||
n = n % 256
|
|
||||||
d = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
|
|
||||||
hi = n / 16
|
|
||||||
lo = n % 16
|
|
||||||
parts = [d[hi], d[lo]]
|
|
||||||
return join(parts, "")
|
|
||||||
|
|
||||||
fun bytes_to_hex(this, arr)
|
fun bytes_to_hex(this, arr)
|
||||||
i = 0
|
i = 0
|
||||||
out = []
|
out = []
|
||||||
while i < len(arr)
|
while i < len(arr)
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -45,21 +45,12 @@ class SHA512()
|
||||||
i = i + 2
|
i = i + 2
|
||||||
return arr
|
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)
|
fun bytes_to_hex(this, arr)
|
||||||
out = []
|
out = []
|
||||||
number i = 0
|
number i = 0
|
||||||
number L = len(arr)
|
number L = len(arr)
|
||||||
while i < L
|
while i < L
|
||||||
push(out, this.two_hex(arr[i]))
|
push(out, two_hex(arr[i]))
|
||||||
i = i + 1
|
i = i + 1
|
||||||
return join(out, "")
|
return join(out, "")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,10 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Hex utility functions
|
// Hex utility functions
|
||||||
|
fun byte_from_hex_pair(hh)
|
||||||
|
hi = hex_val(substr(hh, 0, 1))
|
||||||
|
lo = hex_val(substr(hh, 1, 1))
|
||||||
|
return hi * 16 + lo
|
||||||
|
|
||||||
fun hex_val(ch)
|
fun hex_val(ch)
|
||||||
if (ch == "0")
|
if (ch == "0")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue