diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..9739a43 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,4 @@ +# Security Policy + +If you believe you have found a security issue in BFG, please responsibly disclose by contacting me at +[tristan.morgan@gmail.com](mailto:tristan.morgan@gmail.com). diff --git a/parser/tokenise.go b/parser/tokenise.go index 9f5382f..1160efa 100644 --- a/parser/tokenise.go +++ b/parser/tokenise.go @@ -60,27 +60,34 @@ func Tokenise(input io.ByteReader) (program []Instruction, err error) { pc = jmpPc program = program[:pc] program = append(program, Instruction{opSkip, offset}) - } else if pc-jmpPc == 5 && // [<<<+>>>-] or [-<<<+>>>] - program[pc-4].Complement(program[pc-2]) && - program[pc-3].Complement(program[pc-1]) { - offset := program[pc-4].operand - if program[pc-3].SameOp(NewInstruction('>')) { + } else if pc-jmpPc == 5 { // looking for opMulVal and opMove + var factor, offset = 0, 0 + if program[pc-4].Complement(NewInstruction('+')) && + program[pc-3].Complement(program[pc-1]) && + program[pc-2].SameOp(NewInstruction('+')) { // open with minus + + factor = program[pc-2].operand offset = program[pc-3].operand + + } else if program[pc-1].Complement(NewInstruction('+')) && + program[pc-4].Complement(program[pc-2]) && + program[pc-3].SameOp(NewInstruction('+')) { // close with minus + + factor = program[pc-3].operand + offset = program[pc-4].operand + + } + if factor == 1 { + pc = jmpPc + program = program[:pc] + program = append(program, Instruction{opMove, offset}) + } else if factor != 0 { + pc = jmpPc + program = program[:pc] + program = append(program, Instruction{opMulVal, offset}) + pc++ + program = append(program, Instruction{opNoop, factor}) } - pc = jmpPc - program = program[:pc] - program = append(program, Instruction{opMove, offset}) - } else if pc-jmpPc == 5 && // [<<++++>>-] - program[pc-4].Complement(program[pc-2]) && - program[pc-3].SameOp(NewInstruction('+')) && - program[pc-1].Complement(NewInstruction('+')) { - offset := program[pc-4].operand - factor := program[pc-3].operand - pc = jmpPc - program = program[:pc] - program = append(program, Instruction{opMulVal, offset}) - pc++ - program = append(program, Instruction{opNoop, factor}) } } pc++ diff --git a/parser/tokenise_test.go b/parser/tokenise_test.go index bcba2fc..acde61e 100644 --- a/parser/tokenise_test.go +++ b/parser/tokenise_test.go @@ -16,7 +16,7 @@ func TestTokenise(t *testing.T) { }{ { "small_prog", - ">>>>>[-]zero+++++>+++++[->>+<<]move>>", + ">>>>>[-]zero+++++>+++++[->>+<<]move>>[>>+<<-]move", []Instruction{ {opNoop, 0}, {opAddDp, 5}, @@ -25,15 +25,19 @@ func TestTokenise(t *testing.T) { {opAddVal, 5}, {opMove, 2}, {opAddDp, 2}, + {opMove, 2}, }, }, { "op_mul", - " [<++++++>-]", + " [<++++++>-]>[->>---<<]", []Instruction{ - Instruction{opNoop, 0}, - Instruction{opMulVal, -1}, // dest value pointer - Instruction{opNoop, 6}, // multiplication factor + {opNoop, 0}, + {opMulVal, -1}, // dest value pointer + {opNoop, 6}, // multiplication factor + {opAddDp, 1}, + {opMulVal, 2}, // dest value pointer + {opNoop, -3}, // multiplication factor }, }, {