class Base64
class Base64
lib/base64.tya:2
Base64 provides the base64/Base64 standard library API.
Source
# Base64 provides the base64/Base64 standard library API.
class Base64
private ALPHABET: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
# Base64.decode provides the base64/Base64 standard library operation.
# @param text String text value.
# @return String the resulting value.
static decode: text ->
cleaned = ""
n = text.byte_len()
cr = chr(13)
i = 0
while i < n
c = text[i]
if c != " " and c != "\n" and c != cr and c != "\t" and c != "="
cleaned = cleaned + c
i = i + 1
m = cleaned.byte_len()
if m % 4 == 1
raise error("base64.decode: invalid length")
arr = []
j = 0
while j + 4 <= m
v1 = Self.decode_value(cleaned[j])
v2 = Self.decode_value(cleaned[j + 1])
v3 = Self.decode_value(cleaned[j + 2])
v4 = Self.decode_value(cleaned[j + 3])
arr.push(v1 * 4 + v2 / 16)
arr.push(v2 % 16 * 16 + v3 / 4)
arr.push(v3 % 4 * 64 + v4)
j = j + 4
rem = m - j
if rem == 2
v1 = Self.decode_value(cleaned[j])
v2 = Self.decode_value(cleaned[j + 1])
arr.push(v1 * 4 + v2 / 16)
elseif rem == 3
v1 = Self.decode_value(cleaned[j])
v2 = Self.decode_value(cleaned[j + 1])
v3 = Self.decode_value(cleaned[j + 2])
arr.push(v1 * 4 + v2 / 16)
arr.push(v2 % 16 * 16 + v3 / 4)
bytes(arr)
# Base64.encode provides the base64/Base64 standard library operation.
# @param value String value value.
# @return String the resulting value.
static encode: value ->
data = nil
if value.class == String
data = bytes_array(bytes_of(value))
else
data = bytes_array(value)
n = data.len()
out = ""
i = 0
while i + 3 <= n
b1 = data[i]
b2 = data[i + 1]
b3 = data[i + 2]
g1 = b1 / 4
g2 = b1 % 4 * 16 + b2 / 16
g3 = b2 % 16 * 4 + b3 / 64
g4 = b3 % 64
out = out
+ Self.ALPHABET[g1]
+ Self.ALPHABET[g2]
+ Self.ALPHABET[g3]
+ Self.ALPHABET[g4]
i = i + 3
rem = n - i
if rem == 1
b1 = data[i]
g1 = b1 / 4
g2 = b1 % 4 * 16
out = out + Self.ALPHABET[g1] + Self.ALPHABET[g2] + "=="
elseif rem == 2
b1 = data[i]
b2 = data[i + 1]
g1 = b1 / 4
g2 = b1 % 4 * 16 + b2 / 16
g3 = b2 % 16 * 4
out = out
+ Self.ALPHABET[g1]
+ Self.ALPHABET[g2]
+ Self.ALPHABET[g3]
+ "="
out
# Base64.decode_value returns the Base64 alphabet index for a character.
private static decode_value: c ->
alphabet = bytes_array(bytes_of(Self.ALPHABET))
target = bytes_array(bytes_of(c))[0]
i = 0
while i < alphabet.len()
if alphabet[i] == target
return i
i = i + 1
raise error("base64.decode: invalid character")
Static Methods
decode
static Base64.decode(text)
lib/base64.tya:8
Base64.decode provides the base64/Base64 standard library operation.
Source
# Base64.decode provides the base64/Base64 standard library operation.
# @param text String text value.
# @return String the resulting value.
static decode: text ->
cleaned = ""
n = text.byte_len()
cr = chr(13)
i = 0
while i < n
c = text[i]
if c != " " and c != "\n" and c != cr and c != "\t" and c != "="
cleaned = cleaned + c
i = i + 1
m = cleaned.byte_len()
if m % 4 == 1
raise error("base64.decode: invalid length")
arr = []
j = 0
while j + 4 <= m
v1 = Self.decode_value(cleaned[j])
v2 = Self.decode_value(cleaned[j + 1])
v3 = Self.decode_value(cleaned[j + 2])
v4 = Self.decode_value(cleaned[j + 3])
arr.push(v1 * 4 + v2 / 16)
arr.push(v2 % 16 * 16 + v3 / 4)
arr.push(v3 % 4 * 64 + v4)
j = j + 4
rem = m - j
if rem == 2
v1 = Self.decode_value(cleaned[j])
v2 = Self.decode_value(cleaned[j + 1])
arr.push(v1 * 4 + v2 / 16)
elseif rem == 3
v1 = Self.decode_value(cleaned[j])
v2 = Self.decode_value(cleaned[j + 1])
v3 = Self.decode_value(cleaned[j + 2])
arr.push(v1 * 4 + v2 / 16)
arr.push(v2 % 16 * 16 + v3 / 4)
bytes(arr)
encode
static Base64.encode(value)
lib/base64.tya:48
Base64.encode provides the base64/Base64 standard library operation.
Source
# Base64.encode provides the base64/Base64 standard library operation.
# @param value String value value.
# @return String the resulting value.
static encode: value ->
data = nil
if value.class == String
data = bytes_array(bytes_of(value))
else
data = bytes_array(value)
n = data.len()
out = ""
i = 0
while i + 3 <= n
b1 = data[i]
b2 = data[i + 1]
b3 = data[i + 2]
g1 = b1 / 4
g2 = b1 % 4 * 16 + b2 / 16
g3 = b2 % 16 * 4 + b3 / 64
g4 = b3 % 64
out = out
+ Self.ALPHABET[g1]
+ Self.ALPHABET[g2]
+ Self.ALPHABET[g3]
+ Self.ALPHABET[g4]
i = i + 3
rem = n - i
if rem == 1
b1 = data[i]
g1 = b1 / 4
g2 = b1 % 4 * 16
out = out + Self.ALPHABET[g1] + Self.ALPHABET[g2] + "=="
elseif rem == 2
b1 = data[i]
b2 = data[i + 1]
g1 = b1 / 4
g2 = b1 % 4 * 16 + b2 / 16
g3 = b2 % 16 * 4
out = out
+ Self.ALPHABET[g1]
+ Self.ALPHABET[g2]
+ Self.ALPHABET[g3]
+ "="
out