strip unused functions, rc4, base64

This commit is contained in:
2025-06-13 14:16:02 +02:00
parent de65f383b3
commit a93274d8ac
4 changed files with 125 additions and 9 deletions

View File

@@ -11,7 +11,7 @@ func print_i64[x: I64] : I64
printf("%ld\n", x)
return 0
func String.is_whitespace[c: Char] : Bool
func String.is_whitespace[c: U8] : Bool
return c == 10 || c == 32 || c == 13 || c == 9
func String.concat[a: String, b: String] : String
@@ -20,6 +20,12 @@ func String.concat[a: String, b: String] : String
strcat(c, b)
return c
func String.find[s: String, needle: U8] : I64
for i in 0:strlen(s)
if String.nth(s, i) == needle
return i
return -1
func String.substr[s: String, start: I64, length: I64] : String
let out: String = malloc(length + 1)
strlcpy(out, s + start, length + 1)
@@ -42,8 +48,7 @@ func String.rev[s: String] : String
let out: String = malloc(len + 1)
for i in 0:len
let c: Char = String.nth(s, len - i - 1)
String.set(out, i, c)
String.set(out, i, String.nth(s, len - i - 1))
String.set(out, len, 0)
return out
@@ -72,7 +77,7 @@ func IO.write_file[path: String, content: String] : I64
fclose(file)
return 0
func Char.parse_i64[c: Char]: I64
func U8.parse_i64[c: U8]: I64
return c - 48
func I64.to_string[n: I64] : String
@@ -145,4 +150,71 @@ func Math.is_prime[n: I64]: I64
return true
func Array.new[] : Array
return calloc(1, 24)
return calloc(1, 24)
func Math.Crypto.rc4[key: String, plaintext: String]: String
let S: String = malloc(256)
for i in 0:256
String.set(S, i, i)
let j: I64 = 0
let key_len: I64 = strlen(key)
for i in 0:256
j = (j + String.nth(S, i) + String.nth(key, i % key_len)) % 256
let tmp: U8 = String.nth(S, i)
String.set(S, i, String.nth(S, j))
String.set(S, j, tmp)
let i: I64 = 0
j = 0
let plaintext_len: I64 = strlen(plaintext)
let ciphertext: String = malloc(plaintext_len+1)
for n in 0:plaintext_len
i = (i + 1) % 256
j = (j + String.nth(S, i)) % 256
let tmp: U8 = String.nth(S, i)
String.set(S, i, String.nth(S, j))
String.set(S, j, tmp)
let r: I64 = String.nth(S, (String.nth(S, i) + String.nth(S, j)) % 256)
String.set(ciphertext, n, r ^ String.nth(plaintext, n))
String.set(ciphertext, plaintext_len, 0)
free(S)
return ciphertext
func Math.Crypto.base64_encode[s: String] : String
let chars: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
let s_len: I64 = strlen(s)
let output: String = malloc(s_len*2)
let i: I64 = 0
let j: I64 = 0
while i < s_len
let b1: U8 = String.nth(s, i)
let b2: U8 = 0
if i + 1 < s_len
b2 = String.nth(s, i+1)
let b3: U8 = 0
if i + 2 < s_len
b3 = String.nth(s, i+2)
i = i + 3
let triple: I64 = Bit.or(Bit.or(Bit.lshift(b1, 16), Bit.lshift(b2, 8)), b3)
String.set(output, j, String.nth(chars, Bit.and(Bit.rshift(triple, 18), 63)))
String.set(output, j+1, String.nth(chars, Bit.and(Bit.rshift(triple, 12), 63)))
String.set(output, j+2, String.nth(chars, Bit.and(Bit.rshift(triple, 6), 63)))
String.set(output, j+3, String.nth(chars, Bit.and(triple, 63)))
j = j + 4
let padding: I64 = s_len % 3
let equals: U8 = String.nth("=", 0)
if padding == 1
String.set(output, j-2, equals)
String.set(output, j-1, equals)
else if padding == 2
String.set(output, j-1, equals)
String.set(output, j, 0)
return output