Skip to content

Commit

Permalink
add encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Coppertine committed Jul 5, 2023
0 parents commit ca64b27
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/*
.vscode/settings.json
15 changes: 15 additions & 0 deletions beet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Base64 Encode",
"author": "Coppertine",
"description": "Test for encoding text to b64",
"require": ["bolt","bolt_expressions"],
"data_pack": {
"load": ["."]
},
"pipeline": ["mecha"],
"meta": {
"bolt": {"entrypoint": "b64:main"}
},

"output": "build"
}
131 changes: 131 additions & 0 deletions data/b64/functions/encode.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from bolt_expressions import Scoreboard, Data
from ./math/bitwise/ops import lshift
storage = Data.storage(b64:convert)
score = Scoreboard("b64.tmp")
math_score = Scoreboard("b64.math")


def rshift(a,b):
math_score["$a"] = a
math_score["$b"] = b
function b64:math/bitwise/rshift
return math_score["$output"]


def bitwise_and(a,b):
math_score["$a"] = a
math_score["$b"] = b
function b64:math/bitwise/and
return math_score["$output"]

chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
storage.result = []
storage.padding = []
storage.tmp = []
storage.tmp_single = ""


# storage.input = ["h","e","l","l","o"," ","w","o","r","l","d"]
tellraw @a storage.input
storage.string = []
storage.tmp_section = []
storage.section = []

# generating the tree to grab the spesific character from the b64 char list
for node in generate_tree(range(len(chars)), name="get_char", root=(b64:get_b64_char)):
append function node.parent:
if node.partition(4):
if score var math_score["$input"] matches node.range run function node.children
else:
if score var math_score["$input"] matches node.value run data modify var storage.tmp_single set value chars[node.value]

# We are grabbing the string from b64:convert input

storage.string = storage.input
execute store result score var score["$length"] run data get var storage.string
# score["$length"] = storage.string
score["$chunk"] = score["$length"] % 3

# Add "=" and '' padding to ensure string is multiple of 3 characters
if score var score["$chunk"] matches 1.. run function b64:add_padding:
score["$iterator"] = score["$chunk"]
if score var score["$iterator"] matches ..3 run function b64:loop:
storage.padding.append('=')
storage.string.append('')
score["$iterator"] += 1
if score var score["$iterator"] matches ..3 run function b64:loop
# Debug to see if the padding is added...
tellraw @a "added padding"
tellraw @a storage.padding

score["$chunk"] = 0
if score var score["$chunk"] < var score["$length"] run function b64:string_increment:

storage.section = []
storage.tmp_section = storage.string
score["$chunk2"] = score["$chunk"] + 1
score["$i"] = 0
# tellraw @a score["$chunk"]
if score var score["$i"] < var score["$chunk"] run function b64:substr_chunk_start:
data remove var storage.tmp_section[0]
score["$i"] += 1
if score var score["$i"] < var score["$chunk"] run function b64:substr_chunk_start

score["$i"] = score["$length"]
if score var score["$i"] > var score["$chunk2"] run function b64:substr_chunk_end:
data remove var storage.tmp_section[-1]
score["$i"] -= 1
if score var score["$i"] > var score["$chunk2"] run function b64:substr_chunk_end
# We SHOULD have an array of just 3 elements.... I hope.. edit: yep
# tellraw @a storage.tmp_section

# TODO: Improve on this to use Scoreboards... storage is slow as hell and we are reading it twice
for x in range(3):
storage.input_single = storage.tmp_section[x]
function b64:get_bitcode
storage.section.append(storage.output_bitcode)
# tellraw @a storage.section

# [0] << 16, [1] << 8
storage.section[0] = lshift(storage.section[0], 16)
storage.section[1] = lshift(storage.section[1], 8)

score['$n'] = 0
score["$n"] = storage.section[0] + storage.section[1] + storage.section[2]

score["$n3"] = score["$n"]
score["$n0"] = score["$n"]
score["$n1"] = score["$n"]
score["$n2"] = score["$n"]

# ($n >> 18) & 63
score["$n3"] = bitwise_and(score["$n3"],63)
score["$n0"] = bitwise_and(rshift(score["$n0"], 18), 63)
score["$n1"] = bitwise_and(rshift(score["$n1"], 12), 63)
score["$n2"] = bitwise_and(rshift(score["$n2"], 6),63)
# tellraw @a [score['$n0'], ",", score['$n1'],",",score['$n2'],",",score['$n3']]

for x in [score["$n0"], score["$n1"], score["$n2"], score["$n3"]]:
math_score["$input"] = x
function b64:get_b64_char
storage.result.append(storage.tmp_single)

score["$chunk"] += 3
if score var score["$chunk"] < var score["$length"] run function b64:string_increment
# tellraw @a storage.result

# remove the fake padding, we need to start at 1 as somehow two pads are added...
execute store result score var score["$paddinglen"] run data get var storage.padding
score["$i"] = 1
if score var score["$i"] < var score["$paddinglen"] run function b64:substr_result_end:
data remove var storage.result[-1]
score["$i"] += 1
if score var score["$i"] < var score["$paddinglen"] run function b64:substr_result_end

score["$i"] = 1
if score var score["$i"] < var score["$paddinglen"] run function b64:padding_append:
storage.result.append("=")
score["$i"] = score["$i"] + 1
if score var score["$i"] < var score["$paddinglen"] run function b64:padding_append

tellraw @a [storage.result.component(interpret=true)]
14 changes: 14 additions & 0 deletions data/b64/functions/get_bitcode.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from bolt_expressions import Scoreboard, Data
from nbtlib import Byte
l = " !#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\"\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

storage = Data.storage(b64:convert)
# storage.input_single = 'a'
storage.output_bitcode = 0
# for node in generate_tree(range(len(l)), name='bitcode', key=int, root=('get_char_bitcode')):
# append function node.parent:
# execute if data storage b64:convert {"input_single":l[node.value]} run function node.children:
# storage.output_bitcode = ord(l[node.value])
for i in range(len(l)):
execute if data storage b64:convert {"input_single":l[i]} run function f"b64:bit/get_{i}_code":
storage.output_bitcode = ord(l[i])
25 changes: 25 additions & 0 deletions data/b64/functions/math/bitwise/and.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# inputs: $a, $b
# output: $output
# Credit: phi
# https://github.com/MinecraftPhi/MinecraftPhi-modules/tree/master/phi.bitwise/src/datapack/data/phi.bitwise/functions
from bolt_expressions import Scoreboard
from ../pow import pow
score = Scoreboard("b64.math")
# Apparently, phi attmempts to multiply a and b by 2 until it overflows.
output = score["$output"]
output = 0
i = 2147483647
while i > 0:
if i == 2147483647:
if score var score["$a"] matches ..-1 if score var score["$b"] matches ..-1 run scoreboard players set var output -2147483648
else:
score["$a"] *= 2
score["$b"] *= 2
itemp = i + 1
if score var score["$a"] matches ..-1 if score var score["$b"] matches ..-1 run scoreboard players add var output int(itemp)
i = int(i / 2)

# one more for 1
score["$a"] *= 2
score["$b"] *= 2
if score var score["$a"] matches ..-1 if score var score["$b"] matches ..-1 run scoreboard players add var output 1
12 changes: 12 additions & 0 deletions data/b64/functions/math/bitwise/ops.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from bolt_expressions import Scoreboard
from ../pow import pow
import sys

score = Scoreboard("b64.math")
# inputs: $a, $b
# output: $output
def lshift(a, b):
output = score["$output"]
output = a
output = output * pow(2, b)
return output
21 changes: 21 additions & 0 deletions data/b64/functions/math/bitwise/rshift.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# inputs: $a, $b
# output: $output
# Credit: phi
# https://github.com/MinecraftPhi/MinecraftPhi-modules/tree/master/phi.bitwise/src/datapack/data/phi.bitwise/functions
from bolt_expressions import Scoreboard
from ../pow import pow
score = Scoreboard("b64.math")
a = score["$a"]
b = score["$b"]
output = score["$output"]
if score var a matches 1.. run function b64:math/bitwise/rshift_sign_prop:
output = a
output = output / pow(2, b)
if score var a matches ..-1 run function b64:math/bitwise/rshift_zero_filling_neg:
# overflow to clear sign bit
a += 2147483647
a += 1
a = a / pow(2,b)
b -= 31
b *= -1
output = a + pow(2,b)
27 changes: 27 additions & 0 deletions data/b64/functions/math/pow.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Author: TheWii
# https://canary.discord.com/channels/900530660677156924/1057459352635719690/1057645088173461516

from bolt_expressions import Scoreboard, identifier_generator

identifiers = identifier_generator(ctx)

value = Scoreboard.objective("b64.math")

def pow(base, exp):
_base, _exp, _result = value["$pow_base", "$pow_exp", "$pow_result"]

_base = base
_exp = exp
_result = 1

if score var _exp matches 1.. function ./exp:
_result *= _base
_exp -= 1

if score var _exp matches 1..
function ./exp

_output = value["$" + next(identifiers)]
_output = _result

return _output
5 changes: 5 additions & 0 deletions data/b64/functions/test_bitwise.mcfunction
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from bolt_expressions import Scoreboard, Data
score = Scoreboard("b64.tmp")

score["$foo"] = 1
# score["$foo"] << 2

0 comments on commit ca64b27

Please sign in to comment.